TreeModel.cc 22.8 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
/*===========================================================================*\
 *                                                                           *
 *                              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/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
38
39
40
41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53

#include <QtGui>
#include <QBrush>

#include "TreeModel.hh"

#include <iostream>


#include <OpenFlipper/common/Types.hh>
#include "../OpenFlipper/BasePlugin/PluginFunctions.hh"

Dirk Wilden's avatar
Dirk Wilden committed
54
55
56
57

//******************************************************************************

/** \brief Constructor
58
 *
Dirk Wilden's avatar
Dirk Wilden committed
59
60
 * @param _parent parent Object
 */
Jan Möbius's avatar
 
Jan Möbius committed
61
62
TreeModel::TreeModel( QObject *_parent) : QAbstractItemModel(_parent)
{
63
  rootItem_ = new TreeItem( -1, "ROOT", DATA_UNKNOWN, 0);
Jan Möbius's avatar
 
Jan Möbius committed
64
65
}

Dirk Wilden's avatar
Dirk Wilden committed
66
67
68
69

//******************************************************************************

/** \brief Destructor
70
 *
Dirk Wilden's avatar
Dirk Wilden committed
71
 */
Jan Möbius's avatar
 
Jan Möbius committed
72
73
TreeModel::~TreeModel()
{
Dirk Wilden's avatar
Dirk Wilden committed
74

Jan Möbius's avatar
 
Jan Möbius committed
75
76
}

Dirk Wilden's avatar
Dirk Wilden committed
77
78
79
80

//******************************************************************************

/** \brief Return the number of columns
81
 *
Dirk Wilden's avatar
Dirk Wilden committed
82
83
84
 * @param unused
 * @return return always 4
 */
Dirk Wilden's avatar
Dirk Wilden committed
85
int TreeModel::columnCount(const QModelIndex &/*_parent*/) const
Jan Möbius's avatar
 
Jan Möbius committed
86
87
88
89
90
{
  // Name,Visible,Source,Target -> 4
  return (4);
}

Dirk Wilden's avatar
Dirk Wilden committed
91
92
93
94

//******************************************************************************

/** \brief Returns the data stored under the given role for the item referred to by the index
95
 *
Dirk Wilden's avatar
Dirk Wilden committed
96
97
98
99
 * @param index a ModelIndex that defines the item in the tree
 * @param role defines the kind of data requested
 * @return requested data
 */
Jan Möbius's avatar
 
