DataControlPlugin.cc 26.4 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           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/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42

43 44
#include "DataControlPlugin.hh"

Matthias Möller's avatar
Matthias Möller committed
45 46 47 48 49
#if QT_VERSION >= 0x050000 
  #include <QtWidgets>
#else
  #include <QtGui>
#endif
Jan Möbius's avatar
 
Jan Möbius committed
50 51

#include <QLayout>
52
#include <QGridLayout>
Jan Möbius's avatar
 
Jan Möbius committed
53
#include <QItemSelectionModel>
54

Jan Möbius's avatar
 
Jan Möbius committed
55 56 57 58
#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
#include <ACG/Scenegraph/BaseNode.hh>
Dirk Wilden's avatar
Dirk Wilden committed
59
#include <ACG/QtWidgets/QtMaterialDialog.hh>
Jan Möbius's avatar
 
Jan Möbius committed
60 61 62 63 64
#include <QModelIndexList>

#include <queue>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
65
#include <OpenFlipper/common/GlobalOptions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
66

67 68
#include <ObjectTypes/Light/Light.hh>

Dirk Wilden's avatar
Dirk Wilden committed
69
//******************************************************************************
Dirk Wilden's avatar
Dirk Wilden committed
70

Jan Möbius's avatar
Jan Möbius committed
71 72 73
const ACG::Vec4f base_color (0.0f,0.0f,0.5f,1.0f);
const ACG::Vec4f source_color (0.5f,0.0f,0.0f,1.0f);
const ACG::Vec4f target_color (0.0f,0.5f,0.2f,1.0f);
74 75 76

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

Jan Möbius's avatar
Jan Möbius committed
77 78 79 80 81 82 83 84 85 86 87 88 89


DataControlPlugin::DataControlPlugin() :
        tool_(0),
        toolIcon_(0),
        MeshDialogLayout_(0),
        objectList_(0),
        locked(false),
        model_(0),
        view_(0),
        viewHeader_(0),
        onlyDown_(0),
        onlyUp_(0),
90
        columnFromGUI_(-1),
Jan Möbius's avatar
Jan Möbius committed
91 92 93 94 95 96 97 98 99
        headerPopupType_(0),
        targetAction_(0),
        sourceAction_(0),
        removeAction_(0)
{

}


Dirk Wilden's avatar
Dirk Wilden committed
100
/** \brief Plugin initialization
101
 *
Dirk Wilden's avatar
Dirk Wilden committed
102
 */
Jan Möbius's avatar
 
Jan Möbius committed
103
void DataControlPlugin::pluginsInitialized() {
104

Jan Möbius's avatar
 
Jan Möbius committed
105 106 107
  //set the slot descriptions
  setDescriptions();

108 109 110
  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
 
Dirk Wilden committed
111
  QMenu* contextMenu = new QMenu("Object");
112

Jan Möbius's avatar
 
Jan Möbius committed
113
  //Target Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
114 115
  QIcon icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-hide-object.png");
  QAction* hideAction = new QAction(icon, tr("&Hide"), this);
Jan Möbius's avatar
 
Jan Möbius committed
116 117 118
  hideAction->setStatusTip(tr("Hide object"));
  connect(hideAction, SIGNAL(triggered()), this, SLOT(slotContextMenuHide()) );
  contextMenu->addAction(hideAction);
119

Jan Möbius's avatar
 
Jan Möbius committed
120
  //Target Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
121 122
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-target-object.png");
  targetAction_ = new QAction(icon, tr("&Target"), this);
Jan Möbius's avatar
 
Jan Möbius committed
123 124 125 126
  targetAction_->setCheckable(true);
  targetAction_->setStatusTip(tr("Set object as target"));
  connect(targetAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuTarget()) );
  contextMenu->addAction(targetAction_);
127

Jan Möbius's avatar
 
Jan Möbius committed
128
  //Source Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
129 130
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-source-object.png");
  sourceAction_ = new QAction(icon, tr("&Source"), this);
Jan Möbius's avatar
 
Jan Möbius committed
131 132 133
  sourceAction_->setCheckable(true);
  sourceAction_->setStatusTip(tr("Set object as source"));
  connect(sourceAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuSource()) );
134 135
  contextMenu->addAction(sourceAction_);

Dirk Wilden's avatar
 
Dirk Wilden committed
136 137 138 139 140 141 142 143 144 145
  contextMenu->addSeparator();
  
  //Remove Objects
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-delete-item.png");
  removeAction_ = new QAction(icon, tr("&Remove"), this);
  removeAction_->setCheckable(false);
  removeAction_->setStatusTip(tr("Remove object"));
  connect(removeAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuRemove()) );
  contextMenu->addAction(removeAction_);
  
