andre@25: /* Copyright (C) 2015 by ETH Zürich andre@25: * Software engineering by Intevation GmbH andre@24: * andre@24: * This file is Free Software under the GNU GPL (v>=2) andre@24: * and comes with ABSOLUTELY NO WARRANTY! andre@24: * See LICENSE.txt for details. andre@24: */ andre@4: #include "filterwidget.h" andre@89: #include "constants.h" andre@4: andre@89: #include andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@89: #include andre@4: andre@88: FilterWidget::FilterWidget(QSortFilterProxyModel *model, bool addWithEmptyChk, andre@88: QWidget *parent, Qt::WindowFlags f) : andre@4: QWidget(parent, f), andre@88: mModel(model), andre@88: mWithEmptyChk(addWithEmptyChk) { andre@4: Q_ASSERT(model); andre@4: setupGUI(); andre@4: andre@4: void (QComboBox:: *idxChanged)(int) = &QComboBox::currentIndexChanged; andre@4: connect(mCombo, idxChanged, this, andre@4: &FilterWidget::filterChanged); andre@4: //connect(mCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged())); andre@4: connect(mEditLine, &QLineEdit::textChanged, this, andre@4: &FilterWidget::filterChanged); andre@4: } andre@4: andre@4: void FilterWidget::headersChanged() { andre@88: mCombo->blockSignals(true); andre@4: QAbstractItemModel *newSource = mModel->sourceModel(); andre@4: Q_ASSERT(newSource); andre@4: mCombo->clear(); andre@14: mColFilterMap.clear(); andre@14: int addedItems = 0; andre@89: QSettings settings; andre@89: settings.beginGroup(HIDE_CONFIG_GROUP); andre@4: for (int i=0; i < newSource->columnCount(); i++) { andre@14: QString entry = newSource->headerData(i, Qt::Horizontal).toString(); andre@89: if (settings.value(entry, false).toBool()) { andre@89: continue; andre@89: } andre@14: if (!entry.startsWith("#")) { andre@14: mCombo->addItem(entry); andre@14: mColFilterMap.insert(addedItems++, i); andre@14: } else { andre@17: if (!newSource->setHeaderData(i, Qt::Horizontal, entry.remove(0,1), Qt::DisplayRole)) { andre@17: qDebug() << "Setting header data failed."; andre@17: } andre@14: } andre@4: } andre@88: mCombo->blockSignals(false); andre@4: } andre@4: andre@4: void FilterWidget::filterChanged() { andre@4: QString filterText = mEditLine->text(); andre@14: mModel->setFilterKeyColumn(mColFilterMap.value(mCombo->currentIndex())); andre@4: mModel->setFilterWildcard(filterText); andre@28: emit filterHasChanged(); andre@4: } andre@4: andre@4: void FilterWidget::setupGUI() { andre@4: QHBoxLayout *root = new QHBoxLayout; andre@4: andre@4: QGroupBox *baseGroup = new QGroupBox(tr("Filter")); andre@4: root->addWidget(baseGroup); andre@4: andre@4: QHBoxLayout *baseLayout = new QHBoxLayout; andre@4: baseGroup->setLayout(baseLayout); andre@4: andre@4: mCombo = new QComboBox; andre@4: baseLayout->addWidget(mCombo); andre@4: andre@4: mEditLine = new QLineEdit; andre@4: mEditLine->setPlaceholderText(tr("Filter expression")); andre@4: mEditLine->setClearButtonEnabled(true); andre@4: baseLayout->addWidget(mEditLine); andre@4: andre@88: if (mWithEmptyChk) { andre@88: mEmptyChk = new QCheckBox(tr("Include empty fields")); andre@88: baseLayout->addWidget(mEmptyChk); andre@88: connect(mEmptyChk, &QCheckBox::stateChanged, this, &FilterWidget::includeEmptyChanged); andre@88: } andre@88: andre@4: setLayout(root); andre@4: } andre@4: