Build fix when building libGLESv2 on Windows with MSVC 2010
[profile/ivi/qtbase.git] / examples / widgets / tools / settingseditor / locationdialog.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
45 LocationDialog::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("Qt"));
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()->setSectionResizeMode(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
134 QSettings::Format LocationDialog::format() const
135 {
136     if (formatComboBox->currentIndex() == 0)
137         return QSettings::NativeFormat;
138     else
139         return QSettings::IniFormat;
140 }
141
142 QSettings::Scope LocationDialog::scope() const
143 {
144     if (scopeComboBox->currentIndex() == 0)
145         return QSettings::UserScope;
146     else
147         return QSettings::SystemScope;
148 }
149
150 QString LocationDialog::organization() const
151 {
152     return organizationComboBox->currentText();
153 }
154
155 QString LocationDialog::application() const
156 {
157     if (applicationComboBox->currentText() == tr("Any"))
158         return "";
159     else
160         return applicationComboBox->currentText();
161 }
162
163 void 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 }