Add seperate actions for adding and removing connection groups.

This commit is contained in:
eelke 2019-09-01 06:42:21 +02:00
parent dbb6e1ab01
commit 521d3cdaac
10 changed files with 105 additions and 4 deletions

View file

@ -8,6 +8,8 @@
#include "BackupDialog.h"
#include "PasswordPromptDialog.h"
#include "ConnectionConfigurationWidget.h"
#include <QInputDialog>
#include <QMessageBox>
ConnectionController::ConnectionController(MasterController *parent)
@ -52,6 +54,14 @@ namespace {
return dynamic_cast<ConnectionConfig*>(node);
}
ConnectionGroup* getGroupFromModelIndex(QModelIndex index)
{
if (!index.isValid())
return nullptr;
auto node = static_cast<ConnectionNode*>(index.internalPointer());
return dynamic_cast<ConnectionGroup*>(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<QSqlError>(res)) {
QMessageBox::critical(nullptr, tr("Add group failed"),
tr("Failed to add group.\n") +
std::get<QSqlError>(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);

View file

@ -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;

View file

@ -726,6 +726,28 @@ std::variant<int, QSqlError> ConnectionTreeModel::addGroup(QString group_name)
return { err };
}
return q.lastInsertId().toInt();
std::optional<QSqlError> 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<QSqlError> ConnectionTreeModel::saveToDb(const ConnectionConfig &cc)

View file

@ -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<int, QSqlError> addGroup(QString group_name);
std::optional<QSqlError> removeGroup(int row);
private:
using Groups = QVector<std::shared_ptr<ConnectionGroup>>;
@ -71,9 +74,6 @@ private:
std::tuple<int, int> 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<int, QSqlError> addGroup(QString group_name);
std::optional<QSqlError> saveToDb(const ConnectionConfig &cc);
};

View file

@ -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);
}

View file

@ -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;

View file

@ -68,12 +68,14 @@ QToolButton {
</attribute>
<addaction name="actionConnect"/>
<addaction name="actionManage_server"/>
<addaction name="actionBackup_database"/>
<addaction name="separator"/>
<addaction name="actionAdd_group"/>
<addaction name="actionRemove_group"/>
<addaction name="actionAdd_Connection"/>
<addaction name="actionConfigure_connection"/>
<addaction name="actionDelete_connection"/>
<addaction name="separator"/>
<addaction name="actionBackup_database"/>
</widget>
<action name="actionAdd_Connection">
<property name="icon">
@ -142,6 +144,26 @@ QToolButton {
<string>Configure connection</string>
</property>
</action>
<action name="actionAdd_group">
<property name="icon">
<iconset>
<normalon>:/icons/folder_add.png</normalon>
</iconset>
</property>
<property name="text">
<string>Add group</string>
</property>
</action>
<action name="actionRemove_group">
<property name="icon">
<iconset>
<normalon>:/icons/folder_delete.png</normalon>
</iconset>
</property>
<property name="text">
<string>Remove group</string>
</property>
</action>
</widget>
<resources>
<include location="resources.qrc"/>

BIN
pglab/icons/folder_add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -29,5 +29,7 @@
<file>icons/constraints/unique.png</file>
<file>icons/constraints/index.png</file>
<file>icons/server_configuration.png</file>
<file>icons/folder_add.png</file>
<file>icons/folder_delete.png</file>
</qresource>
</RCC>