Examples: move widgets specific "tools" examples to the correct place
[profile/ivi/qtbase.git] / examples / widgets / tools / settingseditor / mainwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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.
23 **
24 **
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."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtWidgets>
42
43 #include "locationdialog.h"
44 #include "mainwindow.h"
45 #include "settingstree.h"
46
47 MainWindow::MainWindow()
48 {
49     settingsTree = new SettingsTree;
50     setCentralWidget(settingsTree);
51
52     locationDialog = 0;
53
54     createActions();
55     createMenus();
56
57     autoRefreshAct->setChecked(true);
58     fallbacksAct->setChecked(true);
59
60     setWindowTitle(tr("Settings Editor"));
61     resize(500, 600);
62 }
63
64 void MainWindow::openSettings()
65 {
66     if (!locationDialog)
67         locationDialog = new LocationDialog(this);
68
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);
76     }
77 }
78
79 void MainWindow::openIniFile()
80 {
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);
87     }
88 }
89
90 void MainWindow::openPropertyList()
91 {
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);
99     }
100 }
101
102 void MainWindow::openRegistryPath()
103 {
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);
111     }
112 }
113
114 void MainWindow::about()
115 {
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."));
119 }
120
121 void MainWindow::createActions()
122 {
123     openSettingsAct = new QAction(tr("&Open Application Settings..."), this);
124     openSettingsAct->setShortcuts(QKeySequence::Open);
125     connect(openSettingsAct, SIGNAL(triggered()), this, SLOT(openSettings()));
126
127     openIniFileAct = new QAction(tr("Open I&NI File..."), this);
128     openIniFileAct->setShortcut(tr("Ctrl+N"));
129     connect(openIniFileAct, SIGNAL(triggered()), this, SLOT(openIniFile()));
130
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()));
135
136     openRegistryPathAct = new QAction(tr("Open Windows &Registry Path..."),
137                                       this);
138     openRegistryPathAct->setShortcut(tr("Ctrl+G"));
139     connect(openRegistryPathAct, SIGNAL(triggered()),
140             this, SLOT(openRegistryPath()));
141
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()));
146
147     exitAct = new QAction(tr("E&xit"), this);
148     exitAct->setShortcuts(QKeySequence::Quit);
149     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
150
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)));
159
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)));
166
167     aboutAct = new QAction(tr("&About"), this);
168     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
169
170     aboutQtAct = new QAction(tr("About &Qt"), this);
171     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
172
173 #ifndef Q_OS_MAC
174     openPropertyListAct->setEnabled(false);
175 #endif
176 #ifndef Q_OS_WIN
177     openRegistryPathAct->setEnabled(false);
178 #endif
179 }
180
181 void MainWindow::createMenus()
182 {
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);
192
193     optionsMenu = menuBar()->addMenu(tr("&Options"));
194     optionsMenu->addAction(autoRefreshAct);
195     optionsMenu->addAction(fallbacksAct);
196
197     menuBar()->addSeparator();
198
199     helpMenu = menuBar()->addMenu(tr("&Help"));
200     helpMenu->addAction(aboutAct);
201     helpMenu->addAction(aboutQtAct);
202 }
203
204 void MainWindow::setSettingsObject(QSettings *settings)
205 {
206     settings->setFallbacksEnabled(fallbacksAct->isChecked());
207     settingsTree->setSettingsObject(settings);
208
209     refreshAct->setEnabled(true);
210     autoRefreshAct->setEnabled(true);
211
212     QString niceName = settings->fileName();
213     niceName.replace("\\", "/");
214     int pos = niceName.lastIndexOf("/");
215     if (pos != -1)
216         niceName.remove(0, pos + 1);
217
218     if (!settings->isWritable())
219         niceName = tr("%1 (read only)").arg(niceName);
220
221     setWindowTitle(tr("%1 - %2").arg(niceName).arg(tr("Settings Editor")));
222 }