1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "locationdialog.h"
52
53#include <QBoxLayout>
54#include <QComboBox>
55#include <QDialogButtonBox>
56#include <QDir>
57#include <QPushButton>
58#include <QGroupBox>
59#include <QHeaderView>
60#include <QLabel>
61#include <QLineEdit>
62#include <QTableWidget>
63#include <QTableWidgetItem>
64
65LocationDialog::LocationDialog(QWidget *parent)
66 : QDialog(parent)
67{
68 formatComboBox = new QComboBox;
69 formatComboBox->addItem(tr("Native"));
70 formatComboBox->addItem(tr("INI"));
71
72 scopeComboBox = new QComboBox;
73 scopeComboBox->addItem(tr("User"));
74 scopeComboBox->addItem(tr("System"));
75
76 organizationComboBox = new QComboBox;
77 organizationComboBox->addItem(tr("QtProject"));
78 organizationComboBox->setEditable(true);
79
80 applicationComboBox = new QComboBox;
81 applicationComboBox->addItem(tr("Any"));
82 applicationComboBox->addItem(tr("Qt Creator"));
83 applicationComboBox->addItem(tr("Application Example"));
84 applicationComboBox->addItem(tr("Assistant"));
85 applicationComboBox->addItem(tr("Designer"));
86 applicationComboBox->addItem(tr("Linguist"));
87 applicationComboBox->setEditable(true);
88 applicationComboBox->setCurrentIndex(1);
89
90 formatLabel = new QLabel(tr("&Format:"));
91 formatLabel->setBuddy(formatComboBox);
92
93 scopeLabel = new QLabel(tr("&Scope:"));
94 scopeLabel->setBuddy(scopeComboBox);
95
96 organizationLabel = new QLabel(tr("&Organization:"));
97 organizationLabel->setBuddy(organizationComboBox);
98
99 applicationLabel = new QLabel(tr("&Application:"));
100 applicationLabel->setBuddy(applicationComboBox);
101
102 locationsGroupBox = new QGroupBox(tr("Setting Locations"));
103
104 const QStringList labels{tr("Location"), tr("Access")};
105
106 locationsTable = new QTableWidget;
107 locationsTable->setSelectionMode(QAbstractItemView::SingleSelection);
108 locationsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
109 locationsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
110 locationsTable->setColumnCount(2);
111 locationsTable->setHorizontalHeaderLabels(labels);
112 locationsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
113 locationsTable->horizontalHeader()->resizeSection(1, 180);
114 connect(locationsTable, &QTableWidget::itemActivated, this, &LocationDialog::itemActivated);
115
116 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
117
118 connect(formatComboBox, &QComboBox::activated,
119 this, &LocationDialog::updateLocationsTable);
120 connect(scopeComboBox, &QComboBox::activated,
121 this, &LocationDialog::updateLocationsTable);
122 connect(organizationComboBox->lineEdit(),
123 &QLineEdit::editingFinished,
124 this, &LocationDialog::updateLocationsTable);
125 connect(applicationComboBox->lineEdit(),
126 &QLineEdit::editingFinished,
127 this, &LocationDialog::updateLocationsTable);
128 connect(applicationComboBox, &QComboBox::activated,
129 this, &LocationDialog::updateLocationsTable);
130 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
131 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
132
133 QVBoxLayout *locationsLayout = new QVBoxLayout(locationsGroupBox);
134 locationsLayout->addWidget(locationsTable);
135
136 QGridLayout *mainLayout = new QGridLayout(this);
137 mainLayout->addWidget(formatLabel, 0, 0);
138 mainLayout->addWidget(formatComboBox, 0, 1);
139 mainLayout->addWidget(scopeLabel, 1, 0);
140 mainLayout->addWidget(scopeComboBox, 1, 1);
141 mainLayout->addWidget(organizationLabel, 2, 0);
142 mainLayout->addWidget(organizationComboBox, 2, 1);
143 mainLayout->addWidget(applicationLabel, 3, 0);
144 mainLayout->addWidget(applicationComboBox, 3, 1);
145 mainLayout->addWidget(locationsGroupBox, 4, 0, 1, 2);
146 mainLayout->addWidget(buttonBox, 5, 0, 1, 2);
147
148 updateLocationsTable();
149
150 setWindowTitle(tr("Open Application Settings"));
151 resize(650, 400);
152}
153
154QSettings::Format LocationDialog::format() const
155{
156 if (formatComboBox->currentIndex() == 0)
157 return QSettings::NativeFormat;
158 else
159 return QSettings::IniFormat;
160}
161
162QSettings::Scope LocationDialog::scope() const
163{
164 if (scopeComboBox->currentIndex() == 0)
165 return QSettings::UserScope;
166 else
167 return QSettings::SystemScope;
168}
169
170QString LocationDialog::organization() const
171{
172 return organizationComboBox->currentText();
173}
174
175QString LocationDialog::application() const
176{
177 if (applicationComboBox->currentText() == tr("Any"))
178 return QString();
179 else
180 return applicationComboBox->currentText();
181}
182
183void LocationDialog::itemActivated(QTableWidgetItem *)
184{
185 buttonBox->button(QDialogButtonBox::Ok)->animateClick();
186}
187
188void LocationDialog::updateLocationsTable()
189{
190 locationsTable->setUpdatesEnabled(false);
191 locationsTable->setRowCount(0);
192
193 for (int i = 0; i < 2; ++i) {
194 if (i == 0 && scope() == QSettings::SystemScope)
195 continue;
196
197 QSettings::Scope actualScope = (i == 0) ? QSettings::UserScope
198 : QSettings::SystemScope;
199 for (int j = 0; j < 2; ++j) {
200 if (j == 0 && application().isEmpty())
201 continue;
202
203 QString actualApplication;
204 if (j == 0)
205 actualApplication = application();
206 QSettings settings(format(), actualScope, organization(),
207 actualApplication);
208
209 int row = locationsTable->rowCount();
210 locationsTable->setRowCount(row + 1);
211
212 QTableWidgetItem *item0 = new QTableWidgetItem(QDir::toNativeSeparators(settings.fileName()));
213
214 QTableWidgetItem *item1 = new QTableWidgetItem;
215 bool disable = (settings.childKeys().isEmpty()
216 && settings.childGroups().isEmpty());
217
218 if (row == 0) {
219 if (settings.isWritable()) {
220 item1->setText(tr("Read-write"));
221 disable = false;
222 } else {
223 item1->setText(tr("Read-only"));
224 }
225 buttonBox->button(QDialogButtonBox::Ok)->setDisabled(disable);
226 } else {
227 item1->setText(tr("Read-only fallback"));
228 }
229
230 if (disable) {
231 item0->setFlags(item0->flags() & ~Qt::ItemIsEnabled);
232 item1->setFlags(item1->flags() & ~Qt::ItemIsEnabled);
233 }
234
235 locationsTable->setItem(row, 0, item0);
236 locationsTable->setItem(row, 1, item1);
237 }
238 }
239 locationsTable->setUpdatesEnabled(true);
240}
241