changeset 4:e4748da7140b

Add filter functionalty to metadataview
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 25 Mar 2015 14:23:46 +0100
parents 248d5d1cdb38
children 107e435cb569
files src/CMakeLists.txt src/filterwidget.cpp src/filterwidget.h src/metadataview.cpp src/metadataview.h
diffstat 5 files changed, 129 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- /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 <QComboBox>
+#include <QHBoxLayout>
+#include <QGroupBox>
+#include <QSortFilterProxyModel>
+#include <QAbstractItemModel>
+#include <QLineEdit>
+#include <QDebug>
+
+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);
+}
+
--- /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 <QWidget>
+
+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
--- 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 <QTextCodec>
 #include <QTableView>
@@ -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];
--- 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();
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)