source: trunk/examples/tools/settingseditor/locationdialog.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 7.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "locationdialog.h"
44
45LocationDialog::LocationDialog(QWidget *parent)
46 : QDialog(parent)
47{
48 formatComboBox = new QComboBox;
49 formatComboBox->addItem(tr("Native"));
50 formatComboBox->addItem(tr("INI"));
51
52 scopeComboBox = new QComboBox;
53 scopeComboBox->addItem(tr("User"));
54 scopeComboBox->addItem(tr("System"));
55
56 organizationComboBox = new QComboBox;
57 organizationComboBox->addItem(tr("Trolltech"));
58 organizationComboBox->setEditable(true);
59
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);
68
69 formatLabel = new QLabel(tr("&Format:"));
70 formatLabel->setBuddy(formatComboBox);
71
72 scopeLabel = new QLabel(tr("&Scope:"));
73 scopeLabel->setBuddy(scopeComboBox);
74
75 organizationLabel = new QLabel(tr("&Organization:"));
76 organizationLabel->setBuddy(organizationComboBox);
77
78 applicationLabel = new QLabel(tr("&Application:"));
79 applicationLabel->setBuddy(applicationComboBox);
80
81 locationsGroupBox = new QGroupBox(tr("Setting Locations"));
82
83 QStringList labels;
84 labels << tr("Location") << tr("Access");
85
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()->setResizeMode(0, QHeaderView::Stretch);
93 locationsTable->horizontalHeader()->resizeSection(1, 180);
94
95 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
96 | QDialogButtonBox::Cancel);
97
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()));
110
111 QVBoxLayout *locationsLayout = new QVBoxLayout;
112 locationsLayout->addWidget(locationsTable);
113 locationsGroupBox->setLayout(locationsLayout);
114
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);
127
128 updateLocationsTable();
129
130 setWindowTitle(tr("Open Application Settings"));
131 resize(650, 400);
132}
133
134QSettings::Format LocationDialog::format() const
135{
136 if (formatComboBox->currentIndex() == 0)
137 return QSettings::NativeFormat;
138 else
139 return QSettings::IniFormat;
140}
141
142QSettings::Scope LocationDialog::scope() const
143{
144 if (scopeComboBox->currentIndex() == 0)
145 return QSettings::UserScope;
146 else
147 return QSettings::SystemScope;
148}
149
150QString LocationDialog::organization() const
151{
152 return organizationComboBox->currentText();
153}
154
155QString LocationDialog::application() const
156{
157 if (applicationComboBox->currentText() == tr("Any"))
158 return "";
159 else
160 return applicationComboBox->currentText();
161}
162
163void LocationDialog::updateLocationsTable()
164{
165 locationsTable->setUpdatesEnabled(false);
166 locationsTable->setRowCount(0);
167
168 for (int i = 0; i < 2; ++i) {
169 if (i == 0 && scope() == QSettings::SystemScope)
170 continue;
171
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())
176 continue;
177
178 QString actualApplication;
179 if (j == 0)
180 actualApplication = application();
181 QSettings settings(format(), actualScope, organization(),
182 actualApplication);
183
184 int row = locationsTable->rowCount();
185 locationsTable->setRowCount(row + 1);
186
187 QTableWidgetItem *item0 = new QTableWidgetItem;
188 item0->setText(settings.fileName());
189
190 QTableWidgetItem *item1 = new QTableWidgetItem;
191 bool disable = (settings.childKeys().isEmpty()
192 && settings.childGroups().isEmpty());
193
194 if (row == 0) {
195 if (settings.isWritable()) {
196 item1->setText(tr("Read-write"));
197 disable = false;
198 } else {
199 item1->setText(tr("Read-only"));
200 }
201 buttonBox->button(QDialogButtonBox::Ok)->setDisabled(disable);
202 } else {
203 item1->setText(tr("Read-only fallback"));
204 }
205
206 if (disable) {
207 item0->setFlags(item0->flags() & ~Qt::ItemIsEnabled);
208 item1->setFlags(item1->flags() & ~Qt::ItemIsEnabled);
209 }
210
211 locationsTable->setItem(row, 0, item0);
212 locationsTable->setItem(row, 1, item1);
213 }
214 }
215 locationsTable->setUpdatesEnabled(true);
216}
Note: See TracBrowser for help on using the repository browser.