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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 #include "DataControlPlugin.hh"
54 
55 
56 //******************************************************************************
57 
59 
60  emit setSlotDescription("getSourceObjects(DataType)",tr("Returns the IdList of all source objects with given DataType."),
61  QStringList(tr("Datatype")), QStringList(tr("Datatype of the objects")));
62 
63  emit setSlotDescription("getTargetObjects(DataType)",tr("Returns the IdList of all target objects with given DataType."),
64  QStringList(tr("Datatype")), QStringList(tr("Datatype of the objects")));
65 
66  emit setSlotDescription("getObject(QString)",tr("Returns the id of an object with given name."),
67  QStringList(tr("Name")), QStringList(tr("Name of an object")));
68 
69  emit setSlotDescription("getObjectName(int)",tr("Returns the name of an object with given id."),
70  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
71 
72  emit setSlotDescription("dataType(int)",tr("Returns the DataType of the object with the given id."),
73  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
74 
75  emit setSlotDescription("hideObject(int)",tr("Hide object with the given id."),
76  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
77 
78  emit setSlotDescription("showObject(int)",tr("Show object with the given id."),
79  QStringList(tr("objectId")), QStringList(tr("ID of an object")));
80 
81  emit setSlotDescription("setTarget(int,bool)",tr("Set given object as target."),
82  QString(tr("ObjectId,Target")).split(","),
83  QString(tr("id of the object, set object as target?")).split(","));
84 
85  emit setSlotDescription("setSource(int,bool)",tr("Set given object as source."),
86  QString(tr("ObjectId,Source")).split(","),
87  QString(tr("id of the object, set object as source?")).split(","));
88 
89  emit setSlotDescription("setObjectName(int,QString)",tr("Set name of given object."),
90  QString(tr("ObjectId,name")).split(","),
91  QString(tr("id of the object, the new name")).split(","));
92 
93  emit setSlotDescription("addEmptyGroup(QString)", tr("Create new empty group."),
94  QStringList(tr("GroupName")),
95  QStringList(tr("Name of the new group.")));
96 
97  emit setSlotDescription("addEmptyGroup(QString,int)", tr("Create new empty group."),
98  QString(tr("GroupName,Parent")).split(","),
99  QString(tr("Name of the new group., Parent of the new group, or -1, if there is no parent.")).split(","));
100 
101  emit setSlotDescription("groupObjects(IdList,QString)",tr("Group given Objects together."),
102  QString(tr("objectIds,groupName")).split(","),
103  QString(tr("List of objects that should be grouped., Name of the group.")).split(","));
104 
105  emit setSlotDescription("groupObjects(IdList)",tr("Group given Objects together."),
106  QStringList(tr("objectIds")), QStringList(tr("List of objects that should be grouped.")));
107 
108  emit setSlotDescription("groupCount()",tr("Returns the number of group objects."),
109  QStringList(tr("")), QStringList(tr("")));
110 
111  emit setSlotDescription("unGroupObject(int)",tr("Remove the given object from its group and append to root node."),
112  QStringList(tr("ObjectId")), QStringList(tr("Object to be removed from group.")));
113 
114  emit setSlotDescription("objectDelete(int)",tr("Delete an object"),
115  QStringList(tr("objectId")), QStringList(tr("Delete the given object.")));
116 
117  emit setSlotDescription("getGroupElements(int)",tr("Get elements of a group"),
118  QStringList(tr("groupId")), QStringList(tr("Id of the group.")));
119 
120  emit setSlotDescription("copyObject(int)",tr("Create a copy of an object"),
121  QStringList(tr("objectId")), QStringList(tr("Object to copy.")));
122 
123  emit setSlotDescription("setAllTarget()",tr("Set All objects as targets"),
124  QStringList(), QStringList());
125 
126  emit setSlotDescription("setAllSource()",tr("Set All objects as source"),
127  QStringList(), QStringList());
128 
129  emit setSlotDescription("clearAllTarget()",tr("Clear targets"),
130  QStringList(), QStringList());
131 
132  emit setSlotDescription("clearAllSource()",tr("Clear sources"),
133  QStringList(), QStringList());
134 
135  emit setSlotDescription("showAll()",tr("Show all objects"),
136  QStringList(), QStringList());
137 
138  emit setSlotDescription("hideAll()",tr("Hide all objects"),
139  QStringList(), QStringList());
140 
141  emit setSlotDescription("availableDataTypeNames()",tr("Returns a QStringList of all available DataType names."),
142  QStringList(tr("")), QStringList(tr("")));
143 
144  emit setSlotDescription("printObjectInfoToLog()",tr("Print info about all objects to log"),
145  QStringList(), QStringList());
146 
147  emit setSlotDescription("addObjectToGroup(int,int)",tr("Add an Object to an existing group"),
148  QStringList(tr("objectId,groupId").split(",")),
149  QStringList(tr("ID of an object.,ID of an group where the object has to be added.").split(",")));
150 
151 }
152 
153 
154 //******************************************************************************
155 
161 int DataControlPlugin::getObject( QString _name ) {
162 
164 
165  if ( object == 0)
166  return -1;
167 
168  return object->id();
169 }
170 
171 
172 //******************************************************************************
173 
179 int DataControlPlugin::copyObject( int objectId ) {
180 
181  int newObject;
182 
183  emit copyObject( objectId, newObject );
184 
185  return newObject;
186 }
187 
188 
189 //******************************************************************************
190 
196 QString DataControlPlugin::getObjectName( int objectId ) {
197 
198  BaseObjectData* object;
199  if ( ! PluginFunctions::getObject(objectId,object) ) {
200  emit log(LOGERR,tr("getObjectName : unable to get object") );
201  return QString(tr("Unknown Object"));
202  } else
203  return object->name() ;
204 
205 }
206 
207 //******************************************************************************
208 
211  BaseObject* object = 0;
212  PluginFunctions::getObject(_objectId,object);
213 
214  if ( object )
215  return object->dataType();
216  else
217  return DATA_UNKNOWN;
218 }
219 
220 
221 //******************************************************************************
222 
227 void DataControlPlugin::hideObject( int objectId ) {
228 
229  if ( ! OpenFlipper::Options::gui())
230  return;
231 
232  BaseObjectData* object;
233  if ( PluginFunctions::getObject(objectId,object) ){
234  object->hide();
235  }
236 
237 }
238 
239 
240 //******************************************************************************
241 
247 void DataControlPlugin::setTarget( int objectId, bool _target ) {
248 
249  BaseObjectData* object;
250  if ( PluginFunctions::getObject(objectId,object) ){
251  object->target( _target );
252  }
253 }
254 
255 
256 //******************************************************************************
257 
263 void DataControlPlugin::setSource( int objectId, bool _source ) {
264 
265  BaseObjectData* object;
266  if ( PluginFunctions::getObject(objectId,object) ){
267  object->source( _source );
268  }
269 
270 }
271 
272 
273 //******************************************************************************
274 
280 void DataControlPlugin::setObjectName( int objectId, QString _name ) {
281 
282  BaseObjectData* object;
283  if ( PluginFunctions::getObject(objectId,object) ){
284  object->setName( _name );
285  }
286 }
287 
288 
289 //******************************************************************************
290 
295 void DataControlPlugin::objectDelete( int objectId ) {
296 
297  BaseObjectData* object;
298  if ( ! PluginFunctions::getObject(objectId,object) )
299  return;
300 
301  if ( object == 0)
302  return;
303 
304  emit deleteObject(objectId);
305 }
306 
307 
308 //******************************************************************************
309 
314 void DataControlPlugin::showObject( int objectId ) {
315 
316  if ( ! OpenFlipper::Options::gui())
317  return;
318 
319  BaseObjectData* object;
320  if ( PluginFunctions::getObject(objectId,object) ){
321  object->show();
322  }
323 
324 }
325 
326 
327 //******************************************************************************
328 
334 int DataControlPlugin::addEmptyGroup(QString _groupName, int _parentGroupId) {
335  GroupObject* parentGroupObject = dynamic_cast<GroupObject*>(PluginFunctions::objectRoot());
336  BaseObject* parentObject = 0;
337  if (PluginFunctions::getObject(_parentGroupId, parentObject)) {
338  GroupObject* parent = dynamic_cast<GroupObject*>(parentObject);
339  if (parent) {
340  parentGroupObject = parent;
341  } else {
342  emit log(LOGWARN, tr("Parent object %1 is not a group, creating a new toplevel group").arg(_parentGroupId));
343  }
344  } else if (_parentGroupId != 0 ) {
345  emit log(LOGWARN, tr("Cannot get parent object %1, creating a new toplevel group").arg(_parentGroupId));
346  }
347 
348  GroupObject* groupObject = new GroupObject("newGroup", parentGroupObject);
349 
350  if (_groupName == "") {
351  groupObject->setName(tr("New group ") + QString::number(groupObject->id()));
352  } else {
353  groupObject->setName(_groupName);
354  }
355 
356  emit emptyObjectAdded(groupObject->id());
357 
358  return groupObject->id();
359 }
360 
361 //******************************************************************************
362 
368 int DataControlPlugin::groupObjects(IdList _objectIDs, QString _groupName) {
369  QVector< BaseObject* > objs;
370 
371  // Try to get all objects given in list
372  for (uint i=0; i < _objectIDs.size(); i++){
373  BaseObject* obj;
374  if ( PluginFunctions::getObject(_objectIDs[i],obj) )
375  objs.push_back(obj);
376  }
377 
378  // If all of them fail, stop here.
379  if (objs.size() == 0){
380  emit log(tr("No objects to group."));
381  return -1;
382  }
383 
384  // Check if all objects have the same parent
385  // Abort if the parents differ
386  BaseObject* parent = (objs[0])->parent();
387  for ( int i = 1 ; i < objs.size() ; ++i){
388  if ( parent != (objs[i])->parent() ){
389  emit log(tr("Cannot group Objects with different parents"));
390  return -1;
391  }
392 
393  }
394 
395  //create new group
396  int groupId = addEmptyGroup(_groupName);
397  BaseObject *groupItem = 0;
398  PluginFunctions::getObject(groupId, groupItem);
399 
400  if (parent) {
401  groupItem->setParent(parent);
402  }
403 
404  IdList ids;
405  //append new children to group
406  for ( int i = 0 ; i < objs.size() ; ++i)
407  {
408  (objs[i])->setParent(groupItem);
409  ids.push_back((objs[i])->id());
410  }
411 
412  emit objectsGrouped(ids);
413  emit updatedObject(groupId,UPDATE_ALL);
414 
415  return groupId;
416 }
417 
418 //******************************************************************************
419 
421  BaseObject* group = 0;
422 
423  PluginFunctions::getObject(_id,group);
424 
425  if ( group && group->isGroup())
426  {
427  //iterate over children
428  for (int i=group->childCount()-1; i >= 0; --i){
429  BaseObject* child = group->child(i);
430 
431  // then change the parent
432  child->setParent(group->parent());
433  }
434  //delete the group
435  emit deleteObject( group->id() );
436 
437  return true;
438  } else {
439  if (group)
440  emit log( LOGERR, tr("Cannot Ungroup. Object with id %1 is not a group").arg(_id));
441  else
442  emit log( LOGERR, tr( "Unable to get Object with id %1 for ungrouping").arg(_id) );
443  return false;
444  }
445 }
446 
447 
448 //******************************************************************************
449 
454 
455  DataType type = DATA_ALL;
456 
457  //try to find dataType restriction if called by contextMenu
458  QAction* action = dynamic_cast< QAction* > ( sender() );
459 
460  if ( action != 0 && action->data().isValid() )
461  type = (DataType) action->data().toUInt();
462 
464  o_it != PluginFunctions::objectsEnd(); ++o_it){
465  o_it->target(true);
466  }
467 }
468 
469 
470 //******************************************************************************
471 
476 
477  DataType type = DATA_ALL;
478 
479  //try to find dataType restriction if called by contextMenu
480  QAction* action = dynamic_cast< QAction* > ( sender() );
481 
482  if ( action != 0 && action->data().isValid() )
483  type = (DataType) action->data().toUInt();
484 
486  o_it != PluginFunctions::objectsEnd(); ++o_it){
487  o_it->source(true);
488  }
489 }
490 
491 
492 //******************************************************************************
493 
498 
499  DataType type = DATA_ALL;
500 
501  //try to find dataType restriction if called by contextMenu
502  QAction* action = dynamic_cast< QAction* > ( sender() );
503 
504  if ( action != 0 && action->data().isValid() )
505  type = (DataType) action->data().toUInt();
506 
508  o_it != PluginFunctions::objectsEnd(); ++o_it){
509  o_it->target(false);
510  }
511 }
512 
513 
514 //******************************************************************************
515 
520 
521  DataType type = DATA_ALL;
522 
523  //try to find dataType restriction if called by contextMenu
524  QAction* action = dynamic_cast< QAction* > ( sender() );
525 
526  if ( action != 0 && action->data().isValid() )
527  type = (DataType) action->data().toUInt();
528 
530  o_it != PluginFunctions::objectsEnd(); ++o_it){
531  o_it->source(false);
532  }
533 }
534 
535 
536 //******************************************************************************
537 
542 
543  if ( ! OpenFlipper::Options::gui())
544  return;
545 
546  DataType type = DATA_ALL;
547 
548  //try to find dataType restriction if called by contextMenu
549  QAction* action = dynamic_cast< QAction* > ( sender() );
550 
551  if ( action != 0 && action->data().isValid() )
552  type = (DataType) action->data().toUInt();
553 
555  o_it != PluginFunctions::objectsEnd(); ++o_it){
556  o_it->hide();
557  }
558 
559 }
560 
561 
562 //******************************************************************************
563 
568 
569  if ( ! OpenFlipper::Options::gui())
570  return;
571 
572  DataType type = DATA_ALL;
573 
574  //try to find dataType restriction if called by contextMenu
575  QAction* action = dynamic_cast< QAction* > ( sender() );
576 
577  if ( action != 0 && action->data().isValid() )
578  type = (DataType) action->data().toUInt();
579 
581  o_it->show();
582  }
583 
584 }
585 
586 //******************************************************************************
587 
592 
593  IdList list;
594 
596  list.push_back( o_it->id() );
597 
598  return list;
599 }
600 
601 //******************************************************************************
602 
607 
608  IdList list;
609 
611  list.push_back( o_it->id() );
612 
613  return list;
614 }
615 
616 //******************************************************************************
617 
622 
624  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()));
625 }
626 
627 //******************************************************************************
628 
635 unsigned int DataControlPlugin::groupCount() const {
636  unsigned int count = 0;
638  ++count;
639 
640  return count;
641 }
642 
643 //******************************************************************************
644 
652 
653  QStringList list;
654 
655  for ( std::vector< TypeInfo >::const_iterator dataType = typesBegin(); dataType != typesEnd(); ++dataType )
656  list.push_back(dataType->name);
657 
658  return list;
659 }
660 
661 //******************************************************************************
668 bool DataControlPlugin::addObjectToGroup(int _objectId, int _groupId)
669 {
670  BaseObject* obj = 0;
671  BaseObject* group = 0;
672 
673  PluginFunctions::getObject(_objectId,obj);
674  PluginFunctions::getObject(_groupId,group);
675 
676  if ( obj && group)
677  {
678  if (group->isGroup())
679  {
680  obj->setParent(group);
681  return true;
682  }
683  else
684  emit log( LOGERR, tr(" Object with id %1 is not a group").arg(_groupId));
685  } else {
686  if (!obj)
687  emit log( LOGERR, tr( "Unable to get Object with id %1").arg(_objectId) );
688  if (!_groupId)
689  emit log( LOGERR, tr( "Unable to get Group with id %1").arg(_groupId) );
690  }
691  return false;
692 }
693 
694 //******************************************************************************
703 {
704  BaseObject* group = 0;
705  PluginFunctions::getObject(_groupId,group);
706  IdList result;
707 
708  for (int i = 0; i < group->childCount(); ++i)
709  result.push_back(group->child(i)->id());
710 
711  return result;
712 }
BaseObject *& objectRoot()
Get the root of the object structure.
virtual void hide()
Sets the whole Scenegraph subtree of this node to invisible.
Predefined datatypes.
Definition: DataTypes.hh:96
int getObject(QString _name)
Get the id of an object by its name.
DLLEXPORT std::vector< TypeInfo >::const_iterator typesBegin()
Get iterator pointing to the first element in the types list.
Definition: Types.cc:191
bool getObject(int _identifier, BSplineCurveObject *&_object)
const QStringList SOURCE_OBJECTS("source")
Iterable object range.
void hideObject(int objectId)
Hide the given Object.
const QStringList TARGET_OBJECTS("target")
Iterable object range.
int childCount() const
get the number of children
Definition: BaseObject.cc:522
const DataType DATA_UNKNOWN(0)
None of the other Objects.
void setSource(int objectId, bool _source)
set the given Object as source
void printObjectInfoToLog()
Print information about all open objects to the console.
IdList getTargetObjects(DataType _type)
Get all target objects of given type.
void showAll()
Shows all objects.
virtual void show()
Sets the whole Scenegraph subtree of this node to visible.
DLLEXPORT std::vector< TypeInfo >::const_iterator typesEnd()
Get iterator pointing to the last element in the types list.
Definition: Types.cc:196
int id() const
Definition: BaseObject.cc:201
const QStringList ALL_OBJECTS
Iterable object range.
void clearAllSource()
remove source selection from all objects
BaseObject * parent()
Get the parent item ( 0 if rootitem )
Definition: BaseObject.cc:477
void setParent(BaseObject *_parent)
Set the parent pointer.
Definition: BaseObject.cc:488
DataType dataType(int objectId)
Get the DataType of a given object.
bool target()
Definition: BaseObject.cc:284
void setObjectName(int objectId, QString _name)
set the name of the given object
void setAllSource()
Makes all available objects source.
void setAllTarget()
Makes all available objects target.
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
void clearAllTarget()
Unselect all objects.
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.
const DataType DATA_GROUP(1)
Items used for Grouping.
BaseObject * child(int row)
return a child
Definition: BaseObject.cc:517
bool addObjectToGroup(int _objectId, int _groupId)
add an object to an existing group
bool isGroup() const
Check if object is a group.
Definition: BaseObject.cc:630
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:192
QString name() const
Return the name of this type as text.
Definition: Types.cc:427
QString getObjectName(int objectId)
Get the Objects name from the id.
int addEmptyGroup(QString _groupName="", int _parentGroupId=0)
Create new empty group.
IdList getSourceObjects(DataType _type)
Get all source objects of given type.
Core Data Iterator used to iterate over all objects (Including groups)
void setTarget(int objectId, bool _target)
set the given Object as target
void objectDelete(int objectId)
Delete the given object.
IdList getGroupElements(int _groupId)
Get all elements of the given group.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
BaseObject * childExists(int _objectId)
Check if the element exists in the subtree of this element.
Definition: BaseObject.cc:527
int groupObjects(IdList _objectIDs, QString _groupName="")
Group given Objects together.
bool source()
Definition: BaseObject.cc:302
void hideAll()
Hides all objects.
unsigned int groupCount() const
Returns the number of groups.
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
Definition: BaseObject.cc:734
QStringList availableDataTypeNames() const
Returns a list of all available DataTypes.
void showObject(int objectId)
Show the given Object.