-
Notifications
You must be signed in to change notification settings - Fork 9
/
pqObjectNaming.cxx
317 lines (274 loc) · 8.53 KB
/
pqObjectNaming.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
/*=========================================================================
Program: ParaView
Module: pqObjectNaming.cxx
Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.2.
See License_v1.2.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
#include "pqObjectNaming.h"
#include <QAbstractItemDelegate>
#include <QAbstractItemModel>
#include <QAction>
#include <QApplication>
#include <QDockWidget>
#include <QFocusFrame>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>
#include <QMenu>
#include <QMenuBar>
#include <QScrollBar>
#include <QSet>
#include <QSignalMapper>
#include <QStackedWidget>
#include <QTabBar>
#include <QTabWidget>
#include <QTextStream>
#include <QToolBar>
#include <QToolButton>
#include <QtDebug>
namespace
{
QString ErrorMessage;
}
/** Returns the name of an object as if it was unnamed.*/
static const QString InternalGetNameAsUnnamed(QObject& Object)
{
QString result;
QObjectList siblings;
if (Object.parent())
{
siblings = Object.parent()->children();
}
else
{
QWidgetList widgets = QApplication::topLevelWidgets();
for (int i = 0; i != widgets.size(); ++i)
{
siblings.push_back(widgets[i]);
}
}
const QString type = Object.metaObject()->className();
// order of top level widgets is not guarenteed
// we can someone counter that by checking visibility,
// as we usually only test visible widgets, we would get the right one
int invisible_index = 0;
int visible_index = 0;
for (int i = 0; i != siblings.size(); ++i)
{
QObject* test = siblings[i];
if (test == &Object)
{
break;
}
else if (type == test->metaObject()->className() && test->objectName().isEmpty())
{
QWidget* widget = qobject_cast<QWidget*>(test);
if (widget && widget->isVisible())
{
++visible_index;
}
else
{
++invisible_index;
}
}
}
int index = invisible_index;
if (QWidget* const widget = qobject_cast<QWidget*>(&Object))
{
if (widget->isVisible())
{
result += QString::number(1);
index = visible_index;
}
else
{
result += QString::number(0);
}
}
result += type + QString::number(index);
result.replace("/", "|");
return result;
}
/** Returns the name of an object. If the object doesn't have an explicit name,
assigns a name as a convenience. Also replaces problematic characters such as '/'.
*/
static const QString InternalGetName(QObject& Object)
{
QString result = Object.objectName();
if (result.isEmpty())
{
result = InternalGetNameAsUnnamed(Object);
}
if (qobject_cast<QApplication*>(&Object))
{
result.append("-app");
}
result.replace("/", "|");
return result;
}
const QString pqObjectNaming::GetName(QObject& Object)
{
QString name = InternalGetName(Object);
if (name.isEmpty())
{
qCritical() << "Cannot record event for unnamed object " << &Object;
return QString();
}
for (QObject* p = Object.parent(); p; p = p->parent())
{
const QString parent_name = InternalGetName(*p);
if (parent_name.isEmpty())
{
qCritical() << "Cannot record event for incompletely-named object " << name << " " << &Object
<< " with parent " << p;
return QString();
}
name = parent_name + "/" + name;
if (!p->parent() && !QApplication::topLevelWidgets().contains(qobject_cast<QWidget*>(p)))
{
qCritical() << "Unable to to determine name for Object " << &Object << " because a parent "
<< p << " is not a top-level widget. Name so far = " << name;
return QString();
}
}
return name;
}
QObject* pqObjectNaming::GetObject(const QString& Name)
{
QObject* result = 0;
QObject* lastObject = 0;
if (Name.isEmpty())
{
return 0;
}
const QStringList names = Name.split("/");
// see if QApplication is the requested object
QString app_name = InternalGetName(*QApplication::instance());
if (app_name == Name)
{
return QApplication::instance();
}
const QWidgetList top_level_widgets = QApplication::topLevelWidgets();
for (int i = 0; i != top_level_widgets.size(); ++i)
{
QObject* object = top_level_widgets[i];
const QString name = InternalGetName(*object);
const QString alt_name = InternalGetNameAsUnnamed(*object);
if (name == names[0] || alt_name == names[0])
{
result = object;
lastObject = object;
break;
}
}
for (int j = 1; j < names.size(); ++j)
{
const QObjectList& children = result ? result->children() : QObjectList();
result = 0;
QString objectName = names[j];
for (int k = 0; k != children.size(); ++k)
{
QObject* child = children[k];
const QString name = InternalGetName(*child);
const QString alt_name = InternalGetNameAsUnnamed(*child);
if (name == objectName || alt_name == objectName)
{
result = child;
lastObject = child;
break;
}
// Sometimes, when playing, widget are visible when they were not during recording,
// try again with visibility at 1.
if (k == children.size() - 1 && result == 0 && !objectName.isEmpty() && objectName[0] == '0')
{
objectName[0] = '1';
k = 0;
}
}
}
if (result)
return result;
ErrorMessage.clear();
QTextStream stream(&ErrorMessage);
stream << "Couldn't find object `" << Name << "`\n";
if (lastObject)
{
stream << "Found up to `" << pqObjectNaming::GetName(*lastObject) << "`\n";
}
// controls how many matches to dump in error message.
QString matchLimitEnv = QString::fromUtf8(qgetenv("PQOBJECTNAMING_MATCH_LIMIT"));
const int matchLimit = matchLimitEnv.isEmpty() ? 20 : matchLimitEnv.toInt();
bool foundMatch = false;
if (lastObject)
{
const QObjectList matches = lastObject->findChildren<QObject*>(names[names.size() - 1]);
for (int cc = 0; (matchLimit <= 0 || cc < matchLimit) && cc < matches.size(); ++cc)
{
stream << "\tPossible match: `" << pqObjectNaming::GetName(*matches[cc]) << "`\n";
}
if (matchLimit > 0 && matches.size() > matchLimit)
{
stream << "\tPossible match: .... (and " << (matches.size() - matchLimit) << " more!)\n"
<< "\tSet PQOBJECTNAMING_MATCH_LIMIT environment var to a +'ve number to limit "
"entries (or 0 for unlimited).\n";
}
}
if (!foundMatch)
{
const QObjectList matches = lastObject->findChildren<QObject*>();
for (int cc = 0; (matchLimit <= 0 || cc < matchLimit) && cc < matches.size(); ++cc)
{
stream << "\tAvailable widget: `" << pqObjectNaming::GetName(*matches[cc]) << "`\n";
}
if (matchLimit > 0 && matches.size() > matchLimit)
{
stream << "\tAvailable widget: .... (and " << (matches.size() - matchLimit) << " more!)\n"
<< "\tSet PQOBJECTNAMING_MATCH_LIMIT environment var to a +'ve number to limit "
"entries (or 0 for unlimited).\n";
}
}
return 0;
}
void pqObjectNaming::DumpHierarchy(QStringList& results)
{
const QWidgetList widgets = QApplication::topLevelWidgets();
for (int i = 0; i != widgets.size(); ++i)
{
DumpHierarchy(*widgets[i], results);
}
}
void pqObjectNaming::DumpHierarchy(QObject& object, QStringList& results)
{
results << GetName(object);
const QObjectList children = object.children();
for (int i = 0; i != children.size(); ++i)
{
DumpHierarchy(*children[i], results);
}
}
QString pqObjectNaming::lastErrorMessage()
{
return ErrorMessage;
}