DataControlPlugin.cc 27.8 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40 41 42
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
48
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49

50 51
#include "DataControlPlugin.hh"

Matthias Möller's avatar
Matthias Möller committed
52 53 54 55 56
#if QT_VERSION >= 0x050000 
  #include <QtWidgets>
#else
  #include <QtGui>
#endif
Jan Möbius's avatar
 
Jan Möbius committed
57 58

#include <QLayout>
59
#include <QGridLayout>
Jan Möbius's avatar
 
Jan Möbius committed
60
#include <QItemSelectionModel>
61

Jan Möbius's avatar
 
Jan Möbius committed
62 63 64 65
#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
#include <ACG/Scenegraph/BaseNode.hh>
Dirk Wilden's avatar
Dirk Wilden committed
66
#include <ACG/QtWidgets/QtMaterialDialog.hh>
Jan Möbius's avatar
 
Jan Möbius committed
67 68 69 70 71
#include <QModelIndexList>

#include <queue>

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

74 75
#include <ObjectTypes/Light/Light.hh>

Dirk Wilden's avatar
Dirk Wilden committed
76
//******************************************************************************
Dirk Wilden's avatar
Dirk Wilden committed
77

Jan Möbius's avatar
Jan Möbius committed
78 79 80
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);
81 82 83

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

Jan Möbius's avatar
Jan Möbius committed
84 85 86 87 88 89 90 91 92 93 94 95 96


DataControlPlugin::DataControlPlugin() :
        tool_(0),
        toolIcon_(0),
        MeshDialogLayout_(0),
        objectList_(0),
        locked(false),
        model_(0),
        view_(0),
        viewHeader_(0),
        onlyDown_(0),
        onlyUp_(0),
97
        columnFromGUI_(-1),
Jan Möbius's avatar
Jan Möbius committed
98 99 100 101 102 103 104 105 106
        headerPopupType_(0),
        targetAction_(0),
        sourceAction_(0),
        removeAction_(0)
{

}


Dirk Wilden's avatar
Dirk Wilden committed
107
/** \brief Plugin initialization
108
 *
Dirk Wilden's avatar
Dirk Wilden committed
109
 */
Jan Möbius's avatar
 
Jan Möbius committed
110
void DataControlPlugin::pluginsInitialized() {
111

Jan Möbius's avatar
 
Jan Möbius committed
112 113 114
  //set the slot descriptions
  setDescriptions();

115 116 117
  if ( ! OpenFlipper::Options::gui())
    return;

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

Jan Möbius's avatar
 
Jan Möbius committed
120
  //Target Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
121 122
  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
123 124 125
  hideAction->setStatusTip(tr("Hide object"));
  connect(hideAction, SIGNAL(triggered()), this, SLOT(slotContextMenuHide()) );
  contextMenu->addAction(hideAction);
126

Jan Möbius's avatar
 
Jan Möbius committed
127
  //Target Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
128 129
  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
130 131 132 133
  targetAction_->setCheckable(true);
  targetAction_->setStatusTip(tr("Set object as target"));
  connect(targetAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuTarget()) );
  contextMenu->addAction(targetAction_);
134

Jan Möbius's avatar
 
Jan Möbius committed
135
  //Source Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
136 137
  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
138 139 140
  sourceAction_->setCheckable(true);
  sourceAction_->setStatusTip(tr("Set object as source"));
  connect(sourceAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuSource()) );
141 142
  contextMenu->addAction(sourceAction_);

Dirk Wilden's avatar
 
Dirk Wilden committed
143 144 145 146 147 148 149 150 151 152
  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
153
  emit addContextMenuItem(contextMenu->menuAction() , DATA_ALL , CONTEXTOBJECTMENU);
154

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

Matthias Möller's avatar
Matthias Möller committed
160 161
  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);
162 163 164
  connect (copyMaterial, SIGNAL( triggered() ), this, SLOT ( slotCopyMaterialToTargeted() ));
  emit addContextMenuItem(copyMaterial , DATA_ALL , CONTEXTOBJECTMENU);

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

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

175 176
  PluginFunctions::setDefaultViewObjectMarker (&objectMarker);
  PluginFunctions::setViewObjectMarker (&objectMarker);
177 178
  
  connect(tool_->lightSources, SIGNAL(stateChanged(int)), this, SLOT(slotShowLightSources(int)));
179 180
  //update light visibility, if layout was changed
  connect(model_, SIGNAL(layoutChanged ()), this, SLOT(slotShowLightSources()) );
181
  connect(model_, SIGNAL(rowsRemoved(const QModelIndex& , int , int )), this, SLOT(slotShowLightSources()));
Jan Möbius's avatar
 
Jan Möbius committed
182 183
}

Dirk Wilden's avatar
Dirk Wilden committed
184 185 186

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

187
void DataControlPlugin::initializePlugin()
Jan Möbius's avatar
 
Jan Möbius committed
188
{
189 190 191 192 193 194 195 196 197 198 199 200
  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;
201

202
  tool_->treeView->setModel(model_);
203

204 205 206
  view_->QTreeView::resizeColumnToContents(1);
  view_->QTreeView::resizeColumnToContents(2);
  view_->QTreeView::resizeColumnToContents(3);
207

208

209 210
  connect( model_,SIGNAL(dataChangedInside(int,int,const QVariant&) ),
      this,  SLOT(    slotDataChanged(int,int,const QVariant&)) );
211

212 213
  connect( model_,SIGNAL(   moveBaseObject(int,int) ),
      this,  SLOT( slotMoveBaseObject(int,int) ) );
Dirk Wilden's avatar
Dirk Wilden committed
214

215 216
  connect( view_,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
      this,SLOT(slotCustomContextMenuRequested ( const QPoint & ) ));
217

218 219 220 221 222 223
  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 ( ) ));
224 225


226 227
  viewHeader_ = tool_->treeView->header();
  viewHeader_->setContextMenuPolicy(Qt::CustomContextMenu);
228

229 230 231
  // connect the slot for the context menu
  connect( viewHeader_, SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
      this,        SLOT(slotHeaderCustomContextMenuRequested ( const QPoint & ) ));
232

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

235
  emit addToolbox("Data Control", tool_, toolIcon_);
236

237 238 239 240 241 242 243 244
  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
245 246 247
}


Dirk Wilden's avatar
Dirk Wilden committed
248
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
249

Dirk Wilden's avatar
Dirk Wilden committed
250
/** \brief update drawing of objects when the active object changed
251
 *
Dirk Wilden's avatar
Dirk Wilden committed
252 253 254
 */
void DataControlPlugin::slotObjectSelectionChanged( int _identifier )
{
255 256 257
  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
258 259
  BaseObjectData* obj = 0;

260
  if ( PluginFunctions::getObject( _identifier, obj) )
261 262
    updateBoundingBox (obj);

Dirk Wilden's avatar
Dirk Wilden committed
263
  BaseObject* object = 0;
264 265
  if ( !PluginFunctions::getObject( _identifier, object) )
    return;
Dirk Wilden's avatar
Dirk Wilden committed
266

267 268 269 270 271 272 273
  //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;
274

275 276 277 278 279 280 281
    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
  }
282

Dirk Wilden's avatar
Dirk Wilden committed
283

284
  model_->objectChanged( _identifier );
285

286 287 288 289
  //block scenegraph reset and redraw in core
  OpenFlipper::Options::blockSceneGraphUpdates();
  OpenFlipper::Options::redrawDisabled(true);

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

293
    onlyDown_++;
294

295 296
    if ( object->isGroup() )
      propagateDownwards(object, column); // 2: source 3: target
297

298 299
    onlyDown_--;
  }
300

301 302
  // if we are allowed to propagate up
  if ( onlyDown_ == 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
303

304 305 306 307 308
    onlyUp_++;

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

    onlyUp_--;
309
  }