Jan Möbius committed
100
101
102
103
104
105
QVariant TreeModel::data(const QModelIndex &index, int role) const
{

  // Skip invalid requests
  if (!index.isValid())
      return QVariant();
106

Jan Möbius's avatar
 
Jan Möbius committed
107
  // Get the corresponding tree item
Dirk Wilden's avatar
Dirk Wilden committed
108
  TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
109

Jan Möbius's avatar
 
Jan Möbius committed
110
  if ( item == rootItem_ ) {
111
    std::cerr << "Root" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
112
  }
113

Jan Möbius's avatar
 
Jan Möbius committed
114
115
116
  // Set the background color of the objects row
  if ( role == Qt::BackgroundRole ) {
    if ( !item->visible() ) {
117
      return QVariant( QBrush(QColor(100,100,100) ) );
Jan Möbius's avatar
 
Jan Möbius committed
118
119
    }
  }
Dirk Wilden's avatar
Dirk Wilden committed
120

Jan Möbius's avatar
 
Jan Möbius committed
121
122
123
  switch (index.column() ) {
    // Name
    case 0 :
124

Dirk Wilden's avatar
Dirk Wilden committed
125
      if (role != Qt::DisplayRole && role != Qt::EditRole )
Jan Möbius's avatar
 
Jan Möbius committed
126
        return QVariant();
127

Jan Möbius's avatar
 
Jan Möbius committed
128
      return QVariant(item->name());
129

Jan Möbius's avatar
 
Jan Möbius committed
130
131
132
133
134
135
136
      break;
    // Visible
    case 1 :
      if (role == Qt::CheckStateRole ) {
        bool visibility = false;
        // visiblity group
        if (item->isGroup()){
Dirk Wilden's avatar
Dirk Wilden committed
137
          QList< TreeItem* > children = item->getLeafs();
Jan Möbius's avatar
 
Jan Möbius committed
138
139
140
141
142
143
144
145
146
147
148
          bool initRound = true;
          for (int i=0; i < children.size(); i++){
            if (initRound){
              visibility = children[i]->visible();
              initRound = false;
            }else if (visibility != children[i]->visible())
              return QVariant(Qt::PartiallyChecked);
          }
        }else
          //visibility item
          visibility = item->visible();
149
        if (visibility)
Jan Möbius's avatar
 
Jan Möbius committed
150
151
152
153
154
          return QVariant(Qt::Checked);
        else
          return QVariant(Qt::Unchecked);
      }
      return QVariant();
155

Jan Möbius's avatar
 
Jan Möbius committed
156
157
158
159
160
161
162
      break;
    // Source
    case 2 :
      if (role == Qt::CheckStateRole ) {
        bool source = false;
        // source group
        if (item->isGroup()){
Dirk Wilden's avatar
Dirk Wilden committed
163
          QList< TreeItem* > children = item->getLeafs();
164

Jan Möbius's avatar
 
Jan Möbius committed
165
166
167
168
169
170
171
172
173
174
175
          bool initRound = true;
          for (int i=0; i < children.size(); i++){
            if (initRound){
              source = children[i]->source();
              initRound = false;
            }else if (source != children[i]->source())
              return QVariant(Qt::PartiallyChecked);
          }
        }else
          //source item
          source = item->source();
176
        if (source)
Jan Möbius's avatar
 
Jan Möbius committed
177
178
179
180
181
182
          return QVariant(Qt::Checked);
        else
          return QVariant(Qt::Unchecked);
      }
      return QVariant();

183
    // Target
Jan Möbius's avatar
 
Jan Möbius committed
184
185
186
187
188
    case 3 :
      if (role == Qt::CheckStateRole ) {
        bool target = false;
        // target group
        if (item->isGroup()){
Dirk Wilden's avatar
Dirk Wilden committed
189
          QList< TreeItem* > children = item->getLeafs();
190

Jan Möbius's avatar
 
Jan Möbius committed
191
192
193
194
195
196
197
198
199
200
201
          bool initRound = true;
          for (int i=0; i < children.size(); i++){
            if (initRound){
              target = children[i]->target();
              initRound = false;
            }else if (target != children[i]->target())
              return QVariant(Qt::PartiallyChecked);
          }
        }else
          //target item
          target = item->target();
202
        if (target)
Jan Möbius's avatar
 
Jan Möbius committed
203
204
205
206
207
208
209
          return QVariant(Qt::Checked);
        else
          return QVariant(Qt::Unchecked);
      }
      return QVariant();

      break;
210

Jan Möbius's avatar
 
Jan Möbius committed
211
212
213
214
215
216
    default:
      return QVariant();
  }

}

Dirk Wilden's avatar
Dirk Wilden committed
217
218
219
220

//******************************************************************************

/** \brief Returns the item flags for the given index
221
 *
Dirk Wilden's avatar
Dirk Wilden committed
222
223
224
 * @param index ModelIndex that defines an item in the tree
 * @return flags for the given ModelIndex
 */
Jan Möbius's avatar
 
Jan Möbius committed
225
226
227
228
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;
229
230
231

    Qt::ItemFlags flags = 0;

Jan Möbius's avatar
 
Jan Möbius committed
232
    // Show/Source/Target
233
    if ( ( index.column() == 1 ) ||
Jan Möbius's avatar
 
Jan Möbius committed
234
235
         ( index.column() == 2 ) ||
         ( index.column() == 3 ) )
236
237
      flags = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    else
Jan Möbius's avatar
 
Jan Möbius committed
238
    if ( index.column() == 0 )