Jan Möbius's avatar
 
Jan Möbius committed
146
  emit addContextMenuItem(contextMenu->menuAction() , DATA_ALL , CONTEXTOBJECTMENU);
147

Dirk Wilden's avatar
 
Dirk Wilden committed
148
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-material.png");
Jan Möbius's avatar
Jan Möbius committed
149
  QAction* material = new QAction(icon, tr("Material Properties"), 0);
150 151
  connect (material, SIGNAL( triggered() ), this, SLOT ( slotMaterialProperties() ));
  emit addContextMenuItem(material , DATA_ALL , CONTEXTOBJECTMENU);
152

Matthias Möller's avatar
Matthias Möller committed
153 154
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-copyToTargets-material.png");
  QAction* copyMaterial = new QAction(icon, tr("Copy Material Properties to Targeted Objects"), 0);
155 156 157
  connect (copyMaterial, SIGNAL( triggered() ), this, SLOT ( slotCopyMaterialToTargeted() ));
  emit addContextMenuItem(copyMaterial , DATA_ALL , CONTEXTOBJECTMENU);

Matthias Möller's avatar
Matthias Möller committed
158 159
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-copy-material.png");
  QAction* copyMaterialToClipboard = new QAction(icon, tr("Copy Material Properties to Clipboard"), 0);
160 161 162
  connect (copyMaterialToClipboard, SIGNAL( triggered() ), this, SLOT ( slotCopyMaterialToClipboard() ));
  emit addContextMenuItem(copyMaterialToClipboard , DATA_ALL , CONTEXTOBJECTMENU);

Matthias Möller's avatar
Matthias Möller committed
163 164
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-paste-material.png");
  QAction* pasteMaterialFromClipboard = new QAction(icon, tr("Paste Material Properties from Clipboard"), 0);
165 166 167
  connect (pasteMaterialFromClipboard, SIGNAL( triggered() ), this, SLOT ( slotPasteMaterialFromClipboard() ));
  emit addContextMenuItem(pasteMaterialFromClipboard , DATA_ALL , CONTEXTOBJECTMENU);

168 169
  PluginFunctions::setDefaultViewObjectMarker (&objectMarker);
  PluginFunctions::setViewObjectMarker (&objectMarker);
170 171
  
  connect(tool_->lightSources, SIGNAL(stateChanged(int)), this, SLOT(slotShowLightSources(int)));
172 173
  //update light visibility, if layout was changed
  connect(model_, SIGNAL(layoutChanged ()), this, SLOT(slotShowLightSources()) );
174
  connect(model_, SIGNAL(rowsRemoved(const QModelIndex& , int , int )), this, SLOT(slotShowLightSources()));
Jan Möbius's avatar
 
Jan Möbius committed
175 176
}

Dirk Wilden's avatar
Dirk Wilden committed
177 178 179

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

180
void DataControlPlugin::initializePlugin()
Jan Möbius's avatar
 
Jan Möbius committed
181
{
182 183 184 185 186 187 188 189 190 191 192 193
  if ( ! OpenFlipper::Options::gui())
    return;

  tool_ = new DatacontrolToolboxWidget();
  connect( tool_ , SIGNAL( keyEvent( QKeyEvent* ) ),
      this  , SLOT(slotKeyEvent ( QKeyEvent* ) ));
  QSize size(300, 300);
  tool_->resize(size);

  model_ = new TreeModel( );

  view_ = tool_->treeView;
194

195
  tool_->treeView->setModel(model_);
196

197 198 199
  view_->QTreeView::resizeColumnToContents(1);
  view_->QTreeView::resizeColumnToContents(2);
  view_->QTreeView::resizeColumnToContents(3);
200

201

202 203
  connect( model_,SIGNAL(dataChangedInside(int,int,const QVariant&) ),
      this,  SLOT(    slotDataChanged(int,int,const QVariant&)) );
204

205 206
  connect( model_,SIGNAL(   moveBaseObject(int,int) ),
      this,  SLOT( slotMoveBaseObject(int,int) ) );
Dirk Wilden's avatar
Dirk Wilden committed
207

208 209
  connect( view_,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
      this,SLOT(slotCustomContextMenuRequested ( const QPoint & ) ));
210

211 212 213 214 215 216
  connect( tool_->notSelected, SIGNAL(stateChanged ( int ) ),
      this, SLOT (slotBoundingBoxChange ( ) ));
  connect( tool_->sourceSelected, SIGNAL(stateChanged ( int ) ),
      this, SLOT (slotBoundingBoxChange ( ) ));
  connect( tool_->targetSelected, SIGNAL(stateChanged ( int ) ),
      this, SLOT (slotBoundingBoxChange ( ) ));
217 218


219 220
  viewHeader_ = tool_->treeView->header();
  viewHeader_->setContextMenuPolicy(Qt::CustomContextMenu);
221

222 223 224
  // connect the slot for the context menu
  connect( viewHeader_, SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
      this,        SLOT(slotHeaderCustomContextMenuRequested ( const QPoint & ) ));
225

226
  toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-toolbox.png");
227

228
  emit addToolbox("Data Control", tool_, toolIcon_);
229

230 231 232 233 234 235 236 237
  QIcon icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-boundingBox.png");
  tool_->boundingBoxBtn->setIcon( icon );
  icon = QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-hide-object.png");
  tool_->visibleDataBtn->setIcon( icon );

  //hide additional boxes
  tool_->visibleDataBtn->setChecked(false);
  tool_->boundingBoxBtn->setChecked(false);
Jan Möbius's avatar
 
Jan Möbius committed
238 239 240
}


