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

Public Member Functions

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

Detailed Description

Definition at line 152 of file katecommandbar.cpp.

Constructor & Destructor Documentation

◆ ShortcutStyleDelegate()

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

Definition at line 155 of file katecommandbar.cpp.

156 : QStyledItemDelegate(parent)
157 {
158 }

Member Function Documentation

◆ paint()

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

Definition at line 160 of file katecommandbar.cpp.

161 {
162 QStyleOptionViewItem options = option;
163 initStyleOption(&options, index);
164 painter->save();
165
166 const auto shortcutString = index.data().toString();
167
168 // paint background
169 if (option.state & QStyle::State_Selected) {
170 painter->fillRect(option.rect, option.palette.highlight());
171 } else {
172 painter->fillRect(option.rect, option.palette.base());
173 }
174
175 options.text = QString(); // clear old text
176 options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
177
178 if (!shortcutString.isEmpty()) {
179 // collect rects for each word
181 const auto list = [&shortcutString] {
182 auto list = shortcutString.split(QLatin1Char('+'), Qt::SkipEmptyParts);
183 if (shortcutString.endsWith(QLatin1String("+"))) {
184 list.append(QStringLiteral("+"));
185 }
186 return list;
187 }();
188 btns.reserve(list.size());
189 for (const QString &text : list) {
190 QRect r = option.fontMetrics.boundingRect(text);
191 r.setWidth(r.width() + 8);
192 btns.append({r, text});
193 }
194
195 const auto plusRect = option.fontMetrics.boundingRect(QLatin1Char('+'));
196
197 // draw them
198 int x = option.rect.x();
199 const int y = option.rect.y();
200 const int plusY = option.rect.y() + plusRect.height() / 2;
201 const int total = btns.size();
202
203 // make sure our rects are nicely V-center aligned in the row
204 painter->translate(QPoint(0, (option.rect.height() - btns.at(0).first.height()) / 2));
205
206 int i = 0;
207 painter->setRenderHint(QPainter::Antialiasing);
208 for (const auto &btn : btns) {
209 painter->setPen(Qt::NoPen);
210 const QRect &rect = btn.first;
211
212 QRect buttonRect(x, y, rect.width(), rect.height());
213
214 // draw rounded rect shadow
215 auto shadowRect = buttonRect.translated(0, 1);
216 painter->setBrush(option.palette.shadow());
217 painter->drawRoundedRect(shadowRect, 3, 3);
218
219 // draw rounded rect itself
220 painter->setBrush(option.palette.button());
221 painter->drawRoundedRect(buttonRect, 3, 3);
222
223 // draw text inside rounded rect
224 painter->setPen(option.palette.buttonText().color());
225 painter->drawText(buttonRect, Qt::AlignCenter, btn.second);
226
227 // draw '+'
228 if (i + 1 < total) {
229 x += rect.width() + 5;
230 painter->drawText(QPoint(x, plusY + (rect.height() / 2)), QString("+"));
231 x += plusRect.width() + 5;
232 }
233 i++;
234 }
235 }
236
237 painter->restore();
238 }

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