Proper Size for Suggestion View

Header&Row evenly spread with fixed height.
This commit is contained in:
ShaopengLin 2024-10-22 17:05:48 -04:00
parent 5a6f2b27c8
commit 357e4dbad0
4 changed files with 62 additions and 0 deletions

View File

@ -3,6 +3,23 @@ QWidget {
border: none;
}
QHeaderView {
background-color: white;
}
QHeaderView::section {
border: none;
color: #3b3b3b;
background-color: white;
margin-top: 5px; /* XXX: duplicated in css_constants.h */
padding: 5px 10px; /* XXX: duplicated in css_constants.h */
font-size: 16px;
line-height: 24px; /* XXX: duplicated in css_constants.h */
font-weight: 400;
}
QScrollBar {
width: 5px;
border: none;

View File

@ -43,6 +43,17 @@ namespace QTreeView {
}
}
/* In popup.css */
namespace PopupCSS {
namespace QHeaderView {
namespace section {
const int marginTop = 5;
const int lineHeight = 24;
const int paddingVertical = 5;
}
}
}
}
#endif // CSS_CONSTANTS_H

View File

@ -8,6 +8,8 @@
#include "suggestionlistworker.h"
#include "css_constants.h"
namespace HeaderSectionCSS = CSS::PopupCSS::QHeaderView::section;
BookmarkButton::BookmarkButton(QWidget *parent) :
QToolButton(parent)
{
@ -81,6 +83,24 @@ SearchBarLineEdit::SearchBarLineEdit(QWidget *parent) :
m_suggestionView->setRootIsDecorated(false);
m_suggestionView->setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/popup.css"));
const int contentHeight = HeaderSectionCSS::lineHeight;
m_suggestionView->setIconSize(QSize(contentHeight, contentHeight));
/* The suggestionView sizing unfortunately is not aware of headers. We
have to do this manually. We also sized header the same as items.
*/
connect(&m_suggestionModel, &QAbstractListModel::modelReset, [=](){
/* +1 for header. */
const int maxItem = m_completer.maxVisibleItems();
const int count = std::min(m_suggestionModel.rowCount(), maxItem) + 1;
const int itemHeight = m_suggestionView->sizeHintForRow(0);
/* Extra space styling above header and below last suggestion item. */
const int extraMargin = 2 * HeaderSectionCSS::marginTop;
m_suggestionView->setFixedHeight(itemHeight * count + extraMargin);
});
connect(m_suggestionView->verticalScrollBar(), &QScrollBar::valueChanged,
this, &SearchBarLineEdit::onScroll);

View File

@ -1,8 +1,11 @@
#include "suggestionlistmodel.h"
#include "kiwixapp.h"
#include "css_constants.h"
#include <QIcon>
namespace HeaderSectionCSS = CSS::PopupCSS::QHeaderView::section;
QString getZimIdFromUrl(QUrl url);
SuggestionListModel::SuggestionListModel(QObject *parent)
@ -35,9 +38,20 @@ QVariant SuggestionListModel::data(const QModelIndex &index, int role) const
case Qt::UserRole:
return m_suggestions.at(row).url;
case Qt::DecorationRole:
{
const auto library = KiwixApp::instance()->getLibrary();
const auto zimId = getZimIdFromUrl(m_suggestions.at(row).url);
return library->getBookIcon(zimId);
}
case Qt::SizeHintRole:
{
/* Padding in css can't change height, we have to achieve padding
by increasing height.
*/
const int padding = HeaderSectionCSS::paddingVertical;
const int lineHeight = HeaderSectionCSS::lineHeight;
return QSize(0, lineHeight + 2 * padding);
}
}
return QVariant();
}