diff options
Diffstat (limited to 'examples/itemviews')
114 files changed, 9529 insertions, 0 deletions
diff --git a/examples/itemviews/README b/examples/itemviews/README new file mode 100644 index 0000000..0ac70f4 --- /dev/null +++ b/examples/itemviews/README @@ -0,0 +1,39 @@ +Item views are widgets that typically display data sets. Qt 4's model/view +framework lets you handle large data sets by separating the underlying data +from the way it is represented to the user, and provides support for +customized rendering through the use of delegates. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/itemviews/addressbook/adddialog.cpp b/examples/itemviews/addressbook/adddialog.cpp new file mode 100644 index 0000000..3eaba49 --- /dev/null +++ b/examples/itemviews/addressbook/adddialog.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** 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 "adddialog.h" + +//! [0] +AddDialog::AddDialog(QWidget *parent) + : QDialog(parent) +{ + nameLabel = new QLabel("Name"); + addressLabel = new QLabel("Address"); + okButton = new QPushButton("OK"); + cancelButton = new QPushButton("Cancel"); + + nameText = new QLineEdit; + addressText = new QTextEdit; + + QGridLayout *gLayout = new QGridLayout; + gLayout->setColumnStretch(1, 2); + gLayout->addWidget(nameLabel, 0, 0); + gLayout->addWidget(nameText, 0, 1); + + gLayout->addWidget(addressLabel, 1, 0, Qt::AlignLeft|Qt::AlignTop); + gLayout->addWidget(addressText, 1, 1, Qt::AlignLeft); + + QHBoxLayout *buttonLayout = new QHBoxLayout; + buttonLayout->addWidget(okButton); + buttonLayout->addWidget(cancelButton); + + gLayout->addLayout(buttonLayout, 2, 1, Qt::AlignRight); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addLayout(gLayout); + setLayout(mainLayout); + + connect(okButton, SIGNAL(clicked()), + this, SLOT(accept())); + + connect(cancelButton, SIGNAL(clicked()), + this, SLOT(reject())); + + setWindowTitle(tr("Add a Contact")); +} +//! [0] diff --git a/examples/itemviews/addressbook/adddialog.h b/examples/itemviews/addressbook/adddialog.h new file mode 100644 index 0000000..a68f566 --- /dev/null +++ b/examples/itemviews/addressbook/adddialog.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** 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 ADDDIALOG_H +#define ADDDIALOG_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QLabel; +class QPushButton; +class QTextEdit; +class QLineEdit; +QT_END_NAMESPACE + +//! [0] +class AddDialog : public QDialog +{ + Q_OBJECT + +public: + AddDialog(QWidget *parent=0); + QLineEdit *nameText; + QTextEdit *addressText; + +private: + QLabel *nameLabel; + QLabel *addressLabel; + QPushButton *okButton; + QPushButton *cancelButton; +}; +//! [0] + +#endif diff --git a/examples/itemviews/addressbook/addressbook.pro b/examples/itemviews/addressbook/addressbook.pro new file mode 100644 index 0000000..6f6a72d --- /dev/null +++ b/examples/itemviews/addressbook/addressbook.pro @@ -0,0 +1,17 @@ +SOURCES = adddialog.cpp \ + addresswidget.cpp \ + main.cpp \ + mainwindow.cpp \ + newaddresstab.cpp \ + tablemodel.cpp +HEADERS = adddialog.h \ + addresswidget.h \ + mainwindow.h \ + newaddresstab.h \ + tablemodel.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/addressbook +sources.files = $$SOURCES $$HEADERS $$RESOURCES addressbook.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/addressbook +INSTALLS += target sources diff --git a/examples/itemviews/addressbook/addresswidget.cpp b/examples/itemviews/addressbook/addresswidget.cpp new file mode 100644 index 0000000..e8d0527 --- /dev/null +++ b/examples/itemviews/addressbook/addresswidget.cpp @@ -0,0 +1,238 @@ +/**************************************************************************** +** +** 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 "addresswidget.h" +#include "adddialog.h" + +//! [0] +AddressWidget::AddressWidget(QWidget *parent) + : QTabWidget(parent) +{ + table = new TableModel(this); + newAddressTab = new NewAddressTab(this); + connect(newAddressTab, SIGNAL(sendDetails(QString, QString)), + this, SLOT(addEntry(QString, QString))); + + addTab(newAddressTab, "Address Book"); + + setupTabs(); +} +//! [0] + +//! [2] +void AddressWidget::addEntry() +{ + AddDialog aDialog; + + if (aDialog.exec()) { + QString name = aDialog.nameText->text(); + QString address = aDialog.addressText->toPlainText(); + + addEntry(name, address); + } +} +//! [2] + +//! [3] +void AddressWidget::addEntry(QString name, QString address) +{ + QList< QPair<QString, QString> >list = table->getList(); + QPair<QString, QString> pair(name, address); + + if (!list.contains(pair)) { + table->insertRows(0, 1, QModelIndex()); + + QModelIndex index = table->index(0, 0, QModelIndex()); + table->setData(index, name, Qt::EditRole); + index = table->index(0, 1, QModelIndex()); + table->setData(index, address, Qt::EditRole); + removeTab(indexOf(newAddressTab)); + } else { + QMessageBox::information(this, tr("Duplicate Name"), + tr("The name \"%1\" already exists.").arg(name)); + } +} +//! [3] + +//! [4a] +void AddressWidget::editEntry() +{ + QTableView *temp = static_cast<QTableView*>(currentWidget()); + QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model()); + QItemSelectionModel *selectionModel = temp->selectionModel(); + + QModelIndexList indexes = selectionModel->selectedRows(); + QModelIndex index, i; + QString name; + QString address; + int row; + + foreach (index, indexes) { + row = proxy->mapToSource(index).row(); + i = table->index(row, 0, QModelIndex()); + QVariant varName = table->data(i, Qt::DisplayRole); + name = varName.toString(); + + i = table->index(row, 1, QModelIndex()); + QVariant varAddr = table->data(i, Qt::DisplayRole); + address = varAddr.toString(); + } +//! [4a] + +//! [4b] + AddDialog aDialog; + aDialog.setWindowTitle(tr("Edit a Contact")); + + aDialog.nameText->setReadOnly(true); + aDialog.nameText->setText(name); + aDialog.addressText->setText(address); + + if (aDialog.exec()) { + QString newAddress = aDialog.addressText->toPlainText(); + if (newAddress != address) { + i = table->index(row, 1, QModelIndex()); + table->setData(i, newAddress, Qt::EditRole); + } + } +} +//! [4b] + +//! [5] +void AddressWidget::removeEntry() +{ + QTableView *temp = static_cast<QTableView*>(currentWidget()); + QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model()); + QItemSelectionModel *selectionModel = temp->selectionModel(); + + QModelIndexList indexes = selectionModel->selectedRows(); + QModelIndex index; + + foreach (index, indexes) { + int row = proxy->mapToSource(index).row(); + table->removeRows(row, 1, QModelIndex()); + } + + if (table->rowCount(QModelIndex()) == 0) { + insertTab(0, newAddressTab, "Address Book"); + } +} +//! [5] + +//! [1] +void AddressWidget::setupTabs() +{ + QStringList groups; + groups << "ABC" << "DEF" << "GHI" << "JKL" << "MNO" << "PQR" << "STU" << "VW" << "XYZ"; + + for (int i = 0; i < groups.size(); ++i) { + QString str = groups.at(i); + + proxyModel = new QSortFilterProxyModel(this); + proxyModel->setSourceModel(table); + proxyModel->setDynamicSortFilter(true); + + QTableView *tableView = new QTableView; + tableView->setModel(proxyModel); + tableView->setSortingEnabled(true); + tableView->setSelectionBehavior(QAbstractItemView::SelectRows); + tableView->horizontalHeader()->setStretchLastSection(true); + tableView->verticalHeader()->hide(); + tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); + tableView->setSelectionMode(QAbstractItemView::SingleSelection); + + QString newStr = QString("^[%1].*").arg(str); + + proxyModel->setFilterRegExp(QRegExp(newStr, Qt::CaseInsensitive)); + proxyModel->setFilterKeyColumn(0); + proxyModel->sort(0, Qt::AscendingOrder); + + connect(tableView->selectionModel(), + SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), + this, SIGNAL(selectionChanged(const QItemSelection &))); + + addTab(tableView, str); + } +} +//! [1] + +//! [7] +void AddressWidget::readFromFile(QString fileName) +{ + QFile file(fileName); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QList< QPair<QString, QString> > pairs = table->getList(); + QDataStream in(&file); + in >> pairs; + + if (pairs.isEmpty()) { + QMessageBox::information(this, tr("No contacts in file"), + tr("The file you are attempting to open contains no contacts.")); + } else { + for (int i=0; i<pairs.size(); ++i) { + QPair<QString, QString> p = pairs.at(i); + addEntry(p.first, p.second); + } + } +} +//! [7] + +//! [6] +void AddressWidget::writeToFile(QString fileName) +{ + QFile file(fileName); + + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), file.errorString()); + return; + } + + QList< QPair<QString, QString> > pairs = table->getList(); + QDataStream out(&file); + out << pairs; +} +//! [6] diff --git a/examples/itemviews/addressbook/addresswidget.h b/examples/itemviews/addressbook/addresswidget.h new file mode 100644 index 0000000..826e8b8 --- /dev/null +++ b/examples/itemviews/addressbook/addresswidget.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** 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 ADDRESSWIDGET_H +#define ADDRESSWIDGET_H + +#include <QTabWidget> +#include <QItemSelection> +#include "tablemodel.h" +#include "newaddresstab.h" + +QT_BEGIN_NAMESPACE +class QSortFilterProxyModel; +class QItemSelectionModel; +QT_END_NAMESPACE + +//! [0] +class AddressWidget : public QTabWidget +{ + Q_OBJECT + +public: + AddressWidget(QWidget *parent=0); + void readFromFile(QString fileName); + void writeToFile(QString fileName); + +public slots: + void addEntry(); + void addEntry(QString name, QString address); + void editEntry(); + void removeEntry(); + +signals: + void selectionChanged (const QItemSelection &selected); + +private: + void setupTabs(); + + TableModel *table; + NewAddressTab *newAddressTab; + QSortFilterProxyModel *proxyModel; +}; +//! [0] + +#endif diff --git a/examples/itemviews/addressbook/main.cpp b/examples/itemviews/addressbook/main.cpp new file mode 100644 index 0000000..da15c26 --- /dev/null +++ b/examples/itemviews/addressbook/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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 "mainwindow.h" + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow mw; + mw.show(); + return app.exec(); +} +//! [0] diff --git a/examples/itemviews/addressbook/mainwindow.cpp b/examples/itemviews/addressbook/mainwindow.cpp new file mode 100644 index 0000000..8923522 --- /dev/null +++ b/examples/itemviews/addressbook/mainwindow.cpp @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** 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 "mainwindow.h" + +//! [0] +MainWindow::MainWindow() +{ + addressWidget = new AddressWidget; + setCentralWidget(addressWidget); + createMenus(); + setWindowTitle(tr("Address Book")); +} +//! [0] + +//! [1a] +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + + openAct = new QAction(tr("&Open..."), this); + fileMenu->addAction(openAct); + connect(openAct, SIGNAL(triggered()), + this, SLOT(openFile())); +//! [1a] + + saveAct = new QAction(tr("&Save As..."), this); + fileMenu->addAction(saveAct); + connect(saveAct, SIGNAL(triggered()), + this, SLOT(saveFile())); + + fileMenu->addSeparator(); + + exitAct = new QAction(tr("E&xit"), this); + fileMenu->addAction(exitAct); + connect(exitAct, SIGNAL(triggered()), + this, SLOT(close())); + + toolMenu = menuBar()->addMenu(tr("&Tools")); + + addAct = new QAction(tr("&Add Entry..."), this); + toolMenu->addAction(addAct); + connect(addAct, SIGNAL(triggered()), + addressWidget, SLOT(addEntry())); + +//! [1b] + editAct = new QAction(tr("&Edit Entry..."), this); + editAct->setEnabled(false); + toolMenu->addAction(editAct); + connect(editAct, SIGNAL(triggered()), + addressWidget, SLOT(editEntry())); + + toolMenu->addSeparator(); + + removeAct = new QAction(tr("&Remove Entry"), this); + removeAct->setEnabled(false); + toolMenu->addAction(removeAct); + connect(removeAct, SIGNAL(triggered()), + addressWidget, SLOT(removeEntry())); + + connect(addressWidget, SIGNAL(selectionChanged(const QItemSelection &)), + this, SLOT(updateActions(const QItemSelection &))); +} +//! [1b] + +//! [2] +void MainWindow::openFile() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) { + addressWidget->readFromFile(fileName); + } +} +//! [2] + +//! [3] +void MainWindow::saveFile() +{ + QString fileName = QFileDialog::getSaveFileName(this); + if (!fileName.isEmpty()) { + addressWidget->writeToFile(fileName); + } +} +//! [3] + +//! [4] +void MainWindow::updateActions(const QItemSelection &selection) +{ + QModelIndexList indexes = selection.indexes(); + + if (!indexes.isEmpty()) { + removeAct->setEnabled(true); + editAct->setEnabled(true); + } else { + removeAct->setEnabled(false); + editAct->setEnabled(false); + } +} +//! [4] diff --git a/examples/itemviews/addressbook/mainwindow.h b/examples/itemviews/addressbook/mainwindow.h new file mode 100644 index 0000000..8c25e70 --- /dev/null +++ b/examples/itemviews/addressbook/mainwindow.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include <QtGui> +#include "addresswidget.h" + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void updateActions(const QItemSelection &selection); + void openFile(); + void saveFile(); + +private: + void createMenus(); + + AddressWidget *addressWidget; + QMenu *fileMenu; + QMenu *toolMenu; + QAction *openAct; + QAction *saveAct; + QAction *exitAct; + QAction *addAct; + QAction *editAct; + QAction *removeAct; +}; +//! [0] + +#endif diff --git a/examples/itemviews/addressbook/newaddresstab.cpp b/examples/itemviews/addressbook/newaddresstab.cpp new file mode 100644 index 0000000..bd0a314 --- /dev/null +++ b/examples/itemviews/addressbook/newaddresstab.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** 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 "newaddresstab.h" +#include "adddialog.h" + +//! [0] +NewAddressTab::NewAddressTab(QWidget *parent) +{ + Q_UNUSED(parent); + + descriptionLabel = new QLabel(tr("There are currently no contacts in your address book. " + "\nClick Add to add new contacts.")); + + addButton = new QPushButton(tr("Add")); + + connect(addButton, SIGNAL(clicked()), this, SLOT(addEntry())); + + mainLayout = new QVBoxLayout; + mainLayout->addWidget(descriptionLabel); + mainLayout->addWidget(addButton, 0, Qt::AlignCenter); + + setLayout(mainLayout); +} +//! [0] + +//! [1] +void NewAddressTab::addEntry() +{ + AddDialog aDialog; + + if (aDialog.exec()) { + QString name = aDialog.nameText->text(); + QString address = aDialog.addressText->toPlainText(); + + emit sendDetails(name, address); + } +} +//! [1] diff --git a/examples/itemviews/addressbook/newaddresstab.h b/examples/itemviews/addressbook/newaddresstab.h new file mode 100644 index 0000000..5ef6c37 --- /dev/null +++ b/examples/itemviews/addressbook/newaddresstab.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** 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 NEWADDRESSTAB_H +#define NEWADDRESSTAB_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QLabel; +class QPushButton; +class QVBoxLayout; +QT_END_NAMESPACE + +//! [0] +class NewAddressTab : public QWidget +{ + Q_OBJECT + +public: + NewAddressTab(QWidget *parent=0); + +public slots: + void addEntry(); + +signals: + void sendDetails(QString name, QString address); + +private: + QLabel *descriptionLabel; + QPushButton *addButton; + QVBoxLayout *mainLayout; + +}; +//! [0] + +#endif diff --git a/examples/itemviews/addressbook/tablemodel.cpp b/examples/itemviews/addressbook/tablemodel.cpp new file mode 100644 index 0000000..5673451 --- /dev/null +++ b/examples/itemviews/addressbook/tablemodel.cpp @@ -0,0 +1,185 @@ +/**************************************************************************** +** +** 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 "tablemodel.h" + +//! [0] +TableModel::TableModel(QObject *parent) + : QAbstractTableModel(parent) +{ +} + +TableModel::TableModel(QList< QPair<QString, QString> > pairs, QObject *parent) + : QAbstractTableModel(parent) +{ + listOfPairs=pairs; +} +//! [0] + +//! [1] +int TableModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return listOfPairs.size(); +} + +int TableModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return 2; +} +//! [1] + +//! [2] +QVariant TableModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() >= listOfPairs.size() || index.row() < 0) + return QVariant(); + + if (role == Qt::DisplayRole) { + QPair<QString, QString> pair = listOfPairs.at(index.row()); + + if (index.column() == 0) + return pair.first; + else if (index.column() == 1) + return pair.second; + } + return QVariant(); +} +//! [2] + +//! [3] +QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Horizontal) { + switch (section) { + case 0: + return tr("Name"); + + case 1: + return tr("Address"); + + default: + return QVariant(); + } + } + return QVariant(); +} +//! [3] + +//! [4] +bool TableModel::insertRows(int position, int rows, const QModelIndex &index) +{ + Q_UNUSED(index); + beginInsertRows(QModelIndex(), position, position+rows-1); + + for (int row=0; row < rows; row++) { + QPair<QString, QString> pair(" ", " "); + listOfPairs.insert(position, pair); + } + + endInsertRows(); + return true; +} +//! [4] + +//! [5] +bool TableModel::removeRows(int position, int rows, const QModelIndex &index) +{ + Q_UNUSED(index); + beginRemoveRows(QModelIndex(), position, position+rows-1); + + for (int row=0; row < rows; ++row) { + listOfPairs.removeAt(position); + } + + endRemoveRows(); + return true; +} +//! [5] + +//! [6] +bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.isValid() && role == Qt::EditRole) { + int row = index.row(); + + QPair<QString, QString> p = listOfPairs.value(row); + + if (index.column() == 0) + p.first = value.toString(); + else if (index.column() == 1) + p.second = value.toString(); + else + return false; + + listOfPairs.replace(row, p); + emit(dataChanged(index, index)); + + return true; + } + + return false; +} +//! [6] + +//! [7] +Qt::ItemFlags TableModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return Qt::ItemIsEnabled; + + return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; +} +//! [7] + +//! [8] +QList< QPair<QString, QString> > TableModel::getList() +{ + return listOfPairs; +} +//! [8] diff --git a/examples/itemviews/addressbook/tablemodel.h b/examples/itemviews/addressbook/tablemodel.h new file mode 100644 index 0000000..9433bec --- /dev/null +++ b/examples/itemviews/addressbook/tablemodel.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** 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 TABLEMODEL_H +#define TABLEMODEL_H + +#include <QAbstractTableModel> +#include <QPair> +#include <QList> + +//! [0] +class TableModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + TableModel(QObject *parent=0); + TableModel(QList< QPair<QString, QString> > listofPairs, QObject *parent=0); + + int rowCount(const QModelIndex &parent) const; + int columnCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole); + bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex()); + bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex()); + QList< QPair<QString, QString> > getList(); + +private: + QList< QPair<QString, QString> > listOfPairs; +}; +//! [0] + +#endif diff --git a/examples/itemviews/basicsortfiltermodel/basicsortfiltermodel.pro b/examples/itemviews/basicsortfiltermodel/basicsortfiltermodel.pro new file mode 100644 index 0000000..7870b39 --- /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 0000000..da499ed --- /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 0000000..e60553e --- /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 0000000..029a83a --- /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 diff --git a/examples/itemviews/chart/chart.pro b/examples/itemviews/chart/chart.pro new file mode 100644 index 0000000..d202451 --- /dev/null +++ b/examples/itemviews/chart/chart.pro @@ -0,0 +1,13 @@ +HEADERS = mainwindow.h \ + pieview.h +RESOURCES = chart.qrc +SOURCES = main.cpp \ + mainwindow.cpp \ + pieview.cpp +unix:!mac:LIBS+= -lm + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/chart +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro *.cht +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/chart +INSTALLS += target sources diff --git a/examples/itemviews/chart/chart.qrc b/examples/itemviews/chart/chart.qrc new file mode 100644 index 0000000..7401d4d --- /dev/null +++ b/examples/itemviews/chart/chart.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/Charts" > + <file>qtdata.cht</file> +</qresource> +</RCC> diff --git a/examples/itemviews/chart/main.cpp b/examples/itemviews/chart/main.cpp new file mode 100644 index 0000000..dc3733c --- /dev/null +++ b/examples/itemviews/chart/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(chart); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/chart/mainwindow.cpp b/examples/itemviews/chart/mainwindow.cpp new file mode 100644 index 0000000..208a465 --- /dev/null +++ b/examples/itemviews/chart/mainwindow.cpp @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** 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 "pieview.h" +#include "mainwindow.h" + +MainWindow::MainWindow() +{ + QMenu *fileMenu = new QMenu(tr("&File"), this); + QAction *openAction = fileMenu->addAction(tr("&Open...")); + openAction->setShortcut(QKeySequence(tr("Ctrl+O"))); + QAction *saveAction = fileMenu->addAction(tr("&Save As...")); + saveAction->setShortcut(QKeySequence(tr("Ctrl+S"))); + QAction *quitAction = fileMenu->addAction(tr("E&xit")); + quitAction->setShortcut(QKeySequence(tr("Ctrl+Q"))); + + setupModel(); + setupViews(); + + connect(openAction, SIGNAL(triggered()), this, SLOT(openFile())); + connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile())); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + + menuBar()->addMenu(fileMenu); + statusBar(); + + openFile(":/Charts/qtdata.cht"); + + setWindowTitle(tr("Chart")); + resize(870, 550); +} + +void MainWindow::setupModel() +{ + model = new QStandardItemModel(8, 2, this); + model->setHeaderData(0, Qt::Horizontal, tr("Label")); + model->setHeaderData(1, Qt::Horizontal, tr("Quantity")); +} + +void MainWindow::setupViews() +{ + QSplitter *splitter = new QSplitter; + QTableView *table = new QTableView; + pieChart = new PieView; + splitter->addWidget(table); + splitter->addWidget(pieChart); + splitter->setStretchFactor(0, 0); + splitter->setStretchFactor(1, 1); + + table->setModel(model); + pieChart->setModel(model); + + QItemSelectionModel *selectionModel = new QItemSelectionModel(model); + table->setSelectionModel(selectionModel); + pieChart->setSelectionModel(selectionModel); + + QHeaderView *headerView = table->horizontalHeader(); + headerView->setStretchLastSection(true); + + setCentralWidget(splitter); +} + +void MainWindow::openFile(const QString &path) +{ + QString fileName; + if (path.isNull()) + fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"), + "", "*.cht"); + else + fileName = path; + + if (!fileName.isEmpty()) { + QFile file(fileName); + + if (file.open(QFile::ReadOnly | QFile::Text)) { + QTextStream stream(&file); + QString line; + + model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex()); + + int row = 0; + do { + line = stream.readLine(); + if (!line.isEmpty()) { + + model->insertRows(row, 1, QModelIndex()); + + QStringList pieces = line.split(",", QString::SkipEmptyParts); + model->setData(model->index(row, 0, QModelIndex()), + pieces.value(0)); + model->setData(model->index(row, 1, QModelIndex()), + pieces.value(1)); + model->setData(model->index(row, 0, QModelIndex()), + QColor(pieces.value(2)), Qt::DecorationRole); + row++; + } + } while (!line.isEmpty()); + + file.close(); + statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000); + } + } +} + +void MainWindow::saveFile() +{ + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save file as"), "", "*.cht"); + + if (!fileName.isEmpty()) { + QFile file(fileName); + QTextStream stream(&file); + + if (file.open(QFile::WriteOnly | QFile::Text)) { + for (int row = 0; row < model->rowCount(QModelIndex()); ++row) { + + QStringList pieces; + + pieces.append(model->data(model->index(row, 0, QModelIndex()), + Qt::DisplayRole).toString()); + pieces.append(model->data(model->index(row, 1, QModelIndex()), + Qt::DisplayRole).toString()); + pieces.append(model->data(model->index(row, 0, QModelIndex()), + Qt::DecorationRole).toString()); + + stream << pieces.join(",") << "\n"; + } + } + + file.close(); + statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000); + } +} diff --git a/examples/itemviews/chart/mainwindow.h b/examples/itemviews/chart/mainwindow.h new file mode 100644 index 0000000..f3a9a10 --- /dev/null +++ b/examples/itemviews/chart/mainwindow.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAbstractItemModel; +class QAbstractItemView; +class QItemSelectionModel; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void openFile(const QString &path = QString()); + void saveFile(); + +private: + void setupModel(); + void setupViews(); + + QAbstractItemModel *model; + QAbstractItemView *pieChart; + QItemSelectionModel *selectionModel; +}; + +#endif diff --git a/examples/itemviews/chart/mydata.cht b/examples/itemviews/chart/mydata.cht new file mode 100644 index 0000000..029fd81 --- /dev/null +++ b/examples/itemviews/chart/mydata.cht @@ -0,0 +1,8 @@ +London,4,red +Stockholm,5,pink +Paris,2,lightgreen +Rome,11,green +Lisbon,9,blue +Madrid,8,lightblue +Berlin,6,magenta +Vienna,7,purple diff --git a/examples/itemviews/chart/pieview.cpp b/examples/itemviews/chart/pieview.cpp new file mode 100644 index 0000000..6b62f25 --- /dev/null +++ b/examples/itemviews/chart/pieview.cpp @@ -0,0 +1,562 @@ +/**************************************************************************** +** +** 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 <math.h> +#include <QtGui> + +#ifndef M_PI +#define M_PI 3.1415927 +#endif + +#include "pieview.h" + +PieView::PieView(QWidget *parent) + : QAbstractItemView(parent) +{ + horizontalScrollBar()->setRange(0, 0); + verticalScrollBar()->setRange(0, 0); + + margin = 8; + totalSize = 300; + pieSize = totalSize - 2*margin; + validItems = 0; + totalValue = 0.0; + rubberBand = 0; +} + +void PieView::dataChanged(const QModelIndex &topLeft, + const QModelIndex &bottomRight) +{ + QAbstractItemView::dataChanged(topLeft, bottomRight); + + validItems = 0; + totalValue = 0.0; + + for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { + + QModelIndex index = model()->index(row, 1, rootIndex()); + double value = model()->data(index).toDouble(); + + if (value > 0.0) { + totalValue += value; + validItems++; + } + } + viewport()->update(); +} + +bool PieView::edit(const QModelIndex &index, EditTrigger trigger, QEvent *event) +{ + if (index.column() == 0) + return QAbstractItemView::edit(index, trigger, event); + else + return false; +} + +/* + Returns the item that covers the coordinate given in the view. +*/ + +QModelIndex PieView::indexAt(const QPoint &point) const +{ + if (validItems == 0) + return QModelIndex(); + + // Transform the view coordinates into contents widget coordinates. + int wx = point.x() + horizontalScrollBar()->value(); + int wy = point.y() + verticalScrollBar()->value(); + + if (wx < totalSize) { + double cx = wx - totalSize/2; + double cy = totalSize/2 - wy; // positive cy for items above the center + + // Determine the distance from the center point of the pie chart. + double d = pow(pow(cx, 2) + pow(cy, 2), 0.5); + + if (d == 0 || d > pieSize/2) + return QModelIndex(); + + // Determine the angle of the point. + double angle = (180 / M_PI) * acos(cx/d); + if (cy < 0) + angle = 360 - angle; + + // Find the relevant slice of the pie. + double startAngle = 0.0; + + for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { + + QModelIndex index = model()->index(row, 1, rootIndex()); + double value = model()->data(index).toDouble(); + + if (value > 0.0) { + double sliceAngle = 360*value/totalValue; + + if (angle >= startAngle && angle < (startAngle + sliceAngle)) + return model()->index(row, 1, rootIndex()); + + startAngle += sliceAngle; + } + } + } else { + double itemHeight = QFontMetrics(viewOptions().font).height(); + int listItem = int((wy - margin) / itemHeight); + int validRow = 0; + + for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { + + QModelIndex index = model()->index(row, 1, rootIndex()); + if (model()->data(index).toDouble() > 0.0) { + + if (listItem == validRow) + return model()->index(row, 0, rootIndex()); + + // Update the list index that corresponds to the next valid row. + validRow++; + } + } + } + + return QModelIndex(); +} + +bool PieView::isIndexHidden(const QModelIndex & /*index*/) const +{ + return false; +} + +/* + Returns the rectangle of the item at position \a index in the + model. The rectangle is in contents coordinates. +*/ + +QRect PieView::itemRect(const QModelIndex &index) const +{ + if (!index.isValid()) + return QRect(); + + // Check whether the index's row is in the list of rows represented + // by slices. + QModelIndex valueIndex; + + if (index.column() != 1) + valueIndex = model()->index(index.row(), 1, rootIndex()); + else + valueIndex = index; + + if (model()->data(valueIndex).toDouble() > 0.0) { + + int listItem = 0; + for (int row = index.row()-1; row >= 0; --row) { + if (model()->data(model()->index(row, 1, rootIndex())).toDouble() > 0.0) + listItem++; + } + + double itemHeight; + + switch (index.column()) { + case 0: + itemHeight = QFontMetrics(viewOptions().font).height(); + + return QRect(totalSize, + int(margin + listItem*itemHeight), + totalSize - margin, int(itemHeight)); + case 1: + return viewport()->rect(); + } + + } + return QRect(); +} + +QRegion PieView::itemRegion(const QModelIndex &index) const +{ + if (!index.isValid()) + return QRegion(); + + if (index.column() != 1) + return itemRect(index); + + if (model()->data(index).toDouble() <= 0.0) + return QRegion(); + + double startAngle = 0.0; + for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { + + QModelIndex sliceIndex = model()->index(row, 1, rootIndex()); + double value = model()->data(sliceIndex).toDouble(); + + if (value > 0.0) { + double angle = 360*value/totalValue; + + if (sliceIndex == index) { + QPainterPath slicePath; + slicePath.moveTo(totalSize/2, totalSize/2); + slicePath.arcTo(margin, margin, margin+pieSize, margin+pieSize, + startAngle, angle); + slicePath.closeSubpath(); + + return QRegion(slicePath.toFillPolygon().toPolygon()); + } + + startAngle += angle; + } + } + + return QRegion(); +} + +int PieView::horizontalOffset() const +{ + return horizontalScrollBar()->value(); +} + +void PieView::mousePressEvent(QMouseEvent *event) +{ + QAbstractItemView::mousePressEvent(event); + origin = event->pos(); + if (!rubberBand) + rubberBand = new QRubberBand(QRubberBand::Rectangle, this); + rubberBand->setGeometry(QRect(origin, QSize())); + rubberBand->show(); +} + +void PieView::mouseMoveEvent(QMouseEvent *event) +{ + if (rubberBand) + rubberBand->setGeometry(QRect(origin, event->pos()).normalized()); + QAbstractItemView::mouseMoveEvent(event); +} + +void PieView::mouseReleaseEvent(QMouseEvent *event) +{ + QAbstractItemView::mouseReleaseEvent(event); + if (rubberBand) + rubberBand->hide(); + viewport()->update(); +} + +QModelIndex PieView::moveCursor(QAbstractItemView::CursorAction cursorAction, + Qt::KeyboardModifiers /*modifiers*/) +{ + QModelIndex current = currentIndex(); + + switch (cursorAction) { + case MoveLeft: + case MoveUp: + if (current.row() > 0) + current = model()->index(current.row() - 1, current.column(), + rootIndex()); + else + current = model()->index(0, current.column(), rootIndex()); + break; + case MoveRight: + case MoveDown: + if (current.row() < rows(current) - 1) + current = model()->index(current.row() + 1, current.column(), + rootIndex()); + else + current = model()->index(rows(current) - 1, current.column(), + rootIndex()); + break; + default: + break; + } + + viewport()->update(); + return current; +} + +void PieView::paintEvent(QPaintEvent *event) +{ + QItemSelectionModel *selections = selectionModel(); + QStyleOptionViewItem option = viewOptions(); + QStyle::State state = option.state; + + QBrush background = option.palette.base(); + QPen foreground(option.palette.color(QPalette::WindowText)); + QPen textPen(option.palette.color(QPalette::Text)); + QPen highlightedPen(option.palette.color(QPalette::HighlightedText)); + + QPainter painter(viewport()); + painter.setRenderHint(QPainter::Antialiasing); + + painter.fillRect(event->rect(), background); + painter.setPen(foreground); + + // Viewport rectangles + QRect pieRect = QRect(margin, margin, pieSize, pieSize); + QPoint keyPoint = QPoint(totalSize - horizontalScrollBar()->value(), + margin - verticalScrollBar()->value()); + + if (validItems > 0) { + + painter.save(); + painter.translate(pieRect.x() - horizontalScrollBar()->value(), + pieRect.y() - verticalScrollBar()->value()); + painter.drawEllipse(0, 0, pieSize, pieSize); + double startAngle = 0.0; + int row; + + for (row = 0; row < model()->rowCount(rootIndex()); ++row) { + + QModelIndex index = model()->index(row, 1, rootIndex()); + double value = model()->data(index).toDouble(); + + if (value > 0.0) { + double angle = 360*value/totalValue; + + QModelIndex colorIndex = model()->index(row, 0, rootIndex()); + QColor color = QColor(model()->data(colorIndex, + Qt::DecorationRole).toString()); + + if (currentIndex() == index) + painter.setBrush(QBrush(color, Qt::Dense4Pattern)); + else if (selections->isSelected(index)) + painter.setBrush(QBrush(color, Qt::Dense3Pattern)); + else + painter.setBrush(QBrush(color)); + + painter.drawPie(0, 0, pieSize, pieSize, int(startAngle*16), + int(angle*16)); + + startAngle += angle; + } + } + painter.restore(); + + int keyNumber = 0; + + for (row = 0; row < model()->rowCount(rootIndex()); ++row) { + + QModelIndex index = model()->index(row, 1, rootIndex()); + double value = model()->data(index).toDouble(); + + if (value > 0.0) { + QModelIndex labelIndex = model()->index(row, 0, rootIndex()); + + QStyleOptionViewItem option = viewOptions(); + option.rect = visualRect(labelIndex); + if (selections->isSelected(labelIndex)) + option.state |= QStyle::State_Selected; + if (currentIndex() == labelIndex) + option.state |= QStyle::State_HasFocus; + itemDelegate()->paint(&painter, option, labelIndex); + + keyNumber++; + } + } + } +} + +void PieView::resizeEvent(QResizeEvent * /* event */) +{ + updateGeometries(); +} + +int PieView::rows(const QModelIndex &index) const +{ + return model()->rowCount(model()->parent(index)); +} + +void PieView::rowsInserted(const QModelIndex &parent, int start, int end) +{ + for (int row = start; row <= end; ++row) { + + QModelIndex index = model()->index(row, 1, rootIndex()); + double value = model()->data(index).toDouble(); + + if (value > 0.0) { + totalValue += value; + validItems++; + } + } + + QAbstractItemView::rowsInserted(parent, start, end); +} + +void PieView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) +{ + for (int row = start; row <= end; ++row) { + + QModelIndex index = model()->index(row, 1, rootIndex()); + double value = model()->data(index).toDouble(); + if (value > 0.0) { + totalValue -= value; + validItems--; + } + } + + QAbstractItemView::rowsAboutToBeRemoved(parent, start, end); +} + +void PieView::scrollContentsBy(int dx, int dy) +{ + viewport()->scroll(dx, dy); +} + +void PieView::scrollTo(const QModelIndex &index, ScrollHint) +{ + QRect area = viewport()->rect(); + QRect rect = visualRect(index); + + if (rect.left() < area.left()) + horizontalScrollBar()->setValue( + horizontalScrollBar()->value() + rect.left() - area.left()); + else if (rect.right() > area.right()) + horizontalScrollBar()->setValue( + horizontalScrollBar()->value() + qMin( + rect.right() - area.right(), rect.left() - area.left())); + + if (rect.top() < area.top()) + verticalScrollBar()->setValue( + verticalScrollBar()->value() + rect.top() - area.top()); + else if (rect.bottom() > area.bottom()) + verticalScrollBar()->setValue( + verticalScrollBar()->value() + qMin( + rect.bottom() - area.bottom(), rect.top() - area.top())); + + update(); +} + +/* + Find the indices corresponding to the extent of the selection. +*/ + +void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) +{ + // Use content widget coordinates because we will use the itemRegion() + // function to check for intersections. + + QRect contentsRect = rect.translated( + horizontalScrollBar()->value(), + verticalScrollBar()->value()).normalized(); + + int rows = model()->rowCount(rootIndex()); + int columns = model()->columnCount(rootIndex()); + QModelIndexList indexes; + + for (int row = 0; row < rows; ++row) { + for (int column = 0; column < columns; ++column) { + QModelIndex index = model()->index(row, column, rootIndex()); + QRegion region = itemRegion(index); + if (!region.intersect(contentsRect).isEmpty()) + indexes.append(index); + } + } + + if (indexes.size() > 0) { + int firstRow = indexes[0].row(); + int lastRow = indexes[0].row(); + int firstColumn = indexes[0].column(); + int lastColumn = indexes[0].column(); + + for (int i = 1; i < indexes.size(); ++i) { + firstRow = qMin(firstRow, indexes[i].row()); + lastRow = qMax(lastRow, indexes[i].row()); + firstColumn = qMin(firstColumn, indexes[i].column()); + lastColumn = qMax(lastColumn, indexes[i].column()); + } + + QItemSelection selection( + model()->index(firstRow, firstColumn, rootIndex()), + model()->index(lastRow, lastColumn, rootIndex())); + selectionModel()->select(selection, command); + } else { + QModelIndex noIndex; + QItemSelection selection(noIndex, noIndex); + selectionModel()->select(selection, command); + } + + update(); +} + +void PieView::updateGeometries() +{ + horizontalScrollBar()->setPageStep(viewport()->width()); + horizontalScrollBar()->setRange(0, qMax(0, 2*totalSize - viewport()->width())); + verticalScrollBar()->setPageStep(viewport()->height()); + verticalScrollBar()->setRange(0, qMax(0, totalSize - viewport()->height())); +} + +int PieView::verticalOffset() const +{ + return verticalScrollBar()->value(); +} + +/* + Returns the position of the item in viewport coordinates. +*/ + +QRect PieView::visualRect(const QModelIndex &index) const +{ + QRect rect = itemRect(index); + if (rect.isValid()) + return QRect(rect.left() - horizontalScrollBar()->value(), + rect.top() - verticalScrollBar()->value(), + rect.width(), rect.height()); + else + return rect; +} + +/* + Returns a region corresponding to the selection in viewport coordinates. +*/ + +QRegion PieView::visualRegionForSelection(const QItemSelection &selection) const +{ + int ranges = selection.count(); + + if (ranges == 0) + return QRect(); + + QRegion region; + for (int i = 0; i < ranges; ++i) { + QItemSelectionRange range = selection.at(i); + for (int row = range.top(); row <= range.bottom(); ++row) { + for (int col = range.left(); col <= range.right(); ++col) { + QModelIndex index = model()->index(row, col, rootIndex()); + region += visualRect(index); + } + } + } + return region; +} diff --git a/examples/itemviews/chart/pieview.h b/examples/itemviews/chart/pieview.h new file mode 100644 index 0000000..5bebefc --- /dev/null +++ b/examples/itemviews/chart/pieview.h @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** 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 PIEVIEW_H +#define PIEVIEW_H + +#include <QAbstractItemView> +#include <QFont> +#include <QItemSelection> +#include <QItemSelectionModel> +#include <QModelIndex> +#include <QRect> +#include <QSize> +#include <QPoint> +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QRubberBand; +QT_END_NAMESPACE + +//! [0] +class PieView : public QAbstractItemView +{ + Q_OBJECT + +public: + PieView(QWidget *parent = 0); + + QRect visualRect(const QModelIndex &index) const; + void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible); + QModelIndex indexAt(const QPoint &point) const; + +protected slots: + void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); + void rowsInserted(const QModelIndex &parent, int start, int end); + void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end); + +protected: + bool edit(const QModelIndex &index, EditTrigger trigger, QEvent *event); + QModelIndex moveCursor(QAbstractItemView::CursorAction cursorAction, + Qt::KeyboardModifiers modifiers); + + int horizontalOffset() const; + int verticalOffset() const; + + bool isIndexHidden(const QModelIndex &index) const; + + void setSelection(const QRect&, QItemSelectionModel::SelectionFlags command); + + void mousePressEvent(QMouseEvent *event); + + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + + void paintEvent(QPaintEvent *event); + void resizeEvent(QResizeEvent *event); + void scrollContentsBy(int dx, int dy); + + QRegion visualRegionForSelection(const QItemSelection &selection) const; + +private: + QRect itemRect(const QModelIndex &item) const; + QRegion itemRegion(const QModelIndex &index) const; + int rows(const QModelIndex &index = QModelIndex()) const; + void updateGeometries(); + + int margin; + int totalSize; + int pieSize; + int validItems; + double totalValue; + QPoint origin; + QRubberBand *rubberBand; +}; +//! [0] + +#endif diff --git a/examples/itemviews/chart/qtdata.cht b/examples/itemviews/chart/qtdata.cht new file mode 100644 index 0000000..6386246 --- /dev/null +++ b/examples/itemviews/chart/qtdata.cht @@ -0,0 +1,14 @@ +Scientific Research,21,#99e600 +Engineering & Design,18,#99cc00 +Automotive,14,#99b300 +Aerospace,13,#9f991a +Automation & Machine Tools,13,#a48033 +Medical & Bioinformatics,13,#a9664d +Imaging & Special Effects,12,#ae4d66 +Defense,11,#b33380 +Test & Measurement Systems,9,#a64086 +Oil & Gas,9,#994d8d +Entertainment & Broadcasting,7,#8d5a93 +Financial,6,#806699 +Consumer Electronics,4,#8073a6 +Other,38,#8080b3 diff --git a/examples/itemviews/coloreditorfactory/coloreditorfactory.pro b/examples/itemviews/coloreditorfactory/coloreditorfactory.pro new file mode 100644 index 0000000..ebb3b8e --- /dev/null +++ b/examples/itemviews/coloreditorfactory/coloreditorfactory.pro @@ -0,0 +1,11 @@ +HEADERS = colorlisteditor.h \ + window.h +SOURCES = colorlisteditor.cpp \ + window.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/coloreditorfactory +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/coloreditorfactory +INSTALLS += target sources diff --git a/examples/itemviews/coloreditorfactory/colorlisteditor.cpp b/examples/itemviews/coloreditorfactory/colorlisteditor.cpp new file mode 100644 index 0000000..88b953c --- /dev/null +++ b/examples/itemviews/coloreditorfactory/colorlisteditor.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** 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 "colorlisteditor.h" + +ColorListEditor::ColorListEditor(QWidget *widget) : QComboBox(widget) +{ + populateList(); +} + +//! [0] +QColor ColorListEditor::color() const +{ + return qVariantValue<QColor>(itemData(currentIndex(), Qt::DecorationRole)); +} +//! [0] + +//! [1] +void ColorListEditor::setColor(QColor color) +{ + setCurrentIndex(findData(color, int(Qt::DecorationRole))); +} +//! [1] + +//! [2] +void ColorListEditor::populateList() +{ + QStringList colorNames = QColor::colorNames(); + + for (int i = 0; i < colorNames.size(); ++i) { + QColor color(colorNames[i]); + + insertItem(i, colorNames[i]); + setItemData(i, color, Qt::DecorationRole); + } +} +//! [2] diff --git a/examples/itemviews/coloreditorfactory/colorlisteditor.h b/examples/itemviews/coloreditorfactory/colorlisteditor.h new file mode 100644 index 0000000..810e268 --- /dev/null +++ b/examples/itemviews/coloreditorfactory/colorlisteditor.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** 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 COLORLISTEDITOR_H +#define COLORLISTEDITOR_H + +#include <QComboBox> + +QT_BEGIN_NAMESPACE +class QColor; +class QWidget; +QT_END_NAMESPACE + +//! [0] +class ColorListEditor : public QComboBox +{ + Q_OBJECT + Q_PROPERTY(QColor color READ color WRITE setColor USER true) + +public: + ColorListEditor(QWidget *widget = 0); + +public: + QColor color() const; + void setColor(QColor c); + +private: + void populateList(); +}; +//! [0] + +#endif diff --git a/examples/itemviews/coloreditorfactory/main.cpp b/examples/itemviews/coloreditorfactory/main.cpp new file mode 100644 index 0000000..a9740af --- /dev/null +++ b/examples/itemviews/coloreditorfactory/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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" + +int main(int argv, char **args) +{ + QApplication app(argv, args); + + Window window; + window.show(); + + return app.exec(); +} diff --git a/examples/itemviews/coloreditorfactory/window.cpp b/examples/itemviews/coloreditorfactory/window.cpp new file mode 100644 index 0000000..2ac64a3 --- /dev/null +++ b/examples/itemviews/coloreditorfactory/window.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** 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" +#include "colorlisteditor.h" + +//! [0] +Window::Window() +{ + QItemEditorFactory *factory = new QItemEditorFactory; + + QItemEditorCreatorBase *colorListCreator = + new QStandardItemEditorCreator<ColorListEditor>(); + + factory->registerEditor(QVariant::Color, colorListCreator); + + QItemEditorFactory::setDefaultFactory(factory); + + createGUI(); +} +//! [0] + +void Window::createGUI() +{ + QList<QPair<QString, QColor> > list; + list << QPair<QString, QColor>(tr("Alice"), QColor("aliceblue")) << + QPair<QString, QColor>(tr("Neptun"), QColor("aquamarine")) << + QPair<QString, QColor>(tr("Ferdinand"), QColor("springgreen")); + + QTableWidget *table = new QTableWidget(3, 2); + table->setHorizontalHeaderLabels(QStringList() << tr("Name") + << tr("Hair Color")); + table->verticalHeader()->setVisible(false); + table->resize(150, 50); + + for (int i = 0; i < 3; ++i) { + QPair<QString, QColor> pair = list.at(i); + + QTableWidgetItem *nameItem = new QTableWidgetItem(pair.first); + QTableWidgetItem *colorItem = new QTableWidgetItem; + colorItem->setData(Qt::DisplayRole, pair.second); + + table->setItem(i, 0, nameItem); + table->setItem(i, 1, colorItem); + } + table->resizeColumnToContents(0); + table->horizontalHeader()->setStretchLastSection(true); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(table, 0, 0); + + setLayout(layout); + + setWindowTitle(tr("Color Editor Factory")); +} diff --git a/examples/itemviews/coloreditorfactory/window.h b/examples/itemviews/coloreditorfactory/window.h new file mode 100644 index 0000000..fc7b13a --- /dev/null +++ b/examples/itemviews/coloreditorfactory/window.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** 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> + +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +private: + void createGUI(); +}; + +#endif diff --git a/examples/itemviews/combowidgetmapper/combowidgetmapper.pro b/examples/itemviews/combowidgetmapper/combowidgetmapper.pro new file mode 100644 index 0000000..7f5c8e8 --- /dev/null +++ b/examples/itemviews/combowidgetmapper/combowidgetmapper.pro @@ -0,0 +1,9 @@ +HEADERS = window.h +SOURCES = main.cpp \ + window.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/combowidgetmapper +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/combowidgetmapper +INSTALLS += target sources diff --git a/examples/itemviews/combowidgetmapper/main.cpp b/examples/itemviews/combowidgetmapper/main.cpp new file mode 100644 index 0000000..055ac21 --- /dev/null +++ b/examples/itemviews/combowidgetmapper/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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 <QApplication> + +#include "window.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/combowidgetmapper/window.cpp b/examples/itemviews/combowidgetmapper/window.cpp new file mode 100644 index 0000000..69a4c3c --- /dev/null +++ b/examples/itemviews/combowidgetmapper/window.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** 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" + +//! [Set up widgets] +Window::Window(QWidget *parent) + : QWidget(parent) +{ + setupModel(); + + nameLabel = new QLabel(tr("Na&me:")); + nameEdit = new QLineEdit(); + addressLabel = new QLabel(tr("&Address:")); + addressEdit = new QTextEdit(); + typeLabel = new QLabel(tr("&Type:")); + typeComboBox = new QComboBox(); + nextButton = new QPushButton(tr("&Next")); + previousButton = new QPushButton(tr("&Previous")); + + nameLabel->setBuddy(nameEdit); + addressLabel->setBuddy(addressEdit); + typeLabel->setBuddy(typeComboBox); + + typeComboBox->setModel(typeModel); +//! [Set up widgets] + +//! [Set up the mapper] + mapper = new QDataWidgetMapper(this); + mapper->setModel(model); + mapper->addMapping(nameEdit, 0); + mapper->addMapping(addressEdit, 1); + mapper->addMapping(typeComboBox, 2, "currentIndex"); +//! [Set up the mapper] + +//! [Set up connections and layouts] + connect(previousButton, SIGNAL(clicked()), + mapper, SLOT(toPrevious())); + connect(nextButton, SIGNAL(clicked()), + mapper, SLOT(toNext())); + connect(mapper, SIGNAL(currentIndexChanged(int)), + this, SLOT(updateButtons(int))); + + QGridLayout *layout = new QGridLayout(); + layout->addWidget(nameLabel, 0, 0, 1, 1); + layout->addWidget(nameEdit, 0, 1, 1, 1); + layout->addWidget(previousButton, 0, 2, 1, 1); + layout->addWidget(addressLabel, 1, 0, 1, 1); + layout->addWidget(addressEdit, 1, 1, 2, 1); + layout->addWidget(nextButton, 1, 2, 1, 1); + layout->addWidget(typeLabel, 3, 0, 1, 1); + layout->addWidget(typeComboBox, 3, 1, 1, 1); + setLayout(layout); + + setWindowTitle(tr("Delegate Widget Mapper")); + mapper->toFirst(); +} +//! [Set up connections and layouts] + +//! [Set up the model] +void Window::setupModel() +{ + QStringList items; + items << tr("Home") << tr("Work") << tr("Other"); + typeModel = new QStringListModel(items, this); + + model = new QStandardItemModel(5, 3, this); + QStringList names; + names << "Alice" << "Bob" << "Carol" << "Donald" << "Emma"; + QStringList addresses; + addresses << "<qt>123 Main Street<br/>Market Town</qt>" + << "<qt>PO Box 32<br/>Mail Handling Service" + "<br/>Service City</qt>" + << "<qt>The Lighthouse<br/>Remote Island</qt>" + << "<qt>47338 Park Avenue<br/>Big City</qt>" + << "<qt>Research Station<br/>Base Camp<br/>Big Mountain</qt>"; + + QStringList types; + types << "0" << "1" << "2" << "0" << "2"; + + for (int row = 0; row < 5; ++row) { + QStandardItem *item = new QStandardItem(names[row]); + model->setItem(row, 0, item); + item = new QStandardItem(addresses[row]); + model->setItem(row, 1, item); + item = new QStandardItem(types[row]); + model->setItem(row, 2, item); + } +} +//! [Set up the model] + +//! [Slot for updating the buttons] +void Window::updateButtons(int row) +{ + previousButton->setEnabled(row > 0); + nextButton->setEnabled(row < model->rowCount() - 1); +} +//! [Slot for updating the buttons] diff --git a/examples/itemviews/combowidgetmapper/window.h b/examples/itemviews/combowidgetmapper/window.h new file mode 100644 index 0000000..8c45117 --- /dev/null +++ b/examples/itemviews/combowidgetmapper/window.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** 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 QComboBox; +class QDataWidgetMapper; +class QLabel; +class QLineEdit; +class QPushButton; +class QStandardItemModel; +class QStringListModel; +class QTextEdit; +QT_END_NAMESPACE + +//! [Window definition] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(QWidget *parent = 0); + +private slots: + void updateButtons(int row); + +private: + void setupModel(); + + QLabel *nameLabel; + QLabel *addressLabel; + QLabel *typeLabel; + QLineEdit *nameEdit; + QTextEdit *addressEdit; + QComboBox *typeComboBox; + QPushButton *nextButton; + QPushButton *previousButton; + + QStandardItemModel *model; + QStringListModel *typeModel; + QDataWidgetMapper *mapper; +}; +//! [Window definition] + +#endif diff --git a/examples/itemviews/customsortfiltermodel/customsortfiltermodel.pro b/examples/itemviews/customsortfiltermodel/customsortfiltermodel.pro new file mode 100644 index 0000000..5a9b9f9 --- /dev/null +++ b/examples/itemviews/customsortfiltermodel/customsortfiltermodel.pro @@ -0,0 +1,12 @@ +HEADERS = mysortfilterproxymodel.h \ + window.h +SOURCES = main.cpp \ + mysortfilterproxymodel.cpp \ + window.cpp +CONFIG += qt + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/customsortfiltermodel +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/customsortfiltermodel +INSTALLS += target sources diff --git a/examples/itemviews/customsortfiltermodel/main.cpp b/examples/itemviews/customsortfiltermodel/main.cpp new file mode 100644 index 0000000..043f28a --- /dev/null +++ b/examples/itemviews/customsortfiltermodel/main.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** 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; +} + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Window window; + window.setSourceModel(createMailModel(&window)); + window.show(); + return app.exec(); +} +//! [0] diff --git a/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp b/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp new file mode 100644 index 0000000..ead2eb7 --- /dev/null +++ b/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** 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 "mysortfilterproxymodel.h" + +//! [0] +MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) + : QSortFilterProxyModel(parent) +{ +} +//! [0] + +//! [1] +void MySortFilterProxyModel::setFilterMinimumDate(const QDate &date) +{ + minDate = date; + invalidateFilter(); +} +//! [1] + +//! [2] +void MySortFilterProxyModel::setFilterMaximumDate(const QDate &date) +{ + maxDate = date; + invalidateFilter(); +} +//! [2] + +//! [3] +bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, + const QModelIndex &sourceParent) const +{ + QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent); + QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent); + QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent); + + return (sourceModel()->data(index0).toString().contains(filterRegExp()) + || sourceModel()->data(index1).toString().contains(filterRegExp())) + && dateInRange(sourceModel()->data(index2).toDate()); +} +//! [3] + +//! [4] //! [5] +bool MySortFilterProxyModel::lessThan(const QModelIndex &left, + const QModelIndex &right) const +{ + QVariant leftData = sourceModel()->data(left); + QVariant rightData = sourceModel()->data(right); +//! [4] + +//! [6] + if (leftData.type() == QVariant::DateTime) { + return leftData.toDateTime() < rightData.toDateTime(); + } else { + QRegExp *emailPattern = new QRegExp("([\\w\\.]*@[\\w\\.]*)"); + + QString leftString = leftData.toString(); + if(left.column() == 1 && emailPattern->indexIn(leftString) != -1) + leftString = emailPattern->cap(1); + + QString rightString = rightData.toString(); + if(right.column() == 1 && emailPattern->indexIn(rightString) != -1) + rightString = emailPattern->cap(1); + + return QString::localeAwareCompare(leftString, rightString) < 0; + } +} +//! [5] //! [6] + +//! [7] +bool MySortFilterProxyModel::dateInRange(const QDate &date) const +{ + return (!minDate.isValid() || date > minDate) + && (!maxDate.isValid() || date < maxDate); +} +//! [7] diff --git a/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.h b/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.h new file mode 100644 index 0000000..0e09bf1 --- /dev/null +++ b/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** 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 MYSORTFILTERPROXYMODEL_H +#define MYSORTFILTERPROXYMODEL_H + +#include <QDate> +#include <QSortFilterProxyModel> + +//! [0] +class MySortFilterProxyModel : public QSortFilterProxyModel +{ + Q_OBJECT + +public: + MySortFilterProxyModel(QObject *parent = 0); + + QDate filterMinimumDate() const { return minDate; } + void setFilterMinimumDate(const QDate &date); + + QDate filterMaximumDate() const { return maxDate; } + void setFilterMaximumDate(const QDate &date); + +protected: + bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; + bool lessThan(const QModelIndex &left, const QModelIndex &right) const; + +private: + bool dateInRange(const QDate &date) const; + + QDate minDate; + QDate maxDate; +}; +//! [0] + +#endif diff --git a/examples/itemviews/customsortfiltermodel/window.cpp b/examples/itemviews/customsortfiltermodel/window.cpp new file mode 100644 index 0000000..54ba646 --- /dev/null +++ b/examples/itemviews/customsortfiltermodel/window.cpp @@ -0,0 +1,168 @@ +/**************************************************************************** +** +** 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 "mysortfilterproxymodel.h" +#include "window.h" + +//! [0] +Window::Window() +{ + proxyModel = new MySortFilterProxyModel(this); + proxyModel->setDynamicSortFilter(true); +//! [0] + +//! [1] + sourceView = new QTreeView; + sourceView->setRootIsDecorated(false); + sourceView->setAlternatingRowColors(true); +//! [1] + + QHBoxLayout *sourceLayout = new QHBoxLayout; +//! [2] + sourceLayout->addWidget(sourceView); + sourceGroupBox = new QGroupBox(tr("Original Model")); + sourceGroupBox->setLayout(sourceLayout); +//! [2] + +//! [3] + filterCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive filter")); + filterCaseSensitivityCheckBox->setChecked(true); + + filterPatternLineEdit = new QLineEdit; + filterPatternLineEdit->setText("Grace|Sports"); + + 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); + + fromDateEdit = new QDateEdit; + fromDateEdit->setDate(QDate(1970, 01, 01)); + fromLabel = new QLabel(tr("F&rom:")); + fromLabel->setBuddy(fromDateEdit); + + toDateEdit = new QDateEdit; + toDateEdit->setDate(QDate(2099, 12, 31)); + toLabel = new QLabel(tr("&To:")); + toLabel->setBuddy(toDateEdit); + + connect(filterPatternLineEdit, SIGNAL(textChanged(const QString &)), + this, SLOT(textFilterChanged())); + connect(filterSyntaxComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(textFilterChanged())); + connect(filterCaseSensitivityCheckBox, SIGNAL(toggled(bool)), + this, SLOT(textFilterChanged())); + connect(fromDateEdit, SIGNAL(dateChanged(const QDate &)), + this, SLOT(dateFilterChanged())); + connect(toDateEdit, SIGNAL(dateChanged(const QDate &)), +//! [3] //! [4] + this, SLOT(dateFilterChanged())); +//! [4] + +//! [5] + proxyView = new QTreeView; + proxyView->setRootIsDecorated(false); + proxyView->setAlternatingRowColors(true); + proxyView->setModel(proxyModel); + proxyView->setSortingEnabled(true); + proxyView->sortByColumn(1, Qt::AscendingOrder); + + QGridLayout *proxyLayout = new QGridLayout; + proxyLayout->addWidget(proxyView, 0, 0, 1, 3); + proxyLayout->addWidget(filterPatternLabel, 1, 0); + proxyLayout->addWidget(filterPatternLineEdit, 1, 1); + proxyLayout->addWidget(filterSyntaxComboBox, 1, 2); + proxyLayout->addWidget(filterCaseSensitivityCheckBox, 2, 0, 1, 3); + proxyLayout->addWidget(fromLabel, 3, 0); + proxyLayout->addWidget(fromDateEdit, 3, 1, 1, 2); + proxyLayout->addWidget(toLabel, 4, 0); + proxyLayout->addWidget(toDateEdit, 4, 1, 1, 2); + + proxyGroupBox = new QGroupBox(tr("Sorted/Filtered Model")); + proxyGroupBox->setLayout(proxyLayout); +//! [5] + +//! [6] + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(sourceGroupBox); + mainLayout->addWidget(proxyGroupBox); + setLayout(mainLayout); + + setWindowTitle(tr("Custom Sort/Filter Model")); + resize(500, 450); +} +//! [6] + +//! [7] +void Window::setSourceModel(QAbstractItemModel *model) +{ + proxyModel->setSourceModel(model); + sourceView->setModel(model); +} +//! [7] + +//! [8] +void Window::textFilterChanged() +{ + 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); +} +//! [8] + +//! [9] +void Window::dateFilterChanged() +{ + proxyModel->setFilterMinimumDate(fromDateEdit->date()); + proxyModel->setFilterMaximumDate(toDateEdit->date()); +} +//! [9] diff --git a/examples/itemviews/customsortfiltermodel/window.h b/examples/itemviews/customsortfiltermodel/window.h new file mode 100644 index 0000000..ba97408 --- /dev/null +++ b/examples/itemviews/customsortfiltermodel/window.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** 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 QDateEdit; +class QGroupBox; +class QLabel; +class QLineEdit; +class QTreeView; +QT_END_NAMESPACE +class MySortFilterProxyModel; + +//! [0] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + + void setSourceModel(QAbstractItemModel *model); + +private slots: + void textFilterChanged(); + void dateFilterChanged(); + +private: + MySortFilterProxyModel *proxyModel; + + QGroupBox *sourceGroupBox; + QGroupBox *proxyGroupBox; + QTreeView *sourceView; + QTreeView *proxyView; + QCheckBox *filterCaseSensitivityCheckBox; + QLabel *filterPatternLabel; + QLabel *fromLabel; + QLabel *toLabel; + QLineEdit *filterPatternLineEdit; + QComboBox *filterSyntaxComboBox; + QDateEdit *fromDateEdit; + QDateEdit *toDateEdit; +}; +//! [0] + +#endif diff --git a/examples/itemviews/dirview/dirview.pro b/examples/itemviews/dirview/dirview.pro new file mode 100644 index 0000000..5865d3c --- /dev/null +++ b/examples/itemviews/dirview/dirview.pro @@ -0,0 +1,7 @@ +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/dirview +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/dirview +INSTALLS += target sources diff --git a/examples/itemviews/dirview/main.cpp b/examples/itemviews/dirview/main.cpp new file mode 100644 index 0000000..65d2ecc --- /dev/null +++ b/examples/itemviews/dirview/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** 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> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QDirModel model; + QTreeView tree; + tree.setModel(&model); + + // Demonstrating look and feel features + tree.setAnimated(false); + tree.setIndentation(20); + tree.setSortingEnabled(true); + + tree.setWindowTitle(QObject::tr("Dir View")); + tree.resize(640, 480); + tree.show(); + + return app.exec(); +} diff --git a/examples/itemviews/editabletreemodel/default.txt b/examples/itemviews/editabletreemodel/default.txt new file mode 100644 index 0000000..2b2fb57 --- /dev/null +++ b/examples/itemviews/editabletreemodel/default.txt @@ -0,0 +1,40 @@ +Getting Started How to familiarize yourself with Qt Designer + Launching Designer Running the Qt Designer application + The User Interface How to interact with Qt Designer + +Designing a Component Creating a GUI for your application + Creating a Dialog How to create a dialog + Composing the Dialog Putting widgets into the dialog example + Creating a Layout Arranging widgets on a form + Signal and Slot Connections Making widget communicate with each other + +Using a Component in Your Application Generating code from forms + The Direct Approach Using a form without any adjustments + The Single Inheritance Approach Subclassing a form's base class + The Multiple Inheritance Approach Subclassing the form itself + Automatic Connections Connecting widgets using a naming scheme + A Dialog Without Auto-Connect How to connect widgets without a naming scheme + A Dialog With Auto-Connect Using automatic connections + +Form Editing Mode How to edit a form in Qt Designer + Managing Forms Loading and saving forms + Editing a Form Basic editing techniques + The Property Editor Changing widget properties + The Object Inspector Examining the hierarchy of objects on a form + Layouts Objects that arrange widgets on a form + Applying and Breaking Layouts Managing widgets in layouts + Horizontal and Vertical Layouts Standard row and column layouts + The Grid Layout Arranging widgets in a matrix + Previewing Forms Checking that the design works + +Using Containers How to group widgets together + General Features Common container features + Frames QFrame + Group Boxes QGroupBox + Stacked Widgets QStackedWidget + Tab Widgets QTabWidget + Toolbox Widgets QToolBox + +Connection Editing Mode Connecting widgets together with signals and slots + Connecting Objects Making connections in Qt Designer + Editing Connections Changing existing connections diff --git a/examples/itemviews/editabletreemodel/editabletreemodel.pro b/examples/itemviews/editabletreemodel/editabletreemodel.pro new file mode 100644 index 0000000..2b7b7de --- /dev/null +++ b/examples/itemviews/editabletreemodel/editabletreemodel.pro @@ -0,0 +1,16 @@ +FORMS = mainwindow.ui +HEADERS = mainwindow.h \ + treeitem.h \ + treemodel.h +RESOURCES = editabletreemodel.qrc +SOURCES = mainwindow.cpp \ + treeitem.cpp \ + treemodel.cpp \ + main.cpp +CONFIG += qt + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/editabletreemodel +sources.files = $$FORMS $$HEADERS $$RESOURCES $$SOURCES *.pro *.txt +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/editabletreemodel +INSTALLS += target sources diff --git a/examples/itemviews/editabletreemodel/editabletreemodel.qrc b/examples/itemviews/editabletreemodel/editabletreemodel.qrc new file mode 100644 index 0000000..2fb48f2 --- /dev/null +++ b/examples/itemviews/editabletreemodel/editabletreemodel.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/" > + <file>default.txt</file> + </qresource> +</RCC> diff --git a/examples/itemviews/editabletreemodel/main.cpp b/examples/itemviews/editabletreemodel/main.cpp new file mode 100644 index 0000000..e476d2e --- /dev/null +++ b/examples/itemviews/editabletreemodel/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(editabletreemodel); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/editabletreemodel/mainwindow.cpp b/examples/itemviews/editabletreemodel/mainwindow.cpp new file mode 100644 index 0000000..f81b2fe --- /dev/null +++ b/examples/itemviews/editabletreemodel/mainwindow.cpp @@ -0,0 +1,181 @@ +/**************************************************************************** +** +** 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 "mainwindow.h" +#include "treemodel.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + setupUi(this); + + QStringList headers; + headers << tr("Title") << tr("Description"); + + QFile file(":/default.txt"); + file.open(QIODevice::ReadOnly); + TreeModel *model = new TreeModel(headers, file.readAll()); + file.close(); + + view->setModel(model); + for (int column = 0; column < model->columnCount(); ++column) + view->resizeColumnToContents(column); + + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + + connect(view->selectionModel(), + SIGNAL(selectionChanged(const QItemSelection &, + const QItemSelection &)), + this, SLOT(updateActions())); + + connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions())); + connect(insertRowAction, SIGNAL(triggered()), this, SLOT(insertRow())); + connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn())); + connect(removeRowAction, SIGNAL(triggered()), this, SLOT(removeRow())); + connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn())); + connect(insertChildAction, SIGNAL(triggered()), this, SLOT(insertChild())); + + updateActions(); +} + +void MainWindow::insertChild() +{ + QModelIndex index = view->selectionModel()->currentIndex(); + QAbstractItemModel *model = view->model(); + + if (model->columnCount(index) == 0) { + if (!model->insertColumn(0, index)) + return; + } + + if (!model->insertRow(0, index)) + return; + + for (int column = 0; column < model->columnCount(index); ++column) { + QModelIndex child = model->index(0, column, index); + model->setData(child, QVariant("[No data]"), Qt::EditRole); + if (!model->headerData(column, Qt::Horizontal).isValid()) + model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), + Qt::EditRole); + } + + view->selectionModel()->setCurrentIndex(model->index(0, 0, index), + QItemSelectionModel::ClearAndSelect); + updateActions(); +} + +bool MainWindow::insertColumn(const QModelIndex &parent) +{ + QAbstractItemModel *model = view->model(); + int column = view->selectionModel()->currentIndex().column(); + + // Insert a column in the parent item. + bool changed = model->insertColumn(column + 1, parent); + if (changed) + model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), + Qt::EditRole); + + updateActions(); + + return changed; +} + +void MainWindow::insertRow() +{ + QModelIndex index = view->selectionModel()->currentIndex(); + QAbstractItemModel *model = view->model(); + + if (!model->insertRow(index.row()+1, index.parent())) + return; + + updateActions(); + + for (int column = 0; column < model->columnCount(index.parent()); ++column) { + QModelIndex child = model->index(index.row()+1, column, index.parent()); + model->setData(child, QVariant("[No data]"), Qt::EditRole); + } +} + +bool MainWindow::removeColumn(const QModelIndex &parent) +{ + QAbstractItemModel *model = view->model(); + int column = view->selectionModel()->currentIndex().column(); + + // Insert columns in each child of the parent item. + bool changed = model->removeColumn(column, parent); + + if (!parent.isValid() && changed) + updateActions(); + + return changed; +} + +void MainWindow::removeRow() +{ + QModelIndex index = view->selectionModel()->currentIndex(); + QAbstractItemModel *model = view->model(); + if (model->removeRow(index.row(), index.parent())) + updateActions(); +} + +void MainWindow::updateActions() +{ + bool hasSelection = !view->selectionModel()->selection().isEmpty(); + removeRowAction->setEnabled(hasSelection); + removeColumnAction->setEnabled(hasSelection); + + bool hasCurrent = view->selectionModel()->currentIndex().isValid(); + insertRowAction->setEnabled(hasCurrent); + insertColumnAction->setEnabled(hasCurrent); + + if (hasCurrent) { + view->closePersistentEditor(view->selectionModel()->currentIndex()); + + int row = view->selectionModel()->currentIndex().row(); + int column = view->selectionModel()->currentIndex().column(); + if (view->selectionModel()->currentIndex().parent().isValid()) + statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column)); + else + statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column)); + } +} diff --git a/examples/itemviews/editabletreemodel/mainwindow.h b/examples/itemviews/editabletreemodel/mainwindow.h new file mode 100644 index 0000000..6de08b6 --- /dev/null +++ b/examples/itemviews/editabletreemodel/mainwindow.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include <QModelIndex> + +#include "ui_mainwindow.h" + +class QAction; +class QTreeView; +class QWidget; + +class MainWindow : public QMainWindow, private Ui::MainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +public slots: + void updateActions(); + +private slots: + void insertChild(); + bool insertColumn(const QModelIndex &parent = QModelIndex()); + void insertRow(); + bool removeColumn(const QModelIndex &parent = QModelIndex()); + void removeRow(); +}; + +#endif diff --git a/examples/itemviews/editabletreemodel/mainwindow.ui b/examples/itemviews/editabletreemodel/mainwindow.ui new file mode 100644 index 0000000..2ad084a --- /dev/null +++ b/examples/itemviews/editabletreemodel/mainwindow.ui @@ -0,0 +1,128 @@ +<ui version="4.0" > + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>573</width> + <height>468</height> + </rect> + </property> + <property name="windowTitle" > + <string>Editable Tree Model</string> + </property> + <widget class="QWidget" name="centralwidget" > + <layout class="QVBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>0</number> + </property> + <item> + <widget class="QTreeView" name="view" > + <property name="alternatingRowColors" > + <bool>true</bool> + </property> + <property name="selectionBehavior" > + <enum>QAbstractItemView::SelectItems</enum> + </property> + <property name="horizontalScrollMode" > + <enum>QAbstractItemView::ScrollPerPixel</enum> + </property> + <property name="animated" > + <bool>false</bool> + </property> + <property name="allColumnsShowFocus" > + <bool>true</bool> + </property> + </widget> + </item> + </layout> + </widget> + <widget class="QMenuBar" name="menubar" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>573</width> + <height>31</height> + </rect> + </property> + <widget class="QMenu" name="fileMenu" > + <property name="title" > + <string>&File</string> + </property> + <addaction name="exitAction" /> + </widget> + <widget class="QMenu" name="actionsMenu" > + <property name="title" > + <string>&Actions</string> + </property> + <addaction name="insertRowAction" /> + <addaction name="insertColumnAction" /> + <addaction name="separator" /> + <addaction name="removeRowAction" /> + <addaction name="removeColumnAction" /> + <addaction name="separator" /> + <addaction name="insertChildAction" /> + </widget> + <addaction name="fileMenu" /> + <addaction name="actionsMenu" /> + </widget> + <widget class="QStatusBar" name="statusbar" /> + <action name="exitAction" > + <property name="text" > + <string>E&xit</string> + </property> + <property name="shortcut" > + <string>Ctrl+Q</string> + </property> + </action> + <action name="insertRowAction" > + <property name="text" > + <string>Insert Row</string> + </property> + <property name="shortcut" > + <string>Ctrl+I, R</string> + </property> + </action> + <action name="removeRowAction" > + <property name="text" > + <string>Remove Row</string> + </property> + <property name="shortcut" > + <string>Ctrl+R, R</string> + </property> + </action> + <action name="insertColumnAction" > + <property name="text" > + <string>Insert Column</string> + </property> + <property name="shortcut" > + <string>Ctrl+I, C</string> + </property> + </action> + <action name="removeColumnAction" > + <property name="text" > + <string>Remove Column</string> + </property> + <property name="shortcut" > + <string>Ctrl+R, C</string> + </property> + </action> + <action name="insertChildAction" > + <property name="text" > + <string>Insert Child</string> + </property> + <property name="shortcut" > + <string>Ctrl+N</string> + </property> + </action> + </widget> + <resources> + <include location="editabletreemodel.qrc" /> + </resources> + <connections/> +</ui> diff --git a/examples/itemviews/editabletreemodel/treeitem.cpp b/examples/itemviews/editabletreemodel/treeitem.cpp new file mode 100644 index 0000000..81a1bf3 --- /dev/null +++ b/examples/itemviews/editabletreemodel/treeitem.cpp @@ -0,0 +1,180 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +/* + treeitem.cpp + + A container for items of data supplied by the simple tree model. +*/ + +#include <QStringList> + +#include "treeitem.h" + +//! [0] +TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent) +{ + parentItem = parent; + itemData = data; +} +//! [0] + +//! [1] +TreeItem::~TreeItem() +{ + qDeleteAll(childItems); +} +//! [1] + +//! [2] +TreeItem *TreeItem::child(int number) +{ + return childItems.value(number); +} +//! [2] + +//! [3] +int TreeItem::childCount() const +{ + return childItems.count(); +} +//! [3] + +//! [4] +int TreeItem::childNumber() const +{ + if (parentItem) + return parentItem->childItems.indexOf(const_cast<TreeItem*>(this)); + + return 0; +} +//! [4] + +//! [5] +int TreeItem::columnCount() const +{ + return itemData.count(); +} +//! [5] + +//! [6] +QVariant TreeItem::data(int column) const +{ + return itemData.value(column); +} +//! [6] + +//! [7] +bool TreeItem::insertChildren(int position, int count, int columns) +{ + if (position < 0 || position > childItems.size()) + return false; + + for (int row = 0; row < count; ++row) { + QVector<QVariant> data(columns); + TreeItem *item = new TreeItem(data, this); + childItems.insert(position, item); + } + + return true; +} +//! [7] + +//! [8] +bool TreeItem::insertColumns(int position, int columns) +{ + if (position < 0 || position > itemData.size()) + return false; + + for (int column = 0; column < columns; ++column) + itemData.insert(position, QVariant()); + + foreach (TreeItem *child, childItems) + child->insertColumns(position, columns); + + return true; +} +//! [8] + +//! [9] +TreeItem *TreeItem::parent() +{ + return parentItem; +} +//! [9] + +//! [10] +bool TreeItem::removeChildren(int position, int count) +{ + if (position < 0 || position + count > childItems.size()) + return false; + + for (int row = 0; row < count; ++row) + delete childItems.takeAt(position); + + return true; +} +//! [10] + +bool TreeItem::removeColumns(int position, int columns) +{ + if (position < 0 || position + columns > itemData.size()) + return false; + + for (int column = 0; column < columns; ++column) + itemData.remove(position); + + foreach (TreeItem *child, childItems) + child->removeColumns(position, columns); + + return true; +} + +//! [11] +bool TreeItem::setData(int column, const QVariant &value) +{ + if (column < 0 || column >= itemData.size()) + return false; + + itemData[column] = value; + return true; +} +//! [11] diff --git a/examples/itemviews/editabletreemodel/treeitem.h b/examples/itemviews/editabletreemodel/treeitem.h new file mode 100644 index 0000000..9a36529 --- /dev/null +++ b/examples/itemviews/editabletreemodel/treeitem.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** 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 TREEITEM_H +#define TREEITEM_H + +#include <QList> +#include <QVariant> +#include <QVector> + +//! [0] +class TreeItem +{ +public: + TreeItem(const QVector<QVariant> &data, TreeItem *parent = 0); + ~TreeItem(); + + TreeItem *child(int number); + int childCount() const; + int columnCount() const; + QVariant data(int column) const; + bool insertChildren(int position, int count, int columns); + bool insertColumns(int position, int columns); + TreeItem *parent(); + bool removeChildren(int position, int count); + bool removeColumns(int position, int columns); + int childNumber() const; + bool setData(int column, const QVariant &value); + +private: + QList<TreeItem*> childItems; + QVector<QVariant> itemData; + TreeItem *parentItem; +}; +//! [0] + +#endif diff --git a/examples/itemviews/editabletreemodel/treemodel.cpp b/examples/itemviews/editabletreemodel/treemodel.cpp new file mode 100644 index 0000000..f1dff03 --- /dev/null +++ b/examples/itemviews/editabletreemodel/treemodel.cpp @@ -0,0 +1,289 @@ +/**************************************************************************** +** +** 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 "treeitem.h" +#include "treemodel.h" + +//! [0] +TreeModel::TreeModel(const QStringList &headers, const QString &data, + QObject *parent) + : QAbstractItemModel(parent) +{ + QVector<QVariant> rootData; + foreach (QString header, headers) + rootData << header; + + rootItem = new TreeItem(rootData); + setupModelData(data.split(QString("\n")), rootItem); +} +//! [0] + +//! [1] +TreeModel::~TreeModel() +{ + delete rootItem; +} +//! [1] + +//! [2] +int TreeModel::columnCount(const QModelIndex & /* parent */) const +{ + return rootItem->columnCount(); +} +//! [2] + +QVariant TreeModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role != Qt::DisplayRole && role != Qt::EditRole) + return QVariant(); + + TreeItem *item = getItem(index); + + return item->data(index.column()); +} + +//! [3] +Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return 0; + + return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} +//! [3] + +//! [4] +TreeItem *TreeModel::getItem(const QModelIndex &index) const +{ + if (index.isValid()) { + TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); + if (item) return item; + } + return rootItem; +} +//! [4] + +QVariant TreeModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) + return rootItem->data(section); + + return QVariant(); +} + +//! [5] +QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const +{ + if (parent.isValid() && parent.column() != 0) + return QModelIndex(); +//! [5] + +//! [6] + TreeItem *parentItem = getItem(parent); + + TreeItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + else + return QModelIndex(); +} +//! [6] + +bool TreeModel::insertColumns(int position, int columns, const QModelIndex &parent) +{ + bool success; + + beginInsertColumns(parent, position, position + columns - 1); + success = rootItem->insertColumns(position, columns); + endInsertColumns(); + + return success; +} + +bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent) +{ + TreeItem *parentItem = getItem(parent); + bool success; + + beginInsertRows(parent, position, position + rows - 1); + success = parentItem->insertChildren(position, rows, rootItem->columnCount()); + endInsertRows(); + + return success; +} + +//! [7] +QModelIndex TreeModel::parent(const QModelIndex &index) const +{ + if (!index.isValid()) + return QModelIndex(); + + TreeItem *childItem = getItem(index); + TreeItem *parentItem = childItem->parent(); + + if (parentItem == rootItem) + return QModelIndex(); + + return createIndex(parentItem->childNumber(), 0, parentItem); +} +//! [7] + +bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent) +{ + bool success; + + beginRemoveColumns(parent, position, position + columns - 1); + success = rootItem->removeColumns(position, columns); + endRemoveColumns(); + + if (rootItem->columnCount() == 0) + removeRows(0, rowCount()); + + return success; +} + +bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent) +{ + TreeItem *parentItem = getItem(parent); + bool success = true; + + beginRemoveRows(parent, position, position + rows - 1); + success = parentItem->removeChildren(position, rows); + endRemoveRows(); + + return success; +} + +//! [8] +int TreeModel::rowCount(const QModelIndex &parent) const +{ + TreeItem *parentItem = getItem(parent); + + return parentItem->childCount(); +} +//! [8] + +bool TreeModel::setData(const QModelIndex &index, const QVariant &value, + int role) +{ + if (role != Qt::EditRole) + return false; + + TreeItem *item = getItem(index); + bool result = item->setData(index.column(), value); + + if (result) + emit dataChanged(index, index); + + return result; +} + +bool TreeModel::setHeaderData(int section, Qt::Orientation orientation, + const QVariant &value, int role) +{ + if (role != Qt::EditRole || orientation != Qt::Horizontal) + return false; + + bool result = rootItem->setData(section, value); + + if (result) + emit headerDataChanged(orientation, section, section); + + return result; +} + +void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) +{ + QList<TreeItem*> parents; + QList<int> indentations; + parents << parent; + indentations << 0; + + int number = 0; + + while (number < lines.count()) { + int position = 0; + while (position < lines[number].length()) { + if (lines[number].mid(position, 1) != " ") + break; + position++; + } + + QString lineData = lines[number].mid(position).trimmed(); + + if (!lineData.isEmpty()) { + // Read the column data from the rest of the line. + QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); + QVector<QVariant> columnData; + for (int column = 0; column < columnStrings.count(); ++column) + columnData << columnStrings[column]; + + if (position > indentations.last()) { + // The last child of the current parent is now the new parent + // unless the current parent has no children. + + if (parents.last()->childCount() > 0) { + parents << parents.last()->child(parents.last()->childCount()-1); + indentations << position; + } + } else { + while (position < indentations.last() && parents.count() > 0) { + parents.pop_back(); + indentations.pop_back(); + } + } + + // Append a new item to the current parent's list of children. + TreeItem *parent = parents.last(); + parent->insertChildren(parent->childCount(), 1, rootItem->columnCount()); + for (int column = 0; column < columnData.size(); ++column) + parent->child(parent->childCount() - 1)->setData(column, columnData[column]); + } + + number++; + } +} diff --git a/examples/itemviews/editabletreemodel/treemodel.h b/examples/itemviews/editabletreemodel/treemodel.h new file mode 100644 index 0000000..36ae739 --- /dev/null +++ b/examples/itemviews/editabletreemodel/treemodel.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** 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 TREEMODEL_H +#define TREEMODEL_H + +#include <QAbstractItemModel> +#include <QModelIndex> +#include <QVariant> + +class TreeItem; + +//! [0] +class TreeModel : public QAbstractItemModel +{ + Q_OBJECT + +public: + TreeModel(const QStringList &headers, const QString &data, + QObject *parent = 0); + ~TreeModel(); +//! [0] //! [1] + + QVariant data(const QModelIndex &index, int role) const; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex &index) const; + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; +//! [1] + +//! [2] + Qt::ItemFlags flags(const QModelIndex &index) const; + bool setData(const QModelIndex &index, const QVariant &value, + int role = Qt::EditRole); + bool setHeaderData(int section, Qt::Orientation orientation, + const QVariant &value, int role = Qt::EditRole); + + bool insertColumns(int position, int columns, + const QModelIndex &parent = QModelIndex()); + bool removeColumns(int position, int columns, + const QModelIndex &parent = QModelIndex()); + bool insertRows(int position, int rows, + const QModelIndex &parent = QModelIndex()); + bool removeRows(int position, int rows, + const QModelIndex &parent = QModelIndex()); + +private: + void setupModelData(const QStringList &lines, TreeItem *parent); + TreeItem *getItem(const QModelIndex &index) const; + + TreeItem *rootItem; +}; +//! [2] + +#endif diff --git a/examples/itemviews/fetchmore/fetchmore.pro b/examples/itemviews/fetchmore/fetchmore.pro new file mode 100644 index 0000000..dcb84ed --- /dev/null +++ b/examples/itemviews/fetchmore/fetchmore.pro @@ -0,0 +1,12 @@ +HEADERS = filelistmodel.h \ + window.h +SOURCES = filelistmodel.cpp \ + main.cpp \ + window.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/fetchmore +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/fetchmore +INSTALLS += target sources + diff --git a/examples/itemviews/fetchmore/filelistmodel.cpp b/examples/itemviews/fetchmore/filelistmodel.cpp new file mode 100644 index 0000000..fc51087 --- /dev/null +++ b/examples/itemviews/fetchmore/filelistmodel.cpp @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** 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 "filelistmodel.h" +#include <QApplication> +#include <QPalette> +#include <QBrush> +#include <QDir> + +FileListModel::FileListModel(QObject *parent) + : QAbstractListModel(parent) +{ +} + +//![4] +int FileListModel::rowCount(const QModelIndex & /* parent */) const +{ + return fileCount; +} + +QVariant FileListModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() >= fileList.size() || index.row() < 0) + return QVariant(); + + if (role == Qt::DisplayRole) + return fileList.at(index.row()); + else if (role == Qt::BackgroundRole) { + int batch = (index.row() / 100) % 2; + if (batch == 0) + return qApp->palette().base(); + else + return qApp->palette().alternateBase(); + } + return QVariant(); +} +//![4] + +//![1] +bool FileListModel::canFetchMore(const QModelIndex & /* index */) const +{ + if (fileCount < fileList.size()) + return true; + else + return false; +} +//![1] + +//![2] +void FileListModel::fetchMore(const QModelIndex & /* index */) +{ + int remainder = fileList.size() - fileCount; + int itemsToFetch = qMin(100, remainder); + + beginInsertRows(QModelIndex(), fileCount, fileCount+itemsToFetch); + + fileCount += itemsToFetch; + + endInsertRows(); + + emit numberPopulated(itemsToFetch); +} +//![2] + +//![0] +void FileListModel::setDirPath(const QString &path) +{ + QDir dir(path); + + fileList = dir.entryList(); + fileCount = 0; + reset(); +} +//![0] + diff --git a/examples/itemviews/fetchmore/filelistmodel.h b/examples/itemviews/fetchmore/filelistmodel.h new file mode 100644 index 0000000..921a9e2 --- /dev/null +++ b/examples/itemviews/fetchmore/filelistmodel.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** 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 FILELISTMODEL_H +#define FILELISTMODEL_H + +#include <QAbstractListModel> +#include <QList> +#include <QStringList> + +//![0] +class FileListModel : public QAbstractListModel +{ + Q_OBJECT + +public: + FileListModel(QObject *parent = 0); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + +signals: + void numberPopulated(int number); + +public slots: + void setDirPath(const QString &path); + +protected: + bool canFetchMore(const QModelIndex &parent) const; + void fetchMore(const QModelIndex &parent); + +private: + QStringList fileList; + int fileCount; +}; +//![0] + +#endif diff --git a/examples/itemviews/fetchmore/main.cpp b/examples/itemviews/fetchmore/main.cpp new file mode 100644 index 0000000..58c9705 --- /dev/null +++ b/examples/itemviews/fetchmore/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** 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 <QApplication> +#include "window.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/fetchmore/window.cpp b/examples/itemviews/fetchmore/window.cpp new file mode 100644 index 0000000..066f26b --- /dev/null +++ b/examples/itemviews/fetchmore/window.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** 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 "filelistmodel.h" +#include "window.h" + +Window::Window(QWidget *parent) + : QWidget(parent) +{ + FileListModel *model = new FileListModel(this); + model->setDirPath(QLibraryInfo::location(QLibraryInfo::PrefixPath)); + + QLabel *label = new QLabel(tr("&Directory:")); + QLineEdit *lineEdit = new QLineEdit; + label->setBuddy(lineEdit); + + QListView *view = new QListView; + view->setModel(model); + + logViewer = new QTextBrowser; + logViewer->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)); + + connect(lineEdit, SIGNAL(textChanged(const QString &)), + model, SLOT(setDirPath(const QString &))); + connect(lineEdit, SIGNAL(textChanged(const QString &)), + logViewer, SLOT(clear())); + connect(model, SIGNAL(numberPopulated(int)), + this, SLOT(updateLog(int))); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(label, 0, 0); + layout->addWidget(lineEdit, 0, 1); + layout->addWidget(view, 1, 0, 1, 2); + layout->addWidget(logViewer, 2, 0, 1, 2); + + setLayout(layout); + setWindowTitle(tr("Fetch More Example")); +} + +void Window::updateLog(int number) +{ + logViewer->append(tr("%1 items added.").arg(number)); +} diff --git a/examples/itemviews/fetchmore/window.h b/examples/itemviews/fetchmore/window.h new file mode 100644 index 0000000..1b8076f --- /dev/null +++ b/examples/itemviews/fetchmore/window.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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 QTextBrowser; +QT_END_NAMESPACE + +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(QWidget *parent = 0); + +public slots: + void updateLog(int number); + +private: + QTextBrowser *logViewer; +}; + +#endif diff --git a/examples/itemviews/itemviews.pro b/examples/itemviews/itemviews.pro new file mode 100644 index 0000000..6202bbc --- /dev/null +++ b/examples/itemviews/itemviews.pro @@ -0,0 +1,22 @@ +TEMPLATE = subdirs +SUBDIRS = addressbook \ + basicsortfiltermodel \ + chart \ + coloreditorfactory \ + combowidgetmapper \ + customsortfiltermodel \ + dirview \ + editabletreemodel \ + fetchmore \ + pixelator \ + puzzle \ + simpledommodel \ + simpletreemodel \ + simplewidgetmapper \ + spinboxdelegate \ + stardelegate + +# install +sources.files = README *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews +INSTALLS += sources diff --git a/examples/itemviews/pixelator/imagemodel.cpp b/examples/itemviews/pixelator/imagemodel.cpp new file mode 100644 index 0000000..ea42b62 --- /dev/null +++ b/examples/itemviews/pixelator/imagemodel.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** 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 "imagemodel.h" + +//! [0] +ImageModel::ImageModel(QObject *parent) + : QAbstractTableModel(parent) +{ +} +//! [0] + +//! [1] +void ImageModel::setImage(const QImage &image) +{ + modelImage = image; + reset(); +} +//! [1] + +//! [2] +int ImageModel::rowCount(const QModelIndex & /* parent */) const +{ + return modelImage.height(); +} + +int ImageModel::columnCount(const QModelIndex & /* parent */) const +//! [2] //! [3] +{ + return modelImage.width(); +} +//! [3] + +//! [4] +QVariant ImageModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || role != Qt::DisplayRole) + return QVariant(); + return qGray(modelImage.pixel(index.column(), index.row())); +} +//! [4] + +//! [5] +QVariant ImageModel::headerData(int /* section */, + Qt::Orientation /* orientation */, + int role) const +{ + if (role == Qt::SizeHintRole) + return QSize(1, 1); + return QVariant(); +} +//! [5] diff --git a/examples/itemviews/pixelator/imagemodel.h b/examples/itemviews/pixelator/imagemodel.h new file mode 100644 index 0000000..e92115a --- /dev/null +++ b/examples/itemviews/pixelator/imagemodel.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** 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 IMAGEMODEL_H +#define IMAGEMODEL_H + +#include <QAbstractTableModel> +#include <QImage> + +//! [0] +class ImageModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + ImageModel(QObject *parent = 0); + + void setImage(const QImage &image); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + +private: + QImage modelImage; +}; +//! [0] + +#endif diff --git a/examples/itemviews/pixelator/images.qrc b/examples/itemviews/pixelator/images.qrc new file mode 100644 index 0000000..c105e13 --- /dev/null +++ b/examples/itemviews/pixelator/images.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/qt.png</file> +</qresource> +</RCC> diff --git a/examples/itemviews/pixelator/images/qt.png b/examples/itemviews/pixelator/images/qt.png Binary files differnew file mode 100644 index 0000000..a2c9c77 --- /dev/null +++ b/examples/itemviews/pixelator/images/qt.png diff --git a/examples/itemviews/pixelator/main.cpp b/examples/itemviews/pixelator/main.cpp new file mode 100644 index 0000000..7a30347 --- /dev/null +++ b/examples/itemviews/pixelator/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** 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 <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(images); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + window.openImage(":/images/qt.png"); + return app.exec(); +} diff --git a/examples/itemviews/pixelator/mainwindow.cpp b/examples/itemviews/pixelator/mainwindow.cpp new file mode 100644 index 0000000..a2b98cf --- /dev/null +++ b/examples/itemviews/pixelator/mainwindow.cpp @@ -0,0 +1,245 @@ +/**************************************************************************** +** +** 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 "imagemodel.h" +#include "mainwindow.h" +#include "pixeldelegate.h" + +//! [0] +MainWindow::MainWindow() +{ +//! [0] + currentPath = QDir::homePath(); + model = new ImageModel(this); + + QWidget *centralWidget = new QWidget; + +//! [1] + view = new QTableView; + view->setShowGrid(false); + view->horizontalHeader()->hide(); + view->verticalHeader()->hide(); + view->horizontalHeader()->setMinimumSectionSize(1); + view->verticalHeader()->setMinimumSectionSize(1); + view->setModel(model); +//! [1] + +//! [2] + PixelDelegate *delegate = new PixelDelegate(this); + view->setItemDelegate(delegate); +//! [2] + +//! [3] + QLabel *pixelSizeLabel = new QLabel(tr("Pixel size:")); + QSpinBox *pixelSizeSpinBox = new QSpinBox; + pixelSizeSpinBox->setMinimum(4); + pixelSizeSpinBox->setMaximum(32); + pixelSizeSpinBox->setValue(12); +//! [3] + + QMenu *fileMenu = new QMenu(tr("&File"), this); + QAction *openAction = fileMenu->addAction(tr("&Open...")); + openAction->setShortcut(QKeySequence(tr("Ctrl+O"))); + + printAction = fileMenu->addAction(tr("&Print...")); + printAction->setEnabled(false); + printAction->setShortcut(QKeySequence(tr("Ctrl+P"))); + + QAction *quitAction = fileMenu->addAction(tr("E&xit")); + quitAction->setShortcut(QKeySequence(tr("Ctrl+Q"))); + + QMenu *helpMenu = new QMenu(tr("&Help"), this); + QAction *aboutAction = helpMenu->addAction(tr("&About")); + + menuBar()->addMenu(fileMenu); + menuBar()->addSeparator(); + menuBar()->addMenu(helpMenu); + + connect(openAction, SIGNAL(triggered()), this, SLOT(chooseImage())); + connect(printAction, SIGNAL(triggered()), this, SLOT(printImage())); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAboutBox())); +//! [4] + connect(pixelSizeSpinBox, SIGNAL(valueChanged(int)), + delegate, SLOT(setPixelSize(int))); + connect(pixelSizeSpinBox, SIGNAL(valueChanged(int)), + this, SLOT(updateView())); +//! [4] + + QHBoxLayout *controlsLayout = new QHBoxLayout; + controlsLayout->addWidget(pixelSizeLabel); + controlsLayout->addWidget(pixelSizeSpinBox); + controlsLayout->addStretch(1); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(view); + mainLayout->addLayout(controlsLayout); + centralWidget->setLayout(mainLayout); + + setCentralWidget(centralWidget); + + setWindowTitle(tr("Pixelator")); + resize(640, 480); +//! [5] +} +//! [5] + +void MainWindow::chooseImage() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Choose an image"), currentPath, "*"); + + if (!fileName.isEmpty()) + openImage(fileName); +} + +void MainWindow::openImage(const QString &fileName) +{ + QImage image; + + if (image.load(fileName)) { + model->setImage(image); + if (!fileName.startsWith(":/")) { + currentPath = fileName; + setWindowTitle(tr("%1 - Pixelator").arg(currentPath)); + } + + printAction->setEnabled(true); + updateView(); + } +} + +void MainWindow::printImage() +{ +#ifndef QT_NO_PRINTER + if (model->rowCount(QModelIndex())*model->columnCount(QModelIndex()) + > 90000) { + QMessageBox::StandardButton answer; + answer = QMessageBox::question(this, tr("Large Image Size"), + tr("The printed image may be very large. Are you sure that " + "you want to print it?"), + QMessageBox::Yes | QMessageBox::No); + if (answer == QMessageBox::No) + return; + } + + QPrinter printer(QPrinter::HighResolution); + + QPrintDialog *dlg = new QPrintDialog(&printer, this); + dlg->setWindowTitle(tr("Print Image")); + + if (dlg->exec() != QDialog::Accepted) + return; + + QPainter painter; + painter.begin(&printer); + + int rows = model->rowCount(QModelIndex()); + int columns = model->columnCount(QModelIndex()); + int sourceWidth = (columns+1) * ItemSize; + int sourceHeight = (rows+1) * ItemSize; + + painter.save(); + + double xscale = printer.pageRect().width()/double(sourceWidth); + double yscale = printer.pageRect().height()/double(sourceHeight); + double scale = qMin(xscale, yscale); + + painter.translate(printer.paperRect().x() + printer.pageRect().width()/2, + printer.paperRect().y() + printer.pageRect().height()/2); + painter.scale(scale, scale); + painter.translate(-sourceWidth/2, -sourceHeight/2); + + QStyleOptionViewItem option; + QModelIndex parent = QModelIndex(); + + QProgressDialog progress(tr("Printing..."), tr("Cancel"), 0, rows, this); + progress.setWindowModality(Qt::ApplicationModal); + float y = ItemSize/2; + + for (int row = 0; row < rows; ++row) { + progress.setValue(row); + qApp->processEvents(); + if (progress.wasCanceled()) + break; + + float x = ItemSize/2; + + for (int column = 0; column < columns; ++column) { + option.rect = QRect(int(x), int(y), ItemSize, ItemSize); + view->itemDelegate()->paint(&painter, option, + model->index(row, column, parent)); + x = x + ItemSize; + } + y = y + ItemSize; + } + progress.setValue(rows); + + painter.restore(); + painter.end(); + + if (progress.wasCanceled()) { + QMessageBox::information(this, tr("Printing canceled"), + tr("The printing process was canceled."), QMessageBox::Cancel); + } +#else + QMessageBox::information(this, tr("Printing canceled"), + tr("Printing is not supported on this Qt build"), QMessageBox::Cancel); +#endif +} + +void MainWindow::showAboutBox() +{ + QMessageBox::about(this, tr("About the Pixelator example"), + tr("This example demonstrates how a standard view and a custom\n" + "delegate can be used to produce a specialized representation\n" + "of data in a simple custom model.")); +} + +//! [6] +void MainWindow::updateView() +{ + view->resizeColumnsToContents(); + view->resizeRowsToContents(); +} +//! [6] diff --git a/examples/itemviews/pixelator/mainwindow.h b/examples/itemviews/pixelator/mainwindow.h new file mode 100644 index 0000000..df9510e --- /dev/null +++ b/examples/itemviews/pixelator/mainwindow.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +class ImageModel; +QT_BEGIN_NAMESPACE +class QAction; +class QTableView; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + + void openImage(const QString &fileName); + +public slots: + void chooseImage(); + void printImage(); + void showAboutBox(); + void updateView(); + +private: + ImageModel *model; + QAction *printAction; + QString currentPath; + QTableView *view; +}; + +#endif diff --git a/examples/itemviews/pixelator/pixelator.pro b/examples/itemviews/pixelator/pixelator.pro new file mode 100644 index 0000000..dc05296 --- /dev/null +++ b/examples/itemviews/pixelator/pixelator.pro @@ -0,0 +1,14 @@ +HEADERS = imagemodel.h \ + mainwindow.h \ + pixeldelegate.h +SOURCES = imagemodel.cpp \ + main.cpp \ + mainwindow.cpp \ + pixeldelegate.cpp +RESOURCES += images.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/pixelator +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/pixelator +INSTALLS += target sources diff --git a/examples/itemviews/pixelator/pixeldelegate.cpp b/examples/itemviews/pixelator/pixeldelegate.cpp new file mode 100644 index 0000000..496b365 --- /dev/null +++ b/examples/itemviews/pixelator/pixeldelegate.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** 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 "pixeldelegate.h" + +//! [0] +PixelDelegate::PixelDelegate(QObject *parent) + : QAbstractItemDelegate(parent) +{ + pixelSize = 12; +} +//! [0] + +//! [1] +void PixelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ +//! [2] + if (option.state & QStyle::State_Selected) + painter->fillRect(option.rect, option.palette.highlight()); +//! [1] + +//! [3] + int size = qMin(option.rect.width(), option.rect.height()); +//! [3] //! [4] + int brightness = index.model()->data(index, Qt::DisplayRole).toInt(); + double radius = (size/2.0) - (brightness/255.0 * size/2.0); + if (radius == 0.0) + return; +//! [4] + +//! [5] + painter->save(); +//! [5] //! [6] + painter->setRenderHint(QPainter::Antialiasing, true); +//! [6] //! [7] + painter->setPen(Qt::NoPen); +//! [7] //! [8] + if (option.state & QStyle::State_Selected) +//! [8] //! [9] + painter->setBrush(option.palette.highlightedText()); + else +//! [2] + painter->setBrush(QBrush(Qt::black)); +//! [9] + +//! [10] + painter->drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius, + option.rect.y() + option.rect.height()/2 - radius, + 2*radius, 2*radius)); + painter->restore(); +} +//! [10] + +//! [11] +QSize PixelDelegate::sizeHint(const QStyleOptionViewItem & /* option */, + const QModelIndex & /* index */) const +{ + return QSize(pixelSize, pixelSize); +} +//! [11] + +//! [12] +void PixelDelegate::setPixelSize(int size) +{ + pixelSize = size; +} +//! [12] diff --git a/examples/itemviews/pixelator/pixeldelegate.h b/examples/itemviews/pixelator/pixeldelegate.h new file mode 100644 index 0000000..b38e470 --- /dev/null +++ b/examples/itemviews/pixelator/pixeldelegate.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** 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 PIXELDELEGATE_H +#define PIXELDELEGATE_H + +#include <QAbstractItemDelegate> +#include <QFontMetrics> +#include <QModelIndex> +#include <QSize> + +QT_BEGIN_NAMESPACE +class QAbstractItemModel; +class QObject; +class QPainter; +QT_END_NAMESPACE + +static const int ItemSize = 256; + +//! [0] +class PixelDelegate : public QAbstractItemDelegate +{ + Q_OBJECT + +public: + PixelDelegate(QObject *parent = 0); + + void paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + + QSize sizeHint(const QStyleOptionViewItem &option, + const QModelIndex &index ) const; + +public slots: + void setPixelSize(int size); + +private: + int pixelSize; +}; +//! [0] + +#endif diff --git a/examples/itemviews/puzzle/example.jpg b/examples/itemviews/puzzle/example.jpg Binary files differnew file mode 100644 index 0000000..e09fb70 --- /dev/null +++ b/examples/itemviews/puzzle/example.jpg diff --git a/examples/itemviews/puzzle/main.cpp b/examples/itemviews/puzzle/main.cpp new file mode 100644 index 0000000..e0e5cc1 --- /dev/null +++ b/examples/itemviews/puzzle/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** 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 <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(puzzle); + + QApplication app(argc, argv); + MainWindow window; + window.openImage(":/images/example.jpg"); + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/puzzle/mainwindow.cpp b/examples/itemviews/puzzle/mainwindow.cpp new file mode 100644 index 0000000..c6088f6 --- /dev/null +++ b/examples/itemviews/puzzle/mainwindow.cpp @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** 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 <stdlib.h> + +#include "mainwindow.h" +#include "piecesmodel.h" +#include "puzzlewidget.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + setupMenus(); + setupWidgets(); + model = new PiecesModel(this); + piecesList->setModel(model); + + setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); + setWindowTitle(tr("Puzzle")); +} + +void MainWindow::openImage(const QString &path) +{ + QString fileName = path; + + if (fileName.isNull()) + fileName = QFileDialog::getOpenFileName(this, + tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)")); + + if (!fileName.isEmpty()) { + QPixmap newImage; + if (!newImage.load(fileName)) { + QMessageBox::warning(this, tr("Open Image"), + tr("The image file could not be loaded."), + QMessageBox::Cancel); + return; + } + puzzleImage = newImage; + setupPuzzle(); + } +} + +void MainWindow::setCompleted() +{ + QMessageBox::information(this, tr("Puzzle Completed"), + tr("Congratulations! You have completed the puzzle!\n" + "Click OK to start again."), + QMessageBox::Ok); + + setupPuzzle(); +} + +void MainWindow::setupPuzzle() +{ + int size = qMin(puzzleImage.width(), puzzleImage.height()); + puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2, + (puzzleImage.height() - size)/2, size, size).scaled(400, + 400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + + qsrand(QCursor::pos().x() ^ QCursor::pos().y()); + + model->addPieces(puzzleImage); + puzzleWidget->clear(); +} + +void MainWindow::setupMenus() +{ + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + + QAction *openAction = fileMenu->addAction(tr("&Open...")); + openAction->setShortcut(QKeySequence(tr("Ctrl+O"))); + + QAction *exitAction = fileMenu->addAction(tr("E&xit")); + exitAction->setShortcut(QKeySequence(tr("Ctrl+Q"))); + + QMenu *gameMenu = menuBar()->addMenu(tr("&Game")); + + QAction *restartAction = gameMenu->addAction(tr("&Restart")); + + connect(openAction, SIGNAL(triggered()), this, SLOT(openImage())); + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle())); +} + +void MainWindow::setupWidgets() +{ + QFrame *frame = new QFrame; + QHBoxLayout *frameLayout = new QHBoxLayout(frame); + + piecesList = new QListView; + piecesList->setDragEnabled(true); + piecesList->setViewMode(QListView::IconMode); + piecesList->setIconSize(QSize(60, 60)); + piecesList->setGridSize(QSize(80, 80)); + piecesList->setSpacing(10); + piecesList->setMovement(QListView::Snap); + piecesList->setAcceptDrops(true); + piecesList->setDropIndicatorShown(true); + + PiecesModel *model = new PiecesModel(this); + piecesList->setModel(model); + + puzzleWidget = new PuzzleWidget; + + connect(puzzleWidget, SIGNAL(puzzleCompleted()), + this, SLOT(setCompleted()), Qt::QueuedConnection); + + frameLayout->addWidget(piecesList); + frameLayout->addWidget(puzzleWidget); + setCentralWidget(frame); +} diff --git a/examples/itemviews/puzzle/mainwindow.h b/examples/itemviews/puzzle/mainwindow.h new file mode 100644 index 0000000..2fb97d4 --- /dev/null +++ b/examples/itemviews/puzzle/mainwindow.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include <QPixmap> +#include <QMainWindow> + +class PuzzleWidget; +class PiecesModel; +QT_BEGIN_NAMESPACE +class QListView; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +public slots: + void openImage(const QString &path = QString()); + void setupPuzzle(); + +private slots: + void setCompleted(); + +private: + void setupMenus(); + void setupWidgets(); + + QPixmap puzzleImage; + QListView *piecesList; + PuzzleWidget *puzzleWidget; + PiecesModel *model; +}; + +#endif diff --git a/examples/itemviews/puzzle/piecesmodel.cpp b/examples/itemviews/puzzle/piecesmodel.cpp new file mode 100644 index 0000000..f480837 --- /dev/null +++ b/examples/itemviews/puzzle/piecesmodel.cpp @@ -0,0 +1,204 @@ +/**************************************************************************** +** +** 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 "piecesmodel.h" + +PiecesModel::PiecesModel(QObject *parent) + : QAbstractListModel(parent) +{ +} + +QVariant PiecesModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role == Qt::DecorationRole) + return QIcon(pixmaps.value(index.row()).scaled(60, 60, + Qt::KeepAspectRatio, Qt::SmoothTransformation)); + else if (role == Qt::UserRole) + return pixmaps.value(index.row()); + else if (role == Qt::UserRole + 1) + return locations.value(index.row()); + + return QVariant(); +} + +void PiecesModel::addPiece(const QPixmap &pixmap, const QPoint &location) +{ + int row; + if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1) + row = 0; + else + row = pixmaps.size(); + + beginInsertRows(QModelIndex(), row, row); + pixmaps.insert(row, pixmap); + locations.insert(row, location); + endInsertRows(); +} + +Qt::ItemFlags PiecesModel::flags(const QModelIndex &index) const +{ + if (index.isValid()) + return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); + + return Qt::ItemIsDropEnabled; +} + +bool PiecesModel::removeRows(int row, int count, const QModelIndex &parent) +{ + if (parent.isValid()) + return false; + + if (row >= pixmaps.size() || row + count <= 0) + return false; + + int beginRow = qMax(0, row); + int endRow = qMin(row + count - 1, pixmaps.size() - 1); + + beginRemoveRows(parent, beginRow, endRow); + + while (beginRow <= endRow) { + pixmaps.removeAt(beginRow); + locations.removeAt(beginRow); + ++beginRow; + } + + endRemoveRows(); + return true; +} + +QStringList PiecesModel::mimeTypes() const +{ + QStringList types; + types << "image/x-puzzle-piece"; + return types; +} + +QMimeData *PiecesModel::mimeData(const QModelIndexList &indexes) const +{ + QMimeData *mimeData = new QMimeData(); + QByteArray encodedData; + + QDataStream stream(&encodedData, QIODevice::WriteOnly); + + foreach (QModelIndex index, indexes) { + if (index.isValid()) { + QPixmap pixmap = qVariantValue<QPixmap>(data(index, Qt::UserRole)); + QPoint location = data(index, Qt::UserRole+1).toPoint(); + stream << pixmap << location; + } + } + + mimeData->setData("image/x-puzzle-piece", encodedData); + return mimeData; +} + +bool PiecesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) +{ + if (!data->hasFormat("image/x-puzzle-piece")) + return false; + + if (action == Qt::IgnoreAction) + return true; + + if (column > 0) + return false; + + int endRow; + + if (!parent.isValid()) { + if (row < 0) + endRow = pixmaps.size(); + else + endRow = qMin(row, pixmaps.size()); + } else + endRow = parent.row(); + + QByteArray encodedData = data->data("image/x-puzzle-piece"); + QDataStream stream(&encodedData, QIODevice::ReadOnly); + + while (!stream.atEnd()) { + QPixmap pixmap; + QPoint location; + stream >> pixmap >> location; + + beginInsertRows(QModelIndex(), endRow, endRow); + pixmaps.insert(endRow, pixmap); + locations.insert(endRow, location); + endInsertRows(); + + ++endRow; + } + + return true; +} + +int PiecesModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return 0; + else + return pixmaps.size(); +} + +Qt::DropActions PiecesModel::supportedDropActions() const +{ + return Qt::CopyAction | Qt::MoveAction; +} + +void PiecesModel::addPieces(const QPixmap& pixmap) +{ + beginRemoveRows(QModelIndex(), 0, 24); + pixmaps.clear(); + locations.clear(); + endRemoveRows(); + for (int y = 0; y < 5; ++y) { + for (int x = 0; x < 5; ++x) { + QPixmap pieceImage = pixmap.copy(x*80, y*80, 80, 80); + addPiece(pieceImage, QPoint(x, y)); + } + } +} diff --git a/examples/itemviews/puzzle/piecesmodel.h b/examples/itemviews/puzzle/piecesmodel.h new file mode 100644 index 0000000..777768a --- /dev/null +++ b/examples/itemviews/puzzle/piecesmodel.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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 PIECESLIST_H +#define PIECESLIST_H + +#include <QAbstractListModel> +#include <QList> +#include <QPixmap> +#include <QPoint> +#include <QStringList> + +QT_BEGIN_NAMESPACE +class QMimeData; +QT_END_NAMESPACE + +class PiecesModel : public QAbstractListModel +{ + Q_OBJECT + +public: + PiecesModel(QObject *parent = 0); + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + bool removeRows(int row, int count, const QModelIndex &parent); + + bool dropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent); + QMimeData *mimeData(const QModelIndexList &indexes) const; + QStringList mimeTypes() const; + int rowCount(const QModelIndex &parent) const; + Qt::DropActions supportedDropActions() const; + + void addPiece(const QPixmap &pixmap, const QPoint &location); + void addPieces(const QPixmap& pixmap); + +private: + QList<QPoint> locations; + QList<QPixmap> pixmaps; +}; + +#endif diff --git a/examples/itemviews/puzzle/puzzle.pro b/examples/itemviews/puzzle/puzzle.pro new file mode 100644 index 0000000..deed112 --- /dev/null +++ b/examples/itemviews/puzzle/puzzle.pro @@ -0,0 +1,14 @@ +HEADERS = mainwindow.h \ + piecesmodel.h \ + puzzlewidget.h +RESOURCES = puzzle.qrc +SOURCES = main.cpp \ + mainwindow.cpp \ + piecesmodel.cpp \ + puzzlewidget.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/puzzle +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro *.jpg +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/puzzle +INSTALLS += target sources diff --git a/examples/itemviews/puzzle/puzzle.qrc b/examples/itemviews/puzzle/puzzle.qrc new file mode 100644 index 0000000..4076cec --- /dev/null +++ b/examples/itemviews/puzzle/puzzle.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/images"> + <file>example.jpg</file> +</qresource> +</RCC> diff --git a/examples/itemviews/puzzle/puzzlewidget.cpp b/examples/itemviews/puzzle/puzzlewidget.cpp new file mode 100644 index 0000000..aa41ead --- /dev/null +++ b/examples/itemviews/puzzle/puzzlewidget.cpp @@ -0,0 +1,205 @@ +/**************************************************************************** +** +** 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 "puzzlewidget.h" + +PuzzleWidget::PuzzleWidget(QWidget *parent) + : QWidget(parent) +{ + setAcceptDrops(true); + setMinimumSize(400, 400); + setMaximumSize(400, 400); +} + +void PuzzleWidget::clear() +{ + pieceLocations.clear(); + piecePixmaps.clear(); + pieceRects.clear(); + highlightedRect = QRect(); + inPlace = 0; + update(); +} + +void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasFormat("image/x-puzzle-piece")) + event->accept(); + else + event->ignore(); +} + +void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event) +{ + QRect updateRect = highlightedRect; + highlightedRect = QRect(); + update(updateRect); + event->accept(); +} + +void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event) +{ + QRect updateRect = highlightedRect.unite(targetSquare(event->pos())); + + if (event->mimeData()->hasFormat("image/x-puzzle-piece") + && findPiece(targetSquare(event->pos())) == -1) { + + highlightedRect = targetSquare(event->pos()); + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + highlightedRect = QRect(); + event->ignore(); + } + + update(updateRect); +} + +void PuzzleWidget::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasFormat("image/x-puzzle-piece") + && findPiece(targetSquare(event->pos())) == -1) { + + QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece"); + QDataStream stream(&pieceData, QIODevice::ReadOnly); + QRect square = targetSquare(event->pos()); + QPixmap pixmap; + QPoint location; + stream >> pixmap >> location; + + pieceLocations.append(location); + piecePixmaps.append(pixmap); + pieceRects.append(square); + + highlightedRect = QRect(); + update(square); + + event->setDropAction(Qt::MoveAction); + event->accept(); + + if (location == QPoint(square.x()/80, square.y()/80)) { + inPlace++; + if (inPlace == 25) + emit puzzleCompleted(); + } + } else { + highlightedRect = QRect(); + event->ignore(); + } +} + +int PuzzleWidget::findPiece(const QRect &pieceRect) const +{ + for (int i = 0; i < pieceRects.size(); ++i) { + if (pieceRect == pieceRects[i]) { + return i; + } + } + return -1; +} + +void PuzzleWidget::mousePressEvent(QMouseEvent *event) +{ + QRect square = targetSquare(event->pos()); + int found = findPiece(square); + + if (found == -1) + return; + + QPoint location = pieceLocations[found]; + QPixmap pixmap = piecePixmaps[found]; + pieceLocations.removeAt(found); + piecePixmaps.removeAt(found); + pieceRects.removeAt(found); + + if (location == QPoint(square.x()/80, square.y()/80)) + inPlace--; + + update(square); + + QByteArray itemData; + QDataStream dataStream(&itemData, QIODevice::WriteOnly); + + dataStream << pixmap << location; + + QMimeData *mimeData = new QMimeData; + mimeData->setData("image/x-puzzle-piece", itemData); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setHotSpot(event->pos() - square.topLeft()); + drag->setPixmap(pixmap); + + if (drag->start(Qt::MoveAction) == 0) { + pieceLocations.insert(found, location); + piecePixmaps.insert(found, pixmap); + pieceRects.insert(found, square); + update(targetSquare(event->pos())); + + if (location == QPoint(square.x()/80, square.y()/80)) + inPlace++; + } +} + +void PuzzleWidget::paintEvent(QPaintEvent *event) +{ + QPainter painter; + painter.begin(this); + painter.fillRect(event->rect(), Qt::white); + + if (highlightedRect.isValid()) { + painter.setBrush(QColor("#ffcccc")); + painter.setPen(Qt::NoPen); + painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1)); + } + + for (int i = 0; i < pieceRects.size(); ++i) { + painter.drawPixmap(pieceRects[i], piecePixmaps[i]); + } + painter.end(); +} + +const QRect PuzzleWidget::targetSquare(const QPoint &position) const +{ + return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80); +} diff --git a/examples/itemviews/puzzle/puzzlewidget.h b/examples/itemviews/puzzle/puzzlewidget.h new file mode 100644 index 0000000..312e25f --- /dev/null +++ b/examples/itemviews/puzzle/puzzlewidget.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** 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 PUZZLEWIDGET_H +#define PUZZLEWIDGET_H + +#include <QList> +#include <QPoint> +#include <QPixmap> +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +class QMouseEvent; +QT_END_NAMESPACE + +class PuzzleWidget : public QWidget +{ + Q_OBJECT + +public: + PuzzleWidget(QWidget *parent = 0); + void clear(); + +signals: + void puzzleCompleted(); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + void mousePressEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event); + +private: + int findPiece(const QRect &pieceRect) const; + const QRect targetSquare(const QPoint &position) const; + + QList<QPixmap> piecePixmaps; + QList<QRect> pieceRects; + QList<QPoint> pieceLocations; + QRect highlightedRect; + int inPlace; +}; + +#endif diff --git a/examples/itemviews/simpledommodel/domitem.cpp b/examples/itemviews/simpledommodel/domitem.cpp new file mode 100644 index 0000000..18f65ea --- /dev/null +++ b/examples/itemviews/simpledommodel/domitem.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** 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 <QtXml> + +#include "domitem.h" + +//! [0] +DomItem::DomItem(QDomNode &node, int row, DomItem *parent) +{ + domNode = node; +//! [0] + // Record the item's location within its parent. +//! [1] + rowNumber = row; + parentItem = parent; +} +//! [1] + +//! [2] +DomItem::~DomItem() +{ + QHash<int,DomItem*>::iterator it; + for (it = childItems.begin(); it != childItems.end(); ++it) + delete it.value(); +} +//! [2] + +//! [3] +QDomNode DomItem::node() const +{ + return domNode; +} +//! [3] + +//! [4] +DomItem *DomItem::parent() +{ + return parentItem; +} +//! [4] + +//! [5] +DomItem *DomItem::child(int i) +{ + if (childItems.contains(i)) + return childItems[i]; + + if (i >= 0 && i < domNode.childNodes().count()) { + QDomNode childNode = domNode.childNodes().item(i); + DomItem *childItem = new DomItem(childNode, i, this); + childItems[i] = childItem; + return childItem; + } + return 0; +} +//! [5] + +//! [6] +int DomItem::row() +{ + return rowNumber; +} +//! [6] diff --git a/examples/itemviews/simpledommodel/domitem.h b/examples/itemviews/simpledommodel/domitem.h new file mode 100644 index 0000000..61f2acc --- /dev/null +++ b/examples/itemviews/simpledommodel/domitem.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** 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 DOMITEM_H +#define DOMITEM_H + +#include <QDomNode> +#include <QHash> + +//! [0] +class DomItem +{ +public: + DomItem(QDomNode &node, int row, DomItem *parent = 0); + ~DomItem(); + DomItem *child(int i); + DomItem *parent(); + QDomNode node() const; + int row(); + +private: + QDomNode domNode; + QHash<int,DomItem*> childItems; + DomItem *parentItem; + int rowNumber; +}; +//! [0] + +#endif diff --git a/examples/itemviews/simpledommodel/dommodel.cpp b/examples/itemviews/simpledommodel/dommodel.cpp new file mode 100644 index 0000000..495fd55 --- /dev/null +++ b/examples/itemviews/simpledommodel/dommodel.cpp @@ -0,0 +1,190 @@ +/**************************************************************************** +** +** 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 <QtXml> + +#include "domitem.h" +#include "dommodel.h" + +//! [0] +DomModel::DomModel(QDomDocument document, QObject *parent) + : QAbstractItemModel(parent), domDocument(document) +{ + rootItem = new DomItem(domDocument, 0); +} +//! [0] + +//! [1] +DomModel::~DomModel() +{ + delete rootItem; +} +//! [1] + +//! [2] +int DomModel::columnCount(const QModelIndex &/*parent*/) const +{ + return 3; +} +//! [2] + +//! [3] +QVariant DomModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role != Qt::DisplayRole) + return QVariant(); + + DomItem *item = static_cast<DomItem*>(index.internalPointer()); + + QDomNode node = item->node(); +//! [3] //! [4] + QStringList attributes; + QDomNamedNodeMap attributeMap = node.attributes(); + + switch (index.column()) { + case 0: + return node.nodeName(); + case 1: + for (int i = 0; i < attributeMap.count(); ++i) { + QDomNode attribute = attributeMap.item(i); + attributes << attribute.nodeName() + "=\"" + +attribute.nodeValue() + "\""; + } + return attributes.join(" "); + case 2: + return node.nodeValue().split("\n").join(" "); + default: + return QVariant(); + } +} +//! [4] + +//! [5] +Qt::ItemFlags DomModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return 0; + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} +//! [5] + +//! [6] +QVariant DomModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + switch (section) { + case 0: + return tr("Name"); + case 1: + return tr("Attributes"); + case 2: + return tr("Value"); + default: + return QVariant(); + } + } + + return QVariant(); +} +//! [6] + +//! [7] +QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) + const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); +//! [7] + +//! [8] + DomItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + else + return QModelIndex(); +} +//! [8] + +//! [9] +QModelIndex DomModel::parent(const QModelIndex &child) const +{ + if (!child.isValid()) + return QModelIndex(); + + DomItem *childItem = static_cast<DomItem*>(child.internalPointer()); + DomItem *parentItem = childItem->parent(); + + if (!parentItem || parentItem == rootItem) + return QModelIndex(); + + return createIndex(parentItem->row(), 0, parentItem); +} +//! [9] + +//! [10] +int DomModel::rowCount(const QModelIndex &parent) const +{ + if (parent.column() > 0) + return 0; + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); + + return parentItem->node().childNodes().count(); +} +//! [10] diff --git a/examples/itemviews/simpledommodel/dommodel.h b/examples/itemviews/simpledommodel/dommodel.h new file mode 100644 index 0000000..1178b2c --- /dev/null +++ b/examples/itemviews/simpledommodel/dommodel.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** 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 DOMMODEL_H +#define DOMMODEL_H + +#include <QAbstractItemModel> +#include <QDomDocument> +#include <QModelIndex> +#include <QVariant> + +class DomItem; + +//! [0] +class DomModel : public QAbstractItemModel +{ + Q_OBJECT + +public: + DomModel(QDomDocument document, QObject *parent = 0); + ~DomModel(); + + QVariant data(const QModelIndex &index, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex &child) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + +private: + QDomDocument domDocument; + DomItem *rootItem; +}; +//! [0] + +#endif diff --git a/examples/itemviews/simpledommodel/main.cpp b/examples/itemviews/simpledommodel/main.cpp new file mode 100644 index 0000000..d2ecfc9 --- /dev/null +++ b/examples/itemviews/simpledommodel/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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 <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.resize(640, 480); + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/simpledommodel/mainwindow.cpp b/examples/itemviews/simpledommodel/mainwindow.cpp new file mode 100644 index 0000000..ac96899 --- /dev/null +++ b/examples/itemviews/simpledommodel/mainwindow.cpp @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** 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 <QDomDocument> +#include <QFile> +#include <QtGui> + +#include "dommodel.h" +#include "mainwindow.h" + +MainWindow::MainWindow() : QMainWindow(), model(0) +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()), + QKeySequence(tr("Ctrl+O"))); + fileMenu->addAction(tr("E&xit"), this, SLOT(close()), + QKeySequence(tr("Ctrl+Q"))); + + model = new DomModel(QDomDocument(), this); + view = new QTreeView(this); + view->setModel(model); + + setCentralWidget(view); + setWindowTitle(tr("Simple DOM Model")); +} + +void MainWindow::openFile() +{ + QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"), + xmlPath, tr("XML files (*.xml);;HTML files (*.html);;" + "SVG files (*.svg);;User Interface files (*.ui)")); + + if (!filePath.isEmpty()) { + QFile file(filePath); + if (file.open(QIODevice::ReadOnly)) { + QDomDocument document; + if (document.setContent(&file)) { + DomModel *newModel = new DomModel(document, this); + view->setModel(newModel); + delete model; + model = newModel; + xmlPath = filePath; + } + file.close(); + } + } +} diff --git a/examples/itemviews/simpledommodel/mainwindow.h b/examples/itemviews/simpledommodel/mainwindow.h new file mode 100644 index 0000000..4bc967a --- /dev/null +++ b/examples/itemviews/simpledommodel/mainwindow.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include <QString> + +class DomModel; +QT_BEGIN_NAMESPACE +class QMenu; +class QTreeView; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +public slots: + void openFile(); + +private: + DomModel *model; + QMenu *fileMenu; + QString xmlPath; + QTreeView *view; +}; + +#endif diff --git a/examples/itemviews/simpledommodel/simpledommodel.pro b/examples/itemviews/simpledommodel/simpledommodel.pro new file mode 100644 index 0000000..bc9b1ca --- /dev/null +++ b/examples/itemviews/simpledommodel/simpledommodel.pro @@ -0,0 +1,15 @@ +HEADERS = domitem.h \ + dommodel.h \ + mainwindow.h +SOURCES = domitem.cpp \ + dommodel.cpp \ + main.cpp \ + mainwindow.cpp +CONFIG += qt +QT += xml + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simpledommodel +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simpledommodel +INSTALLS += target sources diff --git a/examples/itemviews/simpletreemodel/default.txt b/examples/itemviews/simpletreemodel/default.txt new file mode 100644 index 0000000..2b2fb57 --- /dev/null +++ b/examples/itemviews/simpletreemodel/default.txt @@ -0,0 +1,40 @@ +Getting Started How to familiarize yourself with Qt Designer + Launching Designer Running the Qt Designer application + The User Interface How to interact with Qt Designer + +Designing a Component Creating a GUI for your application + Creating a Dialog How to create a dialog + Composing the Dialog Putting widgets into the dialog example + Creating a Layout Arranging widgets on a form + Signal and Slot Connections Making widget communicate with each other + +Using a Component in Your Application Generating code from forms + The Direct Approach Using a form without any adjustments + The Single Inheritance Approach Subclassing a form's base class + The Multiple Inheritance Approach Subclassing the form itself + Automatic Connections Connecting widgets using a naming scheme + A Dialog Without Auto-Connect How to connect widgets without a naming scheme + A Dialog With Auto-Connect Using automatic connections + +Form Editing Mode How to edit a form in Qt Designer + Managing Forms Loading and saving forms + Editing a Form Basic editing techniques + The Property Editor Changing widget properties + The Object Inspector Examining the hierarchy of objects on a form + Layouts Objects that arrange widgets on a form + Applying and Breaking Layouts Managing widgets in layouts + Horizontal and Vertical Layouts Standard row and column layouts + The Grid Layout Arranging widgets in a matrix + Previewing Forms Checking that the design works + +Using Containers How to group widgets together + General Features Common container features + Frames QFrame + Group Boxes QGroupBox + Stacked Widgets QStackedWidget + Tab Widgets QTabWidget + Toolbox Widgets QToolBox + +Connection Editing Mode Connecting widgets together with signals and slots + Connecting Objects Making connections in Qt Designer + Editing Connections Changing existing connections diff --git a/examples/itemviews/simpletreemodel/main.cpp b/examples/itemviews/simpletreemodel/main.cpp new file mode 100644 index 0000000..89bdc77 --- /dev/null +++ b/examples/itemviews/simpletreemodel/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** 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 "treemodel.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(simpletreemodel); + + QApplication app(argc, argv); + + QFile file(":/default.txt"); + file.open(QIODevice::ReadOnly); + TreeModel model(file.readAll()); + file.close(); + + QTreeView view; + view.setModel(&model); + view.setWindowTitle(QObject::tr("Simple Tree Model")); + view.show(); + return app.exec(); +} diff --git a/examples/itemviews/simpletreemodel/simpletreemodel.pro b/examples/itemviews/simpletreemodel/simpletreemodel.pro new file mode 100644 index 0000000..9db74d4 --- /dev/null +++ b/examples/itemviews/simpletreemodel/simpletreemodel.pro @@ -0,0 +1,13 @@ +HEADERS = treeitem.h \ + treemodel.h +RESOURCES = simpletreemodel.qrc +SOURCES = treeitem.cpp \ + treemodel.cpp \ + main.cpp +CONFIG += qt + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simpletreemodel +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro *.txt +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simpletreemodel +INSTALLS += target sources diff --git a/examples/itemviews/simpletreemodel/simpletreemodel.qrc b/examples/itemviews/simpletreemodel/simpletreemodel.qrc new file mode 100644 index 0000000..a8ecc98 --- /dev/null +++ b/examples/itemviews/simpletreemodel/simpletreemodel.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>default.txt</file> +</qresource> +</RCC> diff --git a/examples/itemviews/simpletreemodel/treeitem.cpp b/examples/itemviews/simpletreemodel/treeitem.cpp new file mode 100644 index 0000000..d2c1eed --- /dev/null +++ b/examples/itemviews/simpletreemodel/treeitem.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +/* + treeitem.cpp + + A container for items of data supplied by the simple tree model. +*/ + +#include <QStringList> + +#include "treeitem.h" + +//! [0] +TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) +{ + parentItem = parent; + itemData = data; +} +//! [0] + +//! [1] +TreeItem::~TreeItem() +{ + qDeleteAll(childItems); +} +//! [1] + +//! [2] +void TreeItem::appendChild(TreeItem *item) +{ + childItems.append(item); +} +//! [2] + +//! [3] +TreeItem *TreeItem::child(int row) +{ + return childItems.value(row); +} +//! [3] + +//! [4] +int TreeItem::childCount() const +{ + return childItems.count(); +} +//! [4] + +//! [5] +int TreeItem::columnCount() const +{ + return itemData.count(); +} +//! [5] + +//! [6] +QVariant TreeItem::data(int column) const +{ + return itemData.value(column); +} +//! [6] + +//! [7] +TreeItem *TreeItem::parent() +{ + return parentItem; +} +//! [7] + +//! [8] +int TreeItem::row() const +{ + if (parentItem) + return parentItem->childItems.indexOf(const_cast<TreeItem*>(this)); + + return 0; +} +//! [8] diff --git a/examples/itemviews/simpletreemodel/treeitem.h b/examples/itemviews/simpletreemodel/treeitem.h new file mode 100644 index 0000000..7bfd63b --- /dev/null +++ b/examples/itemviews/simpletreemodel/treeitem.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** 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 TREEITEM_H +#define TREEITEM_H + +#include <QList> +#include <QVariant> + +//! [0] +class TreeItem +{ +public: + TreeItem(const QList<QVariant> &data, TreeItem *parent = 0); + ~TreeItem(); + + void appendChild(TreeItem *child); + + TreeItem *child(int row); + int childCount() const; + int columnCount() const; + QVariant data(int column) const; + int row() const; + TreeItem *parent(); + +private: + QList<TreeItem*> childItems; + QList<QVariant> itemData; + TreeItem *parentItem; +}; +//! [0] + +#endif diff --git a/examples/itemviews/simpletreemodel/treemodel.cpp b/examples/itemviews/simpletreemodel/treemodel.cpp new file mode 100644 index 0000000..ec7bd64 --- /dev/null +++ b/examples/itemviews/simpletreemodel/treemodel.cpp @@ -0,0 +1,219 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +/* + treemodel.cpp + + Provides a simple tree model to show how to create and use hierarchical + models. +*/ + +#include <QtGui> + +#include "treeitem.h" +#include "treemodel.h" + +//! [0] +TreeModel::TreeModel(const QString &data, QObject *parent) + : QAbstractItemModel(parent) +{ + QList<QVariant> rootData; + rootData << "Title" << "Summary"; + rootItem = new TreeItem(rootData); + setupModelData(data.split(QString("\n")), rootItem); +} +//! [0] + +//! [1] +TreeModel::~TreeModel() +{ + delete rootItem; +} +//! [1] + +//! [2] +int TreeModel::columnCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return static_cast<TreeItem*>(parent.internalPointer())->columnCount(); + else + return rootItem->columnCount(); +} +//! [2] + +//! [3] +QVariant TreeModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role != Qt::DisplayRole) + return QVariant(); + + TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); + + return item->data(index.column()); +} +//! [3] + +//! [4] +Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return 0; + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} +//! [4] + +//! [5] +QVariant TreeModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) + return rootItem->data(section); + + return QVariant(); +} +//! [5] + +//! [6] +QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) + const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + + TreeItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<TreeItem*>(parent.internalPointer()); + + TreeItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + else + return QModelIndex(); +} +//! [6] + +//! [7] +QModelIndex TreeModel::parent(const QModelIndex &index) const +{ + if (!index.isValid()) + return QModelIndex(); + + TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer()); + TreeItem *parentItem = childItem->parent(); + + if (parentItem == rootItem) + return QModelIndex(); + + return createIndex(parentItem->row(), 0, parentItem); +} +//! [7] + +//! [8] +int TreeModel::rowCount(const QModelIndex &parent) const +{ + TreeItem *parentItem; + if (parent.column() > 0) + return 0; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<TreeItem*>(parent.internalPointer()); + + return parentItem->childCount(); +} +//! [8] + +void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) +{ + QList<TreeItem*> parents; + QList<int> indentations; + parents << parent; + indentations << 0; + + int number = 0; + + while (number < lines.count()) { + int position = 0; + while (position < lines[number].length()) { + if (lines[number].mid(position, 1) != " ") + break; + position++; + } + + QString lineData = lines[number].mid(position).trimmed(); + + if (!lineData.isEmpty()) { + // Read the column data from the rest of the line. + QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); + QList<QVariant> columnData; + for (int column = 0; column < columnStrings.count(); ++column) + columnData << columnStrings[column]; + + if (position > indentations.last()) { + // The last child of the current parent is now the new parent + // unless the current parent has no children. + + if (parents.last()->childCount() > 0) { + parents << parents.last()->child(parents.last()->childCount()-1); + indentations << position; + } + } else { + while (position < indentations.last() && parents.count() > 0) { + parents.pop_back(); + indentations.pop_back(); + } + } + + // Append a new item to the current parent's list of children. + parents.last()->appendChild(new TreeItem(columnData, parents.last())); + } + + number++; + } +} diff --git a/examples/itemviews/simpletreemodel/treemodel.h b/examples/itemviews/simpletreemodel/treemodel.h new file mode 100644 index 0000000..3b535f9 --- /dev/null +++ b/examples/itemviews/simpletreemodel/treemodel.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** 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 TREEMODEL_H +#define TREEMODEL_H + +#include <QAbstractItemModel> +#include <QModelIndex> +#include <QVariant> + +class TreeItem; + +//! [0] +class TreeModel : public QAbstractItemModel +{ + Q_OBJECT + +public: + TreeModel(const QString &data, QObject *parent = 0); + ~TreeModel(); + + QVariant data(const QModelIndex &index, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex &index) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + +private: + void setupModelData(const QStringList &lines, TreeItem *parent); + + TreeItem *rootItem; +}; +//! [0] + +#endif diff --git a/examples/itemviews/simplewidgetmapper/main.cpp b/examples/itemviews/simplewidgetmapper/main.cpp new file mode 100644 index 0000000..055ac21 --- /dev/null +++ b/examples/itemviews/simplewidgetmapper/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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 <QApplication> + +#include "window.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/simplewidgetmapper/simplewidgetmapper.pro b/examples/itemviews/simplewidgetmapper/simplewidgetmapper.pro new file mode 100644 index 0000000..b2940d7 --- /dev/null +++ b/examples/itemviews/simplewidgetmapper/simplewidgetmapper.pro @@ -0,0 +1,9 @@ +HEADERS = window.h +SOURCES = main.cpp \ + window.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simplewidgetmapper +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simplewidgetmapper +INSTALLS += target sources diff --git a/examples/itemviews/simplewidgetmapper/window.cpp b/examples/itemviews/simplewidgetmapper/window.cpp new file mode 100644 index 0000000..406a3d7 --- /dev/null +++ b/examples/itemviews/simplewidgetmapper/window.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** 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" + +//! [Set up widgets] +Window::Window(QWidget *parent) + : QWidget(parent) +{ + setupModel(); + + nameLabel = new QLabel(tr("Na&me:")); + nameEdit = new QLineEdit(); + addressLabel = new QLabel(tr("&Address:")); + addressEdit = new QTextEdit(); + ageLabel = new QLabel(tr("A&ge (in years):")); + ageSpinBox = new QSpinBox(); + nextButton = new QPushButton(tr("&Next")); + previousButton = new QPushButton(tr("&Previous")); + + nameLabel->setBuddy(nameEdit); + addressLabel->setBuddy(addressEdit); + ageLabel->setBuddy(ageSpinBox); +//! [Set up widgets] + +//! [Set up the mapper] + mapper = new QDataWidgetMapper(this); + mapper->setModel(model); + mapper->addMapping(nameEdit, 0); + mapper->addMapping(addressEdit, 1); + mapper->addMapping(ageSpinBox, 2); + + connect(previousButton, SIGNAL(clicked()), + mapper, SLOT(toPrevious())); + connect(nextButton, SIGNAL(clicked()), + mapper, SLOT(toNext())); + connect(mapper, SIGNAL(currentIndexChanged(int)), + this, SLOT(updateButtons(int))); +//! [Set up the mapper] + +//! [Set up the layout] + QGridLayout *layout = new QGridLayout(); + layout->addWidget(nameLabel, 0, 0, 1, 1); + layout->addWidget(nameEdit, 0, 1, 1, 1); + layout->addWidget(previousButton, 0, 2, 1, 1); + layout->addWidget(addressLabel, 1, 0, 1, 1); + layout->addWidget(addressEdit, 1, 1, 2, 1); + layout->addWidget(nextButton, 1, 2, 1, 1); + layout->addWidget(ageLabel, 3, 0, 1, 1); + layout->addWidget(ageSpinBox, 3, 1, 1, 1); + setLayout(layout); + + setWindowTitle(tr("Simple Widget Mapper")); + mapper->toFirst(); +} +//! [Set up the layout] + +//! [Set up the model] +void Window::setupModel() +{ + model = new QStandardItemModel(5, 3, this); + + QStringList names; + names << "Alice" << "Bob" << "Carol" << "Donald" << "Emma"; + + QStringList addresses; + addresses << "<qt>123 Main Street<br/>Market Town</qt>" + << "<qt>PO Box 32<br/>Mail Handling Service" + "<br/>Service City</qt>" + << "<qt>The Lighthouse<br/>Remote Island</qt>" + << "<qt>47338 Park Avenue<br/>Big City</qt>" + << "<qt>Research Station<br/>Base Camp<br/>Big Mountain</qt>"; + + QStringList ages; + ages << "20" << "31" << "32" << "19" << "26"; + + for (int row = 0; row < 5; ++row) { + QStandardItem *item = new QStandardItem(names[row]); + model->setItem(row, 0, item); + item = new QStandardItem(addresses[row]); + model->setItem(row, 1, item); + item = new QStandardItem(ages[row]); + model->setItem(row, 2, item); + } +} +//! [Set up the model] + +//! [Slot for updating the buttons] +void Window::updateButtons(int row) +{ + previousButton->setEnabled(row > 0); + nextButton->setEnabled(row < model->rowCount() - 1); +} +//! [Slot for updating the buttons] diff --git a/examples/itemviews/simplewidgetmapper/window.h b/examples/itemviews/simplewidgetmapper/window.h new file mode 100644 index 0000000..726d4d6 --- /dev/null +++ b/examples/itemviews/simplewidgetmapper/window.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** 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 QDataWidgetMapper; +class QLabel; +class QLineEdit; +class QPushButton; +class QSpinBox; +class QStandardItemModel; +class QTextEdit; +QT_END_NAMESPACE + +//! [Window definition] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(QWidget *parent = 0); + +private slots: + void updateButtons(int row); + +private: + void setupModel(); + + QLabel *nameLabel; + QLabel *addressLabel; + QLabel *ageLabel; + QLineEdit *nameEdit; + QTextEdit *addressEdit; + QSpinBox *ageSpinBox; + QPushButton *nextButton; + QPushButton *previousButton; + + QStandardItemModel *model; + QDataWidgetMapper *mapper; +}; +//! [Window definition] + +#endif diff --git a/examples/itemviews/spinboxdelegate/delegate.cpp b/examples/itemviews/spinboxdelegate/delegate.cpp new file mode 100644 index 0000000..03a1a23 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/delegate.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +/* + delegate.cpp + + A delegate that allows the user to change integer values from the model + using a spin box widget. +*/ + +#include <QtGui> + +#include "delegate.h" + + +//! [0] +SpinBoxDelegate::SpinBoxDelegate(QObject *parent) + : QItemDelegate(parent) +{ +} +//! [0] + +//! [1] +QWidget *SpinBoxDelegate::createEditor(QWidget *parent, + const QStyleOptionViewItem &/* option */, + const QModelIndex &/* index */) const +{ + QSpinBox *editor = new QSpinBox(parent); + editor->setMinimum(0); + editor->setMaximum(100); + + return editor; +} +//! [1] + +//! [2] +void SpinBoxDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + int value = index.model()->data(index, Qt::EditRole).toInt(); + + QSpinBox *spinBox = static_cast<QSpinBox*>(editor); + spinBox->setValue(value); +} +//! [2] + +//! [3] +void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const +{ + QSpinBox *spinBox = static_cast<QSpinBox*>(editor); + spinBox->interpretText(); + int value = spinBox->value(); + + model->setData(index, value, Qt::EditRole); +} +//! [3] + +//! [4] +void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, + const QStyleOptionViewItem &option, const QModelIndex &/* index */) const +{ + editor->setGeometry(option.rect); +} +//! [4] diff --git a/examples/itemviews/spinboxdelegate/delegate.h b/examples/itemviews/spinboxdelegate/delegate.h new file mode 100644 index 0000000..a81cfd1 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/delegate.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** 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 DELEGATE_H +#define DELEGATE_H + +#include <QItemDelegate> +#include <QModelIndex> +#include <QObject> +#include <QSize> +#include <QSpinBox> + +//! [0] +class SpinBoxDelegate : public QItemDelegate +{ + Q_OBJECT + +public: + SpinBoxDelegate(QObject *parent = 0); + + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + + void setEditorData(QWidget *editor, const QModelIndex &index) const; + void setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const; + + void updateEditorGeometry(QWidget *editor, + const QStyleOptionViewItem &option, const QModelIndex &index) const; +}; +//! [0] + +#endif diff --git a/examples/itemviews/spinboxdelegate/main.cpp b/examples/itemviews/spinboxdelegate/main.cpp new file mode 100644 index 0000000..4b9d6a4 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/main.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +/* + main.cpp + + A simple example that shows how a view can use a custom delegate to edit + data obtained from a model. +*/ + +#include <QApplication> +#include <QHeaderView> +#include <QItemSelectionModel> +#include <QStandardItemModel> +#include <QTableView> + +#include "delegate.h" + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QStandardItemModel model(4, 2); + QTableView tableView; + tableView.setModel(&model); + + SpinBoxDelegate delegate; + tableView.setItemDelegate(&delegate); +//! [0] + + tableView.horizontalHeader()->setStretchLastSection(true); + +//! [1] + for (int row = 0; row < 4; ++row) { + for (int column = 0; column < 2; ++column) { + QModelIndex index = model.index(row, column, QModelIndex()); + model.setData(index, QVariant((row+1) * (column+1))); + } +//! [1] //! [2] + } +//! [2] + +//! [3] + tableView.setWindowTitle(QObject::tr("Spin Box Delegate")); + tableView.show(); + return app.exec(); +} +//! [3] diff --git a/examples/itemviews/spinboxdelegate/spinboxdelegate.pro b/examples/itemviews/spinboxdelegate/spinboxdelegate.pro new file mode 100644 index 0000000..a202748 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/spinboxdelegate.pro @@ -0,0 +1,9 @@ +HEADERS = delegate.h +SOURCES = delegate.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/spinboxdelegate +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/spinboxdelegate +INSTALLS += target sources diff --git a/examples/itemviews/stardelegate/main.cpp b/examples/itemviews/stardelegate/main.cpp new file mode 100644 index 0000000..5b27ee1 --- /dev/null +++ b/examples/itemviews/stardelegate/main.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** 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 "stardelegate.h" +#include "stareditor.h" +#include "starrating.h" + +//! [0] +void populateTableWidget(QTableWidget *tableWidget) +{ + static const struct { + const char *title; + const char *genre; + const char *artist; + int rating; + } staticData[] = { +//! [0] //! [1] + { "Mass in B-Minor", "Baroque", "J.S. Bach", 5 }, +//! [1] + { "Three More Foxes", "Jazz", "Maynard Ferguson", 4 }, + { "Sex Bomb", "Pop", "Tom Jones", 3 }, + { "Barbie Girl", "Pop", "Aqua", 5 }, +//! [2] + { 0, 0, 0, 0 } +//! [2] //! [3] + }; +//! [3] //! [4] + + for (int row = 0; staticData[row].title != 0; ++row) { + QTableWidgetItem *item0 = new QTableWidgetItem(staticData[row].title); + QTableWidgetItem *item1 = new QTableWidgetItem(staticData[row].genre); + QTableWidgetItem *item2 = new QTableWidgetItem(staticData[row].artist); + QTableWidgetItem *item3 = new QTableWidgetItem; + item3->setData(0, + qVariantFromValue(StarRating(staticData[row].rating))); + + tableWidget->setItem(row, 0, item0); + tableWidget->setItem(row, 1, item1); + tableWidget->setItem(row, 2, item2); + tableWidget->setItem(row, 3, item3); + } +} +//! [4] + +//! [5] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QTableWidget tableWidget(4, 4); + tableWidget.setItemDelegate(new StarDelegate); + tableWidget.setEditTriggers(QAbstractItemView::DoubleClicked + | QAbstractItemView::SelectedClicked); + tableWidget.setSelectionBehavior(QAbstractItemView::SelectRows); + + QStringList headerLabels; + headerLabels << "Title" << "Genre" << "Artist" << "Rating"; + tableWidget.setHorizontalHeaderLabels(headerLabels); + + populateTableWidget(&tableWidget); + + tableWidget.resizeColumnsToContents(); + tableWidget.resize(500, 300); + tableWidget.show(); + + return app.exec(); +} +//! [5] diff --git a/examples/itemviews/stardelegate/stardelegate.cpp b/examples/itemviews/stardelegate/stardelegate.cpp new file mode 100644 index 0000000..1e12971 --- /dev/null +++ b/examples/itemviews/stardelegate/stardelegate.cpp @@ -0,0 +1,130 @@ +/**************************************************************************** +** +** 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 "stardelegate.h" +#include "stareditor.h" +#include "starrating.h" + +//! [0] +void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + if (qVariantCanConvert<StarRating>(index.data())) { + StarRating starRating = qVariantValue<StarRating>(index.data()); + + if (option.state & QStyle::State_Selected) + painter->fillRect(option.rect, option.palette.highlight()); + + starRating.paint(painter, option.rect, option.palette, + StarRating::ReadOnly); + } else { + QStyledItemDelegate::paint(painter, option, index); + } +//! [0] +} + +//! [1] +QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + if (qVariantCanConvert<StarRating>(index.data())) { + StarRating starRating = qVariantValue<StarRating>(index.data()); + return starRating.sizeHint(); + } else { + return QStyledItemDelegate::sizeHint(option, index); + } +} +//! [1] + +//! [2] +QWidget *StarDelegate::createEditor(QWidget *parent, + const QStyleOptionViewItem &option, + const QModelIndex &index) const + +{ + if (qVariantCanConvert<StarRating>(index.data())) { + StarEditor *editor = new StarEditor(parent); + connect(editor, SIGNAL(editingFinished()), + this, SLOT(commitAndCloseEditor())); + return editor; + } else { + return QStyledItemDelegate::createEditor(parent, option, index); + } +} +//! [2] + +//! [3] +void StarDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + if (qVariantCanConvert<StarRating>(index.data())) { + StarRating starRating = qVariantValue<StarRating>(index.data()); + StarEditor *starEditor = qobject_cast<StarEditor *>(editor); + starEditor->setStarRating(starRating); + } else { + QStyledItemDelegate::setEditorData(editor, index); + } +} +//! [3] + +//! [4] +void StarDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const +{ + if (qVariantCanConvert<StarRating>(index.data())) { + StarEditor *starEditor = qobject_cast<StarEditor *>(editor); + model->setData(index, qVariantFromValue(starEditor->starRating())); + } else { + QStyledItemDelegate::setModelData(editor, model, index); + } +} +//! [4] + +//! [5] +void StarDelegate::commitAndCloseEditor() +{ + StarEditor *editor = qobject_cast<StarEditor *>(sender()); + emit commitData(editor); + emit closeEditor(editor); +} +//! [5] diff --git a/examples/itemviews/stardelegate/stardelegate.h b/examples/itemviews/stardelegate/stardelegate.h new file mode 100644 index 0000000..84814ed --- /dev/null +++ b/examples/itemviews/stardelegate/stardelegate.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** 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 STARDELEGATE_H +#define STARDELEGATE_H + +#include <QStyledItemDelegate> + +//! [0] +class StarDelegate : public QStyledItemDelegate +{ + Q_OBJECT + +public: + StarDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {} + + void paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + QSize sizeHint(const QStyleOptionViewItem &option, + const QModelIndex &index) const; + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + void setEditorData(QWidget *editor, const QModelIndex &index) const; + void setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const; + +private slots: + void commitAndCloseEditor(); +}; +//! [0] + +#endif diff --git a/examples/itemviews/stardelegate/stardelegate.pro b/examples/itemviews/stardelegate/stardelegate.pro new file mode 100644 index 0000000..4e1ef45 --- /dev/null +++ b/examples/itemviews/stardelegate/stardelegate.pro @@ -0,0 +1,14 @@ +HEADERS = stardelegate.h \ + stareditor.h \ + starrating.h +SOURCES = main.cpp \ + stardelegate.cpp \ + stareditor.cpp \ + starrating.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/stardelegate +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/stardelegate +INSTALLS += target sources + diff --git a/examples/itemviews/stardelegate/stareditor.cpp b/examples/itemviews/stardelegate/stareditor.cpp new file mode 100644 index 0000000..633229f --- /dev/null +++ b/examples/itemviews/stardelegate/stareditor.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** 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 "stareditor.h" +#include "starrating.h" + +//! [0] +StarEditor::StarEditor(QWidget *parent) + : QWidget(parent) +{ + setMouseTracking(true); + setAutoFillBackground(true); +} +//! [0] + +QSize StarEditor::sizeHint() const +{ + return myStarRating.sizeHint(); +} + +//! [1] +void StarEditor::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + myStarRating.paint(&painter, rect(), this->palette(), + StarRating::Editable); +} +//! [1] + +//! [2] +void StarEditor::mouseMoveEvent(QMouseEvent *event) +{ + int star = starAtPosition(event->x()); + + if (star != myStarRating.starCount() && star != -1) { + myStarRating.setStarCount(star); + update(); + } +} +//! [2] + +//! [3] +void StarEditor::mouseReleaseEvent(QMouseEvent * /* event */) +{ + emit editingFinished(); +} +//! [3] + +//! [4] +int StarEditor::starAtPosition(int x) +{ + int star = (x / (myStarRating.sizeHint().width() + / myStarRating.maxStarCount())) + 1; + if (star <= 0 || star > myStarRating.maxStarCount()) + return -1; + + return star; +} +//! [4] diff --git a/examples/itemviews/stardelegate/stareditor.h b/examples/itemviews/stardelegate/stareditor.h new file mode 100644 index 0000000..7365ca1 --- /dev/null +++ b/examples/itemviews/stardelegate/stareditor.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** 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 STAREDITOR_H +#define STAREDITOR_H + +#include <QWidget> + +#include "starrating.h" + +//! [0] +class StarEditor : public QWidget +{ + Q_OBJECT + +public: + StarEditor(QWidget *parent = 0); + + QSize sizeHint() const; + void setStarRating(const StarRating &starRating) { + myStarRating = starRating; + } + StarRating starRating() { return myStarRating; } + +signals: + void editingFinished(); + +protected: + void paintEvent(QPaintEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + +private: + int starAtPosition(int x); + + StarRating myStarRating; +}; +//! [0] + +#endif diff --git a/examples/itemviews/stardelegate/starrating.cpp b/examples/itemviews/stardelegate/starrating.cpp new file mode 100644 index 0000000..e40f22d --- /dev/null +++ b/examples/itemviews/stardelegate/starrating.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** 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 <math.h> + +#include "starrating.h" + +const int PaintingScaleFactor = 20; + +//! [0] +StarRating::StarRating(int starCount, int maxStarCount) +{ + myStarCount = starCount; + myMaxStarCount = maxStarCount; + + starPolygon << QPointF(1.0, 0.5); + for (int i = 1; i < 5; ++i) + starPolygon << QPointF(0.5 + 0.5 * cos(0.8 * i * 3.14), + 0.5 + 0.5 * sin(0.8 * i * 3.14)); + + diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4) + << QPointF(0.6, 0.5) << QPointF(0.5, 0.6) + << QPointF(0.4, 0.5); +} +//! [0] + +//! [1] +QSize StarRating::sizeHint() const +{ + return PaintingScaleFactor * QSize(myMaxStarCount, 1); +} +//! [1] + +//! [2] +void StarRating::paint(QPainter *painter, const QRect &rect, + const QPalette &palette, EditMode mode) const +{ + painter->save(); + + painter->setRenderHint(QPainter::Antialiasing, true); + painter->setPen(Qt::NoPen); + + if (mode == Editable) { + painter->setBrush(palette.highlight()); + } else { + painter->setBrush(palette.foreground()); + } + + int yOffset = (rect.height() - PaintingScaleFactor) / 2; + painter->translate(rect.x(), rect.y() + yOffset); + painter->scale(PaintingScaleFactor, PaintingScaleFactor); + + for (int i = 0; i < myMaxStarCount; ++i) { + if (i < myStarCount) { + painter->drawPolygon(starPolygon, Qt::WindingFill); + } else if (mode == Editable) { + painter->drawPolygon(diamondPolygon, Qt::WindingFill); + } + painter->translate(1.0, 0.0); + } + + painter->restore(); +} +//! [2] diff --git a/examples/itemviews/stardelegate/starrating.h b/examples/itemviews/stardelegate/starrating.h new file mode 100644 index 0000000..1e73076 --- /dev/null +++ b/examples/itemviews/stardelegate/starrating.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** 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 STARRATING_H +#define STARRATING_H + +#include <QMetaType> +#include <QPointF> +#include <QVector> + +//! [0] +class StarRating +{ +public: + enum EditMode { Editable, ReadOnly }; + + StarRating(int starCount = 1, int maxStarCount = 5); + + void paint(QPainter *painter, const QRect &rect, + const QPalette &palette, EditMode mode) const; + QSize sizeHint() const; + int starCount() const { return myStarCount; } + int maxStarCount() const { return myMaxStarCount; } + void setStarCount(int starCount) { myStarCount = starCount; } + void setMaxStarCount(int maxStarCount) { myMaxStarCount = maxStarCount; } + +private: + QPolygonF starPolygon; + QPolygonF diamondPolygon; + int myStarCount; + int myMaxStarCount; +}; +//! [0] + +//! [1] +Q_DECLARE_METATYPE(StarRating) +//! [1] + +#endif |