Polish the settings editor example.

- Set QtProject as organization.
- Remove unneeded member variables.
- Use member initialization in the constructor.
- Use new connection syntax in createActions()
  to assemble the menu there, removing the createMenus()
  function.
- Introduce a QSharedPointer to ensure settings are deleted.
  Previously, the settings were parented on the tree widget,
  which is a hack of sorts.
- Fix OS X macros.

Change-Id: Ibbc6bfb03eb5c7eda077b1a3aa3f1707667f7f13
Reviewed-by: Topi Reiniö <topi.reinio@digia.com>
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2015-08-27 17:23:55 +02:00
parent 2e02130e39
commit 47e4ae86a8
7 changed files with 151 additions and 161 deletions

View File

@ -54,17 +54,18 @@ LocationDialog::LocationDialog(QWidget *parent)
scopeComboBox->addItem(tr("System")); scopeComboBox->addItem(tr("System"));
organizationComboBox = new QComboBox; organizationComboBox = new QComboBox;
organizationComboBox->addItem(tr("Qt")); organizationComboBox->addItem(tr("QtProject"));
organizationComboBox->setEditable(true); organizationComboBox->setEditable(true);
applicationComboBox = new QComboBox; applicationComboBox = new QComboBox;
applicationComboBox->addItem(tr("Any")); applicationComboBox->addItem(tr("Any"));
applicationComboBox->addItem(tr("Qt Creator"));
applicationComboBox->addItem(tr("Application Example")); applicationComboBox->addItem(tr("Application Example"));
applicationComboBox->addItem(tr("Assistant")); applicationComboBox->addItem(tr("Assistant"));
applicationComboBox->addItem(tr("Designer")); applicationComboBox->addItem(tr("Designer"));
applicationComboBox->addItem(tr("Linguist")); applicationComboBox->addItem(tr("Linguist"));
applicationComboBox->setEditable(true); applicationComboBox->setEditable(true);
applicationComboBox->setCurrentIndex(3); applicationComboBox->setCurrentIndex(1);
formatLabel = new QLabel(tr("&Format:")); formatLabel = new QLabel(tr("&Format:"));
formatLabel->setBuddy(formatComboBox); formatLabel->setBuddy(formatComboBox);
@ -91,28 +92,30 @@ LocationDialog::LocationDialog(QWidget *parent)
locationsTable->setHorizontalHeaderLabels(labels); locationsTable->setHorizontalHeaderLabels(labels);
locationsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); locationsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
locationsTable->horizontalHeader()->resizeSection(1, 180); locationsTable->horizontalHeader()->resizeSection(1, 180);
connect(locationsTable, &QTableWidget::itemActivated, this, &LocationDialog::itemActivated);
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
| QDialogButtonBox::Cancel);
connect(formatComboBox, SIGNAL(activated(int)), typedef void (QComboBox::*QComboIntSignal)(int);
this, SLOT(updateLocationsTable())); connect(formatComboBox, static_cast<QComboIntSignal>(&QComboBox::activated),
connect(scopeComboBox, SIGNAL(activated(int)), this, &LocationDialog::updateLocationsTable);
this, SLOT(updateLocationsTable())); connect(scopeComboBox, static_cast<QComboIntSignal>(&QComboBox::activated),
this, &LocationDialog::updateLocationsTable);
connect(organizationComboBox->lineEdit(), connect(organizationComboBox->lineEdit(),
SIGNAL(editingFinished()), &QLineEdit::editingFinished,
this, SLOT(updateLocationsTable())); this, &LocationDialog::updateLocationsTable);
connect(applicationComboBox->lineEdit(), connect(applicationComboBox->lineEdit(),
SIGNAL(editingFinished()), &QLineEdit::editingFinished,
this, SLOT(updateLocationsTable())); this, &LocationDialog::updateLocationsTable);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(applicationComboBox, static_cast<QComboIntSignal>(&QComboBox::activated),
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); this, &LocationDialog::updateLocationsTable);
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QVBoxLayout *locationsLayout = new QVBoxLayout; QVBoxLayout *locationsLayout = new QVBoxLayout(locationsGroupBox);
locationsLayout->addWidget(locationsTable); locationsLayout->addWidget(locationsTable);
locationsGroupBox->setLayout(locationsLayout);
QGridLayout *mainLayout = new QGridLayout; QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addWidget(formatLabel, 0, 0); mainLayout->addWidget(formatLabel, 0, 0);
mainLayout->addWidget(formatComboBox, 0, 1); mainLayout->addWidget(formatComboBox, 0, 1);
mainLayout->addWidget(scopeLabel, 1, 0); mainLayout->addWidget(scopeLabel, 1, 0);
@ -123,7 +126,6 @@ LocationDialog::LocationDialog(QWidget *parent)
mainLayout->addWidget(applicationComboBox, 3, 1); mainLayout->addWidget(applicationComboBox, 3, 1);
mainLayout->addWidget(locationsGroupBox, 4, 0, 1, 2); mainLayout->addWidget(locationsGroupBox, 4, 0, 1, 2);
mainLayout->addWidget(buttonBox, 5, 0, 1, 2); mainLayout->addWidget(buttonBox, 5, 0, 1, 2);
setLayout(mainLayout);
updateLocationsTable(); updateLocationsTable();
@ -155,11 +157,16 @@ QString LocationDialog::organization() const
QString LocationDialog::application() const QString LocationDialog::application() const
{ {
if (applicationComboBox->currentText() == tr("Any")) if (applicationComboBox->currentText() == tr("Any"))
return ""; return QString();
else else
return applicationComboBox->currentText(); return applicationComboBox->currentText();
} }
void LocationDialog::itemActivated(QTableWidgetItem *)
{
buttonBox->button(QDialogButtonBox::Ok)->animateClick();
}
void LocationDialog::updateLocationsTable() void LocationDialog::updateLocationsTable()
{ {
locationsTable->setUpdatesEnabled(false); locationsTable->setUpdatesEnabled(false);
@ -184,8 +191,7 @@ void LocationDialog::updateLocationsTable()
int row = locationsTable->rowCount(); int row = locationsTable->rowCount();
locationsTable->setRowCount(row + 1); locationsTable->setRowCount(row + 1);
QTableWidgetItem *item0 = new QTableWidgetItem; QTableWidgetItem *item0 = new QTableWidgetItem(QDir::toNativeSeparators(settings.fileName()));
item0->setText(settings.fileName());
QTableWidgetItem *item1 = new QTableWidgetItem; QTableWidgetItem *item1 = new QTableWidgetItem;
bool disable = (settings.childKeys().isEmpty() bool disable = (settings.childKeys().isEmpty()

View File

@ -50,6 +50,7 @@ class QDialogButtonBox;
class QGroupBox; class QGroupBox;
class QLabel; class QLabel;
class QTableWidget; class QTableWidget;
class QTableWidgetItem;
QT_END_NAMESPACE QT_END_NAMESPACE
class LocationDialog : public QDialog class LocationDialog : public QDialog
@ -66,6 +67,7 @@ public:
private slots: private slots:
void updateLocationsTable(); void updateLocationsTable();
void itemActivated(QTableWidgetItem *);
private: private:
QLabel *formatLabel; QLabel *formatLabel;

View File

@ -45,6 +45,9 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
QCoreApplication::setApplicationName("Settings Editor");
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
MainWindow mainWin; MainWindow mainWin;
mainWin.show(); mainWin.show();
return app.exec(); return app.exec();

View File

@ -45,20 +45,20 @@
#include "settingstree.h" #include "settingstree.h"
MainWindow::MainWindow() MainWindow::MainWindow()
: settingsTree(new SettingsTree)
, locationDialog(Q_NULLPTR)
{ {
settingsTree = new SettingsTree;
setCentralWidget(settingsTree); setCentralWidget(settingsTree);
locationDialog = 0;
createActions(); createActions();
createMenus();
autoRefreshAct->setChecked(true); autoRefreshAct->setChecked(true);
fallbacksAct->setChecked(true); fallbacksAct->setChecked(true);
setWindowTitle(tr("Settings Editor")); setWindowTitle(QCoreApplication::applicationName());
resize(500, 600); const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
adjustSize();
move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2);
} }
void MainWindow::openSettings() void MainWindow::openSettings()
@ -66,49 +66,60 @@ void MainWindow::openSettings()
if (!locationDialog) if (!locationDialog)
locationDialog = new LocationDialog(this); locationDialog = new LocationDialog(this);
if (locationDialog->exec()) { if (locationDialog->exec() != QDialog::Accepted)
QSettings *settings = new QSettings(locationDialog->format(), return;
locationDialog->scope(),
locationDialog->organization(), SettingsPtr settings(new QSettings(locationDialog->format(),
locationDialog->application()); locationDialog->scope(),
setSettingsObject(settings); locationDialog->organization(),
fallbacksAct->setEnabled(true); locationDialog->application()));
}
setSettingsObject(settings);
fallbacksAct->setEnabled(true);
} }
void MainWindow::openIniFile() void MainWindow::openIniFile()
{ {
QString fileName = QFileDialog::getOpenFileName(this, tr("Open INI File"), const QString directory = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
"", tr("INI Files (*.ini *.conf)")); const QString fileName =
if (!fileName.isEmpty()) { QFileDialog::getOpenFileName(this, tr("Open INI File"),
QSettings *settings = new QSettings(fileName, QSettings::IniFormat); directory, tr("INI Files (*.ini *.conf)"));
setSettingsObject(settings); if (fileName.isEmpty())
fallbacksAct->setEnabled(false); return;
}
SettingsPtr settings(new QSettings(fileName, QSettings::IniFormat));
setSettingsObject(settings);
fallbacksAct->setEnabled(false);
} }
void MainWindow::openPropertyList() void MainWindow::openPropertyList()
{ {
QString fileName = QFileDialog::getOpenFileName(this, const QString directory = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
tr("Open Property List"), const QString fileName =
"", tr("Property List Files (*.plist)")); QFileDialog::getOpenFileName(this, tr("Open Property List"),
if (!fileName.isEmpty()) { directory, tr("Property List Files (*.plist)"));
QSettings *settings = new QSettings(fileName, QSettings::NativeFormat); if (fileName.isEmpty())
setSettingsObject(settings); return;
fallbacksAct->setEnabled(false);
} SettingsPtr settings(new QSettings(fileName, QSettings::NativeFormat));
setSettingsObject(settings);
fallbacksAct->setEnabled(false);
} }
void MainWindow::openRegistryPath() void MainWindow::openRegistryPath()
{ {
QString path = QInputDialog::getText(this, tr("Open Registry Path"), const QString path =
tr("Enter the path in the Windows registry:"), QInputDialog::getText(this, tr("Open Registry Path"),
QLineEdit::Normal, "HKEY_CURRENT_USER\\"); tr("Enter the path in the Windows registry:"),
if (!path.isEmpty()) { QLineEdit::Normal, "HKEY_CURRENT_USER\\");
QSettings *settings = new QSettings(path, QSettings::NativeFormat); if (path.isEmpty())
setSettingsObject(settings); return;
fallbacksAct->setEnabled(false);
} SettingsPtr settings(new QSettings(path, QSettings::NativeFormat));
setSettingsObject(settings);
fallbacksAct->setEnabled(false);
} }
void MainWindow::about() void MainWindow::about()
@ -120,88 +131,59 @@ void MainWindow::about()
void MainWindow::createActions() void MainWindow::createActions()
{ {
openSettingsAct = new QAction(tr("&Open Application Settings..."), this); QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QAction *openSettingsAct = fileMenu->addAction(tr("&Open Application Settings..."), this, &MainWindow::openSettings);
openSettingsAct->setShortcuts(QKeySequence::Open); openSettingsAct->setShortcuts(QKeySequence::Open);
connect(openSettingsAct, SIGNAL(triggered()), this, SLOT(openSettings()));
openIniFileAct = new QAction(tr("Open I&NI File..."), this); QAction *openIniFileAct = fileMenu->addAction(tr("Open I&NI File..."), this, &MainWindow::openIniFile);
openIniFileAct->setShortcut(tr("Ctrl+N")); openIniFileAct->setShortcut(tr("Ctrl+N"));
connect(openIniFileAct, SIGNAL(triggered()), this, SLOT(openIniFile()));
openPropertyListAct = new QAction(tr("Open Mac &Property List..."), this); #ifdef Q_OS_OSX
QAction *openPropertyListAct = fileMenu->addAction(tr("Open Apple &Property List..."), this, &MainWindow::openPropertyList);
openPropertyListAct->setShortcut(tr("Ctrl+P")); openPropertyListAct->setShortcut(tr("Ctrl+P"));
connect(openPropertyListAct, SIGNAL(triggered()), #endif // Q_OS_OSX
this, SLOT(openPropertyList()));
openRegistryPathAct = new QAction(tr("Open Windows &Registry Path..."), #ifdef Q_OS_WIN
this); QAction *openRegistryPathAct = fileMenu->addAction(tr("Open Windows &Registry Path..."), this, &MainWindow::openRegistryPath);
openRegistryPathAct->setShortcut(tr("Ctrl+G")); openRegistryPathAct->setShortcut(tr("Ctrl+G"));
connect(openRegistryPathAct, SIGNAL(triggered()), #endif // Q_OS_WIN
this, SLOT(openRegistryPath()));
refreshAct = new QAction(tr("&Refresh"), this); fileMenu->addSeparator();
refreshAct = fileMenu->addAction(tr("&Refresh"), settingsTree, &SettingsTree::refresh);
refreshAct->setShortcut(tr("Ctrl+R")); refreshAct->setShortcut(tr("Ctrl+R"));
refreshAct->setEnabled(false); refreshAct->setEnabled(false);
connect(refreshAct, SIGNAL(triggered()), settingsTree, SLOT(refresh()));
exitAct = new QAction(tr("E&xit"), this); fileMenu->addSeparator();
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
exitAct->setShortcuts(QKeySequence::Quit); exitAct->setShortcuts(QKeySequence::Quit);
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
autoRefreshAct = new QAction(tr("&Auto-Refresh"), this); QMenu *optionsMenu = menuBar()->addMenu(tr("&Options"));
autoRefreshAct = optionsMenu->addAction(tr("&Auto-Refresh"));
autoRefreshAct->setShortcut(tr("Ctrl+A")); autoRefreshAct->setShortcut(tr("Ctrl+A"));
autoRefreshAct->setCheckable(true); autoRefreshAct->setCheckable(true);
autoRefreshAct->setEnabled(false); autoRefreshAct->setEnabled(false);
connect(autoRefreshAct, SIGNAL(triggered(bool)), connect(autoRefreshAct, &QAction::triggered,
settingsTree, SLOT(setAutoRefresh(bool))); settingsTree, &SettingsTree::setAutoRefresh);
connect(autoRefreshAct, SIGNAL(triggered(bool)), connect(autoRefreshAct, &QAction::triggered,
refreshAct, SLOT(setDisabled(bool))); refreshAct, &QAction::setDisabled);
fallbacksAct = new QAction(tr("&Fallbacks"), this); fallbacksAct = optionsMenu->addAction(tr("&Fallbacks"));
fallbacksAct->setShortcut(tr("Ctrl+F")); fallbacksAct->setShortcut(tr("Ctrl+F"));
fallbacksAct->setCheckable(true); fallbacksAct->setCheckable(true);
fallbacksAct->setEnabled(false); fallbacksAct->setEnabled(false);
connect(fallbacksAct, SIGNAL(triggered(bool)), connect(fallbacksAct, &QAction::triggered,
settingsTree, SLOT(setFallbacksEnabled(bool))); settingsTree, &SettingsTree::setFallbacksEnabled);
aboutAct = new QAction(tr("&About"), this); QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); helpMenu->addAction(tr("&About"), this, &MainWindow::about);
helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit);
aboutQtAct = new QAction(tr("About &Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
#ifndef Q_OS_MAC
openPropertyListAct->setEnabled(false);
#endif
#ifndef Q_OS_WIN
openRegistryPathAct->setEnabled(false);
#endif
} }
void MainWindow::createMenus() void MainWindow::setSettingsObject(const SettingsPtr &settings)
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openSettingsAct);
fileMenu->addAction(openIniFileAct);
fileMenu->addAction(openPropertyListAct);
fileMenu->addAction(openRegistryPathAct);
fileMenu->addSeparator();
fileMenu->addAction(refreshAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
optionsMenu = menuBar()->addMenu(tr("&Options"));
optionsMenu->addAction(autoRefreshAct);
optionsMenu->addAction(fallbacksAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
}
void MainWindow::setSettingsObject(QSettings *settings)
{ {
settings->setFallbacksEnabled(fallbacksAct->isChecked()); settings->setFallbacksEnabled(fallbacksAct->isChecked());
settingsTree->setSettingsObject(settings); settingsTree->setSettingsObject(settings);
@ -209,14 +191,14 @@ void MainWindow::setSettingsObject(QSettings *settings)
refreshAct->setEnabled(true); refreshAct->setEnabled(true);
autoRefreshAct->setEnabled(true); autoRefreshAct->setEnabled(true);
QString niceName = settings->fileName(); QString niceName = QDir::cleanPath(settings->fileName());
niceName.replace("\\", "/"); int pos = niceName.lastIndexOf(QLatin1Char('/'));
int pos = niceName.lastIndexOf("/");
if (pos != -1) if (pos != -1)
niceName.remove(0, pos + 1); niceName.remove(0, pos + 1);
if (!settings->isWritable()) if (!settings->isWritable())
niceName = tr("%1 (read only)").arg(niceName); niceName = tr("%1 (read only)").arg(niceName);
setWindowTitle(tr("%1 - %2").arg(niceName).arg(tr("Settings Editor"))); setWindowTitle(tr("%1 - %2").arg(niceName, QCoreApplication::applicationName()));
statusBar()->showMessage(tr("Opened \"%1\"").arg(QDir::toNativeSeparators(settings->fileName())));
} }

View File

@ -42,10 +42,10 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
#include <QSharedPointer>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QAction; class QAction;
class QMenu;
class QSettings; class QSettings;
QT_END_NAMESPACE QT_END_NAMESPACE
class LocationDialog; class LocationDialog;
@ -56,6 +56,8 @@ class MainWindow : public QMainWindow
Q_OBJECT Q_OBJECT
public: public:
typedef QSharedPointer<QSettings> SettingsPtr;
MainWindow(); MainWindow();
private slots: private slots:
@ -67,25 +69,13 @@ private slots:
private: private:
void createActions(); void createActions();
void createMenus(); void setSettingsObject(const SettingsPtr &settings);
void setSettingsObject(QSettings *settings);
SettingsTree *settingsTree; SettingsTree *settingsTree;
LocationDialog *locationDialog; LocationDialog *locationDialog;
QMenu *fileMenu;
QMenu *optionsMenu;
QMenu *helpMenu;
QAction *openSettingsAct;
QAction *openIniFileAct;
QAction *openPropertyListAct;
QAction *openRegistryPathAct;
QAction *refreshAct; QAction *refreshAct;
QAction *exitAct;
QAction *autoRefreshAct; QAction *autoRefreshAct;
QAction *fallbacksAct; QAction *fallbacksAct;
QAction *aboutAct;
QAction *aboutQtAct;
}; };
#endif #endif

