-
Notifications
You must be signed in to change notification settings - Fork 14
/
pqAbstractItemViewEventTranslatorBase.cxx
328 lines (302 loc) · 11.5 KB
/
pqAbstractItemViewEventTranslatorBase.cxx
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
// SPDX-License-Identifier: BSD-3-Clause
#include "pqAbstractItemViewEventTranslatorBase.h"
#include "pqEventTypes.h"
#include <QAbstractItemView>
#include <QContextMenuEvent>
#include <QEvent>
#include <QKeyEvent>
#include <QVariant>
#include <QtGlobal>
//-----------------------------------------------------------------------------
pqAbstractItemViewEventTranslatorBase::pqAbstractItemViewEventTranslatorBase(QObject* parentObject)
: Superclass(parentObject)
{
this->Editing = false;
this->AbstractItemView = NULL;
this->ModelItemCheck = NULL;
this->AbstractItemViewMouseTracking = false;
this->Checking = false;
}
//-----------------------------------------------------------------------------
pqAbstractItemViewEventTranslatorBase::~pqAbstractItemViewEventTranslatorBase()
{
if (this->AbstractItemView != NULL)
{
this->AbstractItemView->setMouseTracking(this->AbstractItemViewMouseTracking);
}
}
//-----------------------------------------------------------------------------
bool pqAbstractItemViewEventTranslatorBase::translateEvent(
QObject* object, QEvent* event, int eventType, bool& error)
{
// Recover corrected abstract item view
QAbstractItemView* abstractItemView = this->findCorrectedAbstractItemView(object);
if (!abstractItemView)
{
return false;
}
// Don't try to record events for QComboBox implementation details
if (abstractItemView->inherits("QComboBoxListView"))
{
return false;
}
if (eventType == pqEventTypes::ACTION_EVENT)
{
switch (event->type())
{
case QEvent::KeyRelease:
{
QKeyEvent* ke = static_cast<QKeyEvent*>(event);
QModelIndex index = abstractItemView->currentIndex();
QString indexString = this->getIndexAsString(index);
if (this->Editing)
{
if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
{
QVariant value = abstractItemView->model()->data(index);
this->Editing = false;
Q_EMIT this->recordEvent(abstractItemView, "editAccepted",
QString("%1,%2").arg(indexString, value.toString()));
return true;
break;
}
if (ke->key() == Qt::Key_Escape)
{
this->Editing = false;
Q_EMIT this->recordEvent(abstractItemView, "editCancel", indexString);
return true;
break;
}
}
else if (ke->key() == Qt::Key_F2)
{
this->Editing = true;
Q_EMIT this->recordEvent(abstractItemView, "edit", indexString);
return true;
break;
}
break;
}
case QEvent::Enter:
{
if (this->Checking)
{
// If we entered before in check mode, then we need to reset cached view pointer.
this->Checking = false;
this->AbstractItemView = nullptr;
}
this->monitorSignals(abstractItemView);
return true;
break;
}
case QEvent::ContextMenu:
{
auto contextMenuEvent = dynamic_cast<QContextMenuEvent*>(event);
Q_EMIT this->recordEvent(abstractItemView, "openContextMenu",
this->getIndexAsString(abstractItemView->indexAt(contextMenuEvent->pos())));
return true;
}
default:
{
break;
}
}
}
else if (eventType == pqEventTypes::CHECK_EVENT)
{
if (event->type() == QEvent::MouseMove)
{
// Entering while checking
if (this->AbstractItemView != abstractItemView || !this->Checking)
{
// Checking flag
this->Checking = true;
// Disconnect precedently check item view
if (this->AbstractItemView != NULL)
{
this->AbstractItemView->setMouseTracking(this->AbstractItemViewMouseTracking);
QObject::disconnect(this->AbstractItemView, SIGNAL(entered(const QModelIndex&)), this,
SLOT(onEnteredCheck(const QModelIndex&)));
QObject::disconnect(this->AbstractItemView, SIGNAL(viewportEntered()), this,
SLOT(onViewportEnteredCheck()));
}
// Keep track of checked item view
this->AbstractItemView = abstractItemView;
// Set mouse tracking so entered item signal are emitted
this->AbstractItemViewMouseTracking = this->AbstractItemView->hasMouseTracking();
this->AbstractItemView->setMouseTracking(true);
// Connect item entered event to the entered check slot
QObject::connect(this->AbstractItemView, SIGNAL(entered(const QModelIndex&)), this,
SLOT(onEnteredCheck(const QModelIndex&)));
QObject::connect(
this->AbstractItemView, SIGNAL(viewportEntered()), this, SLOT(onViewportEnteredCheck()));
}
return true;
}
// Clicking while checking, actual check event
if (event->type() == QEvent::MouseButtonRelease)
{
// Item Check
if (this->ModelItemCheck != NULL)
{
QString indexString = this->getIndexAsString(*this->ModelItemCheck);
Q_EMIT this->recordEvent(abstractItemView, "modelItemData",
QString("%1,%2")
.arg(indexString)
.arg(
// Replacing tab by space, as they are not valid in xml
this->ModelItemCheck->data().toString().replace("\t", " ")),
pqEventTypes::CHECK_EVENT);
}
// Abstract Item View nb row check
else
{
Q_EMIT this->recordEvent(abstractItemView, "modelRowCount",
QString::number(abstractItemView->model()->rowCount()), pqEventTypes::CHECK_EVENT);
}
return true;
}
}
return this->Superclass::translateEvent(object, event, eventType, error);
}
//-----------------------------------------------------------------------------
void pqAbstractItemViewEventTranslatorBase::monitorSignals(QAbstractItemView* abstractItemView)
{
if (abstractItemView != this->AbstractItemView)
{
if (this->AbstractItemView)
{
this->AbstractItemView->disconnect(this);
}
QObject::connect(abstractItemView, SIGNAL(clicked(const QModelIndex&)), this,
SLOT(onClicked(const QModelIndex&)));
QObject::connect(abstractItemView, SIGNAL(activated(const QModelIndex&)), this,
SLOT(onActivated(const QModelIndex&)));
QObject::connect(abstractItemView, SIGNAL(doubleClicked(const QModelIndex&)), this,
SLOT(onDoubleClicked(const QModelIndex&)));
monitorSignalsInternal(abstractItemView);
this->AbstractItemView = abstractItemView;
}
// If no model has been set yet, there will be no selectionModel
auto selectionModel = abstractItemView->selectionModel();
if (selectionModel)
{
if (selectionModel != this->ItemSelectionModel)
{
if (this->ItemSelectionModel)
{
this->ItemSelectionModel->disconnect(this);
}
this->ItemSelectionModel = selectionModel;
QObject::connect(selectionModel,
SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this,
SLOT(onCurrentChanged(const QModelIndex&)));
QObject::connect(selectionModel,
SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this,
SLOT(onSelectionChanged(const QItemSelection&)));
}
}
}
//-----------------------------------------------------------------------------
void pqAbstractItemViewEventTranslatorBase::onClicked(const QModelIndex& index)
{
static QModelIndex oldIndex;
QAbstractItemView* abstractItemView = qobject_cast<QAbstractItemView*>(this->sender());
QString indexString = this->getIndexAsString(index);
if ((index.model()->flags(index) & Qt::ItemIsUserCheckable) != 0)
{
// record the check state change if the item is user-checkable.
Q_EMIT this->recordEvent(abstractItemView, "setCheckState",
QString("%1,%3")
.arg(indexString)
.arg(index.model()->data(index, Qt::CheckStateRole).toInt()));
}
if ((abstractItemView->editTriggers() & QAbstractItemView::SelectedClicked) ==
QAbstractItemView::SelectedClicked &&
index == oldIndex)
{
this->Editing = true;
Q_EMIT this->recordEvent(abstractItemView, "edit", indexString);
}
oldIndex = index;
}
//-----------------------------------------------------------------------------
void pqAbstractItemViewEventTranslatorBase::onActivated(const QModelIndex& index)
{
QAbstractItemView* abstractItemView = qobject_cast<QAbstractItemView*>(this->sender());
QString indexString = this->getIndexAsString(index);
Q_EMIT this->recordEvent(abstractItemView, "activate", indexString);
}
//-----------------------------------------------------------------------------
void pqAbstractItemViewEventTranslatorBase::onDoubleClicked(const QModelIndex& index)
{
QAbstractItemView* abstractItemView = qobject_cast<QAbstractItemView*>(this->sender());
QString indexString = this->getIndexAsString(index);
if ((abstractItemView->editTriggers() & QAbstractItemView::DoubleClicked) ==
QAbstractItemView::DoubleClicked)
{
this->Editing = true;
Q_EMIT this->recordEvent(abstractItemView, "doubleClick", indexString);
Q_EMIT this->recordEvent(abstractItemView, "edit", indexString);
}
}
//-----------------------------------------------------------------------------
QString pqAbstractItemViewEventTranslatorBase::getIndexAsString(const QModelIndex& index)
{
QString indexString;
for (QModelIndex curIndex = index; curIndex.isValid(); curIndex = curIndex.parent())
{
indexString.prepend(QString("%1.%2.").arg(curIndex.row()).arg(curIndex.column()));
}
// remove the last ".".
indexString.chop(1);
return indexString;
}
QString pqAbstractItemViewEventTranslatorBase::getIndicesAsString(
const QModelIndexList& selectedIndices)
{
QString listString;
for (QModelIndex idx : selectedIndices)
{
listString.append(QString("%1,").arg(this->getIndexAsString(idx)));
}
// remove the last ",".
listString.chop(1);
return listString;
}
//-----------------------------------------------------------------------------
void pqAbstractItemViewEventTranslatorBase::onCurrentChanged(const QModelIndex& index)
{
Q_EMIT this->recordEvent(this->AbstractItemView, "setCurrent", this->getIndexAsString(index));
}
//-----------------------------------------------------------------------------
void pqAbstractItemViewEventTranslatorBase::onSelectionChanged(const QItemSelection& selected)
{
Q_UNUSED(selected);
// see if the view supports multi-select
auto selMode = this->AbstractItemView->selectionMode();
if (!(QAbstractItemView::SingleSelection == selMode || QAbstractItemView::NoSelection == selMode))
{
// selected and deselected args build on current selection. Retrieve the total selection instead
QItemSelectionModel* selModel = qobject_cast<QItemSelectionModel*>(this->sender());
QModelIndexList selectedIndices;
QItemSelection selection = selModel->selection();
// selections are a list of disjoint ranges, record the bounds of each.
// this works better for playback than recording all the selected indexes.
for (QItemSelectionRange selRange : selection)
{
selectedIndices.push_back(selRange.topLeft());
selectedIndices.push_back(selRange.bottomRight());
}
Q_EMIT this->recordEvent(
this->AbstractItemView, "setSelection", this->getIndicesAsString(selectedIndices));
}
}
//-----------------------------------------------------------------------------
void pqAbstractItemViewEventTranslatorBase::onViewportEnteredCheck()
{
this->ModelItemCheck = NULL;
Q_EMIT this->specificOverlay(this->AbstractItemView->rect());
}