looqs/gui/mainwindow.cpp

323 строки
9.2 KiB
C++
Исходник Обычный вид История

#include <poppler-qt5.h>
#include <QLabel>
#include <QtDebug>
#include <QFileInfo>
#include <QDesktopServices>
#include <QUrl>
#include <QClipboard>
#include <QtGlobal>
#include <QSettings>
#include <QDateTime>
#include <QProcess>
#include <QComboBox>
2019-04-27 21:24:53 +02:00
#include <QtConcurrent/QtConcurrent>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "clicklabel.h"
#include "../shared/sqlitesearch.h"
2021-06-12 14:59:58 +02:00
#include "../shared/looqsgeneralexception.h"
#include "../shared/common.h"
MainWindow::MainWindow(QWidget *parent, IPCClient &client) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
2021-03-07 21:09:38 +01:00
setWindowTitle(QCoreApplication::applicationName());
this->ipcClient = &client;
2019-08-26 09:20:21 +02:00
QSettings settings;
2019-04-27 21:24:53 +02:00
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(Common::databasePath());
2019-04-27 21:24:53 +02:00
if(!db.open())
{
qDebug() << "failed to open database";
throw std::runtime_error("Failed to open database");
}
connectSignals();
ui->treeResultsList->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
ui->tabWidget->setCurrentIndex(0);
ui->statusBar->addWidget(ui->lblSearchResults);
ui->statusBar->addWidget(ui->pdfProcessBar);
ui->pdfProcessBar->hide();
ui->comboScale->setCurrentText(settings.value("currentScale").toString());
2019-08-26 09:20:21 +02:00
pdfPreviewsPerPage = settings.value("pdfPreviewsPerPage", 20).toInt();
ui->spinPdfPreviewPage->setMinimum(1);
}
void MainWindow::connectSignals()
{
connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed);
2019-04-27 21:24:53 +02:00
connect(&searchWatcher, &QFutureWatcher<SearchResult>::finished, this,
[&]
{
try
{
this->ui->txtSearch->setEnabled(true);
auto results = searchWatcher.future().result();
handleSearchResults(results);
}
2021-06-12 14:59:58 +02:00
catch(LooqsGeneralException &e)
{
handleSearchError(e.message);
}
2019-04-27 21:24:53 +02:00
});
connect(&pdfWorkerWatcher, &QFutureWatcher<PdfPreview>::resultReadyAt, this,
[&](int index) { pdfPreviewReceived(pdfWorkerWatcher.resultAt(index)); });
connect(&pdfWorkerWatcher, &QFutureWatcher<PdfPreview>::progressValueChanged, ui->pdfProcessBar,
&QProgressBar::setValue);
connect(ui->treeResultsList, &QTreeWidget::itemActivated, this, &MainWindow::treeSearchItemActivated);
connect(ui->treeResultsList, &QTreeWidget::customContextMenuRequested, this,
&MainWindow::showSearchResultsContextMenu);
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::tabChanged);
2021-06-12 23:01:14 +02:00
connect(ui->comboScale, qOverload<int>(&QComboBox::currentIndexChanged), this, &MainWindow::comboScaleChanged);
2019-08-26 09:20:21 +02:00
connect(ui->spinPdfPreviewPage, qOverload<int>(&QSpinBox::valueChanged), this,
&MainWindow::spinPdfPreviewPageValueChanged);
}
void MainWindow::spinPdfPreviewPageValueChanged(int val)
{
makePdfPreview(val);
}
2021-06-12 23:01:14 +02:00
void MainWindow::comboScaleChanged(int i)
{
QSettings scaleSetting;
scaleSetting.setValue("currentScale", ui->comboScale->currentText());
2019-08-26 09:20:21 +02:00
makePdfPreview(ui->spinPdfPreviewPage->value());
}
bool MainWindow::pdfTabActive()
{
return ui->tabWidget->currentIndex() == 1;
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
bool quit =
((event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_Q) || event->key() == Qt::Key_Escape);
if(quit)
{
qApp->quit();
}
if(event->modifiers() & Qt::ControlModifier)
{
if(event->key() == Qt::Key_L)
{
ui->txtSearch->setFocus();
ui->txtSearch->selectAll();
}
}
QWidget::keyPressEvent(event);
}
void MainWindow::tabChanged()
{
if(pdfTabActive())
{
if(pdfDirty)
{
2019-08-26 09:20:21 +02:00
makePdfPreview(ui->spinPdfPreviewPage->value());
}
ui->pdfProcessBar->show();
}
else
{
ui->pdfProcessBar->hide();
}
}
void MainWindow::pdfPreviewReceived(PdfPreview preview)
{
if(preview.hasPreviewImage())
{
ClickLabel *label = new ClickLabel();
QString docPath = preview.documentPath;
auto previewPage = preview.page;
label->setPixmap(QPixmap::fromImage(preview.previewImage));
label->setToolTip(preview.documentPath);
ui->scrollAreaWidgetContents->layout()->addWidget(label);
connect(label, &ClickLabel::leftClick, [this, docPath, previewPage]() { ipcDocOpen(docPath, previewPage); });
connect(label, &ClickLabel::rightClick,
[this, docPath, previewPage]()
{
QFileInfo fileInfo{docPath};
QMenu menu("labeRightClick", this);
createSearchResutlMenu(menu, fileInfo);
menu.addAction("Copy page number", [previewPage]
{ QGuiApplication::clipboard()->setText(QString::number(previewPage)); });
menu.exec(QCursor::pos());
});
}
}
void MainWindow::lineEditReturnPressed()
{
QString q = ui->txtSearch->text();
2021-06-12 14:59:58 +02:00
if(!LooqsQuery::checkParanthesis(q))
{
ui->lblSearchResults->setText("Invalid paranthesis");
return;
}
2018-09-02 12:27:23 +02:00
// TODO: validate q;
2018-09-02 12:33:04 +02:00
ui->lblSearchResults->setText("Searching...");
this->ui->txtSearch->setEnabled(false);
2019-04-27 21:24:53 +02:00
QFuture<QVector<SearchResult>> searchFuture = QtConcurrent::run(
[&, q]()
{
SqliteSearch searcher(db);
2021-06-12 14:59:58 +02:00
this->currentQuery = LooqsQuery::build(q);
return searcher.search(this->currentQuery);
2019-04-27 21:24:53 +02:00
});
searchWatcher.setFuture(searchFuture);
}
void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
{
this->pdfSearchResults.clear();
ui->treeResultsList->clear();
bool hasDeleted = false;
for(const SearchResult &result : results)
{
QFileInfo pathInfo(result.fileData.absPath);
QString fileName = pathInfo.fileName();
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeResultsList);
QDateTime dt = QDateTime::fromSecsSinceEpoch(result.fileData.mtime);
item->setIcon(0, iconProvider.icon(pathInfo));
item->setText(0, fileName);
item->setText(1, result.fileData.absPath);
item->setText(2, dt.toString(Qt::ISODate));
2019-08-17 14:46:30 +02:00
item->setText(3, this->locale().formattedDataSize(result.fileData.size));
bool exists = pathInfo.exists();
if(exists)
{
if(result.fileData.absPath.endsWith(".pdf"))
{
this->pdfSearchResults.append(result);
}
}
else
{
hasDeleted = true;
}
}
ui->treeResultsList->resizeColumnToContents(0);
ui->treeResultsList->resizeColumnToContents(1);
pdfDirty = !this->pdfSearchResults.empty();
2019-08-26 09:20:21 +02:00
int numpages = ceil(static_cast<double>(this->pdfSearchResults.size()) / pdfPreviewsPerPage);
ui->spinPdfPreviewPage->setMinimum(1);
ui->spinPdfPreviewPage->setMaximum(numpages);
ui->spinPdfPreviewPage->setValue(1);
2018-09-02 12:27:23 +02:00
if(pdfTabActive() && pdfDirty)
{
2019-08-26 09:20:21 +02:00
makePdfPreview(1);
2018-09-02 12:27:23 +02:00
}
QString statusText = "Results: " + QString::number(results.size()) + " files";
if(hasDeleted)
{
statusText += " WARNING: Some files don't exist anymore. No preview available for those. Index out of sync";
}
ui->lblSearchResults->setText(statusText);
}
2019-08-26 09:20:21 +02:00
void MainWindow::makePdfPreview(int page)
{
this->pdfWorkerWatcher.cancel();
this->pdfWorkerWatcher.waitForFinished();
QCoreApplication::processEvents(); // Maybe not necessary anymore, depends on whether it's possible that a slot is
// still to be fired.
qDeleteAll(ui->scrollAreaWidgetContents->children());
ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout());
ui->pdfProcessBar->setMaximum(this->pdfSearchResults.size());
processedPdfPreviews = 0;
QString scaleText = ui->comboScale->currentText();
scaleText.chop(1);
QVector<QString> wordsToHighlight;
QRegularExpression extractor(R"#("([^"]*)"|(\w+))#");
for(const Token &token : this->currentQuery.getTokens())
{
if(token.type == FILTER_CONTENT_CONTAINS)
{
QRegularExpressionMatchIterator i = extractor.globalMatch(token.value);
while(i.hasNext())
{
QRegularExpressionMatch m = i.next();
QString value = m.captured(1);
if(value.isEmpty())
{
value = m.captured(2);
}
wordsToHighlight.append(value);
}
}
}
PdfWorker worker;
2019-08-26 09:20:21 +02:00
int end = pdfPreviewsPerPage;
int begin = page * pdfPreviewsPerPage - pdfPreviewsPerPage;
this->pdfWorkerWatcher.setFuture(
2019-08-26 09:20:21 +02:00
worker.generatePreviews(this->pdfSearchResults.mid(begin, end), wordsToHighlight, scaleText.toInt() / 100.));
ui->pdfProcessBar->setMaximum(this->pdfWorkerWatcher.progressMaximum());
ui->pdfProcessBar->setMinimum(this->pdfWorkerWatcher.progressMinimum());
}
void MainWindow::handleSearchError(QString error)
{
ui->lblSearchResults->setText("Error:" + error);
}
void MainWindow::createSearchResutlMenu(QMenu &menu, const QFileInfo &fileInfo)
{
menu.addAction("Copy filename to clipboard",
[&fileInfo] { QGuiApplication::clipboard()->setText(fileInfo.fileName()); });
menu.addAction("Copy full path to clipboard",
[&fileInfo] { QGuiApplication::clipboard()->setText(fileInfo.absoluteFilePath()); });
menu.addAction("Open containing folder", [this, &fileInfo] { this->ipcFileOpen(fileInfo.absolutePath()); });
}
void MainWindow::ipcDocOpen(QString path, int num)
{
QStringList args;
args << path;
args << QString::number(num);
this->ipcClient->sendCommand(DocOpen, args);
}
void MainWindow::ipcFileOpen(QString path)
{
QStringList args;
args << path;
this->ipcClient->sendCommand(FileOpen, args);
}
void MainWindow::treeSearchItemActivated(QTreeWidgetItem *item, int i)
{
ipcFileOpen(item->text(1));
}
void MainWindow::showSearchResultsContextMenu(const QPoint &point)
{
QTreeWidgetItem *item = ui->treeResultsList->itemAt(point);
if(item == nullptr)
{
return;
}
QFileInfo pathinfo(item->text(1));
QMenu menu("SearchResults", this);
createSearchResutlMenu(menu, pathinfo);
menu.exec(QCursor::pos());
}
MainWindow::~MainWindow()
{
delete ui;
}