1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
43 #include "locationdialog.h"
44 #include "mainwindow.h"
45 #include "settingstree.h"
47 MainWindow::MainWindow()
49 settingsTree = new SettingsTree;
50 setCentralWidget(settingsTree);
57 autoRefreshAct->setChecked(true);
58 fallbacksAct->setChecked(true);
60 setWindowTitle(tr("Settings Editor"));
64 void MainWindow::openSettings()
67 locationDialog = new LocationDialog(this);
69 if (locationDialog->exec()) {
70 QSettings *settings = new QSettings(locationDialog->format(),
71 locationDialog->scope(),
72 locationDialog->organization(),
73 locationDialog->application());
74 setSettingsObject(settings);
75 fallbacksAct->setEnabled(true);
79 void MainWindow::openIniFile()
81 QString fileName = QFileDialog::getOpenFileName(this, tr("Open INI File"),
82 "", tr("INI Files (*.ini *.conf)"));
83 if (!fileName.isEmpty()) {
84 QSettings *settings = new QSettings(fileName, QSettings::IniFormat);
85 setSettingsObject(settings);
86 fallbacksAct->setEnabled(false);
90 void MainWindow::openPropertyList()
92 QString fileName = QFileDialog::getOpenFileName(this,
93 tr("Open Property List"),
94 "", tr("Property List Files (*.plist)"));
95 if (!fileName.isEmpty()) {
96 QSettings *settings = new QSettings(fileName, QSettings::NativeFormat);
97 setSettingsObject(settings);
98 fallbacksAct->setEnabled(false);
102 void MainWindow::openRegistryPath()
104 QString path = QInputDialog::getText(this, tr("Open Registry Path"),
105 tr("Enter the path in the Windows registry:"),
106 QLineEdit::Normal, "HKEY_CURRENT_USER\\");
107 if (!path.isEmpty()) {
108 QSettings *settings = new QSettings(path, QSettings::NativeFormat);
109 setSettingsObject(settings);
110 fallbacksAct->setEnabled(false);
114 void MainWindow::about()
116 QMessageBox::about(this, tr("About Settings Editor"),
117 tr("The <b>Settings Editor</b> example shows how to access "
118 "application settings using Qt."));
121 void MainWindow::createActions()
123 openSettingsAct = new QAction(tr("&Open Application Settings..."), this);
124 openSettingsAct->setShortcuts(QKeySequence::Open);
125 connect(openSettingsAct, SIGNAL(triggered()), this, SLOT(openSettings()));
127 openIniFileAct = new QAction(tr("Open I&NI File..."), this);
128 openIniFileAct->setShortcut(tr("Ctrl+N"));
129 connect(openIniFileAct, SIGNAL(triggered()), this, SLOT(openIniFile()));
131 openPropertyListAct = new QAction(tr("Open Mac &Property List..."), this);
132 openPropertyListAct->setShortcut(tr("Ctrl+P"));
133 connect(openPropertyListAct, SIGNAL(triggered()),
134 this, SLOT(openPropertyList()));
136 openRegistryPathAct = new QAction(tr("Open Windows &Registry Path..."),
138 openRegistryPathAct->setShortcut(tr("Ctrl+G"));
139 connect(openRegistryPathAct, SIGNAL(triggered()),
140 this, SLOT(openRegistryPath()));
142 refreshAct = new QAction(tr("&Refresh"), this);
143 refreshAct->setShortcut(tr("Ctrl+R"));
144 refreshAct->setEnabled(false);
145 connect(refreshAct, SIGNAL(triggered()), settingsTree, SLOT(refresh()));
147 exitAct = new QAction(tr("E&xit"), this);
148 exitAct->setShortcuts(QKeySequence::Quit);
149 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
151 autoRefreshAct = new QAction(tr("&Auto-Refresh"), this);
152 autoRefreshAct->setShortcut(tr("Ctrl+A"));
153 autoRefreshAct->setCheckable(true);
154 autoRefreshAct->setEnabled(false);
155 connect(autoRefreshAct, SIGNAL(triggered(bool)),
156 settingsTree, SLOT(setAutoRefresh(bool)));
157 connect(autoRefreshAct, SIGNAL(triggered(bool)),
158 refreshAct, SLOT(setDisabled(bool)));
160 fallbacksAct = new QAction(tr("&Fallbacks"), this);
161 fallbacksAct->setShortcut(tr("Ctrl+F"));
162 fallbacksAct->setCheckable(true);
163 fallbacksAct->setEnabled(false);
164 connect(fallbacksAct, SIGNAL(triggered(bool)),
165 settingsTree, SLOT(setFallbacksEnabled(bool)));
167 aboutAct = new QAction(tr("&About"), this);
168 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
170 aboutQtAct = new QAction(tr("About &Qt"), this);
171 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
174 openPropertyListAct->setEnabled(false);
177 openRegistryPathAct->setEnabled(false);
181 void MainWindow::createMenus()
183 fileMenu = menuBar()->addMenu(tr("&File"));
184 fileMenu->addAction(openSettingsAct);
185 fileMenu->addAction(openIniFileAct);
186 fileMenu->addAction(openPropertyListAct);
187 fileMenu->addAction(openRegistryPathAct);
188 fileMenu->addSeparator();
189 fileMenu->addAction(refreshAct);
190 fileMenu->addSeparator();
191 fileMenu->addAction(exitAct);
193 optionsMenu = menuBar()->addMenu(tr("&Options"));
194 optionsMenu->addAction(autoRefreshAct);
195 optionsMenu->addAction(fallbacksAct);
197 menuBar()->addSeparator();
199 helpMenu = menuBar()->addMenu(tr("&Help"));
200 helpMenu->addAction(aboutAct);
201 helpMenu->addAction(aboutQtAct);
204 void MainWindow::setSettingsObject(QSettings *settings)
206 settings->setFallbacksEnabled(fallbacksAct->isChecked());
207 settingsTree->setSettingsObject(settings);
209 refreshAct->setEnabled(true);
210 autoRefreshAct->setEnabled(true);
212 QString niceName = settings->fileName();
213 niceName.replace("\\", "/");
214 int pos = niceName.lastIndexOf("/");
216 niceName.remove(0, pos + 1);
218 if (!settings->isWritable())
219 niceName = tr("%1 (read only)").arg(niceName);
221 setWindowTitle(tr("%1 - %2").arg(niceName).arg(tr("Settings Editor")));