forked from nuttyartist/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoldertreedelegateeditor.cpp
More file actions
153 lines (147 loc) · 6.5 KB
/
Copy pathfoldertreedelegateeditor.cpp
File metadata and controls
153 lines (147 loc) · 6.5 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "foldertreedelegateeditor.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QDebug>
#include <QTreeView>
#include <QMouseEvent>
#include "pushbuttontype.h"
#include "nodetreemodel.h"
#include "nodetreeview.h"
#include "labeledittype.h"
FolderTreeDelegateEditor::FolderTreeDelegateEditor(QTreeView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index,
QWidget *parent) :
QWidget(parent),
m_option(option),
m_index(index),
#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_titleSelectedColor(255, 255, 255),
m_activeColor(68, 138, 201),
m_hoverColor(207, 207, 207),
m_view(view)
{
setContentsMargins(0, 0, 0, 0);
auto layout = new QHBoxLayout(this);
layout->setContentsMargins(5, 0, 0, 0);
layout->setSpacing(5);
setLayout(layout);
m_expandIcon = new QLabel(this);
m_expandIcon->setMinimumSize({15, 15});
m_expandIcon->setMaximumSize({15, 15});
m_expanded.load(QStringLiteral(":/images/tree-node-expanded.png"));
m_notExpanded.load(QStringLiteral(":/images/tree-node-normal.png"));
m_expandIcon->setScaledContents(true);
layout->addWidget(m_expandIcon);
m_label = new LabelEditType(this);
m_label->setFont(m_titleFont);
QSizePolicy labelPolicy;
labelPolicy.setVerticalPolicy(QSizePolicy::Expanding);
labelPolicy.setHorizontalPolicy(QSizePolicy::Expanding);
m_label->setSizePolicy(labelPolicy);
m_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
connect(m_label, &LabelEditType::editingStarted, this, [this] {
auto tree_view = dynamic_cast<NodeTreeView*>(m_view);
tree_view->setIsEditing(true);
});
connect(m_label, &LabelEditType::editingFinished, this, [this] (const QString& label) {
auto tree_view = dynamic_cast<NodeTreeView*>(m_view);
tree_view->onRenameFolderFinished(label);
tree_view->setIsEditing(false);
});
connect(dynamic_cast<NodeTreeView*>(m_view), &NodeTreeView::renameFolderRequested,
m_label, &LabelEditType::openEditor);
layout->addWidget(m_label);
m_contextButton = new PushButtonType(parent);
m_contextButton->setMaximumSize({33, 25});
m_contextButton->setMinimumSize({33, 25});
m_contextButton->setCursor(QCursor(Qt::PointingHandCursor));
m_contextButton->setFocusPolicy(Qt::TabFocus);
m_contextButton->setIconSize(QSize(16, 16));
m_contextButton->setStyleSheet(QStringLiteral(R"(QPushButton { )"
R"( border: none; )"
R"( padding: 0px; )"
R"(})"));
connect(m_contextButton, &QPushButton::clicked, m_view, [this] (bool) {
auto tree_view = dynamic_cast<NodeTreeView*>(m_view);
// tree_view->setCurrentIndexC(m_index);
tree_view->onCustomContextMenu(
tree_view->visualRect(m_index).topLeft() + m_contextButton->geometry().bottomLeft());
});
layout->addWidget(m_contextButton, 0, Qt::AlignRight);
layout->addSpacing(5);
connect(m_view, &QTreeView::expanded, this, [this] (const QModelIndex &) {
this->update();
});
}
void FolderTreeDelegateEditor::updateDelegate()
{
auto displayName = m_index.data(NodeItem::Roles::DisplayText).toString();
QFontMetrics fm(m_titleFont);
displayName = fm.elidedText(displayName, Qt::ElideRight, m_label->contentsRect().width());
if (m_view->selectionModel()->isSelected(m_index)) {
m_label->setStyleSheet(QStringLiteral("QLabel{color: rgb(%1, %2, %3);}")
.arg(QString::number(m_titleSelectedColor.red()),
QString::number(m_titleSelectedColor.green()),
QString::number(m_titleSelectedColor.blue())));
m_contextButton->setNormalIcon(QIcon(QString::fromUtf8(":/images/3dots_Highlighted.png")));
m_contextButton->setHoveredIcon(QIcon(QString::fromUtf8(":/images/3dots_Highlighted.png")));
m_contextButton->setPressedIcon(QIcon(QString::fromUtf8(":/images/3dots_Highlighted.png")));
} else {
m_label->setStyleSheet(QStringLiteral("QLabel{color: rgb(%1, %2, %3);}")
.arg(QString::number(m_titleColor.red()),
QString::number(m_titleColor.green()),
QString::number(m_titleColor.blue())));
m_contextButton->setNormalIcon(QIcon(QString::fromUtf8(":/images/3dots_Regular.png")));
m_contextButton->setHoveredIcon(QIcon(QString::fromUtf8(":/images/3dots_Hovered.png")));
m_contextButton->setPressedIcon(QIcon(QString::fromUtf8(":/images/3dots_Pressed.png")));
}
m_label->setText(displayName);
auto theme = dynamic_cast<NodeTreeView*>(m_view)->theme();
if (theme == Theme::Dark) {
m_expanded.load(QStringLiteral(":/images/tree-node-expanded-dark.png"));
m_notExpanded.load(QStringLiteral(":/images/tree-node-normal-dark.png"));
} else {
m_expanded.load(QStringLiteral(":/images/tree-node-expanded.png"));
m_notExpanded.load(QStringLiteral(":/images/tree-node-normal.png"));
}
if (m_index.data(NodeItem::Roles::IsExpandable).toBool()) {
if(m_view->isExpanded(m_index)) {
m_expandIcon->setPixmap(m_expanded);
} else {
m_expandIcon->setPixmap(m_notExpanded);
}
}
}
void FolderTreeDelegateEditor::paintEvent(QPaintEvent *event)
{
updateDelegate();
QPainter painter(this);
if (m_view->selectionModel()->isSelected(m_index)) {
painter.fillRect(rect(), QBrush(m_activeColor));
} else {
painter.fillRect(rect(), QBrush(m_hoverColor));
}
QWidget::paintEvent(event);
}
void FolderTreeDelegateEditor::mouseDoubleClickEvent(QMouseEvent *event)
{
if (m_label->geometry().contains(event->pos())) {
m_label->openEditor();
} else {
QWidget::mouseDoubleClickEvent(event);
}
}