-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParameter_Tree_Model.cc
566 lines (518 loc) · 13.1 KB
/
Parameter_Tree_Model.cc
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* Parameter_Tree_Model
HiROC CVS ID: $Id: Parameter_Tree_Model.cc,v 1.23 2012/11/13 22:29:37 guym Exp $
Copyright (C) 2012 Arizona Board of Regents on behalf of the
Planetary Image Research Laboratory, Lunar and Planetary Laboratory at
the University of Arizona.
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License, version 2.1,
as published by the Free Software Foundation.
This library 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 this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*******************************************************************************/
#include "Parameter_Tree_Model.hh"
#include "PVL.hh"
using idaeim::PVL::Aggregate;
using idaeim::PVL::Parameter;
using idaeim::PVL::Value;
#include <QTreeView>
#include <string>
using std::string;
#if defined (DEBUG_SECTION)
/* DEBUG_SECTION controls
DEBUG_SECTION report selection options.
Define any of the following options to obtain the desired debug reports:
Note: In the context of Makefiles generated by the Qt qmake utility
the DEBUG_SECTION token is always defined when a "debug" build is
done. The token will be defined as empty by default in the Makefile
unless DEBUG_SECTION is defined by the user as an environment
variable or on the make command line. Since an empty definition can
not be distinguished from a 0 definition the default when a debug
build is done is to use DEBUG_OFF.
*/
#define DEBUG_OFF 0
#define DEBUG_ALL (-1)
#define DEBUG_CONSTRUCTORS (1 << 0)
#define DEBUG_MODEL (1 << 1)
#define DEBUG_DATA (1 << 2)
#define DEBUG_ARRAY_SUBTREES (1 << 3)
#if (DEBUG_SECTION + 0) == 0
#undef DEBUG_SECTION
#define DEBUG_SECTION DEBUG_OFF
#else
#include <iostream>
#include <iomanip>
using std::clog;
using std::endl;
using std::boolalpha;
using std::hex;
using std::dec;
#endif
#endif // DEBUG_SECTION
namespace UA
{
namespace HiRISE
{
/*==============================================================================
Constants
*/
const char* const
Parameter_Tree_Model::ID = "UA::HiRISE::Parameter_Tree_Model ($Revision: 1.23 $ $Date: 2012/11/13 22:29:37 $)";
#ifndef AS_STRING
/* Provides stringification of #defined names.
Note: The extra double quotes are for MSVC which fails to stringify
__VA_ARGS__ if its value is empty (STRINGIFIED has no argument).
In this case the double quotes coalesce into the intended empty
string constant; otherwise they have no effect on the string generated.
*/
#define STRINGIFIED(...) "" #__VA_ARGS__ ""
#define AS_STRING(...) STRINGIFIED(__VA_ARGS__)
#endif
#ifndef DEFAULT_PARAMETER_TREE_ROOT_NAME
#define DEFAULT_PARAMETER_TREE_ROOT_NAME Root
#endif
#define _DEFAULT_ROOT_NAME_ AS_STRING(DEFAULT_PARAMETER_TREE_ROOT_NAME)
const char* const
Parameter_Tree_Model::DEFAULT_ROOT_NAME = _DEFAULT_ROOT_NAME_;
/*==============================================================================
Constructors
*/
Parameter_Tree_Model::Parameter_Tree_Model
(
Aggregate* root,
QObject* parent
)
: QAbstractItemModel (parent),
Root (root),
Root_Visible (false),
Root_Name (tr (DEFAULT_ROOT_NAME))
{
#if (DEBUG_SECTION & DEBUG_CONSTRUCTORS)
clog << "<-> Parameter_Tree_Model::Parameter_Tree_Model @ " << (void*)this
<< ": root @ " << (void*)root << endl;
#endif
#if (DEBUG_SECTION != 0)
clog << boolalpha;
QHash<int, QByteArray>
role_names;
role_names.insert (0, "Display");
role_names.insert (1, "Decoration");
role_names.insert (2, "Edit");
role_names.insert (3, "ToolTip");
role_names.insert (4, "StatusTip");
role_names.insert (5, "WhatsThis");
role_names.insert (6, "Font");
role_names.insert (7, "TextAlignment");
role_names.insert (8, "Background");
role_names.insert (9, "Foreground");
role_names.insert (10, "CheckState");
role_names.insert (11, "AccessibleText");
role_names.insert (12, "AccesibleDescription");
role_names.insert (13, "SizeHint");
//TODO QT4 OBSOLETE setRoleNames (role_names);
#endif
}
Parameter_Tree_Model::Parameter_Tree_Model
(
const Parameter_Tree_Model& model
)
: QAbstractItemModel (),
Root (model.Root),
Root_Visible (model.Root_Visible),
Root_Name (model.Root_Name)
{
#if (DEBUG_SECTION & DEBUG_CONSTRUCTORS)
clog << "<-> Parameter_Tree_Model::Parameter_Tree_Model: copy @ "
<< (void*)(&model) << " with root @ " << (void*)Root << endl;
#endif
#if (DEBUG_SECTION != 0)
clog << boolalpha;
QHash<int, QByteArray>
role_names;
role_names.insert (0, "Display");
role_names.insert (1, "Decoration");
role_names.insert (2, "Edit");
role_names.insert (3, "ToolTip");
role_names.insert (4, "StatusTip");
role_names.insert (5, "WhatsThis");
role_names.insert (6, "Font");
role_names.insert (7, "TextAlignment");
role_names.insert (8, "Background");
role_names.insert (9, "Foreground");
role_names.insert (10, "CheckState");
role_names.insert (11, "AccessibleText");
role_names.insert (12, "AccesibleDescription");
role_names.insert (13, "SizeHint");
//TODO QT4 OBSOLETE setRoleNames (role_names);
#endif
}
Parameter_Tree_Model&
Parameter_Tree_Model::operator= (const Parameter_Tree_Model& model)
{
if (this != &model)
{
Root = model.Root;
Root_Visible = model.Root_Visible;
Root_Name = model.Root_Name;
}
return *this;
}
/*==============================================================================
QAbstractItemModel interface implementation
*/
QModelIndex
Parameter_Tree_Model::index
(
int row,
int column,
const QModelIndex& parent_index
) const
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << ">>> Parameter_Tree_Model::index: row " << row << " col " << column
<< endl
<< " parent " << parent_index << endl
<< " is valid = " << parent_index.isValid () << endl;
#endif
QModelIndex
index;
if (column >= 0 &&
column <= 1 &&
parent_index.column () != 1) // Value column has no children.
{
Aggregate*
parent = NULL;
if (parent_index.isValid ())
// Non-root parent.
parent = static_cast<Aggregate*>(parent_index.internalPointer ());
else
{
// Root parent.
if (Root_Visible &&
row == 0)
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << " createIndex (" << row << ", " << column << ", "
<< (void*)(Root) << ')' << endl
<< " at Root";
#endif
index = createIndex (row, column, Root);
}
else
parent = Root;
}
if (parent &&
row < (int)parent->size ())
{
// Set the internal pointer to the Parameter for the row.
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << " createIndex (" << row << ", " << column << ", "
<< (void*)(&(*parent)[row]) << ')' << endl
<< " parameter " << parent->name () << '/'
<< parent->at(row).name () << endl;
#endif
index = createIndex (row, column, &(*parent)[row]);
}
}
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << "<<< Parameter_Tree_Model::index: " << index << endl;
#endif
return index;
}
QVariant
Parameter_Tree_Model::data
(
const QModelIndex& index,
int role
) const
{
#if (DEBUG_SECTION & DEBUG_DATA)
clog << ">>> Parameter_Tree_Model::data:" << endl
<< " " << index << endl
<< " role = " << role
<< " (" << qPrintable (QString (roleNames ().value (role)))
<< ')' << endl;
#endif
QVariant
datum;
if (role == Qt::TextAlignmentRole)
{
#if (DEBUG_SECTION & DEBUG_DATA)
clog << " TextAlignmentRole" << endl;
#endif
datum = static_cast<int>(Qt::AlignTop | Qt::AlignLeft);
}
else
if (role == Qt::DisplayRole &&
index.isValid ())
{
Parameter*
parameter = static_cast<Parameter*>(index.internalPointer ());
if (index.column () == 0)
{
// Name column.
if (Root_Visible &&
Root == parameter)
{
#if (DEBUG_SECTION & DEBUG_DATA)
clog << " Root_Name " << qPrintable (Root_Name) << endl;
#endif
datum = Root_Name;
}
else
{
#if (DEBUG_SECTION & DEBUG_DATA)
clog << " parameter name " << parameter->name () << endl;
#endif
datum = QString::fromStdString (parameter->name ());
}
}
else
if (index.column () == 1)
{
// Value column.
if (parameter->is_Aggregate ())
{
#if (DEBUG_SECTION & DEBUG_DATA)
clog << " " << parameter->type_name () << " Aggregate "
<< parameter->name () << endl;
#endif
datum = tr (parameter->type_name ().c_str ());
}
else
{
Value*
value = ¶meter->value ();
if (value->is_Array ())
{
#if (DEBUG_SECTION & DEBUG_DATA)
clog << " " << value->type_name () << " Array" << endl;
#endif
datum = tr (value->type_name ().c_str ());
}
else
{
#if (DEBUG_SECTION & DEBUG_DATA)
clog << " Value = " << *value << endl;
#endif
datum = QString::fromStdString (static_cast<string>(*value));
}
}
}
}
#if (DEBUG_SECTION & DEBUG_DATA)
clog << "<<< Parameter_Tree_Model::data: "
<< qPrintable (datum.toString ()) << endl;
#endif
return datum;
}
QModelIndex
Parameter_Tree_Model::parent
(
const QModelIndex& index
) const
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << ">>> Parameter_Tree_Model::parent: " << index << endl
<< " is valid = " << index.isValid () << endl;
#endif
QModelIndex
parent_index;
if (index.isValid ())
{
Parameter*
parameter = static_cast<Parameter*>(index.internalPointer ());
if (parameter)
parameter = parameter->parent ();
if (parameter &&
(parameter != Root ||
Root_Visible))
parent_index = createIndex (parameter_row (parameter), 0, parameter);
}
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << "<<< Parameter_Tree_Model::parent: " << parent_index << endl;
#endif
return parent_index;
}
int
Parameter_Tree_Model::rowCount
(
const QModelIndex& index
) const
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << ">>> Parameter_Tree_Model::rowCount: " << index << endl
<< " is valid = " << index.isValid () << endl;
#endif
int
count = 0;
if (index.isValid ())
{
if (index.column () == 0)
{
Parameter*
parameter = static_cast<Parameter*>(index.internalPointer ());
if (parameter)
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << " " << parameter->type_name () << " parameter "
<< parameter->name () << endl;
#endif
if (parameter->is_Aggregate ())
count = static_cast<Aggregate*>(parameter)->size ();
}
}
}
else
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << " tree root" << endl;
#endif
if (Root_Visible)
count = 1;
else
if (Root)
count = Root->size ();
}
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << "<<< Parameter_Tree_Model::rowCount: " << count << endl;
#endif
return count;
}
QVariant
Parameter_Tree_Model::headerData
(
int section,
Qt::Orientation orientation,
int role
) const
{
QVariant
data;
if (orientation == Qt::Horizontal &&
role == Qt::DisplayRole &&
section >= 0 &&
section <= 1)
data = ((section == 0) ? tr ("Name") : tr ("Value"));
return data;
}
/*==============================================================================
Accessors
*/
void
Parameter_Tree_Model::root_visible
(
bool enable
)
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << ">>> Parameter_Tree_Model::root_visible: " << enable << endl;
#endif
if (Root_Visible != enable)
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << " visibility changed" << endl;
#endif
// >>> SIGNAL <<<
emit layoutAboutToBeChanged ();
Root_Visible = enable;
// >>> SIGNAL <<<
emit layoutChanged ();
}
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << "<<< Parameter_Tree_Model::root_visible" << endl;
#endif
}
void
Parameter_Tree_Model::root_name
(
const QString& name
)
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << ">>> Parameter_Tree_Model::root_name: " << name.toStdString() << endl;
#endif
if (Root_Name != name)
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << " name changed from " << Root_Name.toStdString() << endl;
#endif
Root_Name = name;
if (Root_Visible)
{
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << " root is visible" << endl;
#endif
// >>> SIGNAL <<< Nudge the viewer to redisplay the model.
emit dataChanged
(index (0, 0, QModelIndex ()), index (0, 0, QModelIndex ()));
}
}
#if (DEBUG_SECTION & DEBUG_MODEL)
clog << "<<< Parameter_Tree_Model::root_name" << endl;
#endif
}
QString
Parameter_Tree_Model::comments
(
const QModelIndex& index
) const
{
QString
comments;
Parameter*
parameter = static_cast<Parameter*>(index.internalPointer ());
if (parameter)
comments = parameter->comment ().c_str ();
return comments;
}
/*==============================================================================
Helpers
*/
int
Parameter_Tree_Model::parameter_row
(
Parameter* parameter
) const
{
int
row = -1; // Not found.
if (parameter)
{
Aggregate*
parent = parameter->parent ();
if (parent)
{
row = parent->size ();
while (row--)
if (&(*parent)[row] == parameter)
break;
}
else
if (Root_Visible)
row = 0;
}
return row;
}
/*==============================================================================
Utility
*/
std::ostream&
operator<<
(
std::ostream& stream,
const QModelIndex& index
)
{
stream
<< "QModelIndex = row "
<< index.row () << " col " << index.column () << " object @ "
<< (void*)index.internalPointer ();
return stream;
}
} // end namespace HiRISE
} // end namespace UA