diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 10:18:55 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 10:18:55 +0100 |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/itemviews/basicsortfiltermodel |
Long live Qt 4.5!
Diffstat (limited to 'examples/itemviews/basicsortfiltermodel')
4 files changed, 350 insertions, 0 deletions
diff --git a/examples/itemviews/basicsortfiltermodel/basicsortfiltermodel.pro b/examples/itemviews/basicsortfiltermodel/basicsortfiltermodel.pro new file mode 100644 index 0000000000..7870b39679 --- /dev/null +++ b/examples/itemviews/basicsortfiltermodel/basicsortfiltermodel.pro @@ -0,0 +1,10 @@ +HEADERS = window.h +SOURCES = main.cpp \ + window.cpp +CONFIG += qt + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/basicsortfiltermodel +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/basicsortfiltermodel +INSTALLS += target sources diff --git a/examples/itemviews/basicsortfiltermodel/main.cpp b/examples/itemviews/basicsortfiltermodel/main.cpp new file mode 100644 index 0000000000..da499eda94 --- /dev/null +++ b/examples/itemviews/basicsortfiltermodel/main.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "window.h" + +void addMail(QAbstractItemModel *model, const QString &subject, + const QString &sender, const QDateTime &date) +{ + model->insertRow(0); + model->setData(model->index(0, 0), subject); + model->setData(model->index(0, 1), sender); + model->setData(model->index(0, 2), date); +} + +QAbstractItemModel *createMailModel(QObject *parent) +{ + QStandardItemModel *model = new QStandardItemModel(0, 3, parent); + + model->setHeaderData(0, Qt::Horizontal, QObject::tr("Subject")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("Sender")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("Date")); + + addMail(model, "Happy New Year!", "Grace K. <grace@software-inc.com>", + QDateTime(QDate(2006, 12, 31), QTime(17, 03))); + addMail(model, "Radically new concept", "Grace K. <grace@software-inc.com>", + QDateTime(QDate(2006, 12, 22), QTime(9, 44))); + addMail(model, "Accounts", "pascale@nospam.com", + QDateTime(QDate(2006, 12, 31), QTime(12, 50))); + addMail(model, "Expenses", "Joe Bloggs <joe@bloggs.com>", + QDateTime(QDate(2006, 12, 25), QTime(11, 39))); + addMail(model, "Re: Expenses", "Andy <andy@nospam.com>", + QDateTime(QDate(2007, 01, 02), QTime(16, 05))); + addMail(model, "Re: Accounts", "Joe Bloggs <joe@bloggs.com>", + QDateTime(QDate(2007, 01, 03), QTime(14, 18))); + addMail(model, "Re: Accounts", "Andy <andy@nospam.com>", + QDateTime(QDate(2007, 01, 03), QTime(14, 26))); + addMail(model, "Sports", "Linda Smith <linda.smith@nospam.com>", + QDateTime(QDate(2007, 01, 05), QTime(11, 33))); + addMail(model, "AW: Sports", "Rolf Newschweinstein <rolfn@nospam.com>", + QDateTime(QDate(2007, 01, 05), QTime(12, 00))); + addMail(model, "RE: Sports", "Petra Schmidt <petras@nospam.com>", + QDateTime(QDate(2007, 01, 05), QTime(12, 01))); + + return model; +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Window window; + window.setSourceModel(createMailModel(&window)); + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/basicsortfiltermodel/window.cpp b/examples/itemviews/basicsortfiltermodel/window.cpp new file mode 100644 index 0000000000..e60553e687 --- /dev/null +++ b/examples/itemviews/basicsortfiltermodel/window.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "window.h" + +Window::Window() +{ + proxyModel = new QSortFilterProxyModel; + proxyModel->setDynamicSortFilter(true); + + sourceGroupBox = new QGroupBox(tr("Original Model")); + proxyGroupBox = new QGroupBox(tr("Sorted/Filtered Model")); + + sourceView = new QTreeView; + sourceView->setRootIsDecorated(false); + sourceView->setAlternatingRowColors(true); + + proxyView = new QTreeView; + proxyView->setRootIsDecorated(false); + proxyView->setAlternatingRowColors(true); + proxyView->setModel(proxyModel); + proxyView->setSortingEnabled(true); + + sortCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive sorting")); + filterCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive filter")); + + filterPatternLineEdit = new QLineEdit; + filterPatternLabel = new QLabel(tr("&Filter pattern:")); + filterPatternLabel->setBuddy(filterPatternLineEdit); + + filterSyntaxComboBox = new QComboBox; + filterSyntaxComboBox->addItem(tr("Regular expression"), QRegExp::RegExp); + filterSyntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard); + filterSyntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString); + filterSyntaxLabel = new QLabel(tr("Filter &syntax:")); + filterSyntaxLabel->setBuddy(filterSyntaxComboBox); + + filterColumnComboBox = new QComboBox; + filterColumnComboBox->addItem(tr("Subject")); + filterColumnComboBox->addItem(tr("Sender")); + filterColumnComboBox->addItem(tr("Date")); + filterColumnLabel = new QLabel(tr("Filter &column:")); + filterColumnLabel->setBuddy(filterColumnComboBox); + + connect(filterPatternLineEdit, SIGNAL(textChanged(const QString &)), + this, SLOT(filterRegExpChanged())); + connect(filterSyntaxComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(filterRegExpChanged())); + connect(filterColumnComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(filterColumnChanged())); + connect(filterCaseSensitivityCheckBox, SIGNAL(toggled(bool)), + this, SLOT(filterRegExpChanged())); + connect(sortCaseSensitivityCheckBox, SIGNAL(toggled(bool)), + this, SLOT(sortChanged())); + + QHBoxLayout *sourceLayout = new QHBoxLayout; + sourceLayout->addWidget(sourceView); + sourceGroupBox->setLayout(sourceLayout); + + QGridLayout *proxyLayout = new QGridLayout; + proxyLayout->addWidget(proxyView, 0, 0, 1, 3); + proxyLayout->addWidget(filterPatternLabel, 1, 0); + proxyLayout->addWidget(filterPatternLineEdit, 1, 1, 1, 2); + proxyLayout->addWidget(filterSyntaxLabel, 2, 0); + proxyLayout->addWidget(filterSyntaxComboBox, 2, 1, 1, 2); + proxyLayout->addWidget(filterColumnLabel, 3, 0); + proxyLayout->addWidget(filterColumnComboBox, 3, 1, 1, 2); + proxyLayout->addWidget(filterCaseSensitivityCheckBox, 4, 0, 1, 2); + proxyLayout->addWidget(sortCaseSensitivityCheckBox, 4, 2); + proxyGroupBox->setLayout(proxyLayout); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(sourceGroupBox); + mainLayout->addWidget(proxyGroupBox); + setLayout(mainLayout); + + setWindowTitle(tr("Basic Sort/Filter Model")); + resize(500, 450); + + proxyView->sortByColumn(1, Qt::AscendingOrder); + filterColumnComboBox->setCurrentIndex(1); + + filterPatternLineEdit->setText("Andy|Grace"); + filterCaseSensitivityCheckBox->setChecked(true); + sortCaseSensitivityCheckBox->setChecked(true); +} + +void Window::setSourceModel(QAbstractItemModel *model) +{ + proxyModel->setSourceModel(model); + sourceView->setModel(model); +} + +void Window::filterRegExpChanged() +{ + QRegExp::PatternSyntax syntax = + QRegExp::PatternSyntax(filterSyntaxComboBox->itemData( + filterSyntaxComboBox->currentIndex()).toInt()); + Qt::CaseSensitivity caseSensitivity = + filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive + : Qt::CaseInsensitive; + + QRegExp regExp(filterPatternLineEdit->text(), caseSensitivity, syntax); + proxyModel->setFilterRegExp(regExp); +} + +void Window::filterColumnChanged() +{ + proxyModel->setFilterKeyColumn(filterColumnComboBox->currentIndex()); +} + +void Window::sortChanged() +{ + proxyModel->setSortCaseSensitivity( + sortCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive + : Qt::CaseInsensitive); +} diff --git a/examples/itemviews/basicsortfiltermodel/window.h b/examples/itemviews/basicsortfiltermodel/window.h new file mode 100644 index 0000000000..029a83ad01 --- /dev/null +++ b/examples/itemviews/basicsortfiltermodel/window.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QAbstractItemModel; +class QCheckBox; +class QComboBox; +class QGroupBox; +class QLabel; +class QLineEdit; +class QSortFilterProxyModel; +class QTreeView; +QT_END_NAMESPACE + +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + + void setSourceModel(QAbstractItemModel *model); + +private slots: + void filterRegExpChanged(); + void filterColumnChanged(); + void sortChanged(); + +private: + QSortFilterProxyModel *proxyModel; + + QGroupBox *sourceGroupBox; + QGroupBox *proxyGroupBox; + QTreeView *sourceView; + QTreeView *proxyView; + QCheckBox *filterCaseSensitivityCheckBox; + QCheckBox *sortCaseSensitivityCheckBox; + QLabel *filterPatternLabel; + QLabel *filterSyntaxLabel; + QLabel *filterColumnLabel; + QLineEdit *filterPatternLineEdit; + QComboBox *filterSyntaxComboBox; + QComboBox *filterColumnComboBox; +}; + +#endif |