Vuo 2.4.4
Loading...
Searching...
No Matches
VuoNodeClassListItemDelegate.cc
Go to the documentation of this file.
1
12#include "VuoEditor.hh"
13
15const int VuoNodeClassListItemDelegate::classNameIndex = Qt::UserRole+1;
19
24 QStyledItemDelegate(parent)
25{
26 humanReadable = false;
27 filterText = "";
28}
29
33void VuoNodeClassListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
34{
35 paintTextDocument(painter, option, index);
36}
37
43void VuoNodeClassListItemDelegate::paintTextDocument(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
44{
45 painter->save();
46
47 // Customize display for currently selected node classes.
48 QColor baseTextColor, disambiguatingTitleFontColor;
49 setUpSelectionHighlighting(painter, baseTextColor, disambiguatingTitleFontColor, option, index);
50
51 // Generate a text document containing the node title.
52 QTextDocument *doc = generateTextDocument(index, baseTextColor, disambiguatingTitleFontColor);
53
54 // Display the node name.
55 QAbstractTextDocumentLayout::PaintContext context;
56 QRect r = option.rect;
57 painter->setClipRect(r);
58
59 // Center node name vertically within row.
60 int h = /*doc->size().height()*/ baseTitleFontPt * 2;
61 int yAdjusted = r.y() + r.size().height()/2 - h/2 + 1;
62
63 painter->translate(r.x(), yAdjusted);
64 doc->drawContents(painter);
65
66 delete doc;
67 painter->restore();
68}
69
74void VuoNodeClassListItemDelegate::setUpSelectionHighlighting(QPainter *painter, QColor &baseTextColor, QColor &disambiguatingTitleFontColor, const QStyleOptionViewItem &option, const QModelIndex &index) const
75{
76 VuoEditor *editor = (VuoEditor *)qApp;
77 bool isDark = editor->isInterfaceDark();
78 if (option.state & QStyle::State_Selected)
79 {
80 bool isActive = option.state & QStyle::State_Active;
81 painter->fillRect(option.rect, isDark ? QColor(isActive ? "#1d6ae5" : "#606060") : QColor(isActive ? "#74acec" : "#e0e0e0"));
82
83 QPalette::ColorGroup cg = isActive ? QPalette::Normal : QPalette::Inactive;
84 QColor textColor = option.palette.color(cg, QPalette::HighlightedText);
85 baseTextColor = isDark ? QColor("#dadada") : textColor;
86 disambiguatingTitleFontColor = isDark ? QColor("#909090") : textColor;
87 }
88 else
89 {
90 // Highlight alternating rows.
91 if (index.row() & 1)
92 painter->fillRect(option.rect, isDark ? QColor("#2b2b2b") : QColor("#f8f8f8"));
93
94 baseTextColor = QColor(isDark ? "#a0a0a0" : "#606060");
95 disambiguatingTitleFontColor = QColor(isDark ? "#606060" : "#b0b0b0");
96 }
97}
98
103QTextDocument *VuoNodeClassListItemDelegate::generateTextDocument(const QModelIndex &index, QColor baseTextColor, QColor disambiguatingTitleFontColor) const
104{
105 const QColor baseTitleFontColor = baseTextColor;
106
107 // Retrieve node name data.
108 QString humanReadableName = index.data(VuoNodeClassListItemDelegate::humanReadableNameIndex).toString();
109 QString className = index.data(VuoNodeClassListItemDelegate::classNameIndex).toString();
110 QString baseTitle = (humanReadable? humanReadableName:className);
111
112 QTextOption to;
113 to.setWrapMode(QTextOption::NoWrap);
114 QTextDocument *document = new QTextDocument();
115 document->setDefaultTextOption(to);
116 document->setDefaultFont(QFont("PT Sans"));
117
118 // Locate text filter tokens within the node name for highlighting.
120 QStringList tokenizedTextFilter = filterText.split(QRegExp("[\\s\\.]+"));
121 for (int tokenIndex = 0; tokenIndex < tokenizedTextFilter.size(); ++tokenIndex)
122 {
123 QString token = tokenizedTextFilter.at(tokenIndex);
124 if (! token.isEmpty())
125 highlighter->addTargetTerm(token);
126 }
127
128 // Indicate where the human-readable node name ends and the (optional) class name begins,
129 // so the highlighter knows what symbol to tokenize on within each segment of the text.
130 QString disambiguationSeparator = " ";
131 highlighter->setNodeClassNameStartIndex(humanReadable? humanReadableName.length()+disambiguationSeparator.length():0);
132
133 QString formattedBaseTitle = QString("<span style=\"color:%1; font-size:%2pt;\">%3</span>")
134 .arg(baseTitleFontColor.name())
135 .arg(baseTitleFontPt)
136 .arg(baseTitle);
137 QString fullText = formattedBaseTitle;
138
139 // Display class names even in human-readable mode.
140 if (humanReadable)
141 {
142 QString formattedDisambiguationText = QString("%1<span style=\"color:%2; font-size:%3pt;\">%4</span>")
143 .arg(disambiguationSeparator)
144 .arg(disambiguatingTitleFontColor.name())
146 .arg(className);
147 fullText.append(formattedDisambiguationText);
148 }
149
150 document->setHtml(fullText);
151
152 return document;
153}
154
158QSize VuoNodeClassListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
159{
160 QWidget *parentWidget = (QWidget *)parent();
161 return QSize(parentWidget->width() - parentWidget->style()->pixelMetric(QStyle::PM_ScrollBarExtent) - 4,
162
163 // Instead of using `QStyledItemDelegate::sizeHint` (which inexplicably changes between app launches),
164 // calculate the line height based on the text size.
165 // https://b33p.net/kosada/node/13897
166// QStyledItemDelegate::sizeHint(option, index).height()
167 baseTitleFontPt * 1.5);
168}
169
175{
176 return humanReadable;
177}
178
184{
185 this->humanReadable = humanReadable;
186}
187
192{
193 this->filterText = filterText;
194}