1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | #include "locationdialog.h"
|
---|
45 | #include "mainwindow.h"
|
---|
46 | #include "settingstree.h"
|
---|
47 |
|
---|
48 | MainWindow::MainWindow()
|
---|
49 | {
|
---|
50 | settingsTree = new SettingsTree;
|
---|
51 | setCentralWidget(settingsTree);
|
---|
52 |
|
---|
53 | locationDialog = 0;
|
---|
54 |
|
---|
55 | createActions();
|
---|
56 | createMenus();
|
---|
57 |
|
---|
58 | autoRefreshAct->setChecked(true);
|
---|
59 | fallbacksAct->setChecked(true);
|
---|
60 |
|
---|
61 | setWindowTitle(tr("Settings Editor"));
|
---|
62 | resize(500, 600);
|
---|
63 | }
|
---|
64 |
|
---|
65 | void MainWindow::openSettings()
|
---|
66 | {
|
---|
67 | if (!locationDialog)
|
---|
68 | locationDialog = new LocationDialog(this);
|
---|
69 |
|
---|
70 | if (locationDialog->exec()) {
|
---|
71 | QSettings *settings = new QSettings(locationDialog->format(),
|
---|
72 | locationDialog->scope(),
|
---|
73 | locationDialog->organization(),
|
---|
74 | locationDialog->application());
|
---|
75 | setSettingsObject(settings);
|
---|
76 | fallbacksAct->setEnabled(true);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | void MainWindow::openIniFile()
|
---|
81 | {
|
---|
82 | QString fileName = QFileDialog::getOpenFileName(this, tr("Open INI File"),
|
---|
83 | "", tr("INI Files (*.ini *.conf)"));
|
---|
84 | if (!fileName.isEmpty()) {
|
---|
85 | QSettings *settings = new QSettings(fileName, QSettings::IniFormat);
|
---|
86 | setSettingsObject(settings);
|
---|
87 | fallbacksAct->setEnabled(false);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | void MainWindow::openPropertyList()
|
---|
92 | {
|
---|
93 | QString fileName = QFileDialog::getOpenFileName(this,
|
---|
94 | tr("Open Property List"),
|
---|
95 | "", tr("Property List Files (*.plist)"));
|
---|
96 | if (!fileName.isEmpty()) {
|
---|
97 | QSettings *settings = new QSettings(fileName, QSettings::NativeFormat);
|
---|
98 | setSettingsObject(settings);
|
---|
99 | fallbacksAct->setEnabled(false);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | void MainWindow::openRegistryPath()
|
---|
104 | {
|
---|
105 | QString path = QInputDialog::getText(this, tr("Open Registry Path"),
|
---|
106 | tr("Enter the path in the Windows registry:"),
|
---|
107 | QLineEdit::Normal, "HKEY_CURRENT_USER\\");
|
---|
108 | if (!path.isEmpty()) {
|
---|
109 | QSettings *settings = new QSettings(path, QSettings::NativeFormat);
|
---|
110 | setSettingsObject(settings);
|
---|
111 | fallbacksAct->setEnabled(false);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | void MainWindow::about()
|
---|
116 | {
|
---|
117 | QMessageBox::about(this, tr("About Settings Editor"),
|
---|
118 | tr("The <b>Settings Editor</b> example shows how to access "
|
---|
119 | "application settings using Qt."));
|
---|
120 | }
|
---|
121 |
|
---|
122 | void MainWindow::createActions()
|
---|
123 | {
|
---|
124 | openSettingsAct = new QAction(tr("&Open Application Settings..."), this);
|
---|
125 | openSettingsAct->setShortcut(tr("Ctrl+O"));
|
---|
126 | connect(openSettingsAct, SIGNAL(triggered()), this, SLOT(openSettings()));
|
---|
127 |
|
---|
128 | openIniFileAct = new QAction(tr("Open I&NI File..."), this);
|
---|
129 | openIniFileAct->setShortcut(tr("Ctrl+N"));
|
---|
130 | connect(openIniFileAct, SIGNAL(triggered()), this, SLOT(openIniFile()));
|
---|
131 |
|
---|
132 | openPropertyListAct = new QAction(tr("Open Mac &Property List..."), this);
|
---|
133 | openPropertyListAct->setShortcut(tr("Ctrl+P"));
|
---|
134 | connect(openPropertyListAct, SIGNAL(triggered()),
|
---|
135 | this, SLOT(openPropertyList()));
|
---|
136 |
|
---|
137 | openRegistryPathAct = new QAction(tr("Open Windows &Registry Path..."),
|
---|
138 | this);
|
---|
139 | openRegistryPathAct->setShortcut(tr("Ctrl+G"));
|
---|
140 | connect(openRegistryPathAct, SIGNAL(triggered()),
|
---|
141 | this, SLOT(openRegistryPath()));
|
---|
142 |
|
---|
143 | refreshAct = new QAction(tr("&Refresh"), this);
|
---|
144 | refreshAct->setShortcut(tr("Ctrl+R"));
|
---|
145 | refreshAct->setEnabled(false);
|
---|
146 | connect(refreshAct, SIGNAL(triggered()), settingsTree, SLOT(refresh()));
|
---|
147 |
|
---|
148 | exitAct = new QAction(tr("E&xit"), this);
|
---|
149 | exitAct->setShortcut(tr("Ctrl+Q"));
|
---|
150 | connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
|
---|
151 |
|
---|
152 | autoRefreshAct = new QAction(tr("&Auto-Refresh"), this);
|
---|
153 | autoRefreshAct->setShortcut(tr("Ctrl+A"));
|
---|
154 | autoRefreshAct->setCheckable(true);
|
---|
155 | autoRefreshAct->setEnabled(false);
|
---|
156 | connect(autoRefreshAct, SIGNAL(triggered(bool)),
|
---|
157 | settingsTree, SLOT(setAutoRefresh(bool)));
|
---|
158 | connect(autoRefreshAct, SIGNAL(triggered(bool)),
|
---|
159 | refreshAct, SLOT(setDisabled(bool)));
|
---|
160 |
|
---|
161 | fallbacksAct = new QAction(tr("&Fallbacks"), this);
|
---|
162 | fallbacksAct->setShortcut(tr("Ctrl+F"));
|
---|
163 | fallbacksAct->setCheckable(true);
|
---|
164 | fallbacksAct->setEnabled(false);
|
---|
165 | connect(fallbacksAct, SIGNAL(triggered(bool)),
|
---|
166 | settingsTree, SLOT(setFallbacksEnabled(bool)));
|
---|
167 |
|
---|
168 | aboutAct = new QAction(tr("&About"), this);
|
---|
169 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
---|
170 |
|
---|
171 | aboutQtAct = new QAction(tr("About &Qt"), this);
|
---|
172 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
---|
173 |
|
---|
174 | #ifndef Q_WS_MAC
|
---|
175 | openPropertyListAct->setEnabled(false);
|
---|
176 | #endif
|
---|
177 | #ifndef Q_WS_WIN
|
---|
178 | openRegistryPathAct->setEnabled(false);
|
---|
179 | #endif
|
---|
180 | }
|
---|
181 |
|
---|
182 | void MainWindow::createMenus()
|
---|
183 | {
|
---|
184 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
185 | fileMenu->addAction(openSettingsAct);
|
---|
186 | fileMenu->addAction(openIniFileAct);
|
---|
187 | fileMenu->addAction(openPropertyListAct);
|
---|
188 | fileMenu->addAction(openRegistryPathAct);
|
---|
189 | fileMenu->addSeparator();
|
---|
190 | fileMenu->addAction(refreshAct);
|
---|
191 | fileMenu->addSeparator();
|
---|
192 | fileMenu->addAction(exitAct);
|
---|
193 |
|
---|
194 | optionsMenu = menuBar()->addMenu(tr("&Options"));
|
---|
195 | optionsMenu->addAction(autoRefreshAct);
|
---|
196 | optionsMenu->addAction(fallbacksAct);
|
---|
197 |
|
---|
198 | menuBar()->addSeparator();
|
---|
199 |
|
---|
200 | helpMenu = menuBar()->addMenu(tr("&Help"));
|
---|
201 | helpMenu->addAction(aboutAct);
|
---|
202 | helpMenu->addAction(aboutQtAct);
|
---|
203 | }
|
---|
204 |
|
---|
205 | void MainWindow::setSettingsObject(QSettings *settings)
|
---|
206 | {
|
---|
207 | settings->setFallbacksEnabled(fallbacksAct->isChecked());
|
---|
208 | settingsTree->setSettingsObject(settings);
|
---|
209 |
|
---|
210 | refreshAct->setEnabled(true);
|
---|
211 | autoRefreshAct->setEnabled(true);
|
---|
212 |
|
---|
213 | QString niceName = settings->fileName();
|
---|
214 | niceName.replace("\\", "/");
|
---|
215 | int pos = niceName.lastIndexOf("/");
|
---|
216 | if (pos != -1)
|
---|
217 | niceName.remove(0, pos + 1);
|
---|
218 |
|
---|
219 | if (!settings->isWritable())
|
---|
220 | niceName = tr("%1 (read only)").arg(niceName);
|
---|
221 |
|
---|
222 | setWindowTitle(tr("%1 - %2").arg(niceName).arg(tr("Settings Editor")));
|
---|
223 | }
|
---|