Examples/sqlbrowser: improve coding style
Fix the coding style to match the current Qt style. Fixes: QTBUG-68661 Fixes: QTBUG-120909 Change-Id: I314ca9da8a03727e3e0336a23fce1ce9d065d3a4 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Kai Köhne <kai.koehne@qt.io> (cherry picked from commit 2690822428deec4f0c08f4d118d69a7c6036369e) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
02ba4b14dc
commit
fb453fe28c
@ -3,23 +3,49 @@
|
|||||||
|
|
||||||
#include "browser.h"
|
#include "browser.h"
|
||||||
#include "qsqlconnectiondialog.h"
|
#include "qsqlconnectiondialog.h"
|
||||||
|
#include <ui_browserwidget.h>
|
||||||
|
|
||||||
#include <QtWidgets>
|
#include <QAction>
|
||||||
#include <QtSql>
|
#include <QMessageBox>
|
||||||
|
#include <QStandardItemModel>
|
||||||
|
#include <QSqlDriver>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include <QSqlField>
|
||||||
|
#include <QSqlQuery>
|
||||||
|
#include <QSqlRecord>
|
||||||
|
#include <QTextEdit>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
Browser::Browser(QWidget *parent)
|
Browser::Browser(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
|
, m_ui(new Ui::Browser)
|
||||||
{
|
{
|
||||||
setupUi(this);
|
m_ui->setupUi(this);
|
||||||
|
|
||||||
table->addAction(insertRowAction);
|
m_ui->table->addAction(m_ui->insertRowAction);
|
||||||
table->addAction(deleteRowAction);
|
m_ui->table->addAction(m_ui->deleteRowAction);
|
||||||
table->addAction(fieldStrategyAction);
|
m_ui->table->addAction(m_ui->fieldStrategyAction);
|
||||||
table->addAction(rowStrategyAction);
|
m_ui->table->addAction(m_ui->rowStrategyAction);
|
||||||
table->addAction(manualStrategyAction);
|
m_ui->table->addAction(m_ui->manualStrategyAction);
|
||||||
table->addAction(submitAction);
|
m_ui->table->addAction(m_ui->submitAction);
|
||||||
table->addAction(revertAction);
|
m_ui->table->addAction(m_ui->revertAction);
|
||||||
table->addAction(selectAction);
|
m_ui->table->addAction(m_ui->selectAction);
|
||||||
|
|
||||||
|
connect(m_ui->insertRowAction, &QAction::triggered, this, &Browser::insertRow);
|
||||||
|
connect(m_ui->deleteRowAction, &QAction::triggered, this, &Browser::deleteRow);
|
||||||
|
connect(m_ui->fieldStrategyAction, &QAction::triggered, this, &Browser::onFieldStrategyAction);
|
||||||
|
connect(m_ui->rowStrategyAction, &QAction::triggered, this, &Browser::onRowStrategyAction);
|
||||||
|
connect(m_ui->sqlEdit, &QTextEdit::textChanged, this, &Browser::updateActions);
|
||||||
|
|
||||||
|
connect(m_ui->connectionWidget, &ConnectionWidget::tableActivated,
|
||||||
|
this, &Browser::showTable);
|
||||||
|
connect(m_ui->connectionWidget, &ConnectionWidget::metaDataRequested,
|
||||||
|
this, &Browser::showMetaData);
|
||||||
|
|
||||||
|
connect(m_ui->submitButton, &QPushButton::clicked,
|
||||||
|
this, &Browser::onSubmitButton);
|
||||||
|
connect(m_ui->clearButton, &QPushButton::clicked,
|
||||||
|
this, &Browser::onClearButton);
|
||||||
|
|
||||||
if (QSqlDatabase::drivers().isEmpty())
|
if (QSqlDatabase::drivers().isEmpty())
|
||||||
QMessageBox::information(this, tr("No database drivers found"),
|
QMessageBox::information(this, tr("No database drivers found"),
|
||||||
@ -27,18 +53,22 @@ Browser::Browser(QWidget *parent)
|
|||||||
"Please check the documentation how to build the "
|
"Please check the documentation how to build the "
|
||||||
"Qt SQL plugins."));
|
"Qt SQL plugins."));
|
||||||
|
|
||||||
emit statusMessage(tr("Ready."));
|
QTimer::singleShot(0, this, [this]() {
|
||||||
|
updateActions();
|
||||||
|
emit statusMessage(tr("Ready."));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Browser::~Browser()
|
Browser::~Browser()
|
||||||
{
|
{
|
||||||
|
delete m_ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::exec()
|
void Browser::exec()
|
||||||
{
|
{
|
||||||
QSqlQueryModel *model = new QSqlQueryModel(table);
|
QSqlQueryModel *model = new QSqlQueryModel(m_ui->table);
|
||||||
model->setQuery(QSqlQuery(sqlEdit->toPlainText(), connectionWidget->currentDatabase()));
|
model->setQuery(QSqlQuery(m_ui->sqlEdit->toPlainText(), m_ui->connectionWidget->currentDatabase()));
|
||||||
table->setModel(model);
|
m_ui->table->setModel(model);
|
||||||
|
|
||||||
if (model->lastError().type() != QSqlError::NoError)
|
if (model->lastError().type() != QSqlError::NoError)
|
||||||
emit statusMessage(model->lastError().text());
|
emit statusMessage(model->lastError().text());
|
||||||
@ -52,7 +82,7 @@ void Browser::exec()
|
|||||||
}
|
}
|
||||||
|
|
||||||
QSqlError Browser::addConnection(const QString &driver, const QString &dbName, const QString &host,
|
QSqlError Browser::addConnection(const QString &driver, const QString &dbName, const QString &host,
|
||||||
const QString &user, const QString &passwd, int port)
|
const QString &user, const QString &passwd, int port)
|
||||||
{
|
{
|
||||||
static int cCount = 0;
|
static int cCount = 0;
|
||||||
|
|
||||||
@ -66,7 +96,7 @@ QSqlError Browser::addConnection(const QString &driver, const QString &dbName, c
|
|||||||
db = QSqlDatabase();
|
db = QSqlDatabase();
|
||||||
QSqlDatabase::removeDatabase(QString("Browser%1").arg(cCount));
|
QSqlDatabase::removeDatabase(QString("Browser%1").arg(cCount));
|
||||||
}
|
}
|
||||||
connectionWidget->refresh();
|
m_ui->connectionWidget->refresh();
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@ -82,10 +112,14 @@ void Browser::openNewConnectionDialog()
|
|||||||
QSqlDatabase::removeDatabase("in_mem_db");
|
QSqlDatabase::removeDatabase("in_mem_db");
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "in_mem_db");
|
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "in_mem_db");
|
||||||
db.setDatabaseName(":memory:");
|
db.setDatabaseName(":memory:");
|
||||||
if (!db.open())
|
if (!db.open()) {
|
||||||
QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
|
QMessageBox::warning(this, tr("Unable to open database"),
|
||||||
"opening the connection: ") + db.lastError().text());
|
tr("An error occurred while "
|
||||||
QSqlQuery q("", db);
|
"opening the connection: %1") .arg(db.lastError().text()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
db.transaction();
|
||||||
|
QSqlQuery q(db);
|
||||||
q.exec("drop table Movies");
|
q.exec("drop table Movies");
|
||||||
q.exec("drop table Names");
|
q.exec("drop table Names");
|
||||||
q.exec("create table Movies (id integer primary key, Title varchar, Director varchar, Rating number)");
|
q.exec("create table Movies (id integer primary key, Title varchar, Director varchar, Rating number)");
|
||||||
@ -100,37 +134,43 @@ void Browser::openNewConnectionDialog()
|
|||||||
q.exec("insert into Names values (2, 'Donald', 'Duck', 'Andeby')");
|
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 (3, 'Buck', 'Rogers', 'Paris')");
|
||||||
q.exec("insert into Names values (4, 'Sherlock', 'Holmes', 'London')");
|
q.exec("insert into Names values (4, 'Sherlock', 'Holmes', 'London')");
|
||||||
connectionWidget->refresh();
|
db.commit();
|
||||||
|
m_ui->connectionWidget->refresh();
|
||||||
} else {
|
} else {
|
||||||
QSqlError err = addConnection(dialog.driverName(), dialog.databaseName(), dialog.hostName(),
|
QSqlError err = addConnection(dialog.driverName(), dialog.databaseName(), dialog.hostName(),
|
||||||
dialog.userName(), dialog.password(), dialog.port());
|
dialog.userName(), dialog.password(), dialog.port());
|
||||||
if (err.type() != QSqlError::NoError)
|
if (err.type() != QSqlError::NoError)
|
||||||
QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
|
QMessageBox::warning(this, tr("Unable to open database"),
|
||||||
"opening the connection: ") + err.text());
|
tr("An error occurred while "
|
||||||
|
"opening the connection: %1").arg(err.text()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::showTable(const QString &t)
|
void Browser::showTable(const QString &t)
|
||||||
{
|
{
|
||||||
QSqlTableModel *model = new CustomModel(table, connectionWidget->currentDatabase());
|
QSqlTableModel *model = new CustomModel(m_ui->table, m_ui->connectionWidget->currentDatabase());
|
||||||
model->setEditStrategy(QSqlTableModel::OnRowChange);
|
model->setEditStrategy(QSqlTableModel::OnRowChange);
|
||||||
model->setTable(connectionWidget->currentDatabase().driver()->escapeIdentifier(t, QSqlDriver::TableName));
|
model->setTable(m_ui->connectionWidget->currentDatabase().driver()->escapeIdentifier(t, QSqlDriver::TableName));
|
||||||
model->select();
|
model->select();
|
||||||
if (model->lastError().type() != QSqlError::NoError)
|
if (model->lastError().type() != QSqlError::NoError)
|
||||||
emit statusMessage(model->lastError().text());
|
emit statusMessage(model->lastError().text());
|
||||||
|
|
||||||
table->setModel(model);
|
m_ui->table->setModel(model);
|
||||||
table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
|
m_ui->table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
|
||||||
connect(table->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
connect(m_ui->table->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
||||||
this, &Browser::currentChanged);
|
this, &Browser::updateActions);
|
||||||
|
|
||||||
|
connect(m_ui->submitAction, &QAction::triggered, model, &QSqlTableModel::submitAll);
|
||||||
|
connect(m_ui->revertAction, &QAction::triggered, model, &QSqlTableModel::revertAll);
|
||||||
|
connect(m_ui->selectAction, &QAction::triggered, model, &QSqlTableModel::select);
|
||||||
|
|
||||||
updateActions();
|
updateActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::showMetaData(const QString &t)
|
void Browser::showMetaData(const QString &t)
|
||||||
{
|
{
|
||||||
QSqlRecord rec = connectionWidget->currentDatabase().record(t);
|
QSqlRecord rec = m_ui->connectionWidget->currentDatabase().record(t);
|
||||||
QStandardItemModel *model = new QStandardItemModel(table);
|
QStandardItemModel *model = new QStandardItemModel(m_ui->table);
|
||||||
|
|
||||||
model->insertRows(0, rec.count());
|
model->insertRows(0, rec.count());
|
||||||
model->insertColumns(0, 7);
|
model->insertColumns(0, 7);
|
||||||
@ -157,37 +197,37 @@ void Browser::showMetaData(const QString &t)
|
|||||||
model->setData(model->index(i, 6), fld.defaultValue());
|
model->setData(model->index(i, 6), fld.defaultValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
table->setModel(model);
|
m_ui->table->setModel(model);
|
||||||
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
m_ui->table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||||
|
|
||||||
updateActions();
|
updateActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::insertRow()
|
void Browser::insertRow()
|
||||||
{
|
{
|
||||||
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
|
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(m_ui->table->model());
|
||||||
if (!model)
|
if (!model)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QModelIndex insertIndex = table->currentIndex();
|
QModelIndex insertIndex = m_ui->table->currentIndex();
|
||||||
int row = insertIndex.row() == -1 ? 0 : insertIndex.row();
|
int row = insertIndex.row() == -1 ? 0 : insertIndex.row();
|
||||||
model->insertRow(row);
|
model->insertRow(row);
|
||||||
insertIndex = model->index(row, 0);
|
insertIndex = model->index(row, 0);
|
||||||
table->setCurrentIndex(insertIndex);
|
m_ui->table->setCurrentIndex(insertIndex);
|
||||||
table->edit(insertIndex);
|
m_ui->table->edit(insertIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::deleteRow()
|
void Browser::deleteRow()
|
||||||
{
|
{
|
||||||
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
|
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(m_ui->table->model());
|
||||||
if (!model)
|
if (!model)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QModelIndexList currentSelection = table->selectionModel()->selectedIndexes();
|
const QModelIndexList currentSelection = m_ui->table->selectionModel()->selectedIndexes();
|
||||||
for (int i = 0; i < currentSelection.count(); ++i) {
|
for (const auto &idx : currentSelection) {
|
||||||
if (currentSelection.at(i).column() != 0)
|
if (idx.column() != 0)
|
||||||
continue;
|
continue;
|
||||||
model->removeRow(currentSelection.at(i).row());
|
model->removeRow(idx.row());
|
||||||
}
|
}
|
||||||
|
|
||||||
updateActions();
|
updateActions();
|
||||||
@ -195,74 +235,70 @@ void Browser::deleteRow()
|
|||||||
|
|
||||||
void Browser::updateActions()
|
void Browser::updateActions()
|
||||||
{
|
{
|
||||||
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->table->model());
|
||||||
bool enableIns = tm;
|
bool enableIns = tm != nullptr;
|
||||||
bool enableDel = enableIns && table->currentIndex().isValid();
|
bool enableDel = enableIns && m_ui->table->currentIndex().isValid();
|
||||||
|
|
||||||
insertRowAction->setEnabled(enableIns);
|
m_ui->insertRowAction->setEnabled(enableIns);
|
||||||
deleteRowAction->setEnabled(enableDel);
|
m_ui->deleteRowAction->setEnabled(enableDel);
|
||||||
|
|
||||||
fieldStrategyAction->setEnabled(tm);
|
m_ui->submitAction->setEnabled(tm);
|
||||||
rowStrategyAction->setEnabled(tm);
|
m_ui->revertAction->setEnabled(tm);
|
||||||
manualStrategyAction->setEnabled(tm);
|
m_ui->selectAction->setEnabled(tm);
|
||||||
submitAction->setEnabled(tm);
|
|
||||||
revertAction->setEnabled(tm);
|
const bool isEmpty = m_ui->sqlEdit->toPlainText().isEmpty();
|
||||||
selectAction->setEnabled(tm);
|
m_ui->submitButton->setEnabled(m_ui->connectionWidget->currentDatabase().isOpen() && !isEmpty);
|
||||||
|
m_ui->clearButton->setEnabled(!isEmpty);
|
||||||
|
|
||||||
if (tm) {
|
if (tm) {
|
||||||
QSqlTableModel::EditStrategy es = tm->editStrategy();
|
QSqlTableModel::EditStrategy es = tm->editStrategy();
|
||||||
fieldStrategyAction->setChecked(es == QSqlTableModel::OnFieldChange);
|
m_ui->fieldStrategyAction->setChecked(es == QSqlTableModel::OnFieldChange);
|
||||||
rowStrategyAction->setChecked(es == QSqlTableModel::OnRowChange);
|
m_ui->rowStrategyAction->setChecked(es == QSqlTableModel::OnRowChange);
|
||||||
manualStrategyAction->setChecked(es == QSqlTableModel::OnManualSubmit);
|
m_ui->manualStrategyAction->setChecked(es == QSqlTableModel::OnManualSubmit);
|
||||||
|
} else {
|
||||||
|
m_ui->fieldStrategyAction->setEnabled(false);
|
||||||
|
m_ui->rowStrategyAction->setEnabled(false);
|
||||||
|
m_ui->manualStrategyAction->setEnabled(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::about()
|
void Browser::about()
|
||||||
{
|
{
|
||||||
QMessageBox::about(this, tr("About"), tr("The SQL Browser demonstration "
|
QMessageBox::about(this, tr("About"),
|
||||||
"shows how a data browser can be used to visualize the results of SQL"
|
tr("The SQL Browser demonstration shows how a data browser "
|
||||||
"statements on a live database"));
|
"can be used to visualize the results of SQL "
|
||||||
|
"statements on a live database"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::on_fieldStrategyAction_triggered()
|
void Browser::onFieldStrategyAction()
|
||||||
{
|
{
|
||||||
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->table->model());
|
||||||
if (tm)
|
if (tm)
|
||||||
tm->setEditStrategy(QSqlTableModel::OnFieldChange);
|
tm->setEditStrategy(QSqlTableModel::OnFieldChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::on_rowStrategyAction_triggered()
|
void Browser::onRowStrategyAction()
|
||||||
{
|
{
|
||||||
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->table->model());
|
||||||
if (tm)
|
if (tm)
|
||||||
tm->setEditStrategy(QSqlTableModel::OnRowChange);
|
tm->setEditStrategy(QSqlTableModel::OnRowChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::on_manualStrategyAction_triggered()
|
void Browser::onManualStrategyAction()
|
||||||
{
|
{
|
||||||
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->table->model());
|
||||||
if (tm)
|
if (tm)
|
||||||
tm->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
tm->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::on_submitAction_triggered()
|
void Browser::onSubmitButton()
|
||||||
{
|
{
|
||||||
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
exec();
|
||||||
if (tm)
|
m_ui->sqlEdit->setFocus();
|
||||||
tm->submitAll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::on_revertAction_triggered()
|
void Browser::onClearButton()
|
||||||
{
|
{
|
||||||
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
m_ui->sqlEdit->clear();
|
||||||
if (tm)
|
m_ui->sqlEdit->setFocus();
|
||||||
tm->revertAll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::on_selectAction_triggered()
|
|
||||||
{
|
|
||||||
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
|
|
||||||
if (tm)
|
|
||||||
tm->select();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -6,72 +6,58 @@
|
|||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <QSqlTableModel>
|
#include <QSqlTableModel>
|
||||||
#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)
|
QT_FORWARD_DECLARE_CLASS(QSqlError)
|
||||||
|
|
||||||
class Browser: public QWidget, private Ui::Browser
|
QT_BEGIN_NAMESPACE
|
||||||
|
namespace Ui
|
||||||
|
{
|
||||||
|
class Browser;
|
||||||
|
}
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
class Browser : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Browser(QWidget *parent = nullptr);
|
Browser(QWidget *parent = nullptr);
|
||||||
virtual ~Browser();
|
~Browser();
|
||||||
|
|
||||||
QSqlError addConnection(const QString &driver, const QString &dbName, const QString &host,
|
QSqlError addConnection(const QString &driver, const QString &dbName, const QString &host,
|
||||||
const QString &user, const QString &passwd, int port = -1);
|
const QString &user, const QString &passwd, int port);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void openNewConnectionDialog();
|
||||||
|
void about();
|
||||||
|
|
||||||
|
protected:
|
||||||
void insertRow();
|
void insertRow();
|
||||||
void deleteRow();
|
void deleteRow();
|
||||||
void updateActions();
|
void updateActions();
|
||||||
|
|
||||||
public slots:
|
protected slots:
|
||||||
void exec();
|
void exec();
|
||||||
void showTable(const QString &table);
|
void showTable(const QString &table);
|
||||||
void showMetaData(const QString &table);
|
void showMetaData(const QString &table);
|
||||||
void openNewConnectionDialog();
|
|
||||||
void currentChanged() { updateActions(); }
|
|
||||||
void about();
|
|
||||||
|
|
||||||
void on_insertRowAction_triggered()
|
void onFieldStrategyAction();
|
||||||
{ insertRow(); }
|
void onRowStrategyAction();
|
||||||
void on_deleteRowAction_triggered()
|
void onManualStrategyAction();
|
||||||
{ deleteRow(); }
|
void onSubmitButton();
|
||||||
void on_fieldStrategyAction_triggered();
|
void onClearButton();
|
||||||
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)
|
|
||||||
{ 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:
|
signals:
|
||||||
void statusMessage(const QString &message);
|
void statusMessage(const QString &message);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ui::Browser *m_ui;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CustomModel: public QSqlTableModel
|
class CustomModel : public QSqlTableModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit CustomModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase())
|
using QSqlTableModel::QSqlTableModel;
|
||||||
: QSqlTableModel(parent, db) {}
|
|
||||||
|
|
||||||
QVariant data(const QModelIndex &idx, int role) const override
|
QVariant data(const QModelIndex &idx, int role) const override
|
||||||
{
|
{
|
||||||
if (role == Qt::BackgroundRole && isDirty(idx))
|
if (role == Qt::BackgroundRole && isDirty(idx))
|
||||||
|
@ -3,17 +3,19 @@
|
|||||||
|
|
||||||
#include "connectionwidget.h"
|
#include "connectionwidget.h"
|
||||||
|
|
||||||
#include <QtWidgets>
|
#include <QAction>
|
||||||
#include <QtSql>
|
#include <QHeaderView>
|
||||||
|
#include <QSqlDatabase>
|
||||||
|
#include <QTreeWidget>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
|
||||||
ConnectionWidget::ConnectionWidget(QWidget *parent)
|
ConnectionWidget::ConnectionWidget(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
|
, tree(new QTreeWidget(this))
|
||||||
{
|
{
|
||||||
QVBoxLayout *layout = new QVBoxLayout(this);
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
||||||
tree = new QTreeWidget(this);
|
tree->setHeaderLabels(QStringList(tr("Database")));
|
||||||
tree->setObjectName(QLatin1String("tree"));
|
tree->header()->setStretchLastSection(true);
|
||||||
tree->setHeaderLabels(QStringList(tr("database")));
|
|
||||||
tree->header()->setSectionResizeMode(QHeaderView::Stretch);
|
|
||||||
QAction *refreshAction = new QAction(tr("Refresh"), tree);
|
QAction *refreshAction = new QAction(tr("Refresh"), tree);
|
||||||
metaDataAction = new QAction(tr("Show Schema"), tree);
|
metaDataAction = new QAction(tr("Show Schema"), tree);
|
||||||
connect(refreshAction, &QAction::triggered, this, &ConnectionWidget::refresh);
|
connect(refreshAction, &QAction::triggered, this, &ConnectionWidget::refresh);
|
||||||
@ -24,34 +26,37 @@ ConnectionWidget::ConnectionWidget(QWidget *parent)
|
|||||||
|
|
||||||
layout->addWidget(tree);
|
layout->addWidget(tree);
|
||||||
|
|
||||||
QMetaObject::connectSlotsByName(this);
|
connect(tree, &QTreeWidget::itemActivated,
|
||||||
|
this, &ConnectionWidget::onItemActivated);
|
||||||
|
connect(tree, &QTreeWidget::currentItemChanged,
|
||||||
|
this, &ConnectionWidget::onCurrentItemChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionWidget::~ConnectionWidget()
|
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()
|
void ConnectionWidget::refresh()
|
||||||
{
|
{
|
||||||
|
const auto qDBCaption = [](const QSqlDatabase &db)
|
||||||
|
{
|
||||||
|
QString nm = db.driverName() + QLatin1Char(':');
|
||||||
|
if (!db.userName().isEmpty())
|
||||||
|
nm += db.userName() + QLatin1Char('@');
|
||||||
|
nm += db.databaseName();
|
||||||
|
return nm;
|
||||||
|
};
|
||||||
|
|
||||||
tree->clear();
|
tree->clear();
|
||||||
QStringList connectionNames = QSqlDatabase::connectionNames();
|
const QStringList connectionNames = QSqlDatabase::connectionNames();
|
||||||
|
|
||||||
bool gotActiveDb = false;
|
bool gotActiveDb = false;
|
||||||
for (int i = 0; i < connectionNames.count(); ++i) {
|
for (const auto &connectionName : connectionNames) {
|
||||||
QTreeWidgetItem *root = new QTreeWidgetItem(tree);
|
QTreeWidgetItem *root = new QTreeWidgetItem(tree);
|
||||||
QSqlDatabase db = QSqlDatabase::database(connectionNames.at(i), false);
|
QSqlDatabase db = QSqlDatabase::database(connectionName, false);
|
||||||
root->setText(0, qDBCaption(db));
|
root->setText(0, qDBCaption(db));
|
||||||
if (connectionNames.at(i) == activeDb) {
|
if (connectionName == activeDb) {
|
||||||
gotActiveDb = true;
|
gotActiveDb = true;
|
||||||
setActive(root);
|
setActive(root);
|
||||||
}
|
}
|
||||||
@ -76,15 +81,15 @@ QSqlDatabase ConnectionWidget::currentDatabase() const
|
|||||||
return QSqlDatabase::database(activeDb);
|
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)
|
void ConnectionWidget::setActive(QTreeWidgetItem *item)
|
||||||
{
|
{
|
||||||
|
const auto qSetBold = [](QTreeWidgetItem *item, bool bold)
|
||||||
|
{
|
||||||
|
QFont font = item->font(0);
|
||||||
|
font.setBold(bold);
|
||||||
|
item->setFont(0, font);
|
||||||
|
};
|
||||||
|
|
||||||
for (int i = 0; i < tree->topLevelItemCount(); ++i) {
|
for (int i = 0; i < tree->topLevelItemCount(); ++i) {
|
||||||
if (tree->topLevelItem(i)->font(0).bold())
|
if (tree->topLevelItem(i)->font(0).bold())
|
||||||
qSetBold(tree->topLevelItem(i), false);
|
qSetBold(tree->topLevelItem(i), false);
|
||||||
@ -97,7 +102,7 @@ void ConnectionWidget::setActive(QTreeWidgetItem *item)
|
|||||||
activeDb = QSqlDatabase::connectionNames().value(tree->indexOfTopLevelItem(item));
|
activeDb = QSqlDatabase::connectionNames().value(tree->indexOfTopLevelItem(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionWidget::on_tree_itemActivated(QTreeWidgetItem *item, int /* column */)
|
void ConnectionWidget::onItemActivated(QTreeWidgetItem *item)
|
||||||
{
|
{
|
||||||
if (!item)
|
if (!item)
|
||||||
return;
|
return;
|
||||||
@ -119,7 +124,7 @@ void ConnectionWidget::showMetaData()
|
|||||||
emit metaDataRequested(cItem->text(0));
|
emit metaDataRequested(cItem->text(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionWidget::on_tree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
|
void ConnectionWidget::onCurrentItemChanged(QTreeWidgetItem *current)
|
||||||
{
|
{
|
||||||
metaDataAction->setEnabled(current && current->parent());
|
metaDataAction->setEnabled(current && current->parent());
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,13 @@
|
|||||||
QT_FORWARD_DECLARE_CLASS(QTreeWidget)
|
QT_FORWARD_DECLARE_CLASS(QTreeWidget)
|
||||||
QT_FORWARD_DECLARE_CLASS(QTreeWidgetItem)
|
QT_FORWARD_DECLARE_CLASS(QTreeWidgetItem)
|
||||||
QT_FORWARD_DECLARE_CLASS(QSqlDatabase)
|
QT_FORWARD_DECLARE_CLASS(QSqlDatabase)
|
||||||
QT_FORWARD_DECLARE_CLASS(QMenu)
|
|
||||||
|
|
||||||
class ConnectionWidget: public QWidget
|
class ConnectionWidget : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
ConnectionWidget(QWidget *parent = nullptr);
|
ConnectionWidget(QWidget *parent = nullptr);
|
||||||
virtual ~ConnectionWidget();
|
~ConnectionWidget();
|
||||||
|
|
||||||
QSqlDatabase currentDatabase() const;
|
QSqlDatabase currentDatabase() const;
|
||||||
|
|
||||||
@ -27,8 +26,8 @@ signals:
|
|||||||
public slots:
|
public slots:
|
||||||
void refresh();
|
void refresh();
|
||||||
void showMetaData();
|
void showMetaData();
|
||||||
void on_tree_itemActivated(QTreeWidgetItem *item, int column);
|
void onItemActivated(QTreeWidgetItem *item);
|
||||||
void on_tree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
|
void onCurrentItemChanged(QTreeWidgetItem *current);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setActive(QTreeWidgetItem *);
|
void setActive(QTreeWidgetItem *);
|
||||||
|
@ -3,16 +3,21 @@
|
|||||||
|
|
||||||
#include "browser.h"
|
#include "browser.h"
|
||||||
|
|
||||||
#include <QtCore>
|
#include <QApplication>
|
||||||
#include <QtWidgets>
|
#include <QMainWindow>
|
||||||
#include <QtSql>
|
#include <QMenu>
|
||||||
|
#include <QMenuBar>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include <QStatusBar>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
void addConnectionsFromCommandline(const QStringList &args, Browser *browser)
|
void addConnectionsFromCommandline(const QStringList &args, Browser *browser)
|
||||||
{
|
{
|
||||||
for (int i = 1; i < args.count(); ++i) {
|
for (qsizetype i = 1; i < args.count(); ++i) {
|
||||||
QUrl url(args.at(i), QUrl::TolerantMode);
|
const auto &arg = args.at(i);
|
||||||
|
const QUrl url(arg, QUrl::TolerantMode);
|
||||||
if (!url.isValid()) {
|
if (!url.isValid()) {
|
||||||
qWarning("Invalid URL: %s", qPrintable(args.at(i)));
|
qWarning("Invalid URL: %s", qPrintable(arg));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QSqlError err = browser->addConnection(url.scheme(), url.path().mid(1), url.host(),
|
QSqlError err = browser->addConnection(url.scheme(), url.path().mid(1), url.host(),
|
||||||
@ -27,23 +32,26 @@ int main(int argc, char *argv[])
|
|||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
QMainWindow mainWin;
|
QMainWindow mainWin;
|
||||||
mainWin.setWindowTitle(QObject::tr("Qt SQL Browser"));
|
mainWin.setWindowTitle(QApplication::translate("MainWindow", "Qt SQL Browser"));
|
||||||
|
|
||||||
Browser browser(&mainWin);
|
Browser browser(&mainWin);
|
||||||
mainWin.setCentralWidget(&browser);
|
mainWin.setCentralWidget(&browser);
|
||||||
|
|
||||||
QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File"));
|
QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File"));
|
||||||
fileMenu->addAction(QObject::tr("Add &Connection..."), &browser,
|
fileMenu->addAction(QApplication::translate("MainWindow", "Add &Connection..."),
|
||||||
&Browser::openNewConnectionDialog);
|
&browser, &Browser::openNewConnectionDialog);
|
||||||
fileMenu->addSeparator();
|
fileMenu->addSeparator();
|
||||||
fileMenu->addAction(QObject::tr("&Quit"), qApp, &QApplication::quit);
|
fileMenu->addAction(QApplication::translate("MainWindow", "&Quit"),
|
||||||
|
qApp, &QApplication::quit);
|
||||||
|
|
||||||
QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help"));
|
QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help"));
|
||||||
helpMenu->addAction(QObject::tr("About"), &browser, &Browser::about);
|
helpMenu->addAction(QApplication::translate("MainWindow", "About"),
|
||||||
helpMenu->addAction(QObject::tr("About Qt"), qApp, &QApplication::aboutQt);
|
&browser, &Browser::about);
|
||||||
|
helpMenu->addAction(QApplication::translate("MainWindow", "About Qt"),
|
||||||
|
qApp, &QApplication::aboutQt);
|
||||||
|
|
||||||
QObject::connect(&browser, &Browser::statusMessage, &mainWin,
|
QObject::connect(&browser, &Browser::statusMessage,
|
||||||
[&mainWin](const QString &text) { mainWin.statusBar()->showMessage(text); });
|
&mainWin, [&mainWin](const QString &text) { mainWin.statusBar()->showMessage(text); });
|
||||||
|
|
||||||
addConnectionsFromCommandline(app.arguments(), &browser);
|
addConnectionsFromCommandline(app.arguments(), &browser);
|
||||||
mainWin.show();
|
mainWin.show();
|
||||||
|
@ -2,69 +2,86 @@
|
|||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||||
|
|
||||||
#include "qsqlconnectiondialog.h"
|
#include "qsqlconnectiondialog.h"
|
||||||
#include "ui_qsqlconnectiondialog.h"
|
#include <ui_qsqlconnectiondialog.h>
|
||||||
|
|
||||||
|
#include <QCheckBox>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QPushButton>
|
||||||
#include <QSqlDatabase>
|
#include <QSqlDatabase>
|
||||||
|
|
||||||
QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent)
|
QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
|
, m_ui(new Ui::QSqlConnectionDialogUi)
|
||||||
{
|
{
|
||||||
ui.setupUi(this);
|
m_ui->setupUi(this);
|
||||||
|
|
||||||
QStringList drivers = QSqlDatabase::drivers();
|
QStringList drivers = QSqlDatabase::drivers();
|
||||||
|
|
||||||
if (!drivers.contains("QSQLITE"))
|
if (!drivers.contains("QSQLITE"))
|
||||||
ui.dbCheckBox->setEnabled(false);
|
m_ui->dbCheckBox->setEnabled(false);
|
||||||
|
|
||||||
ui.comboDriver->addItems(drivers);
|
m_ui->comboDriver->addItems(drivers);
|
||||||
|
|
||||||
|
connect(m_ui->okButton, &QPushButton::clicked,
|
||||||
|
this, &QSqlConnectionDialog::onOkButton);
|
||||||
|
connect(m_ui->cancelButton, &QPushButton::clicked,
|
||||||
|
this, &QSqlConnectionDialog::reject);
|
||||||
|
connect(m_ui->dbCheckBox, &QCheckBox::stateChanged,
|
||||||
|
this, &QSqlConnectionDialog::onDbCheckBox);
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlConnectionDialog::~QSqlConnectionDialog()
|
QSqlConnectionDialog::~QSqlConnectionDialog()
|
||||||
{
|
{
|
||||||
|
delete m_ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QSqlConnectionDialog::driverName() const
|
QString QSqlConnectionDialog::driverName() const
|
||||||
{
|
{
|
||||||
return ui.comboDriver->currentText();
|
return m_ui->comboDriver->currentText();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QSqlConnectionDialog::databaseName() const
|
QString QSqlConnectionDialog::databaseName() const
|
||||||
{
|
{
|
||||||
return ui.editDatabase->text();
|
return m_ui->editDatabase->text();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QSqlConnectionDialog::userName() const
|
QString QSqlConnectionDialog::userName() const
|
||||||
{
|
{
|
||||||
return ui.editUsername->text();
|
return m_ui->editUsername->text();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QSqlConnectionDialog::password() const
|
QString QSqlConnectionDialog::password() const
|
||||||
{
|
{
|
||||||
return ui.editPassword->text();
|
return m_ui->editPassword->text();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QSqlConnectionDialog::hostName() const
|
QString QSqlConnectionDialog::hostName() const
|
||||||
{
|
{
|
||||||
return ui.editHostname->text();
|
return m_ui->editHostname->text();
|
||||||
}
|
}
|
||||||
|
|
||||||
int QSqlConnectionDialog::port() const
|
int QSqlConnectionDialog::port() const
|
||||||
{
|
{
|
||||||
return ui.portSpinBox->value();
|
return m_ui->portSpinBox->value();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QSqlConnectionDialog::useInMemoryDatabase() const
|
bool QSqlConnectionDialog::useInMemoryDatabase() const
|
||||||
{
|
{
|
||||||
return ui.dbCheckBox->isChecked();
|
return m_ui->dbCheckBox->isChecked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QSqlConnectionDialog::on_okButton_clicked()
|
void QSqlConnectionDialog::onOkButton()
|
||||||
{
|
{
|
||||||
if (ui.comboDriver->currentText().isEmpty()) {
|
if (m_ui->comboDriver->currentText().isEmpty()) {
|
||||||
QMessageBox::information(this, tr("No database driver selected"),
|
QMessageBox::information(this, tr("No database driver selected"),
|
||||||
tr("Please select a database driver"));
|
tr("Please select a database driver"));
|
||||||
ui.comboDriver->setFocus();
|
m_ui->comboDriver->setFocus();
|
||||||
} else {
|
} else {
|
||||||
accept();
|
accept();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QSqlConnectionDialog::onDbCheckBox()
|
||||||
|
{
|
||||||
|
m_ui->connGroupBox->setEnabled(!m_ui->dbCheckBox->isChecked());
|
||||||
|
}
|
||||||
|
@ -5,11 +5,15 @@
|
|||||||
#define QSQLCONNECTIONDIALOG_H
|
#define QSQLCONNECTIONDIALOG_H
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
#include <QMessageBox>
|
|
||||||
|
|
||||||
#include "ui_qsqlconnectiondialog.h"
|
QT_BEGIN_NAMESPACE
|
||||||
|
namespace Ui
|
||||||
|
{
|
||||||
|
class QSqlConnectionDialogUi;
|
||||||
|
}
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
class QSqlConnectionDialog: public QDialog
|
class QSqlConnectionDialog : public QDialog
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@ -25,12 +29,11 @@ public:
|
|||||||
bool useInMemoryDatabase() const;
|
bool useInMemoryDatabase() const;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_okButton_clicked();
|
void onOkButton();
|
||||||
void on_cancelButton_clicked() { reject(); }
|
void onDbCheckBox();
|
||||||
void on_dbCheckBox_clicked() { ui.connGroupBox->setEnabled(!ui.dbCheckBox->isChecked()); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::QSqlConnectionDialogUi ui;
|
Ui::QSqlConnectionDialogUi *m_ui;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user