BaseObject.cc 19.5 KB
Newer Older
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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 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$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56




//=============================================================================
//
//  MyTypes
//
//=============================================================================

#define BASEOBJECT_C


//== INCLUDES =================================================================

57
#include "BaseObjectCore.hh"
Jan Möbius's avatar
 
Jan Möbius committed
58
#include "Types.hh"
59
60
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

Jan Möbius's avatar
 
Jan Möbius committed
61
62
63

//== TYPEDEFS =================================================================

Jan Möbius's avatar
Jan Möbius committed
64
65
66
67
//== Variables =================================================================

static ObjectManager objectManager_;

Jan Möbius's avatar
 
Jan Möbius committed
68
69
70
71
72
73
74
//== CLASS DEFINITION =========================================================

/** This counter is used to provide unique object ids. Every time a object is created
 * the counter is incremented and a new objectId given to the object. DONT TOUCH IT.
 */
static int idGenerator = 1;

75
76
77
BaseObject::BaseObject(const BaseObject& _object) :
  QObject() 
{
78
79
80
81
  id_           = idGenerator;
  ++idGenerator;
  persistentId_ = _object.persistentId_;
  objectType_   = _object.objectType_;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
82
  flags_ = _object.flags_;
83
84
85
86
87
88
  visible_ = _object.visible_;
  parentItem_ = 0;
  childItems_.clear();
  name_ = "Copy of " + _object.name_;

  dataMap_.clear();
89
  
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  // Iterate over all per Object datas and try to copy them
  QMap< QString, PerObjectData* >::const_iterator mapIter = _object.dataMap_.begin();  
  while ( mapIter != _object.dataMap_.end() ) {
    // Try to get a copy of the object.
    PerObjectData* copiedData = mapIter.value()->copyPerObjectData();
    
    if ( copiedData ) {
      dataMap_.insert(mapIter.key(),copiedData);
    } else {
      std::cerr << "Failed to copy per Object Data: " << mapIter.key().toStdString() << std::endl;
    }
    
    mapIter++;
  }
  
105
106
107
108
109
110
  // If the pointer is 0 then we are creating the objectroot
  if ( PluginFunctions::objectRoot() ) {
    setParent(PluginFunctions::objectRoot());
    PluginFunctions::objectRoot()->appendChild(this);
  }
  
111
  objectManager_.objectCreated(id());
112
}
Jan Möbius's avatar
 
Jan Möbius committed
113
114

BaseObject::BaseObject(BaseObject* _parent) :
115
  QObject() ,
Jan Möbius's avatar
 
Jan Möbius committed
116
117
  id_(-1),
  persistentId_(-1),
118
  objectType_(DATA_UNKNOWN),
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
119
  flags_(),
Jan Möbius's avatar
 
Jan Möbius committed
120
121
122
123
124
125
  visible_(true),
  parentItem_(_parent),
  name_("NONAME")
{
  id_ = idGenerator;
  ++ idGenerator;
126
  
127
128
  // If the pointer is 0 then something went wrong
  if ( _parent ) {
Jan Möbius's avatar
Jan Möbius committed
129
    
130
131
    _parent->appendChild(this);
    
Jan Möbius's avatar
Jan Möbius committed
132
  } else {
133
134
135
136
137
138
    
    if ( PluginFunctions::objectRoot() ) {
      setParent(PluginFunctions::objectRoot());
      PluginFunctions::objectRoot()->appendChild(this);
    }
    
139
140
  }
  
141
  objectManager_.objectCreated(id());
Jan Möbius's avatar
 
Jan Möbius committed
142
143
}

144

Jan Möbius's avatar
 
Jan Möbius committed
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
BaseObject::~BaseObject() {

  deleteData();

}


// ===============================================================================
// Object Identification
// ===============================================================================

int BaseObject::id() {
  return id_;
}

int BaseObject::persistentId() {
  return persistentId_;
}

void BaseObject::persistentId( int _id ) {
  persistentId_ = _id;
}


// ===============================================================================
// Data
// ===============================================================================

void BaseObject::cleanup() {
  persistentId_ = -1;
175
  objectType_ = DATA_UNKNOWN;
Jan Möbius's avatar
 
Jan Möbius committed
176

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
177
178
  flags_.clear();

Jan Möbius's avatar
 
Jan Möbius committed
179
180
181
182
183
184
185
186
187
  visible_    = true;
  name_       = "NONAME";
}

// ===============================================================================
// Data Type Handling
// ===============================================================================

bool BaseObject::dataType(DataType _type) {
188
189
190
191
192
  if ( _type == DATA_ALL ) {
    return true;
  }

  return ( objectType_ & _type);
Jan Möbius's avatar
 
Jan Möbius committed
193
194
195
196
197
198
199
}

