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@14: QString shortended = entry.remove(0,1); andre@14: // TODO this does not work as expected andre@14: newSource->setHeaderData(i, Qt::Horizontal, shortended); andre@14: } 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@14: qDebug() << "Filter on Column: " << mColFilterMap.value(mCombo->currentIndex()); andre@14: qDebug() << "Instead of: " << mCombo->currentIndex(); 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: