From dd83e113380824e9f9a9fc989d3bc605a0070a13 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 11 Jan 2019 20:50:22 +0100 Subject: [PATCH] Added find function --- CMakeLists.txt | 2 +- src/interface_qt.cpp | 74 +++++++++++++++++++++++++++++------ src/interface_qt.hpp | 4 +- src/whyblocked.cpp | 13 +++++- src/whyblocked.hpp | 2 +- src/whyblocked.ui | 6 ++- translations/whyblocked_de.ts | 36 ++++++++--------- translations/whyblocked_en.ts | 36 ++++++++--------- xdgcfg | 2 +- 9 files changed, 118 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d218388..b7dc363 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project (whyblocked - VERSION 0.12.2 + VERSION 0.13.0 LANGUAGES CXX ) diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index 828814d..c4c2009 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -16,20 +16,26 @@ #include #include +#include +#include +#include #include #include #include #include #include #include "version.hpp" -#include "whyblocked.hpp" #include "interface_qt.hpp" +using std::wstring; + MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent) , _config("whyblocked.cfg") , _headersize({ 250, 125, 125 }) { + std::locale::global(std::locale("")); + setupUi(this); _model = new QStandardItemModel; @@ -90,7 +96,8 @@ MainWindow::MainWindow(QMainWindow *parent) widget_find->hide(); text_find->installEventFilter(this); - populate_tableview(); + reload(); + statusBar()->showMessage(tr("Try dragging an account from your webbrowser " "into this window.")); } @@ -152,7 +159,7 @@ MainWindow::~MainWindow() _config.write(); } -void MainWindow::populate_tableview() +void MainWindow::populate_tableview(const result_view &entries) { _model->clear(); _model->setHorizontalHeaderLabels( @@ -165,18 +172,21 @@ void MainWindow::populate_tableview() tableview->horizontalHeader()->resizeSection(1, _headersize[1]); tableview->horizontalHeader()->resizeSection(2, _headersize[2]); - result_view result; - if (database::view(result)) + for (const std::tuple &line : entries) { - for (const std::tuple &line : result) - { - add_row(QString::fromStdString(std::get<0>(line)), - std::get<1>(line), - QString::fromStdString(std::get<2>(line))); - } + add_row(QString::fromStdString(std::get<0>(line)), + std::get<1>(line), + QString::fromStdString(std::get<2>(line))); } } +void MainWindow::reload() +{ + result_view entries; + database::view(entries); + populate_tableview(entries); +} + void MainWindow::add_row(const QString &user, const int &blocked, const QString &reason) { @@ -268,9 +278,47 @@ void MainWindow::find() bool MainWindow::eventFilter(QObject *obj, QEvent *event) { - if (obj == text_find && event->type() == QEvent::KeyPress) + if (obj == text_find && event->type() == QEvent::KeyRelease) { - // + string columns; + if (check_user->isChecked()) + { + columns = "user"; + } + + result_view entries; + result_view filtered_entries; + if (database::view(entries)) + { + for (const std::tuple &line : entries) + { + const string user = std::get<0>(line); + const string reason = std::get<2>(line); + wstring searchstring; + + std::wstring_convert> convert; + + if (check_user->isChecked()) + { + searchstring += convert.from_bytes(user); + } + if (check_reason->isChecked()) + { + searchstring += convert.from_bytes(reason); + } + std::transform(searchstring.begin(), searchstring.end(), + searchstring.begin(), ::towlower); + if (searchstring.find( + text_find->text().toLower().toStdWString()) + != std::string::npos) + { + filtered_entries.push_back({ + user, std::get<1>(line), reason }); + } + } + } + + populate_tableview(filtered_entries); } return QObject::eventFilter(obj, event); } diff --git a/src/interface_qt.hpp b/src/interface_qt.hpp index 3024e87..928aa96 100644 --- a/src/interface_qt.hpp +++ b/src/interface_qt.hpp @@ -25,6 +25,7 @@ #include #include #include "xdgcfg.hpp" +#include "whyblocked.hpp" #include "ui_whyblocked.h" #include "ui_whyblocked_add.h" @@ -66,7 +67,8 @@ private slots: void edit(); void about(); void show_details(QModelIndex index); - void populate_tableview(); + void populate_tableview(const result_view &entries); + void reload(); void find(); }; diff --git a/src/whyblocked.cpp b/src/whyblocked.cpp index 576c3ce..853e91e 100644 --- a/src/whyblocked.cpp +++ b/src/whyblocked.cpp @@ -111,12 +111,21 @@ const bool database::remove(const string &user) return true; } -const bool database::view(result_view &result) +const bool database::view(result_view &result, const string &sql_query) { try { + string query; + if (sql_query.empty()) + { + query = "SELECT * FROM blocks;"; + } + else + { + query = sql_query; + } sqlite::connection con(get_filepath()); - sqlite::query q(con, "SELECT * FROM blocks;"); + sqlite::query q(con, query); sqlite::result_type res = q.get_result(); while(res->next_row()) { diff --git a/src/whyblocked.hpp b/src/whyblocked.hpp index c0f4372..8a263b1 100644 --- a/src/whyblocked.hpp +++ b/src/whyblocked.hpp @@ -32,7 +32,7 @@ namespace database const string &reason); const bool add_receipt(const string &user, const string &receipt); const bool remove(const string &user); - const bool view(result_view &result); + const bool view(result_view &result, const string &sql_query = ""); const bool details(const string &user, result_details &result); } diff --git a/src/whyblocked.ui b/src/whyblocked.ui index c32740b..017f2e9 100644 --- a/src/whyblocked.ui +++ b/src/whyblocked.ui @@ -293,7 +293,8 @@ true - + + .. &Find @@ -360,7 +361,7 @@ action_reload triggered() MainWindow - populate_tableview() + reload() -1 @@ -461,5 +462,6 @@ show_details(QModelIndex) edit() find() + reload() diff --git a/translations/whyblocked_de.ts b/translations/whyblocked_de.ts index 5f9a518..67b2097 100644 --- a/translations/whyblocked_de.ts +++ b/translations/whyblocked_de.ts @@ -74,7 +74,7 @@ Du kannst URLs hier hineinziehen - + Insert receipt here. Beleg hier einfügen. @@ -198,89 +198,89 @@ - + Edit entry Eintrag bearbeiten - + &Find &Finden - + Find entries Dinde Einträge - + Ctrl+F - + User/Instance Benutzer/Instanz - + Blocked/Silenced Blockiert/Gedämpft - + Reason Begründung - + Try dragging an account from your webbrowser into this window. Versuche, einen account von deinem webbrowser in dieses fenster zu ziehen. - + blocked blockiert - + silenced gedämpft - + Invalid selection Ungültige Auswahl - + Please select only 1 entry to edit. Bitte nur 1 Eintrag zum bearbeiten auswählen. - + Nothing selected Nichts ausgewählt - + Please select entries to remove. Bitte wähle einträge aus, die gelöscht werden sollen. - + About Whyblocked Über Whyblocked - + <p><b>Whyblocked</b> %1</p><p>Reminds you why you blocked someone.</p><p>Sourcecode: <a href="https://schlomp.space/tastytea/whyblocked">https://schlomp.space/tastytea/whyblocked</a></p><p><small>Copyright © 2018 <a href="mailto:tastytea@tastytea.de">tastytea</a>.<br>Licence GPLv3: <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU GPL version 3</a>.<br>This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.</small></p> <p><b>Whyblocked</b> %1</p><p>Erinnert dich, warum du jemanden blockiertest.</p><p>Quelltext: <a href="https://schlomp.space/tastytea/whyblocked">https://schlomp.space/tastytea/whyblocked</a></p><p><small>Copyright © 2018 <a href="mailto:tastytea@tastytea.de">tastytea</a>.<br>Lizenz GPLv3: <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU GPL version 3</a>.<br>Für dieses Programm besteht KEINERLEI GARANTIE. Dies ist freie Software, die Sie unter bestimmten Bedingungen weitergeben dürfen.</small></p> - + Receipts: Belege: diff --git a/translations/whyblocked_en.ts b/translations/whyblocked_en.ts index 3928222..17eaade 100644 --- a/translations/whyblocked_en.ts +++ b/translations/whyblocked_en.ts @@ -74,7 +74,7 @@ - + Insert receipt here. @@ -198,89 +198,89 @@ - + Edit entry - + &Find - + Find entries - + Ctrl+F - + User/Instance - + Blocked/Silenced - + Reason - + Try dragging an account from your webbrowser into this window. - + blocked - + silenced - + Invalid selection - + Please select only 1 entry to edit. - + Nothing selected - + Please select entries to remove. - + About Whyblocked - + <p><b>Whyblocked</b> %1</p><p>Reminds you why you blocked someone.</p><p>Sourcecode: <a href="https://schlomp.space/tastytea/whyblocked">https://schlomp.space/tastytea/whyblocked</a></p><p><small>Copyright © 2018 <a href="mailto:tastytea@tastytea.de">tastytea</a>.<br>Licence GPLv3: <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU GPL version 3</a>.<br>This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.</small></p> - + Receipts: diff --git a/xdgcfg b/xdgcfg index 6c0976b..e22f82f 160000 --- a/xdgcfg +++ b/xdgcfg @@ -1 +1 @@ -Subproject commit 6c0976baa74f959ed3218af56e56ff58202a5a05 +Subproject commit e22f82fc6f1c40cda3d3ce5e671299f26f622528