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@4: for (int i=0; i < newSource->columnCount(); i++) { andre@4: mCombo->addItem(newSource->headerData(i, Qt::Horizontal).toString()); andre@4: } andre@4: } andre@4: andre@4: void FilterWidget::filterChanged() { andre@4: QString filterText = mEditLine->text(); andre@4: if (filterText.size() < 1) { andre@4: return; andre@4: } andre@4: mModel->setFilterKeyColumn(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: