Added add and remove to Qt interface

develop 0.7.0
tastytea 2018-10-13 05:07:34 +02:00
parent 8bbe6fa30e
commit 1bcbd320b2
No known key found for this signature in database
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)
project (whyblocked
VERSION 0.6.2
VERSION 0.7.0
LANGUAGES CXX
)

View File

@ -17,6 +17,7 @@
#include <regex>
#include <QMessageBox>
#include <QDebug>
#include <iostream>
#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<string, int, string> &line : result)
{
QList<QStandardItem*> 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<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()
{
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<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()
{
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, "<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[])
{
QApplication app(argc, argv);

View File

@ -18,8 +18,10 @@
#define INTERFACE_QT_HPP
#include <string>
#include <tuple>
#include <QMainWindow>
#include <QStandardItemModel>
#include <QDialog>
#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<const string, const bool, const string> get_data();
};
#endif // INTERFACE_QT_HPP

View File

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

View File

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