-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpqAbstractItemViewEventPlayerBase.cxx
348 lines (327 loc) · 11.7 KB
/
pqAbstractItemViewEventPlayerBase.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
// SPDX-License-Identifier: BSD-3-Clause
#include "pqAbstractItemViewEventPlayerBase.h"
#include "pqEventTypes.h"
#include <QAbstractItemView>
#include <QContextMenuEvent>
#include <QCoreApplication>
#include <QDebug>
#include <QMenu>
#include <QtGlobal>
namespace
{
int findSection(QAbstractItemModel* model, const QString& label, Qt::Orientation orientation,
int role = Qt::DisplayRole)
{
QStringList currentHeaders;
const int max = orientation == Qt::Horizontal ? model->columnCount() : model->rowCount();
for (int section = 0; section < max; ++section)
{
auto data = model->headerData(section, orientation, role).toString();
currentHeaders.push_back(data);
if (data == label)
{
return section;
}
}
qCritical() << "No header labeled '" << label << "' was found. "
<< "Available values are " << currentHeaders;
return -1;
}
} // end of namespace
//-----------------------------------------------------------------------------
pqAbstractItemViewEventPlayerBase::pqAbstractItemViewEventPlayerBase(QObject* parentObject)
: Superclass(parentObject)
{
}
//-----------------------------------------------------------------------------
pqAbstractItemViewEventPlayerBase::~pqAbstractItemViewEventPlayerBase() {}
//-----------------------------------------------------------------------------
QModelIndex pqAbstractItemViewEventPlayerBase::GetIndex(
const QString& itemStr, QAbstractItemView* abstractItemView, bool& error)
{
// Get only the "index" part of the string
int sep = itemStr.indexOf(",");
QString strIndex = itemStr.left(sep);
// Recover model index
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
const QStringList indices = strIndex.split(".", Qt::SkipEmptyParts);
#else
const QStringList indices = strIndex.split(".", QString::SkipEmptyParts);
#endif
if (indices.isEmpty() || (indices.size() % 2 != 0)) // indices are in pairs (row, column).
{
error = true;
qCritical() << "ERROR: Incorrect number of values in index! Cannot playback.";
return QModelIndex();
}
QList<int> iIndices;
auto model = abstractItemView->model();
// indices may be simply ints or strings. If not ints, then assume strings that
// represent row or column name.
for (int cc = 0; cc < indices.size(); ++cc)
{
bool ok;
int index = indices[cc].toInt(&ok);
if (!ok)
{
// must be a string that represents the row/column name. determine the index.
index = ::findSection(model, indices[cc], (cc % 2 == 0) ? Qt::Vertical : Qt::Horizontal);
if (index == -1)
{
error = true;
return QModelIndex();
}
}
iIndices.push_back(index);
}
QModelIndex index;
for (int cc = 0; (cc + 1) < iIndices.size(); cc += 2)
{
index = abstractItemView->model()->index(iIndices[cc], iIndices[cc + 1], /*parent=*/index);
if (!index.isValid())
{
error = true;
qCritical() << "ERROR: Abstract Item view must have changed. "
<< "Indices recorded in the test are no longer valid. Cannot playback.";
break;
}
}
return index;
}
//-----------------------------------------------------------------------------
QString pqAbstractItemViewEventPlayerBase::GetDataString(const QString& itemStr, bool& error)
{
Q_UNUSED(error);
// Get only the "data" part of the string
int sep = itemStr.indexOf(",");
return itemStr.mid(sep + 1);
}
//-----------------------------------------------------------------------------
bool pqAbstractItemViewEventPlayerBase::playEvent(
QObject* object, const QString& command, const QString& arguments, int eventType, bool& error)
{
QAbstractItemView* abstractItemView = qobject_cast<QAbstractItemView*>(object);
QMenu* contextMenu = qobject_cast<QMenu*>(object);
// if this a QMenu (potentially a context menu of the view),
// we should not move onto parent
if (!abstractItemView && !contextMenu)
{
// mouse events go to the viewport widget
abstractItemView = qobject_cast<QAbstractItemView*>(object->parent());
}
if (!abstractItemView)
{
return false;
}
if (eventType == pqEventTypes::ACTION_EVENT)
{
if (command == "key")
{
return false;
}
QRegularExpression regExp1("^([\\d\\.]+),(\\d+)$");
QRegularExpressionMatch match = regExp1.match(arguments);
if (command == "setCheckState" && match.hasMatch())
{
QString strIndex = match.captured(1);
int check_state = match.captured(2).toInt();
QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(strIndex, abstractItemView, error);
if (error)
{
return true;
}
if (abstractItemView->model()->data(index, Qt::CheckStateRole).toInt() != check_state)
{
abstractItemView->model()->setData(
index, static_cast<Qt::CheckState>(check_state), Qt::CheckStateRole);
}
return true;
}
else if (command == "edit")
{
QString strIndex = arguments;
QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(strIndex, abstractItemView, error);
if (error)
{
return true;
}
abstractItemView->edit(index);
return true;
}
else if (command == "doubleClick")
{
QString strIndex = arguments;
QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(strIndex, abstractItemView, error);
if (error)
{
return true;
}
abstractItemView->doubleClicked(index);
return true;
}
else if (command == "editCancel")
{
QString strIndex = arguments;
QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(strIndex, abstractItemView, error);
abstractItemView->closePersistentEditor(index);
return true;
}
else if (command == "editAccepted")
{
QStringList list = arguments.split(',');
QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(list.at(0), abstractItemView, error);
QVariant value = QVariant(list.at(1));
abstractItemView->model()->setData(index, value);
abstractItemView->closePersistentEditor(index);
return true;
}
else if (command == "setCurrent" || command == "activate")
{
QString strIndex = arguments;
QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(strIndex, abstractItemView, error);
if (error)
{
return true;
}
abstractItemView->setFocus();
abstractItemView->setCurrentIndex(index);
// The following code will cause a modal dialog to close,
// such as the pqColorPresetDialog, if the abstractItemView widget is not handling the key
// event,
// the key event will bevpropogated to the abstractItemView's parent, in this case the dialog,
// and the dialog be default has OK/Cancel button in focus, and will process the key event,
// close itself. This is causing a few a paraview's tests using pqColorPresetDialog to fail.
// The fix here (above) is to add "abstractItemView->setFocus();" so that the
// "abstractItemView->setCurrentIndex(index);" will trigger selection changed event in
// abstractItemView,
// which is the purpose of the following code.
/*
QKeyEvent kd(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QKeyEvent ku(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
QCoreApplication::sendEvent(abstractItemView, &kd);
QCoreApplication::sendEvent(abstractItemView, &ku);
*/
return true;
}
else if (command == "setSelection")
{
QString strIndexList = arguments;
QStringList indexList = strIndexList.split(',');
QItemSelectionModel* selModel = abstractItemView->selectionModel();
// selection doesn't work as expected if we don't respect row/col selection.
QItemSelectionModel::SelectionFlags selFlag = QItemSelectionModel::SelectCurrent;
if (abstractItemView->selectionBehavior() == QAbstractItemView::SelectRows)
{
selFlag |= QItemSelectionModel::Rows;
}
else if (abstractItemView->selectionBehavior() == QAbstractItemView::SelectColumns)
{
selFlag |= QItemSelectionModel::Columns;
}
// shouldn't have to check selectionMode - single or multi enforced when recorded.
auto selMode = abstractItemView->selectionMode();
if (QAbstractItemView::NoSelection == selMode)
{
qCritical() << "ERROR: Multi-select on ItemView with no-select mode :" << selMode;
return true;
}
if (strIndexList.isEmpty())
{
abstractItemView->clearSelection();
return true;
}
QModelIndex topLeft, bottomRight;
QItemSelection selection;
// don't reset the selection - setCurrent does that, and is always recorded first.
for (int i = 0; i < indexList.size(); i += 2)
{
// ranges are recorded in pairs, topLeft -> bottomRight, but to a flat list for
// simplicity.
topLeft =
pqAbstractItemViewEventPlayerBase::GetIndex(indexList.at(i), abstractItemView, error);
bottomRight =
pqAbstractItemViewEventPlayerBase::GetIndex(indexList.at(i + 1), abstractItemView, error);
if (error)
{
return true;
}
QItemSelection itemSel(topLeft, bottomRight);
// merge allows a single selection to contain multiple ranges.
selection.merge(itemSel, QItemSelectionModel::Select);
}
selModel->select(selection, selFlag);
return true;
}
else if (command == "openContextMenu")
{
const QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(arguments, abstractItemView, error);
if (error)
{
return true;
}
const QRect itemRect = abstractItemView->visualRect(index);
const QPoint centerOfItem = itemRect.topLeft();
const QPoint globalCenterOfItem = abstractItemView->viewport()->mapToGlobal(centerOfItem);
QContextMenuEvent contextMenuEvent(
QContextMenuEvent::Other, centerOfItem, globalCenterOfItem);
if (!qApp->notify(abstractItemView->viewport(), &contextMenuEvent))
{
qCritical() << "The " << abstractItemView->objectName()
<< " rejected the context menu event";
error = true;
}
return true;
}
}
else if (eventType == pqEventTypes::CHECK_EVENT)
{
if (command == "modelItemData")
{
// Recover index to check
QString itemString = arguments;
QModelIndex index =
pqAbstractItemViewEventPlayerBase::GetIndex(itemString, abstractItemView, error);
if (error)
{
return true;
}
// Recover Data to check
QString testDataString = pqAbstractItemViewEventPlayerBase::GetDataString(itemString, error);
if (error)
{
return true;
}
// Recover current data, replacing tab by space as they are not existing in xml
QString currentDataString = index.data().toString();
currentDataString.replace("\t", " ");
// Compare strings
if (currentDataString != testDataString)
{
qCritical() << "ERROR: Checked item contain :" << index.data().toString()
<< ".Expecting :" << testDataString;
error = true;
}
return true;
}
else if (command == "modelRowCount")
{
if (abstractItemView->model()->rowCount() != arguments.toInt())
{
qCritical() << "ERROR: Checked abstract item view has "
<< abstractItemView->model()->rowCount() << " rows. Expecting :" << arguments;
error = true;
}
return true;
}
}
return this->Superclass::playEvent(object, command, arguments, eventType, error);
}