andre@24: /* Copyright (C) 2014 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@4: andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@4: #include andre@4: andre@4: FilterWidget::FilterWidget(QSortFilterProxyModel *model, QWidget *parent, Qt::WindowFlags f) : andre@4: QWidget(parent, f), andre@4: mModel(model) { andre@4: Q_ASSERT(model); andre@4: setupGUI(); andre@4: andre@4: connect(mModel, &QSortFilterProxyModel::sourceModelChanged, andre@4: this, &FilterWidget::headersChanged); 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@4: QAbstractItemModel *newSource = mModel->sourceModel(); andre@4: Q_ASSERT(newSource); andre@4: mCombo->clear(); andre@14: mColFilterMap.clear(); andre@14: int addedItems = 0; andre@4: for (int i=0; i < newSource->columnCount(); i++) { andre@14: QString entry = newSource->headerData(i, Qt::Horizontal).toString(); 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@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@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@4: setLayout(root); andre@4: } andre@4: