# HG changeset patch # User Andre Heinecke # Date 1427289826 -3600 # Node ID e4748da7140b8afd10a1f18fa9295299dffd1838 # Parent 248d5d1cdb383fba0bb204a32d0716a4305e033c Add filter functionalty to metadataview diff -r 248d5d1cdb38 -r e4748da7140b src/CMakeLists.txt --- a/src/CMakeLists.txt Mon Mar 23 19:10:01 2015 +0100 +++ b/src/CMakeLists.txt Wed Mar 25 14:23:46 2015 +0100 @@ -20,6 +20,7 @@ metadataview.cpp pngplayer.cpp imagelabel.cpp + filterwidget.cpp ) find_package(Qt5LinguistTools) diff -r 248d5d1cdb38 -r e4748da7140b src/filterwidget.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filterwidget.cpp Wed Mar 25 14:23:46 2015 +0100 @@ -0,0 +1,64 @@ +#include "filterwidget.h" + +#include +#include +#include +#include +#include +#include +#include + +FilterWidget::FilterWidget(QSortFilterProxyModel *model, QWidget *parent, Qt::WindowFlags f) : + QWidget(parent, f), + mModel(model) { + Q_ASSERT(model); + setupGUI(); + + connect(mModel, &QSortFilterProxyModel::sourceModelChanged, + this, &FilterWidget::headersChanged); + void (QComboBox:: *idxChanged)(int) = &QComboBox::currentIndexChanged; + connect(mCombo, idxChanged, this, + &FilterWidget::filterChanged); + //connect(mCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged())); + connect(mEditLine, &QLineEdit::textChanged, this, + &FilterWidget::filterChanged); +} + +void FilterWidget::headersChanged() { + QAbstractItemModel *newSource = mModel->sourceModel(); + Q_ASSERT(newSource); + mCombo->clear(); + for (int i=0; i < newSource->columnCount(); i++) { + mCombo->addItem(newSource->headerData(i, Qt::Horizontal).toString()); + } +} + +void FilterWidget::filterChanged() { + QString filterText = mEditLine->text(); + if (filterText.size() < 1) { + return; + } + mModel->setFilterKeyColumn(mCombo->currentIndex()); + mModel->setFilterWildcard(filterText); +} + +void FilterWidget::setupGUI() { + QHBoxLayout *root = new QHBoxLayout; + + QGroupBox *baseGroup = new QGroupBox(tr("Filter")); + root->addWidget(baseGroup); + + QHBoxLayout *baseLayout = new QHBoxLayout; + baseGroup->setLayout(baseLayout); + + mCombo = new QComboBox; + baseLayout->addWidget(mCombo); + + mEditLine = new QLineEdit; + mEditLine->setPlaceholderText(tr("Filter expression")); + mEditLine->setClearButtonEnabled(true); + baseLayout->addWidget(mEditLine); + + setLayout(root); +} + diff -r 248d5d1cdb38 -r e4748da7140b src/filterwidget.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filterwidget.h Wed Mar 25 14:23:46 2015 +0100 @@ -0,0 +1,36 @@ +#ifndef FILTERWIDGET_H +#define FILTERWIDGET_H +/* Copyright (C) 2014 by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=2) + * and comes with ABSOLUTELY NO WARRANTY! + * See LICENSE.txt for details. + */ +#include + +class QSortFilterProxyModel; +class QComboBox; +class QLineEdit; +class FilterWidget: public QWidget +{ + Q_OBJECT + +public: + /**@brief construct a filterwidget for the model model.*/ + FilterWidget (QSortFilterProxyModel *model, + QWidget * parent = 0, Qt::WindowFlags f = 0); + +protected: + void setupGUI(); + +protected slots: + void headersChanged(); + + void filterChanged(); + +private: + QSortFilterProxyModel *mModel; + QComboBox *mCombo; + QLineEdit *mEditLine; +}; +#endif // FILTERWIDGET_H diff -r 248d5d1cdb38 -r e4748da7140b src/metadataview.cpp --- a/src/metadataview.cpp Mon Mar 23 19:10:01 2015 +0100 +++ b/src/metadataview.cpp Wed Mar 25 14:23:46 2015 +0100 @@ -1,5 +1,6 @@ #include "metadataview.h" #include "qxtcsvmodel.h" +#include "filterwidget.h" #include #include @@ -20,11 +21,17 @@ connect(mView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MetaDataView::viewSelectionChanged); + connect(mSortModel, &QSortFilterProxyModel::dataChanged, + this, &MetaDataView::dataChanged); return; } void MetaDataView::setupGUI() { QVBoxLayout *baseLayout = new QVBoxLayout; + + FilterWidget *filterWidget = new FilterWidget(mSortModel); + baseLayout->addWidget(filterWidget); + mView = new QTableView; mView->setModel(mSortModel); @@ -52,8 +59,26 @@ return QString(); } +void MetaDataView::dataChanged() +{ + QItemSelectionModel *selection = mView->selectionModel(); + QModelIndexList selected = selection->selectedIndexes(); + + qDebug() << "Data Changed."; + if (selected.isEmpty()) { + /* Nothing selected still we need to emit this signal to update + * the viewer otherwise selection changed handles it. */ + emit selectionChanged(QString(), 0, mSortModel->rowCount() - 1, + QDateTime()); + } +} + void MetaDataView::viewSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { + if (selected.indexes().isEmpty()) { + /* Nothing selected */ + return; + } /* One row selected */ Q_ASSERT(selected.indexes().count() == mCSVModel->columnCount()); const QModelIndex idx = selected.indexes()[0]; diff -r 248d5d1cdb38 -r e4748da7140b src/metadataview.h --- a/src/metadataview.h Mon Mar 23 19:10:01 2015 +0100 +++ b/src/metadataview.h Wed Mar 25 14:23:46 2015 +0100 @@ -42,6 +42,9 @@ /** @brief internal slot to handle table view selection changes */ void viewSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected); + /** @brief the data has been changed. Could be filter. */ + void dataChanged(); + public slots: /**@brief selects the next row and emits a selection changed signal */ void selectNextRow();