310 311 312 313

  OpenFlipper::Options::unblockSceneGraphUpdates();
  OpenFlipper::Options::redrawDisabled(false);
  emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
314 315 316 317 318 319
}


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

/** \brief Update the model if the visibility of an object changed
320
 *
Dirk Wilden's avatar
Dirk Wilden committed
321 322
 * @param _identifier id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
323
void DataControlPlugin::slotVisibilityChanged( int _identifier ){
324

325 326 327
  if ( ! OpenFlipper::Options::gui())
    return;

328 329 330 331 332 333
  // 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
334 335 336 337

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

338 339 340 341
  //block scenegraph reset and redraw in core
  OpenFlipper::Options::blockSceneGraphUpdates();
  OpenFlipper::Options::redrawDisabled(true);

Dirk Wilden's avatar
Dirk Wilden committed
342 343
  if ( PluginFunctions::getObject( _identifier, obj) ){

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    // 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
365 366
  }

367 368 369
  OpenFlipper::Options::unblockSceneGraphUpdates();
  OpenFlipper::Options::redrawDisabled(false);

370 371 372 373 374
  BaseObjectData* object = 0;

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

375 376
  emit updateView();

Jan Möbius's avatar
 
Jan Möbius committed
377 378 379
}


Dirk Wilden's avatar
Dirk Wilden committed
380
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
381

Dirk Wilden's avatar
Dirk Wilden committed
382
/** \brief Update the model if properties of an object changed
383
 *
Dirk Wilden's avatar
Dirk Wilden committed
384
 * @param _identifier id of an object
Dirk Wilden's avatar
Dirk Wilden committed
385
 */
Dirk Wilden's avatar
Dirk Wilden committed
386
void DataControlPlugin::slotObjectPropertiesChanged( int _identifier ){
387 388 389 390

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

Dirk Wilden's avatar
Dirk Wilden committed
391 392
  model_->objectChanged( _identifier );
}
Jan Möbius's avatar
 
Jan Möbius committed
393

Dirk Wilden's avatar
Dirk Wilden committed
394

Dirk Wilden's avatar
Dirk Wilden committed
395 396 397
//******************************************************************************

/** \brief Update the model if a file has been opened
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
void DataControlPlugin::fileOpened(int _id){

403 404 405
  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
406 407 408 409
  BaseObject* obj = 0;

  if ( PluginFunctions::getObject(_id, obj) )
    model_->objectAdded(obj);
410 411 412 413 414

  // 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
415
  view_->resizeColumnToContents(0);
Dirk Wilden's avatar
Dirk Wilden committed
416 417 418 419 420 421
}


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

/** \brief Update the model if an empty object has been added
422
 *
Dirk Wilden's avatar
Dirk Wilden committed
423 424
 * @param _id id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
425 426 427 428 429 430 431
void DataControlPlugin::addedEmptyObject(int _id){
  fileOpened(_id);
}

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

/** \brief an object was deleted. delete it internally
432
 *
Dirk Wilden's avatar
Dirk Wilden committed
433 434 435
 * @param _id id of the object
 */
void DataControlPlugin::objectDeleted(int _id){
436 437 438 439

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

Dirk Wilden's avatar
Dirk Wilden committed
440
  model_->objectDeleted(_id);
Jan Möbius's avatar
 
Jan Möbius committed
441 442
}

Dirk Wilden's avatar
Dirk Wilden committed
443 444 445
//******************************************************************************

/** \brief a key event occurred
446
 *
Dirk Wilden's avatar
Dirk Wilden committed
447 448
 * @param _event the event that occurred
 */
Jan Möbius's avatar
 
