changeset 88:3916cb3c9105

Add new FilterSort model that allows to include empty values
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 19 Jun 2015 14:26:16 +0200
parents 3b3a1384eb5f
children b8c7644a9d49
files src/CMakeLists.txt src/filterwidget.cpp src/filterwidget.h src/folderselectdialog.cpp src/includeemptysortmodel.cpp src/includeemptysortmodel.h src/metadataview.cpp src/metadataview.h
diffstat 8 files changed, 106 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Thu Jun 18 19:35:22 2015 +0200
+++ b/src/CMakeLists.txt	Fri Jun 19 14:26:16 2015 +0200
@@ -23,6 +23,7 @@
    imagelabel.cpp
    filterwidget.cpp
    folderselectdialog.cpp
+   includeemptysortmodel.cpp
    icons/icon.rc
 )
 
--- a/src/filterwidget.cpp	Thu Jun 18 19:35:22 2015 +0200
+++ b/src/filterwidget.cpp	Fri Jun 19 14:26:16 2015 +0200
@@ -15,14 +15,14 @@
 #include <QLineEdit>
 #include <QDebug>
 
-FilterWidget::FilterWidget(QSortFilterProxyModel *model, QWidget *parent, Qt::WindowFlags f) :
+FilterWidget::FilterWidget(QSortFilterProxyModel *model, bool addWithEmptyChk, 
+                           QWidget *parent, Qt::WindowFlags f) :
     QWidget(parent, f),
-    mModel(model) {
+    mModel(model),
+    mWithEmptyChk(addWithEmptyChk) {
     Q_ASSERT(model);
     setupGUI();
 
-    connect(mModel, &QSortFilterProxyModel::sourceModelChanged,
-            this, &FilterWidget::headersChanged);
     void (QComboBox:: *idxChanged)(int) = &QComboBox::currentIndexChanged;
     connect(mCombo, idxChanged, this,
             &FilterWidget::filterChanged);
@@ -32,6 +32,7 @@
 }
 
 void FilterWidget::headersChanged() {
+    mCombo->blockSignals(true);
     QAbstractItemModel *newSource = mModel->sourceModel();
     Q_ASSERT(newSource);
     mCombo->clear();
@@ -48,6 +49,7 @@
             }
         }
     }
+    mCombo->blockSignals(false);
 }
 
 void FilterWidget::filterChanged() {
@@ -74,6 +76,12 @@
     mEditLine->setClearButtonEnabled(true);
     baseLayout->addWidget(mEditLine);
 
+    if (mWithEmptyChk) {
+        mEmptyChk = new QCheckBox(tr("Include empty fields"));
+        baseLayout->addWidget(mEmptyChk);
+        connect(mEmptyChk, &QCheckBox::stateChanged, this, &FilterWidget::includeEmptyChanged);
+    }
+
     setLayout(root);
 }
 
--- a/src/filterwidget.h	Thu Jun 18 19:35:22 2015 +0200
+++ b/src/filterwidget.h	Fri Jun 19 14:26:16 2015 +0200
@@ -12,6 +12,7 @@
 
 class QSortFilterProxyModel;
 class QComboBox;
+class QCheckBox;
 class QLineEdit;
 /**
  * @class FilterWidget
@@ -22,26 +23,35 @@
     Q_OBJECT
 
 public:
-    /**@brief construct a filterwidget for the model model.*/
-    FilterWidget (QSortFilterProxyModel *model,
+    /**@brief construct a filterwidget for the model model.
+     *
+     * @param model The model to filter
+     * @param addWithEmptyChk wether or not to add the include empty checkbox.
+     * */
+    FilterWidget (QSortFilterProxyModel *model, bool addWithEmptyChk,
             QWidget * parent = 0, Qt::WindowFlags f = 0);
 
 Q_SIGNALS:
     void filterHasChanged();
 
+    void includeEmptyChanged(int state);
+
 protected:
     void setupGUI();
 
-protected slots:
+public slots:
     void headersChanged();
 
+protected slots:
     void filterChanged();
 
 private:
     QSortFilterProxyModel *mModel;
     QComboBox *mCombo;
+    QCheckBox *mEmptyChk;
     QLineEdit *mEditLine;
     /* Maps the combo index to the model col index */
     QMap<int, int> mColFilterMap;
+    bool mWithEmptyChk;
 };
 #endif // FILTERWIDGET_H
--- a/src/folderselectdialog.cpp	Thu Jun 18 19:35:22 2015 +0200
+++ b/src/folderselectdialog.cpp	Fri Jun 19 14:26:16 2015 +0200
@@ -115,8 +115,10 @@
                 &FolderSelectDialog::goClicked);
     }
 
-    mFilterWidget = new FilterWidget(mSortModel);
+    mFilterWidget = new FilterWidget(mSortModel, false);
     base->addWidget(mFilterWidget);
