-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheaderview.cpp
182 lines (171 loc) · 4.39 KB
/
headerview.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "headerview.h"
HeaderView::HeaderView(Qt::Orientation orientation, QWidget* parent)
: QHeaderView(orientation, parent)
{
connect(this, &QHeaderView::sectionClicked, this, &HeaderView::onSectionClicked);
}
HeaderView::~HeaderView()
{
}
void HeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
{
if (!rect.isValid())
return;
//使用QHeaderView默认
QHeaderView::paintSection(painter, rect, logicalIndex);
TreeModel* tmodel = dynamic_cast<TreeModel*>(model());
if (tmodel)
{
MultiColumnOrder sortOdrs = tmodel->orderColumns;
for (int i = 0; i < sortOdrs.size(); i++)
{
QPair<int, Qt::SortOrder> sortCol = sortOdrs.at(i);
if (sortCol.first == logicalIndex)
{
///暂时只做如下处理
QStyleOptionHeader opt;
initStyleOption(&opt);
switch (sortCol.second)
{
case Qt::AscendingOrder:
opt.sortIndicator = QStyleOptionHeader::SortDown; //
break;
case Qt::DescendingOrder:
opt.sortIndicator = QStyleOptionHeader::SortUp;
break;
default:
break;
}
opt.rect = rect;
opt.section = logicalIndex;
//获取表头数据
opt.text = model()->headerData(logicalIndex, Qt::Horizontal).toString();
opt.textAlignment = defaultAlignment();
opt.iconAlignment = Qt::AlignVCenter;
style()->drawControl(QStyle::CE_Header, &opt, painter, this);
return;
}
}
}
}
///slot to sectionClicked()
void HeaderView::onSectionClicked(int logicalIndex)
{
//HeadView->flipSortIndicator函数中会调用setSortIndicator,然后emit sortIndicatorChanged 信号
//TreeView 接收sortIndicatorChanged信号后,会调用sort函数。
//而在HeadView->flipSortIndicator返回后,才会emit sectionClicked 信号。
//在headView中,增加了sectionClicked信号的处理函数onSectionClicked,设置表头多列排序样式。
//这两处需保持效果同步
Qt::SortOrder sortOdr = QHeaderView::sortIndicatorOrder();
QPair<int, Qt::SortOrder> colSort(logicalIndex, sortOdr);
if (QApplication::keyboardModifiers() == Qt::ShiftModifier)
{
this->ableMultiSort = true;
for (int i = 0; i < orderCols.size(); i++)
{
if (orderCols.at(i).first == logicalIndex)
{
orderCols.removeAt(i);
break;
}
}
}
else
{
this->ableMultiSort = false;
orderCols.clear();
}
orderCols.append(colSort); //无论如何都把最新的sortIndicator存入orderCols
}
void HeaderView::hideSectionAndEmitSignal(int logicalIndex)
{
QHeaderView::hideSection(logicalIndex);
emit hideSectionSignal(logicalIndex);
}
void HeaderView::showSectionAndEmitSignal(int logicalIndex)
{
QHeaderView::showSection(logicalIndex);
emit showSectionSignal(logicalIndex);
}
int HeaderView::firstVisiableLogicalIndex()
{
for (int i = 0; i < count(); i++)
{
int idx = logicalIndex(i);
if (!isSectionHidden(idx))
{
return idx;
}
}
return 0;
}
void HeaderView::mousePressEvent(QMouseEvent* e)
{
if (e->button() == Qt::RightButton)
{
int logicalIndex = logicalIndexAt(e->pos());
emit sectionRightClicked(logicalIndex);
}
else if (e->button() == Qt::LeftButton)
{
bLeftMousePressed = true;
toHideIndex = logicalIndexAt(e->pos());
if (toHideIndex > -1)
{
mstate = PressState;
}
}
QHeaderView::mousePressEvent(e);
}
void HeaderView::mouseMoveEvent(QMouseEvent* e)
{
bLeftMousePressed = false;
int curpos, btmpos;
if (orientation() == Qt::Orientation::Horizontal)
{
curpos = e->y();
btmpos = QPoint(pos().x(), pos().y() + height()).y();
}
else
{
curpos = e->x();
btmpos = QPoint(pos().x() + width(), pos().y()).x();
}
if (curpos > btmpos)
{
if (mstate == PressState && cursor() == Qt::ArrowCursor)///暂时通过鼠标形状来判断是否
{
mstate = MoveState;
stageCursor = cursor();
setCursor(QCursor(QPixmap(":Resources/blind.png")));
}
}
else
{
if (mstate == MoveState)
{
mstate = PressState;
setCursor(stageCursor);
}
}
QHeaderView::mouseMoveEvent(e);
}
void HeaderView::mouseReleaseEvent(QMouseEvent* e)
{
if (mstate == MoveState)
{
if (toHideIndex > -1 && toHideIndex < count())
{
hideSectionAndEmitSignal(toHideIndex);
}
}
mstate = NoState;
if (bLeftMousePressed)
{
bLeftMousePressed = false;
int pos = orientation() == Qt::Horizontal ? e->x() : e->y();
int logicalIndex = logicalIndexAt(pos);
emit sectionSingleClicked(logicalIndex);
}
QHeaderView::mouseReleaseEvent(e);
}