Added means to edit entries in the Qt interface

fixes #1
develop
tastytea 2018-10-17 20:44:58 +02:00
parent 6e57c4b6dd
commit f6e0b97e77
No known key found for this signature in database
GPG Key ID: CFC39497F1B26E07
4 changed files with 116 additions and 26 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.7.8 VERSION 0.8.0
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -77,6 +77,39 @@ void MainWindow::add()
dialog->show(); 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() void MainWindow::remove()
{ {
QItemSelectionModel *selection = tableview->selectionModel(); QItemSelectionModel *selection = tableview->selectionModel();
@ -147,7 +180,7 @@ DialogAdd::DialogAdd(QMainWindow *parent)
setupUi(this); setupUi(this);
} }
const dialogdata DialogAdd::get_data() const Dialogdata DialogAdd::get_data() const
{ {
std::vector<string> receipts; std::vector<string> receipts;
for (int row = 0; row <= list_receipts->count() - 1; ++row) 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()); receipts.push_back(list_receipts->item(row)->text().toStdString());
} }
return std::make_tuple(text_user->text().toStdString(), Dialogdata data;
radio_blocked->isChecked(), data.user = text_user->text().toStdString();
text_reason->text().toStdString(), data.blocked = radio_blocked->isChecked();
receipts); 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() void DialogAdd::add_receipt()
@ -179,26 +229,27 @@ void DialogAdd::remove_receipt()
void DialogAdd::accept() void DialogAdd::accept()
{ {
auto data = get_data(); if (property("edit").toBool())
const string user = std::get<0>(data); {
const int blocked = static_cast<int>(std::get<1>(data)); _parent->remove();
const string reason = std::get<2>(data); }
Dialogdata data = get_data();
if (user.empty()) if (data.user.empty())
{ {
return; return;
} }
database::add_block(user, blocked, reason); database::add_block(data.user, data.blocked, data.reason);
_parent->add_row(QString::fromStdString(user), _parent->add_row(QString::fromStdString(data.user),
blocked, data.blocked,
QString::fromStdString(reason)); QString::fromStdString(data.reason));
for (const string &receipt : std::get<3>(data)) 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.") _parent->statusBar()->showMessage(tr("Added %1 to database.")
.arg(QString::fromStdString(user))); .arg(QString::fromStdString(data.user)));
delete this; delete this;
} }

View File

@ -18,8 +18,6 @@
#define INTERFACE_QT_HPP #define INTERFACE_QT_HPP
#include <string> #include <string>
#include <tuple>
#include <list>
#include <QMainWindow> #include <QMainWindow>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QDialog> #include <QDialog>
@ -27,10 +25,14 @@
#include "ui_whyblocked_add.h" #include "ui_whyblocked_add.h"
using std::string; using std::string;
using dialogdata = std::tuple<const string,
const bool, struct Dialogdata
const string, {
const std::vector<string>>; string user;
bool blocked;
string reason;
std::vector<string> receipts;
};
class MainWindow : public QMainWindow, private Ui::MainWindow class MainWindow : public QMainWindow, private Ui::MainWindow
{ {
@ -40,6 +42,9 @@ public:
explicit MainWindow(QMainWindow *parent = nullptr); explicit MainWindow(QMainWindow *parent = nullptr);
void add_row(const QString &user, const int &blocked, const QString &reason); void add_row(const QString &user, const int &blocked, const QString &reason);
public slots:
void remove();
private: private:
const string urls_to_hyperlinks(const string &text); const string urls_to_hyperlinks(const string &text);
@ -47,7 +52,7 @@ private:
private slots: private slots:
void add(); void add();
void remove(); void edit();
void about(); void about();
void show_details(QModelIndex index); void show_details(QModelIndex index);
void populate_tableview(); void populate_tableview();
@ -60,9 +65,10 @@ class DialogAdd : public QDialog, private Ui::DialogAdd
public: public:
explicit DialogAdd(QMainWindow *parent = nullptr); explicit DialogAdd(QMainWindow *parent = nullptr);
const void set_data(const Dialogdata &data);
private: private:
const dialogdata get_data(); const Dialogdata get_data() const;
MainWindow *_parent; MainWindow *_parent;

View File

@ -97,6 +97,7 @@
<bool>false</bool> <bool>false</bool>
</attribute> </attribute>
<addaction name="action_add"/> <addaction name="action_add"/>
<addaction name="action_edit"/>
<addaction name="action_remove"/> <addaction name="action_remove"/>
<addaction name="action_reload"/> <addaction name="action_reload"/>
<addaction name="action_about"/> <addaction name="action_about"/>
@ -116,6 +117,7 @@
<string>&amp;Database</string> <string>&amp;Database</string>
</property> </property>
<addaction name="action_add"/> <addaction name="action_add"/>
<addaction name="action_edit"/>
<addaction name="action_remove"/> <addaction name="action_remove"/>
<addaction name="action_reload"/> <addaction name="action_reload"/>
<addaction name="action_quit"/> <addaction name="action_quit"/>
@ -201,6 +203,20 @@
<string>Ctrl+Q</string> <string>Ctrl+Q</string>
</property> </property>
</action> </action>
<action name="action_edit">
<property name="icon">
<iconset theme="edit-redo"/>
</property>
<property name="text">
<string>&amp;Edit</string>
</property>
<property name="toolTip">
<string>Edit entry</string>
</property>
<property name="shortcut">
<string>Ctrl+E</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections> <connections>
@ -316,6 +332,22 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>action_edit</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>edit()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>299</x>
<y>299</y>
</hint>
</hints>
</connection>
</connections> </connections>
<slots> <slots>
<slot>add()</slot> <slot>add()</slot>
@ -323,5 +355,6 @@
<slot>populate_tableview()</slot> <slot>populate_tableview()</slot>
<slot>about()</slot> <slot>about()</slot>
<slot>show_details(QModelIndex)</slot> <slot>show_details(QModelIndex)</slot>
<slot>edit()</slot>
</slots> </slots>
</ui> </ui>