view src/filterwidget.cpp @ 23:d9626e84b93d

Added tag 0.0.2 for changeset 0a08c1c2531a
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 14 Apr 2015 18:58:49 +0200
parents e12284a222ba
children 6b9b5efcd7f4
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();
    mColFilterMap.clear();
    int addedItems = 0;
    for (int i=0; i < newSource->columnCount(); i++) {
        QString entry = newSource->headerData(i, Qt::Horizontal).toString();
        if (!entry.startsWith("#")) {
            mCombo->addItem(entry);
            mColFilterMap.insert(addedItems++, i);
        } else {
            if (!newSource->setHeaderData(i, Qt::Horizontal, entry.remove(0,1), Qt::DisplayRole)) {
                qDebug() << "Setting header data failed.";
            }
        }
    }
}

void FilterWidget::filterChanged() {
    QString filterText = mEditLine->text();
    mModel->setFilterKeyColumn(mColFilterMap.value(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)