Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Fine-Tuned Zim Search #1189

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7b7c949
Introduce suggestiolistmodel.{h, cpp}
ShaopengLin Aug 19, 2024
1f1743b
Move SearchBarLineEdit urlList to SuggestionListModel
ShaopengLin Sep 1, 2024
8611a1c
Enter Library::getZimIcon()
ShaopengLin Sep 1, 2024
6cbc9e8
Add Suggestion Icon Decoration Role
ShaopengLin Sep 1, 2024
c029da2
Refactor SuggestionListWorker
ShaopengLin Sep 1, 2024
d5de7c6
Introduce SuggestionListModel::lastIndex
ShaopengLin Sep 17, 2024
0aac03f
Introduce Endless Suggestions
ShaopengLin Sep 17, 2024
84e29ec
Endless Suggestion Key press compatible
ShaopengLin Sep 17, 2024
2aca115
Endless Suggestion Page_Down Compatible
ShaopengLin Sep 17, 2024
1894d41
Correctly handle Endless w/Fulltext
ShaopengLin Sep 17, 2024
d0a1680
Fetch Only When More Suggesion Exist
ShaopengLin Sep 18, 2024
76bae0d
Make First Row Visible on Suggestion Popup
ShaopengLin Sep 17, 2024
a7d7304
Introduce Kiwix Search Header
ShaopengLin Sep 17, 2024
d713173
Replace m_completer.popup() with m_suggestionView
ShaopengLin Sep 17, 2024
b3d9424
Enter KiwixApp::getSearchBar()
ShaopengLin Sep 17, 2024
ff7117b
BugFix openCompletion Connected Multiple Times
ShaopengLin Sep 28, 2024
ce36366
Make SearchBar Margin Consistent
ShaopengLin Sep 28, 2024
b9f4f94
Align Search Suggestion with Line Edit
ShaopengLin Sep 28, 2024
02e042a
Fix Suggestion Flickering when Typing
ShaopengLin Sep 28, 2024
dac8d04
Proper Size for Suggestion View
ShaopengLin Sep 28, 2024
fcebdd0
Proper Suggestion Icon&Text Spacing
ShaopengLin Sep 28, 2024
64b30c3
Proper Hover Style to Suggestion Items
ShaopengLin Sep 28, 2024
b19ea81
Proper Elide for Suggestion List Text
ShaopengLin Sep 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kiwix-desktop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ SOURCES += \
src/rownode.cpp \
src/suggestionlistworker.cpp \
src/suggestionlistmodel.cpp \
ShaopengLin marked this conversation as resolved.
Show resolved Hide resolved
src/suggestionlistdelegate.cpp \
src/thumbnaildownloader.cpp \
src/translation.cpp \
src/main.cpp \
Expand Down Expand Up @@ -111,6 +112,7 @@ HEADERS += \
src/rownode.h \
src/suggestionlistworker.h \
src/suggestionlistmodel.h \
src/suggestionlistdelegate.h \
src/thumbnaildownloader.h \
src/translation.h \
src/mainwindow.h \
Expand Down
2 changes: 2 additions & 0 deletions src/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "kiwixapp.h"
#include "suggestionlistworker.h"
#include "suggestionlistdelegate.h"

BookmarkButton::BookmarkButton(QWidget *parent) :
QToolButton(parent)
Expand Down Expand Up @@ -76,6 +77,7 @@ SearchBarLineEdit::SearchBarLineEdit(QWidget *parent) :
/* QCompleter's uses default list views, which do not have headers. */
m_completer.setPopup(m_suggestionView);

m_suggestionView->setItemDelegate(new SuggestionListDelegate(this));
m_suggestionView->header()->setStretchLastSection(true);
m_suggestionView->setRootIsDecorated(false);
m_suggestionView->setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/popup.css"));
Expand Down
70 changes: 70 additions & 0 deletions src/suggestionlistdelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "suggestionlistdelegate.h"
#include "kiwixapp.h"

#include <QPainter>

void SuggestionListDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
/* Paint without text and icon */
QStyleOptionViewItem opt(option);
QStyledItemDelegate::paint(painter, opt, QModelIndex());

paintIcon(painter, opt, index);
paintText(painter, opt, index);
}

void SuggestionListDelegate::paintIcon(QPainter *p,
const QStyleOptionViewItem &opt,
const QModelIndex &index) const
{
QRect pixmapRect = opt.rect;

/* See line-height&padding resources/css/popup.css QHeaderView::section.
We need to add 10px of padding to match header.
*/
QSize mapSize = QSize(24, 24);
auto pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(mapSize);
if (KiwixApp::isRightToLeft())
{
int rightEnd = pixmapRect.width() - mapSize.width();
pixmapRect.setX(pixmapRect.x() + rightEnd - 10);
}
else
pixmapRect.setX(pixmapRect.x() + 10);

pixmapRect.setY(pixmapRect.y() + (pixmapRect.height() - mapSize.height()) / 2);
pixmapRect.setSize(mapSize);
p->drawPixmap(pixmapRect, pixmap);
}

void SuggestionListDelegate::paintText(QPainter *p,
const QStyleOptionViewItem &opt,
const QModelIndex &index) const
{
auto& searchBar = KiwixApp::instance()->getSearchBar();
auto lineEditGeo = searchBar.getLineEdit().geometry();

/* See SearchBar border in resources/css/style.css. Align text with text in
line edit. Remove border from left() as completer is inside border.
*/
auto left = lineEditGeo.left() - 1;
QRect textRect = opt.rect;
if (KiwixApp::isRightToLeft())
{
/* Calculate the distance of right side of search bar to line edit as
text now starts on the right.
*/
auto searchGeo = searchBar.geometry();
auto right = searchGeo.width() - left - lineEditGeo.width();
textRect.setWidth(textRect.width() - right);
}
else
textRect.setX(textRect.x() + left);

int flag = {Qt::AlignVCenter | Qt::AlignLeading};
QString text = index.data(Qt::DisplayRole).toString();
p->drawText(textRect, flag, text);
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please elaborate in the commit message why custom painting of suggestions is needed, i.e. why it couldn't be solved with CSS. Hopefully, you'll find a better solution while trying to describe the problem.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I had spent quite a lot of time to figure out how to do it by style and Qt simply does not provide any API to do this. They are drawing the elements manually themselves as well...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please prepare the next version of the PR so that custom drawing is localized in a few commits at the end so that that change can be easily excluded via a single git checkout. Then I will try to find a better solution.

17 changes: 17 additions & 0 deletions src/suggestionlistdelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SUGGESTIONLISTDELEGATE_H
#define SUGGESTIONLISTDELEGATE_H

#include <QStyledItemDelegate>

class SuggestionListDelegate : public QStyledItemDelegate
{
public:
SuggestionListDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {};
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;

private:
void paintIcon(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &index) const;
void paintText(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &index) const;
};

#endif // SUGGESTIONLISTDELEGATE_H