Krita Source Code Documentation
Loading...
Searching...
No Matches
CommandBarStyleDelegate Class Reference
+ Inheritance diagram for CommandBarStyleDelegate:

Public Slots

void setFilterString (const QString &text)
 

Public Member Functions

 CommandBarStyleDelegate (QObject *parent=nullptr)
 
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 

Private Attributes

QString m_filterString
 

Detailed Description

Definition at line 75 of file katecommandbar.cpp.

Constructor & Destructor Documentation

◆ CommandBarStyleDelegate()

CommandBarStyleDelegate::CommandBarStyleDelegate ( QObject * parent = nullptr)
inline

Definition at line 78 of file katecommandbar.cpp.

79 : QStyledItemDelegate(parent)
80 {
81 }

Member Function Documentation

◆ paint()

void CommandBarStyleDelegate::paint ( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
inlineoverride

Definition at line 83 of file katecommandbar.cpp.

84 {
85 QStyleOptionViewItem options = option;
86 initStyleOption(&options, index);
87
88 QTextDocument doc;
89
90 const auto original = index.data().toString();
91
92 const auto strs = index.data().toString().split(QLatin1Char(':'));
93 QString str = strs.at(1);
94 const QString nameColor = option.palette.color(QPalette::Link).name();
95 kfts::to_fuzzy_matched_display_string(m_filterString, str, QString("<b style=\"color:%1;\">").arg(nameColor), QString("</b>"));
96
97 const QString component = QString("<span style=\"color: %1;\"><b>").arg(nameColor) + strs.at(0) + QString(":</b> </span>");
98
99 doc.setHtml(component + str);
100 doc.setDocumentMargin(2);
101
102 painter->save();
103
104 // paint background
105 if (option.state & QStyle::State_Selected) {
106 painter->fillRect(option.rect, option.palette.highlight());
107 } else {
108 painter->fillRect(option.rect, option.palette.base());
109 }
110
111 options.text = QString(); // clear old text
112 options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
113
114 // fix stuff for rtl
115 // QTextDocument doesn't work with RTL text out of the box so we give it a hand here by increasing
116 // the text width to our rect size. Icon displacement is also calculated here because 'translate()'
117 // later will not work.
118 const bool rtl = original.isRightToLeft();
119 if (rtl) {
120 auto r = options.widget->style()->subElementRect(QStyle::SE_ItemViewItemText, &options, options.widget);
121 auto hasIcon = index.data(Qt::DecorationRole).value<QIcon>().isNull();
122 if (hasIcon) {
123 doc.setTextWidth(r.width() - 25);
124 } else {
125 doc.setTextWidth(r.width());
126 }
127 }
128
129 // draw text
130 painter->translate(option.rect.x(), option.rect.y());
131 // leave space for icon
132
133 if (!rtl) {
134 painter->translate(25, 0);
135 }
136
137 doc.drawContents(painter);
138
139 painter->restore();
140 }
static Q_DECL_UNUSED QString to_fuzzy_matched_display_string(const QString pattern, QString &str, const QString &htmlTag, const QString &htmlTagClose)
get string for display in treeview / listview. This should be used from style delegate....

References m_filterString, and kfts::to_fuzzy_matched_display_string().

◆ setFilterString

void CommandBarStyleDelegate::setFilterString ( const QString & text)
inlineslot

Definition at line 143 of file katecommandbar.cpp.

144 {
145 m_filterString = text;
146 }

References m_filterString.

Member Data Documentation

◆ m_filterString

QString CommandBarStyleDelegate::m_filterString
private

Definition at line 149 of file katecommandbar.cpp.


The documentation for this class was generated from the following file: