2018-08-12 16:45:39 +02:00
|
|
|
#include <poppler-qt5.h>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QClipboard>
|
2018-09-02 20:30:52 +02:00
|
|
|
#include <QtGlobal>
|
2018-08-12 16:45:39 +02:00
|
|
|
#include <QSettings>
|
|
|
|
#include <QDateTime>
|
2018-08-31 20:08:23 +02:00
|
|
|
#include <QProcess>
|
2018-09-02 20:30:52 +02:00
|
|
|
#include <QComboBox>
|
2019-04-27 21:24:53 +02:00
|
|
|
#include <QtConcurrent/QtConcurrent>
|
2018-08-12 16:45:39 +02:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
#include "clicklabel.h"
|
2019-04-25 10:27:54 +02:00
|
|
|
#include "../shared/sqlitesearch.h"
|
2018-08-12 16:45:39 +02:00
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
|
|
QMainWindow(parent),
|
|
|
|
ui(new Ui::MainWindow)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2019-04-27 21:24:53 +02:00
|
|
|
db = QSqlDatabase::addDatabase("QSQLITE");
|
|
|
|
db.setDatabaseName(QSettings().value("dbpath").toString());
|
|
|
|
if(!db.open())
|
|
|
|
{
|
|
|
|
qDebug() << "failed to open database";
|
|
|
|
throw std::runtime_error("Failed to open database");
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:45:39 +02:00
|
|
|
pdfWorker = new PdfWorker();
|
|
|
|
pdfWorker->moveToThread(&pdfWorkerThread);
|
|
|
|
connectSignals();
|
|
|
|
searchThread.start();
|
|
|
|
ui->treeResultsList->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
|
|
|
|
|
|
|
|
ui->tabWidget->setCurrentIndex(0);
|
2018-08-31 20:40:07 +02:00
|
|
|
ui->statusBar->addWidget(ui->lblSearchResults);
|
|
|
|
ui->statusBar->addWidget(ui->pdfProcessBar);
|
|
|
|
ui->pdfProcessBar->hide();
|
2019-04-25 10:27:54 +02:00
|
|
|
QSettings settings;
|
|
|
|
ui->comboScale->setCurrentText(settings.value("currentScale").toString());
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::connectSignals()
|
|
|
|
{
|
|
|
|
connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed);
|
2019-04-27 21:24:53 +02:00
|
|
|
// connect(this, &MainWindow::beginSearch, searchWorker, &SearchWorker::search);
|
|
|
|
connect(&searchWatcher, &QFutureWatcher<SearchResult>::finished, this, [&]{
|
|
|
|
auto results = searchWatcher.future().result();
|
|
|
|
handleSearchResults(results);
|
|
|
|
});
|
|
|
|
// connect(searchWorker, &SearchWorker::searchCancelled, this, &MainWindow::handleCancelledSearch);
|
|
|
|
// connect(searchWorker, &SearchWorker::searchError, this, &MainWindow::handleSearchError);
|
2018-08-12 16:45:39 +02:00
|
|
|
connect(ui->treeResultsList, &QTreeWidget::itemActivated, this, &MainWindow::treeSearchItemActivated);
|
|
|
|
connect(ui->treeResultsList, &QTreeWidget::customContextMenuRequested, this, &MainWindow::showSearchResultsContextMenu);
|
|
|
|
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::tabChanged);
|
|
|
|
connect(this, &MainWindow::startPdfPreviewGeneration, pdfWorker, &PdfWorker::generatePreviews);
|
|
|
|
connect(pdfWorker, &PdfWorker::previewReady, this, &MainWindow::pdfPreviewReceived);
|
|
|
|
connect(pdfWorker, &PdfWorker::previewsFinished, [&] { this->pdfDirty = false; });
|
2018-09-02 20:30:52 +02:00
|
|
|
connect(ui->comboScale, qOverload<const QString &>(&QComboBox::currentIndexChanged), this, &MainWindow::comboScaleChanged);
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
|
|
|
|
2018-09-02 20:30:52 +02:00
|
|
|
void MainWindow::comboScaleChanged(QString text)
|
|
|
|
{
|
2019-04-25 10:27:54 +02:00
|
|
|
QSettings scaleSetting;
|
|
|
|
scaleSetting.setValue("currentScale", ui->comboScale->currentText());
|
2018-09-02 20:30:52 +02:00
|
|
|
makePdfPreview();
|
|
|
|
}
|
2018-08-12 16:45:39 +02:00
|
|
|
bool MainWindow::pdfTabActive()
|
|
|
|
{
|
|
|
|
return ui->tabWidget->currentIndex() == 1;
|
|
|
|
}
|
|
|
|
|
2018-12-29 20:21:13 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:45:39 +02:00
|
|
|
void MainWindow::tabChanged()
|
|
|
|
{
|
2018-08-31 20:40:07 +02:00
|
|
|
if(pdfTabActive())
|
2018-08-12 16:45:39 +02:00
|
|
|
{
|
2018-08-31 20:40:07 +02:00
|
|
|
if(pdfDirty)
|
|
|
|
{
|
|
|
|
makePdfPreview();
|
|
|
|
}
|
|
|
|
ui->pdfProcessBar->show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->pdfProcessBar->hide();
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::pdfPreviewReceived(PdfPreview preview)
|
|
|
|
{
|
|
|
|
ClickLabel *label = new ClickLabel();
|
|
|
|
label->setPixmap(QPixmap::fromImage(preview.previewImage));
|
|
|
|
ui->scrollAreaWidgetContents->layout()->addWidget(label);
|
2018-08-31 20:40:07 +02:00
|
|
|
ui->pdfProcessBar->setValue(++processedPdfPreviews);
|
2019-04-26 15:31:42 +02:00
|
|
|
|
2018-08-31 20:08:23 +02:00
|
|
|
connect(label, &ClickLabel::clicked, [=]() {
|
|
|
|
QSettings settings;
|
|
|
|
QString command = settings.value("pdfviewer").toString();
|
|
|
|
if(command != "" && command.contains("%p") && command.contains("%f"))
|
|
|
|
{
|
|
|
|
command = command.replace("%f", preview.documentPath);
|
|
|
|
command = command.replace("%p", QString::number(preview.page));
|
|
|
|
QStringList splitted = command.split(" ");
|
|
|
|
if(splitted.size() > 1)
|
|
|
|
{
|
|
|
|
QString cmd = splitted[0];
|
|
|
|
QStringList args = splitted.mid(1);
|
|
|
|
QProcess::startDetached(cmd, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(preview.documentPath));
|
|
|
|
}
|
|
|
|
});
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::lineEditReturnPressed()
|
|
|
|
{
|
|
|
|
QString q = ui->txtSearch->text();
|
2019-04-25 10:27:54 +02:00
|
|
|
if(!SqliteSearch::checkParanthesis(q))
|
2018-09-02 13:54:27 +02:00
|
|
|
{
|
|
|
|
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...");
|
2019-04-27 21:24:53 +02:00
|
|
|
QFuture<QVector<SearchResult>> searchFuture = QtConcurrent::run([&, q]() {
|
|
|
|
SqliteSearch searcher(db);
|
|
|
|
return searcher.search(q);
|
|
|
|
});
|
|
|
|
searchWatcher.setFuture(searchFuture);
|
|
|
|
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
|
|
|
|
{
|
|
|
|
this->pdfSearchResults.clear();
|
|
|
|
ui->treeResultsList->clear();
|
2018-08-31 20:40:07 +02:00
|
|
|
ui->lblSearchResults->setText("Results: " + QString::number(results.size()));
|
2018-08-12 16:45:39 +02:00
|
|
|
for(const SearchResult &result : results)
|
|
|
|
{
|
2019-04-26 15:31:42 +02:00
|
|
|
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));
|
2018-09-02 20:38:21 +02:00
|
|
|
|
|
|
|
//TODO: this must be user defined or done more intelligently
|
2018-08-12 16:45:39 +02:00
|
|
|
if(this->pdfSearchResults.size() < 300)
|
|
|
|
{
|
2019-04-22 21:07:41 +02:00
|
|
|
if(result.fileData.absPath.endsWith(".pdf"))
|
2018-08-12 16:45:39 +02:00
|
|
|
{
|
|
|
|
this->pdfSearchResults.append(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ui->treeResultsList->resizeColumnToContents(0);
|
|
|
|
ui->treeResultsList->resizeColumnToContents(1);
|
|
|
|
pdfDirty = ! this->pdfSearchResults.empty();
|
2018-09-02 12:27:23 +02:00
|
|
|
if(pdfTabActive() && pdfDirty)
|
|
|
|
{
|
|
|
|
makePdfPreview();
|
|
|
|
}
|
2018-08-12 16:45:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::makePdfPreview()
|
|
|
|
{
|
|
|
|
if(!pdfWorkerThread.isRunning())
|
|
|
|
pdfWorkerThread.start();
|
2018-08-30 21:54:29 +02:00
|
|
|
|
|
|
|
pdfWorker->cancelAndWait();
|
|
|
|
QCoreApplication::processEvents(); //Process not processed images
|
|
|
|
qDeleteAll(ui->scrollAreaWidgetContents->children());
|
2018-08-12 16:45:39 +02:00
|
|
|
|
|
|
|
ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout());
|
2018-08-31 20:40:07 +02:00
|
|
|
ui->pdfProcessBar->setMaximum(this->pdfSearchResults.size());
|
|
|
|
processedPdfPreviews = 0;
|
2018-09-02 20:30:52 +02:00
|
|
|
QString scaleText = ui->comboScale->currentText();
|
|
|
|
scaleText.chop(1);
|
|
|
|
|
|
|
|
emit startPdfPreviewGeneration(this->pdfSearchResults, scaleText.toInt() / 100.);
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::handleCancelledSearch()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-02 13:54:27 +02:00
|
|
|
void MainWindow::handleSearchError(QString error)
|
|
|
|
{
|
|
|
|
ui->lblSearchResults->setText("Error:" + error);
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:45:39 +02:00
|
|
|
void MainWindow::treeSearchItemActivated(QTreeWidgetItem *item, int i)
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(item->text(1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::showSearchResultsContextMenu(const QPoint &point)
|
|
|
|
{
|
|
|
|
QTreeWidgetItem *item = ui->treeResultsList->itemAt(point);
|
|
|
|
if(item == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QMenu menu("SearchResult", this);
|
|
|
|
menu.addAction("Copy filename to clipboard", [&] { QGuiApplication::clipboard()->setText(item->text(0));});
|
|
|
|
menu.addAction("Copy full path to clipboard", [&] { QGuiApplication::clipboard()->setText(item->text(1)); });
|
|
|
|
menu.addAction("Open containing folder", [&] {
|
|
|
|
QFileInfo pathinfo(item->text(1));
|
|
|
|
QString dir = pathinfo.absolutePath();
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(dir));
|
|
|
|
|
|
|
|
});
|
|
|
|
menu.exec(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|