239
240
241
242
243
      flags = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    else
      flags = Qt::ItemIsEnabled;

  // Get the corresponding tree item
Dirk Wilden's avatar
Dirk Wilden committed
244
  TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
245
246
247
248
249

  if ( item->isGroup() )
    return flags | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
  else
    return flags | Qt::ItemIsDragEnabled;
Jan Möbius's avatar
 
Jan Möbius committed
250
251
}

Dirk Wilden's avatar
Dirk Wilden committed
252
253
254
255

//******************************************************************************

/** \brief Returns the data in the header
256
 *
Dirk Wilden's avatar
Dirk Wilden committed
257
258
259
260
261
 * @param section the column in the header
 * @param orientation header orientation (only horizontal handled)
 * @param role the role that defines the type of data
 * @return the requested data
 */
Jan Möbius's avatar
 
Jan Möbius committed
262
263
264
265
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
                                int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
266

Jan Möbius's avatar
 
Jan Möbius committed
267
268
269
270
271
272
273
274
275
276
277
278
      switch (section) {
        case 0 : return QVariant("Name");
        case 1 : return QVariant("Show");
        case 2 : return QVariant("Source");
        case 3 : return QVariant("Target");
        default :
          return QVariant();
      }
    }
    return QVariant();
}

Dirk Wilden's avatar
Dirk Wilden committed
279
280
281
282

//******************************************************************************

/** \brief Returns the index of the item in the model specified by the given row, column and parent index.
283
284
 *
 * @param row the row
Dirk Wilden's avatar
Dirk Wilden committed
285
286
287
288
 * @param column the column
 * @param _parent parent item
 * @return corresponding ModelIndex
 */
Jan Möbius's avatar
 
Jan Möbius committed
289
290
QModelIndex TreeModel::index(int row, int column, const QModelIndex &_parent) const
{
Dirk Wilden's avatar
Dirk Wilden committed
291
292
//     if (!hasIndex(row, column, _parent))
//         return QModelIndex();
Jan Möbius's avatar
 
Jan Möbius committed
293

Dirk Wilden's avatar
Dirk Wilden committed
294
    TreeItem *parentItem;
Jan Möbius's avatar
 
Jan Möbius committed
295
296
297
298

    if (!_parent.isValid())
        parentItem = rootItem_;
    else
Dirk Wilden's avatar
Dirk Wilden committed
299
        parentItem = static_cast<TreeItem*>(_parent.internalPointer());
Jan Möbius's avatar
 
Jan Möbius committed
300

Dirk Wilden's avatar
Dirk Wilden committed
301
    TreeItem *childItem = parentItem->child(row);
Jan Möbius's avatar
 
Jan Möbius committed
302
303
304
305
306
307
    if (childItem)
        return createIndex(row, column, childItem);
    else
        return QModelIndex();
}

Dirk Wilden's avatar
Dirk Wilden committed
308
309
310
311

//******************************************************************************

/** \brief Return index of parent item
312
 *
Dirk Wilden's avatar
Dirk Wilden committed
313
314
315
 * @param index a ModelIndex
 * @return parent of the given ModelIndex
 */
Jan Möbius's avatar
 
Jan Möbius committed
316
317
318
319
320
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
        return QModelIndex();

Dirk Wilden's avatar
Dirk Wilden committed
321
322
    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    TreeItem *parentItem = childItem->parent();
Jan Möbius's avatar
 
Jan Möbius committed
323
324
325
326
327
328
329

    if (parentItem == rootItem_)
        return QModelIndex();

    return createIndex(parentItem->row(), 0, parentItem);
}

Dirk Wilden's avatar
Dirk Wilden committed
330
331
332
333

//******************************************************************************

/** \brief Returns the number of rows under given parent
334
 *
Dirk Wilden's avatar
Dirk Wilden committed
335
336
337
 * @param _parent parent Item
 * @return number of rows that are children of given parent
 */
Jan Möbius's avatar
 
