-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobtree.cpp
121 lines (107 loc) · 3.46 KB
/
jobtree.cpp
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
// Copyright 2022-present Contributors to the jobman project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/jobman
#include "jobtree.h"
#include "icctransform.h"
#include <QPainter>
#include <QPointer>
#include <QMouseEvent>
#include <QStyledItemDelegate>
#include <QDebug>
class JobTreePrivate : public QObject
{
Q_OBJECT
public:
JobTreePrivate();
void init();
public Q_SLOTS:
void selectionChanged();
public:
class ItemDelegate : public QStyledItemDelegate {
public:
ItemDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override {
QSize size = QStyledItemDelegate::sizeHint(option, index);
size.setHeight(30);
return size;
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
QStyleOptionViewItem opt(option);
initStyleOption(&opt, index);
QTreeWidgetItem *item = static_cast<const QTreeWidget*>(opt.widget)->itemFromIndex(index);
std::function<bool(QTreeWidgetItem*)> hasSelectedChildren = [&](QTreeWidgetItem* parentItem) -> bool {
for (int i = 0; i < parentItem->childCount(); ++i) {
QTreeWidgetItem* child = parentItem->child(i);
if (child->isSelected() || hasSelectedChildren(child)) {
return true;
}
}
return false;
};
if (hasSelectedChildren(item)) {
opt.font.setBold(true);
opt.font.setItalic(true);
}
QStyledItemDelegate::paint(painter, opt, index);
}
bool hasSelectedChildren(QTreeWidgetItem *item) const {
for (int i = 0; i < item->childCount(); ++i) {
QTreeWidgetItem *child = item->child(i);
if (child->isSelected() || hasSelectedChildren(child)) {
return true;
}
}
return false;
}
};
public:
QPointer<JobTree> widget;
};
JobTreePrivate::JobTreePrivate()
{
}
void
JobTreePrivate::init()
{
ItemDelegate* delegate = new ItemDelegate(widget.data());
widget->setItemDelegate(delegate);
// connect
connect(widget.data(), &QTreeWidget::itemSelectionChanged, this, &JobTreePrivate::selectionChanged);
}
void
JobTreePrivate::selectionChanged()
{
widget->viewport()->update(); // we need to force a redraw
}
#include "jobtree.moc"
JobTree::JobTree(QWidget* parent)
: QTreeWidget(parent)
, p(new JobTreePrivate())
{
p->widget = this;
p->init();
}
JobTree::~JobTree()
{
}
void
JobTree::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_A && (event->modifiers() & Qt::ControlModifier)) {
for (int i = 0; i < topLevelItemCount(); ++i) {
QTreeWidgetItem* item = topLevelItem(i);
item->setSelected(true);
}
} else {
QTreeWidget::keyPressEvent(event);
}
}
void
JobTree::mousePressEvent(QMouseEvent *event)
{
QTreeWidget::mousePressEvent(event);
if (itemAt(event->pos()) == nullptr) {
clearSelection();
emit itemSelectionChanged();
}
}