add strategy support to sqlbrowser example
Several useful options added to context menu. Also added yellow background for unsubmitted changes. Change-Id: I132cd4498a5fb7275ea10c0497910aba99c06a2b Reviewed-by: Honglei Zhang <honglei.zhang@nokia.com> Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
This commit is contained in:
parent
698e620aad
commit
e999ca25c6
@ -52,6 +52,12 @@ Browser::Browser(QWidget *parent)
|
|||||||
|
|
||||||
table->addAction(insertRowAction);
|
table->addAction(insertRowAction);
|
||||||
table->addAction(deleteRowAction);
|
table->addAction(deleteRowAction);
|
||||||
|
table->addAction(fieldStrategyAction);
|
||||||
|
table->addAction(rowStrategyAction);
|
||||||
|
table->addAction(manualStrategyAction);
|
||||||
|
table->addAction(submitAction);
|
||||||
|
table->addAction(revertAction);
|
||||||
|
table->addAction(selectAction);
|
||||||
|
|
||||||
if (QSqlDatabase::drivers().isEmpty())
|
if (QSqlDatabase::drivers().isEmpty())
|
||||||
QMessageBox::information(this, tr("No database drivers found"),
|
QMessageBox::information(this, tr("No database drivers found"),
|
||||||
@ -144,7 +150,7 @@ void Browser::addConnection()
|
|||||||
|
|
||||||
void Browser::showTable(const QString &t)
|
void Browser::showTable(const QString &t)
|
||||||
{
|
{
|
||||||
QSqlTableModel *model = new QSqlTableModel(table, connectionWidget->currentDatabase());
|
QSqlTableModel *model = new CustomModel(table, connectionWidget->currentDatabase());
|
||||||
model->setEditStrategy(QSqlTableModel::OnRowChange);
|
model->setEditStrategy(QSqlTableModel::OnRowChange);
|
||||||
model->setTable(connectionWidget->currentDatabase().driver()->escapeIdentifier(t, QSqlDriver::TableName));
|
model->setTable(connectionWidget->currentDatabase().driver()->escapeIdentifier(t, QSqlDriver::TableName));
|
||||||
model->select();
|
model->select();
|
||||||
@ -215,8 +221,6 @@ void Browser::deleteRow()
|
|||||||
if (!model)
|
if (!model)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
||||||
|
|
||||||
QModelIndexList currentSelection = table->selectionModel()->selectedIndexes();
|
QModelIndexList currentSelection = table->selectionModel()->selectedIndexes();
|
||||||
for (int i = 0; i < currentSelection.count(); ++i) {
|
for (int i = 0; i < currentSelection.count(); ++i) {
|
||||||
if (currentSelection.at(i).column() != 0)
|
if (currentSelection.at(i).column() != 0)
|
||||||
@ -224,24 +228,79 @@ void Browser::deleteRow()
|
|||||||
model->removeRow(currentSelection.at(i).row());
|
model->removeRow(currentSelection.at(i).row());
|
||||||
}
|
}
|
||||||
|
|
||||||
model->submitAll();
|
|
||||||
model->setEditStrategy(QSqlTableModel::OnRowChange);
|
|
||||||
|
|
||||||
updateActions();
|
updateActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::updateActions()
|
void Browser::updateActions()
|
||||||
{
|
{
|
||||||
bool enableIns = qobject_cast<QSqlTableModel *>(table->model());
|
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
||||||
|
bool enableIns = tm;
|
||||||
bool enableDel = enableIns && table->currentIndex().isValid();
|
bool enableDel = enableIns && table->currentIndex().isValid();
|
||||||
|
|
||||||
insertRowAction->setEnabled(enableIns);
|
insertRowAction->setEnabled(enableIns);
|
||||||
deleteRowAction->setEnabled(enableDel);
|
deleteRowAction->setEnabled(enableDel);
|
||||||
|
|
||||||
|
fieldStrategyAction->setEnabled(tm);
|
||||||
|
rowStrategyAction->setEnabled(tm);
|
||||||
|
manualStrategyAction->setEnabled(tm);
|
||||||
|
submitAction->setEnabled(tm);
|
||||||
|
revertAction->setEnabled(tm);
|
||||||
|
selectAction->setEnabled(tm);
|
||||||
|
|
||||||
|
if (tm) {
|
||||||
|
QSqlTableModel::EditStrategy es = tm->editStrategy();
|
||||||
|
fieldStrategyAction->setChecked(es == QSqlTableModel::OnFieldChange);
|
||||||
|
rowStrategyAction->setChecked(es == QSqlTableModel::OnRowChange);
|
||||||
|
manualStrategyAction->setChecked(es == QSqlTableModel::OnManualSubmit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::about()
|
void Browser::about()
|
||||||
{
|
{
|
||||||
QMessageBox::about(this, tr("About"), tr("The SQL Browser demonstration "
|
QMessageBox::about(this, tr("About"), tr("The SQL Browser demonstration "
|
||||||
"shows how a data browser can be used to visualize the results of SQL"
|
"shows how a data browser can be used to visualize the results of SQL"
|
||||||
"statements on a live database"));
|
"statements on a live database"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Browser::on_fieldStrategyAction_triggered()
|
||||||
|
{
|
||||||
|
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
||||||
|
if (tm)
|
||||||
|
tm->setEditStrategy(QSqlTableModel::OnFieldChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Browser::on_rowStrategyAction_triggered()
|
||||||
|
{
|
||||||
|
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
||||||
|
if (tm)
|
||||||
|
tm->setEditStrategy(QSqlTableModel::OnRowChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Browser::on_manualStrategyAction_triggered()
|
||||||
|
{
|
||||||
|
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
||||||
|
if (tm)
|
||||||
|
tm->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Browser::on_submitAction_triggered()
|
||||||
|
{
|
||||||
|
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
||||||
|
if (tm)
|
||||||
|
tm->submitAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Browser::on_revertAction_triggered()
|
||||||
|
{
|
||||||
|
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
||||||
|
if (tm)
|
||||||
|
tm->revertAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Browser::on_selectAction_triggered()
|
||||||
|
{
|
||||||
|
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
||||||
|
if (tm)
|
||||||
|
tm->select();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
#define BROWSER_H
|
#define BROWSER_H
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QSqlTableModel>
|
||||||
#include "ui_browserwidget.h"
|
#include "ui_browserwidget.h"
|
||||||
|
|
||||||
class ConnectionWidget;
|
class ConnectionWidget;
|
||||||
@ -77,6 +78,12 @@ public slots:
|
|||||||
{ insertRow(); }
|
{ insertRow(); }
|
||||||
void on_deleteRowAction_triggered()
|
void on_deleteRowAction_triggered()
|
||||||
{ deleteRow(); }
|
{ deleteRow(); }
|
||||||
|
void on_fieldStrategyAction_triggered();
|
||||||
|
void on_rowStrategyAction_triggered();
|
||||||
|
void on_manualStrategyAction_triggered();
|
||||||
|
void on_submitAction_triggered();
|
||||||
|
void on_revertAction_triggered();
|
||||||
|
void on_selectAction_triggered();
|
||||||
void on_connectionWidget_tableActivated(const QString &table)
|
void on_connectionWidget_tableActivated(const QString &table)
|
||||||
{ showTable(table); }
|
{ showTable(table); }
|
||||||
void on_connectionWidget_metaDataRequested(const QString &table)
|
void on_connectionWidget_metaDataRequested(const QString &table)
|
||||||
@ -96,4 +103,17 @@ signals:
|
|||||||
void statusMessage(const QString &message);
|
void statusMessage(const QString &message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CustomModel: public QSqlTableModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
CustomModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase()):QSqlTableModel(parent, db) {}
|
||||||
|
QVariant data(const QModelIndex &idx, int role) const
|
||||||
|
{
|
||||||
|
if (role == Qt::BackgroundRole && isDirty(idx))
|
||||||
|
return QBrush(QColor(Qt::yellow));
|
||||||
|
return QSqlTableModel::data(idx, role);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
<ui version="4.0" >
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<author></author>
|
<ui version="4.0">
|
||||||
<comment></comment>
|
|
||||||
<exportmacro></exportmacro>
|
|
||||||
<class>Browser</class>
|
<class>Browser</class>
|
||||||
<widget class="QWidget" name="Browser" >
|
<widget class="QWidget" name="Browser">
|
||||||
<property name="geometry" >
|
<property name="geometry">
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
@ -12,100 +10,90 @@
|
|||||||
<height>515</height>
|
<height>515</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle" >
|
<property name="windowTitle">
|
||||||
<string>Qt SQL Browser</string>
|
<string>Qt SQL Browser</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" >
|
<layout class="QVBoxLayout">
|
||||||
<property name="margin" >
|
<property name="spacing">
|
||||||
<number>8</number>
|
|
||||||
</property>
|
|
||||||
<property name="spacing" >
|
|
||||||
<number>6</number>
|
<number>6</number>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="margin">
|
||||||
|
<number>8</number>
|
||||||
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QSplitter" name="splitter_2" >
|
<widget class="QSplitter" name="splitter_2">
|
||||||
<property name="sizePolicy" >
|
<property name="sizePolicy">
|
||||||
<sizepolicy>
|
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
||||||
<hsizetype>7</hsizetype>
|
|
||||||
<vsizetype>7</vsizetype>
|
|
||||||
<horstretch>0</horstretch>
|
<horstretch>0</horstretch>
|
||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<property name="orientation" >
|
<property name="orientation">
|
||||||
<enum>Qt::Horizontal</enum>
|
<enum>Qt::Horizontal</enum>
|
||||||
</property>
|
</property>
|
||||||
<widget class="ConnectionWidget" name="connectionWidget" >
|
<widget class="ConnectionWidget" name="connectionWidget">
|
||||||
<property name="sizePolicy" >
|
<property name="sizePolicy">
|
||||||
<sizepolicy>
|
<sizepolicy hsizetype="Ignored" vsizetype="Expanding">
|
||||||
<hsizetype>13</hsizetype>
|
|
||||||
<vsizetype>7</vsizetype>
|
|
||||||
<horstretch>1</horstretch>
|
<horstretch>1</horstretch>
|
||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QTableView" name="table" >
|
<widget class="QTableView" name="table">
|
||||||
<property name="sizePolicy" >
|
<property name="sizePolicy">
|
||||||
<sizepolicy>
|
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
||||||
<hsizetype>7</hsizetype>
|
|
||||||
<vsizetype>7</vsizetype>
|
|
||||||
<horstretch>2</horstretch>
|
<horstretch>2</horstretch>
|
||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<property name="contextMenuPolicy" >
|
<property name="contextMenuPolicy">
|
||||||
<enum>Qt::ActionsContextMenu</enum>
|
<enum>Qt::ActionsContextMenu</enum>
|
||||||
</property>
|
</property>
|
||||||
<property name="selectionBehavior" >
|
<property name="selectionBehavior">
|
||||||
<enum>QAbstractItemView::SelectRows</enum>
|
<enum>QAbstractItemView::SelectRows</enum>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QGroupBox" name="groupBox" >
|
<widget class="QGroupBox" name="groupBox">
|
||||||
<property name="sizePolicy" >
|
<property name="sizePolicy">
|
||||||
<sizepolicy>
|
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
|
||||||
<hsizetype>5</hsizetype>
|
|
||||||
<vsizetype>3</vsizetype>
|
|
||||||
<horstretch>0</horstretch>
|
<horstretch>0</horstretch>
|
||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<property name="maximumSize" >
|
<property name="maximumSize">
|
||||||
<size>
|
<size>
|
||||||
<width>16777215</width>
|
<width>16777215</width>
|
||||||
<height>180</height>
|
<height>180</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
<property name="title" >
|
<property name="title">
|
||||||
<string>SQL Query</string>
|
<string>SQL Query</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" >
|
<layout class="QVBoxLayout">
|
||||||
<property name="margin" >
|
<property name="spacing">
|
||||||
<number>9</number>
|
|
||||||
</property>
|
|
||||||
<property name="spacing" >
|
|
||||||
<number>6</number>
|
<number>6</number>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="margin">
|
||||||
|
<number>9</number>
|
||||||
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QTextEdit" name="sqlEdit" >
|
<widget class="QTextEdit" name="sqlEdit">
|
||||||
<property name="sizePolicy" >
|
<property name="sizePolicy">
|
||||||
<sizepolicy>
|
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
|
||||||
<hsizetype>7</hsizetype>
|
|
||||||
<vsizetype>3</vsizetype>
|
|
||||||
<horstretch>0</horstretch>
|
<horstretch>0</horstretch>
|
||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<property name="minimumSize" >
|
<property name="minimumSize">
|
||||||
<size>
|
<size>
|
||||||
<width>0</width>
|
<width>0</width>
|
||||||
<height>18</height>
|
<height>18</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
<property name="baseSize" >
|
<property name="baseSize">
|
||||||
<size>
|
<size>
|
||||||
<width>0</width>
|
<width>0</width>
|
||||||
<height>120</height>
|
<height>120</height>
|
||||||
@ -114,19 +102,19 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" >
|
<layout class="QHBoxLayout">
|
||||||
<property name="margin" >
|
<property name="spacing">
|
||||||
<number>1</number>
|
|
||||||
</property>
|
|
||||||
<property name="spacing" >
|
|
||||||
<number>6</number>
|
<number>6</number>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="margin">
|
||||||
|
<number>1</number>
|
||||||
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<spacer>
|
<spacer>
|
||||||
<property name="orientation" >
|
<property name="orientation">
|
||||||
<enum>Qt::Horizontal</enum>
|
<enum>Qt::Horizontal</enum>
|
||||||
</property>
|
</property>
|
||||||
<property name="sizeHint" >
|
<property name="sizeHint" stdset="0">
|
||||||
<size>
|
<size>
|
||||||
<width>40</width>
|
<width>40</width>
|
||||||
<height>20</height>
|
<height>20</height>
|
||||||
@ -135,15 +123,15 @@
|
|||||||
</spacer>
|
</spacer>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="clearButton" >
|
<widget class="QPushButton" name="clearButton">
|
||||||
<property name="text" >
|
<property name="text">
|
||||||
<string>&Clear</string>
|
<string>&Clear</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="submitButton" >
|
<widget class="QPushButton" name="submitButton">
|
||||||
<property name="text" >
|
<property name="text">
|
||||||
<string>&Submit</string>
|
<string>&Submit</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
@ -154,37 +142,91 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
<action name="insertRowAction" >
|
<action name="insertRowAction">
|
||||||
<property name="enabled" >
|
<property name="enabled">
|
||||||
<bool>false</bool>
|
<bool>false</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="text" >
|
<property name="text">
|
||||||
<string>&Insert Row</string>
|
<string>&Insert Row</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="statusTip" >
|
<property name="statusTip">
|
||||||
<string>Inserts a new Row</string>
|
<string>Inserts a new Row</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
<action name="deleteRowAction" >
|
<action name="deleteRowAction">
|
||||||
<property name="enabled" >
|
<property name="enabled">
|
||||||
<bool>false</bool>
|
<bool>false</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="text" >
|
<property name="text">
|
||||||
<string>&Delete Row</string>
|
<string>&Delete Row</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="statusTip" >
|
<property name="statusTip">
|
||||||
<string>Deletes the current Row</string>
|
<string>Deletes the current Row</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
|
<action name="fieldStrategyAction">
|
||||||
|
<property name="checkable">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Submit on &Field Change</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Commit on Field Change</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="rowStrategyAction">
|
||||||
|
<property name="checkable">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Submit on &Row Change</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Commit on Row Change</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="manualStrategyAction">
|
||||||
|
<property name="checkable">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Submit &Manually</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Commit Manually</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="submitAction">
|
||||||
|
<property name="text">
|
||||||
|
<string>&Submit All</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Submit Changes</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="revertAction">
|
||||||
|
<property name="text">
|
||||||
|
<string>&Revert All</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Revert</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="selectAction">
|
||||||
|
<property name="text">
|
||||||
|
<string>S&elect</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Refresh Data from Database</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
</widget>
|
</widget>
|
||||||
<pixmapfunction></pixmapfunction>
|
|
||||||
<customwidgets>
|
<customwidgets>
|
||||||
<customwidget>
|
<customwidget>
|
||||||
<class>ConnectionWidget</class>
|
<class>ConnectionWidget</class>
|
||||||
<extends>QTreeView</extends>
|
<extends>QTreeView</extends>
|
||||||
<header>connectionwidget.h</header>
|
<header>connectionwidget.h</header>
|
||||||
<container>0</container>
|
|
||||||
<pixmap></pixmap>
|
|
||||||
</customwidget>
|
</customwidget>
|
||||||
</customwidgets>
|
</customwidgets>
|
||||||
<tabstops>
|
<tabstops>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user