diff options
author | Gabriel de Dietrich <gabriel.dietrich-de@nokia.com> | 2012-08-17 13:23:19 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-08-20 12:20:55 +0200 |
commit | 806dda08d685bc5f9ed71dfe8b61f21848d48066 (patch) | |
tree | a63533a1c4a335ae17adc105abb0ae4e62e2f26e /examples/widgets/itemviews/addressbook | |
parent | 9f942014e31842b512c3198de035d041c59f54a9 (diff) |
Moving .qdoc files under examples/widgets/doc
Updated those .qdoc files to refer to the new relative examples
emplacement. Images and snippets to be moved later.
Also grouped all widgets related examples under widgets.
Change-Id: Ib29696e2d8948524537f53e8dda88f9ee26a597f
Reviewed-by: J-P Nurmi <j-p.nurmi@nokia.com>
Diffstat (limited to 'examples/widgets/itemviews/addressbook')
13 files changed, 1172 insertions, 0 deletions
diff --git a/examples/widgets/itemviews/addressbook/adddialog.cpp b/examples/widgets/itemviews/addressbook/adddialog.cpp new file mode 100644 index 0000000000..f34bd0d46e --- /dev/null +++ b/examples/widgets/itemviews/addressbook/adddialog.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#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/widgets/itemviews/addressbook/adddialog.h b/examples/widgets/itemviews/addressbook/adddialog.h new file mode 100644 index 0000000000..6d9093b638 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/adddialog.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $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/widgets/itemviews/addressbook/addressbook.desktop b/examples/widgets/itemviews/addressbook/addressbook.desktop new file mode 100644 index 0000000000..11767f59d8 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/addressbook.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Address Book +Exec=/opt/usr/bin/addressbook +Icon=addressbook +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/itemviews/addressbook/addressbook.pro b/examples/widgets/itemviews/addressbook/addressbook.pro new file mode 100644 index 0000000000..32cbc4ca36 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/addressbook.pro @@ -0,0 +1,19 @@ +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]/qtbase/itemviews/addressbook +sources.files = $$SOURCES $$HEADERS $$RESOURCES addressbook.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/itemviews/addressbook +INSTALLS += target sources +QT += widgets + diff --git a/examples/widgets/itemviews/addressbook/addresswidget.cpp b/examples/widgets/itemviews/addressbook/addresswidget.cpp new file mode 100644 index 0000000000..066d5588c7 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/addresswidget.cpp @@ -0,0 +1,236 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#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 = -1; + + 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); + + 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(QItemSelection,QItemSelection)), + this, SIGNAL(selectionChanged(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/widgets/itemviews/addressbook/addresswidget.h b/examples/widgets/itemviews/addressbook/addresswidget.h new file mode 100644 index 0000000000..3a46818463 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/addresswidget.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $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/widgets/itemviews/addressbook/main.cpp b/examples/widgets/itemviews/addressbook/main.cpp new file mode 100644 index 0000000000..9f388dc123 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#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/widgets/itemviews/addressbook/mainwindow.cpp b/examples/widgets/itemviews/addressbook/mainwindow.cpp new file mode 100644 index 0000000000..e16aec94b0 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/mainwindow.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#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(QItemSelection)), + this, SLOT(updateActions(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/widgets/itemviews/addressbook/mainwindow.h b/examples/widgets/itemviews/addressbook/mainwindow.h new file mode 100644 index 0000000000..284e492c4a --- /dev/null +++ b/examples/widgets/itemviews/addressbook/mainwindow.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QtWidgets> +#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/widgets/itemviews/addressbook/newaddresstab.cpp b/examples/widgets/itemviews/addressbook/newaddresstab.cpp new file mode 100644 index 0000000000..f27789a511 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/newaddresstab.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#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/widgets/itemviews/addressbook/newaddresstab.h b/examples/widgets/itemviews/addressbook/newaddresstab.h new file mode 100644 index 0000000000..4562050f75 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/newaddresstab.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $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/widgets/itemviews/addressbook/tablemodel.cpp b/examples/widgets/itemviews/addressbook/tablemodel.cpp new file mode 100644 index 0000000000..6d99a02b55 --- /dev/null +++ b/examples/widgets/itemviews/addressbook/tablemodel.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $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/widgets/itemviews/addressbook/tablemodel.h b/examples/widgets/itemviews/addressbook/tablemodel.h new file mode 100644 index 0000000000..bb21af587f --- /dev/null +++ b/examples/widgets/itemviews/addressbook/tablemodel.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $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 |