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"
45 LocationDialog::LocationDialog(QWidget *parent)
48 formatComboBox = new QComboBox;
49 formatComboBox->addItem(tr("Native"));
50 formatComboBox->addItem(tr("INI"));
52 scopeComboBox = new QComboBox;
53 scopeComboBox->addItem(tr("User"));
54 scopeComboBox->addItem(tr("System"));
56 organizationComboBox = new QComboBox;
57 organizationComboBox->addItem(tr("Qt"));
58 organizationComboBox->setEditable(true);
60 applicationComboBox = new QComboBox;
61 applicationComboBox->addItem(tr("Any"));
62 applicationComboBox->addItem(tr("Application Example"));
63 applicationComboBox->addItem(tr("Assistant"));
64 applicationComboBox->addItem(tr("Designer"));
65 applicationComboBox->addItem(tr("Linguist"));
66 applicationComboBox->setEditable(true);
67 applicationComboBox->setCurrentIndex(3);
69 formatLabel = new QLabel(tr("&Format:"));
70 formatLabel->setBuddy(formatComboBox);
72 scopeLabel = new QLabel(tr("&Scope:"));
73 scopeLabel->setBuddy(scopeComboBox);
75 organizationLabel = new QLabel(tr("&Organization:"));
76 organizationLabel->setBuddy(organizationComboBox);
78 applicationLabel = new QLabel(tr("&Application:"));
79 applicationLabel->setBuddy(applicationComboBox);
81 locationsGroupBox = new QGroupBox(tr("Setting Locations"));
84 labels << tr("Location") << tr("Access");
86 locationsTable = new QTableWidget;
87 locationsTable->setSelectionMode(QAbstractItemView::SingleSelection);
88 locationsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
89 locationsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
90 locationsTable->setColumnCount(2);
91 locationsTable->setHorizontalHeaderLabels(labels);
92 locationsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
93 locationsTable->horizontalHeader()->resizeSection(1, 180);
95 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
96 | QDialogButtonBox::Cancel);
98 connect(formatComboBox, SIGNAL(activated(int)),
99 this, SLOT(updateLocationsTable()));
100 connect(scopeComboBox, SIGNAL(activated(int)),
101 this, SLOT(updateLocationsTable()));
102 connect(organizationComboBox->lineEdit(),
103 SIGNAL(editingFinished()),
104 this, SLOT(updateLocationsTable()));
105 connect(applicationComboBox->lineEdit(),
106 SIGNAL(editingFinished()),
107 this, SLOT(updateLocationsTable()));
108 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
109 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
111 QVBoxLayout *locationsLayout = new QVBoxLayout;
112 locationsLayout->addWidget(locationsTable);
113 locationsGroupBox->setLayout(locationsLayout);
115 QGridLayout *mainLayout = new QGridLayout;
116 mainLayout->addWidget(formatLabel, 0, 0);
117 mainLayout->addWidget(formatComboBox, 0, 1);
118 mainLayout->addWidget(scopeLabel, 1, 0);
119 mainLayout->addWidget(scopeComboBox, 1, 1);
120 mainLayout->addWidget(organizationLabel, 2, 0);
121 mainLayout->addWidget(organizationComboBox, 2, 1);
122 mainLayout->addWidget(applicationLabel, 3, 0);
123 mainLayout->addWidget(applicationComboBox, 3, 1);
124 mainLayout->addWidget(locationsGroupBox, 4, 0, 1, 2);
125 mainLayout->addWidget(buttonBox, 5, 0, 1, 2);
126 setLayout(mainLayout);
128 updateLocationsTable();
130 setWindowTitle(tr("Open Application Settings"));
134 QSettings::Format LocationDialog::format() const
136 if (formatComboBox->currentIndex() == 0)
137 return QSettings::NativeFormat;
139 return QSettings::IniFormat;
142 QSettings::Scope LocationDialog::scope() const
144 if (scopeComboBox->currentIndex() == 0)
145 return QSettings::UserScope;
147 return QSettings::SystemScope;
150 QString LocationDialog::organization() const
152 return organizationComboBox->currentText();
155 QString LocationDialog::application() const
157 if (applicationComboBox->currentText() == tr("Any"))
160 return applicationComboBox->currentText();
163 void LocationDialog::updateLocationsTable()
165 locationsTable->setUpdatesEnabled(false);
166 locationsTable->setRowCount(0);
168 for (int i = 0; i < 2; ++i) {
169 if (i == 0 && scope() == QSettings::SystemScope)
172 QSettings::Scope actualScope = (i == 0) ? QSettings::UserScope
173 : QSettings::SystemScope;
174 for (int j = 0; j < 2; ++j) {
175 if (j == 0 && application().isEmpty())
178 QString actualApplication;
180 actualApplication = application();
181 QSettings settings(format(), actualScope, organization(),
184 int row = locationsTable->rowCount();
185 locationsTable->setRowCount(row + 1);
187 QTableWidgetItem *item0 = new QTableWidgetItem;
188 item0->setText(settings.fileName());
190 QTableWidgetItem *item1 = new QTableWidgetItem;
191 bool disable = (settings.childKeys().isEmpty()
192 && settings.childGroups().isEmpty());
195 if (settings.isWritable()) {
196 item1->setText(tr("Read-write"));
199 item1->setText(tr("Read-only"));
201 buttonBox->button(QDialogButtonBox::Ok)->setDisabled(disable);
203 item1->setText(tr("Read-only fallback"));
207 item0->setFlags(item0->flags() & ~Qt::ItemIsEnabled);
208 item1->setFlags(item1->flags() & ~Qt::ItemIsEnabled);
211 locationsTable->setItem(row, 0, item0);
212 locationsTable->setItem(row, 1, item1);
215 locationsTable->setUpdatesEnabled(true);