diff options
author | Christian Kandeler <christian.kandeler@digia.com> | 2014-10-31 10:21:55 +0100 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@digia.com> | 2014-11-05 12:33:27 +0100 |
commit | e5bf7e784e90417551d681731aee8b27beeefc6f (patch) | |
tree | 4a2e2ceaa66d9262aefe64eb9c73929ffaac6ee5 /src/app/config-ui | |
parent | df5b5f42d1ac6a9c0ce19a0d7a0b6cb2c6720e55 (diff) |
Move the SettingsModel into the library.
So that other applications can easily display qbs settings
as well.
Change-Id: Ic5e4ab5354f7879df29c90407905fc50b5546796
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
Diffstat (limited to 'src/app/config-ui')
-rw-r--r-- | src/app/config-ui/config-ui.pro | 8 | ||||
-rw-r--r-- | src/app/config-ui/config-ui.qbs | 4 | ||||
-rw-r--r-- | src/app/config-ui/mainwindow.cpp | 4 | ||||
-rw-r--r-- | src/app/config-ui/mainwindow.h | 6 | ||||
-rw-r--r-- | src/app/config-ui/settingsmodel.cpp | 297 | ||||
-rw-r--r-- | src/app/config-ui/settingsmodel.h | 61 |
6 files changed, 7 insertions, 373 deletions
diff --git a/src/app/config-ui/config-ui.pro b/src/app/config-ui/config-ui.pro index a62de6dae..b7c207a3c 100644 --- a/src/app/config-ui/config-ui.pro +++ b/src/app/config-ui/config-ui.pro @@ -6,17 +6,13 @@ QT += gui widgets TARGET = qbs-config-ui HEADERS += \ - ../shared/qbssettings.h \ commandlineparser.h \ - mainwindow.h \ - settingsmodel.h + mainwindow.h SOURCES += \ - ../shared/qbssettings.cpp \ commandlineparser.cpp \ main.cpp \ - mainwindow.cpp \ - settingsmodel.cpp + mainwindow.cpp OTHER_FILES += \ Info.plist diff --git a/src/app/config-ui/config-ui.qbs b/src/app/config-ui/config-ui.qbs index bed34673d..946205634 100644 --- a/src/app/config-ui/config-ui.qbs +++ b/src/app/config-ui/config-ui.qbs @@ -5,16 +5,12 @@ QbsApp { name: "qbs-config-ui" consoleApplication: false files: [ - "../shared/qbssettings.cpp", - "../shared/qbssettings.h", "commandlineparser.cpp", "commandlineparser.h", "main.cpp", "mainwindow.cpp", "mainwindow.h", "mainwindow.ui", - "settingsmodel.cpp", - "settingsmodel.h" ] cpp.infoPlistFile: "Info.plist" diff --git a/src/app/config-ui/mainwindow.cpp b/src/app/config-ui/mainwindow.cpp index 761c447a6..5c3871606 100644 --- a/src/app/config-ui/mainwindow.cpp +++ b/src/app/config-ui/mainwindow.cpp @@ -30,7 +30,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include "settingsmodel.h" +#include <tools/settingsmodel.h> #include <QAction> #include <QKeyEvent> @@ -46,7 +46,7 @@ MainWindow::MainWindow(const QString &settingsDir, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); - m_model = new SettingsModel(settingsDir, this); + m_model = new qbs::SettingsModel(settingsDir, this); ui->treeView->setModel(m_model); ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->treeView, SIGNAL(expanded(QModelIndex)), SLOT(adjustColumns())); diff --git a/src/app/config-ui/mainwindow.h b/src/app/config-ui/mainwindow.h index db0072c18..a5160ea3e 100644 --- a/src/app/config-ui/mainwindow.h +++ b/src/app/config-ui/mainwindow.h @@ -32,13 +32,13 @@ #include <QMainWindow> +namespace qbs { class SettingsModel; } + QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } class QPoint; QT_END_NAMESPACE -class SettingsModel; - class MainWindow : public QMainWindow { Q_OBJECT @@ -60,7 +60,7 @@ private slots: private: Ui::MainWindow *ui; - SettingsModel *m_model; + qbs::SettingsModel *m_model; }; #endif // MAINWINDOW_H diff --git a/src/app/config-ui/settingsmodel.cpp b/src/app/config-ui/settingsmodel.cpp deleted file mode 100644 index a2074b881..000000000 --- a/src/app/config-ui/settingsmodel.cpp +++ /dev/null @@ -1,297 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt Build Suite. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://www.qt.io/licensing. For further information -** use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -****************************************************************************/ -#include "settingsmodel.h" - -#include "../shared/qbssettings.h" - -#include <QList> -#include <QScopedPointer> -#include <QString> - -using qbs::Settings; - -struct Node -{ - Node() : parent(0) {} - ~Node() { qDeleteAll(children); } - - QString uniqueChildName() const; - bool hasDirectChildWithName(const QString &name) const; - - QString name; - QString value; - Node *parent; - QList<Node *> children; -}; - -class SettingsModel::SettingsModelPrivate -{ -public: - void readSettings(); - void addNode(Node *parentNode, const QString &fullyQualifiedName); - void doSave(const Node *node, const QString &prefix); - Node *indexToNode(const QModelIndex &index); - - Node rootNode; - QScopedPointer<Settings> settings; - bool dirty; -}; - -SettingsModel::SettingsModel(const QString &settingsDir, QObject *parent) - : QAbstractItemModel(parent), d(new SettingsModelPrivate) -{ - d->settings.reset(new Settings(settingsDir)); - d->readSettings(); -} - -SettingsModel::~SettingsModel() -{ - delete d; -} - -void SettingsModel::reload() -{ - beginResetModel(); - d->readSettings(); - endResetModel(); -} - -void SettingsModel::save() -{ - if (!d->dirty) - return; - d->settings->clear(); - d->doSave(&d->rootNode, QString()); - d->dirty = false; -} - -void SettingsModel::addNewKey(const QModelIndex &parent) -{ - Node *parentNode = d->indexToNode(parent); - if (!parentNode) - return; - Node * const newNode = new Node; - newNode->parent = parentNode; - newNode->name = parentNode->uniqueChildName(); - beginInsertRows(parent, parentNode->children.count(), parentNode->children.count()); - parentNode->children << newNode; - endInsertRows(); - d->dirty = true; -} - -void SettingsModel::removeKey(const QModelIndex &index) -{ - Node * const node = d->indexToNode(index); - if (!node || node == &d->rootNode) - return; - const int positionInParent = node->parent->children.indexOf(node); - beginRemoveRows(parent(index), positionInParent, positionInParent); - node->parent->children.removeAt(positionInParent); - delete node; - endRemoveRows(); - d->dirty = true; -} - -bool SettingsModel::hasUnsavedChanges() const -{ - return d->dirty; -} - -Qt::ItemFlags SettingsModel::flags(const QModelIndex &index) const -{ - if (!index.isValid()) - return Qt::ItemFlags(); - const Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; - if (index.column() == keyColumn()) - return flags | Qt::ItemIsEditable; - if (index.column() == valueColumn()) { - const Node * const node = d->indexToNode(index); - if (!node) - return Qt::ItemFlags(); - - // Only leaf nodes have values. - return node->children.isEmpty() ? flags | Qt::ItemIsEditable : flags; - } - return Qt::ItemFlags(); -} - -QVariant SettingsModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation != Qt::Horizontal) - return QVariant(); - if (role != Qt::DisplayRole) - return QVariant(); - if (section == keyColumn()) - return tr("Key"); - if (section == valueColumn()) - return tr("Value"); - return QVariant(); -} - -int SettingsModel::columnCount(const QModelIndex &parent) const -{ - Q_UNUSED(parent); - return 2; -} - -int SettingsModel::rowCount(const QModelIndex &parent) const -{ - if (parent.column() > 0) - return 0; - const Node * const node = d->indexToNode(parent); - Q_ASSERT(node); - return node->children.count(); -} - -QVariant SettingsModel::data(const QModelIndex &index, int role) const -{ - if (role != Qt::DisplayRole && role != Qt::EditRole) - return QVariant(); - const Node * const node = d->indexToNode(index); - if (!node) - return QVariant(); - if (index.column() == keyColumn()) - return node->name; - if (index.column() == valueColumn() && node->children.isEmpty()) - return node->value; - return QVariant(); -} - -bool SettingsModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - if (!index.isValid() || role != Qt::EditRole) - return false; - Node * const node = d->indexToNode(index); - if (!node) - return false; - const QString valueString = value.toString(); - QString *toChange = 0; - if (index.column() == keyColumn() && !valueString.isEmpty() - && !node->parent->hasDirectChildWithName(valueString)) { - toChange = &node->name; - } else if (index.column() == valueColumn() && valueString != node->value) { - toChange = &node->value; - } - - if (toChange) { - *toChange = valueString; - emit dataChanged(index, index); - d->dirty = true; - } - return toChange; -} - -QModelIndex SettingsModel::index(int row, int column, const QModelIndex &parent) const -{ - const Node * const parentNode = d->indexToNode(parent); - Q_ASSERT(parentNode); - if (parentNode->children.count() <= row) - return QModelIndex(); - return createIndex(row, column, parentNode->children.at(row)); -} - -QModelIndex SettingsModel::parent(const QModelIndex &child) const -{ - Node * const childNode = static_cast<Node *>(child.internalPointer()); - Q_ASSERT(childNode); - Node * const parentNode = childNode->parent; - if (parentNode == &d->rootNode) - return QModelIndex(); - const Node * const grandParentNode = parentNode->parent; - Q_ASSERT(grandParentNode); - return createIndex(grandParentNode->children.indexOf(parentNode), 0, parentNode); -} - - -void SettingsModel::SettingsModelPrivate::readSettings() -{ - qDeleteAll(rootNode.children); - rootNode.children.clear(); - foreach (const QString &topLevelKey, settings->directChildren(QString())) - addNode(&rootNode, topLevelKey); - dirty = false; -} - -void SettingsModel::SettingsModelPrivate::addNode(Node *parentNode, - const QString &fullyQualifiedName) -{ - Node * const node = new Node; - node->name = fullyQualifiedName.mid(fullyQualifiedName.lastIndexOf(QLatin1Char('.')) + 1); - node->value = settingsValueToRepresentation(settings->value(fullyQualifiedName)); - node->parent = parentNode; - parentNode->children << node; - foreach (const QString &childKey, settings->directChildren(fullyQualifiedName)) - addNode(node, fullyQualifiedName + QLatin1Char('.') + childKey); - dirty = true; -} - -void SettingsModel::SettingsModelPrivate::doSave(const Node *node, const QString &prefix) -{ - if (node->children.isEmpty()) { - settings->setValue(prefix + node->name, representationToSettingsValue(node->value)); - return; - } - - const QString newPrefix = prefix + node->name + QLatin1Char('.'); - foreach (const Node * const child, node->children) - doSave(child, newPrefix); -} - -Node *SettingsModel::SettingsModelPrivate::indexToNode(const QModelIndex &index) -{ - return index.isValid() ? static_cast<Node *>(index.internalPointer()) : &rootNode; -} - - -QString Node::uniqueChildName() const -{ - QString newName = QLatin1String("newkey"); - bool unique; - do { - unique = true; - foreach (const Node *childNode, children) { - if (childNode->name == newName) { - unique = false; - newName += QLatin1Char('_'); - break; - } - } - } while (!unique); - return newName; -} - -bool Node::hasDirectChildWithName(const QString &name) const -{ - foreach (const Node * const child, children) { - if (child->name == name) - return true; - } - return false; -} diff --git a/src/app/config-ui/settingsmodel.h b/src/app/config-ui/settingsmodel.h deleted file mode 100644 index 9ef8c41e6..000000000 --- a/src/app/config-ui/settingsmodel.h +++ /dev/null @@ -1,61 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt Build Suite. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://www.qt.io/licensing. For further information -** use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -****************************************************************************/ -#include <QAbstractItemModel> - -class SettingsModel : public QAbstractItemModel -{ - Q_OBJECT -public: - SettingsModel(const QString &settingsDir, QObject *parent = 0); - ~SettingsModel(); - - int keyColumn() const { return 0; } - int valueColumn() const { return 1; } - bool hasUnsavedChanges() const; - - void reload(); - void save(); - - void addNewKey(const QModelIndex &parent); - void removeKey(const QModelIndex &index); - - Qt::ItemFlags flags(const QModelIndex &index) const; - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; - int columnCount(const QModelIndex &parent = QModelIndex()) const; - int rowCount(const QModelIndex &parent = QModelIndex()) const; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex &child) const; - -private: - class SettingsModelPrivate; - SettingsModelPrivate * const d; -}; |