Jan Möbius committed
338
339
int TreeModel::rowCount(const QModelIndex &_parent) const
{
Dirk Wilden's avatar
Dirk Wilden committed
340
    TreeItem *parentItem;
Jan Möbius's avatar
 
Jan Möbius committed
341
342
343
344
345
346
    if (_parent.column() > 0)
        return 0;

    if (!_parent.isValid())
        parentItem = rootItem_;
    else
Dirk Wilden's avatar
Dirk Wilden committed
347
        parentItem = static_cast<TreeItem*>(_parent.internalPointer());
Jan Möbius's avatar
 
Jan Möbius committed
348
349
350
351
352

    return parentItem->childCount();
}


Dirk Wilden's avatar
Dirk Wilden committed
353
354
//******************************************************************************

Dirk Wilden's avatar
Dirk Wilden committed
355
/** \brief The object with the given id has been changed. Update the model.
356
 *
Dirk Wilden's avatar
Dirk Wilden committed
357
358
 * @param _id  id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
359
void TreeModel::objectChanged(int _id) {
Jan Möbius's avatar
 
Jan Möbius committed
360

Dirk Wilden's avatar
Dirk Wilden committed
361
  if ( _id != -1 ){
Jan Möbius's avatar
 
Jan Möbius committed
362

Dirk Wilden's avatar
Dirk Wilden committed
363
364
    BaseObject* obj = 0;
    PluginFunctions::getObject(_id, obj);
Jan Möbius's avatar
 
Jan Möbius committed
365

Dirk Wilden's avatar
Dirk Wilden committed
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
    TreeItem* item = rootItem_->childExists(_id);

    //if internal and external representation are both valid
    if (obj != 0 && item != 0){
      //update the name
      if ( obj->name() != item->name() ){

        item->name( obj->name() );

        QModelIndex index = getModelIndex(item,0);
        if ( index.isValid() )
          emit dataChanged( index, index);
      }

      //update visibility
      if ( obj->visible() != item->visible() || obj->isGroup() ){

        item->visible( obj->visible() );

        QModelIndex index0 = getModelIndex(item,0);
        QModelIndex index1 = getModelIndex(item,3);

        if ( index0.isValid() && index1.isValid() ){
          //the whole row has to be updated because of the grey background-color
          emit dataChanged( index0, index1);
          propagateUpwards(item->parent(), 1, obj->visible() );
        }

        if ( obj->isGroup() )
          propagateDownwards(item, 1 );
      }

      //update source flag
      if ( obj->source() != item->source() || obj->isGroup() ){

        item->source( obj->source() );

        QModelIndex index = getModelIndex(item,2);
        if ( index.isValid() ){
          emit dataChanged( index, index);
          propagateUpwards(item->parent(), 2, false );
        }

        if ( obj->isGroup() )
          propagateDownwards(item, 2 );
      }

      //update target flag
      if ( obj->target() != item->target() || obj->isGroup() ){

        item->target( obj->target() );

        QModelIndex index = getModelIndex(item,3);
        if ( index.isValid() ){
          emit dataChanged( index, index);
          propagateUpwards(item->parent(), 3, false );
        }

        if ( obj->isGroup() )
          propagateDownwards(item, 3 );
      }

      //update parent
      if ( obj->parent() == PluginFunctions::objectRoot() && isRoot( item->parent() ) ){
        return;
      }else if ( obj->parent() == PluginFunctions::objectRoot() && !isRoot( item->parent() ) ){
        moveItem(item, rootItem_ );
      }else if ( obj->parent()->id() != item->parent()->id() ){
        TreeItem* parent = rootItem_->childExists( obj->parent()->id() );

        if (parent != 0)
          moveItem(item, parent );
      }
Dirk Wilden's avatar
Dirk Wilden committed
439
    }
Dirk Wilden's avatar
Dirk Wilden committed
440
441
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
442

Dirk Wilden's avatar
Dirk Wilden committed
443
444

/** \brief The object with the given id has been added. add it to the internal tree
445
 *
Dirk Wilden's avatar
Dirk Wilden committed
446
447
448
449
 * @param id_ id of the object
 */
