Developer Documentation
DataControlPluginScripting.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 
46 
47 #include "DataControlPlugin.hh"
48 
49 
50 //******************************************************************************
51 
53 
54  emit setSlotDescription("getSourceObjects(DataType)",tr("Returns the IdList of all source objects with given DataType."),
55  QStringList(tr("Datatype")), QStringList(tr("Datatype of the objects")));
56 
57  emit setSlotDescription("getTargetObjects(DataType)",tr("Returns the IdList of all target objects with given DataType."),
58  QStringList(tr("Datatype")), QStringList(tr("Datatype of the objects")));
59 
60  emit setSlotDescription("getObject(QString)",tr("Returns the id of an object with given name."),
61  QStringList(tr("Name")), QStringList(tr("Name of an object")));
62 
63  emit setSlotDescription("getObjectName(int)",tr("Returns the name of an object with given id."),
64  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
65 
66  emit setSlotDescription("dataType(int)",tr("Returns the DataType of the object with the given id."),
67  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
68 
69  emit setSlotDescription("hideObject(int)",tr("Hide object with the given id."),
70  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
71 
72  emit setSlotDescription("showObject(int)",tr("Show object with the given id."),
73  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
74 
75  emit setSlotDescription("setTarget(int,bool)",tr("Set given object as target."),
76  QString(tr("ObjectId,Target")).split(","),
77  QString(tr("id of the object, set object as target?")).split(","));
78 
79  emit setSlotDescription("setSource(int,bool)",tr("Set given object as source."),
80  QString(tr("ObjectId,Source")).split(","),
81  QString(tr("id of the object, set object as source?")).split(","));
82 
83  emit setSlotDescription("setObjectName(int,QString)",tr("Set name of given object."),
84  QString(tr("ObjectId,name")).split(","),
85  QString(tr("id of the object, the new name")).split(","));
86 
87  emit setSlotDescription("addEmptyGroup(QString)", tr("Create new empty group."),
88  QStringList(tr("GroupName")),
89  QStringList(tr("Name of the new group.")));
90 
91  emit setSlotDescription("addEmptyGroup(QString,int)", tr("Create new empty group."),
92  QString(tr("GroupName,Parent")).split(","),
93  QString(tr("Name of the new group., Parent of the new group, or -1, if there is no parent.")).split(","));
94 
95  emit setSlotDescription("groupObjects(IdList,QString)",tr("Group given Objects together."),
96  QString(tr("objectIds,groupName")).split(","),
97  QString(tr("List of objects that should be grouped., Name of the group.")).split(","));
98 
99  emit setSlotDescription("groupObjects(IdList)",tr("Group given Objects together."),
100  QStringList(tr("objectIds")), QStringList(tr("List of objects that should be grouped.")));
101 
102  emit setSlotDescription("groupCount()",tr("Returns the number of group objects."),
103  QStringList(tr("")), QStringList(tr("")));
104 
105  emit setSlotDescription("unGroupObject(int)",tr("Remove the given object from its group and append to root node."),
106  QStringList(tr("ObjectId")), QStringList(tr("Object to be removed from group.")));
107 
108  emit setSlotDescription("objectDelete(int)",tr("Delete an object"),
109  QStringList(tr("objectId")), QStringList(tr("Delete the given object.")));
110 
111  emit setSlotDescription("getGroupElements(int)",tr("Get elements of a group"),
112  QStringList(tr("groupId")), QStringList(tr("Id of the group.")));
113 
114  emit setSlotDescription("copyObject(int)",tr("Create a copy of an object"),
115  QStringList(tr("objectId")), QStringList(tr("Object to copy.")));
116 
117  emit setSlotDescription("setAllTarget()",tr("Set All objects as targets"),
118  QStringList(), QStringList());
119 
120  emit setSlotDescription("setAllSource()",tr("Set All objects as source"),
121  QStringList(), QStringList());
122 
123  emit setSlotDescription("clearAllTarget()",tr("Clear targets"),
124  QStringList(), QStringList());
125 
126  emit setSlotDescription("clearAllSource()",tr("Clear sources"),
127  QStringList(), QStringList());
128 
129  emit setSlotDescription("showAll()",tr("Show all objects"),
130  QStringList(), QStringList());
131 
132  emit setSlotDescription("hideAll()",tr("Hide all objects"),
133  QStringList(), QStringList());
134 
135  emit setSlotDescription("availableDataTypeNames()",tr("Returns a QStringList of all available DataType names."),
136  QStringList(tr("")), QStringList(tr("")));
137 
138  emit setSlotDescription("printObjectInfoToLog()",tr("Print info about all objects to log"),
139  QStringList(), QStringList());
140 
141  emit setSlotDescription("addObjectToGroup(int,int)",tr("Add an Object to an existing group"),
142  QStringList(tr("objectId,groupId").split(",")),
143  QStringList(tr("ID of an object.,ID of an group where the object has to be added.").split(",")));
144 
145 }
146 
147 
148 //******************************************************************************
149 
155 int DataControlPlugin::getObject( QString _name ) {
156 
158 
159  if ( object == 0)
160  return -1;
161 
162  return object->id();
163 }
164 
165 
166 //******************************************************************************
167 
173 int DataControlPlugin::copyObject( int objectId ) {
174 
175  int newObject;
176 
177  emit copyObject( objectId, newObject );
178 
179  return newObject;
180 }
181 
182 
183 //******************************************************************************
184 
190 QString DataControlPlugin::getObjectName( int objectId ) {
191 
192  BaseObjectData* object;
193  if ( ! PluginFunctions::getObject(objectId,object) ) {
194  emit log(LOGERR,tr("getObjectName : unable to get object") );
195  return QString(tr("Unknown Object"));
196  } else
197  return object->name() ;
198 
199 }
200 
201 //******************************************************************************
202 
205  BaseObject* object = 0;
206  PluginFunctions::getObject(_objectId,object);
207 
208  if ( object )
209  return object->dataType();
210  else
211  return DATA_UNKNOWN;
212 }
213 
214 
215 //******************************************************************************
216 
221 void DataControlPlugin::hideObject( int objectId ) {
222 
223  if ( ! OpenFlipper::Options::gui())
224  return;
225 
226  BaseObjectData* object;
227  if ( PluginFunctions::getObject(objectId,object) ){
228  object->hide();
229  }
230 
231 }
232 
233 
234 //******************************************************************************
235 
241 void DataControlPlugin::setTarget( int objectId, bool _target ) {
242 
243  BaseObjectData* object;
244  if ( PluginFunctions::getObject(objectId,object) ){
245  object->target( _target );
246  }
247 }
248 
249 
250 //******************************************************************************
251 
257 void DataControlPlugin::setSource( int objectId, bool _source ) {
258 
259  BaseObjectData* object;
260  if ( PluginFunctions::getObject(objectId,object) ){
261  object->source( _source );
262  }
263 
264 }
265 
266 
267 //******************************************************************************
268 
274 void DataControlPlugin::setObjectName( int objectId, QString _name ) {
275 
276  BaseObjectData* object;
277  if ( PluginFunctions::getObject(objectId,object) ){
278  object->setName( _name );
279  }
280 }
281 
282 
283 //******************************************************************************
284 
289 void DataControlPlugin::objectDelete( int objectId ) {
290 
291  BaseObjectData* object;
292  if ( ! PluginFunctions::getObject(objectId,object) )
293  return;
294 
295  if ( object == 0)
296  return;
297 
298  emit deleteObject(objectId);
299 }
300 
301 
302 //******************************************************************************
303 
308 void DataControlPlugin::showObject( int objectId ) {
309 
310  if ( ! OpenFlipper::Options::gui())
311  return;
312 
313  BaseObjectData* object;
314  if ( PluginFunctions::getObject(objectId,object) ){
315  object->show();
316  }
317 
318 }
319 
320 
321 //******************************************************************************
322 
328 int DataControlPlugin::addEmptyGroup(QString _groupName, int _parentGroupId) {
329  GroupObject* parentGroupObject = dynamic_cast<GroupObject*>(PluginFunctions::objectRoot());
330  BaseObject* parentObject = 0;
331  if (PluginFunctions::getObject(_parentGroupId, parentObject)) {
332  GroupObject* parent = dynamic_cast<GroupObject*>(parentObject);
333  if (parent) {
334  parentGroupObject = parent;
335  } else {
336  emit log(LOGWARN, tr("Parent object %1 is not a group, creating a new toplevel group").arg(_parentGroupId));
337  }
338  } else if (_parentGroupId != 0 ) {
339  emit log(LOGWARN, tr("Cannot get parent object %1, creating a new toplevel group").arg(_parentGroupId));
340  }
341 
342  GroupObject* groupObject = new GroupObject("newGroup", parentGroupObject);
343 
344  if (_groupName == "") {
345  groupObject->setName(tr("New group ") + QString::number(groupObject->id()));
346  } else {
347  groupObject->setName(_groupName);
348  }
349 
350  emit emptyObjectAdded(groupObject->id());
351 
352  return groupObject->id();
353 }
354 
355 //******************************************************************************
356 
362 int DataControlPlugin::groupObjects(IdList _objectIDs, QString _groupName) {
363  QVector< BaseObject* > objs;
364 
365  // Try to get all objects given in list
366  for (uint i=0; i < _objectIDs.size(); i++){
367  BaseObject* obj;
368  if ( PluginFunctions::getObject(_objectIDs[i],obj) )
369  objs.push_back(obj);
370  }
371 
372  // If all of them fail, stop here.
373  if (objs.size() == 0){
374  emit log(tr("No objects to group."));
375  return -1;
376  }
377 
378  // Check if all objects have the same parent
379  // Abort if the parents differ
380  BaseObject* parent = (objs[0])->parent();
381  for ( int i = 1 ; i < objs.size() ; ++i){
382  if ( parent != (objs[i])->parent() ){
383  emit log(tr("Cannot group Objects with different parents"));
384  return -1;
385  }
386 
387  }
388 
389  //create new group
390  int groupId = addEmptyGroup(_groupName);
391  BaseObject *groupItem = 0;
392  PluginFunctions::getObject(groupId, groupItem);
393 
394  if (parent) {
395  groupItem->setParent(parent);
396  }
397 
398  IdList ids;
399  //append new children to group
400  for ( int i = 0 ; i < objs.size() ; ++i)
401  {
402  (objs[i])->setParent(groupItem);
403  ids.push_back((objs[i])->id());
404  }
405 
406  emit objectsGrouped(ids);
407  emit updatedObject(groupId,UPDATE_ALL);
408 
409  return groupId;
410 }
411 
412 //******************************************************************************
413 
415  BaseObject* group = 0;
416 
417  PluginFunctions::getObject(_id,group);
418 
419  if ( group && group->isGroup())
420  {
421  //iterate over children
422  for (int i=group->childCount()-1; i >= 0; --i){
423  BaseObject* child = group->child(i);
424 
425  // then change the parent
426  child->setParent(group->parent());
427  }
428  //delete the group
429  emit deleteObject( group->id() );
430 
431  return true;
432  } else {
433  if (group)
434  emit log( LOGERR, tr("Cannot Ungroup. Object with id %1 is not a group").arg(_id));
435  else
436  emit log( LOGERR, tr( "Unable to get Object with id %1 for ungrouping").arg(_id) );
437  return false;
438  }
439 }
440 
441 
442 //******************************************************************************
443 
448 
449  DataType type = DATA_ALL;
450 
451  //try to find dataType restriction if called by contextMenu
452  QAction* action = dynamic_cast< QAction* > ( sender() );
453 
454  if ( action != 0 && action->data().isValid() )
455  type = (DataType) action->data().toUInt();
456 
457  for (auto* o_it : PluginFunctions::objects(PluginFunctions::ALL_OBJECTS, type) ) {
458  o_it->target(true);
459  }
460 }
461 
462 
463 //******************************************************************************
464 
469 
470  DataType type = DATA_ALL;
471 
472  //try to find dataType restriction if called by contextMenu
473  QAction* action = dynamic_cast< QAction* > ( sender() );
474 
475  if ( action != 0 && action->data().isValid() )
476  type = (DataType) action->data().toUInt();
477 
478  for (auto* o_it : PluginFunctions::objects(PluginFunctions::ALL_OBJECTS, type) ) {
479  o_it->source(true);
480  }
481 }
482 
483 
484 //******************************************************************************
485 
490 
491  DataType type = DATA_ALL;
492 
493  //try to find dataType restriction if called by contextMenu
494  QAction* action = dynamic_cast< QAction* > ( sender() );
495 
496  if ( action != 0 && action->data().isValid() )
497  type = (DataType) action->data().toUInt();
498 
499  for (auto* o_it : PluginFunctions::objects(PluginFunctions::ALL_OBJECTS, type) ) {
500  o_it->target(false);
501  }
502 }
503 
504 
505 //******************************************************************************
506 
511 
512  DataType type = DATA_ALL;
513 
514  //try to find dataType restriction if called by contextMenu
515  QAction* action = dynamic_cast< QAction* > ( sender() );
516 
517  if ( action != 0 && action->data().isValid() )
518  type = (DataType) action->data().toUInt();
519 
520  for (auto* o_it : PluginFunctions::objects(PluginFunctions::ALL_OBJECTS, type) ) {
521  o_it->source(false);
522  }
523 }
524 
525 
526 //******************************************************************************
527 
532 
533  if ( ! OpenFlipper::Options::gui())
534  return;
535 
536  DataType type = DATA_ALL;
537 
538  //try to find dataType restriction if called by contextMenu
539  QAction* action = dynamic_cast< QAction* > ( sender() );
540 
541  if ( action != 0 && action->data().isValid() )
542  type = (DataType) action->data().toUInt();
543 
544  for (auto* o_it : PluginFunctions::objects(PluginFunctions::ALL_OBJECTS, type) ) {
545  o_it->hide();
546  }
547 
548 }
549 
550 
551 //******************************************************************************
552 
557 
558  if ( ! OpenFlipper::Options::gui())
559  return;
560 
561  DataType type = DATA_ALL;
562 
563  //try to find dataType restriction if called by contextMenu
564  QAction* action = dynamic_cast< QAction* > ( sender() );
565 
566  if ( action != 0 && action->data().isValid() )
567  type = (DataType) action->data().toUInt();
568 
569  for (auto* o_it : PluginFunctions::objects(PluginFunctions::ALL_OBJECTS, type) ) {
570  o_it->show();
571  }
572 
573 }
574 
575 //******************************************************************************
576 
581 
582  IdList list;
583 
584  for (auto* o_it : PluginFunctions::objects(PluginFunctions::TARGET_OBJECTS, _type) ) {
585  list.push_back( o_it->id() );
586  }
587 
588  return list;
589 }
590 
591 //******************************************************************************
592 
597 
598  IdList list;
599 
600  for (auto* o_it : PluginFunctions::objects(PluginFunctions::SOURCE_OBJECTS, _type) ) {
601  list.push_back( o_it->id() );
602  }
603 
604  return list;
605 }
606 
607 //******************************************************************************
608 
613 
615  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()));
616  }
617 }
618 
619 //******************************************************************************
620 
627 unsigned int DataControlPlugin::groupCount() const {
628  unsigned int count = 0;
630  ++count;
631 
632  return count;
633 }
634 
635 //******************************************************************************
636 
644 
645  QString list;
646 
647  for ( std::vector< TypeInfo >::const_iterator dataType = typesBegin(); dataType != typesEnd(); ++dataType )
648  list.append(dataType->name + "\n");
649 
650  return list;
651 }
652 
653 //******************************************************************************
660 bool DataControlPlugin::addObjectToGroup(int _objectId, int _groupId)
661 {
662  BaseObject* obj = 0;
663  BaseObject* group = 0;
664 
665  PluginFunctions::getObject(_objectId,obj);
666  PluginFunctions::getObject(_groupId,group);
667 
668  if ( obj && group)
669  {
670  if (group->isGroup())
671  {
672  obj->setParent(group);
673  return true;
674  }
675  else
676  emit log( LOGERR, tr(" Object with id %1 is not a group").arg(_groupId));
677  } else {
678  if (!obj)
679  emit log( LOGERR, tr( "Unable to get Object with id %1").arg(_objectId) );
680  if (!_groupId)
681  emit log( LOGERR, tr( "Unable to get Group with id %1").arg(_groupId) );
682  }
683  return false;
684 }
685 
686 //******************************************************************************
695 {
696  BaseObject* group = 0;
697  PluginFunctions::getObject(_groupId,group);
698  IdList result;
699 
700  for (int i = 0; i < group->childCount(); ++i)
701  result.push_back(group->child(i)->id());
702 
703  return result;
704 }
DLLEXPORT std::vector< TypeInfo >::const_iterator typesEnd()
Get iterator pointing to the last element in the types list.
Definition: Types.cc:185
void hideAll()
Hides all objects.
IdList getSourceObjects(DataType _type)
Get all source objects of given type.
void hideObject(int objectId)
Hide the given Object.
void showAll()
Shows all objects.
BaseObject * child(int row)
return a child
Definition: BaseObject.cc:506
virtual void show()
Sets the whole Scenegraph subtree of this node to visible.
int groupObjects(IdList _objectIDs, QString _groupName="")
Group given Objects together.
bool source()
Definition: BaseObject.cc:291
void setSource(int objectId, bool _source)
set the given Object as source
bool addObjectToGroup(int _objectId, int _groupId)
add an object to an existing group
void setTarget(int objectId, bool _target)
set the given Object as target
virtual void setName(QString _name) override
path to the file from which the object is loaded ( defaults to "." )
int childCount() const
get the number of children
Definition: BaseObject.cc:511
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
QString name() const
Return the name of this type as text.
Definition: Types.cc:411
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
BaseObject *& objectRoot()
Get the root of the object structure.
IdList getTargetObjects(DataType _type)
Get all target objects of given type.
void clearAllTarget()
Unselect all objects.
bool target()
Definition: BaseObject.cc:273
const QStringList SOURCE_OBJECTS("source")
Iterable object range.
const QStringList ALL_OBJECTS
Iterable object range.
const DataType DATA_GROUP(1)
Items used for Grouping.
Predefined datatypes.
Definition: DataTypes.hh:83
void setAllTarget()
Makes all available objects target.
int id() const
Definition: BaseObject.cc:190
BaseObject * childExists(int _objectId)
Check if the element exists in the subtree of this element.
Definition: BaseObject.cc:516
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:181
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
BaseObject * parent()
Get the parent item ( 0 if rootitem )
Definition: BaseObject.cc:466
void setParent(BaseObject *_parent)
Set the parent pointer.
Definition: BaseObject.cc:477
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
Definition: BaseObject.cc:723
QString getObjectName(int objectId)
Get the Objects name from the id.
void setAllSource()
Makes all available objects source.
Core Data Iterator used to iterate over all objects (Including groups)
unsigned int groupCount() const
Returns the number of groups.
DataType dataType(int objectId)
Get the DataType of a given object.
IdList getGroupElements(int _groupId)
Get all elements of the given group.
virtual void hide()
Sets the whole Scenegraph subtree of this node to invisible.
const QStringList TARGET_OBJECTS("target")
Iterable object range.
bool isGroup() const
Check if object is a group.
Definition: BaseObject.cc:619
ObjectRange objects(IteratorRestriction _restriction, DataType _dataType)
Iterable object range.
void printObjectInfoToLog()
Print information about all open objects to the console.
void objectDelete(int objectId)
Delete the given object.
int getObject(QString _name)
Get the id of an object by its name.
void clearAllSource()
remove source selection from all objects
DLLEXPORT std::vector< TypeInfo >::const_iterator typesBegin()
Get iterator pointing to the first element in the types list.
Definition: Types.cc:180
const DataType DATA_UNKNOWN(0)
None of the other Objects.
void setObjectName(int objectId, QString _name)
set the name of the given object
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.
int addEmptyGroup(QString _groupName="", int _parentGroupId=0)
Create new empty group.
QString availableDataTypeNames() const
Returns a list of all available DataTypes.
void showObject(int objectId)
Show the given Object.