diff options
Diffstat (limited to 'examples/widgets')
171 files changed, 10809 insertions, 0 deletions
diff --git a/examples/widgets/tutorials/README b/examples/widgets/tutorials/README new file mode 100644 index 0000000000..54c0b17706 --- /dev/null +++ b/examples/widgets/tutorials/README @@ -0,0 +1,6 @@ +Qt is supplied with tutorials that have been written to provide developers +with an introduction to the Qt API. + + +Documentation for tutorials can be found in the Tutorials +link in the main documentation. diff --git a/examples/widgets/tutorials/addressbook-fr/README b/examples/widgets/tutorials/addressbook-fr/README new file mode 100644 index 0000000000..d24cedf51e --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/README @@ -0,0 +1,40 @@ +The Address Book Tutorial shows how to put together a simple yet +fully-functioning GUI application. The tutorial chapters can be found in the +Qt documentation, which can be viewed using Qt Assistant or a Web browser. + +The tutorial is also available online at + +http://qt.nokia.com/doc/4.4/tutorial.html + +All programs corresponding to the chapters in the tutorial should +automatically be built when Qt is compiled, or will be provided as +pre-built executables if you have obtained a binary package of Qt. + +If you have only compiled the Qt libraries, use the following instructions +to build the tutorial. + +On Linux/Unix: + +Typing 'make' in this directory builds all the programs (part1/part1, +part2/part2, part3/part3 and so on). Typing 'make' in each subdirectory +builds just that tutorial program. + +On Windows: + +Create a single Visual Studio project for the tutorial directory in +the usual way. You can do this by typing the following at the command +line: + +qmake -tp vc + +You should now be able to open the project file in Visual Studio and +build all of the tutorial programs at the same time. + +On Mac OS X: + +Create an Xcode project with the .pro file in the tutorial directory. +You can do this by typing the following at the command line: + +qmake -spec macx-xcode + +Then open the generated Xcode project in Xcode and build it. diff --git a/examples/widgets/tutorials/addressbook-fr/addressbook-fr.pro b/examples/widgets/tutorials/addressbook-fr/addressbook-fr.pro new file mode 100644 index 0000000000..0ee8387437 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/addressbook-fr.pro @@ -0,0 +1,10 @@ +TEMPLATE = subdirs +SUBDIRS = part1 part2 part3 part4 part5 part6 part7 + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook-fr +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS addressbook-fr.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook-fr +INSTALLS += target sources +QT += widgets + diff --git a/examples/widgets/tutorials/addressbook-fr/part1/addressbook.cpp b/examples/widgets/tutorials/addressbook-fr/part1/addressbook.cpp new file mode 100644 index 0000000000..119baacb37 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part1/addressbook.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +//! [constructor and input fields] +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; +//! [constructor and input fields] + +//! [layout] + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); +//! [layout] + +//![setting the layout] + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} +//! [setting the layout] diff --git a/examples/widgets/tutorials/addressbook-fr/part1/addressbook.h b/examples/widgets/tutorials/addressbook-fr/part1/addressbook.h new file mode 100644 index 0000000000..0bcd93e3bb --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part1/addressbook.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + +//! [class definition] +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + +private: + QLineEdit *nameLine; + QTextEdit *addressText; +}; +//! [class definition] + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part1/main.cpp b/examples/widgets/tutorials/addressbook-fr/part1/main.cpp new file mode 100644 index 0000000000..09562a2d2b --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part1/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +//! [main function] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} +//! [main function] diff --git a/examples/widgets/tutorials/addressbook-fr/part1/part1.desktop b/examples/widgets/tutorials/addressbook-fr/part1/part1.desktop new file mode 100644 index 0000000000..0cf4115f33 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part1/part1.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=1 Address Book FR +Exec=/opt/usr/bin/part1 +Icon=part1 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook-fr/part1/part1.pro b/examples/widgets/tutorials/addressbook-fr/part1/part1.pro new file mode 100644 index 0000000000..f04c8ff10d --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part1/part1.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part1 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part1.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part1 +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook-fr/part2/addressbook.cpp b/examples/widgets/tutorials/addressbook-fr/part2/addressbook.cpp new file mode 100644 index 0000000000..503cf90ada --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part2/addressbook.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; +//! [setting readonly 1] + nameLine->setReadOnly(true); +//! [setting readonly 1] + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; +//! [setting readonly 2] + addressText->setReadOnly(true); +//! [setting readonly 2] + +//! [pushbutton declaration] + addButton = new QPushButton(tr("&Add")); + addButton->show(); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); +//! [pushbutton declaration] +//! [connecting signals and slots] + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); +//! [connecting signals and slots] +//! [vertical layout] + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton, Qt::AlignTop); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); +//! [vertical layout] +//! [grid layout] + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); +//! [grid layout] + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} +//! [addContact] +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + submitButton->show(); + cancelButton->show(); +} +//! [addContact] + +//! [submitContact part1] +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if ( name.isEmpty()|| address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } +//! [submitContact part1] +//! [submitContact part2] + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + return; + } +//! [submitContact part2] +//! [submitContact part3] + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + submitButton->hide(); + cancelButton->hide(); +} +//! [submitContact part3] +//! [cancel] +void AddressBook::cancel() +{ + nameLine->setText(oldName); + nameLine->setReadOnly(true); + + addressText->setText(oldAddress); + addressText->setReadOnly(true); + + addButton->setEnabled(true); + submitButton->hide(); + cancelButton->hide(); +} +//! [cancel] diff --git a/examples/widgets/tutorials/addressbook-fr/part2/addressbook.h b/examples/widgets/tutorials/addressbook-fr/part2/addressbook.h new file mode 100644 index 0000000000..c2cc341d60 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part2/addressbook.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> + +QT_BEGIN_NAMESPACE +class QLabel; +class QLineEdit; +class QPushButton; +class QTextEdit; +QT_END_NAMESPACE + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + +//! [slots] +public slots: + void addContact(); + void submitContact(); + void cancel(); +//! [slots] + +//! [pushbutton declaration] +private: + QPushButton *addButton; + QPushButton *submitButton; + QPushButton *cancelButton; + QLineEdit *nameLine; + QTextEdit *addressText; +//! [pushbutton declaration] + +//! [remaining private variables] + QMap<QString, QString> contacts; + QString oldName; + QString oldAddress; +}; +//! [remaining private variables] + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part2/main.cpp b/examples/widgets/tutorials/addressbook-fr/part2/main.cpp new file mode 100644 index 0000000000..09562a2d2b --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part2/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +//! [main function] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} +//! [main function] diff --git a/examples/widgets/tutorials/addressbook-fr/part2/part2.desktop b/examples/widgets/tutorials/addressbook-fr/part2/part2.desktop new file mode 100644 index 0000000000..681c6a1e4d --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part2/part2.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=2 Address Book FR +Exec=/opt/usr/bin/part2 +Icon=part2 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook-fr/part2/part2.pro b/examples/widgets/tutorials/addressbook-fr/part2/part2.pro new file mode 100644 index 0000000000..46f811676a --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part2/part2.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part2 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part2.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part2 +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook-fr/part3/addressbook.cpp b/examples/widgets/tutorials/addressbook-fr/part3/addressbook.cpp new file mode 100644 index 0000000000..d83d31b08f --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part3/addressbook.cpp @@ -0,0 +1,215 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + addButton->show(); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); +//! [navigation pushbuttons] + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); +//! [navigation pushbuttons] + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); +//! [connecting navigation signals] + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); +//! [connecting navigation signals] + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton, Qt::AlignTop); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); +//! [navigation layout] + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); +//! [ navigation layout] + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); +//! [adding navigation layout] + mainLayout->addLayout(buttonLayout2, 3, 1); +//! [adding navigation layout] + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); +//! [disabling navigation] + nextButton->setEnabled(false); + previousButton->setEnabled(false); +//! [disabling navigation] + submitButton->show(); + cancelButton->show(); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + +//! [enabling navigation] + int number = contacts.size(); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); +//! [enabling navigation] + submitButton->hide(); + cancelButton->hide(); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); +} + +//! [next() function] +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [next() function] +//! [previous() function] +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()){ + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [previous() function] diff --git a/examples/widgets/tutorials/addressbook-fr/part3/addressbook.h b/examples/widgets/tutorials/addressbook-fr/part3/addressbook.h new file mode 100644 index 0000000000..5c6f398ba0 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part3/addressbook.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> + +QT_BEGIN_NAMESPACE +class QLabel; +class QLineEdit; +class QPushButton; +class QTextEdit; +QT_END_NAMESPACE + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + +public slots: + void addContact(); + void submitContact(); + void cancel(); +//! [navigation functions] + void next(); + void previous(); +//! [navigation functions] + +private: + QPushButton *addButton; + QPushButton *submitButton; + QPushButton *cancelButton; +//! [navigation pushbuttons] + QPushButton *nextButton; + QPushButton *previousButton; +//! [navigation pushbuttons] + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + QString oldName; + QString oldAddress; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part3/main.cpp b/examples/widgets/tutorials/addressbook-fr/part3/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part3/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook-fr/part3/part3.desktop b/examples/widgets/tutorials/addressbook-fr/part3/part3.desktop new file mode 100644 index 0000000000..3c97d51f4e --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part3/part3.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=3 Address Book FR +Exec=/opt/usr/bin/part3 +Icon=part3 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook-fr/part3/part3.pro b/examples/widgets/tutorials/addressbook-fr/part3/part3.pro new file mode 100644 index 0000000000..d76baaae5d --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part3/part3.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part3 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part3.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part3 +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook-fr/part4/addressbook.cpp b/examples/widgets/tutorials/addressbook-fr/part4/addressbook.cpp new file mode 100644 index 0000000000..45999d4710 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part4/addressbook.cpp @@ -0,0 +1,288 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); +//! [edit and remove buttons] + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); +//! [edit and remove buttons] + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); +//! [connecting edit and remove] + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); +//! [connecting edit and remove] + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); +//! [adding edit and remove to the layout] + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); +//! [adding edit and remove to the layout] + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 3, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} +//! [editContact() function] +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} +//! [editContact() function] +//! [submitContact() function beginning] +void AddressBook::submitContact() +{ +//! [submitContact() function beginning] + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } +//! [submitContact() function part1] + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } +//! [submitContact() function part1] +//! [submitContact() function part2] + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} +//! [submitContact() function part2] + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} +//! [removeContact() function] +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} +//! [removeContact() function] +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [update interface() part 1] +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + break; +//! [update interface() part 1] +//! [update interface() part 2] + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number >1 ); + + submitButton->hide(); + cancelButton->hide(); + break; + } +} +//! [update interface() part 2] diff --git a/examples/widgets/tutorials/addressbook-fr/part4/addressbook.h b/examples/widgets/tutorials/addressbook-fr/part4/addressbook.h new file mode 100644 index 0000000000..ddf03c217e --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part4/addressbook.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> + +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); +//! [Mode enum] + enum Mode { NavigationMode, AddingMode, EditingMode }; +//! [Mode enum] + +public slots: + void addContact(); + void submitContact(); + void cancel(); +//! [edit and remove slots] + void editContact(); + void removeContact(); +//! [edit and remove slots] + void next(); + void previous(); + +private: +//! [updateInterface() declaration] + void updateInterface(Mode mode); +//! [updateInterface() declaration] + QPushButton *addButton; +//! [buttons declaration] + QPushButton *editButton; + QPushButton *removeButton; +//! [buttons declaration] + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + QString oldName; + QString oldAddress; +//! [mode declaration] + Mode currentMode; +//! [mode declaration] +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part4/main.cpp b/examples/widgets/tutorials/addressbook-fr/part4/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part4/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook-fr/part4/part4.desktop b/examples/widgets/tutorials/addressbook-fr/part4/part4.desktop new file mode 100644 index 0000000000..7726989689 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part4/part4.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=4 Address Book FR +Exec=/opt/usr/bin/part4 +Icon=part4 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook-fr/part4/part4.pro b/examples/widgets/tutorials/addressbook-fr/part4/part4.pro new file mode 100644 index 0000000000..1684d5e5f4 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part4/part4.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part4 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part4.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part4 +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook-fr/part5/addressbook.cpp b/examples/widgets/tutorials/addressbook-fr/part5/addressbook.cpp new file mode 100644 index 0000000000..63aac8495e --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part5/addressbook.cpp @@ -0,0 +1,312 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); +//! [instantiating findButton] + findButton = new QPushButton(tr("&Find")); + findButton->setEnabled(false); +//! [instantiating findButton] + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + +//! [instantiating FindDialog] + dialog = new FindDialog; +//! [instantiating FindDialog] + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); +//! [signals and slots for find] + connect(findButton, SIGNAL(clicked()), this, SLOT(findContact())); +//! [signals and slots for find] + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); +//! [adding findButton to layout] + buttonLayout1->addWidget(findButton); +//! [adding findButton to layout] + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 2, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} + +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} + +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [findContact() function] +void AddressBook::findContact() +{ + dialog->show(); + + if (dialog->exec() == QDialog::Accepted) { + QString contactName = dialog->getFindText(); + + if (contacts.contains(contactName)) { + nameLine->setText(contactName); + addressText->setText(contacts.value(contactName)); + } else { + QMessageBox::information(this, tr("Contact Not Found"), + tr("Sorry, \"%1\" is not in your address book.").arg(contactName)); + return; + } + } + + updateInterface(NavigationMode); +} +//! [findContact() function] + +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + break; + + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + findButton->setEnabled(number > 2); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); + break; + } +} diff --git a/examples/widgets/tutorials/addressbook-fr/part5/addressbook.h b/examples/widgets/tutorials/addressbook-fr/part5/addressbook.h new file mode 100644 index 0000000000..5d58952037 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part5/addressbook.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> +//! [include finddialog's header] +#include "finddialog.h" +//! [include finddialog's header] +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + enum Mode { NavigationMode, AddingMode, EditingMode }; + +public slots: + void addContact(); + void editContact(); + void submitContact(); + void cancel(); + void removeContact(); +//! [findContact() declaration] + void findContact(); +//! [findContact() declaration] + void next(); + void previous(); + +private: + void updateInterface(Mode mode); + + QPushButton *addButton; + QPushButton *editButton; + QPushButton *removeButton; +//! [findButton declaration] + QPushButton *findButton; +//! [findButton declaration] + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; +//! [FindDialog declaration] + FindDialog *dialog; +//! [FindDialog declaration] + QString oldName; + QString oldAddress; + Mode currentMode; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part5/finddialog.cpp b/examples/widgets/tutorials/addressbook-fr/part5/finddialog.cpp new file mode 100644 index 0000000000..fdeab4c4bb --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part5/finddialog.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** 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 "finddialog.h" + +//! [constructor] +FindDialog::FindDialog(QWidget *parent) + : QDialog(parent) +{ + QLabel *findLabel = new QLabel(tr("Enter the name of a contact:")); + lineEdit = new QLineEdit; + + findButton = new QPushButton(tr("&Find")); + findText = ""; + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(findLabel); + layout->addWidget(lineEdit); + layout->addWidget(findButton); + + setLayout(layout); + setWindowTitle(tr("Find a Contact")); + connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); + connect(findButton, SIGNAL(clicked()), this, SLOT(accept())); +} +//! [constructor] +//! [findClicked() function] +void FindDialog::findClicked() +{ + QString text = lineEdit->text(); + + if (text.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name.")); + return; + } else { + findText = text; + lineEdit->clear(); + hide(); + } +} +//! [findClicked() function] +//! [getFindText() function] +QString FindDialog::getFindText() +{ + return findText; +} +//! [getFindText() function] diff --git a/examples/widgets/tutorials/addressbook-fr/part5/finddialog.h b/examples/widgets/tutorials/addressbook-fr/part5/finddialog.h new file mode 100644 index 0000000000..2ebaef1d86 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part5/finddialog.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** 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 FINDDIALOG_H +#define FINDDIALOG_H +//! [FindDialog header] +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QLineEdit; +class QPushButton; +QT_END_NAMESPACE + +class FindDialog : public QDialog +{ + Q_OBJECT + +public: + FindDialog(QWidget *parent = 0); + QString getFindText(); + +public slots: + void findClicked(); + +private: + QPushButton *findButton; + QLineEdit *lineEdit; + QString findText; +}; +//! [FindDialog header] +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part5/main.cpp b/examples/widgets/tutorials/addressbook-fr/part5/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part5/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook-fr/part5/part5.desktop b/examples/widgets/tutorials/addressbook-fr/part5/part5.desktop new file mode 100644 index 0000000000..0efcb1550c --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part5/part5.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=5 Address Book FR +Exec=/opt/usr/bin/part5 +Icon=part5 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook-fr/part5/part5.pro b/examples/widgets/tutorials/addressbook-fr/part5/part5.pro new file mode 100644 index 0000000000..1978068cc7 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part5/part5.pro @@ -0,0 +1,15 @@ +SOURCES = addressbook.cpp \ + finddialog.cpp \ + main.cpp +HEADERS = addressbook.h \ + finddialog.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part5 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part5.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part5 +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook-fr/part6/addressbook.cpp b/examples/widgets/tutorials/addressbook-fr/part6/addressbook.cpp new file mode 100644 index 0000000000..ff2756d4bb --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part6/addressbook.cpp @@ -0,0 +1,393 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); + findButton = new QPushButton(tr("&Find")); + findButton->setEnabled(false); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + + loadButton = new QPushButton(tr("&Load...")); +//! [tooltip 1] + loadButton->setToolTip(tr("Load contacts from a file")); +//! [tooltip 1] + saveButton = new QPushButton(tr("Sa&ve...")); +//! [tooltip 2] + saveButton->setToolTip(tr("Save contacts to a file")); +//! [tooltip 2] + saveButton->setEnabled(false); + + dialog = new FindDialog; + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); + connect(findButton, SIGNAL(clicked()), this, SLOT(findContact())); + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFromFile())); + connect(saveButton, SIGNAL(clicked()), this, SLOT(saveToFile())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); + buttonLayout1->addWidget(findButton); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addWidget(loadButton); + buttonLayout1->addWidget(saveButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 2, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} + +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} + +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::findContact() +{ + dialog->show(); + + if (dialog->exec() == 1) { + QString contactName = dialog->getFindText(); + + if (contacts.contains(contactName)) { + nameLine->setText(contactName); + addressText->setText(contacts.value(contactName)); + } else { + QMessageBox::information(this, tr("Contact Not Found"), + tr("Sorry, \"%1\" is not in your address book.").arg(contactName)); + return; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + + loadButton->setEnabled(false); + saveButton->setEnabled(false); + break; + + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + findButton->setEnabled(number > 2); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); + + loadButton->setEnabled(true); + saveButton->setEnabled(number >= 1); + break; + } +} + +//! [saveToFile() function part1] +void AddressBook::saveToFile() +{ + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); + +//! [saveToFile() function part1] +//! [saveToFile() function part2] + if (fileName.isEmpty()) + return; + else { + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + +//! [saveToFile() function part2] +//! [saveToFile() function part3] + QDataStream out(&file); + out.setVersion(QDataStream::Qt_4_5); + out << contacts; + } +} +//! [saveToFile() function part3] + +//! [loadFromFile() function part1] +void AddressBook::loadFromFile() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); +//! [loadFromFile() function part1] + +//! [loadFromFile() function part2] + if (fileName.isEmpty()) + return; + else { + + QFile file(fileName); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QDataStream in(&file); + in.setVersion(QDataStream::Qt_4_5); + contacts.empty(); // empty existing contacts + in >> contacts; +//! [loadFromFile() function part2] + +//! [loadFromFile() function part3] + if (contacts.isEmpty()) { + QMessageBox::information(this, tr("No contacts in file"), + tr("The file you are attempting to open contains no contacts.")); + } else { + QMap<QString, QString>::iterator i = contacts.begin(); + nameLine->setText(i.key()); + addressText->setText(i.value()); + } + } + + updateInterface(NavigationMode); +} +//! [loadFromFile() function part3] diff --git a/examples/widgets/tutorials/addressbook-fr/part6/addressbook.h b/examples/widgets/tutorials/addressbook-fr/part6/addressbook.h new file mode 100644 index 0000000000..59a8b9951d --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part6/addressbook.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> +#include "finddialog.h" + +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + enum Mode { NavigationMode, AddingMode, EditingMode }; + +public slots: + void addContact(); + void editContact(); + void submitContact(); + void cancel(); + void removeContact(); + void findContact(); + void next(); + void previous(); +//! [save and load functions declaration] + void saveToFile(); + void loadFromFile(); +//! [save and load functions declaration] + +private: + void updateInterface(Mode mode); + + QPushButton *addButton; + QPushButton *editButton; + QPushButton *removeButton; + QPushButton *findButton; + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; +//! [save and load buttons declaration] + QPushButton *loadButton; + QPushButton *saveButton; +//! [save and load buttons declaration] + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + FindDialog *dialog; + QString oldName; + QString oldAddress; + Mode currentMode; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part6/finddialog.cpp b/examples/widgets/tutorials/addressbook-fr/part6/finddialog.cpp new file mode 100644 index 0000000000..686af08c71 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part6/finddialog.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 "finddialog.h" + +FindDialog::FindDialog(QWidget *parent) + : QDialog(parent) +{ + QLabel *findLabel = new QLabel(tr("Enter the name of a contact:")); + lineEdit = new QLineEdit; + + findButton = new QPushButton(tr("&Find")); + findText = ""; + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(findLabel); + layout->addWidget(lineEdit); + layout->addWidget(findButton); + + setLayout(layout); + setWindowTitle(tr("Find a Contact")); + connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); + connect(findButton, SIGNAL(clicked()), this, SLOT(accept())); +} + +void FindDialog::findClicked() +{ + QString text = lineEdit->text(); + + if (text.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name.")); + return; + } else { + findText = text; + lineEdit->clear(); + hide(); + } +} + +QString FindDialog::getFindText() +{ + return findText; +} diff --git a/examples/widgets/tutorials/addressbook-fr/part6/finddialog.h b/examples/widgets/tutorials/addressbook-fr/part6/finddialog.h new file mode 100644 index 0000000000..86a33bbb72 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part6/finddialog.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** 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 FINDDIALOG_H +#define FINDDIALOG_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QLineEdit; +class QPushButton; +QT_END_NAMESPACE + +class FindDialog : public QDialog +{ + Q_OBJECT + +public: + FindDialog(QWidget *parent = 0); + QString getFindText(); + +public slots: + void findClicked(); + +private: + QPushButton *findButton; + QLineEdit *lineEdit; + QString findText; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part6/main.cpp b/examples/widgets/tutorials/addressbook-fr/part6/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part6/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook-fr/part6/part6.desktop b/examples/widgets/tutorials/addressbook-fr/part6/part6.desktop new file mode 100644 index 0000000000..144025924a --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part6/part6.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=6 Address Book FR +Exec=/opt/usr/bin/part6 +Icon=part6 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook-fr/part6/part6.pro b/examples/widgets/tutorials/addressbook-fr/part6/part6.pro new file mode 100644 index 0000000000..b5ec9569a8 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part6/part6.pro @@ -0,0 +1,15 @@ +SOURCES = addressbook.cpp \ + finddialog.cpp \ + main.cpp +HEADERS = addressbook.h \ + finddialog.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part6 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part6.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part6 +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook-fr/part7/addressbook.cpp b/examples/widgets/tutorials/addressbook-fr/part7/addressbook.cpp new file mode 100644 index 0000000000..a958108885 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part7/addressbook.cpp @@ -0,0 +1,446 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); + findButton = new QPushButton(tr("&Find")); + findButton->setEnabled(false); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + + loadButton = new QPushButton(tr("&Load...")); + loadButton->setToolTip(tr("Load contacts from a file")); + saveButton = new QPushButton(tr("Sa&ve...")); + saveButton->setToolTip(tr("Save contacts to a file")); + saveButton->setEnabled(false); + + exportButton = new QPushButton(tr("E&xport")); + exportButton->setToolTip(tr("Export as vCard")); + exportButton->setEnabled(false); + + dialog = new FindDialog; + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); + connect(findButton, SIGNAL(clicked()), this, SLOT(findContact())); + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFromFile())); + connect(saveButton, SIGNAL(clicked()), this, SLOT(saveToFile())); + connect(exportButton, SIGNAL(clicked()), this, SLOT(exportAsVCard())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); + buttonLayout1->addWidget(findButton); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addWidget(loadButton); + buttonLayout1->addWidget(saveButton); + buttonLayout1->addWidget(exportButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 2, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} + +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} + +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::findContact() +{ + dialog->show(); + + if (dialog->exec() == 1) { + QString contactName = dialog->getFindText(); + + if (contacts.contains(contactName)) { + nameLine->setText(contactName); + addressText->setText(contacts.value(contactName)); + } else { + QMessageBox::information(this, tr("Contact Not Found"), + tr("Sorry, \"%1\" is not in your address book.").arg(contactName)); + return; + } + } + + updateInterface(NavigationMode); +} +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + + loadButton->setEnabled(false); + saveButton->setEnabled(false); + exportButton->setEnabled(false); + break; + + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + findButton->setEnabled(number > 2); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); + + exportButton->setEnabled(number >= 1); + + loadButton->setEnabled(true); + saveButton->setEnabled(number >= 1); + break; + } +} + +void AddressBook::saveToFile() +{ + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); + + if (fileName.isEmpty()) + return; + else { + QFile file(fileName); + + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QDataStream out(&file); + out.setVersion(QDataStream::Qt_4_3); + out << contacts; + } + + updateInterface(NavigationMode); +} + +void AddressBook::loadFromFile() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); + + if (fileName.isEmpty()) + return; + else { + QFile file(fileName); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QDataStream in(&file); + in.setVersion(QDataStream::Qt_4_3); + contacts.empty(); // empty existing contacts + in >> contacts; + + QMap<QString, QString>::iterator i = contacts.begin(); + nameLine->setText(i.key()); + addressText->setText(i.value()); + } + + updateInterface(NavigationMode); +} + +//! [export function part1] +void AddressBook::exportAsVCard() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + QString firstName; + QString lastName; + QStringList nameList; + + int index = name.indexOf(" "); + + if (index != -1) { + nameList = name.split(QRegExp("\\s+"), QString::SkipEmptyParts); + firstName = nameList.first(); + lastName = nameList.last(); + } else { + firstName = name; + lastName = ""; + } + + QString fileName = QFileDialog::getSaveFileName(this, + tr("Export Contact"), "", + tr("vCard Files (*.vcf);;All Files (*)")); + + if (fileName.isEmpty()) + return; + + QFile file(fileName); +//! [export function part1] + +//! [export function part2] + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QTextStream out(&file); +//! [export function part2] + +//! [export function part3] + out << "BEGIN:VCARD" << "\n"; + out << "VERSION:2.1" << "\n"; + out << "N:" << lastName << ";" << firstName << "\n"; + + if (!nameList.isEmpty()) + out << "FN:" << nameList.join(" ") << "\n"; + else + out << "FN:" << firstName << "\n"; +//! [export function part3] + +//! [export function part4] + address.replace(";", "\\;", Qt::CaseInsensitive); + address.replace("\n", ";", Qt::CaseInsensitive); + address.replace(",", " ", Qt::CaseInsensitive); + + out << "ADR;HOME:;" << address << "\n"; + out << "END:VCARD" << "\n"; + + QMessageBox::information(this, tr("Export Successful"), + tr("\"%1\" has been exported as a vCard.").arg(name)); +} +//! [export function part4] diff --git a/examples/widgets/tutorials/addressbook-fr/part7/addressbook.h b/examples/widgets/tutorials/addressbook-fr/part7/addressbook.h new file mode 100644 index 0000000000..eb18cdc955 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part7/addressbook.h @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> +#include "finddialog.h" + +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + enum Mode { NavigationMode, AddingMode, EditingMode }; + +public slots: + void addContact(); + void editContact(); + void submitContact(); + void cancel(); + void removeContact(); + void findContact(); + void next(); + void previous(); + void saveToFile(); + void loadFromFile(); +//! [exportAsVCard() declaration] + void exportAsVCard(); +//! [exportAsVCard() declaration] + +private: + void updateInterface(Mode mode); + + QPushButton *addButton; + QPushButton *editButton; + QPushButton *removeButton; + QPushButton *findButton; + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; + QPushButton *loadButton; + QPushButton *saveButton; +//! [exportButton declaration] + QPushButton *exportButton; +//! [exportButton declaration] + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + FindDialog *dialog; + QString oldName; + QString oldAddress; + Mode currentMode; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part7/finddialog.cpp b/examples/widgets/tutorials/addressbook-fr/part7/finddialog.cpp new file mode 100644 index 0000000000..686af08c71 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part7/finddialog.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 "finddialog.h" + +FindDialog::FindDialog(QWidget *parent) + : QDialog(parent) +{ + QLabel *findLabel = new QLabel(tr("Enter the name of a contact:")); + lineEdit = new QLineEdit; + + findButton = new QPushButton(tr("&Find")); + findText = ""; + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(findLabel); + layout->addWidget(lineEdit); + layout->addWidget(findButton); + + setLayout(layout); + setWindowTitle(tr("Find a Contact")); + connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); + connect(findButton, SIGNAL(clicked()), this, SLOT(accept())); +} + +void FindDialog::findClicked() +{ + QString text = lineEdit->text(); + + if (text.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name.")); + return; + } else { + findText = text; + lineEdit->clear(); + hide(); + } +} + +QString FindDialog::getFindText() +{ + return findText; +} diff --git a/examples/widgets/tutorials/addressbook-fr/part7/finddialog.h b/examples/widgets/tutorials/addressbook-fr/part7/finddialog.h new file mode 100644 index 0000000000..86a33bbb72 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part7/finddialog.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** 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 FINDDIALOG_H +#define FINDDIALOG_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QLineEdit; +class QPushButton; +QT_END_NAMESPACE + +class FindDialog : public QDialog +{ + Q_OBJECT + +public: + FindDialog(QWidget *parent = 0); + QString getFindText(); + +public slots: + void findClicked(); + +private: + QPushButton *findButton; + QLineEdit *lineEdit; + QString findText; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook-fr/part7/main.cpp b/examples/widgets/tutorials/addressbook-fr/part7/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part7/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook-fr/part7/part7.desktop b/examples/widgets/tutorials/addressbook-fr/part7/part7.desktop new file mode 100644 index 0000000000..f78ff4b221 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part7/part7.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=7 Address Book FR +Exec=/opt/usr/bin/part7 +Icon=part7 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook-fr/part7/part7.pro b/examples/widgets/tutorials/addressbook-fr/part7/part7.pro new file mode 100644 index 0000000000..0b8ba50969 --- /dev/null +++ b/examples/widgets/tutorials/addressbook-fr/part7/part7.pro @@ -0,0 +1,15 @@ +SOURCES = addressbook.cpp \ + finddialog.cpp \ + main.cpp +HEADERS = addressbook.h \ + finddialog.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part7 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part7.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part7 +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook/README b/examples/widgets/tutorials/addressbook/README new file mode 100644 index 0000000000..39753b4b25 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/README @@ -0,0 +1,40 @@ +The Address Book Tutorial shows how to put together a simple yet +fully-functioning GUI application. The tutorial chapters can be found in the +Qt documentation, which can be viewed using Qt Assistant or a Web browser. + +The tutorial is also available online at + +http://qt.nokia.com/doc/tutorial.html + +All programs corresponding to the chapters in the tutorial should +automatically be built when Qt is compiled, or will be provided as +pre-built executables if you have obtained a binary package of Qt. + +If you have only compiled the Qt libraries, use the following instructions +to build the tutorial. + +On Linux/Unix: + +Typing 'make' in this directory builds all the programs (part1/part1, +part2/part2, part3/part3 and so on). Typing 'make' in each subdirectory +builds just that tutorial program. + +On Windows: + +Create a single Visual Studio project for the tutorial directory in +the usual way. You can do this by typing the following at the command +line: + +qmake -tp vc + +You should now be able to open the project file in Visual Studio and +build all of the tutorial programs at the same time. + +On Mac OS X: + +Create an Xcode project with the .pro file in the tutorial directory. +You can do this by typing the following at the command line: + +qmake -spec macx-xcode + +Then open the generated Xcode project in Xcode and build it. diff --git a/examples/widgets/tutorials/addressbook/addressbook.pro b/examples/widgets/tutorials/addressbook/addressbook.pro new file mode 100644 index 0000000000..51438698da --- /dev/null +++ b/examples/widgets/tutorials/addressbook/addressbook.pro @@ -0,0 +1,10 @@ +TEMPLATE = subdirs +SUBDIRS = part1 part2 part3 part4 part5 part6 part7 + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS addressbook.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tutorials/addressbook/part1/addressbook.cpp b/examples/widgets/tutorials/addressbook/part1/addressbook.cpp new file mode 100644 index 0000000000..119baacb37 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part1/addressbook.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +//! [constructor and input fields] +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; +//! [constructor and input fields] + +//! [layout] + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); +//! [layout] + +//![setting the layout] + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} +//! [setting the layout] diff --git a/examples/widgets/tutorials/addressbook/part1/addressbook.h b/examples/widgets/tutorials/addressbook/part1/addressbook.h new file mode 100644 index 0000000000..0bcd93e3bb --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part1/addressbook.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + +//! [class definition] +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + +private: + QLineEdit *nameLine; + QTextEdit *addressText; +}; +//! [class definition] + +#endif diff --git a/examples/widgets/tutorials/addressbook/part1/main.cpp b/examples/widgets/tutorials/addressbook/part1/main.cpp new file mode 100644 index 0000000000..09562a2d2b --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part1/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +//! [main function] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} +//! [main function] diff --git a/examples/widgets/tutorials/addressbook/part1/part1.desktop b/examples/widgets/tutorials/addressbook/part1/part1.desktop new file mode 100644 index 0000000000..69946edf46 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part1/part1.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=1 Address Book +Exec=/opt/usr/bin/part1 +Icon=part1 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook/part1/part1.pro b/examples/widgets/tutorials/addressbook/part1/part1.pro new file mode 100644 index 0000000000..67f9f3db20 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part1/part1.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part1 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part1.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part1 +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook/part2/addressbook.cpp b/examples/widgets/tutorials/addressbook/part2/addressbook.cpp new file mode 100644 index 0000000000..1bd97d4b5b --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part2/addressbook.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; +//! [setting readonly 1] + nameLine->setReadOnly(true); +//! [setting readonly 1] + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; +//! [setting readonly 2] + addressText->setReadOnly(true); +//! [setting readonly 2] + +//! [pushbutton declaration] + addButton = new QPushButton(tr("&Add")); + addButton->show(); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); +//! [pushbutton declaration] +//! [connecting signals and slots] + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); +//! [connecting signals and slots] +//! [vertical layout] + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton, Qt::AlignTop); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); +//! [vertical layout] +//! [grid layout] + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); +//! [grid layout] + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} +//! [addContact] +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + submitButton->show(); + cancelButton->show(); +} +//! [addContact] + +//! [submitContact part1] +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } +//! [submitContact part1] +//! [submitContact part2] + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + return; + } +//! [submitContact part2] +//! [submitContact part3] + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + submitButton->hide(); + cancelButton->hide(); +} +//! [submitContact part3] +//! [cancel] +void AddressBook::cancel() +{ + nameLine->setText(oldName); + nameLine->setReadOnly(true); + + addressText->setText(oldAddress); + addressText->setReadOnly(true); + + addButton->setEnabled(true); + submitButton->hide(); + cancelButton->hide(); +} +//! [cancel] diff --git a/examples/widgets/tutorials/addressbook/part2/addressbook.h b/examples/widgets/tutorials/addressbook/part2/addressbook.h new file mode 100644 index 0000000000..c2cc341d60 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part2/addressbook.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> + +QT_BEGIN_NAMESPACE +class QLabel; +class QLineEdit; +class QPushButton; +class QTextEdit; +QT_END_NAMESPACE + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + +//! [slots] +public slots: + void addContact(); + void submitContact(); + void cancel(); +//! [slots] + +//! [pushbutton declaration] +private: + QPushButton *addButton; + QPushButton *submitButton; + QPushButton *cancelButton; + QLineEdit *nameLine; + QTextEdit *addressText; +//! [pushbutton declaration] + +//! [remaining private variables] + QMap<QString, QString> contacts; + QString oldName; + QString oldAddress; +}; +//! [remaining private variables] + +#endif diff --git a/examples/widgets/tutorials/addressbook/part2/main.cpp b/examples/widgets/tutorials/addressbook/part2/main.cpp new file mode 100644 index 0000000000..09562a2d2b --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part2/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +//! [main function] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} +//! [main function] diff --git a/examples/widgets/tutorials/addressbook/part2/part2.desktop b/examples/widgets/tutorials/addressbook/part2/part2.desktop new file mode 100644 index 0000000000..5c87ef807b --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part2/part2.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=2 Address Book +Exec=/opt/usr/bin/part2 +Icon=part2 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook/part2/part2.pro b/examples/widgets/tutorials/addressbook/part2/part2.pro new file mode 100644 index 0000000000..26e2e270fa --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part2/part2.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part2 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part2.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part2 +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook/part3/addressbook.cpp b/examples/widgets/tutorials/addressbook/part3/addressbook.cpp new file mode 100644 index 0000000000..9a4c845dee --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part3/addressbook.cpp @@ -0,0 +1,215 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + addButton->show(); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); +//! [navigation pushbuttons] + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); +//! [navigation pushbuttons] + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); +//! [connecting navigation signals] + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); +//! [connecting navigation signals] + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton, Qt::AlignTop); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); +//! [navigation layout] + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); +//! [ navigation layout] + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); +//! [adding navigation layout] + mainLayout->addLayout(buttonLayout2, 2, 1); +//! [adding navigation layout] + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); +//! [disabling navigation] + nextButton->setEnabled(false); + previousButton->setEnabled(false); +//! [disabling navigation] + submitButton->show(); + cancelButton->show(); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + +//! [enabling navigation] + int number = contacts.size(); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); +//! [enabling navigation] + submitButton->hide(); + cancelButton->hide(); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); +} + +//! [next() function] +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [next() function] +//! [previous() function] +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()){ + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [previous() function] diff --git a/examples/widgets/tutorials/addressbook/part3/addressbook.h b/examples/widgets/tutorials/addressbook/part3/addressbook.h new file mode 100644 index 0000000000..5c6f398ba0 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part3/addressbook.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> + +QT_BEGIN_NAMESPACE +class QLabel; +class QLineEdit; +class QPushButton; +class QTextEdit; +QT_END_NAMESPACE + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + +public slots: + void addContact(); + void submitContact(); + void cancel(); +//! [navigation functions] + void next(); + void previous(); +//! [navigation functions] + +private: + QPushButton *addButton; + QPushButton *submitButton; + QPushButton *cancelButton; +//! [navigation pushbuttons] + QPushButton *nextButton; + QPushButton *previousButton; +//! [navigation pushbuttons] + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + QString oldName; + QString oldAddress; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook/part3/main.cpp b/examples/widgets/tutorials/addressbook/part3/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part3/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook/part3/part3.desktop b/examples/widgets/tutorials/addressbook/part3/part3.desktop new file mode 100644 index 0000000000..882a242b44 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part3/part3.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=3 Address Book +Exec=/opt/usr/bin/part3 +Icon=part3 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook/part3/part3.pro b/examples/widgets/tutorials/addressbook/part3/part3.pro new file mode 100644 index 0000000000..5a5d78ed73 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part3/part3.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part3 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part3.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part3 +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook/part4/addressbook.cpp b/examples/widgets/tutorials/addressbook/part4/addressbook.cpp new file mode 100644 index 0000000000..bb0c2543b1 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part4/addressbook.cpp @@ -0,0 +1,288 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); +//! [edit and remove buttons] + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); +//! [edit and remove buttons] + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); +//! [connecting edit and remove] + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); +//! [connecting edit and remove] + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); +//! [adding edit and remove to the layout] + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); +//! [adding edit and remove to the layout] + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 2, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} +//! [editContact() function] +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} +//! [editContact() function] +//! [submitContact() function beginning] +void AddressBook::submitContact() +{ +//! [submitContact() function beginning] + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } +//! [submitContact() function part1] + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } +//! [submitContact() function part1] +//! [submitContact() function part2] + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} +//! [submitContact() function part2] + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} +//! [removeContact() function] +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} +//! [removeContact() function] +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [update interface() part 1] +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + break; +//! [update interface() part 1] +//! [update interface() part 2] + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number >1 ); + + submitButton->hide(); + cancelButton->hide(); + break; + } +} +//! [update interface() part 2] diff --git a/examples/widgets/tutorials/addressbook/part4/addressbook.h b/examples/widgets/tutorials/addressbook/part4/addressbook.h new file mode 100644 index 0000000000..ddf03c217e --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part4/addressbook.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> + +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); +//! [Mode enum] + enum Mode { NavigationMode, AddingMode, EditingMode }; +//! [Mode enum] + +public slots: + void addContact(); + void submitContact(); + void cancel(); +//! [edit and remove slots] + void editContact(); + void removeContact(); +//! [edit and remove slots] + void next(); + void previous(); + +private: +//! [updateInterface() declaration] + void updateInterface(Mode mode); +//! [updateInterface() declaration] + QPushButton *addButton; +//! [buttons declaration] + QPushButton *editButton; + QPushButton *removeButton; +//! [buttons declaration] + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + QString oldName; + QString oldAddress; +//! [mode declaration] + Mode currentMode; +//! [mode declaration] +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook/part4/main.cpp b/examples/widgets/tutorials/addressbook/part4/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part4/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook/part4/part4.desktop b/examples/widgets/tutorials/addressbook/part4/part4.desktop new file mode 100644 index 0000000000..27802b10f2 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part4/part4.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=4 Address Book +Exec=/opt/usr/bin/part4 +Icon=part4 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook/part4/part4.pro b/examples/widgets/tutorials/addressbook/part4/part4.pro new file mode 100644 index 0000000000..5b3462c65a --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part4/part4.pro @@ -0,0 +1,13 @@ +SOURCES = addressbook.cpp \ + main.cpp +HEADERS = addressbook.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part4 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part4.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part4 +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook/part5/addressbook.cpp b/examples/widgets/tutorials/addressbook/part5/addressbook.cpp new file mode 100644 index 0000000000..49dc67edf0 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part5/addressbook.cpp @@ -0,0 +1,312 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); +//! [instantiating findButton] + findButton = new QPushButton(tr("&Find")); + findButton->setEnabled(false); +//! [instantiating findButton] + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + +//! [instantiating FindDialog] + dialog = new FindDialog; +//! [instantiating FindDialog] + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); +//! [signals and slots for find] + connect(findButton, SIGNAL(clicked()), this, SLOT(findContact())); +//! [signals and slots for find] + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); +//! [adding findButton to layout] + buttonLayout1->addWidget(findButton); +//! [adding findButton to layout] + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 2, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} + +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} + +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} +//! [findContact() function] +void AddressBook::findContact() +{ + dialog->show(); + + if (dialog->exec() == QDialog::Accepted) { + QString contactName = dialog->getFindText(); + + if (contacts.contains(contactName)) { + nameLine->setText(contactName); + addressText->setText(contacts.value(contactName)); + } else { + QMessageBox::information(this, tr("Contact Not Found"), + tr("Sorry, \"%1\" is not in your address book.").arg(contactName)); + return; + } + } + + updateInterface(NavigationMode); +} +//! [findContact() function] + +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + break; + + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + findButton->setEnabled(number > 2); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); + break; + } +} diff --git a/examples/widgets/tutorials/addressbook/part5/addressbook.h b/examples/widgets/tutorials/addressbook/part5/addressbook.h new file mode 100644 index 0000000000..5d58952037 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part5/addressbook.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> +//! [include finddialog's header] +#include "finddialog.h" +//! [include finddialog's header] +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + enum Mode { NavigationMode, AddingMode, EditingMode }; + +public slots: + void addContact(); + void editContact(); + void submitContact(); + void cancel(); + void removeContact(); +//! [findContact() declaration] + void findContact(); +//! [findContact() declaration] + void next(); + void previous(); + +private: + void updateInterface(Mode mode); + + QPushButton *addButton; + QPushButton *editButton; + QPushButton *removeButton; +//! [findButton declaration] + QPushButton *findButton; +//! [findButton declaration] + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; +//! [FindDialog declaration] + FindDialog *dialog; +//! [FindDialog declaration] + QString oldName; + QString oldAddress; + Mode currentMode; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook/part5/finddialog.cpp b/examples/widgets/tutorials/addressbook/part5/finddialog.cpp new file mode 100644 index 0000000000..fdeab4c4bb --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part5/finddialog.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** 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 "finddialog.h" + +//! [constructor] +FindDialog::FindDialog(QWidget *parent) + : QDialog(parent) +{ + QLabel *findLabel = new QLabel(tr("Enter the name of a contact:")); + lineEdit = new QLineEdit; + + findButton = new QPushButton(tr("&Find")); + findText = ""; + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(findLabel); + layout->addWidget(lineEdit); + layout->addWidget(findButton); + + setLayout(layout); + setWindowTitle(tr("Find a Contact")); + connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); + connect(findButton, SIGNAL(clicked()), this, SLOT(accept())); +} +//! [constructor] +//! [findClicked() function] +void FindDialog::findClicked() +{ + QString text = lineEdit->text(); + + if (text.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name.")); + return; + } else { + findText = text; + lineEdit->clear(); + hide(); + } +} +//! [findClicked() function] +//! [getFindText() function] +QString FindDialog::getFindText() +{ + return findText; +} +//! [getFindText() function] diff --git a/examples/widgets/tutorials/addressbook/part5/finddialog.h b/examples/widgets/tutorials/addressbook/part5/finddialog.h new file mode 100644 index 0000000000..2ebaef1d86 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part5/finddialog.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** 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 FINDDIALOG_H +#define FINDDIALOG_H +//! [FindDialog header] +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QLineEdit; +class QPushButton; +QT_END_NAMESPACE + +class FindDialog : public QDialog +{ + Q_OBJECT + +public: + FindDialog(QWidget *parent = 0); + QString getFindText(); + +public slots: + void findClicked(); + +private: + QPushButton *findButton; + QLineEdit *lineEdit; + QString findText; +}; +//! [FindDialog header] +#endif diff --git a/examples/widgets/tutorials/addressbook/part5/main.cpp b/examples/widgets/tutorials/addressbook/part5/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part5/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook/part5/part5.desktop b/examples/widgets/tutorials/addressbook/part5/part5.desktop new file mode 100644 index 0000000000..e8b151c40f --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part5/part5.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=5 Address Book +Exec=/opt/usr/bin/part5 +Icon=part5 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook/part5/part5.pro b/examples/widgets/tutorials/addressbook/part5/part5.pro new file mode 100644 index 0000000000..84757b2455 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part5/part5.pro @@ -0,0 +1,15 @@ +SOURCES = addressbook.cpp \ + finddialog.cpp \ + main.cpp +HEADERS = addressbook.h \ + finddialog.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part5 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part5.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part5 +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook/part6/addressbook.cpp b/examples/widgets/tutorials/addressbook/part6/addressbook.cpp new file mode 100644 index 0000000000..c7155cc370 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part6/addressbook.cpp @@ -0,0 +1,393 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); + findButton = new QPushButton(tr("&Find")); + findButton->setEnabled(false); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + + loadButton = new QPushButton(tr("&Load...")); +//! [tooltip 1] + loadButton->setToolTip(tr("Load contacts from a file")); +//! [tooltip 1] + saveButton = new QPushButton(tr("&Save...")); +//! [tooltip 2] + saveButton->setToolTip(tr("Save contacts to a file")); +//! [tooltip 2] + saveButton->setEnabled(false); + + dialog = new FindDialog; + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); + connect(findButton, SIGNAL(clicked()), this, SLOT(findContact())); + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFromFile())); + connect(saveButton, SIGNAL(clicked()), this, SLOT(saveToFile())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); + buttonLayout1->addWidget(findButton); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addWidget(loadButton); + buttonLayout1->addWidget(saveButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 2, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} + +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} + +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::findContact() +{ + dialog->show(); + + if (dialog->exec() == 1) { + QString contactName = dialog->getFindText(); + + if (contacts.contains(contactName)) { + nameLine->setText(contactName); + addressText->setText(contacts.value(contactName)); + } else { + QMessageBox::information(this, tr("Contact Not Found"), + tr("Sorry, \"%1\" is not in your address book.").arg(contactName)); + return; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + + loadButton->setEnabled(false); + saveButton->setEnabled(false); + break; + + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + findButton->setEnabled(number > 2); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); + + loadButton->setEnabled(true); + saveButton->setEnabled(number >= 1); + break; + } +} + +//! [saveToFile() function part1] +void AddressBook::saveToFile() +{ + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); + +//! [saveToFile() function part1] +//! [saveToFile() function part2] + if (fileName.isEmpty()) + return; + else { + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + +//! [saveToFile() function part2] +//! [saveToFile() function part3] + QDataStream out(&file); + out.setVersion(QDataStream::Qt_4_5); + out << contacts; + } +} +//! [saveToFile() function part3] + +//! [loadFromFile() function part1] +void AddressBook::loadFromFile() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); +//! [loadFromFile() function part1] + +//! [loadFromFile() function part2] + if (fileName.isEmpty()) + return; + else { + + QFile file(fileName); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QDataStream in(&file); + in.setVersion(QDataStream::Qt_4_5); + contacts.clear(); // clear existing contacts + in >> contacts; +//! [loadFromFile() function part2] + +//! [loadFromFile() function part3] + if (contacts.isEmpty()) { + QMessageBox::information(this, tr("No contacts in file"), + tr("The file you are attempting to open contains no contacts.")); + } else { + QMap<QString, QString>::iterator i = contacts.begin(); + nameLine->setText(i.key()); + addressText->setText(i.value()); + } + } + + updateInterface(NavigationMode); +} +//! [loadFromFile() function part3] diff --git a/examples/widgets/tutorials/addressbook/part6/addressbook.h b/examples/widgets/tutorials/addressbook/part6/addressbook.h new file mode 100644 index 0000000000..59a8b9951d --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part6/addressbook.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> +#include "finddialog.h" + +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + enum Mode { NavigationMode, AddingMode, EditingMode }; + +public slots: + void addContact(); + void editContact(); + void submitContact(); + void cancel(); + void removeContact(); + void findContact(); + void next(); + void previous(); +//! [save and load functions declaration] + void saveToFile(); + void loadFromFile(); +//! [save and load functions declaration] + +private: + void updateInterface(Mode mode); + + QPushButton *addButton; + QPushButton *editButton; + QPushButton *removeButton; + QPushButton *findButton; + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; +//! [save and load buttons declaration] + QPushButton *loadButton; + QPushButton *saveButton; +//! [save and load buttons declaration] + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + FindDialog *dialog; + QString oldName; + QString oldAddress; + Mode currentMode; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook/part6/finddialog.cpp b/examples/widgets/tutorials/addressbook/part6/finddialog.cpp new file mode 100644 index 0000000000..686af08c71 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part6/finddialog.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 "finddialog.h" + +FindDialog::FindDialog(QWidget *parent) + : QDialog(parent) +{ + QLabel *findLabel = new QLabel(tr("Enter the name of a contact:")); + lineEdit = new QLineEdit; + + findButton = new QPushButton(tr("&Find")); + findText = ""; + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(findLabel); + layout->addWidget(lineEdit); + layout->addWidget(findButton); + + setLayout(layout); + setWindowTitle(tr("Find a Contact")); + connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); + connect(findButton, SIGNAL(clicked()), this, SLOT(accept())); +} + +void FindDialog::findClicked() +{ + QString text = lineEdit->text(); + + if (text.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name.")); + return; + } else { + findText = text; + lineEdit->clear(); + hide(); + } +} + +QString FindDialog::getFindText() +{ + return findText; +} diff --git a/examples/widgets/tutorials/addressbook/part6/finddialog.h b/examples/widgets/tutorials/addressbook/part6/finddialog.h new file mode 100644 index 0000000000..86a33bbb72 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part6/finddialog.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** 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 FINDDIALOG_H +#define FINDDIALOG_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QLineEdit; +class QPushButton; +QT_END_NAMESPACE + +class FindDialog : public QDialog +{ + Q_OBJECT + +public: + FindDialog(QWidget *parent = 0); + QString getFindText(); + +public slots: + void findClicked(); + +private: + QPushButton *findButton; + QLineEdit *lineEdit; + QString findText; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook/part6/main.cpp b/examples/widgets/tutorials/addressbook/part6/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part6/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook/part6/part6.desktop b/examples/widgets/tutorials/addressbook/part6/part6.desktop new file mode 100644 index 0000000000..dd49260597 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part6/part6.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=6 Address Book +Exec=/opt/usr/bin/part6 +Icon=part6 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook/part6/part6.pro b/examples/widgets/tutorials/addressbook/part6/part6.pro new file mode 100644 index 0000000000..c6be890e91 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part6/part6.pro @@ -0,0 +1,15 @@ +SOURCES = addressbook.cpp \ + finddialog.cpp \ + main.cpp +HEADERS = addressbook.h \ + finddialog.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part6 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part6.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part6 +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/addressbook/part7/addressbook.cpp b/examples/widgets/tutorials/addressbook/part7/addressbook.cpp new file mode 100644 index 0000000000..2ebb04555f --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part7/addressbook.cpp @@ -0,0 +1,446 @@ +/**************************************************************************** +** +** 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 "addressbook.h" + +AddressBook::AddressBook(QWidget *parent) + : QWidget(parent) +{ + QLabel *nameLabel = new QLabel(tr("Name:")); + nameLine = new QLineEdit; + nameLine->setReadOnly(true); + + QLabel *addressLabel = new QLabel(tr("Address:")); + addressText = new QTextEdit; + addressText->setReadOnly(true); + + addButton = new QPushButton(tr("&Add")); + + editButton = new QPushButton(tr("&Edit")); + editButton->setEnabled(false); + removeButton = new QPushButton(tr("&Remove")); + removeButton->setEnabled(false); + findButton = new QPushButton(tr("&Find")); + findButton->setEnabled(false); + submitButton = new QPushButton(tr("&Submit")); + submitButton->hide(); + cancelButton = new QPushButton(tr("&Cancel")); + cancelButton->hide(); + + nextButton = new QPushButton(tr("&Next")); + nextButton->setEnabled(false); + previousButton = new QPushButton(tr("&Previous")); + previousButton->setEnabled(false); + + loadButton = new QPushButton(tr("&Load...")); + loadButton->setToolTip(tr("Load contacts from a file")); + saveButton = new QPushButton(tr("&Save...")); + saveButton->setToolTip(tr("Save contacts to a file")); + saveButton->setEnabled(false); + + exportButton = new QPushButton(tr("E&xport")); + exportButton->setToolTip(tr("Export as vCard")); + exportButton->setEnabled(false); + + dialog = new FindDialog; + + connect(addButton, SIGNAL(clicked()), this, SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact())); + connect(editButton, SIGNAL(clicked()), this, SLOT(editContact())); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact())); + connect(findButton, SIGNAL(clicked()), this, SLOT(findContact())); + connect(nextButton, SIGNAL(clicked()), this, SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, SLOT(previous())); + connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFromFile())); + connect(saveButton, SIGNAL(clicked()), this, SLOT(saveToFile())); + connect(exportButton, SIGNAL(clicked()), this, SLOT(exportAsVCard())); + + QVBoxLayout *buttonLayout1 = new QVBoxLayout; + buttonLayout1->addWidget(addButton); + buttonLayout1->addWidget(editButton); + buttonLayout1->addWidget(removeButton); + buttonLayout1->addWidget(findButton); + buttonLayout1->addWidget(submitButton); + buttonLayout1->addWidget(cancelButton); + buttonLayout1->addWidget(loadButton); + buttonLayout1->addWidget(saveButton); + buttonLayout1->addWidget(exportButton); + buttonLayout1->addStretch(); + + QHBoxLayout *buttonLayout2 = new QHBoxLayout; + buttonLayout2->addWidget(previousButton); + buttonLayout2->addWidget(nextButton); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(nameLabel, 0, 0); + mainLayout->addWidget(nameLine, 0, 1); + mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); + mainLayout->addWidget(addressText, 1, 1); + mainLayout->addLayout(buttonLayout1, 1, 2); + mainLayout->addLayout(buttonLayout2, 2, 1); + + setLayout(mainLayout); + setWindowTitle(tr("Simple Address Book")); +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} + +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name.isEmpty() || address.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + return; + } + + if (currentMode == AddingMode) { + + if (!contacts.contains(name)) { + contacts.insert(name, address); + QMessageBox::information(this, tr("Add Successful"), + tr("\"%1\" has been added to your address book.").arg(name)); + } else { + QMessageBox::information(this, tr("Add Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + addressText->setText(oldAddress); + updateInterface(NavigationMode); +} + +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap<QString, QString>::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::findContact() +{ + dialog->show(); + + if (dialog->exec() == 1) { + QString contactName = dialog->getFindText(); + + if (contacts.contains(contactName)) { + nameLine->setText(contactName); + addressText->setText(contacts.value(contactName)); + } else { + QMessageBox::information(this, tr("Contact Not Found"), + tr("Sorry, \"%1\" is not in your address book.").arg(contactName)); + return; + } + } + + updateInterface(NavigationMode); +} +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + + loadButton->setEnabled(false); + saveButton->setEnabled(false); + exportButton->setEnabled(false); + break; + + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + findButton->setEnabled(number > 2); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); + + exportButton->setEnabled(number >= 1); + + loadButton->setEnabled(true); + saveButton->setEnabled(number >= 1); + break; + } +} + +void AddressBook::saveToFile() +{ + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); + + if (fileName.isEmpty()) + return; + else { + QFile file(fileName); + + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QDataStream out(&file); + out.setVersion(QDataStream::Qt_4_3); + out << contacts; + } + + updateInterface(NavigationMode); +} + +void AddressBook::loadFromFile() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open Address Book"), "", + tr("Address Book (*.abk);;All Files (*)")); + + if (fileName.isEmpty()) + return; + else { + QFile file(fileName); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QDataStream in(&file); + in.setVersion(QDataStream::Qt_4_3); + contacts.empty(); // empty existing contacts + in >> contacts; + + QMap<QString, QString>::iterator i = contacts.begin(); + nameLine->setText(i.key()); + addressText->setText(i.value()); + } + + updateInterface(NavigationMode); +} + +//! [export function part1] +void AddressBook::exportAsVCard() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + QString firstName; + QString lastName; + QStringList nameList; + + int index = name.indexOf(" "); + + if (index != -1) { + nameList = name.split(QRegExp("\\s+"), QString::SkipEmptyParts); + firstName = nameList.first(); + lastName = nameList.last(); + } else { + firstName = name; + lastName = ""; + } + + QString fileName = QFileDialog::getSaveFileName(this, + tr("Export Contact"), "", + tr("vCard Files (*.vcf);;All Files (*)")); + + if (fileName.isEmpty()) + return; + + QFile file(fileName); +//! [export function part1] + +//! [export function part2] + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QTextStream out(&file); +//! [export function part2] + +//! [export function part3] + out << "BEGIN:VCARD" << "\n"; + out << "VERSION:2.1" << "\n"; + out << "N:" << lastName << ";" << firstName << "\n"; + + if (!nameList.isEmpty()) + out << "FN:" << nameList.join(" ") << "\n"; + else + out << "FN:" << firstName << "\n"; +//! [export function part3] + +//! [export function part4] + address.replace(";", "\\;", Qt::CaseInsensitive); + address.replace("\n", ";", Qt::CaseInsensitive); + address.replace(",", " ", Qt::CaseInsensitive); + + out << "ADR;HOME:;" << address << "\n"; + out << "END:VCARD" << "\n"; + + QMessageBox::information(this, tr("Export Successful"), + tr("\"%1\" has been exported as a vCard.").arg(name)); +} +//! [export function part4] diff --git a/examples/widgets/tutorials/addressbook/part7/addressbook.h b/examples/widgets/tutorials/addressbook/part7/addressbook.h new file mode 100644 index 0000000000..eb18cdc955 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part7/addressbook.h @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** 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 ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include <QWidget> +#include <QMap> +#include "finddialog.h" + +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QTextEdit; +QT_END_NAMESPACE + + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); + enum Mode { NavigationMode, AddingMode, EditingMode }; + +public slots: + void addContact(); + void editContact(); + void submitContact(); + void cancel(); + void removeContact(); + void findContact(); + void next(); + void previous(); + void saveToFile(); + void loadFromFile(); +//! [exportAsVCard() declaration] + void exportAsVCard(); +//! [exportAsVCard() declaration] + +private: + void updateInterface(Mode mode); + + QPushButton *addButton; + QPushButton *editButton; + QPushButton *removeButton; + QPushButton *findButton; + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *nextButton; + QPushButton *previousButton; + QPushButton *loadButton; + QPushButton *saveButton; +//! [exportButton declaration] + QPushButton *exportButton; +//! [exportButton declaration] + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap<QString, QString> contacts; + FindDialog *dialog; + QString oldName; + QString oldAddress; + Mode currentMode; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook/part7/finddialog.cpp b/examples/widgets/tutorials/addressbook/part7/finddialog.cpp new file mode 100644 index 0000000000..686af08c71 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part7/finddialog.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 "finddialog.h" + +FindDialog::FindDialog(QWidget *parent) + : QDialog(parent) +{ + QLabel *findLabel = new QLabel(tr("Enter the name of a contact:")); + lineEdit = new QLineEdit; + + findButton = new QPushButton(tr("&Find")); + findText = ""; + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(findLabel); + layout->addWidget(lineEdit); + layout->addWidget(findButton); + + setLayout(layout); + setWindowTitle(tr("Find a Contact")); + connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); + connect(findButton, SIGNAL(clicked()), this, SLOT(accept())); +} + +void FindDialog::findClicked() +{ + QString text = lineEdit->text(); + + if (text.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name.")); + return; + } else { + findText = text; + lineEdit->clear(); + hide(); + } +} + +QString FindDialog::getFindText() +{ + return findText; +} diff --git a/examples/widgets/tutorials/addressbook/part7/finddialog.h b/examples/widgets/tutorials/addressbook/part7/finddialog.h new file mode 100644 index 0000000000..86a33bbb72 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part7/finddialog.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** 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 FINDDIALOG_H +#define FINDDIALOG_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QLineEdit; +class QPushButton; +QT_END_NAMESPACE + +class FindDialog : public QDialog +{ + Q_OBJECT + +public: + FindDialog(QWidget *parent = 0); + QString getFindText(); + +public slots: + void findClicked(); + +private: + QPushButton *findButton; + QLineEdit *lineEdit; + QString findText; +}; + +#endif diff --git a/examples/widgets/tutorials/addressbook/part7/main.cpp b/examples/widgets/tutorials/addressbook/part7/main.cpp new file mode 100644 index 0000000000..663fbb7d09 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part7/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 "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + AddressBook addressBook; + addressBook.show(); + + return app.exec(); +} diff --git a/examples/widgets/tutorials/addressbook/part7/part7.desktop b/examples/widgets/tutorials/addressbook/part7/part7.desktop new file mode 100644 index 0000000000..26d3fdd292 --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part7/part7.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=7 Address Book +Exec=/opt/usr/bin/part7 +Icon=part7 +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/addressbook/part7/part7.pro b/examples/widgets/tutorials/addressbook/part7/part7.pro new file mode 100644 index 0000000000..9cee4c94db --- /dev/null +++ b/examples/widgets/tutorials/addressbook/part7/part7.pro @@ -0,0 +1,15 @@ +SOURCES = addressbook.cpp \ + finddialog.cpp \ + main.cpp +HEADERS = addressbook.h \ + finddialog.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part7 +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS part7.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/addressbook/part7 +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/gettingStarted/gettingStarted.pro b/examples/widgets/tutorials/gettingStarted/gettingStarted.pro new file mode 100644 index 0000000000..4ab5f4e6dd --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gettingStarted.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +SUBDIRS += +QT += widgets diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/gsqt.pro b/examples/widgets/tutorials/gettingStarted/gsQt/gsqt.pro new file mode 100644 index 0000000000..3dac79afce --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/gsqt.pro @@ -0,0 +1,14 @@ +TEMPLATE = subdirs + +SUBDIRS = part1 \ + part2 \ + part3 \ + part4 \ + part5 + +# install +sources.files = *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/gettingStarted/gsQt +INSTALLS += sources + +QT += widgets diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part1/main.cpp b/examples/widgets/tutorials/gettingStarted/gsQt/part1/main.cpp new file mode 100644 index 0000000000..6557d0525f --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part1/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> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QTextEdit textEdit; + textEdit.show(); + + return app.exec(); +} + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part1/part1.pro b/examples/widgets/tutorials/gettingStarted/gsQt/part1/part1.pro new file mode 100644 index 0000000000..b3fd65a1f8 --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part1/part1.pro @@ -0,0 +1,10 @@ + +QT += widgets +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tutorials/gettingStarted/gsQt/part1 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tutorial/gettingStarted/gsQt/part1 +INSTALLS += target sources + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part2/main.cpp b/examples/widgets/tutorials/gettingStarted/gsQt/part2/main.cpp new file mode 100644 index 0000000000..996c3149c6 --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part2/main.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** 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> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QTextEdit textEdit; + QPushButton quitButton("&Quit"); + + QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); + + QVBoxLayout layout; + layout.addWidget(&textEdit); + layout.addWidget(&quitButton); + + QWidget window; + window.setLayout(&layout); + + window.show(); + + return app.exec(); +} + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part2/part2.pro b/examples/widgets/tutorials/gettingStarted/gsQt/part2/part2.pro new file mode 100644 index 0000000000..81fb0e9ff9 --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part2/part2.pro @@ -0,0 +1,10 @@ + +QT += widgets +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tutorials/gettingStarted/gsQt/part2 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tutorial/gettingStarted/gsQt/part2 +INSTALLS += target sources + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part3/main.cpp b/examples/widgets/tutorials/gettingStarted/gsQt/part3/main.cpp new file mode 100644 index 0000000000..db3eba7e4a --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part3/main.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** 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> + +class Notepad : public QWidget +{ + Q_OBJECT + +public: + Notepad(); + +private slots: + void quit(); + +private: + QTextEdit *textEdit; + QPushButton *quitButton; + +}; + +Notepad::Notepad() +{ + textEdit = new QTextEdit; + quitButton = new QPushButton(tr("Quit")); + + connect(quitButton, SIGNAL(clicked()), this, SLOT(quit())); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(textEdit); + layout->addWidget(quitButton); + + setLayout(layout); + + setWindowTitle(tr("Notepad")); +} + +void Notepad::quit() +{ + QMessageBox messageBox; + messageBox.setWindowTitle(tr("Notepad")); + messageBox.setText(tr("Do you really want to quit?")); + messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + messageBox.setDefaultButton(QMessageBox::No); + if (messageBox.exec() == QMessageBox::Yes) + qApp->quit(); +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + Notepad notepad; + notepad.show(); + + return app.exec(); +} + +#include "main.moc" + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part3/part3.pro b/examples/widgets/tutorials/gettingStarted/gsQt/part3/part3.pro new file mode 100644 index 0000000000..c89f95fbfa --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part3/part3.pro @@ -0,0 +1,10 @@ + +QT += widgets +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tutorials/gettingStarted/gsQt/part3 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tutorial/gettingStarted/gsQt/part3 +INSTALLS += target sources + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part4/main.cpp b/examples/widgets/tutorials/gettingStarted/gsQt/part4/main.cpp new file mode 100644 index 0000000000..f5ec5bea5e --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part4/main.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** 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> + +class Notepad : public QMainWindow +{ + Q_OBJECT + +public: + Notepad(); + +private slots: + void load(); + void save(); + +private: + QTextEdit *textEdit; + + QAction *loadAction; + QAction *saveAction; + QAction *exitAction; + + QMenu *fileMenu; +}; + +Notepad::Notepad() +{ + + loadAction = new QAction(tr("&Load"), this); + saveAction = new QAction(tr("&Save"), this); + exitAction = new QAction(tr("E&xit"), this); + + connect(loadAction, SIGNAL(triggered()), this, SLOT(load())); + connect(saveAction, SIGNAL(triggered()), this, SLOT(save())); + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(loadAction); + fileMenu->addAction(saveAction); + fileMenu->addSeparator(); + fileMenu->addAction(exitAction); + + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + setWindowTitle(tr("Notepad")); +} + +void Notepad::load() +{ + +} + +void Notepad::save() +{ + +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + Notepad notepad; + notepad.show(); + + return app.exec(); +}; + +#include "main.moc" + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part4/part4.pro b/examples/widgets/tutorials/gettingStarted/gsQt/part4/part4.pro new file mode 100644 index 0000000000..94c7abdf39 --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part4/part4.pro @@ -0,0 +1,10 @@ + +QT += widgets +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tutorials/gettingStarted/gsQt/part4 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tutorial/gettingStarted/gsQt/part4 +INSTALLS += target sources + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part5/main.cpp b/examples/widgets/tutorials/gettingStarted/gsQt/part5/main.cpp new file mode 100644 index 0000000000..25ee5d0fbb --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part5/main.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** 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> + +class Notepad : public QMainWindow +{ + Q_OBJECT + +public: + Notepad(); + +private slots: + void open(); + void save(); + +private: + QTextEdit *textEdit; + + QAction *openAction; + QAction *saveAction; + QAction *exitAction; + + QMenu *fileMenu; +}; + +Notepad::Notepad() +{ + + openAction = new QAction(tr("&Load"), this); + saveAction = new QAction(tr("&Save"), this); + exitAction = new QAction(tr("E&xit"), this); + + connect(openAction, SIGNAL(triggered()), this, SLOT(open())); + connect(saveAction, SIGNAL(triggered()), this, SLOT(save())); + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(openAction); + fileMenu->addAction(saveAction); + fileMenu->addSeparator(); + fileMenu->addAction(exitAction); + + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + setWindowTitle(tr("Notepad")); +} + +void Notepad::open() +{ + QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", + tr("Text Files (*.txt);;C++ Files (*.cpp *.h)")); + + if (fileName != "") { + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::critical(this, tr("Error"), tr("Could not open file")); + return; + } + QTextStream in(&file); + textEdit->setText(in.readAll()); + file.close(); + } +} + +void Notepad::save() +{ + + QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", + tr("Text Files (*.txt);;C++ Files (*.cpp *.h)")); + + if (fileName != "") { + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly)) { + // error message + } else { + QTextStream stream(&file); + stream << textEdit->toPlainText(); + stream.flush(); + file.close(); + } + } +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + Notepad notepad; + notepad.show(); + + return app.exec(); +} + +#include "main.moc" + diff --git a/examples/widgets/tutorials/gettingStarted/gsQt/part5/part5.pro b/examples/widgets/tutorials/gettingStarted/gsQt/part5/part5.pro new file mode 100644 index 0000000000..4631449e71 --- /dev/null +++ b/examples/widgets/tutorials/gettingStarted/gsQt/part5/part5.pro @@ -0,0 +1,10 @@ + +QT += widgets +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tutorials/gettingStarted/gsQt/part5 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tutorial/gettingStarted/gsQt/part5 +INSTALLS += target sources + diff --git a/examples/widgets/tutorials/modelview/1_readonly/1_readonly.desktop b/examples/widgets/tutorials/modelview/1_readonly/1_readonly.desktop new file mode 100644 index 0000000000..137f56eacd --- /dev/null +++ b/examples/widgets/tutorials/modelview/1_readonly/1_readonly.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=1 Model View +Exec=/opt/usr/bin/1_readonly +Icon=1_readonly +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/modelview/1_readonly/1_readonly.pro b/examples/widgets/tutorials/modelview/1_readonly/1_readonly.pro new file mode 100644 index 0000000000..d8b496030d --- /dev/null +++ b/examples/widgets/tutorials/modelview/1_readonly/1_readonly.pro @@ -0,0 +1,19 @@ +TARGET = mv_readonly + +TEMPLATE = app + +SOURCES += main.cpp \ + mymodel.cpp + +HEADERS += mymodel.h + + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/1_readonly +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 1_readonly.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/1_readonly +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/modelview/1_readonly/main.cpp b/examples/widgets/tutorials/modelview/1_readonly/main.cpp new file mode 100644 index 0000000000..2b9a5c7a32 --- /dev/null +++ b/examples/widgets/tutorials/modelview/1_readonly/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +//! [Quoting ModelView Tutorial] +// main.cpp +#include <QtWidgets/QApplication> +#include <QtWidgets/QTableView> +#include "mymodel.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QTableView tableView; + MyModel myModel(0); + tableView.setModel( &myModel ); + tableView.show(); + return a.exec(); +} +//! [Quoting ModelView Tutorial] diff --git a/examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp b/examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp new file mode 100644 index 0000000000..513c64d578 --- /dev/null +++ b/examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +//! [Quoting ModelView Tutorial] +// mymodel.cpp +#include "mymodel.h" + +MyModel::MyModel(QObject *parent) + :QAbstractTableModel(parent) +{ +} + +int MyModel::rowCount(const QModelIndex & /*parent*/) const +{ + return 2; +} + +int MyModel::columnCount(const QModelIndex & /*parent*/) const +{ + return 3; +} + +QVariant MyModel::data(const QModelIndex &index, int role) const +{ + if (role == Qt::DisplayRole) + { + return QString("Row%1, Column%2") + .arg(index.row() + 1) + .arg(index.column() +1); + } + return QVariant(); +} +//! [Quoting ModelView Tutorial] diff --git a/examples/widgets/tutorials/modelview/1_readonly/mymodel.h b/examples/widgets/tutorials/modelview/1_readonly/mymodel.h new file mode 100644 index 0000000000..b4ff23d9c7 --- /dev/null +++ b/examples/widgets/tutorials/modelview/1_readonly/mymodel.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** 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 MYMODEL_H +#define MYMODEL_H + +//! [Quoting ModelView Tutorial] +// mymodel.h +#include <QAbstractTableModel> + +class MyModel : public QAbstractTableModel +{ + Q_OBJECT +public: + MyModel(QObject *parent); + int rowCount(const QModelIndex &parent = QModelIndex()) const ; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; +}; +//! [Quoting ModelView Tutorial] + +#endif // MYMODEL_H diff --git a/examples/widgets/tutorials/modelview/2_formatting/2_formatting.desktop b/examples/widgets/tutorials/modelview/2_formatting/2_formatting.desktop new file mode 100644 index 0000000000..a3950001ba --- /dev/null +++ b/examples/widgets/tutorials/modelview/2_formatting/2_formatting.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=2 Model View +Exec=/opt/usr/bin/2_formatting +Icon=2_formatting +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/modelview/2_formatting/2_formatting.pro b/examples/widgets/tutorials/modelview/2_formatting/2_formatting.pro new file mode 100644 index 0000000000..2b567969de --- /dev/null +++ b/examples/widgets/tutorials/modelview/2_formatting/2_formatting.pro @@ -0,0 +1,18 @@ +TARGET = mv_formatting + +TEMPLATE = app + +SOURCES += main.cpp \ + mymodel.cpp + +HEADERS += mymodel.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/2_formatting +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 2_formatting.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/2_formatting +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/modelview/2_formatting/main.cpp b/examples/widgets/tutorials/modelview/2_formatting/main.cpp new file mode 100644 index 0000000000..2b9a5c7a32 --- /dev/null +++ b/examples/widgets/tutorials/modelview/2_formatting/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +//! [Quoting ModelView Tutorial] +// main.cpp +#include <QtWidgets/QApplication> +#include <QtWidgets/QTableView> +#include "mymodel.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QTableView tableView; + MyModel myModel(0); + tableView.setModel( &myModel ); + tableView.show(); + return a.exec(); +} +//! [Quoting ModelView Tutorial] diff --git a/examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp b/examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp new file mode 100644 index 0000000000..2ad4448e57 --- /dev/null +++ b/examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** 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 <QFont> +#include <QBrush> +#include "mymodel.h" +#include <QDebug> + +MyModel::MyModel(QObject *parent) + :QAbstractTableModel(parent) +{ +} + +int MyModel::rowCount(const QModelIndex & /*parent */) const +{ + return 2; +} + +int MyModel::columnCount(const QModelIndex & /*parent */) const +{ + return 3; +} + +//! [Quoting ModelView Tutorial] +// mymodel.cpp +QVariant MyModel::data(const QModelIndex &index, int role) const +{ + int row = index.row(); + int col = index.column(); + // generate a log message when this method gets called + qDebug() << QString("row %1, col%2, role %3") + .arg(row).arg(col).arg(role); + + switch(role){ + case Qt::DisplayRole: + if (row == 0 && col == 1) return QString("<--left"); + if (row == 1 && col == 1) return QString("right-->"); + + return QString("Row%1, Column%2") + .arg(row + 1) + .arg(col +1); + break; + case Qt::FontRole: + if (row == 0 && col == 0) //change font only for cell(0,0) + { + QFont boldFont; + boldFont.setBold(true); + return boldFont; + } + break; + case Qt::BackgroundRole: + + if (row == 1 && col == 2) //change background only for cell(1,2) + { + QBrush redBackground(Qt::red); + return redBackground; + } + break; + case Qt::TextAlignmentRole: + + if (row == 1 && col == 1) //change text alignment only for cell(1,1) + { + return Qt::AlignRight + Qt::AlignVCenter; + } + break; + case Qt::CheckStateRole: + + if (row == 1 && col == 0) //add a checkbox to cell(1,0) + { + return Qt::Checked; + } + } + return QVariant(); +} +//! [Quoting ModelView Tutorial] diff --git a/examples/widgets/tutorials/modelview/2_formatting/mymodel.h b/examples/widgets/tutorials/modelview/2_formatting/mymodel.h new file mode 100644 index 0000000000..d146f8c49d --- /dev/null +++ b/examples/widgets/tutorials/modelview/2_formatting/mymodel.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** 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 MYMODEL_H +#define MYMODEL_H + +#include <QAbstractTableModel> + +class MyModel : public QAbstractTableModel +{ + Q_OBJECT +public: + MyModel(QObject *parent); + int rowCount(const QModelIndex &parent = QModelIndex()) const ; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; +}; + +#endif // MYMODEL_H diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/3_changingmodel.desktop b/examples/widgets/tutorials/modelview/3_changingmodel/3_changingmodel.desktop new file mode 100644 index 0000000000..3e053c94b1 --- /dev/null +++ b/examples/widgets/tutorials/modelview/3_changingmodel/3_changingmodel.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=3 Model View +Exec=/opt/usr/bin/3_changingmodel +Icon=3_changingmodel +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/3_changingmodel.pro b/examples/widgets/tutorials/modelview/3_changingmodel/3_changingmodel.pro new file mode 100644 index 0000000000..2649168f4d --- /dev/null +++ b/examples/widgets/tutorials/modelview/3_changingmodel/3_changingmodel.pro @@ -0,0 +1,18 @@ +TARGET = mv_changingmodel + +TEMPLATE = app + +SOURCES += main.cpp \ + mymodel.cpp + +HEADERS += mymodel.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/3_changingmodel +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 3_changingmodel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/3_changingmodel +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/main.cpp b/examples/widgets/tutorials/modelview/3_changingmodel/main.cpp new file mode 100644 index 0000000000..0a4f559170 --- /dev/null +++ b/examples/widgets/tutorials/modelview/3_changingmodel/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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/QApplication> +#include <QtWidgets/QTableView> +#include "mymodel.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QTableView tableView; + MyModel myModel(0); + tableView.setModel( &myModel ); + tableView.show(); + return a.exec(); +} diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp new file mode 100644 index 0000000000..a446017344 --- /dev/null +++ b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** 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 <QBrush> +#include <QTime> +#include "mymodel.h" + +//! [quoting mymodel_a] +MyModel::MyModel(QObject *parent) + :QAbstractTableModel(parent) +{ +// selectedCell = 0; + timer = new QTimer(this); + timer->setInterval(1000); + connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit())); + timer->start(); +} +//! [quoting mymodel_a] +//------------------------------------------------------- +int MyModel::rowCount(const QModelIndex & /*parent */) const +{ + return 2; +} + +//------------------------------------------------------- +int MyModel::columnCount(const QModelIndex & /*parent */) const +{ + return 3; +} + +//------------------------------------------------------- +//! [quoting mymodel_QVariant ] +QVariant MyModel::data(const QModelIndex &index, int role) const +{ + int row = index.row(); + int col = index.column(); + + if (role == Qt::DisplayRole) + { + if (row == 0 && col == 0) + { + return QTime::currentTime().toString(); + } + } + return QVariant(); +} +//! [quoting mymodel_QVariant ] +//------------------------------------------------------- +//! [quoting mymodel_b ] +void MyModel::timerHit() +{ + //we identify the top left cell + QModelIndex topLeft = createIndex(0,0); + //emit a signal to make the view reread identified data + emit dataChanged(topLeft, topLeft); +} +//! [quoting mymodel_b ] diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h new file mode 100644 index 0000000000..5bcf893e34 --- /dev/null +++ b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** 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 MYMODEL_H +#define MYMODEL_H + +#include <QAbstractTableModel> +#include <QTimer> + +class MyModel : public QAbstractTableModel +{ + Q_OBJECT +public: + MyModel(QObject *parent); + int rowCount(const QModelIndex &parent = QModelIndex()) const ; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QTimer *timer; +private: + int selectedCell; +private slots: + void timerHit(); +}; + +#endif // MYMODEL_H diff --git a/examples/widgets/tutorials/modelview/4_headers/4_headers.desktop b/examples/widgets/tutorials/modelview/4_headers/4_headers.desktop new file mode 100644 index 0000000000..f17fe456f2 --- /dev/null +++ b/examples/widgets/tutorials/modelview/4_headers/4_headers.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=4 Model View +Exec=/opt/usr/bin/4_headers +Icon=4_headers +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/modelview/4_headers/4_headers.pro b/examples/widgets/tutorials/modelview/4_headers/4_headers.pro new file mode 100644 index 0000000000..308cb2878c --- /dev/null +++ b/examples/widgets/tutorials/modelview/4_headers/4_headers.pro @@ -0,0 +1,18 @@ +TARGET = mv_headers + +TEMPLATE = app + +SOURCES += main.cpp \ + mymodel.cpp + +HEADERS += mymodel.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/4_headers +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 4_headers.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/4_headers +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/modelview/4_headers/main.cpp b/examples/widgets/tutorials/modelview/4_headers/main.cpp new file mode 100644 index 0000000000..a3ce65e94f --- /dev/null +++ b/examples/widgets/tutorials/modelview/4_headers/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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/QApplication> +#include <QtWidgets/QTableView> +#include "mymodel.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QTableView tableView; + MyModel myModel(0); + tableView.setModel( &myModel ); + tableView.show(); + return a.exec(); +}
\ No newline at end of file diff --git a/examples/widgets/tutorials/modelview/4_headers/mymodel.cpp b/examples/widgets/tutorials/modelview/4_headers/mymodel.cpp new file mode 100644 index 0000000000..4a91f6c541 --- /dev/null +++ b/examples/widgets/tutorials/modelview/4_headers/mymodel.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** 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 "mymodel.h" + +MyModel::MyModel(QObject *parent) + :QAbstractTableModel(parent) +{ +} + +//------------------------------------------------------- +int MyModel::rowCount(const QModelIndex & /*parent*/) const +{ + return 2; +} + +//------------------------------------------------------- +int MyModel::columnCount(const QModelIndex & /*parent*/) const +{ + return 3; +} + +//------------------------------------------------------- +QVariant MyModel::data(const QModelIndex &index, int role) const +{ + if (role == Qt::DisplayRole) + { + return QString("Row%1, Column%2") + .arg(index.row() + 1) + .arg(index.column() +1); + } + return QVariant(); +} + +//! [quoting mymodel_c] +QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role == Qt::DisplayRole) + { + if (orientation == Qt::Horizontal) { + switch (section) + { + case 0: + return QString("first"); + case 1: + return QString("second"); + case 2: + return QString("third"); + } + } + } + return QVariant(); +} +//! [quoting mymodel_c] diff --git a/examples/widgets/tutorials/modelview/4_headers/mymodel.h b/examples/widgets/tutorials/modelview/4_headers/mymodel.h new file mode 100644 index 0000000000..fcc657035b --- /dev/null +++ b/examples/widgets/tutorials/modelview/4_headers/mymodel.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** 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 MYMODEL_H +#define MYMODEL_H + +#include <QAbstractTableModel> + +class MyModel : public QAbstractTableModel +{ + Q_OBJECT +public: + MyModel(QObject *parent); + 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) const; +}; + +#endif // MYMODEL_H diff --git a/examples/widgets/tutorials/modelview/5_edit/5_edit.desktop b/examples/widgets/tutorials/modelview/5_edit/5_edit.desktop new file mode 100644 index 0000000000..4402c0a379 --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit/5_edit.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=5 Model View +Exec=/opt/usr/bin/5_edit +Icon=5_edit +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/modelview/5_edit/5_edit.pro b/examples/widgets/tutorials/modelview/5_edit/5_edit.pro new file mode 100644 index 0000000000..cd55a0b8f4 --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit/5_edit.pro @@ -0,0 +1,20 @@ +TARGET = mv_edit + +TEMPLATE = app + +SOURCES += main.cpp \ + mainwindow.cpp \ + mymodel.cpp + +HEADERS += mainwindow.h \ + mymodel.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/5_edit +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 5_edit.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/5_edit +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/modelview/5_edit/main.cpp b/examples/widgets/tutorials/modelview/5_edit/main.cpp new file mode 100644 index 0000000000..0e10352554 --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit/main.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** 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/QApplication> +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp b/examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp new file mode 100644 index 0000000000..e2e13562cf --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** 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 <QTableView> +#include "mainwindow.h" +#include "mymodel.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + tableView = new QTableView(this); + setCentralWidget(tableView); + QAbstractTableModel *myModel = new MyModel(this); + tableView->setModel(myModel); + + //transfer changes to the model to the window title + connect(myModel, SIGNAL(editCompleted(const QString &)), this, SLOT(setWindowTitle(const QString &))); +} + +void MainWindow::showWindowTitle(const QString & title) +{ +setWindowTitle(title); +} diff --git a/examples/widgets/tutorials/modelview/5_edit/mainwindow.h b/examples/widgets/tutorials/modelview/5_edit/mainwindow.h new file mode 100644 index 0000000000..2ab42289e1 --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit/mainwindow.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** 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/QMainWindow> + +QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code +class QTableView; //forward declaration +QT_END_NAMESPACE + + +class MainWindow : public QMainWindow +{ + Q_OBJECT +private: + QTableView *tableView; +public: + MainWindow(QWidget *parent = 0); +public slots: + void showWindowTitle(const QString & title); +}; + +#endif // MAINWINDOW_H diff --git a/examples/widgets/tutorials/modelview/5_edit/mymodel.cpp b/examples/widgets/tutorials/modelview/5_edit/mymodel.cpp new file mode 100644 index 0000000000..214a5b7cde --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit/mymodel.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** 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 "mymodel.h" + + +MyModel::MyModel(QObject *parent) + :QAbstractTableModel(parent) +{ +} + +//----------------------------------------------------------------- +int MyModel::rowCount(const QModelIndex & /*parent*/) const +{ + return ROWS; +} + +//----------------------------------------------------------------- +int MyModel::columnCount(const QModelIndex & /*parent*/) const +{ + return COLS; +} + +//----------------------------------------------------------------- +QVariant MyModel::data(const QModelIndex &index, int role) const +{ + if (role == Qt::DisplayRole) + { + return m_gridData[index.row()][index.column()]; + } + return QVariant(); +} + +//----------------------------------------------------------------- +//! [quoting mymodel_e] +bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role) +{ + if (role == Qt::EditRole) + { + //save value from editor to member m_gridData + m_gridData[index.row()][index.column()] = value.toString(); + //for presentation purposes only: build and emit a joined string + QString result; + for(int row= 0; row < ROWS; row++) + { + for(int col= 0; col < COLS; col++) + { + result += m_gridData[row][col] + " "; + } + } + emit editCompleted( result ); + } + return true; +} +//! [quoting mymodel_e] + +//----------------------------------------------------------------- +//! [quoting mymodel_f] +Qt::ItemFlags MyModel::flags(const QModelIndex & /*index*/) const +{ + return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ; +} +//! [quoting mymodel_f] diff --git a/examples/widgets/tutorials/modelview/5_edit/mymodel.h b/examples/widgets/tutorials/modelview/5_edit/mymodel.h new file mode 100644 index 0000000000..63ed4b8d46 --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit/mymodel.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** 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 MYMODEL_H +#define MYMODEL_H + +//! [Quoting ModelView Tutorial] +// mymodel.h +#include <QAbstractTableModel> +#include <QString> + +const int COLS= 3; +const int ROWS= 2; + + +class MyModel : public QAbstractTableModel +{ + Q_OBJECT +public: + MyModel(QObject *parent); + int rowCount(const QModelIndex &parent = QModelIndex()) const ; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole); + Qt::ItemFlags flags(const QModelIndex & index) const ; +private: + QString m_gridData[ROWS][COLS]; //holds text entered into QTableView +signals: + void editCompleted(const QString &); +}; +//! [Quoting ModelView Tutorial] + +#endif // MYMODEL_H diff --git a/examples/widgets/tutorials/modelview/6_treeview/6_treeview.desktop b/examples/widgets/tutorials/modelview/6_treeview/6_treeview.desktop new file mode 100644 index 0000000000..e0b872bf9c --- /dev/null +++ b/examples/widgets/tutorials/modelview/6_treeview/6_treeview.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=6 Model View +Exec=/opt/usr/bin/6_treeview +Icon=6_treeview +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/modelview/6_treeview/6_treeview.pro b/examples/widgets/tutorials/modelview/6_treeview/6_treeview.pro new file mode 100644 index 0000000000..21ea5d9552 --- /dev/null +++ b/examples/widgets/tutorials/modelview/6_treeview/6_treeview.pro @@ -0,0 +1,15 @@ +TARGET = mv_tree +TEMPLATE = app +SOURCES += main.cpp \ + mainwindow.cpp +HEADERS += mainwindow.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/6_treeview +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 6_treeview.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/6_treeview +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/modelview/6_treeview/main.cpp b/examples/widgets/tutorials/modelview/6_treeview/main.cpp new file mode 100644 index 0000000000..0e10352554 --- /dev/null +++ b/examples/widgets/tutorials/modelview/6_treeview/main.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** 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/QApplication> +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/examples/widgets/tutorials/modelview/6_treeview/mainwindow.cpp b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.cpp new file mode 100644 index 0000000000..aea18c11d6 --- /dev/null +++ b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.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$ +** +****************************************************************************/ + +//! [Quoting ModelView Tutorial] +// modelview.cpp +#include <QTreeView> +#include <QStandardItemModel> +#include <QStandardItem> +#include "mainwindow.h" + + +const int ROWS = 2; +const int COLUMNS = 3; + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + treeView = new QTreeView(this); + setCentralWidget(treeView); + standardModel = new QStandardItemModel ; + + QList<QStandardItem *> preparedRow =prepareRow("first", "second", "third"); + QStandardItem *item = standardModel->invisibleRootItem(); + // adding a row to the invisible root item produces a root element + item->appendRow(preparedRow); + + QList<QStandardItem *> secondRow =prepareRow("111", "222", "333"); + // adding a row to an item starts a subtree + preparedRow.first()->appendRow(secondRow); + + treeView->setModel(standardModel); + treeView->expandAll(); +} + +QList<QStandardItem *> MainWindow::prepareRow(const QString &first, + const QString &second, + const QString &third) +{ + QList<QStandardItem *> rowItems; + rowItems << new QStandardItem(first); + rowItems << new QStandardItem(second); + rowItems << new QStandardItem(third); + return rowItems; +} +//! [Quoting ModelView Tutorial] diff --git a/examples/widgets/tutorials/modelview/6_treeview/mainwindow.h b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.h new file mode 100644 index 0000000000..aab299ca6f --- /dev/null +++ b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** 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/QMainWindow> + +QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code +class QTreeView; //forward declarations +class QStandardItemModel; +class QStandardItem; +QT_END_NAMESPACE + + +class MainWindow : public QMainWindow +{ + Q_OBJECT +private: + QTreeView *treeView; + QStandardItemModel *standardModel; + QList<QStandardItem *> prepareRow( const QString &first, + const QString &second, + const QString &third ); +public: + MainWindow(QWidget *parent = 0); +}; + +#endif // MAINWINDOW_H diff --git a/examples/widgets/tutorials/modelview/7_selections/7_selections.desktop b/examples/widgets/tutorials/modelview/7_selections/7_selections.desktop new file mode 100644 index 0000000000..afba38361f --- /dev/null +++ b/examples/widgets/tutorials/modelview/7_selections/7_selections.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=7 Model View +Exec=/opt/usr/bin/7_selections +Icon=7_selections +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/modelview/7_selections/7_selections.pro b/examples/widgets/tutorials/modelview/7_selections/7_selections.pro new file mode 100644 index 0000000000..c1e4b1553d --- /dev/null +++ b/examples/widgets/tutorials/modelview/7_selections/7_selections.pro @@ -0,0 +1,15 @@ +TARGET = mv_selections +TEMPLATE = app +SOURCES += main.cpp \ + mainwindow.cpp +HEADERS += mainwindow.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/7_selections +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 7_selections.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview/7_selections +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/modelview/7_selections/main.cpp b/examples/widgets/tutorials/modelview/7_selections/main.cpp new file mode 100644 index 0000000000..0e10352554 --- /dev/null +++ b/examples/widgets/tutorials/modelview/7_selections/main.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** 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/QApplication> +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp b/examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp new file mode 100644 index 0000000000..99ecaf9da6 --- /dev/null +++ b/examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +//! [quoting modelview_a] +#include <QTreeView> +#include <QStandardItemModel> +#include <QItemSelectionModel> +#include "mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + treeView = new QTreeView(this); + setCentralWidget(treeView); + standardModel = new QStandardItemModel ; + QStandardItem *rootNode = standardModel->invisibleRootItem(); + + + //defining a couple of items + QStandardItem *americaItem = new QStandardItem("America"); + QStandardItem *mexicoItem = new QStandardItem("Canada"); + QStandardItem *usaItem = new QStandardItem("USA"); + QStandardItem *bostonItem = new QStandardItem("Boston"); + QStandardItem *europeItem = new QStandardItem("Europe"); + QStandardItem *italyItem = new QStandardItem("Italy"); + QStandardItem *romeItem = new QStandardItem("Rome"); + QStandardItem *veronaItem = new QStandardItem("Verona"); + + //building up the hierarchy + rootNode-> appendRow(americaItem); + rootNode-> appendRow(europeItem); + americaItem-> appendRow(mexicoItem); + americaItem-> appendRow(usaItem); + usaItem-> appendRow(bostonItem); + europeItem-> appendRow(italyItem); + italyItem-> appendRow(romeItem); + italyItem-> appendRow(veronaItem); + + //register the model + treeView->setModel(standardModel); + treeView->expandAll(); + + //selection changes shall trigger a slot + QItemSelectionModel *selectionModel= treeView->selectionModel(); + connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)), + this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &))); +} +//! [quoting modelview_a] + +//------------------------------------------------------------------------------------ + +//! [quoting modelview_b] +void MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, const QItemSelection & /*oldSelection*/) +{ + //get the text of the selected item + const QModelIndex index = treeView->selectionModel()->currentIndex(); + QString selectedText = index.data(Qt::DisplayRole).toString(); + //find out the hierarchy level of the selected item + int hierarchyLevel=1; + QModelIndex seekRoot = index; + while(seekRoot.parent() != QModelIndex()) + { + seekRoot = seekRoot.parent(); + hierarchyLevel++; + } + QString showString = QString("%1, Level %2").arg(selectedText) + .arg(hierarchyLevel); + setWindowTitle(showString); +} +//! [quoting modelview_b] + + diff --git a/examples/widgets/tutorials/modelview/7_selections/mainwindow.h b/examples/widgets/tutorials/modelview/7_selections/mainwindow.h new file mode 100644 index 0000000000..076d56d179 --- /dev/null +++ b/examples/widgets/tutorials/modelview/7_selections/mainwindow.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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/QMainWindow> + +QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code +class QTreeView; //forward declarations +class QStandardItemModel; +class QItemSelection; +QT_END_NAMESPACE + + +class MainWindow : public QMainWindow +{ + Q_OBJECT +private: + QTreeView *treeView; + QStandardItemModel *standardModel; +private slots: + void selectionChangedSlot(const QItemSelection & newSelection, const QItemSelection & oldSelection); +public: + MainWindow(QWidget *parent = 0); +}; + +#endif // MAINWINDOW_H diff --git a/examples/widgets/tutorials/modelview/modelview.pro b/examples/widgets/tutorials/modelview/modelview.pro new file mode 100644 index 0000000000..4a7e4a344e --- /dev/null +++ b/examples/widgets/tutorials/modelview/modelview.pro @@ -0,0 +1,16 @@ +TEMPLATE = subdirs +SUBDIRS = 1_readonly \ + 2_formatting \ + 3_changingmodel \ + 4_headers \ + 5_edit \ + 6_treeview \ + 7_selections + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS modelview.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/modelview +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tutorials/threads/clock/clock.pro b/examples/widgets/tutorials/threads/clock/clock.pro new file mode 100644 index 0000000000..b07c84a627 --- /dev/null +++ b/examples/widgets/tutorials/threads/clock/clock.pro @@ -0,0 +1,14 @@ +CONFIG += console +TEMPLATE = app +SOURCES += main.cpp \ + clockthread.cpp +HEADERS += clockthread.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/clock +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS clock.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/clock +INSTALLS += target sources + + +QT += widgets diff --git a/examples/widgets/tutorials/threads/clock/clockthread.cpp b/examples/widgets/tutorials/threads/clock/clockthread.cpp new file mode 100644 index 0000000000..dccf4446ef --- /dev/null +++ b/examples/widgets/tutorials/threads/clock/clockthread.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** 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 "clockthread.h" + + //This class starts another thread where it emits a signal for every new second. + +//! [1] +// clock/clockthread.cpp +void ClockThread::run() +{ + QTimer timer; + connect(&timer, SIGNAL(timeout()), this, SLOT(timerHit()), Qt::DirectConnection); + timer.setInterval(10); + timer.start(); // puts one event in the threads event queue + exec(); + timer.stop(); +} + +void ClockThread::timerHit() +{ + QString newTime= QDateTime::currentDateTime().toString("ddd MMMM d yy, hh:mm:ss"); + if(m_lastTime != newTime ){ + m_lastTime = newTime; + emit sendTime(newTime) ; + } +} +//! [1] diff --git a/examples/widgets/tutorials/threads/clock/clockthread.h b/examples/widgets/tutorials/threads/clock/clockthread.h new file mode 100644 index 0000000000..e33b38fca4 --- /dev/null +++ b/examples/widgets/tutorials/threads/clock/clockthread.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** 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 CLOCKTHREAD_H +#define CLOCKTHREAD_H + +#include <QString> +#include <QThread> + + + +//! [1] +// clock/clockthread.h +class ClockThread : public QThread +{ + Q_OBJECT +signals: + void sendTime(QString time); +private: + void run(); + QString m_lastTime; +private slots: + void timerHit(); + +}; +//! [1] +#endif // CLOCKTHREAD_H
\ No newline at end of file diff --git a/examples/widgets/tutorials/threads/clock/main.cpp b/examples/widgets/tutorials/threads/clock/main.cpp new file mode 100644 index 0000000000..78c27440d6 --- /dev/null +++ b/examples/widgets/tutorials/threads/clock/main.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** 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 "clockthread.h" + +//A clock that does time formatting in another thread + +//! [1] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + // build gui + QWidget widget; + QLabel *label = new QLabel; + QHBoxLayout *layout = new QHBoxLayout(&widget); + layout->addWidget(label); + widget.setWindowTitle("clock"); + + //instantiate thread object + ClockThread clockThread; + QObject::connect(&clockThread, SIGNAL(sendTime(QString)), label, SLOT(setText(QString)), Qt::QueuedConnection); + clockThread.start(); + widget.show(); + app.exec(); + clockThread.quit(); + clockThread.wait(); + return 0; +} +//! [1] diff --git a/examples/widgets/tutorials/threads/helloconcurrent/helloconcurrent.cpp b/examples/widgets/tutorials/threads/helloconcurrent/helloconcurrent.cpp new file mode 100644 index 0000000000..02aed98c1d --- /dev/null +++ b/examples/widgets/tutorials/threads/helloconcurrent/helloconcurrent.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** 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 <QtCore> +#include <QtConcurrent> + +/* + says hello from main thread and secondary thread using QtConcurrent +*/ + +//! [1] +// helloconcurrent/main.cpp +void hello() +{ + qDebug() << "Hello from thread " << QThread::currentThread(); +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + QFuture<void> future = QtConcurrent::run(hello); + qDebug() << "hello from GUI thread " << QThread::currentThread(); + future.waitForFinished(); + return 0; +} +//! [1] diff --git a/examples/widgets/tutorials/threads/helloconcurrent/helloconcurrent.pro b/examples/widgets/tutorials/threads/helloconcurrent/helloconcurrent.pro new file mode 100644 index 0000000000..1941666755 --- /dev/null +++ b/examples/widgets/tutorials/threads/helloconcurrent/helloconcurrent.pro @@ -0,0 +1,16 @@ +QT -= gui +QT += concurrent + +CONFIG += console +CONFIG -= app_bundle +TEMPLATE = app +SOURCES += helloconcurrent.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/helloconcurrent +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS helloconcurrent.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/helloconcurrent +INSTALLS += target sources + + + diff --git a/examples/widgets/tutorials/threads/hellothread/hellothread.cpp b/examples/widgets/tutorials/threads/hellothread/hellothread.cpp new file mode 100644 index 0000000000..5c6bbac1ed --- /dev/null +++ b/examples/widgets/tutorials/threads/hellothread/hellothread.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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 <QDebug> +#include "hellothread.h" +/* + * demonstrates use of QThread, says hello in another thread and terminates + */ + +//! [1] +// hellothread/hellothread.cpp +void HelloThread::run() +{ + qDebug() << "hello from worker thread " << thread()->currentThreadId(); +} +//! [1] diff --git a/examples/widgets/tutorials/threads/hellothread/hellothread.h b/examples/widgets/tutorials/threads/hellothread/hellothread.h new file mode 100644 index 0000000000..9b0a0199fc --- /dev/null +++ b/examples/widgets/tutorials/threads/hellothread/hellothread.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 HELLOTHREAD_H +#define HELLOTHREAD_H + +#include <QThread> +//! [1] +// hellothread/hellothread.h +class HelloThread : public QThread +{ + Q_OBJECT +private: + void run(); +}; +//! [1] +#endif // HELLOTHREAD_H diff --git a/examples/widgets/tutorials/threads/hellothread/hellothread.pro b/examples/widgets/tutorials/threads/hellothread/hellothread.pro new file mode 100644 index 0000000000..877bb10c39 --- /dev/null +++ b/examples/widgets/tutorials/threads/hellothread/hellothread.pro @@ -0,0 +1,16 @@ +QT -= gui + +CONFIG += console +CONFIG -= app_bundle +TEMPLATE = app +SOURCES += main.cpp \ + hellothread.cpp +HEADERS += hellothread.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/hellothread +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS hellothread.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/hellothread +INSTALLS += target sources + + diff --git a/examples/widgets/tutorials/threads/hellothread/main.cpp b/examples/widgets/tutorials/threads/hellothread/main.cpp new file mode 100644 index 0000000000..2f89b5394f --- /dev/null +++ b/examples/widgets/tutorials/threads/hellothread/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 <QtCore> +#include "hellothread.h" + +//! [1] +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + HelloThread thread; + thread.start(); + qDebug() << "hello from GUI thread " << app.thread()->currentThreadId(); + thread.wait(); // do not exit before the thread is completed! + return 0; +} +//! [1] diff --git a/examples/widgets/tutorials/threads/hellothreadpool/hellothreadpool.cpp b/examples/widgets/tutorials/threads/hellothreadpool/hellothreadpool.cpp new file mode 100644 index 0000000000..9cf1351b54 --- /dev/null +++ b/examples/widgets/tutorials/threads/hellothreadpool/hellothreadpool.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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 <QtCore> +// A hello world program to demonstrate the use of the global thread pool + +//! [1] +// hellothreadpool/main.cpp +class Work : public QRunnable +{ +public: + void run() + { + qDebug() << "Hello from thread " << QThread::currentThread(); + } +}; + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + Work work; + work.setAutoDelete(false); + QThreadPool *threadPool = QThreadPool::globalInstance(); + threadPool->start(&work); + qDebug() << "hello from GUI thread " << QThread::currentThread(); + threadPool->waitForDone(); + return 0; +} +//! [1] diff --git a/examples/widgets/tutorials/threads/hellothreadpool/hellothreadpool.pro b/examples/widgets/tutorials/threads/hellothreadpool/hellothreadpool.pro new file mode 100644 index 0000000000..8539f38f45 --- /dev/null +++ b/examples/widgets/tutorials/threads/hellothreadpool/hellothreadpool.pro @@ -0,0 +1,16 @@ +QT -= gui + +CONFIG += console +CONFIG -= app_bundle +TEMPLATE = app +SOURCES += hellothreadpool.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/hellothreadpool +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS hellothreadpool.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/hellothreadpool +INSTALLS += target sources + + + + diff --git a/examples/widgets/tutorials/threads/movedobject/main.cpp b/examples/widgets/tutorials/threads/movedobject/main.cpp new file mode 100644 index 0000000000..7a8da3a3df --- /dev/null +++ b/examples/widgets/tutorials/threads/movedobject/main.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** 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 <QtCore> +#include "workerobject.h" +#include "thread.h" + +/* + * moves a class derived from QObject (WorkerObject) to another thread + * and calls methods over thread boundaries. + */ + +//![1] +// movedobject/main.cpp +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + Thread thread; + qDebug() << "main thread ID: " << app.thread()->currentThreadId(); + WorkerObject *worker = new WorkerObject; + thread.launchWorker(worker); + QMetaObject::invokeMethod(worker, "doWork", Qt::QueuedConnection); + QMetaObject::invokeMethod(worker, "startPolling", Qt::QueuedConnection, Q_ARG(int, 500)); + //let application produce output for 3 seconds and quit + QTimer::singleShot(3000, &app, SLOT(quit())); + app.exec(); + thread.stop(); + thread.wait(); + delete worker; + return 0; +} +//![1] diff --git a/examples/widgets/tutorials/threads/movedobject/movedobject.pro b/examples/widgets/tutorials/threads/movedobject/movedobject.pro new file mode 100644 index 0000000000..c7fd608c7e --- /dev/null +++ b/examples/widgets/tutorials/threads/movedobject/movedobject.pro @@ -0,0 +1,18 @@ +CONFIG += console +CONFIG -= app_bundle +TEMPLATE = app +SOURCES += main.cpp \ + workerobject.cpp \ + thread.cpp + +HEADERS += \ + workerobject.h \ + thread.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/movedobject +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS movedobject.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/threads/movedobject +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tutorials/threads/movedobject/thread.cpp b/examples/widgets/tutorials/threads/movedobject/thread.cpp new file mode 100644 index 0000000000..a53351e491 --- /dev/null +++ b/examples/widgets/tutorials/threads/movedobject/thread.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** 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 "thread.h" + +/* + * QThread derived class with additional capability to move a QObject to the + * new thread, to stop the thread and move the QObject back to the thread where + *it came from. + */ + +Thread::Thread( QObject *parent) + : QThread (parent) +{ + //we need a class that receives signals from other threads and emits a signal in response + shutDownHelper=new QSignalMapper; + shutDownHelper->setMapping(this,0); + connect(this, SIGNAL(started()), this, SLOT(setReadyStatus() ), Qt::DirectConnection); + connect(this, SIGNAL(aboutToStop()), shutDownHelper, SLOT(map()) ); +} + +//------------------------------------------------------ +Thread::~Thread() +{ + delete shutDownHelper; +} + +//------------------------------------------------------ +// starts thread, moves worker to this thread and blocks +void Thread::launchWorker(QObject *worker) +{ + this->worker = worker; + start(); + worker->moveToThread(this); + shutDownHelper->moveToThread(this); + connect(shutDownHelper, SIGNAL(mapped(int) ), this, SLOT(stopExecutor()), Qt::DirectConnection ); + mutex.lock(); + waitCondition.wait(&mutex); +} + +//------------------------------------------------------ +// puts a command to stop processing in the event queue of worker thread +void Thread::stop() +{ + emit aboutToStop(); +} + +//------------------------------------------------------ + +// methods above this line should be called in gui thread context +// methods below this line are private and will be run in secondary thread context + +//------------------------------------------------------ +void Thread::stopExecutor() //secondary thread context +{ + exit(); +} + +//------------------------------------------------------ +void Thread::setReadyStatus() +{ + waitCondition.wakeAll(); +} diff --git a/examples/widgets/tutorials/threads/movedobject/thread.h b/examples/widgets/tutorials/threads/movedobject/thread.h new file mode 100644 index 0000000000..77a76b7bb5 --- /dev/null +++ b/examples/widgets/tutorials/threads/movedobject/thread.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** 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 THREAD_H +#define THREAD_H + +#include <QtCore> + +class Thread :public QThread +{ + Q_OBJECT +public: + Thread( QObject *parent=0); + ~Thread(); + void stop(); + void launchWorker(QObject *worker); +private: + QObject *worker; + QSignalMapper *shutDownHelper; + QWaitCondition waitCondition; + QMutex mutex; +private slots: + void stopExecutor(); + void setReadyStatus(); +signals: + void aboutToStop(); +}; + +#endif // THREAD_H diff --git a/examples/widgets/tutorials/threads/movedobject/workerobject.cpp b/examples/widgets/tutorials/threads/movedobject/workerobject.cpp new file mode 100644 index 0000000000..5f63086b80 --- /dev/null +++ b/examples/widgets/tutorials/threads/movedobject/workerobject.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** 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 <QtCore> +#include "workerobject.h" + +/* + * represents an object that lives in another thread where it polls a resource + * and communicates with the gui thread + */ + +WorkerObject::WorkerObject(QObject *parent) + : QObject(parent) +{ + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(poll())); +} + +//--------------------------------------------------------------- +void WorkerObject::doWork() +{ + + qDebug() << "doing work in thread " << thread()->currentThreadId() ; +} + +//--------------------------------------------------------------- +WorkerObject::~WorkerObject() +{ + qDebug() << "destruction WorkerObject in thread " << thread()->currentThreadId(); +} + +//--------------------------------------------------------------- +void WorkerObject::startPolling(int milliseconds) +{ + count=0; + timer->start(milliseconds); +} + +//--------------------------------------------------------------- +void WorkerObject::stopPolling() +{ + timer->stop(); +} + +//--------------------------------------------------------------- +void WorkerObject::poll() +{ + qDebug() << QString("timer hit %1").arg(count); + count++; +} + diff --git a/examples/widgets/tutorials/threads/movedobject/workerobject.h b/examples/widgets/tutorials/threads/movedobject/workerobject.h new file mode 100644 index 0000000000..e19ab01bfb --- /dev/null +++ b/examples/widgets/tutorials/threads/movedobject/workerobject.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** 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 WORKEROBJECT_H +#define WORKEROBJECT_H + +#include <QtCore> + +class WorkerObject : public QObject +{ + Q_OBJECT +public: + explicit WorkerObject(QObject *parent = 0); + ~WorkerObject(); +public slots: + void doWork(); + void startPolling(int milliseconds); + void stopPolling(); +private slots: + void poll(); +private: + QTimer *timer; + int count; +}; + +#endif // WORKEROBJECT_H diff --git a/examples/widgets/tutorials/threads/threads.pro b/examples/widgets/tutorials/threads/threads.pro new file mode 100644 index 0000000000..a95eccaaa0 --- /dev/null +++ b/examples/widgets/tutorials/threads/threads.pro @@ -0,0 +1,10 @@ +TEMPLATE = subdirs + +SUBDIRS = hellothread \ + hellothreadpool \ + clock \ + movedobject + +contains(QT_CONFIG, concurrent): SUBDIRS += helloconcurrent + +QT += widgets diff --git a/examples/widgets/tutorials/tutorials.pro b/examples/widgets/tutorials/tutorials.pro new file mode 100644 index 0000000000..cb57002082 --- /dev/null +++ b/examples/widgets/tutorials/tutorials.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS += addressbook-fr threads addressbook widgets modelview gettingStarted diff --git a/examples/widgets/tutorials/widgets/childwidget/childwidget.desktop b/examples/widgets/tutorials/widgets/childwidget/childwidget.desktop new file mode 100644 index 0000000000..81bc7c16f1 --- /dev/null +++ b/examples/widgets/tutorials/widgets/childwidget/childwidget.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Child Widgets +Exec=/opt/usr/bin/childwidget +Icon=childwidget +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/widgets/childwidget/childwidget.pro b/examples/widgets/tutorials/widgets/childwidget/childwidget.pro new file mode 100644 index 0000000000..eda5ba83a0 --- /dev/null +++ b/examples/widgets/tutorials/widgets/childwidget/childwidget.pro @@ -0,0 +1,11 @@ +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/childwidget +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS childwidget.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/childwidget +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/widgets/childwidget/main.cpp b/examples/widgets/tutorials/widgets/childwidget/main.cpp new file mode 100644 index 0000000000..e00e7d9fba --- /dev/null +++ b/examples/widgets/tutorials/widgets/childwidget/main.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +//! [main program] +#include <QtWidgets> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QWidget window; + window.resize(320, 240); + window.setWindowTitle(QApplication::translate("childwidget", "Child widget")); + window.show(); + +//! [create, position and show] + QPushButton *button = new QPushButton( + QApplication::translate("childwidget", "Press me"), &window); + button->move(100, 100); + button->show(); +//! [create, position and show] + return app.exec(); +} +//! [main program] diff --git a/examples/widgets/tutorials/widgets/nestedlayouts/main.cpp b/examples/widgets/tutorials/widgets/nestedlayouts/main.cpp new file mode 100644 index 0000000000..21e3d754a5 --- /dev/null +++ b/examples/widgets/tutorials/widgets/nestedlayouts/main.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +//! [main program] +//! [first part] +#include <QtWidgets> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QWidget window; + + QLabel *queryLabel = new QLabel( + QApplication::translate("nestedlayouts", "Query:")); + QLineEdit *queryEdit = new QLineEdit(); + QTableView *resultView = new QTableView(); + + QHBoxLayout *queryLayout = new QHBoxLayout(); + queryLayout->addWidget(queryLabel); + queryLayout->addWidget(queryEdit); + + QVBoxLayout *mainLayout = new QVBoxLayout(); + mainLayout->addLayout(queryLayout); + mainLayout->addWidget(resultView); + window.setLayout(mainLayout); + + // Set up the model and configure the view... +//! [first part] + +//! [set up the model] + QStandardItemModel model; + model.setHorizontalHeaderLabels( + QStringList() << QApplication::translate("nestedlayouts", "Name") + << QApplication::translate("nestedlayouts", "Office")); + + QList<QStringList> rows = QList<QStringList>() + << (QStringList() << "Verne Nilsen" << "123") + << (QStringList() << "Carlos Tang" << "77") + << (QStringList() << "Bronwyn Hawcroft" << "119") + << (QStringList() << "Alessandro Hanssen" << "32") + << (QStringList() << "Andrew John Bakken" << "54") + << (QStringList() << "Vanessa Weatherley" << "85") + << (QStringList() << "Rebecca Dickens" << "17") + << (QStringList() << "David Bradley" << "42") + << (QStringList() << "Knut Walters" << "25") + << (QStringList() << "Andrea Jones" << "34"); + + foreach (QStringList row, rows) { + QList<QStandardItem *> items; + foreach (QString text, row) + items.append(new QStandardItem(text)); + model.appendRow(items); + } + + resultView->setModel(&model); + resultView->verticalHeader()->hide(); + resultView->horizontalHeader()->setStretchLastSection(true); +//! [set up the model] +//! [last part] + window.setWindowTitle( + QApplication::translate("nestedlayouts", "Nested layouts")); + window.show(); + return app.exec(); +} +//! [last part] +//! [main program] diff --git a/examples/widgets/tutorials/widgets/nestedlayouts/nestedlayouts.desktop b/examples/widgets/tutorials/widgets/nestedlayouts/nestedlayouts.desktop new file mode 100644 index 0000000000..9ff737d079 --- /dev/null +++ b/examples/widgets/tutorials/widgets/nestedlayouts/nestedlayouts.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Nested Layouts +Exec=/opt/usr/bin/nestedlayouts +Icon=nestedlayouts +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/widgets/nestedlayouts/nestedlayouts.pro b/examples/widgets/tutorials/widgets/nestedlayouts/nestedlayouts.pro new file mode 100644 index 0000000000..60b9498868 --- /dev/null +++ b/examples/widgets/tutorials/widgets/nestedlayouts/nestedlayouts.pro @@ -0,0 +1,11 @@ +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/nestedlayouts +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS nestedlayouts.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/nestedlayouts +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/widgets/toplevel/main.cpp b/examples/widgets/tutorials/widgets/toplevel/main.cpp new file mode 100644 index 0000000000..8f6f8e8042 --- /dev/null +++ b/examples/widgets/tutorials/widgets/toplevel/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +//! [main program] +#include <QtWidgets> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); +//! [create, resize and show] + QWidget window; + window.resize(320, 240); + window.show(); +//! [create, resize and show] + window.setWindowTitle( + QApplication::translate("toplevel", "Top-level widget")); + return app.exec(); +} +//! [main program] diff --git a/examples/widgets/tutorials/widgets/toplevel/toplevel.desktop b/examples/widgets/tutorials/widgets/toplevel/toplevel.desktop new file mode 100644 index 0000000000..5626297c67 --- /dev/null +++ b/examples/widgets/tutorials/widgets/toplevel/toplevel.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Creating a Window +Exec=/opt/usr/bin/toplevel +Icon=toplevel +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/widgets/toplevel/toplevel.pro b/examples/widgets/tutorials/widgets/toplevel/toplevel.pro new file mode 100644 index 0000000000..cbee1b9bab --- /dev/null +++ b/examples/widgets/tutorials/widgets/toplevel/toplevel.pro @@ -0,0 +1,11 @@ +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/toplevel +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS toplevel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/toplevel +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tutorials/widgets/widgets.pro b/examples/widgets/tutorials/widgets/widgets.pro new file mode 100644 index 0000000000..3a6e065ffc --- /dev/null +++ b/examples/widgets/tutorials/widgets/widgets.pro @@ -0,0 +1,9 @@ +TEMPLATE = subdirs +SUBDIRS = toplevel childwidget windowlayout nestedlayouts + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS widgets.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets +INSTALLS += target sources +QT += widgets diff --git a/examples/widgets/tutorials/widgets/windowlayout/main.cpp b/examples/widgets/tutorials/widgets/windowlayout/main.cpp new file mode 100644 index 0000000000..87c2a9355e --- /dev/null +++ b/examples/widgets/tutorials/widgets/windowlayout/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +//! [main program] +#include <QtWidgets> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QWidget window; +//! [create, lay out widgets and show] + QLabel *label = new QLabel(QApplication::translate("windowlayout", "Name:")); + QLineEdit *lineEdit = new QLineEdit(); + + QHBoxLayout *layout = new QHBoxLayout(); + layout->addWidget(label); + layout->addWidget(lineEdit); + window.setLayout(layout); +//! [create, lay out widgets and show] + window.setWindowTitle( + QApplication::translate("windowlayout", "Window layout")); + window.show(); + return app.exec(); +} +//! [main program] diff --git a/examples/widgets/tutorials/widgets/windowlayout/windowlayout.desktop b/examples/widgets/tutorials/widgets/windowlayout/windowlayout.desktop new file mode 100644 index 0000000000..4a007952fd --- /dev/null +++ b/examples/widgets/tutorials/widgets/windowlayout/windowlayout.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Using Layouts +Exec=/opt/usr/bin/windowlayout +Icon=windowlayout +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tutorials/widgets/windowlayout/windowlayout.pro b/examples/widgets/tutorials/widgets/windowlayout/windowlayout.pro new file mode 100644 index 0000000000..39a32f5f8c --- /dev/null +++ b/examples/widgets/tutorials/widgets/windowlayout/windowlayout.pro @@ -0,0 +1,11 @@ +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/windowlayout +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS windowlayout.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tutorials/widgets/windowlayout +INSTALLS += target sources +QT += widgets + + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/widgets.pro b/examples/widgets/widgets.pro index 8c68b3721e..7fd2d62154 100644 --- a/examples/widgets/widgets.pro +++ b/examples/widgets/widgets.pro @@ -15,6 +15,7 @@ SUBDIRS = \ richtext \ scroller \ statemachine \ + tutorials \ widgets contains(DEFINES, QT_NO_CURSOR): SUBDIRS -= mainwindows |