Jan Möbius committed
449 450 451 452 453
void DataControlPlugin::slotKeyEvent( QKeyEvent* _event )
{

  if ( _event->modifiers() == Qt::ControlModifier ) {
    switch (_event->key()) {
454
      case Qt::Key_A :
Jan Möbius's avatar
 
Jan Möbius committed
455 456 457 458 459 460
          setAllTarget();
        return;
      default:
        return;
    }
  }
461

462 463 464 465 466 467 468
  switch (_event->key()) {
    case Qt::Key_Delete :
        slotPopupRemove();
      return;
    default:
      return;
  }
469

Jan Möbius's avatar
 
Jan Möbius committed
470 471
}

Dirk Wilden's avatar
Dirk Wilden committed
472 473 474 475

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

/** \brief emit the right updates when the model changed
476
 *
Jan Möbius's avatar
Jan Möbius committed
477 478 479
 * @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
480
 */
Dirk Wilden's avatar
Dirk Wilden committed
481
void DataControlPlugin::slotDataChanged ( int _id, int _column, const QVariant& _value)
Jan Möbius's avatar
 
Jan Möbius committed
482
{
483

Dirk Wilden's avatar
Dirk Wilden committed
484 485 486 487
  //get the corresponding baseObject
  BaseObject* obj = 0;
  if ( !PluginFunctions::getObject( _id, obj) )
    return;
488
  
Dirk Wilden's avatar
Dirk Wilden committed
489 490 491 492 493 494 495 496 497 498 499 500 501
  switch ( _column ) {
    // Name
    case 0:
      obj->setName( _value.toString() );
      break;

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

    // source
    case 2:
502
      columnFromGUI_ = 2;//set information, which information will be changed
Dirk Wilden's avatar
Dirk Wilden committed
503
      obj->source( _value.toBool() );
504
      columnFromGUI_ = -1;
Dirk Wilden's avatar
Dirk Wilden committed
505 506 507 508
      break;

    // target
    case 3:
509
      columnFromGUI_ = 3;//set information, which information will be changed
Dirk Wilden's avatar
Dirk Wilden committed
510
      obj->target( _value.toBool() );
511
      columnFromGUI_ = -1;
Dirk Wilden's avatar
Dirk Wilden committed
512
      break;
Dirk Wilden's avatar
Dirk Wilden committed
513

Dirk Wilden's avatar
Dirk Wilden committed
514 515
    default:
      break;
Jan Möbius's avatar
 
Jan Möbius committed
516 517 518
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
519 520 521

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

Dirk Wilden's avatar
Dirk Wilden committed
522
/** \brief Gets called when an object was moved via drag n drop
523
 *
Jan Möbius's avatar
Jan Möbius committed
524 525
 * @param _id          id of the object
 * @param _newParentId id of the new parent
Dirk Wilden's avatar
Dirk Wilden committed
526
 */
Dirk Wilden's avatar
Dirk Wilden committed
527
void DataControlPlugin::slotMoveBaseObject(int _id, int _newParentId){
Dirk Wilden's avatar
Dirk Wilden committed
528

Dirk Wilden's avatar
Dirk Wilden committed
529
  BaseObject* obj = 0;
Dirk Wilden's avatar
Dirk Wilden committed
530

Dirk Wilden's avatar
Dirk Wilden committed
531 532
  if ( !PluginFunctions::getObject(_id, obj) )
    return;
Jan Möbius's avatar
 
Jan Möbius committed
533

Dirk Wilden's avatar
Dirk Wilden committed
534
  BaseObject* parent = 0;
Dirk Wilden's avatar
Dirk Wilden committed
535

Dirk Wilden's avatar
Dirk Wilden committed
536 537
  if ( !PluginFunctions::getObject(_newParentId, parent) )
    return;
Dirk Wilden's avatar
Dirk Wilden committed
538

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

Dirk Wilden's avatar
Dirk Wilden committed
541 542
  //set new parent
  obj->setParent( parent );
Dirk Wilden's avatar
Dirk Wilden committed
543

Dirk Wilden's avatar
Dirk Wilden committed
544 545 546
  //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
547 548 549
}


550 551 552 553 554
//******************************************************************************

void DataControlPlugin::slotShowLightSources( int _state ) {

    int rows = model_->rowCount();
555

556 557 558
    for(int i = 0; i < rows; ++i) {
        TreeItem* item = model_->getItem(model_->index(i,0));
        if(item->dataType() == DATA_LIGHT) {
559 560 561 562
          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);
563 564 565 566
        }
    }
}

567 568 569 570 571

void DataControlPlugin::slotShowLightSources()
{
  slotShowLightSources( tool_->lightSources->checkState() );
}
Dirk Wilden's avatar
Dirk Wilden committed
572 573 574
//******************************************************************************

/** \brief Load Groups from ini file
575
 *
Dirk Wilden's avatar
Dirk Wilden committed
576 577
 * @param _ini an ini file
 */
Jan Möbius's avatar
 
Jan Möbius committed
578
void DataControlPlugin::loadIniFileOptionsLast( INIFile& _ini ) {
579

580
  if ( _ini.section_exists( "BoundingBox" ) && OpenFlipper::Options::gui() )
581 582 583 584 585 586 587 588 589 590
  {
    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);
  }

591
  if ( !_ini.section_exists( "Groups" ) )
Jan Möbius's avatar
 
Jan Möbius committed
592
    return;
593

Jan Möbius's avatar
 
Jan Möbius committed
594 595
  // Names of all groups
  QStringList groupNames;
596

Jan Möbius's avatar
 
Jan Möbius committed
597 598
  // names of the primary groups
  QStringList rootGroup;
599

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

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

Dirk Wilden's avatar
Dirk Wilden committed
606 607 608
  //list of groups
  QVector< BaseObject* > groups;

Jan Möbius's avatar
 
Jan Möbius committed
609 610 611 612
  // Go over one level of the groups
  while ( rootGroup.size() > 0 ) {
    QString current = rootGroup[0];
    rootGroup.removeFirst();
613

Jan Möbius's avatar
 
Jan Möbius committed
614 615
    QStringList groupChildren;
    QStringList elementChildren;
616

Jan Möbius's avatar
 
Jan Möbius committed
617 618
    _ini.get_entry(elementChildren ,current,"children");
    _ini.get_entry(groupChildren ,current,"subgroups");
619

Jan Möbius's avatar
 
Jan Möbius committed
620 621 622 623 624 625
    // 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 )
626 627 628 629 630 631
        parentItem = PluginFunctions::objectRoot();
    } else
      parentItem = PluginFunctions::objectRoot();

    rootGroup << groupChildren;

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

Jan Möbius's avatar
 
Jan Möbius committed
635 636
    // group does not exist
    if ( !group ) {
637

638 639
      int groupId = addEmptyGroup(current, parentItem->id());
      PluginFunctions::getObject(groupId, group);
Dirk Wilden's avatar
Dirk Wilden committed
640 641 642 643 644 645 646 647

      // 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
648
    }
649

Jan Möbius's avatar
 
Jan Möbius committed
650 651 652 653 654 655 656 657 658 659
    // 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
660 661 662 663

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

/** \brief Save groups to ini file
664
 *
Dirk Wilden's avatar
Dirk Wilden committed
665 666
 * @param _ini an ini file
 */
Jan Möbius's avatar
 
Jan Möbius committed
667
void DataControlPlugin::saveIniFileOptions( INIFile& _ini ) {
668

Jan Möbius's avatar
 
Jan Möbius committed
669 670
  std::queue< BaseObject* > children;
  children.push( PluginFunctions::objectRoot() );
671

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

Jan Möbius's avatar
 
Jan Möbius committed
674
  // Get all groups from the tree
Jan Möbius's avatar
Jan Möbius committed
675
  while ( ! children.empty() ) {
Jan Möbius's avatar
 
Jan Möbius committed
676 677
    BaseObject* item = children.front();
    children.pop();
678 679 680 681 682

    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
683 684 685
    if ( item->dataType(DATA_GROUP) && (item != PluginFunctions::objectRoot() ) )
      groups.push_back(item);
  }
686

Jan Möbius's avatar
 
Jan Möbius committed
687 688
  // Names of all groups
  QStringList groupNames;
689

Jan Möbius's avatar
 
Jan Möbius committed
690 691
  // names of the primary groups
  QStringList rootGroup;
692

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

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
705 706 707 708 709 710 711 712 713
    // 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() );
    }
714

Jan Möbius's avatar
 
Jan Möbius committed
715 716 717
    _ini.add_entry(groups[i]->name(),"subgroups",groupchildren);
    _ini.add_entry(groups[i]->name(),"children",elementchildren);
  }
