annotate src/filterwidget.cpp @ 14:315e6988952a

Only add entries that do not start with # to filter
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 14 Apr 2015 17:25:15 +0200
parents e4748da7140b
children 73efe717b944
rev   line source
4
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1 #include "filterwidget.h"
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
3 #include <QComboBox>
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
4 #include <QHBoxLayout>
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
5 #include <QGroupBox>
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
6 #include <QSortFilterProxyModel>
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
7 #include <QAbstractItemModel>
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
8 #include <QLineEdit>
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
9 #include <QDebug>
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
10
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
11 FilterWidget::FilterWidget(QSortFilterProxyModel *model, QWidget *parent, Qt::WindowFlags f) :
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
12 QWidget(parent, f),
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
13 mModel(model) {
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
14 Q_ASSERT(model);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
15 setupGUI();
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
16
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
17 connect(mModel, &QSortFilterProxyModel::sourceModelChanged,
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
18 this, &FilterWidget::headersChanged);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
19 void (QComboBox:: *idxChanged)(int) = &QComboBox::currentIndexChanged;
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
20 connect(mCombo, idxChanged, this,
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
21 &FilterWidget::filterChanged);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
22 //connect(mCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
23 connect(mEditLine, &QLineEdit::textChanged, this,
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
24 &FilterWidget::filterChanged);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
25 }
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
26
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
27 void FilterWidget::headersChanged() {
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
28 QAbstractItemModel *newSource = mModel->sourceModel();
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
29 Q_ASSERT(newSource);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
30 mCombo->clear();
14
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
31 mColFilterMap.clear();
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
32 int addedItems = 0;
4
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
33 for (int i=0; i < newSource->columnCount(); i++) {
14
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
34 QString entry = newSource->headerData(i, Qt::Horizontal).toString();
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
35 if (!entry.startsWith("#")) {
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
36 mCombo->addItem(entry);
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
37 mColFilterMap.insert(addedItems++, i);
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
38 } else {
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
39 QString shortended = entry.remove(0,1);
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
40 // TODO this does not work as expected
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
41 newSource->setHeaderData(i, Qt::Horizontal, shortended);
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
42 }
4
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
43 }
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
44 }
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
45
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
46 void FilterWidget::filterChanged() {
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
47 QString filterText = mEditLine->text();
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
48 if (filterText.size() < 1) {
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
49 return;
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
50 }
14
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
51 qDebug() << "Filter on Column: " << mColFilterMap.value(mCombo->currentIndex());
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
52 qDebug() << "Instead of: " << mCombo->currentIndex();
315e6988952a Only add entries that do not start with # to filter
Andre Heinecke <andre.heinecke@intevation.de>
parents: 4
diff changeset
53 mModel->setFilterKeyColumn(mColFilterMap.value(mCombo->currentIndex()));
4
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
54 mModel->setFilterWildcard(filterText);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
55 }
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
56
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
57 void FilterWidget::setupGUI() {
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
58 QHBoxLayout *root = new QHBoxLayout;
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
59
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
60 QGroupBox *baseGroup = new QGroupBox(tr("Filter"));
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
61 root->addWidget(baseGroup);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
62
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
63 QHBoxLayout *baseLayout = new QHBoxLayout;
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
64 baseGroup->setLayout(baseLayout);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
65
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
66 mCombo = new QComboBox;
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
67 baseLayout->addWidget(mCombo);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
68
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
69 mEditLine = new QLineEdit;
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
70 mEditLine->setPlaceholderText(tr("Filter expression"));
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
71 mEditLine->setClearButtonEnabled(true);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
72 baseLayout->addWidget(mEditLine);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
73
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
74 setLayout(root);
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
75 }
e4748da7140b Add filter functionalty to metadataview
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
76
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)