DataType BaseObject::dataType() {
  return BaseObject::objectType_;
}

void BaseObject::setDataType(DataType _type) {
200
  if ( objectType_ != DATA_UNKNOWN )
Jan Möbius's avatar
 
Jan Möbius committed
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
    std::cerr << "BaseObect : overwriting data type" << std::endl;
  objectType_ = _type;
}

// ===============================================================================
// Object Information
// ===============================================================================


QString BaseObject::getObjectinfo() {
  QString output;

  output += "Info for Object with id " + QString::number(id()) +"\n";
  output += "Object is : ";
  if ( target() )
    output += "target ";
  if ( source() )
    output += " source";

  if ( visible() )
    output += " visible";
  else
    output += " invisible";

  output +="\n";

  return output;
}

void BaseObject::printObjectInfo() {
  std::cout << getObjectinfo().toStdString();
}


// ===============================================================================
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
236
// flag Handling
Jan Möbius's avatar
 
Jan Möbius committed
237
238
239
// ===============================================================================

bool BaseObject::target() {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
240
  return flag("target");
Jan Möbius's avatar
 
Jan Möbius committed
241
242
243
}

void BaseObject::target(bool _target) {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
244
  setFlag("target", _target);
Jan Möbius's avatar
 
Jan Möbius committed
245
246
247
}

bool BaseObject::source() {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
248
  return flag("source");
Jan Möbius's avatar
 
Jan Möbius committed
249
250
251
}

void BaseObject::source(bool _source) {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
252
253
254
255
256
257
258
259
260
261
  setFlag("source", _source);
}

bool BaseObject::flag(QString _flag)
{
  return flags_.contains(_flag);
}

void BaseObject::setFlag(QString _flag, bool _set)
{
262
263
  bool emitted = false;
  
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
264
265
  if (flags_.contains(_flag))
  {
266
    if (!_set) {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
267
      flags_.removeAll(_flag);
268
      emit objectSelectionChanged(id());
269
      emitted = true;
270
    }
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
271
272
273
  }
  else
  {
274
    if (_set) {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
275
      flags_ << _flag;
276
      emit objectSelectionChanged(id());
277
      emitted = true;
278
    }
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
279
  }
280
  
281
282
283
  //always emit if its a group
  if ( !emitted && isGroup() )
    emit objectSelectionChanged(id());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
284
285
286
287
288
}

QStringList BaseObject::flags()
{
  return flags_;
Jan Möbius's avatar
 
Jan Möbius committed
289
290
291
292
293
294
295
296
297
298
}

// ===============================================================================
// Object visualization
// ===============================================================================

bool BaseObject::visible() {
  return visible_;
}

299
void BaseObject::visible(bool _visible) {
300
301
302
  // Only do something if this is really a change
  if (  visible_ != _visible ) {
    visible_ = _visible;
303
304
305
306
307
308
309
310
    
    emit visibilityChanged( id() );
    
  } else {
    
    //always emit if its a group
    if ( isGroup() )
      emit visibilityChanged( id() );
311
  }
312
313
}

Jan Möbius's avatar
 
Jan Möbius committed
314
315
316
317
318
319
// ===============================================================================
// ===============================================================================
// Tree Structure :
// ===============================================================================
// ===============================================================================

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
BaseObject* BaseObject::last() {
  //indexOf

//   // Visit child item of this node
//   if ( childItems_.size() > 0 ) {
//      return childItems_[0];
//   }
//
//   // No Child Item so visit the next child of the parentItem_
//   if ( parentItem_ ) {
//
//     BaseObject* parentPointer = parentItem_;
//     BaseObject* thisPointer   = this;
//
//     // while we are not at the root node
//     while ( parentPointer ) {
//
//       // If there is an unvisited child of the parent, return this one
//       if ( parentPointer->childCount() > ( thisPointer->row() + 1) ) {
//         return parentPointer->childItems_[ thisPointer->row() + 1 ];
//       }
//
//       // Go to the next level
//       thisPointer   = parentPointer;
//       parentPointer = parentPointer->parentItem_;
//
//     }
//
//     return thisPointer;
//   }
//
//   return this;
  std::cerr << "Last not implemented yet! " << std::endl;
  return 0;

}

// ===============================================================================
// ===============================================================================

Jan Möbius's avatar
 
Jan Möbius committed
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
BaseObject* BaseObject::next() {
  // Visit child item of this node
  if ( childItems_.size() > 0 ) {
     return childItems_[0];
  }

  // No Child Item so visit the next child of the parentItem_
  if ( parentItem_ ) {

    BaseObject* parentPointer = parentItem_;
    BaseObject* thisPointer   = this;

    // while we are not at the root node
    while ( parentPointer ) {

      // If there is an unvisited child of the parent, return this one
      if ( parentPointer->childCount() > ( thisPointer->row() + 1) ) {
        return parentPointer->childItems_[ thisPointer->row() + 1 ];
      }

      // Go to the next level
      thisPointer   = parentPointer;
      parentPointer = parentPointer->parentItem_;

    }

    return thisPointer;
  }

  return this;

}