718

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

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

725 726 727 728 729 730
  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 ());
  }
731

Jan Möbius's avatar
 
Jan Möbius committed
732 733 734
}


Dirk Wilden's avatar
Dirk Wilden committed
735 736 737
//******************************************************************************

/** \brief Recursively update a column up to the root of the tree
738
 *
Jan Möbius's avatar
Jan Möbius committed
739 740
 * @param _obj    Object to start with
 * @param _column Column to propagate
Dirk Wilden's avatar
Dirk Wilden committed
741 742 743
 */
void DataControlPlugin::propagateUpwards(BaseObject* _obj, int _column ){

744
  
Dirk Wilden's avatar
Dirk Wilden committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
  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;

766
    case 2: //SOURCE
Dirk Wilden's avatar
Dirk Wilden committed
767 768 769 770 771 772 773 774 775

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

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

778 779 780 781 782 783 784 785
    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
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
        changed = true;
      }

      break;

    default:
      break;
  }

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

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

/** \brief Recursively update a column up to the root of the tree
802
 *
Jan Möbius's avatar
Jan Möbius committed
803 804
 * @param _obj    object to start with
 * @param _column Column to be propagated
Dirk Wilden's avatar
Dirk Wilden committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
 */
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;

822
      case 2: //SOURCE