View File

@ -45,18 +45,18 @@
SettingsTree::SettingsTree(QWidget *parent) SettingsTree::SettingsTree(QWidget *parent)
: QTreeWidget(parent) : QTreeWidget(parent)
, autoRefresh(false)
{ {
setItemDelegate(new VariantDelegate(this)); setItemDelegate(new VariantDelegate(this));
QStringList labels; QStringList labels;
labels << tr("Setting") << tr("Type") << tr("Value"); labels << tr("Setting") << tr("Type") << tr("Value");
setHeaderLabels(labels); setHeaderLabels(labels);
header()->setSectionResizeMode(0, QHeaderView::Stretch); header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
header()->setSectionResizeMode(2, QHeaderView::Stretch); header()->setSectionResizeMode(2, QHeaderView::Stretch);
settings = 0;
refreshTimer.setInterval(2000); refreshTimer.setInterval(2000);
autoRefresh = false;
groupIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon), groupIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
QIcon::Normal, QIcon::Off); QIcon::Normal, QIcon::Off);
@ -64,34 +64,37 @@ SettingsTree::SettingsTree(QWidget *parent)
QIcon::Normal, QIcon::On); QIcon::Normal, QIcon::On);
keyIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon)); keyIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(maybeRefresh())); connect(&refreshTimer, &QTimer::timeout, this, &SettingsTree::maybeRefresh);
} }
void SettingsTree::setSettingsObject(QSettings *settings) SettingsTree::~SettingsTree()
{ {
delete this->settings; }
this->settings = settings;
void SettingsTree::setSettingsObject(const SettingsPtr &newSettings)
{
settings = newSettings;
clear(); clear();
if (settings) { if (settings.isNull()) {
settings->setParent(this); refreshTimer.stop();
} else {
refresh(); refresh();
if (autoRefresh) if (autoRefresh)
refreshTimer.start(); refreshTimer.start();
} else {
refreshTimer.stop();
} }
} }
QSize SettingsTree::sizeHint() const QSize SettingsTree::sizeHint() const
{ {
return QSize(800, 600); const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
return QSize(availableGeometry.width() * 2 / 3, availableGeometry.height() * 2 / 3);
} }
void SettingsTree::setAutoRefresh(bool autoRefresh) void SettingsTree::setAutoRefresh(bool autoRefresh)
{ {
this->autoRefresh = autoRefresh; this->autoRefresh = autoRefresh;
if (settings) { if (!settings.isNull()) {
if (autoRefresh) { if (autoRefresh) {
maybeRefresh(); maybeRefresh();
refreshTimer.start(); refreshTimer.start();
@ -103,7 +106,7 @@ void SettingsTree::setAutoRefresh(bool autoRefresh)
void SettingsTree::setFallbacksEnabled(bool enabled) void SettingsTree::setFallbacksEnabled(bool enabled)
{ {
if (settings) { if (!settings.isNull()) {
settings->setFallbacksEnabled(enabled); settings->setFallbacksEnabled(enabled);
refresh(); refresh();
} }
@ -117,17 +120,17 @@ void SettingsTree::maybeRefresh()
void SettingsTree::refresh() void SettingsTree::refresh()
{ {
if (!settings) if (settings.isNull())
return; return;
disconnect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), disconnect(this, &QTreeWidget::itemChanged,
this, SLOT(updateSetting(QTreeWidgetItem*))); this, &SettingsTree::updateSetting);
settings->sync(); settings->sync();
updateChildItems(0); updateChildItems(0);
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), connect(this, &QTreeWidget::itemChanged,
this, SLOT(updateSetting(QTreeWidgetItem*))); this, &SettingsTree::updateSetting);
} }
bool SettingsTree::event(QEvent *event) bool SettingsTree::event(QEvent *event)
@ -144,7 +147,7 @@ void SettingsTree::updateSetting(QTreeWidgetItem *item)
QString key = item->text(0); QString key = item->text(0);
QTreeWidgetItem *ancestor = item->parent(); QTreeWidgetItem *ancestor = item->parent();
while (ancestor) { while (ancestor) {
key.prepend(ancestor->text(0) + "/"); key.prepend(ancestor->text(0) + QLatin1Char('/'));
ancestor = ancestor->parent(); ancestor = ancestor->parent();
} }
@ -162,8 +165,8 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent)
int childIndex = findChild(parent, group, dividerIndex); int childIndex = findChild(parent, group, dividerIndex);
if (childIndex != -1) { if (childIndex != -1) {
child = childAt(parent, childIndex); child = childAt(parent, childIndex);
child->setText(1, ""); child->setText(1, QString());
child->setText(2, ""); child->setText(2, QString());
child->setData(2, Qt::UserRole, QVariant()); child->setData(2, Qt::UserRole, QVariant());
moveItemForward(parent, childIndex, dividerIndex); moveItemForward(parent, childIndex, dividerIndex);
} else { } else {
@ -177,7 +180,7 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent)
settings->endGroup(); settings->endGroup();
} }
foreach (QString key, settings->childKeys()) { foreach (const QString &key, settings->childKeys()) {
QTreeWidgetItem *child; QTreeWidgetItem *child;
int childIndex = findChild(parent, key, 0); int childIndex = findChild(parent, key, 0);

View File

@ -44,6 +44,7 @@
#include <QIcon> #include <QIcon>
#include <QTimer> #include <QTimer>
#include <QTreeWidget> #include <QTreeWidget>
#include <QSharedPointer>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QSettings; class QSettings;
@ -54,9 +55,12 @@ class SettingsTree : public QTreeWidget
Q_OBJECT Q_OBJECT
public: public:
SettingsTree(QWidget *parent = 0); typedef QSharedPointer<QSettings> SettingsPtr;
void setSettingsObject(QSettings *settings); SettingsTree(QWidget *parent = 0);
~SettingsTree();
void setSettingsObject(const SettingsPtr &settings);
QSize sizeHint() const Q_DECL_OVERRIDE; QSize sizeHint() const Q_DECL_OVERRIDE;
public slots: public slots:
@ -80,7 +84,7 @@ private:
int findChild(QTreeWidgetItem *parent, const QString &text, int startIndex); int findChild(QTreeWidgetItem *parent, const QString &text, int startIndex);
void moveItemForward(QTreeWidgetItem *parent, int oldIndex, int newIndex); void moveItemForward(QTreeWidgetItem *parent, int oldIndex, int newIndex);
QSettings *settings; SettingsPtr settings;
QTimer refreshTimer; QTimer refreshTimer;
bool autoRefresh; bool autoRefresh;
QIcon groupIcon; QIcon groupIcon;