| 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 | |
| 65 | LocationDialog::LocationDialog(QWidget *parent) |
| 66 | : QDialog(parent) |
| 67 | { |
| 68 | formatComboBox = new QComboBox; |
| 69 | formatComboBox->addItem(atext: tr(s: "Native" )); |
| 70 | formatComboBox->addItem(atext: tr(s: "INI" )); |
| 71 | |
| 72 | scopeComboBox = new QComboBox; |
| 73 | scopeComboBox->addItem(atext: tr(s: "User" )); |
| 74 | scopeComboBox->addItem(atext: tr(s: "System" )); |
| 75 | |
| 76 | organizationComboBox = new QComboBox; |
| 77 | organizationComboBox->addItem(atext: tr(s: "QtProject" )); |
| 78 | organizationComboBox->setEditable(true); |
| 79 | |
| 80 | applicationComboBox = new QComboBox; |
| 81 | applicationComboBox->addItem(atext: tr(s: "Any" )); |
| 82 | applicationComboBox->addItem(atext: tr(s: "Qt Creator" )); |
| 83 | applicationComboBox->addItem(atext: tr(s: "Application Example" )); |
| 84 | applicationComboBox->addItem(atext: tr(s: "Assistant" )); |
| 85 | applicationComboBox->addItem(atext: tr(s: "Designer" )); |
| 86 | applicationComboBox->addItem(atext: tr(s: "Linguist" )); |
| 87 | applicationComboBox->setEditable(true); |
| 88 | applicationComboBox->setCurrentIndex(1); |
| 89 | |
| 90 | formatLabel = new QLabel(tr(s: "&Format:" )); |
| 91 | formatLabel->setBuddy(formatComboBox); |
| 92 | |
| 93 | scopeLabel = new QLabel(tr(s: "&Scope:" )); |
| 94 | scopeLabel->setBuddy(scopeComboBox); |
| 95 | |
| 96 | organizationLabel = new QLabel(tr(s: "&Organization:" )); |
| 97 | organizationLabel->setBuddy(organizationComboBox); |
| 98 | |
| 99 | applicationLabel = new QLabel(tr(s: "&Application:" )); |
| 100 | applicationLabel->setBuddy(applicationComboBox); |
| 101 | |
| 102 | locationsGroupBox = new QGroupBox(tr(s: "Setting Locations" )); |
| 103 | |
| 104 | const QStringList labels{tr(s: "Location" ), tr(s: "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(logicalIndex: 0, mode: QHeaderView::Stretch); |
| 113 | locationsTable->horizontalHeader()->resizeSection(logicalIndex: 1, size: 180); |
| 114 | connect(sender: locationsTable, signal: &QTableWidget::itemActivated, receiver: this, slot: &LocationDialog::itemActivated); |
| 115 | |
| 116 | buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 117 | |
| 118 | connect(sender: formatComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 119 | receiver: this, slot: &LocationDialog::updateLocationsTable); |
| 120 | connect(sender: scopeComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 121 | receiver: this, slot: &LocationDialog::updateLocationsTable); |
| 122 | connect(sender: organizationComboBox->lineEdit(), |
| 123 | signal: &QLineEdit::editingFinished, |
| 124 | receiver: this, slot: &LocationDialog::updateLocationsTable); |
| 125 | connect(sender: applicationComboBox->lineEdit(), |
| 126 | signal: &QLineEdit::editingFinished, |
| 127 | receiver: this, slot: &LocationDialog::updateLocationsTable); |
| 128 | connect(sender: applicationComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 129 | receiver: this, slot: &LocationDialog::updateLocationsTable); |
| 130 | connect(sender: buttonBox, signal: &QDialogButtonBox::accepted, receiver: this, slot: &QDialog::accept); |
| 131 | connect(sender: buttonBox, signal: &QDialogButtonBox::rejected, receiver: this, slot: &QDialog::reject); |
| 132 | |
| 133 | QVBoxLayout *locationsLayout = new QVBoxLayout(locationsGroupBox); |
| 134 | locationsLayout->addWidget(locationsTable); |
| 135 | |
| 136 | QGridLayout *mainLayout = new QGridLayout(this); |
| 137 | mainLayout->addWidget(formatLabel, row: 0, column: 0); |
| 138 | mainLayout->addWidget(formatComboBox, row: 0, column: 1); |
| 139 | mainLayout->addWidget(scopeLabel, row: 1, column: 0); |
| 140 | mainLayout->addWidget(scopeComboBox, row: 1, column: 1); |
| 141 | mainLayout->addWidget(organizationLabel, row: 2, column: 0); |
| 142 | mainLayout->addWidget(organizationComboBox, row: 2, column: 1); |
| 143 | mainLayout->addWidget(applicationLabel, row: 3, column: 0); |
| 144 | mainLayout->addWidget(applicationComboBox, row: 3, column: 1); |
| 145 | mainLayout->addWidget(locationsGroupBox, row: 4, column: 0, rowSpan: 1, columnSpan: 2); |
| 146 | mainLayout->addWidget(buttonBox, row: 5, column: 0, rowSpan: 1, columnSpan: 2); |
| 147 | |
| 148 | updateLocationsTable(); |
| 149 | |
| 150 | setWindowTitle(tr(s: "Open Application Settings" )); |
| 151 | resize(w: 650, h: 400); |
| 152 | } |
| 153 | |
| 154 | QSettings::Format LocationDialog::format() const |
| 155 | { |
| 156 | if (formatComboBox->currentIndex() == 0) |
| 157 | return QSettings::NativeFormat; |
| 158 | else |
| 159 | return QSettings::IniFormat; |
| 160 | } |
| 161 | |
| 162 | QSettings::Scope LocationDialog::scope() const |
| 163 | { |
| 164 | if (scopeComboBox->currentIndex() == 0) |
| 165 | return QSettings::UserScope; |
| 166 | else |
| 167 | return QSettings::SystemScope; |
| 168 | } |
| 169 | |
| 170 | QString LocationDialog::organization() const |
| 171 | { |
| 172 | return organizationComboBox->currentText(); |
| 173 | } |
| 174 | |
| 175 | QString LocationDialog::application() const |
| 176 | { |
| 177 | if (applicationComboBox->currentText() == tr(s: "Any" )) |
| 178 | return QString(); |
| 179 | else |
| 180 | return applicationComboBox->currentText(); |
| 181 | } |
| 182 | |
| 183 | void LocationDialog::itemActivated(QTableWidgetItem *) |
| 184 | { |
| 185 | buttonBox->button(which: QDialogButtonBox::Ok)->animateClick(); |
| 186 | } |
| 187 | |
| 188 | void 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(pathName: 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(s: "Read-write" )); |
| 221 | disable = false; |
| 222 | } else { |
| 223 | item1->setText(tr(s: "Read-only" )); |
| 224 | } |
| 225 | buttonBox->button(which: QDialogButtonBox::Ok)->setDisabled(disable); |
| 226 | } else { |
| 227 | item1->setText(tr(s: "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, column: 0, item: item0); |
| 236 | locationsTable->setItem(row, column: 1, item: item1); |
| 237 | } |
| 238 | } |
| 239 | locationsTable->setUpdatesEnabled(true); |
| 240 | } |
| 241 | |