void TreeModel::objectAdded(BaseObject* _object){

450
451
452
453
454
455
456
  // check if item already in model tree
  // this function can be called by addEmpty and fileOpened
  // both will be called by fileOpened such that the item
  // already exists
  if( rootItem_->childExists( _object->id() )) 
    return;

Dirk Wilden's avatar
Dirk Wilden committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
  TreeItem* parent = 0;
  //find the parent
  if ( _object->parent() == PluginFunctions::objectRoot() )
    parent = rootItem_;
  else
    parent = rootItem_->childExists( _object->parent()->id() );

  if (parent != 0){
    QModelIndex parentIndex = getModelIndex(parent, 0);

    beginInsertRows(parentIndex, parent->childCount(), parent->childCount()); //insert at the bottom

      TreeItem* item = new TreeItem( _object->id(), _object->name(), _object->dataType(), parent);

      parent->appendChild( item );

    endInsertRows();
Jan Möbius's avatar
 
Jan Möbius committed
474
  }
Dirk Wilden's avatar
Dirk Wilden committed
475

Dirk Wilden's avatar
Dirk Wilden committed
476
  objectChanged( _object->id() );
Jan Möbius's avatar
 
Jan Möbius committed
477
478
}

Dirk Wilden's avatar
Dirk Wilden committed
479
/** \brief The object with the given id has been deleted. delete it from the internal tree
480
 *
Dirk Wilden's avatar
Dirk Wilden committed
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
 * @param id_ id of the object
 */
void TreeModel::objectDeleted(int _id){

  TreeItem* item = rootItem_->childExists(_id);

  if ( item != 0 && !isRoot(item) ){

    QModelIndex itemIndex   = getModelIndex(item, 0);
    QModelIndex parentIndex = itemIndex.parent();

    beginRemoveRows( parentIndex, itemIndex.row(), itemIndex.row() );

    item->parent()->removeChild(item);
    item->deleteSubtree();

    delete item;

    endRemoveRows();
  }
}

//******************************************************************************

/** \brief move the item to a new parent
506
 *
Dirk Wilden's avatar
Dirk Wilden committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
 * @param _item the item
 * @param _parent new parent
 */
void TreeModel::moveItem(TreeItem* _item, TreeItem* _parent ){

  QModelIndex itemIndex   = getModelIndex(_item, 0);
  QModelIndex oldParentIndex = itemIndex.parent();
  QModelIndex newParentIndex = getModelIndex(_parent, 0);

  //delete everything at the old location
  beginRemoveRows( oldParentIndex, itemIndex.row(), itemIndex.row() );

    _item->parent()->removeChild(_item);

  endRemoveRows();

  //insert it at the new location
524
  beginInsertRows(newParentIndex, _parent->childCount(), _parent->childCount() ); //insert at the bottom
Dirk Wilden's avatar
Dirk Wilden committed
525
526
527
528
    _item->setParent( _parent );
    _parent->appendChild( _item );
  endInsertRows();

529
  emit layoutChanged();
Dirk Wilden's avatar
Dirk Wilden committed
530
}
Dirk Wilden's avatar
Dirk Wilden committed
531
532
533
534

//******************************************************************************

/** \brief Return item at given index
535
 *
Dirk Wilden's avatar
Dirk Wilden committed
536
537
538
 * @param index a ModelIndex
 * @return item at given index
 */
Dirk Wilden's avatar
Dirk Wilden committed
539
TreeItem* TreeModel::getItem(const QModelIndex &index) const
Jan Möbius's avatar
 
Jan Möbius committed
540
541
{
    if (index.isValid()) {
Dirk Wilden's avatar
Dirk Wilden committed
542
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
Jan Möbius's avatar
 
Jan Möbius committed
543
544
545
546
547
        if (item) return item;
    }
    return rootItem_;
}

Dirk Wilden's avatar
Dirk Wilden committed
548

Dirk Wilden's avatar
Dirk Wilden committed
549
550
551
//******************************************************************************

