view src/filterwidget.cpp @ 4:e4748da7140b

Add filter functionalty to metadataview
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 25 Mar 2015 14:23:46 +0100
parents
children 315e6988952a
line wrap: on
line source
#include "filterwidget.h"

#include <QComboBox>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QSortFilterProxyModel>
#include <QAbstractItemModel>
#include <QLineEdit>
#include <QDebug>

FilterWidget::FilterWidget(QSortFilterProxyModel *model, QWidget *parent, Qt::WindowFlags f) :
    QWidget(parent, f),
    mModel(model) {
    Q_ASSERT(model);
    setupGUI();

    connect(mModel, &QSortFilterProxyModel::sourceModelChanged,
            this, &FilterWidget::headersChanged);
    void (QComboBox:: *idxChanged)(int) = &QComboBox::currentIndexChanged;
    connect(mCombo, idxChanged, this,
            &FilterWidget::filterChanged);
 //connect(mCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
    connect(mEditLine, &QLineEdit::textChanged, this,
            &FilterWidget::filterChanged);
}

void FilterWidget::headersChanged() {
    QAbstractItemModel *newSource = mModel->sourceModel();
    Q_ASSERT(newSource);
    mCombo->clear();
    for (int i=0; i < newSource->columnCount(); i++) {
        mCombo->addItem(newSource->headerData(i, Qt::Horizontal).toString());
    }
}

void FilterWidget::filterChanged() {
    QString filterText = mEditLine->text();
    if (filterText.size() < 1) {
        return;
    }
    mModel->setFilterKeyColumn(mCombo->currentIndex());
    mModel->setFilterWildcard(filterText);
}

void FilterWidget::setupGUI() {
    QHBoxLayout *root = new QHBoxLayout;

    QGroupBox *baseGroup = new QGroupBox(tr("Filter"));
    root->addWidget(baseGroup);

    QHBoxLayout *baseLayout = new QHBoxLayout;
    baseGroup->setLayout(baseLayout);

    mCombo = new QComboBox;
    baseLayout->addWidget(mCombo);

    mEditLine = new QLineEdit;
    mEditLine->setPlaceholderText(tr("Filter expression"));
    mEditLine->setClearButtonEnabled(true);
    baseLayout->addWidget(mEditLine);

    setLayout(root);
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)