From 5177db6d63e48e7ff6ee9ffcef2dee9d77b80075 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 19 Jan 2016 17:23:23 +0100 Subject: Re-introduce tree structure: Allow server-defined sections --- src/dialog.cpp | 1 + src/session.h | 1 + src/sessiontreemodel.cpp | 48 +++++++++++++++++++++++++++++++++--------------- src/vsession.h | 7 +++++++ src/xsession.h | 4 ++++ 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/dialog.cpp b/src/dialog.cpp index fdf3d78..1d497d2 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -481,6 +481,7 @@ void Dialog::setListModel(QAbstractItemModel *model) { old = ui->treeView->model(); } ui->treeView->setModel(model); + ui->treeView->expandAll(); if (old != NULL) { old->deleteLater(); } diff --git a/src/session.h b/src/session.h index 515bd2d..8e41120 100644 --- a/src/session.h +++ b/src/session.h @@ -17,6 +17,7 @@ class Session { virtual QIcon icon() const = 0; virtual bool run() const = 0; virtual int type() const = 0; + virtual QString section() const = 0; virtual bool operator<(const Session& s) const = 0; diff --git a/src/sessiontreemodel.cpp b/src/sessiontreemodel.cpp index cf4f2cf..69c7302 100644 --- a/src/sessiontreemodel.cpp +++ b/src/sessiontreemodel.cpp @@ -125,8 +125,27 @@ QModelIndex SessionTreeModel::parent(const QModelIndex &index) const { } void SessionTreeModel::addItems(const QList& sessions) { + SessionTreeItem* parentItem; + foreach (Session* s, sessions) { - root_->appendChild(new SessionTreeItem(s, root_)); + bool sectionExists = false; + QString section(s->section()); + + for (int i = 0; i < root_->childCount(); ++i) { + SessionTreeItem* item = root_->child(i); + if (item->text() == section) { + parentItem = item; + sectionExists = true; + break; + } + } + + if (!sectionExists) { + parentItem = new SessionTreeItem(section, root_); + root_->appendChild(parentItem); + } + + parentItem->appendChild(new SessionTreeItem(s, parentItem)); } } @@ -143,32 +162,31 @@ void SessionTreeModel::removeItem(const QString& name) { } } +static void lookDeeper(QList& result, QList& items, SessionTreeItem* root) { + for (int i = 0; i < root->childCount(); ++i) { + SessionTreeItem* item = root->child(i); + if (item->session() != NULL && item->session()->containsKeywords(items)) { + result.append(const_cast(item->session())); + } + lookDeeper(result, items, item); + } +} + QList SessionTreeModel::lookForItem(const QString& label) { QList result; + QList items; if (label.startsWith("\"")) { QString searchTerm = label; searchTerm.remove(0, 1); if (label.endsWith("\"")) { searchTerm.chop(1); } - QList items; items.append(searchTerm); - for (int i = 0; i < root_->childCount(); ++i) { - SessionTreeItem* item = root_->child(i); - if (item->session() != NULL && item->session()->containsKeywords(items)) { - result.append(const_cast(item->session())); - } - } } else { - QList items = label.split(" ", QString::SkipEmptyParts); - for (int i = 0; i < root_->childCount(); ++i) { - SessionTreeItem* item = root_->child(i); - if (item->session() != NULL && item->session()->containsKeywords(items)) { - result.append(const_cast(item->session())); - } - } + items = label.split(" ", QString::SkipEmptyParts); } + lookDeeper(result, items, root_); return result; diff --git a/src/vsession.h b/src/vsession.h index 22f3a94..34c7457 100644 --- a/src/vsession.h +++ b/src/vsession.h @@ -39,6 +39,13 @@ class VSession : public Session { return getAttribute("os"); } + QString section() const { + QString attr = getAttribute("section"); + if (!attr.isEmpty()) + return attr; + return QObject::tr("Priority: ").append(QString::number(priority())); + } + QString getAttribute(const QString& nodeName, const QString& attribute = "param") const; QList keywords() const; diff --git a/src/xsession.h b/src/xsession.h index 75deffc..10b3b0b 100644 --- a/src/xsession.h +++ b/src/xsession.h @@ -19,6 +19,10 @@ class XSession : public Session { bool isValid() const; int priority() const; + QString section() const { + return QObject::tr("X-Sessions"); + } + QString shortDescription() const { return this->name_; } -- cgit v1.2.3-55-g7522