2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-03-26 18:02:43 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2009-03-26 18:02:43 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2009-03-26 18:02:43 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2009-03-26 18:02:43 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2009-03-26 18:02:43 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-03-26 18:02:43 +01:00
|
|
|
|
|
|
|
|
#include <utils/savedaction.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/pathchooser.h>
|
2013-05-27 09:56:07 +02:00
|
|
|
#include <utils/pathlisteditor.h>
|
2009-03-26 18:02:43 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QSettings>
|
2009-03-26 18:02:43 +01:00
|
|
|
|
2015-05-29 11:06:10 +02:00
|
|
|
#include <QCheckBox>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QGroupBox>
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QSpinBox>
|
|
|
|
|
#include <QTextEdit>
|
2009-03-26 18:02:43 +01:00
|
|
|
|
2015-05-29 16:51:31 +02:00
|
|
|
namespace Utils {
|
2009-03-26 18:02:43 +01:00
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// SavedAction
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
/*!
|
2009-10-05 11:06:05 +02:00
|
|
|
\class Utils::SavedAction
|
2010-01-29 21:33:57 +01:00
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
\brief The SavedAction class is a helper class for actions with persistent
|
|
|
|
|
state.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-03-26 18:02:43 +01:00
|
|
|
SavedAction::SavedAction(QObject *parent)
|
|
|
|
|
: QAction(parent)
|
|
|
|
|
{
|
|
|
|
|
m_widget = 0;
|
2015-02-27 18:26:31 +01:00
|
|
|
connect(this, &QAction::triggered, this, &SavedAction::actionTriggered);
|
2009-03-26 18:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns the current value of the object.
|
|
|
|
|
|
|
|
|
|
\sa setValue()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
QVariant SavedAction::value() const
|
|
|
|
|
{
|
|
|
|
|
return m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the current value of the object. If the value changed and
|
|
|
|
|
\a doemit is true, the \c valueChanged() signal will be emitted.
|
|
|
|
|
|
|
|
|
|
\sa value()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::setValue(const QVariant &value, bool doemit)
|
|
|
|
|
{
|
|
|
|
|
if (value == m_value)
|
|
|
|
|
return;
|
|
|
|
|
m_value = value;
|
|
|
|
|
if (this->isCheckable())
|
|
|
|
|
this->setChecked(m_value.toBool());
|
|
|
|
|
if (doemit)
|
|
|
|
|
emit valueChanged(m_value);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns the default value to be used when the item does not exist yet
|
|
|
|
|
in the settings.
|
|
|
|
|
|
|
|
|
|
\sa setDefaultValue()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
QVariant SavedAction::defaultValue() const
|
|
|
|
|
{
|
|
|
|
|
return m_defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the default value to be used when the item does not exist yet
|
|
|
|
|
in the settings.
|
|
|
|
|
|
|
|
|
|
\sa defaultValue()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::setDefaultValue(const QVariant &value)
|
|
|
|
|
{
|
|
|
|
|
m_defaultValue = value;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns the key to be used when accessing the settings.
|
|
|
|
|
|
|
|
|
|
\sa settingsKey()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
QString SavedAction::settingsKey() const
|
|
|
|
|
{
|
|
|
|
|
return m_settingsKey;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the key to be used when accessing the settings.
|
|
|
|
|
|
|
|
|
|
\sa settingsKey()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::setSettingsKey(const QString &key)
|
|
|
|
|
{
|
|
|
|
|
m_settingsKey = key;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the key and group to be used when accessing the settings.
|
|
|
|
|
|
|
|
|
|
\sa settingsKey()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::setSettingsKey(const QString &group, const QString &key)
|
|
|
|
|
{
|
|
|
|
|
m_settingsKey = key;
|
|
|
|
|
m_settingsGroup = group;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the key to be used when accessing the settings.
|
|
|
|
|
|
|
|
|
|
\sa settingsKey()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
QString SavedAction::settingsGroup() const
|
|
|
|
|
{
|
|
|
|
|
return m_settingsGroup;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
/*!
|
|
|
|
|
Sets the group to be used when accessing the settings.
|
|
|
|
|
|
|
|
|
|
\sa settingsGroup()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::setSettingsGroup(const QString &group)
|
|
|
|
|
{
|
|
|
|
|
m_settingsGroup = group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SavedAction::toString() const
|
|
|
|
|
{
|
2009-05-29 10:35:04 +02:00
|
|
|
return QLatin1String("value: ") + m_value.toString()
|
|
|
|
|
+ QLatin1String(" defaultvalue: ") + m_defaultValue.toString()
|
|
|
|
|
+ QLatin1String(" settingskey: ") + m_settingsGroup
|
2012-01-04 17:34:08 +01:00
|
|
|
+ QLatin1Char('/') + m_settingsKey;
|
2009-03-26 18:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
/*
|
2010-01-29 21:33:57 +01:00
|
|
|
Uses \c settingsGroup() and \c settingsKey() to restore the
|
2009-03-27 17:20:53 +01:00
|
|
|
item from \a settings,
|
|
|
|
|
|
|
|
|
|
\sa settingsKey(), settingsGroup(), writeSettings()
|
|
|
|
|
*/
|
2009-11-24 15:05:02 +01:00
|
|
|
void SavedAction::readSettings(const QSettings *settings)
|
2009-03-26 18:02:43 +01:00
|
|
|
{
|
|
|
|
|
if (m_settingsGroup.isEmpty() || m_settingsKey.isEmpty())
|
|
|
|
|
return;
|
2009-11-24 15:05:02 +01:00
|
|
|
QVariant var = settings->value(m_settingsGroup + QLatin1Char('/') + m_settingsKey, m_defaultValue);
|
2009-06-26 13:40:16 +02:00
|
|
|
// work around old ini files containing @Invalid() entries
|
|
|
|
|
if (isCheckable() && !var.isValid())
|
|
|
|
|
var = false;
|
|
|
|
|
setValue(var);
|
2009-03-26 18:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
/*
|
2010-01-29 21:33:57 +01:00
|
|
|
Uses \c settingsGroup() and \c settingsKey() to write the
|
2009-03-27 17:20:53 +01:00
|
|
|
item to \a settings,
|
|
|
|
|
|
|
|
|
|
\sa settingsKey(), settingsGroup(), readSettings()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::writeSettings(QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
if (m_settingsGroup.isEmpty() || m_settingsKey.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
settings->beginGroup(m_settingsGroup);
|
|
|
|
|
settings->setValue(m_settingsKey, m_value);
|
|
|
|
|
settings->endGroup();
|
|
|
|
|
}
|
2010-01-29 21:33:57 +01:00
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
/*
|
|
|
|
|
A \c SavedAction can be connected to a widget, typically a
|
|
|
|
|
checkbox, radiobutton, or a lineedit in some configuration dialog.
|
|
|
|
|
|
2010-01-29 21:33:57 +01:00
|
|
|
The widget will retrieve its contents from the SavedAction's
|
2009-03-27 17:20:53 +01:00
|
|
|
value, and - depending on the \a ApplyMode - either write
|
|
|
|
|
changes back immediately, or when \s SavedAction::apply()
|
|
|
|
|
is called explicitly.
|
|
|
|
|
|
|
|
|
|
\sa apply(), disconnectWidget()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::connectWidget(QWidget *widget, ApplyMode applyMode)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!m_widget,
|
|
|
|
|
qDebug() << "ALREADY CONNECTED: " << widget << m_widget << toString(); return);
|
|
|
|
|
m_widget = widget;
|
2010-01-29 21:33:57 +01:00
|
|
|
|
2015-05-29 16:51:31 +02:00
|
|
|
if (auto button = qobject_cast<QCheckBox *>(widget)) {
|
2015-05-29 11:06:10 +02:00
|
|
|
if (!m_dialogText.isEmpty())
|
|
|
|
|
button->setText(m_dialogText);
|
|
|
|
|
button->setChecked(m_value.toBool());
|
2015-05-29 16:51:31 +02:00
|
|
|
if (applyMode == ImmediateApply) {
|
|
|
|
|
connect(button, &QCheckBox::clicked,
|
|
|
|
|
this, [this, button] { setValue(button->isChecked()); });
|
|
|
|
|
}
|
|
|
|
|
} else if (auto spinBox = qobject_cast<QSpinBox *>(widget)) {
|
2009-04-02 14:49:49 +02:00
|
|
|
spinBox->setValue(m_value.toInt());
|
2015-05-29 16:51:31 +02:00
|
|
|
if (applyMode == ImmediateApply) {
|
|
|
|
|
connect(spinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
|
|
|
|
|
this, [this, spinBox]() { setValue(spinBox->value()); });
|
|
|
|
|
}
|
|
|
|
|
} else if (auto lineEdit = qobject_cast<QLineEdit *>(widget)) {
|
2009-03-26 18:02:43 +01:00
|
|
|
lineEdit->setText(m_value.toString());
|
2015-05-29 16:51:31 +02:00
|
|
|
if (applyMode == ImmediateApply) {
|
|
|
|
|
connect(lineEdit, &QLineEdit::editingFinished,
|
|
|
|
|
this, [this, lineEdit] { setValue(lineEdit->text()); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (auto pathChooser = qobject_cast<PathChooser *>(widget)) {
|
2009-03-26 18:02:43 +01:00
|
|
|
pathChooser->setPath(m_value.toString());
|
2015-05-29 16:51:31 +02:00
|
|
|
if (applyMode == ImmediateApply) {
|
|
|
|
|
auto finished = [this, pathChooser] { setValue(pathChooser->path()); };
|
|
|
|
|
connect(pathChooser, &PathChooser::editingFinished, this, finished);
|
|
|
|
|
connect(pathChooser, &PathChooser::browsingFinished, this, finished);
|
|
|
|
|
}
|
|
|
|
|
} else if (auto groupBox = qobject_cast<QGroupBox *>(widget)) {
|
2009-11-24 15:05:02 +01:00
|
|
|
if (!groupBox->isCheckable())
|
|
|
|
|
qDebug() << "connectWidget to non-checkable group box" << widget << toString();
|
|
|
|
|
groupBox->setChecked(m_value.toBool());
|
2015-05-29 16:51:31 +02:00
|
|
|
if (applyMode == ImmediateApply) {
|
|
|
|
|
connect(groupBox, &QGroupBox::toggled,
|
|
|
|
|
this, [this, groupBox] { setValue(QVariant(groupBox->isChecked())); });
|
|
|
|
|
}
|
|
|
|
|
} else if (auto textEdit = qobject_cast<QTextEdit *>(widget)) {
|
2011-11-01 16:45:20 +01:00
|
|
|
textEdit->setPlainText(m_value.toString());
|
2015-05-29 16:51:31 +02:00
|
|
|
if (applyMode == ImmediateApply) {
|
|
|
|
|
connect(textEdit, &QTextEdit::textChanged,
|
|
|
|
|
this, [this, textEdit] { setValue(textEdit->toPlainText()); });
|
|
|
|
|
}
|
|
|
|
|
} else if (auto editor = qobject_cast<PathListEditor *>(widget)) {
|
2013-05-27 09:56:07 +02:00
|
|
|
editor->setPathList(m_value.toStringList());
|
2009-03-26 18:02:43 +01:00
|
|
|
} else {
|
|
|
|
|
qDebug() << "Cannot connect widget " << widget << toString();
|
|
|
|
|
}
|
2015-05-29 11:06:10 +02:00
|
|
|
|
|
|
|
|
// Copy tooltip, but only if there's nothing explcitly set on the widget yet.
|
|
|
|
|
if (widget->toolTip().isEmpty())
|
|
|
|
|
widget->setToolTip(toolTip());
|
2009-03-26 18:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-27 17:20:53 +01:00
|
|
|
/*
|
|
|
|
|
Disconnects the \c SavedAction from a widget.
|
|
|
|
|
|
|
|
|
|
\sa apply(), connectWidget()
|
|
|
|
|
*/
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::disconnectWidget()
|
|
|
|
|
{
|
|
|
|
|
m_widget = 0;
|
|
|
|
|
}
|
2009-10-01 12:11:50 +02:00
|
|
|
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::apply(QSettings *s)
|
|
|
|
|
{
|
2015-05-29 16:51:31 +02:00
|
|
|
if (auto button = qobject_cast<QCheckBox *>(m_widget))
|
2009-03-26 18:02:43 +01:00
|
|
|
setValue(button->isChecked());
|
2015-05-29 16:51:31 +02:00
|
|
|
else if (auto lineEdit = qobject_cast<QLineEdit *>(m_widget))
|
2009-03-26 18:02:43 +01:00
|
|
|
setValue(lineEdit->text());
|
2015-05-29 16:51:31 +02:00
|
|
|
else if (auto spinBox = qobject_cast<QSpinBox *>(m_widget))
|
2009-04-02 14:49:49 +02:00
|
|
|
setValue(spinBox->value());
|
2015-05-29 16:51:31 +02:00
|
|
|
else if (auto pathChooser = qobject_cast<PathChooser *>(m_widget))
|
2009-03-26 18:02:43 +01:00
|
|
|
setValue(pathChooser->path());
|
2015-05-29 16:51:31 +02:00
|
|
|
else if (auto groupBox = qobject_cast<QGroupBox *>(m_widget))
|
2009-11-24 15:05:02 +01:00
|
|
|
setValue(groupBox->isChecked());
|
2015-05-29 16:51:31 +02:00
|
|
|
else if (auto textEdit = qobject_cast<QTextEdit *>(m_widget))
|
2011-11-01 16:45:20 +01:00
|
|
|
setValue(textEdit->toPlainText());
|
2015-05-29 16:51:31 +02:00
|
|
|
else if (auto editor = qobject_cast<PathListEditor *>(m_widget))
|
2013-05-27 09:56:07 +02:00
|
|
|
setValue(editor->pathList());
|
2009-03-26 18:02:43 +01:00
|
|
|
if (s)
|
|
|
|
|
writeSettings(s);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 11:06:10 +02:00
|
|
|
/*
|
|
|
|
|
Default text to be used in labels if this SavedAction is
|
|
|
|
|
used in a settings dialog.
|
|
|
|
|
|
|
|
|
|
This typically is similar to the text this SavedAction shows
|
|
|
|
|
when used in menus, but differs in capitalization.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\sa text()
|
|
|
|
|
*/
|
|
|
|
|
QString SavedAction::dialogText() const
|
|
|
|
|
{
|
|
|
|
|
return m_dialogText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SavedAction::setDialogText(const QString &dialogText)
|
|
|
|
|
{
|
|
|
|
|
m_dialogText = dialogText;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-26 18:02:43 +01:00
|
|
|
void SavedAction::actionTriggered(bool)
|
|
|
|
|
{
|
|
|
|
|
if (isCheckable())
|
|
|
|
|
setValue(isChecked());
|
|
|
|
|
if (actionGroup() && actionGroup()->isExclusive()) {
|
|
|
|
|
// FIXME: should be taken care of more directly
|
|
|
|
|
foreach (QAction *act, actionGroup()->actions())
|
|
|
|
|
if (SavedAction *dact = qobject_cast<SavedAction *>(act))
|
|
|
|
|
dact->setValue(bool(act == this));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SavedAction::trigger(const QVariant &data)
|
|
|
|
|
{
|
|
|
|
|
setData(data);
|
|
|
|
|
QAction::trigger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// SavedActionSet
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void SavedActionSet::insert(SavedAction *action, QWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
m_list.append(action);
|
2009-10-01 11:14:48 +02:00
|
|
|
if (widget)
|
|
|
|
|
action->connectWidget(widget);
|
2009-03-26 18:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SavedActionSet::apply(QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
foreach (SavedAction *action, m_list)
|
|
|
|
|
action->apply(settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SavedActionSet::finish()
|
|
|
|
|
{
|
|
|
|
|
foreach (SavedAction *action, m_list)
|
|
|
|
|
action->disconnectWidget();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 16:51:31 +02:00
|
|
|
} // namespace Utils
|