summaryrefslogtreecommitdiff
path: root/demos/sqlbrowser
diff options
context:
space:
mode:
authoraxis <qt-info@nokia.com>2009-04-24 13:34:15 +0200
committeraxis <qt-info@nokia.com>2009-04-24 13:34:15 +0200
commit8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76 (patch)
treea17e1a767a89542ab59907462206d7dcf2e504b2 /demos/sqlbrowser
Long live Qt for S60!
Diffstat (limited to 'demos/sqlbrowser')
-rw-r--r--demos/sqlbrowser/browser.cpp247
-rw-r--r--demos/sqlbrowser/browser.h99
-rw-r--r--demos/sqlbrowser/browserwidget.ui199
-rw-r--r--demos/sqlbrowser/connectionwidget.cpp165
-rw-r--r--demos/sqlbrowser/connectionwidget.h79
-rw-r--r--demos/sqlbrowser/main.cpp91
-rw-r--r--demos/sqlbrowser/qsqlconnectiondialog.cpp115
-rw-r--r--demos/sqlbrowser/qsqlconnectiondialog.h74
-rw-r--r--demos/sqlbrowser/qsqlconnectiondialog.ui224
-rw-r--r--demos/sqlbrowser/sqlbrowser.pro25
10 files changed, 1318 insertions, 0 deletions
diff --git a/demos/sqlbrowser/browser.cpp b/demos/sqlbrowser/browser.cpp
new file mode 100644
index 0000000000..f7b24dbd7e
--- /dev/null
+++ b/demos/sqlbrowser/browser.cpp
@@ -0,0 +1,247 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications 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 "browser.h"
+#include "qsqlconnectiondialog.h"
+
+#include <QtGui>
+#include <QtSql>
+
+Browser::Browser(QWidget *parent)
+ : QWidget(parent)
+{
+ setupUi(this);
+
+ table->addAction(insertRowAction);
+ table->addAction(deleteRowAction);
+
+ if (QSqlDatabase::drivers().isEmpty())
+ QMessageBox::information(this, tr("No database drivers found"),
+ tr("This demo requires at least one Qt database driver. "
+ "Please check the documentation how to build the "
+ "Qt SQL plugins."));
+
+ emit statusMessage(tr("Ready."));
+}
+
+Browser::~Browser()
+{
+}
+
+void Browser::exec()
+{
+ QSqlQueryModel *model = new QSqlQueryModel(table);
+ model->setQuery(QSqlQuery(sqlEdit->toPlainText(), connectionWidget->currentDatabase()));
+ table->setModel(model);
+
+ if (model->lastError().type() != QSqlError::NoError)
+ emit statusMessage(model->lastError().text());
+ else if (model->query().isSelect())
+ emit statusMessage(tr("Query OK."));
+ else
+ emit statusMessage(tr("Query OK, number of affected rows: %1").arg(
+ model->query().numRowsAffected()));
+
+ updateActions();
+}
+
+QSqlError Browser::addConnection(const QString &driver, const QString &dbName, const QString &host,
+ const QString &user, const QString &passwd, int port)
+{
+ static int cCount = 0;
+
+ QSqlError err;
+ QSqlDatabase db = QSqlDatabase::addDatabase(driver, QString("Browser%1").arg(++cCount));
+ db.setDatabaseName(dbName);
+ db.setHostName(host);
+ db.setPort(port);
+ if (!db.open(user, passwd)) {
+ err = db.lastError();
+ db = QSqlDatabase();
+ QSqlDatabase::removeDatabase(QString("Browser%1").arg(cCount));
+ }
+ connectionWidget->refresh();
+
+ return err;
+}
+
+void Browser::addConnection()
+{
+ QSqlConnectionDialog dialog(this);
+ if (dialog.exec() != QDialog::Accepted)
+ return;
+
+ if (dialog.useInMemoryDatabase()) {
+ QSqlDatabase::database("in_mem_db", false).close();
+ QSqlDatabase::removeDatabase("in_mem_db");
+ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "in_mem_db");
+ db.setDatabaseName(":memory:");
+ if (!db.open())
+ QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
+ "opening the connection: ") + db.lastError().text());
+ QSqlQuery q("", db);
+ q.exec("drop table Movies");
+ q.exec("drop table Names");
+ q.exec("create table Movies (id integer primary key, Title varchar, Director varchar, Rating number)");
+ q.exec("insert into Movies values (0, 'Metropolis', 'Fritz Lang', '8.4')");
+ q.exec("insert into Movies values (1, 'Nosferatu, eine Symphonie des Grauens', 'F.W. Murnau', '8.1')");
+ q.exec("insert into Movies values (2, 'Bis ans Ende der Welt', 'Wim Wenders', '6.5')");
+ q.exec("insert into Movies values (3, 'Hardware', 'Richard Stanley', '5.2')");
+ q.exec("insert into Movies values (4, 'Mitchell', 'Andrew V. McLaglen', '2.1')");
+ q.exec("create table Names (id integer primary key, Firstname varchar, Lastname varchar, City varchar)");
+ q.exec("insert into Names values (0, 'Sala', 'Palmer', 'Morristown')");
+ q.exec("insert into Names values (1, 'Christopher', 'Walker', 'Morristown')");
+ q.exec("insert into Names values (2, 'Donald', 'Duck', 'Andeby')");
+ q.exec("insert into Names values (3, 'Buck', 'Rogers', 'Paris')");
+ q.exec("insert into Names values (4, 'Sherlock', 'Holmes', 'London')");
+ connectionWidget->refresh();
+ } else {
+ QSqlError err = addConnection(dialog.driverName(), dialog.databaseName(), dialog.hostName(),
+ dialog.userName(), dialog.password(), dialog.port());
+ if (err.type() != QSqlError::NoError)
+ QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
+ "opening the connection: ") + err.text());
+ }
+}
+
+void Browser::showTable(const QString &t)
+{
+ QSqlTableModel *model = new QSqlTableModel(table, connectionWidget->currentDatabase());
+ model->setEditStrategy(QSqlTableModel::OnRowChange);
+ model->setTable(t);
+ model->select();
+ if (model->lastError().type() != QSqlError::NoError)
+ emit statusMessage(model->lastError().text());
+ table->setModel(model);
+ table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
+
+ connect(table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
+ this, SLOT(currentChanged()));
+ updateActions();
+}
+
+void Browser::showMetaData(const QString &t)
+{
+ QSqlRecord rec = connectionWidget->currentDatabase().record(t);
+ QStandardItemModel *model = new QStandardItemModel(table);
+
+ model->insertRows(0, rec.count());
+ model->insertColumns(0, 7);
+
+ model->setHeaderData(0, Qt::Horizontal, "Fieldname");
+ model->setHeaderData(1, Qt::Horizontal, "Type");
+ model->setHeaderData(2, Qt::Horizontal, "Length");
+ model->setHeaderData(3, Qt::Horizontal, "Precision");
+ model->setHeaderData(4, Qt::Horizontal, "Required");
+ model->setHeaderData(5, Qt::Horizontal, "AutoValue");
+ model->setHeaderData(6, Qt::Horizontal, "DefaultValue");
+
+
+ for (int i = 0; i < rec.count(); ++i) {
+ QSqlField fld = rec.field(i);
+ model->setData(model->index(i, 0), fld.name());
+ model->setData(model->index(i, 1), fld.typeID() == -1
+ ? QString(QVariant::typeToName(fld.type()))
+ : QString("%1 (%2)").arg(QVariant::typeToName(fld.type())).arg(fld.typeID()));
+ model->setData(model->index(i, 2), fld.length());
+ model->setData(model->index(i, 3), fld.precision());
+ model->setData(model->index(i, 4), fld.requiredStatus() == -1 ? QVariant("?")
+ : QVariant(bool(fld.requiredStatus())));
+ model->setData(model->index(i, 5), fld.isAutoValue());
+ model->setData(model->index(i, 6), fld.defaultValue());
+ }
+
+ table->setModel(model);
+ table->setEditTriggers(QAbstractItemView::NoEditTriggers);
+
+ updateActions();
+}
+
+void Browser::insertRow()
+{
+ QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
+ if (!model)
+ return;
+
+ QModelIndex insertIndex = table->currentIndex();
+ int row = insertIndex.row() == -1 ? 0 : insertIndex.row();
+ model->insertRow(row);
+ insertIndex = model->index(row, 0);
+ table->setCurrentIndex(insertIndex);
+ table->edit(insertIndex);
+}
+
+void Browser::deleteRow()
+{
+ QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
+ if (!model)
+ return;
+
+ model->setEditStrategy(QSqlTableModel::OnManualSubmit);
+
+ QModelIndexList currentSelection = table->selectionModel()->selectedIndexes();
+ for (int i = 0; i < currentSelection.count(); ++i) {
+ if (currentSelection.at(i).column() != 0)
+ continue;
+ model->removeRow(currentSelection.at(i).row());
+ }
+
+ model->submitAll();
+ model->setEditStrategy(QSqlTableModel::OnRowChange);
+
+ updateActions();
+}
+
+void Browser::updateActions()
+{
+ bool enableIns = qobject_cast<QSqlTableModel *>(table->model());
+ bool enableDel = enableIns && table->currentIndex().isValid();
+
+ insertRowAction->setEnabled(enableIns);
+ deleteRowAction->setEnabled(enableDel);
+}
+
+void Browser::about()
+{
+ QMessageBox::about(this, tr("About"), tr("The SQL Browser demonstration "
+ "show how a data browser can be used to visualize the results of SQL"
+ "statements on a live database"));
+}
diff --git a/demos/sqlbrowser/browser.h b/demos/sqlbrowser/browser.h
new file mode 100644
index 0000000000..787675b482
--- /dev/null
+++ b/demos/sqlbrowser/browser.h
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications 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 BROWSER_H
+#define BROWSER_H
+
+#include <QWidget>
+#include "ui_browserwidget.h"
+
+class ConnectionWidget;
+QT_FORWARD_DECLARE_CLASS(QTableView)
+QT_FORWARD_DECLARE_CLASS(QPushButton)
+QT_FORWARD_DECLARE_CLASS(QTextEdit)
+QT_FORWARD_DECLARE_CLASS(QSqlError)
+
+class Browser: public QWidget, private Ui::Browser
+{
+ Q_OBJECT
+public:
+ Browser(QWidget *parent = 0);
+ virtual ~Browser();
+
+ QSqlError addConnection(const QString &driver, const QString &dbName, const QString &host,
+ const QString &user, const QString &passwd, int port = -1);
+
+ void insertRow();
+ void deleteRow();
+ void updateActions();
+
+public slots:
+ void exec();
+ void showTable(const QString &table);
+ void showMetaData(const QString &table);
+ void addConnection();
+ void currentChanged() { updateActions(); }
+ void about();
+
+ void on_insertRowAction_triggered()
+ { insertRow(); }
+ void on_deleteRowAction_triggered()
+ { deleteRow(); }
+ void on_connectionWidget_tableActivated(const QString &table)
+ { showTable(table); }
+ void on_connectionWidget_metaDataRequested(const QString &table)
+ { showMetaData(table); }
+ void on_submitButton_clicked()
+ {
+ exec();
+ sqlEdit->setFocus();
+ }
+ void on_clearButton_clicked()
+ {
+ sqlEdit->clear();
+ sqlEdit->setFocus();
+ }
+
+signals:
+ void statusMessage(const QString &message);
+};
+
+#endif
diff --git a/demos/sqlbrowser/browserwidget.ui b/demos/sqlbrowser/browserwidget.ui
new file mode 100644
index 0000000000..20946f0ede
--- /dev/null
+++ b/demos/sqlbrowser/browserwidget.ui
@@ -0,0 +1,199 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>Browser</class>
+ <widget class="QWidget" name="Browser" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>765</width>
+ <height>515</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>Qt SQL Browser</string>
+ </property>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>8</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QSplitter" name="splitter_2" >
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>7</hsizetype>
+ <vsizetype>7</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="orientation" >
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <widget class="ConnectionWidget" name="connectionWidget" >
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>13</hsizetype>
+ <vsizetype>7</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ </widget>
+ <widget class="QTableView" name="table" >
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>7</hsizetype>
+ <vsizetype>7</vsizetype>
+ <horstretch>2</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="contextMenuPolicy" >
+ <enum>Qt::ActionsContextMenu</enum>
+ </property>
+ <property name="selectionBehavior" >
+ <enum>QAbstractItemView::SelectRows</enum>
+ </property>
+ </widget>
+ </widget>
+ </item>
+ <item>
+ <widget class="QGroupBox" name="groupBox" >
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>3</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="maximumSize" >
+ <size>
+ <width>16777215</width>
+ <height>180</height>
+ </size>
+ </property>
+ <property name="title" >
+ <string>SQL Query</string>
+ </property>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>9</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QTextEdit" name="sqlEdit" >
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>7</hsizetype>
+ <vsizetype>3</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="minimumSize" >
+ <size>
+ <width>0</width>
+ <height>18</height>
+ </size>
+ </property>
+ <property name="baseSize" >
+ <size>
+ <width>0</width>
+ <height>120</height>
+ </size>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>1</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
+ <widget class="QPushButton" name="clearButton" >
+ <property name="text" >
+ <string>&amp;Clear</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="submitButton" >
+ <property name="text" >
+ <string>&amp;Submit</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ </layout>
+ <action name="insertRowAction" >
+ <property name="enabled" >
+ <bool>false</bool>
+ </property>
+ <property name="text" >
+ <string>&amp;Insert Row</string>
+ </property>
+ <property name="statusTip" >
+ <string>Inserts a new Row</string>
+ </property>
+ </action>
+ <action name="deleteRowAction" >
+ <property name="enabled" >
+ <bool>false</bool>
+ </property>
+ <property name="text" >
+ <string>&amp;Delete Row</string>
+ </property>
+ <property name="statusTip" >
+ <string>Deletes the current Row</string>
+ </property>
+ </action>
+ </widget>
+ <pixmapfunction></pixmapfunction>
+ <customwidgets>
+ <customwidget>
+ <class>ConnectionWidget</class>
+ <extends>QTreeView</extends>
+ <header>connectionwidget.h</header>
+ <container>0</container>
+ <pixmap></pixmap>
+ </customwidget>
+ </customwidgets>
+ <tabstops>
+ <tabstop>sqlEdit</tabstop>
+ <tabstop>clearButton</tabstop>
+ <tabstop>submitButton</tabstop>
+ <tabstop>connectionWidget</tabstop>
+ <tabstop>table</tabstop>
+ </tabstops>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/demos/sqlbrowser/connectionwidget.cpp b/demos/sqlbrowser/connectionwidget.cpp
new file mode 100644
index 0000000000..7df28ac409
--- /dev/null
+++ b/demos/sqlbrowser/connectionwidget.cpp
@@ -0,0 +1,165 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications 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 "connectionwidget.h"
+
+#include <QtGui>
+#include <QtSql>
+
+ConnectionWidget::ConnectionWidget(QWidget *parent)
+ : QWidget(parent)
+{
+ QVBoxLayout *layout = new QVBoxLayout(this);
+ tree = new QTreeWidget(this);
+ tree->setObjectName(QLatin1String("tree"));
+ tree->setHeaderLabels(QStringList(tr("database")));
+ tree->header()->setResizeMode(QHeaderView::Stretch);
+ QAction *refreshAction = new QAction(tr("Refresh"), tree);
+ metaDataAction = new QAction(tr("Show Schema"), tree);
+ connect(refreshAction, SIGNAL(triggered()), SLOT(refresh()));
+ connect(metaDataAction, SIGNAL(triggered()), SLOT(showMetaData()));
+ tree->addAction(refreshAction);
+ tree->addAction(metaDataAction);
+ tree->setContextMenuPolicy(Qt::ActionsContextMenu);
+
+ layout->addWidget(tree);
+
+ QMetaObject::connectSlotsByName(this);
+}
+
+ConnectionWidget::~ConnectionWidget()
+{
+}
+
+static QString qDBCaption(const QSqlDatabase &db)
+{
+ QString nm = db.driverName();
+ nm.append(QLatin1Char(':'));
+ if (!db.userName().isEmpty())
+ nm.append(db.userName()).append(QLatin1Char('@'));
+ nm.append(db.databaseName());
+ return nm;
+}
+
+void ConnectionWidget::refresh()
+{
+ tree->clear();
+ QStringList connectionNames = QSqlDatabase::connectionNames();
+
+ bool gotActiveDb = false;
+ for (int i = 0; i < connectionNames.count(); ++i) {
+ QTreeWidgetItem *root = new QTreeWidgetItem(tree);
+ QSqlDatabase db = QSqlDatabase::database(connectionNames.at(i), false);
+ root->setText(0, qDBCaption(db));
+ if (connectionNames.at(i) == activeDb) {
+ gotActiveDb = true;
+ setActive(root);
+ }
+ if (db.isOpen()) {
+ QStringList tables = db.tables();
+ for (int t = 0; t < tables.count(); ++t) {
+ QTreeWidgetItem *table = new QTreeWidgetItem(root);
+ table->setText(0, tables.at(t));
+ }
+ }
+ }
+ if (!gotActiveDb) {
+ activeDb = connectionNames.value(0);
+ setActive(tree->topLevelItem(0));
+ }
+
+ tree->doItemsLayout(); // HACK
+}
+
+QSqlDatabase ConnectionWidget::currentDatabase() const
+{
+ return QSqlDatabase::database(activeDb);
+}
+
+static void qSetBold(QTreeWidgetItem *item, bool bold)
+{
+ QFont font = item->font(0);
+ font.setBold(bold);
+ item->setFont(0, font);
+}
+
+void ConnectionWidget::setActive(QTreeWidgetItem *item)
+{
+ for (int i = 0; i < tree->topLevelItemCount(); ++i) {
+ if (tree->topLevelItem(i)->font(0).bold())
+ qSetBold(tree->topLevelItem(i), false);
+ }
+
+ if (!item)
+ return;
+
+ qSetBold(item, true);
+ activeDb = QSqlDatabase::connectionNames().value(tree->indexOfTopLevelItem(item));
+}
+
+void ConnectionWidget::on_tree_itemActivated(QTreeWidgetItem *item, int /* column */)
+{
+
+ if (!item)
+ return;
+
+ if (!item->parent()) {
+ setActive(item);
+ } else {
+ setActive(item->parent());
+ emit tableActivated(item->text(0));
+ }
+}
+
+void ConnectionWidget::showMetaData()
+{
+ QTreeWidgetItem *cItem = tree->currentItem();
+ if (!cItem || !cItem->parent())
+ return;
+ setActive(cItem->parent());
+ emit metaDataRequested(cItem->text(0));
+}
+
+void ConnectionWidget::on_tree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
+{
+ metaDataAction->setEnabled(current && current->parent());
+}
+
diff --git a/demos/sqlbrowser/connectionwidget.h b/demos/sqlbrowser/connectionwidget.h
new file mode 100644
index 0000000000..5c4841451f
--- /dev/null
+++ b/demos/sqlbrowser/connectionwidget.h
@@ -0,0 +1,79 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications 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 CONNECTIONWIDGET_H
+#define CONNECTIONWIDGET_H
+
+#include <QWidget>
+
+QT_FORWARD_DECLARE_CLASS(QTreeWidget)
+QT_FORWARD_DECLARE_CLASS(QTreeWidgetItem)
+QT_FORWARD_DECLARE_CLASS(QSqlDatabase)
+QT_FORWARD_DECLARE_CLASS(QMenu)
+
+class ConnectionWidget: public QWidget
+{
+ Q_OBJECT
+public:
+ ConnectionWidget(QWidget *parent = 0);
+ virtual ~ConnectionWidget();
+
+ QSqlDatabase currentDatabase() const;
+
+signals:
+ void tableActivated(const QString &table);
+ void metaDataRequested(const QString &tableName);
+
+public slots:
+ void refresh();
+ void showMetaData();
+ void on_tree_itemActivated(QTreeWidgetItem *item, int column);
+ void on_tree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
+
+private:
+ void setActive(QTreeWidgetItem *);
+
+ QTreeWidget *tree;
+ QAction *metaDataAction;
+ QString activeDb;
+};
+
+#endif
diff --git a/demos/sqlbrowser/main.cpp b/demos/sqlbrowser/main.cpp
new file mode 100644
index 0000000000..b7b7fe5fa8
--- /dev/null
+++ b/demos/sqlbrowser/main.cpp
@@ -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 demonstration applications 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 "browser.h"
+
+#include <QtCore>
+#include <QtGui>
+#include <QtSql>
+
+void addConnectionsFromCommandline(const QStringList &args, Browser *browser)
+{
+ for (int i = 1; i < args.count(); ++i) {
+ QUrl url(args.at(i), QUrl::TolerantMode);
+ if (!url.isValid()) {
+ qWarning("Invalid URL: %s", qPrintable(args.at(i)));
+ continue;
+ }
+ QSqlError err = browser->addConnection(url.scheme(), url.path().mid(1), url.host(),
+ url.userName(), url.password(), url.port(-1));
+ if (err.type() != QSqlError::NoError)
+ qDebug() << "Unable to open connection:" << err;
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ QMainWindow mainWin;
+ mainWin.setWindowTitle(QObject::tr("Qt SQL Browser"));
+
+ Browser browser(&mainWin);
+ mainWin.setCentralWidget(&browser);
+
+ QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File"));
+ fileMenu->addAction(QObject::tr("Add &Connection..."), &browser, SLOT(addConnection()));
+ fileMenu->addSeparator();
+ fileMenu->addAction(QObject::tr("&Quit"), &app, SLOT(quit()));
+
+ QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help"));
+ helpMenu->addAction(QObject::tr("About"), &browser, SLOT(about()));
+ helpMenu->addAction(QObject::tr("About Qt"), qApp, SLOT(aboutQt()));
+
+ QObject::connect(&browser, SIGNAL(statusMessage(QString)),
+ mainWin.statusBar(), SLOT(showMessage(QString)));
+
+ addConnectionsFromCommandline(app.arguments(), &browser);
+ mainWin.show();
+ if (QSqlDatabase::connectionNames().isEmpty())
+ QMetaObject::invokeMethod(&browser, "addConnection", Qt::QueuedConnection);
+
+ return app.exec();
+}
diff --git a/demos/sqlbrowser/qsqlconnectiondialog.cpp b/demos/sqlbrowser/qsqlconnectiondialog.cpp
new file mode 100644
index 0000000000..a2e3c89ac7
--- /dev/null
+++ b/demos/sqlbrowser/qsqlconnectiondialog.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 demonstration applications 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 "qsqlconnectiondialog.h"
+#include "ui_qsqlconnectiondialog.h"
+
+#include <QSqlDatabase>
+
+QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent)
+ : QDialog(parent)
+{
+ ui.setupUi(this);
+
+ QStringList drivers = QSqlDatabase::drivers();
+
+ // remove compat names
+ drivers.removeAll("QMYSQL3");
+ drivers.removeAll("QOCI8");
+ drivers.removeAll("QODBC3");
+ drivers.removeAll("QPSQL7");
+ drivers.removeAll("QTDS7");
+
+ if (!drivers.contains("QSQLITE"))
+ ui.dbCheckBox->setEnabled(false);
+
+ ui.comboDriver->addItems(drivers);
+}
+
+QSqlConnectionDialog::~QSqlConnectionDialog()
+{
+}
+
+QString QSqlConnectionDialog::driverName() const
+{
+ return ui.comboDriver->currentText();
+}
+
+QString QSqlConnectionDialog::databaseName() const
+{
+ return ui.editDatabase->text();
+}
+
+QString QSqlConnectionDialog::userName() const
+{
+ return ui.editUsername->text();
+}
+
+QString QSqlConnectionDialog::password() const
+{
+ return ui.editPassword->text();
+}
+
+QString QSqlConnectionDialog::hostName() const
+{
+ return ui.editHostname->text();
+}
+
+int QSqlConnectionDialog::port() const
+{
+ return ui.portSpinBox->value();
+}
+
+bool QSqlConnectionDialog::useInMemoryDatabase() const
+{
+ return ui.dbCheckBox->isChecked();
+}
+
+void QSqlConnectionDialog::on_okButton_clicked()
+{
+ if (ui.comboDriver->currentText().isEmpty()) {
+ QMessageBox::information(this, tr("No database driver selected"),
+ tr("Please select a database driver"));
+ ui.comboDriver->setFocus();
+ } else {
+ accept();
+ }
+}
diff --git a/demos/sqlbrowser/qsqlconnectiondialog.h b/demos/sqlbrowser/qsqlconnectiondialog.h
new file mode 100644
index 0000000000..d5f3456b9f
--- /dev/null
+++ b/demos/sqlbrowser/qsqlconnectiondialog.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the demonstration applications 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 QSQLCONNECTIONDIALOG_H
+#define QSQLCONNECTIONDIALOG_H
+
+#include <QDialog>
+#include <QMessageBox>
+
+#include "ui_qsqlconnectiondialog.h"
+
+class QSqlConnectionDialog: public QDialog
+{
+ Q_OBJECT
+public:
+ QSqlConnectionDialog(QWidget *parent = 0);
+ ~QSqlConnectionDialog();
+
+ QString driverName() const;
+ QString databaseName() const;
+ QString userName() const;
+ QString password() const;
+ QString hostName() const;
+ int port() const;
+ bool useInMemoryDatabase() const;
+
+private slots:
+ void on_okButton_clicked();
+ void on_cancelButton_clicked() { reject(); }
+ void on_dbCheckBox_clicked() { ui.connGroupBox->setEnabled(!ui.dbCheckBox->isChecked()); }
+
+private:
+ Ui::QSqlConnectionDialogUi ui;
+};
+
+#endif
diff --git a/demos/sqlbrowser/qsqlconnectiondialog.ui b/demos/sqlbrowser/qsqlconnectiondialog.ui
new file mode 100644
index 0000000000..91a8700579
--- /dev/null
+++ b/demos/sqlbrowser/qsqlconnectiondialog.ui
@@ -0,0 +1,224 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>QSqlConnectionDialogUi</class>
+ <widget class="QDialog" name="QSqlConnectionDialogUi" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>315</width>
+ <height>302</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>Connect...</string>
+ </property>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>8</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QGroupBox" name="connGroupBox" >
+ <property name="title" >
+ <string>Connection settings</string>
+ </property>
+ <layout class="QGridLayout" >
+ <property name="margin" >
+ <number>8</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item row="0" column="1" >
+ <widget class="QComboBox" name="comboDriver" />
+ </item>
+ <item row="2" column="0" >
+ <widget class="QLabel" name="textLabel4" >
+ <property name="text" >
+ <string>&amp;Username:</string>
+ </property>
+ <property name="buddy" >
+ <cstring>editUsername</cstring>
+ </property>
+ </widget>
+ </item>
+ <item row="0" column="0" >
+ <widget class="QLabel" name="textLabel2" >
+ <property name="text" >
+ <string>D&amp;river</string>
+ </property>
+ <property name="buddy" >
+ <cstring>comboDriver</cstring>
+ </property>
+ </widget>
+ </item>
+ <item row="1" column="1" >
+ <widget class="QLineEdit" name="editDatabase" />
+ </item>
+ <item row="5" column="1" >
+ <widget class="QSpinBox" name="portSpinBox" >
+ <property name="specialValueText" >
+ <string>Default</string>
+ </property>
+ <property name="maximum" >
+ <number>65535</number>
+ </property>
+ <property name="minimum" >
+ <number>-1</number>
+ </property>
+ <property name="value" >
+ <number>-1</number>
+ </property>
+ </widget>
+ </item>
+ <item row="1" column="0" >
+ <widget class="QLabel" name="textLabel3" >
+ <property name="text" >
+ <string>Database Name:</string>
+ </property>
+ <property name="buddy" >
+ <cstring>editDatabase</cstring>
+ </property>
+ </widget>
+ </item>
+ <item row="3" column="1" >
+ <widget class="QLineEdit" name="editPassword" >
+ <property name="echoMode" >
+ <enum>QLineEdit::Password</enum>
+ </property>
+ </widget>
+ </item>
+ <item row="2" column="1" >
+ <widget class="QLineEdit" name="editUsername" />
+ </item>
+ <item row="4" column="1" >
+ <widget class="QLineEdit" name="editHostname" />
+ </item>
+ <item row="4" column="0" >
+ <widget class="QLabel" name="textLabel5" >
+ <property name="text" >
+ <string>&amp;Hostname:</string>
+ </property>
+ <property name="buddy" >
+ <cstring>editHostname</cstring>
+ </property>
+ </widget>
+ </item>
+ <item row="5" column="0" >
+ <widget class="QLabel" name="textLabel5_2" >
+ <property name="text" >
+ <string>P&amp;ort:</string>
+ </property>
+ <property name="buddy" >
+ <cstring>portSpinBox</cstring>
+ </property>
+ </widget>
+ </item>
+ <item row="3" column="0" >
+ <widget class="QLabel" name="textLabel4_2" >
+ <property name="text" >
+ <string>&amp;Password:</string>
+ </property>
+ <property name="buddy" >
+ <cstring>editPassword</cstring>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
+ <widget class="QCheckBox" name="dbCheckBox" >
+ <property name="text" >
+ <string>Us&amp;e predefined in-memory database</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeType" >
+ <enum>QSizePolicy::Expanding</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>20</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
+ <widget class="QPushButton" name="okButton" >
+ <property name="text" >
+ <string>&amp;OK</string>
+ </property>
+ <property name="default" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="cancelButton" >
+ <property name="text" >
+ <string>&amp;Cancel</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <pixmapfunction></pixmapfunction>
+ <tabstops>
+ <tabstop>comboDriver</tabstop>
+ <tabstop>editDatabase</tabstop>
+ <tabstop>editUsername</tabstop>
+ <tabstop>editPassword</tabstop>
+ <tabstop>editHostname</tabstop>
+ <tabstop>portSpinBox</tabstop>
+ <tabstop>dbCheckBox</tabstop>
+ <tabstop>okButton</tabstop>
+ <tabstop>cancelButton</tabstop>
+ </tabstops>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/demos/sqlbrowser/sqlbrowser.pro b/demos/sqlbrowser/sqlbrowser.pro
new file mode 100644
index 0000000000..1334bc1370
--- /dev/null
+++ b/demos/sqlbrowser/sqlbrowser.pro
@@ -0,0 +1,25 @@
+TEMPLATE = app
+TARGET = sqlbrowser
+
+QT += sql
+
+HEADERS = browser.h connectionwidget.h qsqlconnectiondialog.h
+SOURCES = main.cpp browser.cpp connectionwidget.cpp qsqlconnectiondialog.cpp
+
+FORMS = browserwidget.ui qsqlconnectiondialog.ui
+build_all:!build_pass {
+ CONFIG -= build_all
+ CONFIG += release
+}
+
+# install
+target.path = $$[QT_INSTALL_DEMOS]/sqlbrowser
+sources.files = $$SOURCES $$HEADERS $$FORMS *.pro
+sources.path = $$[QT_INSTALL_DEMOS]/sqlbrowser
+INSTALLS += target sources
+
+include($$QT_SOURCE_TREE/demos/demobase.pri)
+
+wince*: {
+ DEPLOYMENT_PLUGIN += qsqlite
+}