diff options
author | Arno <am@disconnect.de> | 2013-10-13 07:25:50 +0200 |
---|---|---|
committer | Arno <am@disconnect.de> | 2013-10-13 07:25:50 +0200 |
commit | e07de4e0a1bd56aab8f4ee81d6db252e442c3d7a (patch) | |
tree | 02b62d34152e481917ba1bef6a7191e4e3465c72 | |
parent | 5beb56a09363197e23a630d3fd2f71899d7f00a0 (diff) | |
download | SheMov-e07de4e0a1bd56aab8f4ee81d6db252e442c3d7a.tar.gz SheMov-e07de4e0a1bd56aab8f4ee81d6db252e442c3d7a.tar.bz2 SheMov-e07de4e0a1bd56aab8f4ee81d6db252e442c3d7a.zip |
Add filter bar to MappingTreeWidget
Move matchRecursive from ArchiveModel to SmTreeModel so we can use it
here, too.
-rw-r--r-- | archivemodel.cpp | 43 | ||||
-rw-r--r-- | archivemodel.h | 2 | ||||
-rw-r--r-- | mappingtreewidget.cpp | 50 | ||||
-rw-r--r-- | mappingtreewidget.h | 28 | ||||
-rw-r--r-- | smtreemodel.cpp | 43 | ||||
-rw-r--r-- | smtreemodel.h | 2 |
6 files changed, 117 insertions, 51 deletions
diff --git a/archivemodel.cpp b/archivemodel.cpp index 0f266ca..212c60d 100644 --- a/archivemodel.cpp +++ b/archivemodel.cpp @@ -163,38 +163,6 @@ bool ArchiveModel::setData(const QModelIndex &idx, const QVariant &value, int ro return false; } -bool ArchiveModel::matchRecursive(const QModelIndex &parent, const QRegExp ®ex, int column) const { - if(!parent.isValid()){ - return false; - } - // first try the parent itself - QString value = parent.data().toString(); - if(value.isEmpty()){ - return false; - } - if(value.contains(regex)){ - return true; - } - - // no match, check for children - SmTreeItem *item = static_cast<SmTreeItem*>(parent.internalPointer()); - if(!item->childCount()){ - //leaf, chech parents - return checkParents(item, regex, column); - } - - // we have children, traverse them - bool retval = false; - for(int i = 0; i < item->childCount(); ++i){ - QModelIndex newIdx = createIndex(i, column, item->child(i)); - retval = matchRecursive(newIdx, regex, column); - if(retval){ - break; - } - } - return retval; -} - bool ArchiveModel::removeNode(const QModelIndex &idx){ if(!idx.isValid()){ return false; @@ -619,17 +587,6 @@ void ArchiveModel::collectorFinished(QObject *thread){ emit message(msg); } -bool ArchiveModel::checkParents(const SmTreeItem *item, const QRegExp ®ex, int column) const { - while(item != root()){ - QString value = item->data(column).toString(); - if(value.contains(regex)){ - return true; - } - item = item->parent(); - } - return false; -} - void ArchiveModel::emitDatabaseError(const QSqlError &e){ QString databaseText = e.databaseText().isEmpty() ? tr("(none)") : e.databaseText(); QString driverText = e.driverText().isEmpty() ? tr("(none)") : e.driverText(); diff --git a/archivemodel.h b/archivemodel.h index 12b76be..2985e55 100644 --- a/archivemodel.h +++ b/archivemodel.h @@ -35,7 +35,6 @@ class ArchiveModel : public SmTreeModel { const QHash<QString, int> availableOrdersHash() const { return mAvailableOrders; } virtual QVariant data(const QModelIndex &index, int role) const; virtual bool setData(const QModelIndex &idx, const QVariant &value, int role); - virtual bool matchRecursive(const QModelIndex &parent, const QRegExp ®ex, int column = 0) const; virtual bool removeNode(const QModelIndex &idx); QStringList indexToPath(const QModelIndex &idx) const; QModelIndexList pathToIndex(const QStringList &path) const; @@ -72,7 +71,6 @@ class ArchiveModel : public SmTreeModel { void collectorFinished(QObject *thread); private: - bool checkParents(const SmTreeItem *item, const QRegExp ®ex, int column) const; void emitDatabaseError(const QSqlError &e); void writeCache(int o, SmTreeItem *rItem); void writeRecursive(SmTreeItem *start, QDataStream &stream); diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp index 873afd2..dcf96e1 100644 --- a/mappingtreewidget.cpp +++ b/mappingtreewidget.cpp @@ -30,7 +30,7 @@ MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){ //setup model mModel = static_cast<MappingTreeModel*>(SmGlobals::instance()->model("MappingTree")); - mProxy = new QSortFilterProxyModel(this); + mProxy = new MappingTreeProxy(this); mProxy->setSourceModel(mModel); mTypesModel = new QStringListModel(this); mTypesModel->setStringList(mModel->mappingTypeNames()); @@ -46,11 +46,12 @@ MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){ mTree->expandAll(); connect(mModel, SIGNAL(needExpansion()), mTree, SLOT(expandAll())); connect(mTree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged())); + /* Hide buttons, this doesn't really work, + * but keep them for future use :) */ mTypeBox = new QComboBox; mTypeBox->setModel(mTypesModel); connect(mTypeBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(typeChanged(QString))); - /* Hide buttons, this doesn't really work, - * but keep them for future use :) */ + mTypeBox->setHidden(true); mAddType = new QPushButton(tr("Add &type")); connect(mAddType, SIGNAL(clicked()), this, SLOT(addType())); mAddType->setHidden(true); @@ -76,6 +77,20 @@ MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){ typeChanged(lastType); } + // filter + mFilter = new QLineEdit; + connect(mFilter, SIGNAL(returnPressed()), this, SLOT(filter())); + mDoFilter = new QPushButton(tr("Filter")); + connect(mDoFilter, SIGNAL(clicked()), this, SLOT(filter())); + mClearFilter = new QPushButton(tr("Clear")); + connect(mClearFilter, SIGNAL(clicked()), this, SLOT(clearFilter())); + QLabel *filterLabel = new QLabel(tr("Filter")); + QHBoxLayout *filterLayout = new QHBoxLayout; + filterLayout->addWidget(filterLabel); + filterLayout->addWidget(mFilter); + filterLayout->addWidget(mDoFilter); + filterLayout->addWidget(mClearFilter); + //setup actions mAddChildA = new QAction(tr("Add..."), this); connect(mAddChildA, SIGNAL(triggered()), this, SLOT(addChild())); @@ -93,6 +108,7 @@ MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){ //widget layout and tab order QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(mTypeBox); + mainLayout->addLayout(filterLayout); mainLayout->addLayout(typesButtonLayout); mainLayout->addWidget(mTree); setLayout(mainLayout); @@ -259,6 +275,18 @@ void MappingTreeWidget::moveChild(){ } } +void MappingTreeWidget::filter(){ + QString filter = mFilter->text(); + mProxy->setFilter(filter); + mTree->expandAll(); +} + +void MappingTreeWidget::clearFilter(){ + mFilter->clear(); + mProxy->setFilter(QString()); + mTree->expandAll(); +} + const QModelIndex MappingTreeWidget::selected() const{ QModelIndexList sel = mTree->selectionModel()->selectedRows(); if(sel.isEmpty()){ @@ -392,3 +420,19 @@ MappingEditDialog::MappingEditDialog(QWidget *parent, Qt::WindowFlags f) : QDial mainLayout->addLayout(buttonLayout); setLayout(mainLayout); } + +MappingTreeProxy::MappingTreeProxy(QObject *parent) : QSortFilterProxyModel(parent) {} + +void MappingTreeProxy::setFilter(const QString &filter){ + mFilter = QRegExp(filter); + invalidateFilter(); +} + +bool MappingTreeProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const{ + if(mFilter.isEmpty()){ + return true; + } + QModelIndex nameIdx = sourceModel()->index(sourceRow, MappingTreeModel::Name, sourceParent); + MappingTreeModel *model = qobject_cast<MappingTreeModel*>(sourceModel()); + return model->matchRecursive(nameIdx, mFilter); +} diff --git a/mappingtreewidget.h b/mappingtreewidget.h index d72dffa..9fe5739 100644 --- a/mappingtreewidget.h +++ b/mappingtreewidget.h @@ -8,7 +8,8 @@ #ifndef MAPPINGTREEWIDGET_H #define MAPPINGTREEWIDGET_H -#include <QtWidgets/QDialog> +#include <QDialog> +#include <QSortFilterProxyModel> #include "smtreeview.h" @@ -16,9 +17,10 @@ class MappingTreeView; class MappingTreeModel; class QComboBox; class QCheckBox; -class QSortFilterProxyModel; class QStringListModel; class MappingTreeResultModel; +class QLineEdit; +class MappingTreeProxy; // defined in mappingtreemodel.h struct MappingData; @@ -44,6 +46,8 @@ class MappingTreeWidget : public QWidget { void editChild(); void selectionChanged(); void moveChild(); + void filter(); + void clearFilter(); signals: void mappingChanged(int); @@ -52,9 +56,12 @@ class MappingTreeWidget : public QWidget { const QModelIndex selected() const; MappingTreeView *mTree; MappingTreeModel *mModel; - QSortFilterProxyModel *mProxy; + MappingTreeProxy *mProxy; QStringListModel *mTypesModel; QComboBox *mTypeBox; + QLineEdit *mFilter; + QPushButton *mDoFilter; + QPushButton *mClearFilter; QPushButton *mAddType; QPushButton *mDeleteType; QAction *mAddChildA; @@ -124,4 +131,19 @@ class MappingEditDialog : public QDialog { QPushButton *mCancel; }; +class MappingTreeProxy : public QSortFilterProxyModel { + Q_OBJECT + public: + explicit MappingTreeProxy(QObject *parent = 0); + + public slots: + void setFilter(const QString &filter); + + protected: + virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; + + private: + QRegExp mFilter; +}; + #endif // MAPPINGTREEWIDGET_H diff --git a/smtreemodel.cpp b/smtreemodel.cpp index b909fcd..b52859c 100644 --- a/smtreemodel.cpp +++ b/smtreemodel.cpp @@ -166,6 +166,49 @@ QModelIndex SmTreeModel::findRecursive(const QVariant &value, int column, const return QModelIndex(); } +bool SmTreeModel::matchRecursive(const QModelIndex &parent, const QRegExp ®ex, int column) const { + if(!parent.isValid()){ + return false; + } + // first try the parent itself + QString value = parent.data().toString(); + if(value.isEmpty()){ + return false; + } + if(value.contains(regex)){ + return true; + } + + // no match, check for children + SmTreeItem *item = static_cast<SmTreeItem*>(parent.internalPointer()); + if(!item->childCount()){ + //leaf, chech parents + return checkParents(item, regex, column); + } + + // we have children, traverse them + bool retval = false; + for(int i = 0; i < item->childCount(); ++i){ + QModelIndex newIdx = createIndex(i, column, item->child(i)); + retval = matchRecursive(newIdx, regex, column); + if(retval){ + break; + } + } + return retval; +} + +bool SmTreeModel::checkParents(const SmTreeItem *item, const QRegExp ®ex, int column) const { + while(item != root()){ + QString value = item->data(column).toString(); + if(value.contains(regex)){ + return true; + } + item = item->parent(); + } + return false; +} + bool SmTreeModel::setRoot(SmTreeItem *rootItem){ if(mRootItem){ beginResetModel(); diff --git a/smtreemodel.h b/smtreemodel.h index 8d46606..d72d53c 100644 --- a/smtreemodel.h +++ b/smtreemodel.h @@ -38,6 +38,8 @@ class SmTreeModel : public QAbstractItemModel { virtual bool setData(const QModelIndex &index, const QVariant &value, int role); virtual QModelIndex find(const QVariant &value, int column = 0, const QModelIndex &parent = QModelIndex()) const; virtual QModelIndex findRecursive(const QVariant &value, int column, const QModelIndex &start) const; + virtual bool matchRecursive(const QModelIndex &parent, const QRegExp ®ex, int column = 0) const; + virtual bool checkParents(const SmTreeItem *item, const QRegExp ®ex, int column) const; // root + parent item bool setRoot(SmTreeItem *rootItem); |