Dirk Wilden's avatar
Dirk Wilden committed
241
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
242

Dirk Wilden's avatar
Dirk Wilden committed
243
/** \brief update drawing of objects when the active object changed
244
 *
Dirk Wilden's avatar
Dirk Wilden committed
245 246 247
 */
void DataControlPlugin::slotObjectSelectionChanged( int _identifier )
{
248 249 250
  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
251 252
  BaseObjectData* obj = 0;

253
  if ( PluginFunctions::getObject( _identifier, obj) )
254 255
    updateBoundingBox (obj);

Dirk Wilden's avatar
Dirk Wilden committed
256
  BaseObject* object = 0;
257 258
  if ( !PluginFunctions::getObject( _identifier, object) )
    return;
Dirk Wilden's avatar
Dirk Wilden committed
259

260 261 262 263 264 265 266
  //check for changes
  int column = columnFromGUI_;//if GUI has set the column, use this information
  if (column == -1)//otherwise, try to get the column
  {
    TreeItem* item = model_->getItem(_identifier);
    if ( !item )
      return;
267

268 269 270 271 272 273 274
    if (item->source() != object->source())
      column = 2;
    else if (item->target() != object->target())
      column = 3;
    else
      return;//could not decide, which column needs a update, discard
  }
275

Dirk Wilden's avatar
Dirk Wilden committed
276

277
  model_->objectChanged( _identifier );
278

279 280
  // if we are allowed to propagate down
  if ( onlyUp_ == 0 ){
281

282
    onlyDown_++;
283

284 285
    if ( object->isGroup() )
      propagateDownwards(object, column); // 2: source 3: target
286

287 288
    onlyDown_--;
  }
289

290 291
  // if we are allowed to propagate up
  if ( onlyDown_ == 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
292

293 294 295 296 297
    onlyUp_++;

    propagateUpwards(object->parent(), column); // 2: source 3: target

    onlyUp_--;
298
  }
Dirk Wilden's avatar
Dirk Wilden committed
299 300 301 302 303 304
}


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

/** \brief Update the model if the visibility of an object changed
305
 *
Dirk Wilden's avatar
Dirk Wilden committed
306 307
 * @param _identifier id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
308
void DataControlPlugin::slotVisibilityChanged( int _identifier ){
309

310 311 312
  if ( ! OpenFlipper::Options::gui())
    return;

313 314 315 316 317 318
  // if onlyUp_ > 0 --> _identifier is a group and the selection
  // does not have to be applied
  if (onlyUp_ == 0){
    //inform the model
    model_->objectChanged( _identifier );
  }
Dirk Wilden's avatar
Dirk Wilden committed
319 320 321 322 323 324

  //check for changes in the tree
  BaseObject* obj = 0;

  if ( PluginFunctions::getObject( _identifier, obj) ){

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    // if we are allowed to propagate up
    if ( onlyDown_ == 0 ){
    
      onlyUp_++;

      propagateUpwards(obj->parent(), 1); // 1 = visibilty

      onlyUp_--;  

    }

    // if we are allowed to propagate down
    if ( onlyUp_ == 0 ){

      onlyDown_++;
      
      if ( obj->isGroup() )
        propagateDownwards(obj, 1); // 1 = visibilty

      onlyDown_--;
    }
Dirk Wilden's avatar
Dirk Wilden committed
346 347
  }

348 349 350 351 352
  BaseObjectData* object = 0;

  if ( PluginFunctions::getObject( _identifier, object) )
    updateBoundingBox (object);

Jan Möbius's avatar
 
Jan Möbius committed
353 354 355
}


Dirk Wilden's avatar
Dirk Wilden committed
356
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
357

Dirk Wilden's avatar
Dirk Wilden committed
358
/** \brief Update the model if properties of an object changed
359
 *
Dirk Wilden's avatar
Dirk Wilden committed
360
 * @param _identifier id of an object
Dirk Wilden's avatar
Dirk Wilden committed
361
 */
