From 1bcbd320b2d29aeac73f13a5d7cd0d0e8b7044c2 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 13 Oct 2018 05:07:34 +0200 Subject: [PATCH] Added add and remove to Qt interface --- CMakeLists.txt | 2 +- src/interface_qt.cpp | 108 ++++++++++++++++++++++++++++++------------ src/interface_qt.hpp | 16 ++++++- src/whyblocked.ui | 26 ++++++++-- src/whyblocked_add.ui | 2 +- 5 files changed, 118 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a783a43..f1c694d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.6) project (whyblocked - VERSION 0.6.2 + VERSION 0.7.0 LANGUAGES CXX ) diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index 16646e7..5b4fe41 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "version.hpp" #include "whyblocked.hpp" #include "interface_qt.hpp" @@ -24,64 +25,101 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent) { setupUi(this); - statusBar()->showMessage(tr("Ready")); - QStandardItemModel *model = new QStandardItemModel; - tableview->setModel(model); - populate_tableview(*model); - model->setHeaderData(0, Qt::Horizontal, tr("User/Instance")); - model->setHeaderData(1, Qt::Horizontal, tr("Blocked/Silenced?")); - model->setHeaderData(2, Qt::Horizontal, tr("Reason")); - tableview->horizontalHeader()->resizeSection(0, 300); + _model = new QStandardItemModel; + tableview->setModel(_model); tableview->setSelectionBehavior(QAbstractItemView::SelectRows); + populate_tableview(); connect(action_add, &QAction::triggered, this, &MainWindow::add); connect(action_remove, &QAction::triggered, this, &MainWindow::remove); + connect(action_reload, &QAction::triggered, this, &MainWindow::populate_tableview); connect(action_about, &QAction::triggered, this, &MainWindow::about); connect(tableview, &QTableView::clicked, this, &MainWindow::show_details); connect(tableview, &QTableView::activated, this, &MainWindow::show_details); } -void MainWindow::populate_tableview(QStandardItemModel &model) +void MainWindow::populate_tableview() { - model.clear(); + _model->clear(); result_view result; if (view(result)) { for (const std::tuple &line : result) { - QList items; - items.append(new QStandardItem(QString::fromStdString((std::get<0>(line))))); - if (std::get<1>(line) == 1) - { - items.append(new QStandardItem(QString(tr("blocked")))); - } - else - { - items.append(new QStandardItem(QString(tr("silenced")))); - } - items.append(new QStandardItem(QString::fromStdString((std::get<2>(line))))); - model.appendRow(items); + add_row(QString::fromStdString(std::get<0>(line)), + std::get<1>(line), + QString::fromStdString(std::get<2>(line))); } } + + _model->setHeaderData(0, Qt::Horizontal, tr("User/Instance")); + _model->setHeaderData(1, Qt::Horizontal, tr("Blocked/Silenced?")); + _model->setHeaderData(2, Qt::Horizontal, tr("Reason")); + tableview->horizontalHeader()->resizeSection(0, 300); + + statusBar()->showMessage(tr("Database loaded.")); +} + +void MainWindow::add_row(const QString &user, const int &blocked, const QString &reason) +{ + QList items; + items.append(new QStandardItem(user)); + if (blocked == 1) + { + items.append(new QStandardItem(QString(tr("blocked")))); + } + else + { + items.append(new QStandardItem(QString(tr("silenced")))); + } + items.append(new QStandardItem(reason)); + _model->appendRow(items); } void MainWindow::add() { - QDialog *dialog_add = new QDialog; - Ui::DialogAdd ui; - ui.setupUi(dialog_add); - - if (dialog_add->exec() == 1) + DialogAdd dialog; + if (dialog.exec()) { - statusBar()->showMessage(tr("Added nothing. :-P")); + 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)); + + statusBar()->showMessage(tr("Added %1 to database.") + .arg(QString::fromStdString(user))); } } void MainWindow::remove() { - statusBar()->showMessage(tr("Removed nothing. :-P")); + QItemSelectionModel *selection = tableview->selectionModel(); + if (selection->hasSelection()) + { + for (auto &row : selection->selectedRows()) + { + const string user = row.data().toString().toStdString(); + ::remove(user); + statusBar()->showMessage(tr("Removed %1 from database.") + .arg(QString::fromStdString(user))); + _model->removeRow(row.row()); + } + } + else + { + statusBar()->showMessage(tr("Select data to remove.")); + } } void MainWindow::about() @@ -126,6 +164,18 @@ const string MainWindow::urls_to_hyperlinks(const string &text) return std::regex_replace(text, re_url, "$1"); } +DialogAdd::DialogAdd(QMainWindow *parent) : QDialog(parent) +{ + setupUi(this); +} + +const std::tuple DialogAdd::get_data() +{ + return std::make_tuple(text_user->text().toStdString(), + radio_blocked->isChecked(), + text_reason->text().toStdString()); +} + int main(int argc, char *argv[]) { QApplication app(argc, argv); diff --git a/src/interface_qt.hpp b/src/interface_qt.hpp index d85c5aa..034f3e4 100644 --- a/src/interface_qt.hpp +++ b/src/interface_qt.hpp @@ -18,8 +18,10 @@ #define INTERFACE_QT_HPP #include +#include #include #include +#include #include "ui_whyblocked.h" #include "ui_whyblocked_add.h" @@ -39,8 +41,20 @@ private slots: void show_details(QModelIndex index); private: - void populate_tableview(QStandardItemModel &model); + 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 +{ + Q_OBJECT + +public: + explicit DialogAdd(QMainWindow *parent = nullptr); + const std::tuple get_data(); }; #endif // INTERFACE_QT_HPP diff --git a/src/whyblocked.ui b/src/whyblocked.ui index 81c6061..5c9df72 100644 --- a/src/whyblocked.ui +++ b/src/whyblocked.ui @@ -71,11 +71,13 @@ + - + + .. add @@ -89,7 +91,8 @@ - + + .. remove @@ -98,12 +101,13 @@ Remove user or instance - Ctrl+Del + Del - + + .. about @@ -112,6 +116,20 @@ About this application + + + + + + Reload + + + Reload database + + + Ctrl+R + + diff --git a/src/whyblocked_add.ui b/src/whyblocked_add.ui index d3c514e..298daf3 100644 --- a/src/whyblocked_add.ui +++ b/src/whyblocked_add.ui @@ -15,7 +15,7 @@ - +