-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Paint Suggestion Icon&Text Spacing
Qt does not provide css style or API for modifying icon and text spacing in QTreeView. This is a long issue in buttons as well, as Qt draws icon and texts instead of having them as widgets that can be applied styles.
- Loading branch information
1 parent
a63e63b
commit 68a288b
Showing
5 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "suggestionlistdelegate.h" | ||
#include "kiwixapp.h" | ||
#include "css_constants.h" | ||
|
||
#include <QPainter> | ||
|
||
namespace HeaderSectionCSS = CSS::PopupCSS::QHeaderView::section; | ||
|
||
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; | ||
const int lineHeight = HeaderSectionCSS::lineHeight; | ||
const int paddingLeft = HeaderSectionCSS::paddingLeft; | ||
|
||
const QSize mapSize = QSize(lineHeight, lineHeight); | ||
auto pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(mapSize); | ||
|
||
/* Align icon to Header text */ | ||
if (KiwixApp::isRightToLeft()) | ||
{ | ||
const int rightEnd = pixmapRect.width() - mapSize.width(); | ||
pixmapRect.setX(pixmapRect.x() + rightEnd - paddingLeft); | ||
} | ||
else | ||
pixmapRect.setX(pixmapRect.x() + paddingLeft); | ||
|
||
/* Align middle */ | ||
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(); | ||
const auto& lineEditGeo = searchBar.getLineEdit().geometry(); | ||
|
||
/* Remove border from left() since left is is with respect to border. Detail | ||
reason on how this calculation comes about can be seen in | ||
SearchBarLineEdit::getCompleterRect(); | ||
*/ | ||
const int left = lineEditGeo.left() - CSS::SearchBar::border; | ||
QRect textRect = opt.rect; | ||
if (KiwixApp::isRightToLeft()) | ||
{ | ||
const auto& searchGeo = searchBar.geometry(); | ||
const int right = searchGeo.width() - left - lineEditGeo.width(); | ||
textRect.setWidth(textRect.width() - right); | ||
} | ||
else | ||
textRect.setX(textRect.x() + left); | ||
|
||
const int flag = {Qt::AlignVCenter | Qt::AlignLeading}; | ||
const QString text = index.data(Qt::DisplayRole).toString(); | ||
p->drawText(textRect, flag, text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |