diff options
Diffstat (limited to 'src/sessiontreemodel.cpp')
-rw-r--r-- | src/sessiontreemodel.cpp | 48 |
1 files changed, 33 insertions, 15 deletions
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<Session*>& 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<Session*>& result, QList<QString>& 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<Session*>(item->session())); + } + lookDeeper(result, items, item); + } +} + QList<Session*> SessionTreeModel::lookForItem(const QString& label) { QList<Session*> result; + QList<QString> items; if (label.startsWith("\"")) { QString searchTerm = label; searchTerm.remove(0, 1); if (label.endsWith("\"")) { searchTerm.chop(1); } - QList<QString> 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<Session*>(item->session())); - } - } } else { - QList<QString> 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<Session*>(item->session())); - } - } + items = label.split(" ", QString::SkipEmptyParts); } + lookDeeper(result, items, root_); return result; |