Volker Hilsheimer 1f0c629a48 Move the settingseditor example into tests/manual
The example uses QTreeWidget when it should use a QTreeView with a
dedicated item model, primarily shows how to use item views (and very
little about QSettings), and is generally not useful to show how an
application could or should use QSettings to store settings.

Turn it into a manual test instead; it's useful for that as it supports
ini and plist files, and settings in different scopes.

Fixes: QTBUG-119978
Change-Id: I7ce039f6391c41c679d126d90a251eee60327c39
Reviewed-by: Ed Cooke <ed.cooke@qt.io>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit bddf27cd5a5412c8282fab43111e7319874ca44e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2023-12-15 17:21:32 +00:00

176 lines
5.8 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "locationdialog.h"
#include "mainwindow.h"
#include "settingstree.h"
#include <QAction>
#include <QApplication>
#include <QFileDialog>
#include <QInputDialog>
#include <QLineEdit>
#include <QMenuBar>
#include <QMessageBox>
#include <QScreen>
#include <QStandardPaths>
#include <QStatusBar>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
, settingsTree(new SettingsTree)
{
setCentralWidget(settingsTree);
createActions();
autoRefreshAct->setChecked(true);
fallbacksAct->setChecked(true);
setWindowTitle(QCoreApplication::applicationName());
const QRect availableGeometry = screen()->availableGeometry();
adjustSize();
move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2);
}
void MainWindow::openSettings()
{
if (!locationDialog)
locationDialog = new LocationDialog(this);
if (locationDialog->exec() != QDialog::Accepted)
return;
SettingsPtr settings(new QSettings(locationDialog->format(),
locationDialog->scope(),
locationDialog->organization(),
locationDialog->application()));
setSettingsObject(settings);
fallbacksAct->setEnabled(true);
}
void MainWindow::openIniFile()
{
const QString directory = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
const QString fileName =
QFileDialog::getOpenFileName(this, tr("Open INI File"),
directory, tr("INI Files (*.ini *.conf)"));
if (fileName.isEmpty())
return;
SettingsPtr settings(new QSettings(fileName, QSettings::IniFormat));
setSettingsObject(settings);
fallbacksAct->setEnabled(false);
}
void MainWindow::openPropertyList()
{
const QString directory = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
const QString fileName =
QFileDialog::getOpenFileName(this, tr("Open Property List"),
directory, tr("Property List Files (*.plist)"));
if (fileName.isEmpty())
return;
SettingsPtr settings(new QSettings(fileName, QSettings::NativeFormat));
setSettingsObject(settings);
fallbacksAct->setEnabled(false);
}
void MainWindow::openRegistryPath()
{
const QString path =
QInputDialog::getText(this, tr("Open Registry Path"),
tr("Enter the path in the Windows registry:"),
QLineEdit::Normal, "HKEY_CURRENT_USER\\");
if (path.isEmpty())
return;
SettingsPtr settings(new QSettings(path, QSettings::NativeFormat));
setSettingsObject(settings);
fallbacksAct->setEnabled(false);
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About Settings Editor"),
tr("The <b>Settings Editor</b> example shows how to access "
"application settings using Qt."));
}
void MainWindow::createActions()
{
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QAction *openSettingsAct = fileMenu->addAction(tr("&Open Application Settings..."), this, &MainWindow::openSettings);
openSettingsAct->setShortcuts(QKeySequence::Open);
QAction *openIniFileAct = fileMenu->addAction(tr("Open I&NI File..."), this, &MainWindow::openIniFile);
openIniFileAct->setShortcut(tr("Ctrl+N"));
#ifdef Q_OS_MACOS
QAction *openPropertyListAct = fileMenu->addAction(tr("Open Apple &Property List..."), this, &MainWindow::openPropertyList);
openPropertyListAct->setShortcut(tr("Ctrl+P"));
#endif // Q_OS_MACOS
#ifdef Q_OS_WIN
QAction *openRegistryPathAct = fileMenu->addAction(tr("Open Windows &Registry Path..."), this, &MainWindow::openRegistryPath);
openRegistryPathAct->setShortcut(tr("Ctrl+G"));
#endif // Q_OS_WIN
fileMenu->addSeparator();
refreshAct = fileMenu->addAction(tr("&Refresh"), settingsTree, &SettingsTree::refresh);
refreshAct->setShortcut(tr("Ctrl+R"));
refreshAct->setEnabled(false);
fileMenu->addSeparator();
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
exitAct->setShortcuts(QKeySequence::Quit);
QMenu *optionsMenu = menuBar()->addMenu(tr("&Options"));
autoRefreshAct = optionsMenu->addAction(tr("&Auto-Refresh"));
autoRefreshAct->setShortcut(tr("Ctrl+A"));
autoRefreshAct->setCheckable(true);
autoRefreshAct->setEnabled(false);
connect(autoRefreshAct, &QAction::triggered,
settingsTree, &SettingsTree::setAutoRefresh);
connect(autoRefreshAct, &QAction::triggered,
refreshAct, &QAction::setDisabled);
fallbacksAct = optionsMenu->addAction(tr("&Fallbacks"));
fallbacksAct->setShortcut(tr("Ctrl+F"));
fallbacksAct->setCheckable(true);
fallbacksAct->setEnabled(false);
connect(fallbacksAct, &QAction::triggered,
settingsTree, &SettingsTree::setFallbacksEnabled);
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(tr("&About"), this, &MainWindow::about);
helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
}
void MainWindow::setSettingsObject(const SettingsPtr &settings)
{
settings->setFallbacksEnabled(fallbacksAct->isChecked());
settingsTree->setSettingsObject(settings);
refreshAct->setEnabled(true);
autoRefreshAct->setEnabled(true);
QString niceName = QDir::cleanPath(settings->fileName());
int pos = niceName.lastIndexOf(QLatin1Char('/'));
if (pos != -1)
niceName.remove(0, pos + 1);
if (!settings->isWritable())
niceName = tr("%1 (read only)").arg(niceName);
setWindowTitle(tr("%1 - %2").arg(niceName, QCoreApplication::applicationName()));
statusBar()->showMessage(tr("Opened \"%1\"").arg(QDir::toNativeSeparators(settings->fileName())));
}