-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectioncontroller.cpp
189 lines (160 loc) · 5.58 KB
/
selectioncontroller.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
183
184
185
186
187
188
189
#include "selectioncontroller.h"
#include <QQmlEngine>
#include "privateselectioncontroller.h"
static bool registerMe() {
qmlRegisterType<SelectionController>(
QString("cpp.%1").arg(SelectionController::ITEM_NAME).toUtf8(), 12, 34,
SelectionController::ITEM_NAME.toUtf8());
return true;
}
const QString SelectionController::ITEM_NAME = "SelectionController";
const bool SelectionController::IS_QML_REG = registerMe();
SelectionController::SelectionController(QObject *parent) : SelectionController(nullptr, parent) { }
SelectionController::SelectionController(QAbstractItemModel *model, QObject *parent)
: QObject(parent), _model(model),
_private(new PrivateSelectionController(this)) { }
void SelectionController::normalizeBounds() {
if (_startRow > _endRow) {
std::swap(_startRow, _endRow);
_private->emitStartRowChanged();
_private->emitEndRowChanged();
}
if (_startColumn > _endColumn) {
std::swap(_startColumn, _endColumn);
_private->emitStartRowChanged();
_private->emitEndRowChanged();
}
setStartColumn(std::max(0, _startColumn));
setStartRow(std::max(0, _startRow));
setEndColumn(std::min(_endColumn, _model->columnCount() - 1));
setEndRow(std::min(_endRow, _model->rowCount() - 1));
setActiveRow(std::min(std::max(0, _activeRow), _model->rowCount() - 1));
setActiveColumn(std::min(std::max(0, _activeColumn), _model->columnCount() - 1));
}
void SelectionController::collapseToActive() {
setStartRow(activeRow());
setEndRow(activeRow());
setStartColumn(activeColumn());
setEndColumn(activeColumn());
}
int SelectionController::startRow() const {
return _startRow;
}
void SelectionController::setStartRow(int startRow) {
if (_startRow == startRow) { return; }
if (_disable && startRow != activeRow()) { return; }
_startRow = startRow;
_private->emitStartRowChanged();
}
int SelectionController::startColumn() const {
return _startColumn;
}
void SelectionController::setStartColumn(int startColumn) {
if (_startColumn == startColumn) { return; }
if (_disable && startColumn != activeColumn()) { return; }
_startColumn = startColumn;
_private->emitStartColumnChanged();
}
int SelectionController::endRow() const {
return _endRow;
}
void SelectionController::setEndRow(int endRow) {
if (_endRow == endRow) { return; }
if (_disable && endRow != activeRow()) { return; }
_endRow = endRow;
_private->emitEndRowChanged();
}
int SelectionController::endColumn() const {
return _endColumn;
}
void SelectionController::setEndColumn(int endColumn) {
if (_endColumn == endColumn) { return; }
if (_disable && endColumn != activeColumn()) { return; }
_endColumn = endColumn;
_private->emitEndColumnChanged();
}
int SelectionController::activeRow() const {
return _activeRow < 0 ? 0 : _activeRow;
}
void SelectionController::setActiveRow(int activeRow) {
if (_activeRow == activeRow) { return; }
_activeRow = activeRow;
_private->emitActiveRowChanged();
if (_disable) {
collapseToActive();
}
}
int SelectionController::activeColumn() const {
return _activeColumn < 0 ? 0 : _activeColumn;
}
void SelectionController::setActiveColumn(int activeColumn) {
if (_activeColumn == activeColumn) { return; }
_activeColumn = activeColumn;
_private->emitActiveColumnChanged();
if (_disable) {
collapseToActive();
}
}
bool SelectionController::mouseSelection() const {
return _mouseSelection;
}
void SelectionController::setMouseSelection(bool mouseSelection) {
if (_mouseSelection == mouseSelection) { return; }
_mouseSelection = mouseSelection;
_private->emitMouseSelectionChanged();
}
QPoint SelectionController::activeCell() const {
return QPoint(activeColumn(), activeRow());
}
void SelectionController::setActiveCell(const QPoint &active) {
if (activeCell() == active) { return; }
_private->disconnectActiveCell();
setActiveRow(active.y());
setActiveColumn(active.x());
if (_disable) {
collapseToActive();
}
_private->emitActiveCellChanged();
_private->connectActiveCell();
}
QRect SelectionController::selectedArea() const {
return QRect(QPoint(_startColumn, _startRow), QPoint(_endColumn, _endRow));
}
void SelectionController::setSelectedArea(const QRect &selectedArea) {
if (SelectionController::selectedArea() == selectedArea) { return; }
if (_disable) { return; }
_private->disconnectSelectedArea();
const auto &topLeft = selectedArea.topLeft();
setStartRow(topLeft.y());
setStartColumn(topLeft.x());
const auto &bottomRight = selectedArea.bottomRight();
setEndRow(bottomRight.y());
setEndColumn(bottomRight.x());
_private->emitSelectedAreaChanged();
_private->connectSelectedArea();
}
QAbstractItemModel *SelectionController::model() const {
return _model;
}
void SelectionController::setModel(QAbstractItemModel *model) {
if (_model == model) { return; }
_model = model;
_private->emitModelChanged();
}
QString SelectionController::toString() const {
return QString("SelectionController(Active(%1,%2), Selection(%3,%4 %5x%6)")
.arg(activeRow()).arg(activeColumn())
.arg(startColumn()).arg(startRow())
.arg(selectedArea().width()).arg(selectedArea().height());
}
bool SelectionController::disable() const {
return _disable;
}
void SelectionController::setDisable(bool disable) {
if (_disable == disable) { return; }
_disable = disable;
if (_disable) {
collapseToActive();
}
emit disableChanged();
}