diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index 713e79b..89546d6 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include "version.hpp" #include "whyblocked.hpp" #include "interface_qt.hpp" @@ -81,30 +80,8 @@ void MainWindow::add_row(const QString &user, const int &blocked, const QString void MainWindow::add() { - DialogAdd dialog; - if (dialog.exec()) - { - auto data = dialog.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 (user.empty()) - { - return; - } - add_block(user, blocked, reason); - add_row(QString::fromStdString(user), - blocked, - QString::fromStdString(reason)); - for (const string &receipt : std::get<3>(data)) - { - add_url(user, receipt); - } - - statusBar()->showMessage(tr("Added %1 to database.") - .arg(QString::fromStdString(user))); - } + DialogAdd *dialog = new DialogAdd(this); + dialog->show(); } void MainWindow::remove() @@ -170,9 +147,12 @@ const string MainWindow::urls_to_hyperlinks(const string &text) return std::regex_replace(text, re_url, "$1"); } -DialogAdd::DialogAdd(QMainWindow *parent) : QDialog(parent) +DialogAdd::DialogAdd(QMainWindow *parent) +: QDialog(parent) +, _parent(static_cast(parent)) { setupUi(this); + connect(button_receipt_add, &QPushButton::clicked, this, &DialogAdd::add_receipt); connect(button_receipt_remove, &QPushButton::clicked, this, &DialogAdd::remove_receipt); } @@ -207,6 +187,32 @@ 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 (user.empty()) + { + return; + } + ::add_block(user, blocked, reason); + _parent->add_row(QString::fromStdString(user), + blocked, + QString::fromStdString(reason)); + for (const string &receipt : std::get<3>(data)) + { + ::add_url(user, receipt); + } + + _parent->statusBar()->showMessage(tr("Added %1 to database.") + .arg(QString::fromStdString(user))); + + delete this; +} + int main(int argc, char *argv[]) { QApplication app(argc, argv); diff --git a/src/interface_qt.hpp b/src/interface_qt.hpp index 784aeab..8d21fdd 100644 --- a/src/interface_qt.hpp +++ b/src/interface_qt.hpp @@ -38,6 +38,13 @@ class MainWindow : public QMainWindow, private Ui::MainWindow public: explicit MainWindow(QMainWindow *parent = nullptr); + void add_row(const QString &user, const int &blocked, const QString &reason); + +private: + void populate_tableview(); + const string urls_to_hyperlinks(const string &text); + + QStandardItemModel *_model; private slots: void add(); @@ -45,12 +52,6 @@ private slots: void about(); void show_details(QModelIndex index); -private: - void populate_tableview(); - void add_row(const QString &user, const int &blocked, const QString &reason); - const string urls_to_hyperlinks(const string &text); - - QStandardItemModel *_model; }; class DialogAdd : public QDialog, private Ui::DialogAdd @@ -59,11 +60,16 @@ class DialogAdd : public QDialog, private Ui::DialogAdd public: explicit DialogAdd(QMainWindow *parent = nullptr); + +private: const dialogdata get_data(); + MainWindow *_parent; private slots: void add_receipt(); void remove_receipt(); + void accept(); + }; #endif // INTERFACE_QT_HPP