forked from mojocorp/QSpreadsheetHeaderView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QSpreadsheetHeaderView.cpp
178 lines (143 loc) · 5.91 KB
/
QSpreadsheetHeaderView.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
/*
*
* This file is part of QSpreadsheetHeaderView,
* an open-source header view widget
*
* Copyright (C) 2012 Morgan Leborgne
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with QSpreadsheetHeaderView. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <QPainter>
#include <QCursor>
#include <QHoverEvent>
#include <QMenu>
#include "QSpreadsheetHeaderView.h"
QSpreadsheetHeaderView::QSpreadsheetHeaderView(Qt::Orientation orientation, QWidget * parent)
: QHeaderView(orientation, parent)
{
// Required to refresh button menu when the mouse leave the header.
setAttribute(Qt::WA_Hover, true);
}
void QSpreadsheetHeaderView::mousePressEvent ( QMouseEvent * event )
{
QHeaderView::mousePressEvent(event);
int logicalIndex = logicalIndexAt(event->pos());
if (buttonMenuRect(logicalIndex).contains(event->pos())) {
QMenu menu(this);
QAction *hideCol = menu.addAction("Hide column");
QAction *sortAZ = menu.addAction("Sort sheet A->Z");
QAction *sortZA = menu.addAction("Sort sheet Z->A");
// Disable hide column if only one column remains. Otherwise
// the gui is no more available to show them back.
hideCol->setEnabled(hiddenSectionCount() < count() - 1);
QAction *res = menu.exec(mapToGlobal(event->pos()));
if (res == hideCol) {
hideSection(logicalIndex);
updateSection(logicalIndex-1);
}
if (res == sortAZ)
model()->sort(logicalIndex, Qt::AscendingOrder);
if (res == sortZA)
model()->sort(logicalIndex, Qt::DescendingOrder);
}
// Catch previous arrow mouse click.
if (prevRect(logicalIndex).contains(event->pos())) {
showSection(logicalIndex - 1);
updateSection(logicalIndex - 2);
}
// Catch next arrow mouse click.
if (nextRect(logicalIndex).contains(event->pos())) {
showSection(logicalIndex + 1);
updateSection(logicalIndex + 2);
}
}
void QSpreadsheetHeaderView::mouseMoveEvent(QMouseEvent * event)
{
QHeaderView::mouseMoveEvent(event);
// Required to refresh the button menu enable/disable state.
int logicalIndex = logicalIndexAt(event->pos());
updateSection(logicalIndex);
}
void QSpreadsheetHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if (!rect.isValid() || isSortIndicatorShown())
return;
if (isSectionHidden(logicalIndex - 1)) {
drawPrevButton(painter, logicalIndex);
}
if (isSectionHidden(logicalIndex + 1)) {
drawNextButton(painter, logicalIndex);
}
QPoint pos = mapFromGlobal(QCursor::pos());
if (rect.contains(pos)) {
drawMenuButton(painter, logicalIndex, buttonMenuRect(logicalIndex).contains(pos));
}
}
QRect QSpreadsheetHeaderView::sectionRect(int logicalIndex) const
{
return QRect(sectionViewportPosition(logicalIndex), 0, sectionSize(logicalIndex), height());
}
QRect QSpreadsheetHeaderView::buttonMenuRect(int logicalIndex) const
{
QRect sr = sectionRect(logicalIndex);
return QRect(sr.right() - 5 - 13, sr.center().y() - 6, 13, 13);
}
QRect QSpreadsheetHeaderView::prevRect(int logicalIndex) const
{
if (isSectionHidden(logicalIndex))
return QRect();
QRect sr = sectionRect(logicalIndex);
return QRect(sr.left() + 1, sr.center().y() - 6, 13, 13);
}
QRect QSpreadsheetHeaderView::nextRect(int logicalIndex) const
{
if (isSectionHidden(logicalIndex))
return QRect();
QRect sr = sectionRect(logicalIndex);
return QRect(sr.right() - 13, sr.center().y() - 6, 13, 13);
}
void QSpreadsheetHeaderView::drawMenuButton(QPainter *painter, int logicalIndex, bool enabled) const
{
QRect brect = buttonMenuRect(logicalIndex);
painter->setPen(enabled ? QColor(186,186,186) : QColor(223, 223, 223));
painter->setBrush(QColor(246,246,246));
painter->drawRect(brect.adjusted(0,0,-1,-1));
painter->setPen(enabled ? QColor(71,71,71) : QColor(193, 193, 193));
painter->drawLine(brect.left()+3, brect.top()+5, brect.right()-3, brect.top()+5);
painter->drawLine(brect.left()+4, brect.top()+6, brect.right()-4, brect.top()+6);
painter->drawLine(brect.left()+5, brect.top()+7, brect.right()-5, brect.top()+7);
painter->drawPoint(brect.left()+6, brect.top()+8);
}
void QSpreadsheetHeaderView::drawPrevButton(QPainter *painter, int logicalIndex) const
{
QRect rect = prevRect(logicalIndex);
painter->setPen(QColor(71,71,71));
painter->drawLine(rect.left()+1, rect.center().y() - 3, rect.left()+1, rect.center().y() + 3);
painter->drawLine(rect.left()+2, rect.center().y() - 2, rect.left()+2, rect.center().y() + 2);
painter->drawLine(rect.left()+3, rect.center().y() - 1, rect.left()+3, rect.center().y() + 1);
painter->drawPoint(rect.left()+4, rect.center().y());
}
void QSpreadsheetHeaderView::drawNextButton(QPainter *painter, int logicalIndex) const
{
QRect rect = nextRect(logicalIndex);
painter->setPen(QColor(71,71,71));
painter->drawLine(rect.right()-2, rect.center().y() - 3, rect.right()-2, rect.center().y() + 3);
painter->drawLine(rect.right()-3, rect.center().y() - 2, rect.right()-3, rect.center().y() + 2);
painter->drawLine(rect.right()-4, rect.center().y() - 1, rect.right()-4, rect.center().y() + 1);
painter->drawPoint(rect.right()-5, rect.center().y());
}