Add search functionality

This commit is contained in:
altffour 2019-12-20 13:30:36 +03:00
parent c5392fa6fd
commit ed00feb8fa
No known key found for this signature in database
GPG Key ID: 2E7B9E061AF66FC3
8 changed files with 119 additions and 2 deletions

28
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/contractNotifier",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/bin/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build Debug"
}
]
}

13
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,13 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build Debug",
"type": "shell",
"command": "./build.sh",
"group": "build",
}
]
}

View File

@ -20,5 +20,6 @@ auto addCategoryWindow::on_addBtn_clicked() -> void {
category._desc = this->descBox->text().toUtf8().constData();
_db->_categories.push_back(category);
((MainWindow*)(this->parent()))->updateDB();
db_makeList(*(this->_db));
this->close();
}

View File

@ -35,5 +35,6 @@ auto addContractWindow::on_addBtn_clicked() -> void {
}
}
((MainWindow*)(this->parent()))->updateDB();
db_makeList(*(this->_db));
this->close();
}

View File

@ -3,6 +3,8 @@
#include <string>
#include <vector>
#include <algorithm>
#include <mutex>
#include "contract.h"
#include "category.h"
#include <boost/archive/text_oarchive.hpp>
@ -15,6 +17,7 @@ struct DB {
std::string _notifier_email = "";
bool _notify_by_email = true;
bool _notify_by_notify = true;
std::vector<Contract*> *_contractList = nullptr;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version) {
@ -26,7 +29,47 @@ struct DB {
}
};
static auto db_addCategory(DB db, Category item) -> void {
static auto db_search_getCorrectness(const std::string &str, const Contract &contract) -> int {
int val = 0;
if (contract._name.size() < str.size())
for (auto c : contract._name)
if (c == str[val]) val++;
else if (contract._name.size() > str.size())
for (auto c : str)
if (c == contract._name[val++]) val++;
return val;
}
static auto db_search_sort(DB &db, const std::string &str) -> void {
std::stable_sort(db._contractList->begin(), db._contractList->end(), [=](const Contract *l, const Contract *r) -> bool {return db_search_getCorrectness(str, *l)>db_search_getCorrectness(str, *r);});
}
static auto db_makeList(DB &db) -> void {
std::mutex mtx;
mtx.lock();
// Make the list
if (!db._contractList)
db._contractList = new std::vector<Contract*>();
db._contractList->clear();
// Iterate over deactivated category.
for (int i = 0; i < db._deactivatedCategory._contracts.size(); i++)
db._contractList->push_back(&db._deactivatedCategory._contracts[i]);
// Iterate over categories.
for (int i = 0; i < db._categories.size(); i++)
for (int j = 0; j < db._categories[i]._contracts.size(); j++)
db._contractList->push_back(&db._categories[i]._contracts[j]);
// Sort the list.
std::stable_sort(db._contractList->begin(), db._contractList->end(), [=](const Contract *l, const Contract *r) -> bool {return l->_name[0]>r->_name[0];});
mtx.unlock();
}
static auto db_cleanList(DB &db) -> void {
delete db._contractList;
}
static auto db_addCategory(DB &db, Category &item) -> void {
// Check if category is already present.
for (auto i : db._categories)
if (i == item)

View File

@ -3,6 +3,7 @@
#include <ctime>
#include <thread>
#include <mutex>
#include <QDate>
#include <vector>
@ -73,14 +74,20 @@ static auto notify_check(DB &db, bool by_email = true, bool by_notification = tr
}
// change states/switches.
std::mutex mtx;
mtx.lock();
contract._did_notify = true;
res = true;
mtx.unlock();
}
}
if (current_date > expiry_date) {
// expire the contract.
std::mutex mtx;
mtx.lock();
contract._expired = true;
category_moveContract(category, db._deactivatedCategory, contract);
mtx.unlock();
res = true;
}
}

View File

@ -48,9 +48,12 @@ MainWindow::MainWindow(QWidget *parent)
connect(this->_checker, SIGNAL(checkDBepoch()), this, SLOT(updateDB()));
this->treeView->setSelectionMode(QAbstractItemView::SingleSelection);
db_makeList(this->_db);
}
MainWindow::~MainWindow() {
db_cleanList(this->_db);
this->_checker->setClosing(true);
this->_checkingThread->join();
export_db_as_db(_db, "db.db");
@ -75,6 +78,25 @@ auto MainWindow::listDB() -> void {
this->treeView->expandItem(t);
};
auto MainWindow::on_searchBox_textChanged(const QString &text) -> void {
auto str = text.toStdString();
if (str == "") {
this->listDB();
}
else {
db_search_sort(this->_db, str);
// Print contract list to screen.
this->treeView->clear();
auto t = new QTreeWidgetItem(QStringList() << "Search Results");
for (auto contract : *(this->_db._contractList)) {
auto item = new QTreeWidgetItem(QStringList() << contract->_name.c_str());
t->addChild(item);
}
this->treeView->addTopLevelItem(t);
this->treeView->expandItem(t);
}
}
auto MainWindow::closeEvent(QCloseEvent *event) -> void {
if(closing) {
event->accept();
@ -108,7 +130,7 @@ auto MainWindow::on_infoBtn_clicked() -> void {
auto MainWindow::on_deleteBtn_clicked() -> void {
// Get item selected
auto item = this->treeView->currentItem()->text(0).toUtf8().constData();
auto item = this->treeView->currentItem()->text(0).toStdString();
// Check if it is the expired category.
if (item == "Expired") {
@ -138,6 +160,7 @@ auto MainWindow::on_deleteBtn_clicked() -> void {
}
this->updateDB();
db_makeList(this->_db);
}
auto MainWindow::on_treeView_itemClicked(QTreeWidgetItem* _item, int col) -> void {

View File

@ -43,6 +43,7 @@ public slots:
void on_deleteBtn_clicked();
void closeEvent(QCloseEvent *event) override;
void on_treeView_itemClicked(QTreeWidgetItem* _item, int column);
void on_searchBox_textChanged(const QString &text);
void on_actionExport_triggered();
void on_actionImport_triggered();
void on_actionAdd_Contract_triggered();