Added add and remove to Qt interface
the build was successful Details

This commit is contained in:
tastytea 2018-10-13 05:07:34 +02:00
parent 8bbe6fa30e
commit 1bcbd320b2
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 118 additions and 36 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.6) cmake_minimum_required (VERSION 3.6)
project (whyblocked project (whyblocked
VERSION 0.6.2 VERSION 0.7.0
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -17,6 +17,7 @@
#include <regex> #include <regex>
#include <QMessageBox> #include <QMessageBox>
#include <QDebug> #include <QDebug>
#include <iostream>
#include "version.hpp" #include "version.hpp"
#include "whyblocked.hpp" #include "whyblocked.hpp"
#include "interface_qt.hpp" #include "interface_qt.hpp"
@ -24,64 +25,101 @@
MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent) MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
{ {
setupUi(this); setupUi(this);
statusBar()->showMessage(tr("Ready"));
QStandardItemModel *model = new QStandardItemModel; _model = new QStandardItemModel;
tableview->setModel(model); 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);
tableview->setSelectionBehavior(QAbstractItemView::SelectRows); tableview->setSelectionBehavior(QAbstractItemView::SelectRows);
populate_tableview();
connect(action_add, &QAction::triggered, this, &MainWindow::add); connect(action_add, &QAction::triggered, this, &MainWindow::add);
connect(action_remove, &QAction::triggered, this, &MainWindow::remove); 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(action_about, &QAction::triggered, this, &MainWindow::about);
connect(tableview, &QTableView::clicked, this, &MainWindow::show_details); connect(tableview, &QTableView::clicked, this, &MainWindow::show_details);
connect(tableview, &QTableView::activated, 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; result_view result;
if (view(result)) if (view(result))
{ {
for (const std::tuple<string, int, string> &line : result) for (const std::tuple<string, int, string> &line : result)
{ {
QList<QStandardItem*> items; add_row(QString::fromStdString(std::get<0>(line)),
items.append(new QStandardItem(QString::fromStdString((std::get<0>(line))))); std::get<1>(line),
if (std::get<1>(line) == 1) QString::fromStdString(std::get<2>(line)));
{
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);
} }
} }
_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<QStandardItem*> 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() void MainWindow::add()
{ {
QDialog *dialog_add = new QDialog; DialogAdd dialog;
Ui::DialogAdd ui; if (dialog.exec())
ui.setupUi(dialog_add);
if (dialog_add->exec() == 1)
{ {
statusBar()->showMessage(tr("Added nothing. :-P")); auto data = dialog.get_data();
const string user = std::get<0>(data);
const int blocked = static_cast<int>(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() 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() void MainWindow::about()
@ -126,6 +164,18 @@ const string MainWindow::urls_to_hyperlinks(const string &text)
return std::regex_replace(text, re_url, "<a href=\"$1\">$1</a>"); return std::regex_replace(text, re_url, "<a href=\"$1\">$1</a>");
} }
DialogAdd::DialogAdd(QMainWindow *parent) : QDialog(parent)
{
setupUi(this);
}
const std::tuple<const string, const bool, const string> DialogAdd::get_data()
{
return std::make_tuple(text_user->text().toStdString(),
radio_blocked->isChecked(),
text_reason->text().toStdString());
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);

View File

@ -18,8 +18,10 @@
#define INTERFACE_QT_HPP #define INTERFACE_QT_HPP
#include <string> #include <string>
#include <tuple>
#include <QMainWindow> #include <QMainWindow>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QDialog>
#include "ui_whyblocked.h" #include "ui_whyblocked.h"
#include "ui_whyblocked_add.h" #include "ui_whyblocked_add.h"
@ -39,8 +41,20 @@ private slots:
void show_details(QModelIndex index); void show_details(QModelIndex index);
private: 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); 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<const string, const bool, const string> get_data();
}; };
#endif // INTERFACE_QT_HPP #endif // INTERFACE_QT_HPP

View File

@ -71,11 +71,13 @@
</attribute> </attribute>
<addaction name="action_add"/> <addaction name="action_add"/>
<addaction name="action_remove"/> <addaction name="action_remove"/>
<addaction name="action_reload"/>
<addaction name="action_about"/> <addaction name="action_about"/>
</widget> </widget>
<action name="action_add"> <action name="action_add">
<property name="icon"> <property name="icon">
<iconset theme="add"/> <iconset theme="add">
<normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>add</string> <string>add</string>
@ -89,7 +91,8 @@
</action> </action>
<action name="action_remove"> <action name="action_remove">
<property name="icon"> <property name="icon">
<iconset theme="remove"/> <iconset theme="remove">
<normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>remove</string> <string>remove</string>
@ -98,12 +101,13 @@
<string>Remove user or instance</string> <string>Remove user or instance</string>
</property> </property>
<property name="shortcut"> <property name="shortcut">
<string>Ctrl+Del</string> <string>Del</string>
</property> </property>
</action> </action>
<action name="action_about"> <action name="action_about">
<property name="icon"> <property name="icon">
<iconset theme="info"/> <iconset theme="info">
<normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>about</string> <string>about</string>
@ -112,6 +116,20 @@
<string>About this application</string> <string>About this application</string>
</property> </property>
</action> </action>
<action name="action_reload">
<property name="icon">
<iconset theme="reload"/>
</property>
<property name="text">
<string>Reload</string>
</property>
<property name="toolTip">
<string>Reload database</string>
</property>
<property name="shortcut">
<string>Ctrl+R</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

View File

@ -15,7 +15,7 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QGridLayout" name="layout_grid"> <layout class="QGridLayout" name="layout_grid" rowstretch="0,0,0,0">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label_user"> <widget class="QLabel" name="label_user">
<property name="text"> <property name="text">