gui: previews: Ensure order matches relevance ranking

Previously, the order of previews would depend simply
on which generator would finish first.

Fix this by caching out of order previews. This may
cause a small delay but should overall be hardly noticable.
This commit is contained in:
Albert S. 2022-10-19 11:11:24 +02:00
parent 7d3c24e6e1
commit 42e9ac5f41
2 changed files with 27 additions and 2 deletions

View File

@ -643,7 +643,6 @@ void MainWindow::previewReceived(QSharedPointer<PreviewResult> preview, unsigned
{ {
QString docPath = preview->getDocumentPath(); QString docPath = preview->getDocumentPath();
auto previewPage = preview->getPage(); auto previewPage = preview->getPage();
ClickLabel *headerLabel = new ClickLabel(); ClickLabel *headerLabel = new ClickLabel();
headerLabel->setText(QString("Path: ") + preview->getDocumentPath()); headerLabel->setText(QString("Path: ") + preview->getDocumentPath());
@ -685,7 +684,24 @@ void MainWindow::previewReceived(QSharedPointer<PreviewResult> preview, unsigned
previewWidget->setLayout(previewLayout); previewWidget->setLayout(previewLayout);
ui->scrollAreaWidgetContents->layout()->addWidget(previewWidget); QBoxLayout *layout = static_cast<QBoxLayout *>(ui->scrollAreaWidgetContents->layout());
int pos = previewOrder[docPath + QString::number(previewPage)];
if(pos <= layout->count())
{
layout->insertWidget(pos, previewWidget);
for(auto it = previewWidgetOrderCache.constKeyValueBegin();
it != previewWidgetOrderCache.constKeyValueEnd(); it++)
{
if(it->first <= layout->count())
{
layout->insertWidget(it->first, it->second);
}
}
}
else
{
previewWidgetOrderCache[pos] = previewWidget;
}
} }
} }
@ -938,6 +954,10 @@ void MainWindow::makePreviews(int page)
renderConfig.scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * (currentScale / 100.); renderConfig.scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * (currentScale / 100.);
renderConfig.wordsToHighlight = wordsToHighlight; renderConfig.wordsToHighlight = wordsToHighlight;
this->previewOrder.clear();
this->previewWidgetOrderCache.clear();
int previewPos = 0;
QVector<RenderTarget> targets; QVector<RenderTarget> targets;
for(SearchResult &sr : this->previewableSearchResults) for(SearchResult &sr : this->previewableSearchResults)
{ {
@ -952,6 +972,7 @@ void MainWindow::makePreviews(int page)
renderTarget.path = sr.fileData.absPath; renderTarget.path = sr.fileData.absPath;
renderTarget.page = (int)sr.page; renderTarget.page = (int)sr.page;
targets.append(renderTarget); targets.append(renderTarget);
this->previewOrder[renderTarget.path + QString::number(renderTarget.page)] = previewPos++;
} }
int numpages = ceil(static_cast<double>(targets.size()) / previewsPerPage); int numpages = ceil(static_cast<double>(targets.size()) / previewsPerPage);
ui->spinPreviewPage->setMaximum(numpages); ui->spinPreviewPage->setMaximum(numpages);

View File

@ -71,6 +71,10 @@ class MainWindow : public QMainWindow
QVector<QString> searchHistory; QVector<QString> searchHistory;
int currentSearchHistoryIndex = 0; int currentSearchHistoryIndex = 0;
QString currentSavedSearchText; QString currentSavedSearchText;
QHash<QString, int> previewOrder; /* Quick lookup for the order a preview should have */
QMap<int, QWidget *>
previewWidgetOrderCache /* Saves those that arrived out of order to be inserted later at the correct pos */;
private slots: private slots:
void lineEditReturnPressed(); void lineEditReturnPressed();
void treeSearchItemActivated(QTreeWidgetItem *item, int i); void treeSearchItemActivated(QTreeWidgetItem *item, int i);