diff options
Diffstat (limited to 'examples/sql')
48 files changed, 3383 insertions, 0 deletions
diff --git a/examples/sql/README b/examples/sql/README new file mode 100644 index 0000000..56b4b3e --- /dev/null +++ b/examples/sql/README @@ -0,0 +1,40 @@ +Qt provides extensive database interoperability, with support for products +from both open source and proprietary vendors. + +SQL support is integrated with Qt's model/view architecture, making it easier +to provide GUI integration for your database applications. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/sql/cachedtable/cachedtable.pro b/examples/sql/cachedtable/cachedtable.pro new file mode 100644 index 0000000..89e97ae --- /dev/null +++ b/examples/sql/cachedtable/cachedtable.pro @@ -0,0 +1,13 @@ +HEADERS = ../connection.h \ + tableeditor.h +SOURCES = main.cpp \ + tableeditor.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/cachedtable +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS cachedtable.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/cachedtable +INSTALLS += target sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/sql/cachedtable/main.cpp b/examples/sql/cachedtable/main.cpp new file mode 100644 index 0000000..47e3b9f --- /dev/null +++ b/examples/sql/cachedtable/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "../connection.h" +#include "tableeditor.h" + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + + TableEditor editor("person"); + editor.show(); + return editor.exec(); +} +//! [0] diff --git a/examples/sql/cachedtable/tableeditor.cpp b/examples/sql/cachedtable/tableeditor.cpp new file mode 100644 index 0000000..33ddd54 --- /dev/null +++ b/examples/sql/cachedtable/tableeditor.cpp @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtSql> + +#include "tableeditor.h" + +//! [0] +TableEditor::TableEditor(const QString &tableName, QWidget *parent) + : QDialog(parent) +{ + model = new QSqlTableModel(this); + model->setTable(tableName); + model->setEditStrategy(QSqlTableModel::OnManualSubmit); + model->select(); + + model->setHeaderData(0, Qt::Horizontal, tr("ID")); + model->setHeaderData(1, Qt::Horizontal, tr("First name")); + model->setHeaderData(2, Qt::Horizontal, tr("Last name")); + +//! [0] //! [1] + QTableView *view = new QTableView; + view->setModel(model); +//! [1] + +//! [2] + submitButton = new QPushButton(tr("Submit")); + submitButton->setDefault(true); + revertButton = new QPushButton(tr("&Revert")); + quitButton = new QPushButton(tr("Quit")); + + buttonBox = new QDialogButtonBox(Qt::Vertical); + buttonBox->addButton(submitButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(revertButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); +//! [2] + +//! [3] + connect(submitButton, SIGNAL(clicked()), this, SLOT(submit())); + connect(revertButton, SIGNAL(clicked()), model, SLOT(revertAll())); + connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); +//! [3] + +//! [4] + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->addWidget(view); + mainLayout->addWidget(buttonBox); + setLayout(mainLayout); + + setWindowTitle(tr("Cached Table")); +} +//! [4] + +//! [5] +void TableEditor::submit() +{ + model->database().transaction(); + if (model->submitAll()) { + model->database().commit(); + } else { + model->database().rollback(); + QMessageBox::warning(this, tr("Cached Table"), + tr("The database reported an error: %1") + .arg(model->lastError().text())); + } +} +//! [5] diff --git a/examples/sql/cachedtable/tableeditor.h b/examples/sql/cachedtable/tableeditor.h new file mode 100644 index 0000000..3485fd3 --- /dev/null +++ b/examples/sql/cachedtable/tableeditor.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TABLEEDITOR_H +#define TABLEEDITOR_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QDialogButtonBox; +class QPushButton; +class QSqlTableModel; +QT_END_NAMESPACE + +//! [0] +class TableEditor : public QDialog +{ + Q_OBJECT + +public: + TableEditor(const QString &tableName, QWidget *parent = 0); + +private slots: + void submit(); + +private: + QPushButton *submitButton; + QPushButton *revertButton; + QPushButton *quitButton; + QDialogButtonBox *buttonBox; + QSqlTableModel *model; +}; +//! [0] + +#endif diff --git a/examples/sql/connection.h b/examples/sql/connection.h new file mode 100644 index 0000000..d4b77ef --- /dev/null +++ b/examples/sql/connection.h @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CONNECTION_H +#define CONNECTION_H + +#include <QMessageBox> +#include <QSqlDatabase> +#include <QSqlError> +#include <QSqlQuery> + +/* + This file defines a helper function to open a connection to an + in-memory SQLITE database and to create a test table. + + If you want to use another database, simply modify the code + below. All the examples in this directory use this function to + connect to a database. +*/ +//! [0] +static bool createConnection() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + QMessageBox::critical(0, qApp->tr("Cannot open database"), + qApp->tr("Unable to establish a database connection.\n" + "This example needs SQLite support. Please read " + "the Qt SQL driver documentation for information how " + "to build it.\n\n" + "Click Cancel to exit."), QMessageBox::Cancel); + return false; + } + + QSqlQuery query; + query.exec("create table person (id int primary key, " + "firstname varchar(20), lastname varchar(20))"); + query.exec("insert into person values(101, 'Danny', 'Young')"); + query.exec("insert into person values(102, 'Christine', 'Holand')"); + query.exec("insert into person values(103, 'Lars', 'Gordon')"); + query.exec("insert into person values(104, 'Roberto', 'Robitaille')"); + query.exec("insert into person values(105, 'Maria', 'Papadopoulos')"); + + query.exec("create table offices (id int primary key," + "imagefile int," + "location varchar(20)," + "country varchar(20)," + "description varchar(100))"); + query.exec("insert into offices " + "values(0, 0, 'Oslo', 'Norway'," + "'Oslo is home to more than 500 000 citizens and has a " + "lot to offer.It has been called \"The city with the big " + "heart\" and this is a nickname we are happy to live up to.')"); + query.exec("insert into offices " + "values(1, 1, 'Brisbane', 'Australia'," + "'Brisbane is the capital of Queensland, the Sunshine State, " + "where it is beautiful one day, perfect the next. " + "Brisbane is Australia''s 3rd largest city, being home " + "to almost 2 million people.')"); + query.exec("insert into offices " + "values(2, 2, 'Redwood City', 'US'," + "'You find Redwood City in the heart of the Bay Area " + "just north of Silicon Valley. The largest nearby city is " + "San Jose which is the third largest city in California " + "and the 10th largest in the US.')"); + query.exec("insert into offices " + "values(3, 3, 'Berlin', 'Germany'," + "'Berlin, the capital of Germany is dynamic, cosmopolitan " + "and creative, allowing for every kind of lifestyle. " + "East meets West in the metropolis at the heart of a " + "changing Europe.')"); + query.exec("insert into offices " + "values(4, 4, 'Munich', 'Germany'," + "'Several technology companies are represented in Munich, " + "and the city is often called the \"Bavarian Silicon Valley\". " + "The exciting city is also filled with culture, " + "art and music. ')"); + query.exec("insert into offices " + "values(5, 5, 'Beijing', 'China'," + "'Beijing as a capital city has more than 3000 years of " + "history. Today the city counts 12 million citizens, and " + "is the political, economic and cultural centre of China.')"); + + query.exec("create table images (locationid int, file varchar(20))"); + query.exec("insert into images values(0, 'images/oslo.png')"); + query.exec("insert into images values(1, 'images/brisbane.png')"); + query.exec("insert into images values(2, 'images/redwood.png')"); + query.exec("insert into images values(3, 'images/berlin.png')"); + query.exec("insert into images values(4, 'images/munich.png')"); + query.exec("insert into images values(5, 'images/beijing.png')"); + + + + return true; +} +//! [0] + +#endif diff --git a/examples/sql/drilldown/drilldown.pro b/examples/sql/drilldown/drilldown.pro new file mode 100644 index 0000000..7907bd2 --- /dev/null +++ b/examples/sql/drilldown/drilldown.pro @@ -0,0 +1,20 @@ +HEADERS = ../connection.h \ + imageitem.h \ + informationwindow.h \ + view.h +RESOURCES = drilldown.qrc +SOURCES = imageitem.cpp \ + informationwindow.cpp \ + main.cpp \ + view.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/drilldown +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS drilldown.pro *.png *.jpg images +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/drilldown +INSTALLS += target sources + +symbian:TARGET.UID3 = 0xA000C612 + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/sql/drilldown/drilldown.qrc b/examples/sql/drilldown/drilldown.qrc new file mode 100644 index 0000000..d2fb122 --- /dev/null +++ b/examples/sql/drilldown/drilldown.qrc @@ -0,0 +1,11 @@ +<!DOCTYPE RCC><RCC version="1.0"> + <qresource> + <file>images/oslo.png</file> + <file>images/brisbane.png</file> + <file>images/redwood.png</file> + <file>images/berlin.png</file> + <file>images/munich.png</file> + <file>images/beijing.png</file> + <file>logo.png</file> + </qresource> +</RCC> diff --git a/examples/sql/drilldown/imageitem.cpp b/examples/sql/drilldown/imageitem.cpp new file mode 100644 index 0000000..620afed --- /dev/null +++ b/examples/sql/drilldown/imageitem.cpp @@ -0,0 +1,124 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "imageitem.h" + +//! [0] +ImageItem::ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent, + QGraphicsScene *scene) + : QGraphicsPixmapItem(pixmap, parent, scene) +{ + recordId = id; + setAcceptsHoverEvents(true); + + timeLine.setDuration(150); + timeLine.setFrameRange(0, 150); + + connect(&timeLine, SIGNAL(frameChanged(int)), this, SLOT(setFrame(int))); + connect(&timeLine, SIGNAL(finished()), this, SLOT(updateItemPosition())); + + adjust(); +} +//! [0] + +//! [1] +void ImageItem::hoverEnterEvent(QGraphicsSceneHoverEvent * /*event*/) +{ + timeLine.setDirection(QTimeLine::Forward); + + if (z != 1.0) { + z = 1.0; + updateItemPosition(); + } + + if (timeLine.state() == QTimeLine::NotRunning) + timeLine.start(); +} +//! [1] + +//! [2] +void ImageItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * /*event*/) +{ + timeLine.setDirection(QTimeLine::Backward); + if (z != 0.0) + z = 0.0; + + if (timeLine.state() == QTimeLine::NotRunning) + timeLine.start(); +} +//! [2] + +//! [3] +void ImageItem::setFrame(int frame) +{ + adjust(); + QPointF center = boundingRect().center(); + + translate(center.x(), center.y()); + scale(1 + frame / 330.0, 1 + frame / 330.0); + translate(-center.x(), -center.y()); +} +//! [3] + +//! [4] +void ImageItem::adjust() +{ + QMatrix matrix; + matrix.scale(150/ boundingRect().width(), 120/ boundingRect().height()); + setMatrix(matrix); +} +//! [4] + +//! [5] +int ImageItem::id() +{ + return recordId; +} +//! [5] + +//! [6] +void ImageItem::updateItemPosition() +{ + setZValue(z); +} +//! [6] + + diff --git a/examples/sql/drilldown/imageitem.h b/examples/sql/drilldown/imageitem.h new file mode 100644 index 0000000..20c7811 --- /dev/null +++ b/examples/sql/drilldown/imageitem.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef IMAGEITEM_H +#define IMAGEITEM_H + +#include <QtCore> +#include <QtGui/QGraphicsPixmapItem> + +//! [0] +class ImageItem : public QObject, public QGraphicsPixmapItem +{ + Q_OBJECT + +public: + ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent = 0, + QGraphicsScene *scene = 0); + + void adjust(); + int id(); + +protected: + void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + +private slots: + void setFrame(int frame); + void updateItemPosition(); + +private: + QTimeLine timeLine; + int recordId; + double z; +}; +//! [0] + +#endif diff --git a/examples/sql/drilldown/images/beijing.png b/examples/sql/drilldown/images/beijing.png Binary files differnew file mode 100644 index 0000000..45df148 --- /dev/null +++ b/examples/sql/drilldown/images/beijing.png diff --git a/examples/sql/drilldown/images/berlin.png b/examples/sql/drilldown/images/berlin.png Binary files differnew file mode 100644 index 0000000..ead62f0 --- /dev/null +++ b/examples/sql/drilldown/images/berlin.png diff --git a/examples/sql/drilldown/images/brisbane.png b/examples/sql/drilldown/images/brisbane.png Binary files differnew file mode 100644 index 0000000..04fff57 --- /dev/null +++ b/examples/sql/drilldown/images/brisbane.png diff --git a/examples/sql/drilldown/images/munich.png b/examples/sql/drilldown/images/munich.png Binary files differnew file mode 100644 index 0000000..741eb77 --- /dev/null +++ b/examples/sql/drilldown/images/munich.png diff --git a/examples/sql/drilldown/images/oslo.png b/examples/sql/drilldown/images/oslo.png Binary files differnew file mode 100644 index 0000000..02fa25e --- /dev/null +++ b/examples/sql/drilldown/images/oslo.png diff --git a/examples/sql/drilldown/images/redwood.png b/examples/sql/drilldown/images/redwood.png Binary files differnew file mode 100644 index 0000000..f39ac06 --- /dev/null +++ b/examples/sql/drilldown/images/redwood.png diff --git a/examples/sql/drilldown/informationwindow.cpp b/examples/sql/drilldown/informationwindow.cpp new file mode 100644 index 0000000..ea66b0b --- /dev/null +++ b/examples/sql/drilldown/informationwindow.cpp @@ -0,0 +1,172 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "informationwindow.h" + +//! [0] +InformationWindow::InformationWindow(int id, QSqlRelationalTableModel *offices, + QWidget *parent) + : QDialog(parent) +{ +//! [0] //! [1] + QLabel *locationLabel = new QLabel(tr("Location: ")); + QLabel *countryLabel = new QLabel(tr("Country: ")); + QLabel *descriptionLabel = new QLabel(tr("Description: ")); + QLabel *imageFileLabel = new QLabel(tr("Image file: ")); + + createButtons(); + + locationText = new QLabel; + countryText = new QLabel; + descriptionEditor = new QTextEdit; +//! [1] + +//! [2] + imageFileEditor = new QComboBox; + imageFileEditor->setModel(offices->relationModel(1)); + imageFileEditor->setModelColumn(offices->relationModel(1)->fieldIndex("file")); +//! [2] + +//! [3] + mapper = new QDataWidgetMapper(this); + mapper->setModel(offices); + mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); + mapper->setItemDelegate(new QSqlRelationalDelegate(mapper)); + mapper->addMapping(imageFileEditor, 1); + mapper->addMapping(locationText, 2, "text"); + mapper->addMapping(countryText, 3, "text"); + mapper->addMapping(descriptionEditor, 4); + mapper->setCurrentIndex(id); +//! [3] + +//! [4] + connect(descriptionEditor, SIGNAL(textChanged()), + this, SLOT(enableButtons())); + connect(imageFileEditor, SIGNAL(currentIndexChanged(int)), + this, SLOT(enableButtons())); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(locationLabel, 0, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(countryLabel, 1, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(imageFileLabel, 2, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(descriptionLabel, 3, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(locationText, 0, 1); + layout->addWidget(countryText, 1, 1); + layout->addWidget(imageFileEditor, 2, 1); + layout->addWidget(descriptionEditor, 3, 1); + layout->addWidget(buttonBox, 4, 0, 1, 2); + setLayout(layout); + + locationId = id; + displayedImage = imageFileEditor->currentText(); + + setWindowFlags(Qt::Window); + enableButtons(false); + setWindowTitle(tr("Office: %1").arg(locationText->text())); +#ifndef Q_OS_SYMBIAN + resize(320, sizeHint().height()); +#endif +} +//! [4] + +//! [5] +int InformationWindow::id() +{ + return locationId; +} +//! [5] + +//! [6] +void InformationWindow::revert() +{ + mapper->revert(); + enableButtons(false); +} +//! [6] + +//! [7] +void InformationWindow::submit() +{ + QString newImage(imageFileEditor->currentText()); + + if (displayedImage != newImage) { + displayedImage = newImage; + emit imageChanged(locationId, newImage); + } + + mapper->submit(); + mapper->setCurrentIndex(locationId); + + enableButtons(false); +} +//! [7] + +//! [8] +void InformationWindow::createButtons() +{ + closeButton = new QPushButton(tr("&Close")); + revertButton = new QPushButton(tr("&Revert")); + submitButton = new QPushButton(tr("&Submit")); + + closeButton->setDefault(true); + + connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); + connect(revertButton, SIGNAL(clicked()), this, SLOT(revert())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submit())); +//! [8] + +//! [9] + buttonBox = new QDialogButtonBox; + buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole); +} +//! [9] + +//! [10] +void InformationWindow::enableButtons(bool enable) +{ + revertButton->setEnabled(enable); + submitButton->setEnabled(enable); +} +//! [10] + + diff --git a/examples/sql/drilldown/informationwindow.h b/examples/sql/drilldown/informationwindow.h new file mode 100644 index 0000000..f28b47d --- /dev/null +++ b/examples/sql/drilldown/informationwindow.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef INFORMATIONWINDOW_H +#define INFORMATIONWINDOW_H + +#include <QtGui> +#include <QtSql> + +//! [0] +class InformationWindow : public QDialog +{ + Q_OBJECT + +public: + InformationWindow(int id, QSqlRelationalTableModel *offices, + QWidget *parent = 0); + + int id(); + +signals: + void imageChanged(int id, const QString &fileName); +//! [0] + +//! [1] +private slots: + void revert(); + void submit(); + void enableButtons(bool enable = true); +//! [1] + +//! [2] +private: + void createButtons(); + + int locationId; + QString displayedImage; + + QComboBox *imageFileEditor; + QLabel *locationText; + QLabel *countryText; + QTextEdit *descriptionEditor; + + QPushButton *closeButton; + QPushButton *submitButton; + QPushButton *revertButton; + QDialogButtonBox *buttonBox; + + QDataWidgetMapper *mapper; +}; +//! [2] + +#endif diff --git a/examples/sql/drilldown/logo.png b/examples/sql/drilldown/logo.png Binary files differnew file mode 100644 index 0000000..4b1d235 --- /dev/null +++ b/examples/sql/drilldown/logo.png diff --git a/examples/sql/drilldown/main.cpp b/examples/sql/drilldown/main.cpp new file mode 100644 index 0000000..c340f5e --- /dev/null +++ b/examples/sql/drilldown/main.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "../connection.h" +#include "view.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(drilldown); + + QApplication app(argc, argv); + + if (!createConnection()) + return 1; + + View view("offices", "images"); +#ifndef Q_OS_SYMBIAN + view.show(); +#else + view.showFullScreen(); +#endif + return app.exec(); +} diff --git a/examples/sql/drilldown/view.cpp b/examples/sql/drilldown/view.cpp new file mode 100644 index 0000000..778767b --- /dev/null +++ b/examples/sql/drilldown/view.cpp @@ -0,0 +1,192 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "informationwindow.h" +#include "imageitem.h" +#include "view.h" + +//! [0] +View::View(const QString &offices, const QString &images, QWidget *parent) + : QGraphicsView(parent) +{ + officeTable = new QSqlRelationalTableModel(this); + officeTable->setTable(offices); + officeTable->setRelation(1, QSqlRelation(images, "locationid", "file")); + officeTable->select(); +//! [0] + +//! [1] + scene = new QGraphicsScene(this); + scene->setSceneRect(0, 0, 465, 615); + setScene(scene); + + addItems(); + + QGraphicsPixmapItem *logo = scene->addPixmap(QPixmap(":/logo.png")); + logo->setPos(30, 515); + +#ifndef Q_OS_SYMBIAN + setMinimumSize(470, 620); + setMaximumSize(470, 620); +#else + setDragMode(QGraphicsView::ScrollHandDrag); +#endif + + setWindowTitle(tr("Offices World Wide")); +} +//! [1] + +//! [3] +void View::addItems() +{ + int officeCount = officeTable->rowCount(); + + int imageOffset = 150; + int leftMargin = 70; + int topMargin = 40; + + for (int i = 0; i < officeCount; i++) { + ImageItem *image; + QGraphicsTextItem *label; + QSqlRecord record = officeTable->record(i); + + int id = record.value("id").toInt(); + QString file = record.value("file").toString(); + QString location = record.value("location").toString(); + + int columnOffset = ((i / 3) * 37); + int x = ((i / 3) * imageOffset) + leftMargin + columnOffset; + int y = ((i % 3) * imageOffset) + topMargin; + + image = new ImageItem(id, QPixmap(":/" + file)); + image->setData(0, i); + image->setPos(x, y); + scene->addItem(image); + + label = scene->addText(location); + QPointF labelOffset((150 - label->boundingRect().width()) / 2, 120.0); + label->setPos(QPointF(x, y) + labelOffset); + } +} +//! [3] + +//! [5] +void View::mouseReleaseEvent(QMouseEvent *event) +{ + if (QGraphicsItem *item = itemAt(event->pos())) { + if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) + showInformation(image); + } + QGraphicsView::mouseReleaseEvent(event); +} +//! [5] + +//! [6] +void View::showInformation(ImageItem *image) +{ + int id = image->id(); + if (id < 0 || id >= officeTable->rowCount()) + return; + + InformationWindow *window = findWindow(id); + if (window && window->isVisible()) { + window->raise(); + window->activateWindow(); + } else if (window && !window->isVisible()) { +#ifndef Q_OS_SYMBIAN + window->show(); +#else + window->showFullScreen(); +#endif + } else { + InformationWindow *window; + window = new InformationWindow(id, officeTable, this); + + connect(window, SIGNAL(imageChanged(int, QString)), + this, SLOT(updateImage(int, QString))); + +#ifndef Q_OS_SYMBIAN + window->move(pos() + QPoint(20, 40)); + window->show(); +#else + window->showFullScreen(); +#endif + informationWindows.append(window); + } +} +//! [6] + +//! [7] +void View::updateImage(int id, const QString &fileName) +{ + QList<QGraphicsItem *> items = scene->items(); + + while(!items.empty()) { + QGraphicsItem *item = items.takeFirst(); + + if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) { + if (image->id() == id){ + image->setPixmap(QPixmap(":/" +fileName)); + image->adjust(); + break; + } + } + } +} +//! [7] + +//! [8] +InformationWindow* View::findWindow(int id) +{ + QList<InformationWindow*>::iterator i, beginning, end; + + beginning = informationWindows.begin(); + end = informationWindows.end(); + + for (i = beginning; i != end; ++i) { + InformationWindow *window = (*i); + if (window && (window->id() == id)) + return window; + } + return 0; +} +//! [8] + diff --git a/examples/sql/drilldown/view.h b/examples/sql/drilldown/view.h new file mode 100644 index 0000000..37ddabb --- /dev/null +++ b/examples/sql/drilldown/view.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VIEW_H +#define VIEW_H + +#include <QtGui> +#include <QtSql> + +class ImageItem; +class InformationWindow; + +//! [0] +class View : public QGraphicsView +{ + Q_OBJECT + +public: + View(const QString &offices, const QString &images, QWidget *parent = 0); + +protected: + void mouseReleaseEvent(QMouseEvent *event); +//! [0] + +//! [1] +private slots: + void updateImage(int id, const QString &fileName); +//! [1] + +//! [2] +private: + void addItems(); + InformationWindow* findWindow(int id); + void showInformation(ImageItem *image); + + QGraphicsScene *scene; + QList<InformationWindow *> informationWindows; +//! [2] //! [3] + QSqlRelationalTableModel *officeTable; +}; +//! [3] + +#endif diff --git a/examples/sql/masterdetail/albumdetails.xml b/examples/sql/masterdetail/albumdetails.xml new file mode 100644 index 0000000..aacbdac --- /dev/null +++ b/examples/sql/masterdetail/albumdetails.xml @@ -0,0 +1,98 @@ + <archive> + <album id="1" > + <track number="01" >Humming one of your songs</track> + <track number="02" >Are they saying goodbye</track> + <track number="03" >On off</track> + <track number="04" >I shot my heart</track> + <track number="05" >Drowning in Those Eyes</track> + <track number="06" >So you did it again</track> + <track number="07" >One more time</track> + <track number="08" >Headphone silence</track> + <track number="09" >What I want</track> + <track number="10" >Sleeping by the Fyris River</track> + <track number="11" >Wooden Body</track> + <track number="12" >Humming one of your songs (encore)</track> + </album> + <album id="2" > + <track number="01" >To let myself go</track> + <track number="02" >Rubber and Soul</track> + <track number="03" >Balloon ranger</track> + <track number="04" >My lover will go</track> + <track number="05" >Temporary dive</track> + <track number="06" >Laid in earth</track> + <track number="07" >This voice</track> + <track number="08" >Where friend rhymes with end</track> + <track number="09" >Song No.6 feat Ron Sexsmith</track> + <track number="10" >The Fight Song</track> + </album> + <album id="3" > + <track number="01" >From Grace</track> + <track number="02" >All's not last</track> + <track number="03" >That Great October Sound</track> + <track number="04" >Life Here Is Gold</track> + <track number="05" >Tomorrow Stays The Same</track> + <track number="06" >Postulate</track> + <track number="07" >Adelaide</track> + <track number="08" >John Wayne</track> + <track number="09" >Love's Lost</track> + <track number="10" >Dreamweaver</track> + <track number="11" >Outro</track> + </album> + <album id="4" > + <track number="01" >Rain down on me</track> + <track number="02" >Cecilia</track> + <track number="03" >Make a mess of yourself</track> + <track number="04" >Pale green eyes</track> + <track number="05" >Either way I'm gone</track> + <track number="06" >Honey</track> + <track number="07" >Rise in shame</track> + <track number="08" >Stray dogs</track> + <track number="09" >The willow</track> + <track number="10" >Stay home</track> + <track number="11" >Outro</track> + </album> + <album id="5" > + </album> + <album id="6" > + <track number="01" >Kontroll på kontinentet</track> + <track number="02" >Ompa til du dør</track> + <track number="03" >Bøn fra helvete</track> + <track number="04" >170</track> + <track number="05" >Rullett</track> + <track number="06" >Dr. Mowinckel</track> + <track number="07" >Fra sjåfør til passasjer</track> + <track number="08" >Resistansen</track> + <track number="09" >Dekk bord</track> + <track number="10" >Bak et halleluja</track> + <track number="11" >Bris</track> + <track number="12" >Mr. Kaizer, hans Constanze og meg</track> + </album> + <album id="7" > + <track number="01" >Di grind</track> + <track number="02" >Hevnervals</track> + <track number="03" >Evig pint</track> + <track number="04" >De involverte</track> + <track number="05" >Djevelens orkester</track> + <track number="06" >Container</track> + <track number="07" >Naade</track> + <track number="08" >Min kvite russer</track> + <track number="09" >Veterans klage</track> + <track number="10" >Til depotet</track> + <track number="11" >Salt og pepper</track> + <track number="12" >Drøm Hardt (Requiem Part I)</track> + </album> + <album id="8" > + <track number="01" >KGB</track> + <track number="02" >Maestro</track> + <track number="03" >Knekker deg til sist</track> + <track number="04" >Senor Flamingos Adieu</track> + <track number="05" >Blitzregn baby</track> + <track number="06" >Dieter Meyers Inst.</track> + <track number="07" >Christiania</track> + <track number="08" >Delikatessen</track> + <track number="09" >Jævel av en tango</track> + <track number="10" >Papa har lov</track> + <track number="11" >Auksjon (i Dieter Meyers hall)</track> + <track number="12" >På ditt skift</track> + </album> + </archive> diff --git a/examples/sql/masterdetail/database.h b/examples/sql/masterdetail/database.h new file mode 100644 index 0000000..a760e41 --- /dev/null +++ b/examples/sql/masterdetail/database.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DATABASE_H +#define DATABASE_H + +#include <QMessageBox> +#include <QSqlDatabase> +#include <QSqlError> +#include <QSqlQuery> + +static bool createConnection() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + QMessageBox::critical(0, qApp->tr("Cannot open database"), + qApp->tr("Unable to establish a database connection.\n" + "This example needs SQLite support. Please read " + "the Qt SQL driver documentation for information how " + "to build it.\n\n" + "Click Cancel to exit."), QMessageBox::Cancel); + return false; + } + + QSqlQuery query; + + query.exec("create table artists (id int primary key, " + "artist varchar(40), " + "albumcount int)"); + + query.exec("insert into artists values(0, '<all>', 0)"); + query.exec("insert into artists values(1, 'Ane Brun', 2)"); + query.exec("insert into artists values(2, 'Thomas Dybdahl', 3)"); + query.exec("insert into artists values(3, 'Kaizers Orchestra', 3)"); + + query.exec("create table albums (albumid int primary key, " + "title varchar(50), " + "artistid int, " + "year int)"); + + query.exec("insert into albums values(1, 'Spending Time With Morgan', 1, " + "2003)"); + query.exec("insert into albums values(2, 'A Temporary Dive', 1, 2005)"); + query.exec("insert into albums values(3, '...The Great October Sound', 2, " + "2002)"); + query.exec("insert into albums values(4, 'Stray Dogs', 2, 2003)"); + query.exec("insert into albums values(5, " + "'One day you`ll dance for me, New York City', 2, 2004)"); + query.exec("insert into albums values(6, 'Ompa Til Du D\xf8r', 3, 2001)"); + query.exec("insert into albums values(7, 'Evig Pint', 3, 2002)"); + query.exec("insert into albums values(8, 'Maestro', 3, 2005)"); + + return true; +} + +#endif + + diff --git a/examples/sql/masterdetail/dialog.cpp b/examples/sql/masterdetail/dialog.cpp new file mode 100644 index 0000000..0adf208 --- /dev/null +++ b/examples/sql/masterdetail/dialog.cpp @@ -0,0 +1,283 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dialog.h" + +int uniqueAlbumId; +int uniqueArtistId; + +Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details, + QFile *output, QWidget *parent) + : QDialog(parent) +{ + model = albums; + albumDetails = details; + outputFile = output; + + QGroupBox *inputWidgetBox = createInputWidgets(); + QDialogButtonBox *buttonBox = createButtons(); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(inputWidgetBox); + layout->addWidget(buttonBox); + setLayout(layout); + + setWindowTitle(tr("Add Album")); +} + +void Dialog::submit() +{ + QString artist = artistEditor->text(); + QString title = titleEditor->text(); + + if (artist.isEmpty() || title.isEmpty()) { + QString message(tr("Please provide both the name of the artist " + "and the title of the album.")); + QMessageBox::information(this, tr("Add Album"), message); + } else { + int artistId = findArtistId(artist); + int albumId = addNewAlbum(title, artistId); + + QStringList tracks; + tracks = tracksEditor->text().split(',', QString::SkipEmptyParts); + addTracks(albumId, tracks); + + increaseAlbumCount(indexOfArtist(artist)); + accept(); + } +} + +int Dialog::findArtistId(const QString &artist) +{ + QSqlTableModel *artistModel = model->relationModel(2); + int row = 0; + + while (row < artistModel->rowCount()) { + QSqlRecord record = artistModel->record(row); + if (record.value("artist") == artist) + return record.value("id").toInt(); + else + row++; + } + return addNewArtist(artist); +} + + +int Dialog::addNewArtist(const QString &name) +{ + QSqlTableModel *artistModel = model->relationModel(2); + QSqlRecord record; + + int id = generateArtistId(); + + QSqlField f1("id", QVariant::Int); + QSqlField f2("artist", QVariant::String); + QSqlField f3("albumcount", QVariant::Int); + + f1.setValue(QVariant(id)); + f2.setValue(QVariant(name)); + f3.setValue(QVariant(0)); + record.append(f1); + record.append(f2); + record.append(f3); + + artistModel->insertRecord(-1, record); + return id; +} + +int Dialog::addNewAlbum(const QString &title, int artistId) +{ + int id = generateAlbumId(); + QSqlRecord record; + + QSqlField f1("albumid", QVariant::Int); + QSqlField f2("title", QVariant::String); + QSqlField f3("artistid", QVariant::Int); + QSqlField f4("year", QVariant::Int); + + f1.setValue(QVariant(id)); + f2.setValue(QVariant(title)); + f3.setValue(QVariant(artistId)); + f4.setValue(QVariant(yearEditor->value())); + record.append(f1); + record.append(f2); + record.append(f3); + record.append(f4); + + model->insertRecord(-1, record); + return id; +} + +void Dialog::addTracks(int albumId, QStringList tracks) +{ + QDomElement albumNode = albumDetails.createElement("album"); + albumNode.setAttribute("id", albumId); + + for (int i = 0; i < tracks.count(); i++) { + QString trackNumber = QString::number(i); + if (i < 10) + trackNumber.prepend("0"); + + QDomText textNode = albumDetails.createTextNode(tracks.at(i)); + + QDomElement trackNode = albumDetails.createElement("track"); + trackNode.setAttribute("number", trackNumber); + trackNode.appendChild(textNode); + + albumNode.appendChild(trackNode); + } + + QDomNodeList archive = albumDetails.elementsByTagName("archive"); + archive.item(0).appendChild(albumNode); + +/* + The following code is commented out since the example uses an in + memory database, i.e., altering the XML file will bring the data + out of sync. + + if (!outputFile->open(QIODevice::WriteOnly)) { + return; + } else { + QTextStream stream(outputFile); + archive.item(0).save(stream, 4); + outputFile->close(); + } +*/ +} + +void Dialog::increaseAlbumCount(QModelIndex artistIndex) +{ + QSqlTableModel *artistModel = model->relationModel(2); + + QModelIndex albumCountIndex; + albumCountIndex = artistIndex.sibling(artistIndex.row(), 2); + + int albumCount = albumCountIndex.data().toInt(); + artistModel->setData(albumCountIndex, QVariant(albumCount + 1)); +} + + +void Dialog::revert() +{ + artistEditor->clear(); + titleEditor->clear(); + yearEditor->setValue(QDate::currentDate().year()); + tracksEditor->clear(); +} + +QGroupBox *Dialog::createInputWidgets() +{ + QGroupBox *box = new QGroupBox(tr("Add Album")); + + QLabel *artistLabel = new QLabel(tr("Artist:")); + QLabel *titleLabel = new QLabel(tr("Title:")); + QLabel *yearLabel = new QLabel(tr("Year:")); + QLabel *tracksLabel = new QLabel(tr("Tracks (separated by comma):")); + + artistEditor = new QLineEdit; + titleEditor = new QLineEdit; + + yearEditor = new QSpinBox; + yearEditor->setMinimum(1900); + yearEditor->setMaximum(QDate::currentDate().year()); + yearEditor->setValue(yearEditor->maximum()); + yearEditor->setReadOnly(false); + + tracksEditor = new QLineEdit; + + QGridLayout *layout = new QGridLayout; + layout->addWidget(artistLabel, 0, 0); + layout->addWidget(artistEditor, 0, 1); + layout->addWidget(titleLabel, 1, 0); + layout->addWidget(titleEditor, 1, 1); + layout->addWidget(yearLabel, 2, 0); + layout->addWidget(yearEditor, 2, 1); + layout->addWidget(tracksLabel, 3, 0, 1, 2); + layout->addWidget(tracksEditor, 4, 0, 1, 2); + box->setLayout(layout); + + return box; +} + +QDialogButtonBox *Dialog::createButtons() +{ + QPushButton *closeButton = new QPushButton(tr("&Close")); + QPushButton *revertButton = new QPushButton(tr("&Revert")); + QPushButton *submitButton = new QPushButton(tr("&Submit")); + + closeButton->setDefault(true); + + connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); + connect(revertButton, SIGNAL(clicked()), this, SLOT(revert())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submit())); + + QDialogButtonBox *buttonBox = new QDialogButtonBox; + buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole); + + return buttonBox; +} + +QModelIndex Dialog::indexOfArtist(const QString &artist) +{ + QSqlTableModel *artistModel = model->relationModel(2); + + for (int i = 0; i < artistModel->rowCount(); i++) { + QSqlRecord record = artistModel->record(i); + if (record.value("artist") == artist) + return artistModel->index(i, 1); + } + + return QModelIndex(); +} + +int Dialog::generateArtistId() +{ + uniqueArtistId += 1; + return uniqueArtistId; +} + +int Dialog::generateAlbumId() +{ + uniqueAlbumId += 1; + return uniqueAlbumId; +} diff --git a/examples/sql/masterdetail/dialog.h b/examples/sql/masterdetail/dialog.h new file mode 100644 index 0000000..0f4b54a --- /dev/null +++ b/examples/sql/masterdetail/dialog.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIALOG_H +#define DIALOG_H + +#include <QtGui> +#include <QtSql> +#include <QtXml> + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + Dialog(QSqlRelationalTableModel *albums, QDomDocument details, + QFile *output, QWidget *parent = 0); + +private slots: + void revert(); + void submit(); + +private: + int addNewAlbum(const QString &title, int artistId); + int addNewArtist(const QString &name); + void addTracks(int albumId, QStringList tracks); + QDialogButtonBox *createButtons(); + QGroupBox *createInputWidgets(); + int findArtistId(const QString &artist); + int generateAlbumId(); + int generateArtistId(); + void increaseAlbumCount(QModelIndex artistIndex); + QModelIndex indexOfArtist(const QString &artist); + + QSqlRelationalTableModel *model; + QDomDocument albumDetails; + QFile *outputFile; + + QLineEdit *artistEditor; + QLineEdit *titleEditor; + QSpinBox *yearEditor; + QLineEdit *tracksEditor; +}; + +#endif diff --git a/examples/sql/masterdetail/images/icon.png b/examples/sql/masterdetail/images/icon.png Binary files differnew file mode 100644 index 0000000..31f68b8 --- /dev/null +++ b/examples/sql/masterdetail/images/icon.png diff --git a/examples/sql/masterdetail/images/image.png b/examples/sql/masterdetail/images/image.png Binary files differnew file mode 100644 index 0000000..1d78037 --- /dev/null +++ b/examples/sql/masterdetail/images/image.png diff --git a/examples/sql/masterdetail/main.cpp b/examples/sql/masterdetail/main.cpp new file mode 100644 index 0000000..932b67a --- /dev/null +++ b/examples/sql/masterdetail/main.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "database.h" +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(masterdetail); + + QApplication app(argc, argv); + + if (!createConnection()) + return 1; + + QFile *albumDetails = new QFile("albumdetails.xml"); + MainWindow window("artists", "albums", albumDetails); + window.show(); + return app.exec(); +} diff --git a/examples/sql/masterdetail/mainwindow.cpp b/examples/sql/masterdetail/mainwindow.cpp new file mode 100644 index 0000000..67825d6 --- /dev/null +++ b/examples/sql/masterdetail/mainwindow.cpp @@ -0,0 +1,430 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "dialog.h" + +#include <QtGui> +#include <QtSql> +#include <QtXml> + +extern int uniqueAlbumId; +extern int uniqueArtistId; + +MainWindow::MainWindow(const QString &artistTable, const QString &albumTable, + QFile *albumDetails, QWidget *parent) + : QMainWindow(parent) +{ + file = albumDetails; + readAlbumData(); + + model = new QSqlRelationalTableModel(this); + model->setTable(albumTable); + model->setRelation(2, QSqlRelation(artistTable, "id", "artist")); + model->select(); + + QGroupBox *artists = createArtistGroupBox(); + QGroupBox *albums = createAlbumGroupBox(); + QGroupBox *details = createDetailsGroupBox(); + + artistView->setCurrentIndex(0); + uniqueAlbumId = model->rowCount(); + uniqueArtistId = artistView->count(); + + connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)), + this, SLOT(updateHeader(QModelIndex, int, int))); + connect(model, SIGNAL(rowsRemoved(QModelIndex, int, int)), + this, SLOT(updateHeader(QModelIndex, int, int))); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(artists, 0, 0); + layout->addWidget(albums, 1, 0); + layout->addWidget(details, 0, 1, 2, 1); + layout->setColumnStretch(1, 1); + layout->setColumnMinimumWidth(0, 500); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + setCentralWidget(widget); + createMenuBar(); + + showImageLabel(); + resize(850, 400); + setWindowTitle(tr("Music Archive")); +} + +void MainWindow::changeArtist(int row) +{ + if (row > 0) { + QModelIndex index = model->relationModel(2)->index(row, 1); + model->setFilter("artist = '" + index.data().toString() + '\'') ; + showArtistProfile(index); + } else if (row == 0) { + model->setFilter(QString()); + showImageLabel(); + } else { + return; + } +} + +void MainWindow::showArtistProfile(QModelIndex index) +{ + QSqlRecord record = model->relationModel(2)->record(index.row()); + + QString name = record.value("artist").toString(); + QString count = record.value("albumcount").toString(); + profileLabel->setText(tr("Artist : %1 \n" \ + "Number of Albums: %2").arg(name).arg(count)); + + profileLabel->show(); + iconLabel->show(); + + titleLabel->hide(); + trackList->hide(); + imageLabel->hide(); +} + +void MainWindow::showAlbumDetails(QModelIndex index) +{ + QSqlRecord record = model->record(index.row()); + + QString artist = record.value("artist").toString(); + QString title = record.value("title").toString(); + QString year = record.value("year").toString(); + QString albumId = record.value("albumid").toString(); + + showArtistProfile(indexOfArtist(artist)); + titleLabel->setText(tr("Title: %1 (%2)").arg(title).arg(year)); + titleLabel->show(); + + QDomNodeList albums = albumData.elementsByTagName("album"); + for (int i = 0; i < albums.count(); i++) { + QDomNode album = albums.item(i); + if (album.toElement().attribute("id") == albumId) { + getTrackList(album.toElement()); + break; + } + } + if (!trackList->count() == 0) + trackList->show(); +} + +void MainWindow::getTrackList(QDomNode album) +{ + trackList->clear(); + + QDomNodeList tracks = album.childNodes(); + QDomNode track; + QString trackNumber; + + for (int j = 0; j < tracks.count(); j++) { + + track = tracks.item(j); + trackNumber = track.toElement().attribute("number"); + + QListWidgetItem *item = new QListWidgetItem(trackList); + item->setText(trackNumber + ": " + track.toElement().text()); + } +} + +void MainWindow::addAlbum() +{ + Dialog *dialog = new Dialog(model, albumData, file, this); + int accepted = dialog->exec(); + + if (accepted == 1) { + int lastRow = model->rowCount() - 1; + albumView->selectRow(lastRow); + albumView->scrollToBottom(); + showAlbumDetails(model->index(lastRow, 0)); + } +} + +void MainWindow::deleteAlbum() +{ + QModelIndexList selection = albumView->selectionModel()->selectedRows(0); + + if (!selection.empty()) { + QModelIndex idIndex = selection.at(0); + int id = idIndex.data().toInt(); + QString title = idIndex.sibling(idIndex.row(), 1).data().toString(); + QString artist = idIndex.sibling(idIndex.row(), 2).data().toString(); + + QMessageBox::StandardButton button; + button = QMessageBox::question(this, tr("Delete Album"), + QString(tr("Are you sure you want to " \ + "delete '%1' by '%2'?")) + .arg(title).arg(artist), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + removeAlbumFromFile(id); + removeAlbumFromDatabase(idIndex); + decreaseAlbumCount(indexOfArtist(artist)); + + showImageLabel(); + } + } else { + QMessageBox::information(this, tr("Delete Album"), + tr("Select the album you want to delete.")); + } +} + +void MainWindow::removeAlbumFromFile(int id) +{ + + QDomNodeList albums = albumData.elementsByTagName("album"); + + for (int i = 0; i < albums.count(); i++) { + QDomNode node = albums.item(i); + if (node.toElement().attribute("id").toInt() == id) { + albumData.elementsByTagName("archive").item(0).removeChild(node); + break; + } + } +/* + The following code is commented out since the example uses an in + memory database, i.e., altering the XML file will bring the data + out of sync. + + if (!file->open(QIODevice::WriteOnly)) { + return; + } else { + QTextStream stream(file); + albumData.elementsByTagName("archive").item(0).save(stream, 4); + file->close(); + } +*/ +} + +void MainWindow::removeAlbumFromDatabase(QModelIndex index) +{ + model->removeRow(index.row()); +} + +void MainWindow::decreaseAlbumCount(QModelIndex artistIndex) +{ + int row = artistIndex.row(); + QModelIndex albumCountIndex = artistIndex.sibling(row, 2); + int albumCount = albumCountIndex.data().toInt(); + + QSqlTableModel *artists = model->relationModel(2); + + if (albumCount == 1) { + artists->removeRow(row); + showImageLabel(); + } else { + artists->setData(albumCountIndex, QVariant(albumCount - 1)); + } +} + +void MainWindow::readAlbumData() +{ + if (!file->open(QIODevice::ReadOnly)) + return; + + if (!albumData.setContent(file)) { + file->close(); + return; + } + file->close(); +} + +QGroupBox* MainWindow::createArtistGroupBox() +{ + artistView = new QComboBox; + artistView->setModel(model->relationModel(2)); + artistView->setModelColumn(1); + + connect(artistView, SIGNAL(currentIndexChanged(int)), + this, SLOT(changeArtist(int))); + + QGroupBox *box = new QGroupBox(tr("Artist")); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(artistView, 0, 0); + box->setLayout(layout); + + return box; +} + +QGroupBox* MainWindow::createAlbumGroupBox() +{ + QGroupBox *box = new QGroupBox(tr("Album")); + + albumView = new QTableView; + albumView->setEditTriggers(QAbstractItemView::NoEditTriggers); + albumView->setSortingEnabled(true); + albumView->setSelectionBehavior(QAbstractItemView::SelectRows); + albumView->setSelectionMode(QAbstractItemView::SingleSelection); + albumView->setShowGrid(false); + albumView->verticalHeader()->hide(); + albumView->setAlternatingRowColors(true); + albumView->setModel(model); + adjustHeader(); + + QLocale locale = albumView->locale(); + locale.setNumberOptions(QLocale::OmitGroupSeparator); + albumView->setLocale(locale); + + connect(albumView, SIGNAL(clicked(QModelIndex)), + this, SLOT(showAlbumDetails(QModelIndex))); + connect(albumView, SIGNAL(activated(QModelIndex)), + this, SLOT(showAlbumDetails(QModelIndex))); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(albumView, 0, 0); + box->setLayout(layout); + + return box; +} + +QGroupBox* MainWindow::createDetailsGroupBox() +{ + QGroupBox *box = new QGroupBox(tr("Details")); + + profileLabel = new QLabel; + profileLabel->setWordWrap(true); + profileLabel->setAlignment(Qt::AlignBottom); + + titleLabel = new QLabel; + titleLabel->setWordWrap(true); + titleLabel->setAlignment(Qt::AlignBottom); + + iconLabel = new QLabel(); + iconLabel->setAlignment(Qt::AlignBottom | Qt::AlignRight); + iconLabel->setPixmap(QPixmap(":/images/icon.png")); + + imageLabel = new QLabel; + imageLabel->setWordWrap(true); + imageLabel->setAlignment(Qt::AlignCenter); + imageLabel->setPixmap(QPixmap(":/images/image.png")); + + trackList = new QListWidget; + + QGridLayout *layout = new QGridLayout; + layout->addWidget(imageLabel, 0, 0, 3, 2); + layout->addWidget(profileLabel, 0, 0); + layout->addWidget(iconLabel, 0, 1); + layout->addWidget(titleLabel, 1, 0, 1, 2); + layout->addWidget(trackList, 2, 0, 1, 2); + layout->setRowStretch(2, 1); + box->setLayout(layout); + + return box; +} + +void MainWindow::createMenuBar() +{ + QAction *addAction = new QAction(tr("&Add album..."), this); + QAction *deleteAction = new QAction(tr("&Delete album..."), this); + QAction *quitAction = new QAction(tr("&Quit"), this); + QAction *aboutAction = new QAction(tr("&About"), this); + QAction *aboutQtAction = new QAction(tr("About &Qt"), this); + + addAction->setShortcut(tr("Ctrl+A")); + deleteAction->setShortcut(tr("Ctrl+D")); + quitAction->setShortcut(tr("Ctrl+Q")); + + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(addAction); + fileMenu->addAction(deleteAction); + fileMenu->addSeparator(); + fileMenu->addAction(quitAction); + + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAction); + helpMenu->addAction(aboutQtAction); + + connect(addAction, SIGNAL(triggered(bool)), this, SLOT(addAlbum())); + connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(deleteAlbum())); + connect(quitAction, SIGNAL(triggered(bool)), this, SLOT(close())); + connect(aboutAction, SIGNAL(triggered(bool)), this, SLOT(about())); + connect(aboutQtAction, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); +} + +void MainWindow::showImageLabel() +{ + profileLabel->hide(); + titleLabel->hide(); + iconLabel->hide(); + trackList->hide(); + + imageLabel->show(); +} + +QModelIndex MainWindow::indexOfArtist(const QString &artist) +{ + QSqlTableModel *artistModel = model->relationModel(2); + + for (int i = 0; i < artistModel->rowCount(); i++) { + QSqlRecord record = artistModel->record(i); + if (record.value("artist") == artist) + return artistModel->index(i, 1); + } + return QModelIndex(); +} + +void MainWindow::updateHeader(QModelIndex, int, int) +{ + adjustHeader(); +} + +void MainWindow::adjustHeader() +{ + albumView->hideColumn(0); + albumView->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch); + albumView->resizeColumnToContents(2); + albumView->resizeColumnToContents(3); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Music Archive"), + tr("<p>The <b>Music Archive</b> example shows how to present " + "data from different data sources in the same application. " + "The album titles, and the corresponding artists and release dates, " + "are kept in a database, while each album's tracks are stored " + "in an XML file. </p><p>The example also shows how to add as " + "well as remove data from both the database and the " + "associated XML file using the API provided by the QtSql and " + "QtXml modules, respectively.</p>")); +} diff --git a/examples/sql/masterdetail/mainwindow.h b/examples/sql/masterdetail/mainwindow.h new file mode 100644 index 0000000..4d05022 --- /dev/null +++ b/examples/sql/masterdetail/mainwindow.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QDomDocument> +#include <QMainWindow> +#include <QModelIndex> + +QT_BEGIN_NAMESPACE +class QComboBox; +class QFile; +class QGroupBox; +class QLabel; +class QListWidget; +class QSqlRelationalTableModel; +class QTableView; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(const QString &artistTable, const QString &albumTable, + QFile *albumDetails, QWidget *parent = 0); + +private slots: + void about(); + void addAlbum(); + void changeArtist(int row); + void deleteAlbum(); + void showAlbumDetails(QModelIndex index); + void showArtistProfile(QModelIndex index); + void updateHeader(QModelIndex, int, int); + +private: + void adjustHeader(); + QGroupBox *createAlbumGroupBox(); + QGroupBox *createArtistGroupBox(); + QGroupBox *createDetailsGroupBox(); + void createMenuBar(); + void decreaseAlbumCount(QModelIndex artistIndex); + void getTrackList(QDomNode album); + QModelIndex indexOfArtist(const QString &artist); + void readAlbumData(); + void removeAlbumFromDatabase(QModelIndex album); + void removeAlbumFromFile(int id); + void showImageLabel(); + + QTableView *albumView; + QComboBox *artistView; + QListWidget *trackList; + + QLabel *iconLabel; + QLabel *imageLabel; + QLabel *profileLabel; + QLabel *titleLabel; + + QDomDocument albumData; + QFile *file; + QSqlRelationalTableModel *model; +}; + +#endif diff --git a/examples/sql/masterdetail/masterdetail.pro b/examples/sql/masterdetail/masterdetail.pro new file mode 100644 index 0000000..2363e54 --- /dev/null +++ b/examples/sql/masterdetail/masterdetail.pro @@ -0,0 +1,18 @@ +HEADERS = database.h \ + dialog.h \ + mainwindow.h +RESOURCES = masterdetail.qrc +SOURCES = dialog.cpp \ + main.cpp \ + mainwindow.cpp + +QT += sql +QT += xml + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/masterdetail +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS masterdetail.pro *.xml images +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/masterdetail +INSTALLS += target sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/sql/masterdetail/masterdetail.qrc b/examples/sql/masterdetail/masterdetail.qrc new file mode 100644 index 0000000..717c2eb --- /dev/null +++ b/examples/sql/masterdetail/masterdetail.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> + <qresource> + <file>images/icon.png</file> + <file>images/image.png</file> + </qresource> +</RCC> diff --git a/examples/sql/querymodel/customsqlmodel.cpp b/examples/sql/querymodel/customsqlmodel.cpp new file mode 100644 index 0000000..e122ce8 --- /dev/null +++ b/examples/sql/querymodel/customsqlmodel.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "customsqlmodel.h" + +CustomSqlModel::CustomSqlModel(QObject *parent) + : QSqlQueryModel(parent) +{ +} + +//! [0] +QVariant CustomSqlModel::data(const QModelIndex &index, int role) const +{ + QVariant value = QSqlQueryModel::data(index, role); + if (value.isValid() && role == Qt::DisplayRole) { + if (index.column() == 0) + return value.toString().prepend("#"); + else if (index.column() == 2) + return value.toString().toUpper(); + } + if (role == Qt::TextColorRole && index.column() == 1) + return qVariantFromValue(QColor(Qt::blue)); + return value; +} +//! [0] diff --git a/examples/sql/querymodel/customsqlmodel.h b/examples/sql/querymodel/customsqlmodel.h new file mode 100644 index 0000000..f4a0eab --- /dev/null +++ b/examples/sql/querymodel/customsqlmodel.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CUSTOMSQLMODEL_H +#define CUSTOMSQLMODEL_H + +#include <QSqlQueryModel> + +//! [0] +class CustomSqlModel : public QSqlQueryModel +{ + Q_OBJECT + +public: + CustomSqlModel(QObject *parent = 0); + + QVariant data(const QModelIndex &item, int role) const; +}; +//! [0] + +#endif diff --git a/examples/sql/querymodel/editablesqlmodel.cpp b/examples/sql/querymodel/editablesqlmodel.cpp new file mode 100644 index 0000000..d7841f6 --- /dev/null +++ b/examples/sql/querymodel/editablesqlmodel.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtSql> + +#include "editablesqlmodel.h" + +EditableSqlModel::EditableSqlModel(QObject *parent) + : QSqlQueryModel(parent) +{ +} + +//! [0] +Qt::ItemFlags EditableSqlModel::flags( + const QModelIndex &index) const +{ + Qt::ItemFlags flags = QSqlQueryModel::flags(index); + if (index.column() == 1 || index.column() == 2) + flags |= Qt::ItemIsEditable; + return flags; +} +//! [0] + +//! [1] +bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value, int /* role */) +{ + if (index.column() < 1 || index.column() > 2) + return false; + + QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0); + int id = data(primaryKeyIndex).toInt(); + + clear(); + + bool ok; + if (index.column() == 1) { + ok = setFirstName(id, value.toString()); + } else { + ok = setLastName(id, value.toString()); + } + refresh(); + return ok; +} +//! [1] + +void EditableSqlModel::refresh() +{ + setQuery("select * from person"); + setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +//! [2] +bool EditableSqlModel::setFirstName(int personId, const QString &firstName) +{ + QSqlQuery query; + query.prepare("update person set firstname = ? where id = ?"); + query.addBindValue(firstName); + query.addBindValue(personId); + return query.exec(); +} +//! [2] + +bool EditableSqlModel::setLastName(int personId, const QString &lastName) +{ + QSqlQuery query; + query.prepare("update person set lastname = ? where id = ?"); + query.addBindValue(lastName); + query.addBindValue(personId); + return query.exec(); +} diff --git a/examples/sql/querymodel/editablesqlmodel.h b/examples/sql/querymodel/editablesqlmodel.h new file mode 100644 index 0000000..3606ebc --- /dev/null +++ b/examples/sql/querymodel/editablesqlmodel.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EDITABLESQLMODEL_H +#define EDITABLESQLMODEL_H + +#include <QSqlQueryModel> + +class EditableSqlModel : public QSqlQueryModel +{ + Q_OBJECT + +public: + EditableSqlModel(QObject *parent = 0); + + Qt::ItemFlags flags(const QModelIndex &index) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + +private: + bool setFirstName(int personId, const QString &firstName); + bool setLastName(int personId, const QString &lastName); + void refresh(); +}; + +#endif diff --git a/examples/sql/querymodel/main.cpp b/examples/sql/querymodel/main.cpp new file mode 100644 index 0000000..90d4265 --- /dev/null +++ b/examples/sql/querymodel/main.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "../connection.h" +#include "customsqlmodel.h" +#include "editablesqlmodel.h" + +void initializeModel(QSqlQueryModel *model) +{ + model->setQuery("select * from person"); + model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +void createView(const QString &title, QSqlQueryModel *model) +{ + static int offset = 0; + + QTableView *view = new QTableView; + view->setModel(model); + view->setWindowTitle(title); + view->move(100 + offset, 100 + offset); + offset += 20; + view->show(); +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + + QSqlQueryModel plainModel; + EditableSqlModel editableModel; + CustomSqlModel customModel; + + initializeModel(&plainModel); + initializeModel(&editableModel); + initializeModel(&customModel); + + createView(QObject::tr("Plain Query Model"), &plainModel); + createView(QObject::tr("Editable Query Model"), &editableModel); + createView(QObject::tr("Custom Query Model"), &customModel); + + return app.exec(); +} diff --git a/examples/sql/querymodel/querymodel.pro b/examples/sql/querymodel/querymodel.pro new file mode 100644 index 0000000..f20898c --- /dev/null +++ b/examples/sql/querymodel/querymodel.pro @@ -0,0 +1,15 @@ +HEADERS = ../connection.h \ + customsqlmodel.h \ + editablesqlmodel.h +SOURCES = customsqlmodel.cpp \ + editablesqlmodel.cpp \ + main.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/querymodel +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS querymodel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/querymodel +INSTALLS += target sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/sql/relationaltablemodel/relationaltablemodel.cpp b/examples/sql/relationaltablemodel/relationaltablemodel.cpp new file mode 100644 index 0000000..b38cdda --- /dev/null +++ b/examples/sql/relationaltablemodel/relationaltablemodel.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtSql> + +#include "../connection.h" + +void initializeModel(QSqlRelationalTableModel *model) +{ +//! [0] + model->setTable("employee"); +//! [0] + + model->setEditStrategy(QSqlTableModel::OnManualSubmit); +//! [1] + model->setRelation(2, QSqlRelation("city", "id", "name")); +//! [1] //! [2] + model->setRelation(3, QSqlRelation("country", "id", "name")); +//! [2] + +//! [3] + model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("City")); + model->setHeaderData(3, Qt::Horizontal, QObject::tr("Country")); +//! [3] + + model->select(); +} + +QTableView *createView(const QString &title, QSqlTableModel *model) +{ +//! [4] + QTableView *view = new QTableView; + view->setModel(model); + view->setItemDelegate(new QSqlRelationalDelegate(view)); +//! [4] + view->setWindowTitle(title); + return view; +} + +void createRelationalTables() +{ + QSqlQuery query; + query.exec("create table employee(id int primary key, name varchar(20), city int, country int)"); + query.exec("insert into employee values(1, 'Espen', 5000, 47)"); + query.exec("insert into employee values(2, 'Harald', 80000, 49)"); + query.exec("insert into employee values(3, 'Sam', 100, 1)"); + + query.exec("create table city(id int, name varchar(20))"); + query.exec("insert into city values(100, 'San Jose')"); + query.exec("insert into city values(5000, 'Oslo')"); + query.exec("insert into city values(80000, 'Munich')"); + + query.exec("create table country(id int, name varchar(20))"); + query.exec("insert into country values(1, 'USA')"); + query.exec("insert into country values(47, 'Norway')"); + query.exec("insert into country values(49, 'Germany')"); +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + createRelationalTables(); + + QSqlRelationalTableModel model; + + initializeModel(&model); + + QTableView *view = createView(QObject::tr("Relational Table Model"), &model); + view->show(); + + return app.exec(); +} diff --git a/examples/sql/relationaltablemodel/relationaltablemodel.pro b/examples/sql/relationaltablemodel/relationaltablemodel.pro new file mode 100644 index 0000000..e57729e --- /dev/null +++ b/examples/sql/relationaltablemodel/relationaltablemodel.pro @@ -0,0 +1,11 @@ +HEADERS = ../connection.h +SOURCES = relationaltablemodel.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/relationaltablemodel +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS relationaltablemodel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/relationaltablemodel +INSTALLS += target sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/sql/sql.pro b/examples/sql/sql.pro new file mode 100644 index 0000000..7bdff69 --- /dev/null +++ b/examples/sql/sql.pro @@ -0,0 +1,18 @@ +TEMPLATE = subdirs + +SUBDIRS = drilldown +!symbian: SUBDIRS += cachedtable \ + relationaltablemodel \ + sqlwidgetmapper + +!wince*:!symbian: SUBDIRS += \ + querymodel \ + tablemodel \ + masterdetail + +# install +sources.files = connection.h sql.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/sql +INSTALLS += sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/sql/sqlwidgetmapper/main.cpp b/examples/sql/sqlwidgetmapper/main.cpp new file mode 100644 index 0000000..055ac21 --- /dev/null +++ b/examples/sql/sqlwidgetmapper/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "window.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro b/examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro new file mode 100644 index 0000000..1881fce --- /dev/null +++ b/examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro @@ -0,0 +1,10 @@ +HEADERS = window.h +SOURCES = main.cpp \ + window.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/sqlwidgetmapper +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/sqlwidgetmapper +INSTALLS += target sources diff --git a/examples/sql/sqlwidgetmapper/window.cpp b/examples/sql/sqlwidgetmapper/window.cpp new file mode 100644 index 0000000..ebfa2cf --- /dev/null +++ b/examples/sql/sqlwidgetmapper/window.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtSql> + +#include "window.h" + +//! [Set up widgets] +Window::Window(QWidget *parent) + : QWidget(parent) +{ + setupModel(); + + nameLabel = new QLabel(tr("Na&me:")); + nameEdit = new QLineEdit(); + addressLabel = new QLabel(tr("&Address:")); + addressEdit = new QTextEdit(); + typeLabel = new QLabel(tr("&Type:")); + typeComboBox = new QComboBox(); + nextButton = new QPushButton(tr("&Next")); + previousButton = new QPushButton(tr("&Previous")); + + nameLabel->setBuddy(nameEdit); + addressLabel->setBuddy(addressEdit); + typeLabel->setBuddy(typeComboBox); +//! [Set up widgets] + +//! [Set up the mapper] + QSqlTableModel *relModel = model->relationModel(typeIndex); + typeComboBox->setModel(relModel); + typeComboBox->setModelColumn(relModel->fieldIndex("description")); + + mapper = new QDataWidgetMapper(this); + mapper->setModel(model); + mapper->setItemDelegate(new QSqlRelationalDelegate(this)); + mapper->addMapping(nameEdit, model->fieldIndex("name")); + mapper->addMapping(addressEdit, model->fieldIndex("address")); + mapper->addMapping(typeComboBox, typeIndex); +//! [Set up the mapper] + +//! [Set up connections and layouts] + connect(previousButton, SIGNAL(clicked()), + mapper, SLOT(toPrevious())); + connect(nextButton, SIGNAL(clicked()), + mapper, SLOT(toNext())); + connect(mapper, SIGNAL(currentIndexChanged(int)), + this, SLOT(updateButtons(int))); + + QGridLayout *layout = new QGridLayout(); + layout->addWidget(nameLabel, 0, 0, 1, 1); + layout->addWidget(nameEdit, 0, 1, 1, 1); + layout->addWidget(previousButton, 0, 2, 1, 1); + layout->addWidget(addressLabel, 1, 0, 1, 1); + layout->addWidget(addressEdit, 1, 1, 2, 1); + layout->addWidget(nextButton, 1, 2, 1, 1); + layout->addWidget(typeLabel, 3, 0, 1, 1); + layout->addWidget(typeComboBox, 3, 1, 1, 1); + setLayout(layout); + + setWindowTitle(tr("SQL Widget Mapper")); + mapper->toFirst(); +} +//! [Set up connections and layouts] + +//! [Set up the main table] +void Window::setupModel() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + QMessageBox::critical(0, tr("Cannot open database"), + tr("Unable to establish a database connection.\n" + "This example needs SQLite support. Please read " + "the Qt SQL driver documentation for information how " + "to build it."), QMessageBox::Cancel); + return; + } + + QSqlQuery query; + query.exec("create table person (id int primary key, " + "name varchar(20), address varchar(200), typeid int)"); + query.exec("insert into person values(1, 'Alice', " + "'<qt>123 Main Street<br/>Market Town</qt>', 101)"); + query.exec("insert into person values(2, 'Bob', " + "'<qt>PO Box 32<br/>Mail Handling Service" + "<br/>Service City</qt>', 102)"); + query.exec("insert into person values(3, 'Carol', " + "'<qt>The Lighthouse<br/>Remote Island</qt>', 103)"); + query.exec("insert into person values(4, 'Donald', " + "'<qt>47338 Park Avenue<br/>Big City</qt>', 101)"); + query.exec("insert into person values(5, 'Emma', " + "'<qt>Research Station<br/>Base Camp<br/>" + "Big Mountain</qt>', 103)"); +//! [Set up the main table] + +//! [Set up the address type table] + query.exec("create table addresstype (id int, description varchar(20))"); + query.exec("insert into addresstype values(101, 'Home')"); + query.exec("insert into addresstype values(102, 'Work')"); + query.exec("insert into addresstype values(103, 'Other')"); + + model = new QSqlRelationalTableModel(this); + model->setTable("person"); + model->setEditStrategy(QSqlTableModel::OnManualSubmit); + + typeIndex = model->fieldIndex("typeid"); + + model->setRelation(typeIndex, + QSqlRelation("addresstype", "id", "description")); + model->select(); +} +//! [Set up the address type table] + +//! [Slot for updating the buttons] +void Window::updateButtons(int row) +{ + previousButton->setEnabled(row > 0); + nextButton->setEnabled(row < model->rowCount() - 1); +} +//! [Slot for updating the buttons] diff --git a/examples/sql/sqlwidgetmapper/window.h b/examples/sql/sqlwidgetmapper/window.h new file mode 100644 index 0000000..a3628a9 --- /dev/null +++ b/examples/sql/sqlwidgetmapper/window.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QComboBox; +class QDataWidgetMapper; +class QItemSelectionModel; +class QLabel; +class QLineEdit; +class QPushButton; +class QSqlRelationalTableModel; +class QStandardItemModel; +class QStringListModel; +class QTextEdit; +QT_END_NAMESPACE + +//! [Window definition] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(QWidget *parent = 0); + +private slots: + void updateButtons(int row); + +private: + void setupModel(); + + QLabel *nameLabel; + QLabel *addressLabel; + QLabel *typeLabel; + QLineEdit *nameEdit; + QTextEdit *addressEdit; + QComboBox *typeComboBox; + QPushButton *nextButton; + QPushButton *previousButton; + + QSqlRelationalTableModel *model; + QItemSelectionModel *selectionModel; + QDataWidgetMapper *mapper; + int typeIndex; +}; +//! [Window definition] + +#endif diff --git a/examples/sql/tablemodel/tablemodel.cpp b/examples/sql/tablemodel/tablemodel.cpp new file mode 100644 index 0000000..f24c62f --- /dev/null +++ b/examples/sql/tablemodel/tablemodel.cpp @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtSql> + +#include "../connection.h" + +void initializeModel(QSqlTableModel *model) +{ + model->setTable("person"); + model->setEditStrategy(QSqlTableModel::OnManualSubmit); + model->select(); + + model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +QTableView *createView(const QString &title, QSqlTableModel *model) +{ + QTableView *view = new QTableView; + view->setModel(model); + view->setWindowTitle(title); + return view; +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + + QSqlTableModel model; + + initializeModel(&model); + + QTableView *view1 = createView(QObject::tr("Table Model (View 1)"), &model); + QTableView *view2 = createView(QObject::tr("Table Model (View 2)"), &model); + + view1->show(); + view2->move(view1->x() + view1->width() + 20, view1->y()); + view2->show(); + + return app.exec(); +} diff --git a/examples/sql/tablemodel/tablemodel.pro b/examples/sql/tablemodel/tablemodel.pro new file mode 100644 index 0000000..7adf40c --- /dev/null +++ b/examples/sql/tablemodel/tablemodel.pro @@ -0,0 +1,11 @@ +HEADERS = ../connection.h +SOURCES = tablemodel.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/tablemodel +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS tablemodel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/tablemodel +INSTALLS += target sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) |