From b72f1d53558344392daa916f69ece7db8c7a9d89 Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Fri, 21 Mar 2014 17:30:03 +0100 Subject: added functionality to filter box --- src/dialog.cpp | 12 +++++++++--- src/session.h | 2 ++ src/sessiontreemodel.cpp | 28 ++++++++++++++++++++++++---- src/vsession.cpp | 18 ++++++++++++++++++ src/vsession.h | 1 + src/xsession.cpp | 4 ++++ src/xsession.h | 2 ++ 7 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/dialog.cpp b/src/dialog.cpp index fc9a435..2a50c11 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -34,6 +34,7 @@ Dialog::Dialog(QWidget *parent) activeTab = 0; ui->tabButtonLocal->setChecked(true); + ui->filterEdit->setEnabled(false); setListModel(model_[0]); @@ -415,12 +416,15 @@ void Dialog::onTabButtonChanged(int tab) { if (tab == 0) { ui->tabButtonMyClasses->setChecked(false); ui->tabButtonAllClasses->setChecked(false); + ui->filterEdit->setEnabled(false); } else if (tab == 1) { ui->tabButtonLocal->setChecked(false); ui->tabButtonAllClasses->setChecked(false); + ui->filterEdit->setEnabled(true); } else { ui->tabButtonLocal->setChecked(false); ui->tabButtonMyClasses->setChecked(false); + ui->filterEdit->setEnabled(true); } // load the new list @@ -443,13 +447,15 @@ void Dialog::on_filterEdit_textChanged() { } setListModel(newModel); + + // reconnect the treeModel + QObject::connect(ui->treeView->selectionModel(), SIGNAL(currentChanged ( const QModelIndex&, const QModelIndex&)), + this, SLOT(treeView_selectionChanged(const QModelIndex&, const QModelIndex&))); + if (ui->treeView->selectionModel()->selectedRows(0).count() == 0) { ui->treeView->selectionModel()->clearSelection(); ui->treeView->selectionModel()->setCurrentIndex(ui->treeView->model()->index(0, 0, ui->treeView->rootIndex()), QItemSelectionModel::Select); } - // reconnect the treeModel - QObject::connect(ui->treeView->selectionModel(), SIGNAL(currentChanged ( const QModelIndex&, const QModelIndex&)), - this, SLOT(treeView_selectionChanged(const QModelIndex&, const QModelIndex&))); } void Dialog::setListModel(QAbstractItemModel *model) { diff --git a/src/session.h b/src/session.h index f3d4b4b..e1af027 100644 --- a/src/session.h +++ b/src/session.h @@ -22,5 +22,7 @@ class Session { const static int XSESSION = 0; const static int VSESSION = 1; + virtual bool containsKeywords(const QList& keywords) const = 0; + }; #endif /*VMCHOOSER_SESSION_H_*/ diff --git a/src/sessiontreemodel.cpp b/src/sessiontreemodel.cpp index 2078aaa..722850e 100644 --- a/src/sessiontreemodel.cpp +++ b/src/sessiontreemodel.cpp @@ -172,13 +172,33 @@ void SessionTreeModel::removeItem(const QString& name) { QList SessionTreeModel::lookForItem(const QString& label) { QList result; - for (int i = 0; i < root_->childCount(); ++i) { - SessionTreeItem* item = root_->child(i); - if (item->session()->shortDescription().contains(label, Qt::CaseInsensitive)) { - result.append(const_cast(item->session())); + 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()->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()->containsKeywords(items)) { + result.append(const_cast(item->session())); + } } } + return result; + } void SessionTreeModel::updateView() { diff --git a/src/vsession.cpp b/src/vsession.cpp index ba7a478..e8e301e 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -82,6 +82,24 @@ void VSession::readKeywords() { } } +bool VSession::containsKeywords(const QList& keywords) const { + for (int j = 0; j < keywords.length(); ++j) { + bool keywordFlag = true; + if (!this->shortDescription().contains(keywords[j], Qt::CaseInsensitive) + && !this->description().contains(keywords[j], Qt::CaseInsensitive) + && !this->getAttribute("creator", "param").contains(keywords[j], Qt::CaseInsensitive)) { + keywordFlag = false; + for (int i = 0; i < this->keywords().length(); ++i) { + if (this->keywords()[i].contains(keywords[j], Qt::CaseInsensitive)) { + keywordFlag = true; + } + } + if (!keywordFlag) return false; + } + } + return true; +} + QString VSession::getNodeText(const QString& nodeName) const { return this->doc_.namedItem(nodeName).toText().data(); } diff --git a/src/vsession.h b/src/vsession.h index 50a51f7..9cb7a69 100644 --- a/src/vsession.h +++ b/src/vsession.h @@ -42,6 +42,7 @@ class VSession : public Session { QString getAttribute(const QString& nodeName, const QString& attribute = "param") const; QList keywords() const; + bool containsKeywords(const QList& keywords) const; QString getNodeText(const QString& nodeName) const; void addNodeWithAttribute(const QString& nodeName, diff --git a/src/xsession.cpp b/src/xsession.cpp index f4cc3d9..3cd0e7e 100644 --- a/src/xsession.cpp +++ b/src/xsession.cpp @@ -147,3 +147,7 @@ bool XSession::operator<(const Session& other) const { } return false; } + +bool XSession::containsKeywords(const QList&) const { + return true; +} diff --git a/src/xsession.h b/src/xsession.h index 1d36d64..4140c38 100644 --- a/src/xsession.h +++ b/src/xsession.h @@ -37,6 +37,8 @@ class XSession : public Session { static QList readSessions(const QString& path); + bool containsKeywords(const QList& keywords) const; + private: QString name_; QString exec_; -- cgit v1.2.3-55-g7522