Dirk Wilden's avatar
Dirk Wilden committed
362
void DataControlPlugin::slotObjectPropertiesChanged( int _identifier ){
363 364 365 366

  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
367 368
  model_->objectChanged( _identifier );
}
Jan Möbius's avatar
 
Jan Möbius committed
369

Dirk Wilden's avatar
Dirk Wilden committed
370

Dirk Wilden's avatar
Dirk Wilden committed
371 372 373
//******************************************************************************

/** \brief Update the model if a file has been opened
374
 *
Dirk Wilden's avatar
Dirk Wilden committed
375 376
 * @param _id id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
377 378
void DataControlPlugin::fileOpened(int _id){

379 380 381
  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
382 383 384 385
  BaseObject* obj = 0;

  if ( PluginFunctions::getObject(_id, obj) )
    model_->objectAdded(obj);
386 387 388 389 390

  // Only if the added object was a light source, we will traverse the objects!
  if ( obj->dataType() == DATA_LIGHT)
    slotShowLightSources(tool_->lightSources->checkState());

Dirk Wilden's avatar
Dirk Wilden committed
391
  view_->resizeColumnToContents(0);
Dirk Wilden's avatar
Dirk Wilden committed
392 393 394 395 396 397
}


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

/** \brief Update the model if an empty object has been added
398
 *
Dirk Wilden's avatar
Dirk Wilden committed
399 400
 * @param _id id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
401 402 403 404 405 406 407
void DataControlPlugin::addedEmptyObject(int _id){
  fileOpened(_id);
}

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

/** \brief an object was deleted. delete it internally
408
 *
Dirk Wilden's avatar
Dirk Wilden committed
409 410 411
 * @param _id id of the object
 */
void DataControlPlugin::objectDeleted(int _id){
412 413 414 415

  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
416
  model_->objectDeleted(_id);
Jan Möbius's avatar
 
Jan Möbius committed
417 418
}

Dirk Wilden's avatar
Dirk Wilden committed
419 420 421
//******************************************************************************

/** \brief a key event occurred
422
 *
Dirk Wilden's avatar
Dirk Wilden committed
423 424
 * @param _event the event that occurred
 */
Jan Möbius's avatar
 
Jan Möbius committed
425 426 427 428 429
void DataControlPlugin::slotKeyEvent( QKeyEvent* _event )
{

  if ( _event->modifiers() == Qt::ControlModifier ) {
    switch (_event->key()) {
430
      case Qt::Key_A :
Jan Möbius's avatar
 
Jan Möbius committed
431 432 433 434 435 436
          setAllTarget();
        return;
      default:
        return;
    }
  }
437

438 439 440 441 442 443 444
  switch (_event->key()) {
    case Qt::Key_Delete :
        slotPopupRemove();
      return;
    default:
      return;
  }
445

Jan Möbius's avatar
 
Jan Möbius committed
446 447
}

Dirk Wilden's avatar
Dirk Wilden committed
448 449 450 451

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

/** \brief emit the right updates when the model changed
452
 *
Jan Möbius's avatar
Jan Möbius committed
453 454 455
 * @param _id     Object id that was changed
 * @param _column Which column changed
 * @param _value  What is the new value?
Dirk Wilden's avatar
Dirk Wilden committed
456
 */
Dirk Wilden's avatar
Dirk Wilden committed
457
void DataControlPlugin::slotDataChanged ( int _id, int _column, const QVariant& _value)
Jan Möbius's avatar
 