Dirk Wilden's avatar
Dirk Wilden committed
823 824 825 826

        if ( current->source() != _obj->source() ){
          current->source( _obj->source() );
        }
827 828 829
        break;

      case 3: //TARGET
Dirk Wilden's avatar
Dirk Wilden committed
830 831 832 833 834 835 836 837 838 839 840

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

        break;

      default:
        break;
    }

841
    if ( current->isGroup() ){
Dirk Wilden's avatar
Dirk Wilden committed
842 843 844 845 846 847
      propagateDownwards(current, _column);

    }
  }
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
//******************************************************************************

/** \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){
896 897 898
  _ini.add_entry("BoundingBox","notSelected",tool_->notSelected->isChecked ());
  _ini.add_entry("BoundingBox","sourceSelected",tool_->sourceSelected->isChecked ());
  _ini.add_entry("BoundingBox","targetSelected",tool_->targetSelected->isChecked ());
899 900
}

Jan Möbius's avatar
Jan Möbius committed
901 902 903 904 905 906 907
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
908

909 910 911 912 913 914 915 916 917 918 919
void DataControlPlugin::slotObjectUpdated( int _identifier, const UpdateType& _type )
{
  if ( ! OpenFlipper::Options::gui() || !_type.contains(UPDATE_GEOMETRY))
    return;

  BaseObjectData* obj = 0;

  if ( PluginFunctions::getObject( _identifier, obj) )
    updateBoundingBox (obj);
}

Matthias Möller's avatar
Matthias Möller committed
920
#if QT_VERSION < 0x050000
Jan Möbius's avatar
 
Jan Möbius committed
921
Q_EXPORT_PLUGIN2( datacontrolplugin , DataControlPlugin );
Matthias Möller's avatar
Matthias Möller committed
922
#endif
Jan Möbius's avatar
 
Jan Möbius committed
923