From 521d3cdaac72e941c9145ad44321a846bce92601 Mon Sep 17 00:00:00 2001 From: eelke Date: Sun, 1 Sep 2019 06:42:21 +0200 Subject: [PATCH] Add seperate actions for adding and removing connection groups. --- pglab/ConnectionController.cpp | 38 ++++++++++++++++++++++++++++++ pglab/ConnectionController.h | 2 ++ pglab/ConnectionListModel.cpp | 22 +++++++++++++++++ pglab/ConnectionListModel.h | 6 ++--- pglab/ConnectionManagerWindow.cpp | 11 +++++++++ pglab/ConnectionManagerWindow.h | 4 ++++ pglab/ConnectionManagerWindow.ui | 24 ++++++++++++++++++- pglab/icons/folder_add.png | Bin 0 -> 1572 bytes pglab/icons/folder_delete.png | Bin 0 -> 1568 bytes pglab/resources.qrc | 2 ++ 10 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 pglab/icons/folder_add.png create mode 100644 pglab/icons/folder_delete.png diff --git a/pglab/ConnectionController.cpp b/pglab/ConnectionController.cpp index f4890f9..939f512 100644 --- a/pglab/ConnectionController.cpp +++ b/pglab/ConnectionController.cpp @@ -8,6 +8,8 @@ #include "BackupDialog.h" #include "PasswordPromptDialog.h" #include "ConnectionConfigurationWidget.h" +#include +#include ConnectionController::ConnectionController(MasterController *parent) @@ -52,6 +54,14 @@ namespace { return dynamic_cast(node); } + ConnectionGroup* getGroupFromModelIndex(QModelIndex index) + { + if (!index.isValid()) + return nullptr; + auto node = static_cast(index.internalPointer()); + return dynamic_cast(node); + } + } @@ -103,6 +113,34 @@ void ConnectionController::editConnection(QModelIndex index) } } +void ConnectionController::addGroup() +{ + auto result = QInputDialog::getText(nullptr, tr("Add new connection group"), + tr("Group name")); + if (!result.isEmpty()) { + auto res = m_connectionTreeModel->addGroup(result); + if (std::holds_alternative(res)) { + QMessageBox::critical(nullptr, tr("Add group failed"), + tr("Failed to add group.\n") + + std::get(res).text()); + } + } +} + +void ConnectionController::removeGroup(QModelIndex index) +{ + auto group = getGroupFromModelIndex(index); + if (group) { + auto btn = QMessageBox::question(nullptr, tr("Connection group"), + tr("Remove the selected group and all connections contained in the group?"), + QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No), + QMessageBox::NoButton); + if (btn == QMessageBox::Yes) { + m_connectionTreeModel->removeGroup(index.row()); + } + } +} + void ConnectionController::openServerWindowForConnection(QModelIndex index) { auto config = getConfigFromModelIndex(index); diff --git a/pglab/ConnectionController.h b/pglab/ConnectionController.h index b8056bf..ffd51c9 100644 --- a/pglab/ConnectionController.h +++ b/pglab/ConnectionController.h @@ -34,6 +34,8 @@ public: /// Starts the form for editing a conncetion. /// This function returns immidiatly! void editConnection(QModelIndex index); + void addGroup(); + void removeGroup(QModelIndex index); private: MasterController *m_masterController; ConnectionList *m_connectionList = nullptr; diff --git a/pglab/ConnectionListModel.cpp b/pglab/ConnectionListModel.cpp index 1cb67c1..651fd75 100644 --- a/pglab/ConnectionListModel.cpp +++ b/pglab/ConnectionListModel.cpp @@ -726,6 +726,28 @@ std::variant ConnectionTreeModel::addGroup(QString group_name) return { err }; } return q.lastInsertId().toInt(); +std::optional ConnectionTreeModel::removeGroup(int row) +{ + beginRemoveRows({}, row, row); + SCOPE_EXIT { endRemoveRows(); }; + auto id = m_groups[row]->conngroup_id; + QSqlQuery q(m_db); + q.prepare("DELETE FROM connection WHERE conngroup_id=:id"); + q.bindValue(":id", id); + if (!q.exec()) { + auto err = q.lastError(); + return { err }; + } + q.prepare("DELETE FROM conngroup WHERE conngroup_id=:id"); + q.bindValue(":id", id); + if (!q.exec()) { + auto err = q.lastError(); + return { err }; + } + + m_groups.remove(row); + return {}; +} } std::optional ConnectionTreeModel::saveToDb(const ConnectionConfig &cc) diff --git a/pglab/ConnectionListModel.h b/pglab/ConnectionListModel.h index 1a56d0d..d9daf17 100644 --- a/pglab/ConnectionListModel.h +++ b/pglab/ConnectionListModel.h @@ -60,6 +60,9 @@ public: /** Save changed config, group is not allowed to change */ void save(const ConnectionConfig &cc); + /// Create a new group in the DB and place in the tree + std::variant addGroup(QString group_name); + std::optional removeGroup(int row); private: using Groups = QVector>; @@ -71,9 +74,6 @@ private: std::tuple findConfig(const QUuid uuid) const; int findGroup(QString name) const; - /// Create a new group in the DB and place in the tree - /// dataChanged is sent by this function - std::variant addGroup(QString group_name); std::optional saveToDb(const ConnectionConfig &cc); }; diff --git a/pglab/ConnectionManagerWindow.cpp b/pglab/ConnectionManagerWindow.cpp index 2f59300..20ffd5b 100644 --- a/pglab/ConnectionManagerWindow.cpp +++ b/pglab/ConnectionManagerWindow.cpp @@ -89,3 +89,14 @@ void ConnectionManagerWindow::on_actionConfigure_connection_triggered() auto ci = ui->treeView->selectionModel()->currentIndex(); m_connectionController->editConnection(ci); } + +void ConnectionManagerWindow::on_actionAdd_group_triggered() +{ + m_connectionController->addGroup(); +} + +void ConnectionManagerWindow::on_actionRemove_group_triggered() +{ + auto ci = ui->treeView->selectionModel()->currentIndex(); + m_connectionController->removeGroup(ci); +} diff --git a/pglab/ConnectionManagerWindow.h b/pglab/ConnectionManagerWindow.h index f49f711..c72b9cb 100644 --- a/pglab/ConnectionManagerWindow.h +++ b/pglab/ConnectionManagerWindow.h @@ -34,6 +34,10 @@ private slots: void on_actionConfigure_connection_triggered(); + void on_actionAdd_group_triggered(); + + void on_actionRemove_group_triggered(); + private: Ui::ConnectionManagerWindow *ui; MasterController *m_masterController; diff --git a/pglab/ConnectionManagerWindow.ui b/pglab/ConnectionManagerWindow.ui index 84d535f..79b6c2a 100644 --- a/pglab/ConnectionManagerWindow.ui +++ b/pglab/ConnectionManagerWindow.ui @@ -68,12 +68,14 @@ QToolButton { + + + - @@ -142,6 +144,26 @@ QToolButton { Configure connection + + + + :/icons/folder_add.png + + + + Add group + + + + + + :/icons/folder_delete.png + + + + Remove group + + diff --git a/pglab/icons/folder_add.png b/pglab/icons/folder_add.png new file mode 100644 index 0000000000000000000000000000000000000000..d881adf8e9202901244af1bafbc1c39dc939a9a7 GIT binary patch literal 1572 zcmV+<2HW|GP)4k@LZHNm0kuIuj3$~G9|=D&_$DD3O;BSn$`7eQK@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@>_-Sdy2;msN(!ng5LPw;}Ee|{a{IwJRcsHks}V3??0 zTrFOdZ0Bs1ivA;4;kz_~`rW(n=;61amaekYl}IAg(~TP+oPgHk!0AgPw`h7Av)f}9 zUE$#|cs4asWP(;joRt3o5={Z+YDTl)ufk6@~_G^TF|Ioa7{(5`#w4MV{1zbQthWPeTgzG zK1mF$mL~4STZH`6F# zy`(M0C`2v|zK%QaRC%Be_Jiq^Zd0^@WP&ViTSCr>|HLzE*1F;3eCKQ_79oguhfz<+ zFiql9OP5w#L??7=ly)kVdv_o*&wc)S!B?WMG=mKf&eTn-6ze9NR5Y#p#_?lRUX5iO zwJ3p0EFB;kre9Za%;K3ycqaNXQ!B_CTJ~<;cDLE7BG#s@Biw(#}h$NGo+*wySWAjg6uN!mUGWR`e<{BN#*1sDKhJ~EdS Sh`3Jx0000icons/constraints/unique.png icons/constraints/index.png icons/server_configuration.png + icons/folder_add.png + icons/folder_delete.png