forked from nuttyartist/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaglistdelegate.cpp
More file actions
86 lines (79 loc) · 2.48 KB
/
Copy pathtaglistdelegate.cpp
File metadata and controls
86 lines (79 loc) · 2.48 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "taglistdelegate.h"
#include "taglistmodel.h"
#include <QPainter>
#include <QPainterPath>
TagListDelegate::TagListDelegate(QObject *parent) :
QStyledItemDelegate(parent),
#ifdef __APPLE__
m_displayFont(QFont(QStringLiteral("SF Pro Text")).exactMatch() ? QStringLiteral("SF Pro Text") : QStringLiteral("Roboto")),
#elif _WIN32
m_displayFont(QFont(QStringLiteral("Segoe UI")).exactMatch() ? QStringLiteral("Segoe UI") : QStringLiteral("Roboto")),
#else
m_displayFont(QStringLiteral("Roboto")),
#endif
#ifdef __APPLE__
m_titleFont(m_displayFont, 13, 65),
#else
m_titleFont(m_displayFont, 10, 60),
#endif
m_titleColor(26, 26, 26),
m_theme(Theme::Light)
{
}
void TagListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->setRenderHint(QPainter::Antialiasing);
auto name = index.data(TagListModel::NameRole).toString();
auto color = index.data(TagListModel::ColorRole).toString();
auto rect = option.rect;
rect.setHeight(20);
QPainterPath path;
path.addRoundedRect(rect, 10, 10);
if (m_theme == Theme::Dark) {
painter->fillPath(path, QColor(76, 85, 97));
} else {
painter->fillPath(path, QColor(218, 235, 248));
}
auto iconRect = QRect(rect.x() + 5, rect.y() + (rect.height() - 12) / 2, 12, 12);
painter->setBrush(QColor(color));
painter->setPen(QColor(color));
painter->drawEllipse(iconRect);
painter->setBrush(m_titleColor);
painter->setPen(m_titleColor);
QRect nameRect(rect);
nameRect.setLeft(iconRect.x() + iconRect.width() + 5);
painter->setFont(m_titleFont);
painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, name);
}
QSize TagListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
QSize size;
size.setHeight(20);
auto name = index.data(TagListModel::NameRole).toString();
QFontMetrics fmName(m_titleFont);
QRect fmRectName = fmName.boundingRect(name);
size.setWidth(5 + 12 + 5 + fmRectName.width() + 7);
return size;
}
void TagListDelegate::setTheme(Theme newTheme)
{
m_theme = newTheme;
switch(m_theme){
case Theme::Light:
{
m_titleColor = QColor(26, 26, 26);
break;
}
case Theme::Dark:
{
m_titleColor = QColor(204, 204, 204);
break;
}
case Theme::Sepia:
{
m_titleColor = QColor(26, 26, 26);
break;
}
}
}