393
394
395
// ===============================================================================
// ===============================================================================

Jan Möbius's avatar
 
Jan Möbius committed
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
int BaseObject::level() {
  int level = 0;
  BaseObject* current = this;

  // Go up and count the levels to the root node
  while ( current->parent() != 0 ) {
    level++;
    current = current->parent();
  }

  return level;
}

// ===============================================================================
// Parent
// ===============================================================================

int BaseObject::row() const
{
    if (parentItem_)
        return parentItem_->childItems_.indexOf(const_cast<BaseObject*>(this));

    return 0;
}

BaseObject* BaseObject::parent()
{
  return parentItem_;
}

/// Set the parent pointer
void BaseObject::setParent(BaseObject* _parent) {
428
  // remove this child from the old parents list
Jan Möbius's avatar
Jan Möbius committed
429
  if ( parentItem_ != 0 ) {
430
    parentItem_->removeChild(this);
Jan Möbius's avatar
Jan Möbius committed
431
432
433
434
   
    if ( !_parent->childItems_.contains(this) )
      _parent->appendChild(this);
  }
435
436
  
  // Store new parent
Jan Möbius's avatar
 
Jan Möbius committed
437
  parentItem_ = _parent;
438
439
440
  
  // Tell other plugins about this change
  emit objectPropertiesChanged(id());
Jan Möbius's avatar
 
Jan Möbius committed
441
442
443
444
445
446
447
448
449
}


// ===============================================================================
// Children
// ===============================================================================

void BaseObject::appendChild(BaseObject *item)
{
450
  if ( !childItems_.contains(item) )
Jan Möbius's avatar
 
Jan Möbius committed
451
    childItems_.append(item);
452
453
  else 
    std::cerr << "Warning! Trying to append a child twice! Remove the append calls from your File plugin!" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
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
}

BaseObject *BaseObject::child(int row)
{
    return childItems_.value(row);
}

int BaseObject::childCount() const
{
    return childItems_.count();
}

BaseObject* BaseObject::childExists(int _objectId) {

  // Check if this object has the requested id
  if ( id_ == _objectId )
    return this;

  // search in children
  for ( int i = 0 ; i < childItems_.size(); ++i ) {
    BaseObject* tmp = childItems_[i]->childExists(_objectId);
    if ( tmp != 0)
      return tmp;
  }

  return 0;
}

BaseObject* BaseObject::childExists(QString _name) {

  // Check if this object has the requested id
  if ( name() == _name )
    return this;

  // search in children
  for ( int i = 0 ; i < childItems_.size(); ++i ) {
    BaseObject* tmp = childItems_[i]->childExists(_name);
    if ( tmp != 0)
      return tmp;
  }

  return 0;
}

void BaseObject::removeChild( BaseObject* _item ) {

  bool found = false;
  QList<BaseObject*>::iterator i;
  for (i = childItems_.begin(); i != childItems_.end(); ++i) {
     if ( *i == _item ) {
        found = true;
        break;
     }
  }

  if ( !found ) {
    std::cerr << "Illegal remove request" << std::endl;
    return;
  }

  childItems_.erase(i);
}

QList< BaseObject* > BaseObject::getLeafs() {

  QList< BaseObject* > items;

  for ( int i = 0 ; i < childItems_.size(); ++i ) {
    items = items + childItems_[i]->getLeafs();
  }

  // If we are a leave...
  if ( childCount() == 0 )
    items.push_back(this);

  return items;
}

void BaseObject::deleteSubtree() {

  // call function for all children of this node
  for ( int i = 0 ; i < childItems_.size(); ++i) {

    // remove the subtree recursively
    childItems_[i]->deleteSubtree();

    // delete child
    delete childItems_[i];
  }

  // clear the array
  childItems_.clear();
}


// TreeItem* TreeItem::inConsistent() {
//
//   BaseObjectData* object;
//
//   // If id is -1 this is a group or rootitem so dont stop here
Jan Möbius's avatar
   
Jan Möbius committed
554
//   if ( (objectId_ != -1) && !PluginFunctions::getObject(objectId_,object)  )
Jan Möbius's avatar
 
Jan Möbius committed
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
//     return this;
//
//   for ( int i = 0 ; i < childItems.size(); ++i ) {
//     TreeItem* tmp = childItems[i]->inConsistent();
//     if ( tmp != 0)
//       return tmp;
//   }
//
//   return 0;
// }