/** \brief Return item-name at given index
552
 *
Dirk Wilden's avatar
Dirk Wilden committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
 * @param index a ModelIndex
 * @return name of the item at given index
 */
QString TreeModel::itemName(const QModelIndex &index) const
{
    if (index.isValid()) {
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
        if (item)
          return item->name();
    }
    return "not found";
}

//******************************************************************************

/** \brief Return item-id at given index
569
 *
Dirk Wilden's avatar
Dirk Wilden committed
570
571
572
573
574
575
576
577
578
579
580
581
582
 * @param index a ModelIndex
 * @return item-id at given index
 */
int TreeModel::itemId(const QModelIndex &index) const
{
    if (index.isValid()) {
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
        if (item)
          return item->id();
    }
    return -1;
}

Dirk Wilden's avatar
Dirk Wilden committed
583
584
585
//******************************************************************************

/** \brief Return index of given item
Dirk Wilden's avatar
Dirk Wilden committed
586
587
588
 *
 * Warning: Only use this function if you know that all ModelIndices are created
 *
Dirk Wilden's avatar
Dirk Wilden committed
589
590
591
592
 * @param _object an object
 * @param _column a column
 * @return index of object and column
 */
Dirk Wilden's avatar
Dirk Wilden committed
593
QModelIndex TreeModel::getModelIndex(TreeItem* _object, int _column ){
594

Dirk Wilden's avatar
Dirk Wilden committed
595
596
597
598
599
  // root item gets an invalid QModelIndex
  if ( _object == rootItem_ )
    return QModelIndex();

  QModelIndex index = createIndex(_object->row(), _column, _object);
600

Dirk Wilden's avatar
Dirk Wilden committed
601
  return index;
602
603
604
}


Dirk Wilden's avatar
Dirk Wilden committed
605
606
//******************************************************************************

Dirk Wilden's avatar
Dirk Wilden committed
607
/** \brief Recursively update a column up to the root of the tree
Dirk Wilden's avatar
Dirk Wilden committed
608
609
 *
 * @param _item item to start with
Dirk Wilden's avatar
Dirk Wilden committed
610
 */
Dirk Wilden's avatar
Dirk Wilden committed
611
void TreeModel::propagateUpwards(TreeItem* _item, int _column, bool _value ){
612

Dirk Wilden's avatar
Dirk Wilden committed
613
  if ( isRoot(_item) || (!_item->isGroup()) )
614
615
    return;

Dirk Wilden's avatar
Dirk Wilden committed
616
617
  if (_column == 1){ //visibility
    _item->visible( _value );
Dirk Wilden's avatar
Dirk Wilden committed
618

Dirk Wilden's avatar
Dirk Wilden committed
619
620
621
    //the whole row has to be updated because of the grey background-color
    QModelIndex index0 = getModelIndex(_item,0);
    QModelIndex index1 = getModelIndex(_item,3);
Dirk Wilden's avatar
Dirk Wilden committed
622

Dirk Wilden's avatar
Dirk Wilden committed
623
    emit dataChanged( index0, index1);
624

Dirk Wilden's avatar
Dirk Wilden committed
625
  } else {
Dirk Wilden's avatar
Dirk Wilden committed
626

Dirk Wilden's avatar
Dirk Wilden committed
627
628
    QModelIndex index = getModelIndex(_item,_column);
    emit dataChanged(index, index);
Dirk Wilden's avatar
Dirk Wilden committed
629
630
  }

Dirk Wilden's avatar
Dirk Wilden committed
631
  propagateUpwards( _item->parent(), _column, _value );
Dirk Wilden's avatar
Dirk Wilden committed
632
}
Dirk Wilden's avatar
Dirk Wilden committed
633
634
635

//******************************************************************************

Dirk Wilden's avatar
Dirk Wilden committed
636
/** \brief Recursively update a column up to the root of the tree
Dirk Wilden's avatar
Dirk Wilden committed
637
638
 *
 * @param _item item to start with
Dirk Wilden's avatar
Dirk Wilden committed
639
 */
