-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowView.cpp
More file actions
executable file
·157 lines (124 loc) · 3.87 KB
/
WindowView.cpp
File metadata and controls
executable file
·157 lines (124 loc) · 3.87 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
154
155
156
157
#include "WindowView.h"
#include <QDebug>
#include <QPainter>
WindowView::WindowView(QWidget* parent):
QAbstractItemView(parent)
{
}
QRect WindowView::visualRect(const QModelIndex& index) const
{
QVector<QModelIndex> treePath;
treePath.append(index);
while(treePath.front().parent().isValid())
{
treePath.push_front(treePath.front().parent());
}
QRect rect(0, 0, geometry().width(), geometry().height());
for(int i = 0; i < treePath.length(); ++i)
{
QModelIndex treeItem = treePath.at(i);
int width = rect.width() / model()->rowCount(treeItem.parent());
rect.setLeft(rect.left() + treeItem.row() * width);
rect.setWidth(width);
rect.setTop(i * 20);
rect.setHeight(20);
if(index.column() == 4 && model()->data(index) != QVariant::Invalid)
{
rect.setTop(rect.top() + 20);
rect.setHeight(geometry().height() - 20 - i * 20);
}
}
return rect;
}
void WindowView::scrollTo(const QModelIndex& index, QAbstractItemView::ScrollHint hint)
{
}
QModelIndex WindowView::indexAt(const QPoint& point) const
{
return QModelIndex();
}
QModelIndex WindowView::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
return QModelIndex();
}
int WindowView::horizontalOffset() const
{
return 0;
}
int WindowView::verticalOffset() const
{
return 0;
}
bool WindowView::isIndexHidden(const QModelIndex& index) const
{
return false;
}
void WindowView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command)
{
}
QRegion WindowView::visualRegionForSelection(const QItemSelection& selection) const
{
return QRegion();
}
void WindowView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
{
viewport()->update();
//qDebug() << "Data Changed";
//qDebug() << topLeft.row() << ", " << topLeft.column() << ", " << bottomRight.row() << ", " << bottomRight.column() << roles;
}
void WindowView::rowsInserted(const QModelIndex& parent, int start, int end)
{
viewport()->update();
//qDebug() << "Rows Inserted";
//qDebug() << parent.row() << ", " << parent.column() << start << ", " << end;
}
void WindowView::rowsAboutToBeRemoved(const QModelIndex& parent, int start, int end)
{
viewport()->update();
//qDebug() << "Rows about to be removed";
//qDebug() << parent.row() << ", " << parent.column() << start << ", " << end;
}
QVector<QModelIndex> toRender(QAbstractItemModel* model, QModelIndex parent = QModelIndex())
{
QVector<QModelIndex> candidates;
for(int r = 0; r < model->rowCount(parent); ++r)
{
QModelIndex titleIndex = model->index(r, 0, parent);
candidates << titleIndex;
QModelIndex hwndIndex = model->index(r, 4, parent);
if(model->data(hwndIndex) != QVariant::Invalid)
{
candidates << hwndIndex;
}
if( model->hasChildren(titleIndex) ) {
candidates << toRender(model, titleIndex);
}
}
return candidates;
}
void WindowView::paintEvent(QPaintEvent* paintEvent)
{
if (!itemDelegate())
return;
QStyleOptionViewItem option;
QPainter painter(viewport());
const QVector<QModelIndex> toBeRendered = toRender(model());
const int rowCount = this->model()->rowCount();
QVector<QModelIndex>::const_iterator end = toBeRendered.constEnd();
for (QVector<QModelIndex>::const_iterator it = toBeRendered.constBegin(); it != end; ++it) {
Q_ASSERT((*it).isValid());
if (rowCount == 1)
option.viewItemPosition = QStyleOptionViewItem::OnlyOne;
else if ((*it).row() == 0)
option.viewItemPosition = QStyleOptionViewItem::Beginning;
else if ((*it).row() == rowCount - 1)
option.viewItemPosition = QStyleOptionViewItem::End;
else
option.viewItemPosition = QStyleOptionViewItem::Middle;
option.rect = visualRect(*it);
option.state |= QStyle::State_Enabled;
painter.fillRect(option.rect + QMargins(-1, -1, -1, -1), QBrush(QColor(100, 100, 255, 127)));
painter.drawRect(option.rect + QMargins(0, 0, -1, -1));
itemDelegate()->paint(&painter, option, *it);
}
}