-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParameter_Tree_View.cc
478 lines (413 loc) · 11.5 KB
/
Parameter_Tree_View.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
/* Parameter_Tree_View
HiROC CVS ID: $Id: Parameter_Tree_View.cc,v 1.8 2012/08/31 06:54:34 castalia 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_View.hh"
#include "Parameter_Tree_Model.hh"
#include "Value_Tree_Model.hh"
#include "PVL.hh"
using idaeim::PVL::Parameter;
using idaeim::PVL::Aggregate;
using idaeim::PVL::Array;
#include <QTreeView>
#include <QList>
#include <string>
using std::string;
#include <sstream>
using std::ostringstream;
#include <iomanip>
using std::endl;
#include <stdexcept>
using std::invalid_argument;
#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_PARAMETERS (1 << 2)
#define DEBUG_ARRAY_SUBTREES (1 << 3)
#if (DEBUG_SECTION + 0) == 0
#undef DEBUG_SECTION
#define DEBUG_SECTION DEBUG_OFF
#endif
#include <iostream>
#include <iomanip>
using std::clog;
using std::boolalpha;
#endif // DEBUG_SECTION
namespace UA
{
namespace HiRISE
{
/*==============================================================================
Constants
*/
const char* const
Parameter_Tree_View::ID = "UA::HiRISE::Parameter_Tree_View ($Revision: 1.8 $ $Date: 2012/08/31 06:54:34 $)";
#ifndef PARAMETER_VIEW_DEFAULT_ANIMATION
#define PARAMETER_VIEW_DEFAULT_ANIMATION true
#endif
const bool
Parameter_Tree_View::DEFAULT_ANIMATION = PARAMETER_VIEW_DEFAULT_ANIMATION;
/*==============================================================================
Constructors
*/
Parameter_Tree_View::Parameter_Tree_View
(
Aggregate* parameters,
QWidget* parent
)
: QTreeView (parent),
Tree_Model (NULL),
Tree_Model_Locally_Owned (false)
{
#if (DEBUG_SECTION & DEBUG_CONSTRUCTORS)
clog << ">>> Parameter_Tree_View" << endl;
if (parameters)
clog << " parameters -" << endl
<< *parameters << endl;
else
clog << " no parameters" << endl;
#endif
this->parameters (parameters);
#if (DEBUG_SECTION & DEBUG_CONSTRUCTORS)
clog << "<<< Parameter_Tree_View" << endl;
#endif
}
Parameter_Tree_View::Parameter_Tree_View
(
Parameter_Tree_Model* model,
QWidget* parent
)
: QTreeView (parent),
Tree_Model (model),
Tree_Model_Locally_Owned (false)
{
#if (DEBUG_SECTION & DEBUG_CONSTRUCTORS)
clog << ">>> Parameter_Tree_View" << endl
<< " model parameters -" << endl
<< *(model->parameters ()) << endl;
#endif
QTreeView::setModel (model);
initialize ();
#if (DEBUG_SECTION & DEBUG_CONSTRUCTORS)
clog << "<<< Parameter_Tree_View" << endl;
#endif
}
Parameter_Tree_View::~Parameter_Tree_View ()
{
if (Tree_Model_Locally_Owned &&
Tree_Model)
delete Tree_Model;
}
void
Parameter_Tree_View::initialize ()
{
QTreeView::setAnimated (DEFAULT_ANIMATION);
setUniformRowHeights (! subtrees ()); // Optimization.
setSortingEnabled (false);
}
/*==============================================================================
Accessors
*/
void
Parameter_Tree_View::parameters
(
Aggregate* params
)
{
#if (DEBUG_SECTION & DEBUG_PARAMETERS)
clog << ">>> Parameter_Tree_View::parameters" << endl;
if (params)
clog << " params -" << endl
<< *params;
else
clog << " no params" << endl;
clog << " constructing new Parameter_Tree_Model ..." << endl;
#endif
Tree_Model_Locally_Owned = true;
Tree_Model = new Parameter_Tree_Model (params);
QItemSelectionModel
*selection_model = selectionModel ();
#if (DEBUG_SECTION & DEBUG_PARAMETERS)
clog << " QItemSelectionModel @ " << (void*)selection_model << endl
<< " QTreeView::setModel @ " << (void*)Tree_Model << endl;
#endif
QTreeView::setModel (Tree_Model);
#if (DEBUG_SECTION & DEBUG_PARAMETERS)
clog << " delete QItemSelectionModel" << endl;
#endif
delete selection_model;
initialize ();
#if (DEBUG_SECTION & DEBUG_PARAMETERS)
clog << "<<< Parameter_Tree_View::parameters" << endl;
#endif
}
Aggregate*
Parameter_Tree_View::parameters () const
{return static_cast<Parameter_Tree_Model*>(model ())->parameters ();}
void
Parameter_Tree_View::setModel
(
QAbstractItemModel* model
)
{
Parameter_Tree_Model*
parameter_model = dynamic_cast<Parameter_Tree_Model*>(model);
if (parameter_model)
{
Tree_Model_Locally_Owned = false;
QTreeView::setModel (parameter_model);
initialize ();
}
else
{
ostringstream
message;
message
<< ID << endl
<< "Invalid data model for a Parameter_Tree_View;" << endl
<< "a Parameter_Tree_Model is required.";
throw invalid_argument (message.str ());
}
}
bool
Parameter_Tree_View::root_visible () const
{return static_cast<Parameter_Tree_Model*>(model ())->root_visible ();}
void
Parameter_Tree_View::root_visible
(
bool enable
)
{static_cast<Parameter_Tree_Model*>(model ())->root_visible (enable);}
QString
Parameter_Tree_View::root_name () const
{return static_cast<Parameter_Tree_Model*>(model ())->root_name ();}
void
Parameter_Tree_View::root_name
(
const QString& name
)
{static_cast<Parameter_Tree_Model*>(model ())->root_name (name);}
QString
Parameter_Tree_View::default_root_name ()
{return Parameter_Tree_Model::default_root_name ();}
QString
Parameter_Tree_View::comments
(
const QModelIndex& index
) const
{return static_cast<Parameter_Tree_Model*>(model ())->comments (index);}
Parameter_Tree_View::Parameter_List
Parameter_Tree_View::selected_parameters () const
{
Parameter_List
parameter_list;
QModelIndexList
model_list (selectionModel ()->selectedRows ());
for (int
index = 0;
index < model_list.size ();
++index)
parameter_list
<< static_cast <Parameter*>(model_list[index].internalPointer ());
return parameter_list;
}
void
Parameter_Tree_View::setAnimated
(
bool enable
)
{
if (enable != isAnimated ())
{
QTreeView::setAnimated (enable);
Parameter_Tree_Model*
parameter_model = static_cast<Parameter_Tree_Model*>(model ());
QTreeView*
subtree;
QModelIndex
index,
parent;
int
row = 0,
row_count = 1;
// Walk through the tree heirarchy looking for subtrees.
while (true)
{
if (row >= row_count)
{
if (parent.isValid ())
{
// Ascend to the row following the parent.
row = parent.row () + 1;
row_count = parameter_model->rowCount (parent);
parent = parameter_model->parent (parent); // Grandparent.
continue;
}
else
// Returned to the root.
break;
}
index = parameter_model->index (row, 0, parent);
if (parameter_model->hasChildren (index))
{
// Descend into the node that has children.
row = 0;
row_count = parameter_model->rowCount (index);
parent = index; // Where to come back to after scanning children.
continue;
}
index = parameter_model->index (row, 1, parent);
subtree = dynamic_cast<QTreeView*>(indexWidget (index));
if (subtree)
subtree->setAnimated (enable);
++row;
}
}
}
/*==============================================================================
Helpers
*/
bool
Parameter_Tree_View::subtrees ()
{
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << ">>> Parameter_Tree_View::subtrees" << endl;
string
name ("ROOT");
#endif
Parameter_Tree_Model*
parameter_model = static_cast<Parameter_Tree_Model*>(model ());
QTreeView*
subtree = NULL;
Value_Tree_Model*
array_model;
QModelIndex
index,
parent;
Parameter*
parameter;
int
row = 0,
row_count = 1,
// Height of Array subtree.
subtree_height = (fontMetrics ().height () + 1) * 5;
// Walk through the tree heirarchy looking for Parameters with Arrays.
while (true)
{
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << "--> row " << row << '/' << row_count << " in " << name << endl;
#endif
if (row >= row_count)
{
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << " parent " << parent << endl;
#endif
if (parent.isValid ())
{
// Ascend to the row following the parent.
row = parent.row () + 1;
parent = parameter_model->parent (parent); // Grandparent.
row_count = parameter_model->rowCount (parent);
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << " ascend to grandparent " << parent << endl;
parameter = static_cast<Parameter*>(parent.internalPointer ());
if (parameter)
name = parameter->name ();
else
name = "ROOT";
clog << " " << name << endl;
#endif
continue;
}
else
{
// Returned to the root.
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << " invalid parent; returned to the root" << endl;
#endif
break;
}
}
index = parameter_model->index (row, 0, parent);
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << " " << index << endl;
parameter = static_cast<Parameter*>(index.internalPointer ());
if (parameter)
clog << " parameter " << parameter->name () << endl;
else
clog << " no parameter!";
#endif
if (parameter_model->hasChildren (index))
{
// Descend into the node that has children.
row = 0;
row_count = parameter_model->rowCount (index);
parent = index; // Where to come back to after scanning children.
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << " descend into subgroup" << endl;
name = parameter->name ();
#endif
continue;
}
parameter = static_cast<Parameter*>(index.internalPointer ());
if (parameter &&
parameter->is_Assignment () &&
parameter->value ().is_Array ())
{
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << " Array" << endl;
#endif
subtree = new QTreeView (this); // subtree parent is this view.
subtree->setHeaderHidden (true);
subtree->setMaximumHeight (subtree_height);
subtree->setUniformRowHeights (true); // Optimization.
subtree->setAnimated (isAnimated ());
subtree->setSortingEnabled (false);
array_model = new Value_Tree_Model
(static_cast<Array*>(¶meter->value ()));
// Ownership of the array_model is passed to the subtree.
array_model->setParent (subtree);
subtree->setModel (array_model);
// Set subtree as widget for node.
index = parameter_model->index (row, 1, parent);
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << " subtree @ " << (void*)subtree << endl
<< " array_model @ " << (void*)array_model << endl
<< " setIndexWidget " << index << endl;
#endif
// Ownership of the subtree is passed to the view.
setIndexWidget (index, subtree);
}
++row;
}
#if (DEBUG_SECTION & DEBUG_ARRAY_SUBTREES)
clog << "<<< Parameter_Tree_View::subtrees: " << (subtree != NULL) << endl;
#endif
return subtree;
}
} // end namespace HiRISE
} // end namespace UA