# HG changeset patch # User Andre Heinecke # Date 1434716776 -7200 # Node ID 3916cb3c91053780150e2de064872366cf15bb54 # Parent 3b3a1384eb5f9d2647d4724ea6e6638b43f0f70b Add new FilterSort model that allows to include empty values diff -r 3b3a1384eb5f -r 3916cb3c9105 src/CMakeLists.txt --- 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 ) diff -r 3b3a1384eb5f -r 3916cb3c9105 src/filterwidget.cpp --- 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 #include -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); } diff -r 3b3a1384eb5f -r 3916cb3c9105 src/filterwidget.h --- 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 mColFilterMap; + bool mWithEmptyChk; }; #endif // FILTERWIDGET_H diff -r 3b3a1384eb5f -r 3916cb3c9105 src/folderselectdialog.cpp --- 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); diff -r 3b3a1384eb5f -r 3916cb3c9105 src/includeemptysortmodel.cpp --- /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(); +} diff -r 3b3a1384eb5f -r 3916cb3c9105 src/includeemptysortmodel.h --- /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 +#include + +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 diff -r 3b3a1384eb5f -r 3916cb3c9105 src/metadataview.cpp --- 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 @@ -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); diff -r 3b3a1384eb5f -r 3916cb3c9105 src/metadataview.h --- 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; };