summaryrefslogtreecommitdiffstats
path: root/tests/manual/examples/widgets/tools/settingseditor/mainwindow.cpp
blob: be9f19e8ccd3546760668a9fb49268d76b26c0ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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())));
}