-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcodehighlighterdelegate.cpp
More file actions
60 lines (45 loc) · 1.91 KB
/
gcodehighlighterdelegate.cpp
File metadata and controls
60 lines (45 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "gcodehighlighterdelegate.h"
#include "gcodehighlighter.h"
#include <QTextDocument>
#include <QAbstractTextDocumentLayout>
#include <QApplication>
#include <QDebug>
GCodeHighlighterDelegate::GCodeHighlighterDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void GCodeHighlighterDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 optionV4 = option;
initStyleOption(&optionV4, index);
QStyle *style = optionV4.widget ? optionV4.widget->style() : QApplication::style();
QTextDocument doc;
doc.setDefaultTextOption(QTextOption(Qt::AlignVCenter));
doc.setDefaultFont(optionV4.font);
new GCodeHighlighter(&doc);
doc.setPlainText(optionV4.text);
// qDebug() << optionV4.font;
// Painting item without text
optionV4.text = QString();
style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);
QAbstractTextDocumentLayout::PaintContext ctx;
// Highlighting text if item is selected
if (optionV4.state & QStyle::State_Selected) {
ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));
}
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
painter->save();
painter->translate(textRect.topLeft().x(), textRect.topLeft().y() + (textRect.height() - doc.size().height()) / 2);
// painter->setClipRect(textRect.translated(-textRect.topLeft()));
doc.documentLayout()->draw(painter, ctx);
painter->restore();
}
QSize GCodeHighlighterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 optionV4 = option;
initStyleOption(&optionV4, index);
QTextDocument doc;
doc.setDefaultFont(optionV4.font);
doc.setPlainText(optionV4.text);
return QSize(doc.idealWidth() + 10, doc.size().height());
}