diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/itemviews/addressbook | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/itemviews/addressbook')
-rw-r--r-- | examples/itemviews/addressbook/adddialog.cpp | 83 | ||||
-rw-r--r-- | examples/itemviews/addressbook/adddialog.h | 72 | ||||
-rw-r--r-- | examples/itemviews/addressbook/addressbook.pro | 17 | ||||
-rw-r--r-- | examples/itemviews/addressbook/addresswidget.cpp | 238 | ||||
-rw-r--r-- | examples/itemviews/addressbook/addresswidget.h | 83 | ||||
-rw-r--r-- | examples/itemviews/addressbook/main.cpp | 53 | ||||
-rw-r--r-- | examples/itemviews/addressbook/mainwindow.cpp | 138 | ||||
-rw-r--r-- | examples/itemviews/addressbook/mainwindow.h | 76 | ||||
-rw-r--r-- | examples/itemviews/addressbook/newaddresstab.cpp | 78 | ||||
-rw-r--r-- | examples/itemviews/addressbook/newaddresstab.h | 75 | ||||
-rw-r--r-- | examples/itemviews/addressbook/tablemodel.cpp | 185 | ||||
-rw-r--r-- | examples/itemviews/addressbook/tablemodel.h | 73 |
12 files changed, 1171 insertions, 0 deletions
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 |