Jan Möbius committed
458
{
459

Dirk Wilden's avatar
Dirk Wilden committed
460 461 462 463
  //get the corresponding baseObject
  BaseObject* obj = 0;
  if ( !PluginFunctions::getObject( _id, obj) )
    return;
464
  
Dirk Wilden's avatar
Dirk Wilden committed
465 466 467 468 469 470 471 472 473 474 475 476 477
  switch ( _column ) {
    // Name
    case 0:
      obj->setName( _value.toString() );
      break;

    // show/hide
    case 1:
      obj->visible( _value.toBool() );
      break;

    // source
    case 2:
478
      columnFromGUI_ = 2;//set information, which information will be changed
Dirk Wilden's avatar
Dirk Wilden committed
479
      obj->source( _value.toBool() );
480
      columnFromGUI_ = -1;
Dirk Wilden's avatar
Dirk Wilden committed
481 482 483 484
      break;

    // target
    case 3:
485
      columnFromGUI_ = 3;//set information, which information will be changed
Dirk Wilden's avatar
Dirk Wilden committed
486
      obj->target( _value.toBool() );
487
      columnFromGUI_ = -1;
Dirk Wilden's avatar
Dirk Wilden committed
488
      break;
Dirk Wilden's avatar
Dirk Wilden committed
489

Dirk Wilden's avatar
Dirk Wilden committed
490 491
    default:
      break;
Jan Möbius's avatar
 
Jan Möbius committed
492 493 494
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
495 496 497

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

Dirk Wilden's avatar
Dirk Wilden committed
498
/** \brief Gets called when an object was moved via drag n drop
499
 *
Jan Möbius's avatar
Jan Möbius committed
500 501
 * @param _id          id of the object
 * @param _newParentId id of the new parent
Dirk Wilden's avatar
Dirk Wilden committed
502
 */
Dirk Wilden's avatar
Dirk Wilden committed
503
void DataControlPlugin::slotMoveBaseObject(int _id, int _newParentId){
Dirk Wilden's avatar
Dirk Wilden committed
504

Dirk Wilden's avatar
Dirk Wilden committed
505
  BaseObject* obj = 0;
Dirk Wilden's avatar
Dirk Wilden committed
506

Dirk Wilden's avatar
Dirk Wilden committed
507 508
  if ( !PluginFunctions::getObject(_id, obj) )
    return;
Jan Möbius's avatar
 
Jan Möbius committed
509

Dirk Wilden's avatar
Dirk Wilden committed
510
  BaseObject* parent = 0;
Dirk Wilden's avatar
Dirk Wilden committed
511

Dirk Wilden's avatar
Dirk Wilden committed
512 513
  if ( !PluginFunctions::getObject(_newParentId, parent) )
    return;
Dirk Wilden's avatar
Dirk Wilden committed
514

Dirk Wilden's avatar
Dirk Wilden committed
515
  BaseObject* oldParent = obj->parent();
Dirk Wilden's avatar
Dirk Wilden committed
516

Dirk Wilden's avatar
Dirk Wilden committed
517 518
  //set new parent
  obj->setParent( parent );
Dirk Wilden's avatar
Dirk Wilden committed
519

Dirk Wilden's avatar
Dirk Wilden committed
520 521 522
  //if oldParent is an empty group -> delete it
  if ( oldParent != PluginFunctions::objectRoot() && oldParent->childCount() == 0 )
    emit deleteObject( oldParent->id() );
Dirk Wilden's avatar
Dirk Wilden committed
523 524 525
}


526 527 528 529 530
//******************************************************************************

void DataControlPlugin::slotShowLightSources( int _state ) {

    int rows = model_->rowCount();
531

532 533 534
    for(int i = 0; i < rows; ++i) {
        TreeItem* item = model_->getItem(model_->index(i,0));
        if(item->dataType() == DATA_LIGHT) {
535 536 537 538
          view_->setRowHidden(i, model_->parent(model_->index(i,0)), !(_state == Qt::Checked));
        }else{
          //always show, if it is not a light
          view_->setRowHidden(i, model_->parent(model_->index(i,0)), false);
539 540 541 542
        }
    }
}

543 544 545 546 547

void DataControlPlugin::slotShowLightSources()
{
  slotShowLightSources( tool_->lightSources->checkState() );
}
Dirk Wilden's avatar
Dirk Wilden committed
548 549 550
//******************************************************************************

/** \brief Load Groups from ini file
551
 *
Dirk Wilden's avatar
Dirk Wilden committed
552 553
 * @param _ini an ini file
 */
Jan Möbius's avatar
 
Jan Möbius committed
554
void DataControlPlugin::loadIniFileOptionsLast( INIFile& _ini ) {
555

556
  if ( _ini.section_exists( "BoundingBox" ) && OpenFlipper::Options::gui() )
557 558 559 560 561 562 563 564 565 566
  {
    bool value;
    if (_ini.get_entry(value, "BoundingBox","notSelected"))
      tool_->notSelected->setChecked (value);
    if (_ini.get_entry(value, "BoundingBox","sourceSelected"))
      tool_->sourceSelected->setChecked (value);
    if (_ini.get_entry(value, "BoundingBox","targetSelected"))
      tool_->targetSelected->setChecked (value);
  }

567
  if ( !_ini.section_exists( "Groups" ) )
Jan Möbius's avatar
 
Jan Möbius committed
568
    return;
569

Jan Möbius's avatar
 
Jan Möbius committed
570 571
  // Names of all groups
  QStringList groupNames;
572

Jan Möbius's avatar
 
Jan Möbius committed
573 574
  // names of the primary groups
  QStringList rootGroup;
575

Jan Möbius's avatar
 
Jan Möbius committed
576 577
  // Get the list of group names to the file
  _ini.get_entry(groupNames,"Groups","groups");
578

Jan Möbius's avatar
 
Jan Möbius committed
579 580
  // Get the primary group names to the file
  _ini.get_entry(rootGroup,"Groups","rootGroup");
581

Dirk Wilden's avatar
Dirk Wilden committed
582 583 584
  //list of groups
  QVector< BaseObject* > groups;

Jan Möbius's avatar
 
Jan Möbius committed
585 586 587 588
  // Go over one level of the groups
  while ( rootGroup.size() > 0 ) {
    QString current = rootGroup[0];
    rootGroup.removeFirst();
589

Jan Möbius's avatar
 
Jan Möbius committed
590 591
    QStringList groupChildren;
    QStringList elementChildren;
592

Jan Möbius's avatar
 
Jan Möbius committed
593 594
    _ini.get_entry(elementChildren ,current,"children");
    _ini.get_entry(groupChildren ,current,"subgroups");
595

Jan Möbius's avatar
 
Jan Möbius committed
596 597 598 599 600 601
    // if we get a parent item, scan the tree for it or use the root node otherwise
    BaseObject* parentItem;
    QString parentName;
    if ( _ini.get_entry(parentName,current,"parent") ) {
      parentItem = PluginFunctions::objectRoot()->childExists(parentName);
      if ( parentItem == 0 )
602 603 604 605 606 607
        parentItem = PluginFunctions::objectRoot();
    } else
      parentItem = PluginFunctions::objectRoot();

    rootGroup << groupChildren;

Jan Möbius's avatar
 
Jan Möbius committed
608 609
    // check if this group already exists
    BaseObject* group =  PluginFunctions::objectRoot()->childExists( current );
610

Jan Möbius's avatar
 
Jan Möbius committed
611 612
    // group does not exist
    if ( !group ) {
613

614 615
      int groupId = addEmptyGroup(current, parentItem->id());
      PluginFunctions::getObject(groupId, group);
Dirk Wilden's avatar
Dirk Wilden committed
616 617 618 619 620 621 622 623

      // in the groups vector we only need the lowest groups
      // because they are used recursively
      int p = groups.indexOf( group->parent() );
      if ( p > -1 )
        groups.remove( p );

      groups.push_back( group );
Jan Möbius's avatar
 
Jan Möbius committed
624
    }
625

Jan Möbius's avatar
 
Jan Möbius committed
626 627 628 629 630 631 632 633 634 635
    // process children
    for ( int i = 0 ; i < elementChildren.size() ; ++i ) {
      BaseObject* childItem =  PluginFunctions::objectRoot()->childExists( elementChildren[i] );
      if ( childItem ) {
        childItem->setParent(group);
      }
    }
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
636 637 638 639

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

/** \brief Save groups to ini file
640
 *
Dirk Wilden's avatar
Dirk Wilden committed
641 642
 * @param _ini an ini file
 */
Jan Möbius's avatar
 
Jan Möbius committed
643
void DataControlPlugin::saveIniFileOptions( INIFile& _ini ) {
644

Jan Möbius's avatar
 
Jan Möbius committed
645 646
  std::queue< BaseObject* > children;
  children.push( PluginFunctions::objectRoot() );
647

Jan Möbius's avatar
 
Jan Möbius committed
648
  std::vector< BaseObject* > groups;
649

Jan Möbius's avatar
 
Jan Möbius committed
650
  // Get all groups from the tree
Jan Möbius's avatar
Jan Möbius committed
651
  while ( ! children.empty() ) {
Jan Möbius's avatar
 
Jan Möbius committed
652 653
    BaseObject* item = children.front();
    children.pop();
654 655 656 657 658

    for ( int i = 0 ; i < item->childCount(); ++i )
      if ( item->child(i)->dataType(DATA_GROUP))
        children.push( item->child(i) );

Jan Möbius's avatar
 
Jan Möbius committed
659 660 661
    if ( item->dataType(DATA_GROUP) && (item != PluginFunctions::objectRoot() ) )
      groups.push_back(item);
  }
662

Jan Möbius's avatar
 
Jan Möbius committed
663 664
  // Names of all groups
  QStringList groupNames;
665

Jan Möbius's avatar
 
Jan Möbius committed
666 667
  // names of the primary groups
  QStringList rootGroup;
668

Jan Möbius's avatar
 
Jan Möbius committed
669
  for ( uint i = 0 ; i < groups.size() ; ++i ) {
670 671
    groupNames.push_back( groups[i]->name() );

Jan Möbius's avatar
 
Jan Möbius committed
672
    _ini.add_entry(groups[i]->name(),"groupname",groups[i]->name());
673 674

    // write the name of the parent
Jan Möbius's avatar
 
Jan Möbius committed
675 676
    if ( ( groups[i]->parent() != 0 ) && ( groups[i]->parent() != PluginFunctions::objectRoot() ) )
      _ini.add_entry(groups[i]->name(),"parent",groups[i]->parent()->name());
677

Jan Möbius's avatar
 
Jan Möbius committed
678 679
    if ( groups[i]->parent() == PluginFunctions::objectRoot() )
      rootGroup.push_back( groups[i]->name() );
680

Jan Möbius's avatar
 
Jan Möbius committed
681 682 683 684 685 686 687 688 689
    // Write a list of this groups children
    QStringList groupchildren;
    QStringList elementchildren;
    for ( int j = 0 ; j < groups[i]->childCount(); ++j ) {
      if  ( groups[i]->child(j)->dataType(DATA_GROUP) )
        groupchildren.push_back( groups[i]->child(j)->name() );
      else
        elementchildren.push_back( groups[i]->child(j)->name() );
    }
690

Jan Möbius's avatar
 
Jan Möbius committed
691 692 693
    _ini.add_entry(groups[i]->name(),"subgroups",groupchildren);
    _ini.add_entry(groups[i]->name(),"children",elementchildren);
  }
694

Jan Möbius's avatar
 
Jan Möbius committed
695 696
  // Write the list of group names to the file
  _ini.add_entry("Groups","groups",groupNames);
697

Jan Möbius's avatar
 
Jan Möbius committed
698 699
  // Write the primary group names to the file
  _ini.add_entry("Groups","rootGroup",rootGroup);
700

701 702 703 704 705 706
  if ( OpenFlipper::Options::gui() ) {

    _ini.add_entry("BoundingBox","notSelected",tool_->notSelected->isChecked ());
    _ini.add_entry("BoundingBox","sourceSelected",tool_->sourceSelected->isChecked ());
    _ini.add_entry("BoundingBox","targetSelected",tool_->targetSelected->isChecked ());
  }
707

Jan Möbius's avatar
 
Jan Möbius committed
708 709 710
}


Dirk Wilden's avatar
Dirk Wilden committed
711 712 713
//******************************************************************************

/** \brief Recursively update a column up to the root of the tree
714
 *
Jan Möbius's avatar
Jan Möbius committed
715 716
 * @param _obj    Object to start with
 * @param _column Column to propagate
Dirk Wilden's avatar
Dirk Wilden committed
717 718 719
 */
void DataControlPlugin::propagateUpwards(BaseObject* _obj, int _column ){

720
  
Dirk Wilden's avatar
Dirk Wilden committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
  if ( _obj == PluginFunctions::objectRoot() || (!_obj->isGroup()) )
    return;

  QList< BaseObject* > children = _obj->getLeafs();
  bool changed = false;
  bool value    = false;


  switch ( _column ){

    case 1: //VISIBILTY

      for (int i=0; i < children.size(); i++)
        value |= children[i]->visible();

      _obj->visible( value );

      changed = true;

      break;

742
    case 2: //SOURCE
Dirk Wilden's avatar
Dirk Wilden committed
743 744 745 746 747 748 749 750 751

      for (int i=0; i < children.size(); i++){
        value  |= children[i]->source();
      }

      if (_obj->source() != value){
        _obj->source( value );
        changed = true;
      }
752
      break;
Dirk Wilden's avatar
Dirk Wilden committed
753

754 755 756 757 758 759 760 761
    case 3: //TARGET

      for (int i=0; i < children.size(); i++){
        value  |= children[i]->target();
      }

      if (_obj->target() != value){
        _obj->target( value );
Dirk Wilden's avatar
Dirk Wilden committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
        changed = true;
      }

      break;

    default:
      break;
  }

  if ( changed )
    propagateUpwards( _obj->parent(), _column );
}

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

/** \brief Recursively update a column up to the root of the tree
778
 *
Jan Möbius's avatar
Jan Möbius committed
779 780
 * @param _obj    object to start with
 * @param _column Column to be propagated
Dirk Wilden's avatar
Dirk Wilden committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
 */
void DataControlPlugin::propagateDownwards(BaseObject* _obj, int _column ){

  for (int i=0; i < _obj->childCount(); i++){

    BaseObject* current = _obj->child(i);

    switch ( _column ){

      case 1: //VISIBILTY

        if ( current->visible() != _obj->visible() ){

          current->visible( _obj->visible() );
        }
        break;

798
      case 2: //SOURCE
Dirk Wilden's avatar
Dirk Wilden committed
799 800 801 802

        if ( current->source() != _obj->source() ){
          current->source( _obj->source() );
        }
803 804 805
        break;

      case 3: //TARGET
Dirk Wilden's avatar
Dirk Wilden committed
806 807 808 809 810 811 812 813 814 815 816

        if ( current->target() != _obj->target() ){
          current->target( _obj->target() );
        }

        break;

      default:
        break;
    }

817
    if ( current->isGroup() ){
Dirk Wilden's avatar
Dirk Wilden committed
818 819 820 821 822 823
      propagateDownwards(current, _column);

    }
  }
}

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
//******************************************************************************

/** \brief Bounding box visibility selection changed
 */
void DataControlPlugin::slotBoundingBoxChange( )
{
  for (PluginFunctions::ObjectIterator o_it; o_it != PluginFunctions::objectsEnd(); ++o_it)  {
    updateBoundingBox (o_it);
  }

  emit updateView();
}

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

/** \brief Update state of bounding box node
 *
 * @param _obj object
 */
void DataControlPlugin::updateBoundingBox(BaseObjectData* _obj)
{
  if (tool_->notSelected->isChecked () ||
      (_obj->source () && tool_->sourceSelected->isChecked ()) ||
      (_obj->target () && tool_->targetSelected->isChecked ()))
  {
    _obj->boundingBoxNode()->set_status( ACG::SceneGraph::BaseNode::Active );

    ACG::Vec4f color = base_color;

    if (_obj->source () && tool_->sourceSelected->isChecked ())
      color += source_color;

    if (_obj->target () && tool_->targetSelected->isChecked ())
      color += target_color;

    _obj->boundingBoxNode()->set_base_color (color);
  }
  else
    _obj->boundingBoxNode()->set_status( ACG::SceneGraph::TranslationManipulatorNode::HideNode );

}

//******************************************************************************
/** \brief Save settings before application is closed
 *
 * @param _ini reference to ini file
 */
void DataControlPlugin::saveOnExit(INIFile& _ini){
872 873 874
  _ini.add_entry("BoundingBox","notSelected",tool_->notSelected->isChecked ());
  _ini.add_entry("BoundingBox","sourceSelected",tool_->sourceSelected->isChecked ());
  _ini.add_entry("BoundingBox","targetSelected",tool_->targetSelected->isChecked ());
875 876
}

Jan Möbius's avatar
Jan Möbius committed
877 878 879 880 881 882 883
void DataControlPlugin::showReducedUi(bool reduced) {
    tool_->boundingBoxWidget->setVisible(!reduced);
    tool_->boundingBoxBtn->setVisible(!reduced);
    tool_->line_2->setVisible(!reduced);
    tool_->visibleDataWidget->setVisible(!reduced);
    tool_->visibleDataBtn->setVisible(!reduced);
}
Dirk Wilden's avatar
Dirk Wilden committed
884

Matthias Möller's avatar
Matthias Möller committed
885
#if QT_VERSION < 0x050000
Jan Möbius's avatar
 
Jan Möbius committed
886
Q_EXPORT_PLUGIN2( datacontrolplugin , DataControlPlugin );
Matthias Möller's avatar
Matthias Möller committed
887
#endif
Jan Möbius's avatar
 
Jan Möbius committed
888