Dirk Wilden's avatar
Dirk Wilden committed
640
void TreeModel::propagateDownwards(TreeItem* _item, int _column ){
641

Dirk Wilden's avatar
Dirk Wilden committed
642
  for (int i=0; i < _item->childCount(); i++){
643

Dirk Wilden's avatar
Dirk Wilden committed
644
    TreeItem* current = _item->child(i);
645

Dirk Wilden's avatar
Dirk Wilden committed
646
    bool changed = false;
647

Dirk Wilden's avatar
Dirk Wilden committed
648
    switch ( _column ){
649

Dirk Wilden's avatar
Dirk Wilden committed
650
651
      case 1: //VISIBILTY

Dirk Wilden's avatar
Dirk Wilden committed
652
        if ( current->visible() != _item->visible() ){
Dirk Wilden's avatar
Dirk Wilden committed
653

Dirk Wilden's avatar
Dirk Wilden committed
654
          current->visible( _item->visible() );
Dirk Wilden's avatar
Dirk Wilden committed
655
656
657
658
659
          changed = true;
        }
        break;

      case 2: //SOURCE
660

Dirk Wilden's avatar
Dirk Wilden committed
661
        if ( current->source() != _item->source() ){
Dirk Wilden's avatar
Dirk Wilden committed
662

Dirk Wilden's avatar
Dirk Wilden committed
663
          current->source( _item->source() );
Dirk Wilden's avatar
Dirk Wilden committed
664
665
666
667
668
669
          changed = true;
        }
        break;

      case 3: //TARGET

Dirk Wilden's avatar
Dirk Wilden committed
670
        if ( current->target() != _item->target() ){
Dirk Wilden's avatar
Dirk Wilden committed
671

Dirk Wilden's avatar
Dirk Wilden committed
672
          current->target( _item->target() );
Dirk Wilden's avatar
Dirk Wilden committed
673
674
675
676
677
678
679
680
          changed = true;
        }
        break;

      default:
        break;
    }

Dirk Wilden's avatar
Dirk Wilden committed
681
682
683
684
    if (changed){
      QModelIndex index = getModelIndex(current,_column);
      emit dataChanged(index, index);
    }
Dirk Wilden's avatar
Dirk Wilden committed
685

Dirk Wilden's avatar
Dirk Wilden committed
686
687
    if ( current->isGroup() )
      propagateDownwards(current, _column);
Dirk Wilden's avatar
Dirk Wilden committed
688
689
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
690
691
692
693

//******************************************************************************

/** \brief Set Data at 'index' to 'value'
694
 *
Dirk Wilden's avatar
Dirk Wilden committed
695
696
697
698
699
 * @param index a ModelIndex defining the positin in the model
 * @param value the new value
 * @param  unused
 * @return return if the data was set successfully
 */
Dirk Wilden's avatar
Dirk Wilden committed
700
bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
Jan Möbius's avatar
 
Jan Möbius committed
701
{
702

Dirk Wilden's avatar
Dirk Wilden committed
703
  emit dataChangedInside( itemId(index), index.column(), value );
Dirk Wilden's avatar
Dirk Wilden committed
704

Jan Möbius's avatar
 
Jan Möbius committed
705
706
707
  return true;
}

Dirk Wilden's avatar
Dirk Wilden committed
708
709
710
711

//******************************************************************************

/** \brief return if an object is equal to the root object
712
 *
Dirk Wilden's avatar
Dirk Wilden committed
713
714
715
 * @param _item the item to be checked
 * @return is it the root object?
 */
Dirk Wilden's avatar
Dirk Wilden committed
716
bool TreeModel::isRoot(TreeItem * _item) {
717
  return ( _item == rootItem_ );
Jan Möbius's avatar
 
Jan Möbius committed
718
719
}

Dirk Wilden's avatar
Dirk Wilden committed
720
721
722
/*******************************************************************************
        drag & drop stuff
 *******************************************************************************/
723

Dirk Wilden's avatar
Dirk Wilden committed
724
/** \brief return the supported drop actions
725
 *
Dirk Wilden's avatar
Dirk Wilden committed
726
727
 * @return drop actions
 */
728
729
730
731
732
Qt::DropActions TreeModel::supportedDropActions() const
{
    return /*Qt::CopyAction |*/ Qt::MoveAction;
}

Dirk Wilden's avatar
Dirk Wilden committed
733
734
735
736

//******************************************************************************

/** \brief return the mimeType for drag & drop
737
 *
Dirk Wilden's avatar
Dirk Wilden committed
738
739
 * @return the mimeType
 */
740
741
742
743
744
745
746
QStringList TreeModel::mimeTypes() const
{
    QStringList types;
    types << "DataControl/dragDrop";
    return types;
}

Dirk Wilden's avatar
Dirk Wilden committed
747
748
749
750

//******************************************************************************

/** \brief generate mimeData for given ModelIndexes
751
 *
Dirk Wilden's avatar
Dirk Wilden committed
752
753
754
 * @param indexes list of ModelIndexes
 * @return the mimeData
 */
755
756
757
758
759
760
761
762
763
764
765
766
767
QMimeData* TreeModel::mimeData(const QModelIndexList& indexes) const
{
    QMimeData *mimeData = new QMimeData();
    QByteArray encodedData;

    QDataStream stream(&encodedData, QIODevice::WriteOnly);

    QVector< int > rows;

    foreach (QModelIndex index, indexes) {
        if (index.isValid()) {

          if (!rows.contains( index.row() ) ){
Dirk Wilden's avatar
Dirk Wilden committed
768
            TreeItem *item = getItem(index);
769
770
771
772
773
774
775
776
777
778
779
            stream << item->id();

            rows.push_back( index.row() );
          }
        }
    }

    mimeData->setData("DataControl/dragDrop", encodedData);
    return mimeData;
}

Dirk Wilden's avatar
Dirk Wilden committed
780
781
782
783

//******************************************************************************

/** \brief this is called when mimeData is dropped
784
 *
Dirk Wilden's avatar
Dirk Wilden committed
785
786
787
 * @param data the dropped data
 * @param action the definition of the dropAction which occured
 * @param unused
788
789
 * @param unused
 * @param parent parent under which the drop occurred
Dirk Wilden's avatar
Dirk Wilden committed
790
791
 * @return returns if the drop was sucessful
 */
792
793
bool TreeModel::dropMimeData(const QMimeData *data,
     Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex &parent)
Dirk Wilden's avatar
Dirk Wilden committed
794
795
796
{
  if (action == Qt::IgnoreAction)
      return true;
797

Dirk Wilden's avatar
Dirk Wilden committed
798
799
  if (!data->hasFormat("DataControl/dragDrop"))
      return false;
800

Dirk Wilden's avatar
Dirk Wilden committed
801
802
  QByteArray encodedData = data->data("DataControl/dragDrop");
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
803

Dirk Wilden's avatar
Dirk Wilden committed
804
  QVector< int > ids;
805

Dirk Wilden's avatar
Dirk Wilden committed
806
807
808
  while (!stream.atEnd()) {
    int id;
    stream >> id;
809

Dirk Wilden's avatar
Dirk Wilden committed
810
811
    ids.push_back( id );
  }
812

Dirk Wilden's avatar
Dirk Wilden committed
813
814
  if (ids.count() == 0)
    return false;
815

Dirk Wilden's avatar
Dirk Wilden committed
816
817
  //get new parent
  TreeItem *newParent = getItem(parent);
818

Dirk Wilden's avatar
Dirk Wilden committed
819
820
821
822
823
824
825
826
  if ( newParent == 0 || !newParent->isGroup() )
    return false;

  //and move all objects
  for (int i = 0; i < ids.count(); i++){
    //tell the DataControlPlugin to move the corresponding BaseObject
    emit moveBaseObject( ids[i], newParent->id()  );
  }
827

Dirk Wilden's avatar
Dirk Wilden committed
828
  return true;
Dirk Wilden's avatar
Dirk Wilden committed
829

830
 }