-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataControlPluginScripting.cc
701 lines (515 loc) · 22.7 KB
/
DataControlPluginScripting.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper 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 LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#include "DataControlPlugin.hh"
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
//******************************************************************************
void DataControlPlugin::setDescriptions(){
emit setSlotDescription("getSourceObjects(DataType)",tr("Returns the IdList of all source objects with given DataType."),
QStringList(tr("Datatype")), QStringList(tr("Datatype of the objects")));
emit setSlotDescription("getTargetObjects(DataType)",tr("Returns the IdList of all target objects with given DataType."),
QStringList(tr("Datatype")), QStringList(tr("Datatype of the objects")));
emit setSlotDescription("getObject(QString)",tr("Returns the id of an object with given name."),
QStringList(tr("Name")), QStringList(tr("Name of an object")));
emit setSlotDescription("getObjectName(int)",tr("Returns the name of an object with given id."),
QStringList(tr("objectId")), QStringList(tr("ID of an object")));
emit setSlotDescription("dataType(int)",tr("Returns the DataType of the object with the given id."),
QStringList(tr("objectId")), QStringList(tr("ID of an object")));
emit setSlotDescription("hideObject(int)",tr("Hide object with the given id."),
QStringList(tr("objectId")), QStringList(tr("ID of an object")));
emit setSlotDescription("showObject(int)",tr("Show object with the given id."),
QStringList(tr("objectId")), QStringList(tr("ID of an object")));
emit setSlotDescription("setTarget(int,bool)",tr("Set given object as target."),
QString(tr("ObjectId,Target")).split(","),
QString(tr("id of the object, set object as target?")).split(","));
emit setSlotDescription("setSource(int,bool)",tr("Set given object as source."),
QString(tr("ObjectId,Source")).split(","),
QString(tr("id of the object, set object as source?")).split(","));
emit setSlotDescription("setObjectName(int,QString)",tr("Set name of given object."),
QString(tr("ObjectId,name")).split(","),
QString(tr("id of the object, the new name")).split(","));
emit setSlotDescription("addEmptyGroup(QString)", tr("Create new empty group."),
QStringList(tr("GroupName")),
QStringList(tr("Name of the new group.")));
emit setSlotDescription("addEmptyGroup(QString,int)", tr("Create new empty group."),
QString(tr("GroupName,Parent")).split(","),
QString(tr("Name of the new group., Parent of the new group, or -1, if there is no parent.")).split(","));
emit setSlotDescription("groupObjects(IdList,QString)",tr("Group given Objects together."),
QString(tr("objectIds,groupName")).split(","),
QString(tr("List of objects that should be grouped., Name of the group.")).split(","));
emit setSlotDescription("groupObjects(IdList)",tr("Group given Objects together."),
QStringList(tr("objectIds")), QStringList(tr("List of objects that should be grouped.")));
emit setSlotDescription("groupCount()",tr("Returns the number of group objects."),
QStringList(tr("")), QStringList(tr("")));
emit setSlotDescription("unGroupObject(int)",tr("Remove the given object from its group and append to root node."),
QStringList(tr("ObjectId")), QStringList(tr("Object to be removed from group.")));
emit setSlotDescription("objectDelete(int)",tr("Delete an object"),
QStringList(tr("objectId")), QStringList(tr("Delete the given object.")));
emit setSlotDescription("getGroupElements(int)",tr("Get elements of a group"),
QStringList(tr("groupId")), QStringList(tr("Id of the group.")));
emit setSlotDescription("copyObject(int)",tr("Create a copy of an object"),
QStringList(tr("objectId")), QStringList(tr("Object to copy.")));
emit setSlotDescription("setAllTarget()",tr("Set All objects as targets"),
QStringList(), QStringList());
emit setSlotDescription("setAllSource()",tr("Set All objects as source"),
QStringList(), QStringList());
emit setSlotDescription("clearAllTarget()",tr("Clear targets"),
QStringList(), QStringList());
emit setSlotDescription("clearAllSource()",tr("Clear sources"),
QStringList(), QStringList());
emit setSlotDescription("showAll()",tr("Show all objects"),
QStringList(), QStringList());
emit setSlotDescription("hideAll()",tr("Hide all objects"),
QStringList(), QStringList());
emit setSlotDescription("availableDataTypeNames()",tr("Returns a QStringList of all available DataType names."),
QStringList(tr("")), QStringList(tr("")));
emit setSlotDescription("printObjectInfoToLog()",tr("Print info about all objects to log"),
QStringList(), QStringList());
emit setSlotDescription("addObjectToGroup(int,int)",tr("Add an Object to an existing group"),
QStringList(tr("objectId,groupId").split(",")),
QStringList(tr("ID of an object.,ID of an group where the object has to be added.").split(",")));
}
//******************************************************************************
/** \brief Returns the id of an object with given name
*
* @param _name name of an object
* @return the id
*/
int DataControlPlugin::getObject( QString _name ) {
BaseObject* object = PluginFunctions::objectRoot()->childExists(_name);
if ( object == 0)
return -1;
return object->id();
}
//******************************************************************************
/** \brief Generate a copy of an object
*
* @param objectId id of an object
* @return id of the generated object
*/
int DataControlPlugin::copyObject( int objectId ) {
int newObject;
emit copyObject( objectId, newObject );
return newObject;
}
//******************************************************************************
/** \brief Returns the name of an object with given id
*
* @param objectId id of an object
* @return name of the given object
*/
QString DataControlPlugin::getObjectName( int objectId ) {
BaseObjectData* object;
if ( ! PluginFunctions::getObject(objectId,object) ) {
emit log(LOGERR,tr("getObjectName : unable to get object") );
return QString(tr("Unknown Object"));
} else
return object->name() ;
}
//******************************************************************************
/// Get the DataType of a given object
DataType DataControlPlugin::dataType( int _objectId ) {
BaseObject* object = 0;
PluginFunctions::getObject(_objectId,object);
if ( object )
return object->dataType();
else
return DATA_UNKNOWN;
}
//******************************************************************************
/** \brief Hide object with the given id
*
* @param objectId id of an object
*/
void DataControlPlugin::hideObject( int objectId ) {
if ( ! OpenFlipper::Options::gui())
return;
BaseObjectData* object;
if ( PluginFunctions::getObject(objectId,object) ){
object->hide();
}
}
//******************************************************************************
/** \brief set the given Object as target
*
* @param objectId id of an object
* @param _target set object as target?
*/
void DataControlPlugin::setTarget( int objectId, bool _target ) {
BaseObjectData* object;
if ( PluginFunctions::getObject(objectId,object) ){
object->target( _target );
}
}
//******************************************************************************
/** \brief set the given Object as source
*
* @param objectId id of an object
* @param _source set object as source?
*/
void DataControlPlugin::setSource( int objectId, bool _source ) {
BaseObjectData* object;
if ( PluginFunctions::getObject(objectId,object) ){
object->source( _source );
}
}
//******************************************************************************
/** \brief set the name of the given Object
*
* @param objectId id of an object
* @param _name new name
*/
void DataControlPlugin::setObjectName( int objectId, QString _name ) {
BaseObjectData* object;
if ( PluginFunctions::getObject(objectId,object) ){
object->setName( _name );
}
}
//******************************************************************************
/** \brief Delete an object
*
* @param objectId id of the object that should be deleted
*/
void DataControlPlugin::objectDelete( int objectId ) {
BaseObjectData* object;
if ( ! PluginFunctions::getObject(objectId,object) )
return;
if ( object == 0)
return;
emit deleteObject(objectId);
}
//******************************************************************************
/** \brief Show object with the given id
*
* @param objectId id of an object
*/
void DataControlPlugin::showObject( int objectId ) {
if ( ! OpenFlipper::Options::gui())
return;
BaseObjectData* object;
if ( PluginFunctions::getObject(objectId,object) ){
object->show();
}
}
//******************************************************************************
/** \brief Create new empty group
*
* @param _groupName name of the new group
* @param _parentGroupId id of the parent. -1, if the new group will have no parent
*/
int DataControlPlugin::addEmptyGroup(QString _groupName, int _parentGroupId) {
GroupObject* parentGroupObject = dynamic_cast<GroupObject*>(PluginFunctions::objectRoot());
BaseObject* parentObject = 0;
if (PluginFunctions::getObject(_parentGroupId, parentObject)) {
GroupObject* parent = dynamic_cast<GroupObject*>(parentObject);
if (parent) {
parentGroupObject = parent;
} else {
emit log(LOGWARN, tr("Parent object %1 is not a group, creating a new toplevel group").arg(_parentGroupId));
}
} else if (_parentGroupId != 0 ) {
emit log(LOGWARN, tr("Cannot get parent object %1, creating a new toplevel group").arg(_parentGroupId));
}
GroupObject* groupObject = new GroupObject("newGroup", parentGroupObject);
if (_groupName == "") {
groupObject->setName(tr("New group ") + QString::number(groupObject->id()));
} else {
groupObject->setName(_groupName);
}
emit emptyObjectAdded(groupObject->id());
return groupObject->id();
}
//******************************************************************************
/** \brief Group given Objects together
*
* @param _objectIDs list of object ids
* @param _groupName the name of the new group
*/
int DataControlPlugin::groupObjects(IdList _objectIDs, QString _groupName) {
QVector< BaseObject* > objs;
// Try to get all objects given in list
for (uint i=0; i < _objectIDs.size(); i++){
BaseObject* obj;
if ( PluginFunctions::getObject(_objectIDs[i],obj) )
objs.push_back(obj);
}
// If all of them fail, stop here.
if (objs.size() == 0){
emit log(tr("No objects to group."));
return -1;
}
// Check if all objects have the same parent
// Abort if the parents differ
BaseObject* parent = (objs[0])->parent();
for ( int i = 1 ; i < objs.size() ; ++i){
if ( parent != (objs[i])->parent() ){
emit log(tr("Cannot group Objects with different parents"));
return -1;
}
}
//create new group
int groupId = addEmptyGroup(_groupName);
BaseObject *groupItem = 0;
PluginFunctions::getObject(groupId, groupItem);
if (parent) {
groupItem->setParent(parent);
}
//append new children to group
for ( int i = 0 ; i < objs.size() ; ++i) {
(objs[i])->setParent(groupItem);
}
return groupId;
}
//******************************************************************************
bool DataControlPlugin::unGroupObject(int _id) {
BaseObject* group = 0;
PluginFunctions::getObject(_id,group);
if ( group && group->isGroup())
{
//iterate over children
for (int i=group->childCount()-1; i >= 0; --i){
BaseObject* child = group->child(i);
// then change the parent
child->setParent(group->parent());
}
//delete the group
emit deleteObject( group->id() );
return true;
} else {
if (group)
emit log( LOGERR, tr("Cannot Ungroup. Object with id %1 is not a group").arg(_id));
else
emit log( LOGERR, tr( "Unable to get Object with id %1 for ungrouping").arg(_id) );
return false;
}
}
//******************************************************************************
/** \brief Set Target Selection for all objects
*
*/
void DataControlPlugin::setAllTarget() {
DataType type = DATA_ALL;
//try to find dataType restriction if called by contextMenu
QAction* action = dynamic_cast< QAction* > ( sender() );
if ( action != 0 && action->data().isValid() )
type = (DataType) action->data().toUInt();
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, type) ;
o_it != PluginFunctions::objectsEnd(); ++o_it){
o_it->target(true);
}
}
//******************************************************************************
/** \brief Set Source Selection for all objects
*
*/
void DataControlPlugin::setAllSource() {
DataType type = DATA_ALL;
//try to find dataType restriction if called by contextMenu
QAction* action = dynamic_cast< QAction* > ( sender() );
if ( action != 0 && action->data().isValid() )
type = (DataType) action->data().toUInt();
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, type) ;
o_it != PluginFunctions::objectsEnd(); ++o_it){
o_it->source(true);
}
}
//******************************************************************************
/** \brief Clear Target Selection for all objects
*
*/
void DataControlPlugin::clearAllTarget() {
DataType type = DATA_ALL;
//try to find dataType restriction if called by contextMenu
QAction* action = dynamic_cast< QAction* > ( sender() );
if ( action != 0 && action->data().isValid() )
type = (DataType) action->data().toUInt();
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, type) ;
o_it != PluginFunctions::objectsEnd(); ++o_it){
o_it->target(false);
}
}
//******************************************************************************
/** \brief Clear Source Selection for all objects
*
*/
void DataControlPlugin::clearAllSource() {
DataType type = DATA_ALL;
//try to find dataType restriction if called by contextMenu
QAction* action = dynamic_cast< QAction* > ( sender() );
if ( action != 0 && action->data().isValid() )
type = (DataType) action->data().toUInt();
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, type) ;
o_it != PluginFunctions::objectsEnd(); ++o_it){
o_it->source(false);
}
}
//******************************************************************************
/** \brief Hide all objects
*
*/
void DataControlPlugin::hideAll() {
if ( ! OpenFlipper::Options::gui())
return;
DataType type = DATA_ALL;
//try to find dataType restriction if called by contextMenu
QAction* action = dynamic_cast< QAction* > ( sender() );
if ( action != 0 && action->data().isValid() )
type = (DataType) action->data().toUInt();
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS,type) ;
o_it != PluginFunctions::objectsEnd(); ++o_it){
o_it->hide();
}
}
//******************************************************************************
/** \brief Show all objects
*
*/
void DataControlPlugin::showAll() {
if ( ! OpenFlipper::Options::gui())
return;
DataType type = DATA_ALL;
//try to find dataType restriction if called by contextMenu
QAction* action = dynamic_cast< QAction* > ( sender() );
if ( action != 0 && action->data().isValid() )
type = (DataType) action->data().toUInt();
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, type); o_it != PluginFunctions::objectsEnd(); ++o_it){
o_it->show();
}
}
//******************************************************************************
/** \brief Get all target objects of given type
*
*/
IdList DataControlPlugin::getTargetObjects(DataType _type) {
IdList list;
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, _type); o_it != PluginFunctions::objectsEnd(); ++o_it)
list.push_back( o_it->id() );
return list;
}
//******************************************************************************
/** \brief Get all source objects of given type
*
*/
IdList DataControlPlugin::getSourceObjects(DataType _type) {
IdList list;
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::SOURCE_OBJECTS, _type); o_it != PluginFunctions::objectsEnd(); ++o_it)
list.push_back( o_it->id() );
return list;
}
//******************************************************************************
/** \brief Prints information about all open objects to the Log
*
*/
void DataControlPlugin::printObjectInfoToLog() {
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS); o_it != PluginFunctions::objectsEnd(); ++o_it)
emit log(LOGINFO, tr("Object \"%1\" with ID %2 of type %3 ").arg(o_it->name()).arg(o_it->id()).arg(o_it->dataType().name()));
}
//******************************************************************************
/** \brief get number of groups
*
* Returns the number of groups that are in the scene
*
* @return Number of groups
*/
unsigned int DataControlPlugin::groupCount() const {
unsigned int count = 0;
for ( PluginFunctions::BaseObjectIterator o_it(PluginFunctions::ALL_OBJECTS,DATA_GROUP); o_it != PluginFunctions::objectsEnd(); ++o_it)
++count;
return count;
}
//******************************************************************************
/** \brief Return available dataType
*
* Returns a space separated list of all available DataTypes
*
* @return List of available data types
*/
QStringList DataControlPlugin::availableDataTypeNames() const {
QStringList list;
for ( std::vector< TypeInfo >::const_iterator dataType = typesBegin(); dataType != typesEnd(); ++dataType )
list.push_back(dataType->name);
return list;
}
//******************************************************************************
/** \brief add an object to an existing group
*
* add the object with _objectId to the group with _groupId
*
* @return success
*/
bool DataControlPlugin::addObjectToGroup(int _objectId, int _groupId)
{
BaseObject* obj = 0;
BaseObject* group = 0;
PluginFunctions::getObject(_objectId,obj);
PluginFunctions::getObject(_groupId,group);
if ( obj && group)
{
if (group->isGroup())
{
obj->setParent(group);
return true;
}
else
emit log( LOGERR, tr(" Object with id %1 is not a group").arg(_groupId));
} else {
if (!obj)
emit log( LOGERR, tr( "Unable to get Object with id %1").arg(_objectId) );
if (!_groupId)
emit log( LOGERR, tr( "Unable to get Group with id %1").arg(_groupId) );
}
return false;
}
//******************************************************************************
/** \brief get all objects of an given group
*
* get all objects of an given group with id _groupId
*
* @return id list of group elements
*/
IdList DataControlPlugin::getGroupElements(int _groupId)
{
BaseObject* group = 0;
PluginFunctions::getObject(_groupId,group);
IdList result;
for (int i = 0; i < group->childCount(); ++i)
result.push_back(group->child(i)->id());
return result;
}