forked from qt-creator/qt-creator
Utils: Remove savedaction.{h,cpp}
This was only used in the debugger and fakevim, its functionality is provided now by aspects. Change-Id: I8ec7b9d7693aa350bc65b08fca958afc434635a4 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -128,7 +128,6 @@ add_qtc_library(Utils
|
||||
reloadpromptutils.cpp reloadpromptutils.h
|
||||
removefiledialog.cpp removefiledialog.h removefiledialog.ui
|
||||
runextensions.cpp runextensions.h
|
||||
savedaction.cpp savedaction.h
|
||||
savefile.cpp savefile.h
|
||||
scopedswap.h
|
||||
set_algorithm.h
|
||||
|
@@ -1,323 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** 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
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <utils/savedaction.h>
|
||||
|
||||
#include "pathchooser.h"
|
||||
#include "pathlisteditor.h"
|
||||
#include "qtcassert.h"
|
||||
#include "qtcsettings.h"
|
||||
|
||||
#include <QActionGroup>
|
||||
#include <QCheckBox>
|
||||
#include <QDebug>
|
||||
#include <QGroupBox>
|
||||
#include <QLineEdit>
|
||||
#include <QSettings>
|
||||
#include <QSpinBox>
|
||||
#include <QTextEdit>
|
||||
|
||||
namespace Utils {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SavedAction
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*!
|
||||
\class Utils::SavedAction
|
||||
|
||||
\brief The SavedAction class is a helper class for actions with persistent
|
||||
state.
|
||||
*/
|
||||
|
||||
SavedAction::SavedAction(QObject *parent)
|
||||
{
|
||||
setParent(parent);
|
||||
connect(&m_action, &QAction::triggered, this, &SavedAction::actionTriggered);
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
Returns the current value of the object.
|
||||
|
||||
\sa setValue()
|
||||
*/
|
||||
QVariant SavedAction::value() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
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()
|
||||
*/
|
||||
void SavedAction::setValue(const QVariant &value, bool doemit)
|
||||
{
|
||||
if (value == m_value)
|
||||
return;
|
||||
m_value = value;
|
||||
if (m_action.isCheckable())
|
||||
m_action.setChecked(m_value.toBool());
|
||||
if (doemit)
|
||||
emit valueChanged(m_value);
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
Returns the default value to be used when the item does not exist yet
|
||||
in the settings.
|
||||
|
||||
\sa setDefaultValue()
|
||||
*/
|
||||
QVariant SavedAction::defaultValue() const
|
||||
{
|
||||
return m_defaultValue;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
Sets the default value to be used when the item does not exist yet
|
||||
in the settings.
|
||||
|
||||
\sa defaultValue()
|
||||
*/
|
||||
void SavedAction::setDefaultValue(const QVariant &value)
|
||||
{
|
||||
m_defaultValue = value;
|
||||
}
|
||||
|
||||
|
||||
QString SavedAction::toString() const
|
||||
{
|
||||
return QLatin1String("value: ") + m_value.toString()
|
||||
+ QLatin1String(" defaultvalue: ") + m_defaultValue.toString()
|
||||
+ QLatin1String(" settingskey: ") + settingsKey();
|
||||
}
|
||||
|
||||
/*
|
||||
Uses \c settingsGroup() and \c settingsKey() to restore the
|
||||
item from \a settings,
|
||||
|
||||
\sa settingsKey(), settingsGroup(), writeSettings()
|
||||
*/
|
||||
void SavedAction::readSettings(const QSettings *settings)
|
||||
{
|
||||
if (settingsKey().isEmpty())
|
||||
return;
|
||||
QVariant var = settings->value(settingsKey(), m_defaultValue);
|
||||
// work around old ini files containing @Invalid() entries
|
||||
if (m_action.isCheckable() && !var.isValid())
|
||||
var = false;
|
||||
setValue(var);
|
||||
}
|
||||
|
||||
/*
|
||||
Uses \c settingsGroup() and \c settingsKey() to write the
|
||||
item to \a settings,
|
||||
|
||||
\sa settingsKey(), settingsGroup(), readSettings()
|
||||
*/
|
||||
void SavedAction::writeSettings(QSettings *settings)
|
||||
{
|
||||
if (settingsKey().isEmpty())
|
||||
return;
|
||||
QtcSettings::setValueWithDefault(settings, settingsKey(), m_value, m_defaultValue);
|
||||
}
|
||||
|
||||
/*
|
||||
A \c SavedAction can be connected to a widget, typically a
|
||||
checkbox, radiobutton, or a lineedit in some configuration dialog.
|
||||
|
||||
The widget will retrieve its contents from the SavedAction's
|
||||
value, and - depending on the \a ApplyMode - either write
|
||||
changes back immediately, or when \s SavedAction::apply()
|
||||
is called explicitly.
|
||||
|
||||
\sa apply(), disconnectWidget()
|
||||
*/
|
||||
void SavedAction::connectWidget(QWidget *widget, ApplyMode applyMode)
|
||||
{
|
||||
QTC_ASSERT(!m_widget,
|
||||
qDebug() << "ALREADY CONNECTED: " << widget << m_widget << toString(); return);
|
||||
m_widget = widget;
|
||||
|
||||
if (auto button = qobject_cast<QCheckBox *>(widget)) {
|
||||
if (!m_dialogText.isEmpty())
|
||||
button->setText(m_dialogText);
|
||||
button->setChecked(m_value.toBool());
|
||||
if (applyMode == ImmediateApply) {
|
||||
connect(button, &QCheckBox::clicked,
|
||||
this, [this, button] { setValue(button->isChecked()); });
|
||||
}
|
||||
} else if (auto spinBox = qobject_cast<QSpinBox *>(widget)) {
|
||||
spinBox->setValue(m_value.toInt());
|
||||
if (applyMode == ImmediateApply) {
|
||||
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
|
||||
this, [this, spinBox]() { setValue(spinBox->value()); });
|
||||
}
|
||||
} else if (auto lineEdit = qobject_cast<QLineEdit *>(widget)) {
|
||||
lineEdit->setText(m_value.toString());
|
||||
if (applyMode == ImmediateApply) {
|
||||
connect(lineEdit, &QLineEdit::editingFinished,
|
||||
this, [this, lineEdit] { setValue(lineEdit->text()); });
|
||||
}
|
||||
|
||||
} else if (auto pathChooser = qobject_cast<PathChooser *>(widget)) {
|
||||
pathChooser->setPath(m_value.toString());
|
||||
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)) {
|
||||
if (!groupBox->isCheckable())
|
||||
qDebug() << "connectWidget to non-checkable group box" << widget << toString();
|
||||
groupBox->setChecked(m_value.toBool());
|
||||
if (applyMode == ImmediateApply) {
|
||||
connect(groupBox, &QGroupBox::toggled,
|
||||
this, [this, groupBox] { setValue(QVariant(groupBox->isChecked())); });
|
||||
}
|
||||
} else if (auto textEdit = qobject_cast<QTextEdit *>(widget)) {
|
||||
textEdit->setPlainText(m_value.toString());
|
||||
if (applyMode == ImmediateApply) {
|
||||
connect(textEdit, &QTextEdit::textChanged,
|
||||
this, [this, textEdit] { setValue(textEdit->toPlainText()); });
|
||||
}
|
||||
} else if (auto editor = qobject_cast<PathListEditor *>(widget)) {
|
||||
editor->setPathList(m_value.toStringList());
|
||||
} else {
|
||||
qDebug() << "Cannot connect widget " << widget << toString();
|
||||
}
|
||||
|
||||
// Copy tooltip, but only if there's nothing explcitly set on the widget yet.
|
||||
if (widget->toolTip().isEmpty())
|
||||
widget->setToolTip(m_action.toolTip());
|
||||
}
|
||||
|
||||
/*
|
||||
Disconnects the \c SavedAction from a widget.
|
||||
|
||||
\sa apply(), connectWidget()
|
||||
*/
|
||||
void SavedAction::disconnectWidget()
|
||||
{
|
||||
m_widget = nullptr;
|
||||
}
|
||||
|
||||
void SavedAction::apply(QSettings *s)
|
||||
{
|
||||
if (auto button = qobject_cast<QCheckBox *>(m_widget))
|
||||
setValue(button->isChecked());
|
||||
else if (auto lineEdit = qobject_cast<QLineEdit *>(m_widget))
|
||||
setValue(lineEdit->text());
|
||||
else if (auto spinBox = qobject_cast<QSpinBox *>(m_widget))
|
||||
setValue(spinBox->value());
|
||||
else if (auto pathChooser = qobject_cast<PathChooser *>(m_widget))
|
||||
setValue(pathChooser->path());
|
||||
else if (auto groupBox = qobject_cast<QGroupBox *>(m_widget))
|
||||
setValue(groupBox->isChecked());
|
||||
else if (auto textEdit = qobject_cast<QTextEdit *>(m_widget))
|
||||
setValue(textEdit->toPlainText());
|
||||
else if (auto editor = qobject_cast<PathListEditor *>(m_widget))
|
||||
setValue(editor->pathList());
|
||||
if (s)
|
||||
writeSettings(s);
|
||||
}
|
||||
|
||||
/*
|
||||
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;
|
||||
}
|
||||
|
||||
void SavedAction::actionTriggered(bool)
|
||||
{
|
||||
if (m_action.isCheckable())
|
||||
setValue(m_action.isChecked());
|
||||
if (m_action.actionGroup() && m_action.actionGroup()->isExclusive()) {
|
||||
// FIXME: should be taken care of more directly
|
||||
const QList<QAction *> actions = m_action.actionGroup()->actions();
|
||||
for (QAction *act : actions)
|
||||
if (auto dact = qobject_cast<SavedAction *>(act))
|
||||
dact->setValue(bool(act == &m_action));
|
||||
}
|
||||
}
|
||||
|
||||
QAction *SavedAction::action()
|
||||
{
|
||||
return &m_action;
|
||||
}
|
||||
|
||||
void SavedAction::trigger(const QVariant &data)
|
||||
{
|
||||
m_action.setData(data);
|
||||
m_action.trigger();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SavedActionSet
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void SavedActionSet::insert(SavedAction *action, QWidget *widget)
|
||||
{
|
||||
m_list.append(action);
|
||||
if (widget)
|
||||
action->connectWidget(widget);
|
||||
}
|
||||
|
||||
void SavedActionSet::apply(QSettings *settings)
|
||||
{
|
||||
for (SavedAction *action : qAsConst(m_list))
|
||||
action->apply(settings);
|
||||
}
|
||||
|
||||
void SavedActionSet::finish()
|
||||
{
|
||||
for (SavedAction *action : qAsConst(m_list))
|
||||
action->disconnectWidget();
|
||||
}
|
||||
|
||||
} // namespace Utils
|
@@ -1,106 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** 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
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "utils_global.h"
|
||||
|
||||
#include "aspects.h"
|
||||
|
||||
#include <QAction>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QSettings;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Utils {
|
||||
|
||||
enum ApplyMode { ImmediateApply, DeferedApply };
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT SavedAction : public BaseAspect
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
SavedAction(QObject *parent = nullptr);
|
||||
|
||||
QVariant value() const;
|
||||
void setValue(const QVariant &value, bool doemit = true);
|
||||
|
||||
QVariant defaultValue() const;
|
||||
void setDefaultValue(const QVariant &value);
|
||||
|
||||
void trigger(const QVariant &data);
|
||||
|
||||
virtual void readSettings(const QSettings *settings);
|
||||
virtual void writeSettings(QSettings *settings);
|
||||
|
||||
void connectWidget(QWidget *widget, ApplyMode applyMode = DeferedApply);
|
||||
void disconnectWidget();
|
||||
void apply(QSettings *settings);
|
||||
|
||||
QString toString() const;
|
||||
|
||||
QString dialogText() const;
|
||||
void setDialogText(const QString &dialogText);
|
||||
|
||||
QAction *action();
|
||||
|
||||
void setText(const QString &text) { m_action.setText(text); }
|
||||
void setToolTip(const QString &toolTip) { m_action.setToolTip(toolTip); }
|
||||
void setCheckable(bool checkable) { m_action.setCheckable(checkable); }
|
||||
void setChecked(bool checked) { m_action.setChecked(checked); }
|
||||
void setEnabled(bool enabled) { m_action.setEnabled(enabled); }
|
||||
void setIcon(const QIcon &icon) { m_action.setIcon(icon); }
|
||||
|
||||
signals:
|
||||
void valueChanged(const QVariant &newValue);
|
||||
|
||||
private:
|
||||
void actionTriggered(bool);
|
||||
|
||||
QVariant m_value;
|
||||
QVariant m_defaultValue;
|
||||
QString m_dialogText;
|
||||
QWidget *m_widget = nullptr;
|
||||
QAction m_action;
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT SavedActionSet
|
||||
{
|
||||
public:
|
||||
SavedActionSet() = default;
|
||||
~SavedActionSet() = default;
|
||||
|
||||
void insert(SavedAction *action, QWidget *widget);
|
||||
void apply(QSettings *settings);
|
||||
void finish();
|
||||
void clear() { m_list.clear(); }
|
||||
|
||||
private:
|
||||
QList<SavedAction *> m_list;
|
||||
};
|
||||
|
||||
} // namespace Utils
|
@@ -60,7 +60,6 @@ SOURCES += \
|
||||
$$PWD/classnamevalidatinglineedit.cpp \
|
||||
$$PWD/fancylineedit.cpp \
|
||||
$$PWD/qtcolorbutton.cpp \
|
||||
$$PWD/savedaction.cpp \
|
||||
$$PWD/synchronousprocess.cpp \
|
||||
$$PWD/savefile.cpp \
|
||||
$$PWD/fileutils.cpp \
|
||||
@@ -188,7 +187,6 @@ HEADERS += \
|
||||
$$PWD/classnamevalidatinglineedit.h \
|
||||
$$PWD/fancylineedit.h \
|
||||
$$PWD/qtcolorbutton.h \
|
||||
$$PWD/savedaction.h \
|
||||
$$PWD/consoleprocess.h \
|
||||
$$PWD/synchronousprocess.h \
|
||||
$$PWD/savefile.h \
|
||||
|
@@ -232,8 +232,6 @@ Project {
|
||||
"removefiledialog.ui",
|
||||
"runextensions.cpp",
|
||||
"runextensions.h",
|
||||
"savedaction.cpp",
|
||||
"savedaction.h",
|
||||
"savefile.cpp",
|
||||
"savefile.h",
|
||||
"scopedswap.h",
|
||||
|
Reference in New Issue
Block a user