From 244e6aa95e18a4700bffeb482e018e2ed2459b18 Mon Sep 17 00:00:00 2001 From: Albert S Date: Mon, 10 Apr 2023 18:29:27 +0200 Subject: [PATCH] gui: Add tag checkboxes and inputdialog for search context menu --- gui/mainwindow.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++ gui/mainwindow.h | 4 +++ 2 files changed, 70 insertions(+) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index d5a0010..4e1c29c 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -16,6 +16,9 @@ #include #include #include +#include +#include + #include "mainwindow.h" #include "ui_mainwindow.h" #include "clicklabel.h" @@ -42,6 +45,9 @@ MainWindow::MainWindow(QWidget *parent, QString socketPath) indexer = new Indexer(*(this->dbService)); indexer->setParent(this); + + tagManager = new TagManager(*(this->dbService)); + connectSignals(); ui->treeResultsList->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); ui->tabWidget->setCurrentIndex(0); @@ -1008,6 +1014,65 @@ void MainWindow::createSearchResultMenu(QMenu &menu, const QFileInfo &fileInfo) this->ui->comboPreviewFiles->setCurrentText(fileInfo.absoluteFilePath()); }); } + + QMenu *tagMenu = menu.addMenu("Tag file with: "); + QVector allTags = this->dbService->getTags(); + QHash fileTags; + + QString path = fileInfo.absoluteFilePath(); + + for(const QString &fileTag : this->dbService->getTagsForPath(path)) + { + fileTags[fileTag] = true; + } + + for(const QString &tag : allTags) + { + QCheckBox *checkBox = new QCheckBox(tagMenu); + QWidgetAction *checkableAction = new QWidgetAction(tagMenu); + checkableAction->setDefaultWidget(checkBox); + checkBox->setText(tag); + checkBox->setChecked(fileTags.contains(tag)); + tagMenu->addAction(checkableAction); + + connect(checkBox, &QCheckBox::stateChanged, this, + [this, checkBox, path] + { + QVector currentTags = this->dbService->getTagsForPath(path); + QString checkBoxText = checkBox->text(); + if(checkBox->isChecked()) + { + if(!this->tagManager->addTagsToPath(path, {checkBoxText})) + { + QMessageBox::critical(this, "Error while adding tag", + "An error occured while trying to add the tag"); + } + } + else + { + if(!this->tagManager->removeTagsForPath(path, {checkBoxText})) + { + QMessageBox::critical(this, "Error while removing tag", + "An error occured while trying to remove the tag"); + } + } + }); + } + + tagMenu->addAction("Add new tags", this, + [this, path] + { + bool ok; + QString text = + QInputDialog::getText(this, tr("Enter new tags"), tr("New tags (comma separated):"), + QLineEdit::Normal, "", &ok); + + if(ok && !this->tagManager->addTagsToPath(path, text, ',')) + { + QMessageBox::critical(this, "Error while trying to add tags", + "An error occured while trying to add tags"); + } + }); } void MainWindow::openDocument(QString path, int num) @@ -1062,6 +1127,7 @@ MainWindow::~MainWindow() delete this->dbService; delete this->dbFactory; delete this->indexer; + delete this->tagManager; delete ui; } diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 805a3f2..8344193 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -14,6 +14,7 @@ #include "../shared/indexsyncer.h" #include "previewcoordinator.h" #include "indexer.h" +#include "tagmanager.h" namespace Ui { class MainWindow; @@ -39,6 +40,9 @@ class MainWindow : public QMainWindow QFutureWatcher> searchWatcher; LooqsQuery contentSearchQuery; QVector searchHistory; + + TagManager *tagManager; + int currentSearchHistoryIndex = 0; QString currentSavedSearchText; bool previewDirty = false;