// ===============================================================================
// Grouping
// ===============================================================================
int BaseObject::group() {
  // Skip root node
  if ( parent() == 0 )
    return -1;

  // Dont count root node as a group
  if ( parent()->parent() == 0 )
    return -1;

  // Only consider groups
  if ( !parent()->dataType(DATA_GROUP) )
    return -1;

  // Get the group id
  return ( parent()->id() );

}

bool BaseObject::isGroup() {
//   return ( (childItems_.size() > 0) || dataType(DATA_GROUP) ) ;
  return ( dataType(DATA_GROUP) ) ;
};


bool BaseObject::isInGroup( int _id ) {
  BaseObject* current = this;

  // Go up and check for the group id
  do {

    // Check if we found the id
    if ( current->id() == _id )
      return true;

    // Move on to parent object
    current = current->parent();
  } while ( current != 0 );

  return false;
}

bool BaseObject::isInGroup( QString _name ) {
  BaseObject* current = this;

  // Go up and check for the group name
  do {

    // Check if this object is a group and if it has the serach name
    if ( current->dataType( DATA_GROUP ) && (current->name() == _name) )
      return true;

    // Move on to parent object
    current = current->parent();

  } while ( current != 0 );

  return false;
}

std::vector< int > BaseObject::getGroupIds() {
  std::vector< int > groups;

  BaseObject* current = this;

  // Go up and collect all groups in the given order
  do {

    // collect only groups
    if ( current->dataType( DATA_GROUP ) )
      // Skip the root Object
      if ( current->parent() != 0 )
        groups.push_back( current->id() );

    // Move on to parent object
    current = current->parent();
  } while ( current != 0 );

  return groups;
}

QStringList BaseObject::getGroupNames() {
  QStringList groups;

  BaseObject* current = this;

  // Go up and collect all groups in the given order
  do {

    // collect only groups
    if ( current->dataType( DATA_GROUP ) )
      // Skip the root Object
      if ( current->parent() != 0 )
        groups.push_back( current->name() );

    // Move on to parent object
    current = current->parent();
  } while ( current != 0 );

  return groups;
}

// ===============================================================================
// Name
// ===============================================================================
QString BaseObject::name() {
  return name_;
}

void BaseObject::setName(QString _name ) {
  name_ = _name;
679
680
681
  
  // Tell plugins about the name change
  emit objectPropertiesChanged(id());
Jan Möbius's avatar
 
Jan Möbius committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
}

// ===============================================================================
// Content
// ===============================================================================
void BaseObject::update() {
}

void BaseObject::dumpTree() {

  // Add spaces to visualize level
  for ( int i = 0 ; i < level() ; ++i  )
    std::cerr << "   ";

  std::cerr << "Node ";
  std::cerr << std::string(name().toAscii());

  std::cerr << " with id : ";
  std::cerr << id();

  // Write the type of this Object
703
  std::cerr << " and type " << typeName(dataType()).toStdString()  <<  std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
704
705
706
707
708
709
710

  // call function for all children of this node
  for ( int i = 0 ; i < childItems_.size(); ++i)
    childItems_[i]->dumpTree();

}

711
712
713
714
715
BaseObject* BaseObject::copy() {
  std::cerr << "Copy not supported by this Object" << std::endl;
  return  0;
}

Jan Möbius's avatar
 
Jan Möbius committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767

// ===============================================================================
// per Object data functions
// ===============================================================================

void
BaseObject::
setObjectData( QString _dataName , PerObjectData* _data ) {
  dataMap_.insert( _dataName, _data );
}

void
BaseObject::
clearObjectData( QString _dataName ) {
  if (dataMap_.contains(_dataName))
    dataMap_.remove(_dataName);
}


bool
BaseObject::
hasObjectData( QString _dataName )
{
  return dataMap_.contains(_dataName);
}


PerObjectData*
BaseObject::
objectData( QString _dataName ) {
  if (dataMap_.contains(_dataName))
    return dataMap_.value(_dataName);
  else
    return 0;
}

void
BaseObject::
deleteData() {

  QMapIterator<QString, PerObjectData* > i(dataMap_);
  while (i.hasNext()) {
      i.next();
      delete i.value();
  }

  dataMap_.clear();

}



Jan Möbius's avatar
Jan Möbius committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
ObjectManager::ObjectManager() {
}

ObjectManager::~ObjectManager() {
  
}
    
void ObjectManager::objectCreated(int _objectId) 
{
  emit newObject(_objectId);
}

ObjectManager* getObjectManager() {
  return &objectManager_;
}


Jan Möbius's avatar
 
Jan Möbius committed
785
786
787


//=============================================================================