+    connect(mSortModel, &QSortFilterProxyModel::sourceModelChanged,
+            mFilterWidget, &FilterWidget::headersChanged);
 
     mView = new QTableView;
     mView->setModel(mSortModel);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/includeemptysortmodel.cpp	Fri Jun 19 14:26:16 2015 +0200
@@ -0,0 +1,22 @@
+/* Copyright (C) 2015 by ETH Zürich
+ * Software engineering 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 "includeemptysortmodel.h"
+
+bool IncludeEmptySortModel::filterAcceptsRow(int row, const QModelIndex &parent) const
+{
+    bool parentAcceptsRow = QSortFilterProxyModel::filterAcceptsRow(row, parent);
+    if (!mIncludeEmpty || parentAcceptsRow) {
+        return parentAcceptsRow;
+    }
+
+    QModelIndex source_index = sourceModel()->index(row, filterKeyColumn(), parent);
+    if (!source_index.isValid()) // the column may not exist
+        return true;
+    QString key = sourceModel()->data(source_index).toString();
+    return key.isEmpty();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/includeemptysortmodel.h	Fri Jun 19 14:26:16 2015 +0200
@@ -0,0 +1,42 @@
+#ifndef INCLUDEEMPTYSORTMODEL_H
+#define INCLUDEEMPTYSORTMODEL_H
+/* Copyright (C) 2015 by ETH Zürich
+ * Software engineering 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.
+ */
+
+/**
+ * @file  metadataview.h
+ * @brief Table view of the meta data information
+ */
+/**
+ * @class IncludeEmptySortModel
+ * @brief Small wrapper around sort filter model to allow custom filtering
+ */
+#include <QModelIndex>
+#include <QSortFilterProxyModel>
+
+class IncludeEmptySortModel : public QSortFilterProxyModel
+{
+    Q_OBJECT
+
+public:
+    IncludeEmptySortModel() : QSortFilterProxyModel(), mIncludeEmpty(false) {}
+
+    /** @brief wrapper around the base class call that accepts empty
+     * values if includeEmpty is checked. */
+    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
+
+public slots:
+    void setIncludeEmpty(int state) {
+        mIncludeEmpty = state != Qt::Unchecked;
+        invalidate();
+    }
+
+private:
+    bool mIncludeEmpty;
+};
+#endif // INCLUDEEMPTYSORTMODEL_H
--- a/src/metadataview.cpp	Thu Jun 18 19:35:22 2015 +0200
+++ b/src/metadataview.cpp	Fri Jun 19 14:26:16 2015 +0200
@@ -8,6 +8,7 @@
 #include "metadataview.h"
 #include "qxtcsvmodel.h"
 #include "filterwidget.h"
+#include "includeemptysortmodel.h"
 #include "constants.h"
 
 #include <QTextCodec>
@@ -47,7 +48,7 @@
     QWidget(parent, f),
     mDateColIdx(-1) {
     /* Create models */
-    mSortModel = new QSortFilterProxyModel;
+    mSortModel = new IncludeEmptySortModel;
     mCSVModel = new numericSortCSVModel;
     setupGUI();
 
@@ -61,12 +62,14 @@
 void MetaDataView::setupGUI() {
     QVBoxLayout *baseLayout = new QVBoxLayout;
 
-    FilterWidget *filterWidget = new FilterWidget(mSortModel);
-    connect(filterWidget, &FilterWidget::filterHasChanged,
+    mFilterWidget = new FilterWidget(mSortModel, true);
+    connect(mFilterWidget, &FilterWidget::filterHasChanged,
             this, &MetaDataView::applyDefaultSort);
-    connect(filterWidget, &FilterWidget::filterHasChanged,
+    connect(mFilterWidget, &FilterWidget::includeEmptyChanged,
+            mSortModel, &IncludeEmptySortModel::setIncludeEmpty);
+    connect(mFilterWidget, &FilterWidget::filterHasChanged,
             this, &MetaDataView::selectFirstRow);
-    baseLayout->addWidget(filterWidget);
+    baseLayout->addWidget(mFilterWidget);
 
     mView = new QTableView;
     mView->setModel(mSortModel);
--- a/src/metadataview.h	Thu Jun 18 19:35:22 2015 +0200
+++ b/src/metadataview.h	Fri Jun 19 14:26:16 2015 +0200
@@ -20,6 +20,8 @@
 class QTableView;
 class QSortFilterProxyModel;
 class QxtCsvModel;
+class IncludeEmptySortModel;
+class FilterWidget;
 
 /**
  * @class MetaDataView
@@ -77,11 +79,12 @@
     /** @brief resizes the columns to headers content */
     void resizeColsToHeaders();
 
-protected:
+private:
     QxtCsvModel *mCSVModel;
-    QSortFilterProxyModel *mSortModel;
+    IncludeEmptySortModel *mSortModel;
     QTableView *mView;
     int mDateColIdx;
+    FilterWidget *mFilterWidget;
 };
 
 
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)