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 0000000..d881adf Binary files /dev/null and b/pglab/icons/folder_add.png differ diff --git a/pglab/icons/folder_delete.png b/pglab/icons/folder_delete.png new file mode 100644 index 0000000..f35b7a3 Binary files /dev/null and b/pglab/icons/folder_delete.png differ diff --git a/pglab/resources.qrc b/pglab/resources.qrc index 689fb61..b86a06f 100644 --- a/pglab/resources.qrc +++ b/pglab/resources.qrc @@ -29,5 +29,7 @@ icons/constraints/unique.png icons/constraints/index.png icons/server_configuration.png + icons/folder_add.png + icons/folder_delete.png