From f6e0b97e77a466c7513bae659140248a9c30afed Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 17 Oct 2018 20:44:58 +0200 Subject: [PATCH] Added means to edit entries in the Qt interface fixes #1 --- CMakeLists.txt | 2 +- src/interface_qt.cpp | 85 +++++++++++++++++++++++++++++++++++--------- src/interface_qt.hpp | 22 +++++++----- src/whyblocked.ui | 33 +++++++++++++++++ 4 files changed, 116 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7295e66..02bc3f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.6) project (whyblocked - VERSION 0.7.8 + VERSION 0.8.0 LANGUAGES CXX ) diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index c58f537..2e4238a 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -77,6 +77,39 @@ void MainWindow::add() dialog->show(); } +void MainWindow::edit() +{ + if (tableview->selectionModel()->selectedRows().count() != 1) + { + QMessageBox::warning(this, tr("Invalid selection"), + tr("Please select only 1 entry to edit.")); + return; + } + + DialogAdd *dialog = new DialogAdd(this); + dialog->setWindowTitle(tr("Edit entry")); + + Dialogdata data; + QModelIndex index = tableview->selectionModel()->selectedRows().first(); + data.user = index.sibling(index.row(), 0).data().toString().toStdString(); + result_details details; + database::details(data.user, details); + if (std::get<0>(details) == true) + { + data.blocked = 1; + } + else + { + data.blocked = 0; + } + data.reason = std::get<1>(details); + data.receipts = std::get<2>(details); + + dialog->set_data(data); + dialog->setProperty("edit", true); + dialog->show(); +} + void MainWindow::remove() { QItemSelectionModel *selection = tableview->selectionModel(); @@ -147,7 +180,7 @@ DialogAdd::DialogAdd(QMainWindow *parent) setupUi(this); } -const dialogdata DialogAdd::get_data() +const Dialogdata DialogAdd::get_data() const { std::vector receipts; for (int row = 0; row <= list_receipts->count() - 1; ++row) @@ -155,10 +188,27 @@ const dialogdata DialogAdd::get_data() receipts.push_back(list_receipts->item(row)->text().toStdString()); } - return std::make_tuple(text_user->text().toStdString(), - radio_blocked->isChecked(), - text_reason->text().toStdString(), - receipts); + Dialogdata data; + data.user = text_user->text().toStdString(); + data.blocked = radio_blocked->isChecked(); + data.reason = text_reason->text().toStdString(); + data.receipts = receipts; + + return data; +} + +const void DialogAdd::set_data(const Dialogdata &data) +{ + text_user->setText(QString::fromStdString(data.user)); + radio_blocked->setChecked(data.blocked); + radio_silcenced->setChecked(!data.blocked); + text_reason->setText(QString::fromStdString(data.reason)); + for (const string &receipt : data.receipts) + { + QListWidgetItem *item = new QListWidgetItem(QString::fromStdString(receipt)); + item->setFlags(item->flags() | Qt::ItemIsEditable); + list_receipts->insertItem(list_receipts->count(), item); + } } void DialogAdd::add_receipt() @@ -179,26 +229,27 @@ void DialogAdd::remove_receipt() void DialogAdd::accept() { - auto data = get_data(); - const string user = std::get<0>(data); - const int blocked = static_cast(std::get<1>(data)); - const string reason = std::get<2>(data); + if (property("edit").toBool()) + { + _parent->remove(); + } + Dialogdata data = get_data(); - if (user.empty()) + if (data.user.empty()) { return; } - database::add_block(user, blocked, reason); - _parent->add_row(QString::fromStdString(user), - blocked, - QString::fromStdString(reason)); - for (const string &receipt : std::get<3>(data)) + database::add_block(data.user, data.blocked, data.reason); + _parent->add_row(QString::fromStdString(data.user), + data.blocked, + QString::fromStdString(data.reason)); + for (const string &receipt : data.receipts) { - database::add_receipt(user, receipt); + database::add_receipt(data.user, receipt); } _parent->statusBar()->showMessage(tr("Added %1 to database.") - .arg(QString::fromStdString(user))); + .arg(QString::fromStdString(data.user))); delete this; } diff --git a/src/interface_qt.hpp b/src/interface_qt.hpp index 409069f..8644051 100644 --- a/src/interface_qt.hpp +++ b/src/interface_qt.hpp @@ -18,8 +18,6 @@ #define INTERFACE_QT_HPP #include -#include -#include #include #include #include @@ -27,10 +25,14 @@ #include "ui_whyblocked_add.h" using std::string; -using dialogdata = std::tuple>; + +struct Dialogdata +{ + string user; + bool blocked; + string reason; + std::vector receipts; +}; class MainWindow : public QMainWindow, private Ui::MainWindow { @@ -40,6 +42,9 @@ public: explicit MainWindow(QMainWindow *parent = nullptr); void add_row(const QString &user, const int &blocked, const QString &reason); +public slots: + void remove(); + private: const string urls_to_hyperlinks(const string &text); @@ -47,7 +52,7 @@ private: private slots: void add(); - void remove(); + void edit(); void about(); void show_details(QModelIndex index); void populate_tableview(); @@ -60,9 +65,10 @@ class DialogAdd : public QDialog, private Ui::DialogAdd public: explicit DialogAdd(QMainWindow *parent = nullptr); + const void set_data(const Dialogdata &data); private: - const dialogdata get_data(); + const Dialogdata get_data() const; MainWindow *_parent; diff --git a/src/whyblocked.ui b/src/whyblocked.ui index 10e84b4..4d87807 100644 --- a/src/whyblocked.ui +++ b/src/whyblocked.ui @@ -97,6 +97,7 @@ false + @@ -116,6 +117,7 @@ &Database + @@ -201,6 +203,20 @@ Ctrl+Q + + + + + + &Edit + + + Edit entry + + + Ctrl+E + + @@ -316,6 +332,22 @@ + + action_edit + triggered() + MainWindow + edit() + + + -1 + -1 + + + 299 + 299 + + + add() @@ -323,5 +355,6 @@ populate_tableview() about() show_details(QModelIndex) + edit()