-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextreeview.cpp
More file actions
140 lines (120 loc) · 3.27 KB
/
extreeview.cpp
File metadata and controls
140 lines (120 loc) · 3.27 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
#include "extreeview.h"
#include "headerview.h"
#include <QKeyEvent>
ExTreeView::ExTreeView(QWidget *parent) :
QTreeView(parent), mBeginColumnToTabOrder(0), mEndColumnToTabOrder(0)
{
setHeader(new HeaderView(Qt::Horizontal, true, this));
header()->setSectionResizeMode(QHeaderView::Fixed);
connect(this, SIGNAL(collapsed(QModelIndex)), SLOT(setCurrentIndex(QModelIndex)));
connect(this, SIGNAL(expanded(QModelIndex)), SLOT(setCurrentIndex(QModelIndex)));
}
void ExTreeView::normalizeColumnsWidth()
{
if(! model() )
{
return;
}
QList<int> columns;
for(int i = 0; i < model()->columnCount(); ++i)
{
if(! isColumnHidden(i) )
{
columns << i;
}
}
mColumnsPercentWidth.clear();
int percent = 100 / columns.count();
foreach(const int &col, columns)
{
mColumnsPercentWidth[col] = percent;
}
resizeColumnsToPercentWidth();
}
void ExTreeView::resizeColumnsToPercentWidth()
{
double widthPercent = viewport()->width() / 100.0;
QMapIterator<int, int> i(mColumnsPercentWidth);
while (i.hasNext())
{
i.next();
setColumnWidth(i.key(), widthPercent * i.value());
}
}
void ExTreeView::setColumnPercentWidth(int col, int percent)
{
if(! model() || col >= model()->columnCount())
{
return;
}
mColumnsPercentWidth[col] = percent;
resizeColumnsToPercentWidth();
}
int ExTreeView::beginColumnToTabOrder() const
{
return mBeginColumnToTabOrder;
}
int ExTreeView::endColumnToTabOrder() const
{
return mEndColumnToTabOrder;
}
void ExTreeView::setBeginColumnToTabOrder(int beginColumnToTabOrder)
{
mBeginColumnToTabOrder = (beginColumnToTabOrder >= model()->columnCount() || beginColumnToTabOrder < 0)
? 0
: beginColumnToTabOrder;
}
void ExTreeView::setEndColumnToTabOrder(int endColumnToTabOrder)
{
mEndColumnToTabOrder = (endColumnToTabOrder >= model()->columnCount() || endColumnToTabOrder < 0)
? model()->rowCount() - 1
: endColumnToTabOrder;
}
void ExTreeView::resizeEvent(QResizeEvent *event)
{
QTreeView::resizeEvent(event);
resizeColumnsToPercentWidth();
}
void ExTreeView::keyReleaseEvent(QKeyEvent *keyEvent)
{
QTreeView::keyReleaseEvent(keyEvent);
if (keyEvent->key() == Qt::Key_Up ||
keyEvent->key() == Qt::Key_Down)
{
QModelIndex index = currentIndex();
if (index.isValid())
{
emit pressed(index);
}
}
}
QModelIndex ExTreeView::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
if (cursorAction == QAbstractItemView::MoveNext ||
cursorAction == QAbstractItemView::MovePrevious)
{
int step = (cursorAction == QAbstractItemView::MoveNext ? 1 : -1);
int columnForRowStep = (cursorAction == QAbstractItemView::MoveNext ? mBeginColumnToTabOrder
: mEndColumnToTabOrder);
QModelIndex index = currentIndex();
if (index.isValid())
{
QModelIndex stepColumnIndex = index.sibling(index.row(), index.column() + step);
if (stepColumnIndex.isValid() && stepColumnIndex.column() >= beginColumnToTabOrder())
{
return stepColumnIndex;
}
QModelIndex stepRowIndex = index.sibling(index.row() + step, columnForRowStep);
if (stepRowIndex.isValid())
{
return stepRowIndex;
}
}
}
return QTreeView::moveCursor(cursorAction, modifiers);
}
void ExTreeView::setModel(QAbstractItemModel *model)
{
QTreeView::setModel(model);
setEndColumnToTabOrder(model->columnCount() - 1);
}