examples/tools/settingseditor/mainwindow.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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->setShortcuts(QKeySequence::Open);
       
   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->setShortcuts(QKeySequence::Quit);
       
   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 }