DataControlPlugin.cc 24.2 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-2011 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 45 46 47 48 49 50 51





#include <QtGui>

#include "DataControlPlugin.hh"

#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

71 72 73 74 75 76
const ACG::Vec4f base_color (0.0,0.0,0.5,1.0);
const ACG::Vec4f source_color (0.5,0.0,0.0,1.0);
const ACG::Vec4f target_color (0.0,0.5,0.2,1.0);

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

Jan Möbius's avatar
Jan Möbius committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98


DataControlPlugin::DataControlPlugin() :
        tool_(0),
        toolIcon_(0),
        MeshDialogLayout_(0),
        objectList_(0),
        locked(false),
        model_(0),
        view_(0),
        viewHeader_(0),
        onlyDown_(0),
        onlyUp_(0),
        headerPopupType_(0),
        targetAction_(0),
        sourceAction_(0),
        removeAction_(0)
{

}


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

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
112
  //Target Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
113 114
  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
115 116 117
  hideAction->setStatusTip(tr("Hide object"));
  connect(hideAction, SIGNAL(triggered()), this, SLOT(slotContextMenuHide()) );
  contextMenu->addAction(hideAction);
118

Jan Möbius's avatar
 
Jan Möbius committed
119
  //Target Objects
Dirk Wilden's avatar
 
Dirk Wilden committed
120 121
  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
122 123 124 125
  targetAction_->setCheckable(true);
  targetAction_->setStatusTip(tr("Set object as target"));
  connect(targetAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuTarget()) );
  contextMenu->addAction(targetAction_);
126

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

Dirk Wilden's avatar
 
Dirk Wilden committed
135 136 137 138 139 140 141 142 143 144
  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
145
  emit addContextMenuItem(contextMenu->menuAction() , DATA_ALL , CONTEXTOBJECTMENU);
146

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

152 153 154 155
  QAction* copyMaterial = new QAction(tr("Copy Material Properties to Targeted Objects"), 0);
  connect (copyMaterial, SIGNAL( triggered() ), this, SLOT ( slotCopyMaterialToTargeted() ));
  emit addContextMenuItem(copyMaterial , DATA_ALL , CONTEXTOBJECTMENU);

156 157
  PluginFunctions::setDefaultViewObjectMarker (&objectMarker);
  PluginFunctions::setViewObjectMarker (&objectMarker);
158 159 160
  
  connect(tool_->lightSources, SIGNAL(stateChanged(int)), this, SLOT(slotShowLightSources(int)));
  slotShowLightSources(tool_->lightSources->checkState());
Jan Möbius's avatar
 
Jan Möbius committed
161 162
}

Dirk Wilden's avatar
Dirk Wilden committed
163 164 165

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

166
void DataControlPlugin::initializePlugin()
Jan Möbius's avatar
 
Jan Möbius committed
167
{
168 169 170 171 172 173 174 175 176 177 178 179
  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;
180

181
  tool_->treeView->setModel(model_);
182

183 184 185
  view_->QTreeView::resizeColumnToContents(1);
  view_->QTreeView::resizeColumnToContents(2);
  view_->QTreeView::resizeColumnToContents(3);
186

187

188 189
  connect( model_,SIGNAL(dataChangedInside(int,int,const QVariant&) ),
      this,  SLOT(    slotDataChanged(int,int,const QVariant&)) );
190

191 192
  connect( model_,SIGNAL(   moveBaseObject(int,int) ),
      this,  SLOT( slotMoveBaseObject(int,int) ) );
Dirk Wilden's avatar
Dirk Wilden committed
193

194 195
  connect( view_,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
      this,SLOT(slotCustomContextMenuRequested ( const QPoint & ) ));
196

197 198 199 200 201 202
  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 ( ) ));
203 204


205 206
  viewHeader_ = tool_->treeView->header();
  viewHeader_->setContextMenuPolicy(Qt::CustomContextMenu);
207

208 209 210
  // connect the slot for the context menu
  connect( viewHeader_, SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
      this,        SLOT(slotHeaderCustomContextMenuRequested ( const QPoint & ) ));
211

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

214
  emit addToolbox("Data Control", tool_, toolIcon_);
215

216 217 218 219 220 221 222 223
  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
224 225 226
}


Dirk Wilden's avatar
Dirk Wilden committed
227
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
228

Dirk Wilden's avatar
Dirk Wilden committed
229
/** \brief update drawing of objects when the active object changed
230
 *
Dirk Wilden's avatar
Dirk Wilden committed
231 232 233
 */
void DataControlPlugin::slotObjectSelectionChanged( int _identifier )
{
234 235 236
  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
237 238
  BaseObjectData* obj = 0;

239
  if ( PluginFunctions::getObject( _identifier, obj) )
240 241
    updateBoundingBox (obj);

242 243 244 245
  // if onlyUp_ > 0 --> _identifier is a group and the selection
  // does not have to be applied
  if (onlyUp_ == 0)
    model_->objectChanged( _identifier );
Dirk Wilden's avatar
Dirk Wilden committed
246

247
  
Dirk Wilden's avatar
Dirk Wilden committed
248 249 250 251
  //check for changes in the tree
  BaseObject* object = 0;

  if ( PluginFunctions::getObject( _identifier, object) ){
252 253 254 255 256 257 258 259 260 261 262 263 264
    
      // if we are allowed to propagate up
      if ( onlyDown_ == 0 ){
  
        onlyUp_++;
    
        propagateUpwards(object->parent(), 2); // 2 = source-target

        onlyUp_--;
      }
    
      // if we are allowed to propagate down
      if ( onlyUp_ == 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
265

266
        onlyDown_++;
Dirk Wilden's avatar
Dirk Wilden committed
267

268 269 270 271 272 273
        if ( object->isGroup() )
          propagateDownwards(object, 2); // 2 = source-target
      
        onlyDown_--;
      }
  }
Dirk Wilden's avatar
Dirk Wilden committed
274 275 276 277 278 279
}


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

/** \brief Update the model if the visibility of an object changed
280
 *
Dirk Wilden's avatar
Dirk Wilden committed
281 282
 * @param _identifier id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
283
void DataControlPlugin::slotVisibilityChanged( int _identifier ){
284

285 286 287
  if ( ! OpenFlipper::Options::gui())
    return;

288 289 290 291 292 293
  // 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
294 295 296 297 298 299

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

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

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    // 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
321 322
  }

323 324 325 326 327
  BaseObjectData* object = 0;

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

Jan Möbius's avatar
 
Jan Möbius committed
328 329 330
}


Dirk Wilden's avatar
Dirk Wilden committed
331
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
332

Dirk Wilden's avatar
Dirk Wilden committed
333
/** \brief Update the model if properties of an object changed
334
 *
Dirk Wilden's avatar
Dirk Wilden committed
335
 * @param _identifier id of an object
Dirk Wilden's avatar
Dirk Wilden committed
336
 */
Dirk Wilden's avatar
Dirk Wilden committed
337
void DataControlPlugin::slotObjectPropertiesChanged( int _identifier ){
338 339 340 341

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

Dirk Wilden's avatar
Dirk Wilden committed
342 343
  model_->objectChanged( _identifier );
}
Jan Möbius's avatar
 
Jan Möbius committed
344

Dirk Wilden's avatar
Dirk Wilden committed
345

Dirk Wilden's avatar
Dirk Wilden committed
346 347 348
//******************************************************************************

/** \brief Update the model if a file has been opened
349
 *
Dirk Wilden's avatar
Dirk Wilden committed
350 351
 * @param _id id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
352 353
void DataControlPlugin::fileOpened(int _id){

354 355 356
  if ( ! OpenFlipper::Options::gui())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
357 358 359 360
  BaseObject* obj = 0;

  if ( PluginFunctions::getObject(_id, obj) )
    model_->objectAdded(obj);
361 362 363 364 365

  // 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
366
  view_->resizeColumnToContents(0);
Dirk Wilden's avatar
Dirk Wilden committed
367 368 369 370 371 372
}


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

/** \brief Update the model if an empty object has been added
373
 *
Dirk Wilden's avatar
Dirk Wilden committed
374 375
 * @param _id id of an object
 */
Dirk Wilden's avatar
Dirk Wilden committed
376 377 378 379 380 381 382
void DataControlPlugin::addedEmptyObject(int _id){
  fileOpened(_id);
}

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

/** \brief an object was deleted. delete it internally
383
 *
Dirk Wilden's avatar
Dirk Wilden committed
384 385 386
 * @param _id id of the object
 */
void DataControlPlugin::objectDeleted(int _id){
387 388 389 390

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

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

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

/** \brief a key event occurred
397
 *
Dirk Wilden's avatar
Dirk Wilden committed
398 399
 * @param _event the event that occurred
 */
Jan Möbius's avatar
 
Jan Möbius committed
400 401 402 403 404
void DataControlPlugin::slotKeyEvent( QKeyEvent* _event )
{

  if ( _event->modifiers() == Qt::ControlModifier ) {
    switch (_event->key()) {
405
      case Qt::Key_A :
Jan Möbius's avatar
 
Jan Möbius committed
406 407 408 409 410 411
          setAllTarget();
        return;
      default:
        return;
    }
  }
412

413 414 415 416 417 418 419
  switch (_event->key()) {
    case Qt::Key_Delete :
        slotPopupRemove();
      return;
    default:
      return;
  }
420

Jan Möbius's avatar
 
Jan Möbius committed
421 422
}

Dirk Wilden's avatar
Dirk Wilden committed
423 424 425 426

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

/** \brief emit the right updates when the model changed
427
 *
Jan Möbius's avatar
Jan Möbius committed
428 429 430
 * @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
431
 */
Dirk Wilden's avatar
Dirk Wilden committed
432
void DataControlPlugin::slotDataChanged ( int _id, int _column, const QVariant& _value)
Jan Möbius's avatar
 
Jan Möbius committed
433
{
434

Dirk Wilden's avatar
Dirk Wilden committed
435 436 437 438
  //get the corresponding baseObject
  BaseObject* obj = 0;
  if ( !PluginFunctions::getObject( _id, obj) )
    return;
439
  
Dirk Wilden's avatar
Dirk Wilden committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  switch ( _column ) {
    // Name
    case 0:
      obj->setName( _value.toString() );
      break;

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

    // source
    case 2:
      obj->source( _value.toBool() );
      break;

    // target
    case 3:
      obj->target( _value.toBool() );
      break;
Dirk Wilden's avatar
Dirk Wilden committed
460

Dirk Wilden's avatar
Dirk Wilden committed
461 462
    default:
      break;
Jan Möbius's avatar
 
Jan Möbius committed
463 464 465
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
466 467 468

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

Dirk Wilden's avatar
Dirk Wilden committed
469
/** \brief Gets called when an object was moved via drag n drop
470
 *
Jan Möbius's avatar
Jan Möbius committed
471 472
 * @param _id          id of the object
 * @param _newParentId id of the new parent
Dirk Wilden's avatar
Dirk Wilden committed
473
 */
Dirk Wilden's avatar
Dirk Wilden committed
474
void DataControlPlugin::slotMoveBaseObject(int _id, int _newParentId){
Dirk Wilden's avatar
Dirk Wilden committed
475

Dirk Wilden's avatar
Dirk Wilden committed
476
  BaseObject* obj = 0;
Dirk Wilden's avatar
Dirk Wilden committed
477

Dirk Wilden's avatar
Dirk Wilden committed
478 479
  if ( !PluginFunctions::getObject(_id, obj) )
    return;
Jan Möbius's avatar
 
Jan Möbius committed
480

Dirk Wilden's avatar
Dirk Wilden committed
481
  BaseObject* parent = 0;
Dirk Wilden's avatar
Dirk Wilden committed
482

Dirk Wilden's avatar
Dirk Wilden committed
483 484
  if ( !PluginFunctions::getObject(_newParentId, parent) )
    return;
Dirk Wilden's avatar
Dirk Wilden committed
485

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

Dirk Wilden's avatar
Dirk Wilden committed
488 489
  //set new parent
  obj->setParent( parent );
Dirk Wilden's avatar
Dirk Wilden committed
490

Dirk Wilden's avatar
Dirk Wilden committed
491 492 493
  //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
494 495 496
}


497 498 499 500 501 502 503 504 505 506 507 508 509 510
//******************************************************************************

void DataControlPlugin::slotShowLightSources( int _state ) {

    int rows = model_->rowCount();
    
    for(int i = 0; i < rows; ++i) {
        TreeItem* item = model_->getItem(model_->index(i,0));
        if(item->dataType() == DATA_LIGHT) {
            view_->setRowHidden(i, model_->parent(model_->index(i,0)), !(_state == Qt::Checked));
        }
    }
}

Dirk Wilden's avatar
Dirk Wilden committed
511 512 513
//******************************************************************************

/** \brief Load Groups from ini file
514
 *
Dirk Wilden's avatar
Dirk Wilden committed
515 516
 * @param _ini an ini file
 */
Jan Möbius's avatar
 
Jan Möbius committed
517
void DataControlPlugin::loadIniFileOptionsLast( INIFile& _ini ) {
518

519
  if ( _ini.section_exists( "BoundingBox" ) && OpenFlipper::Options::gui() )
520 521 522 523 524 525 526 527 528 529
  {
    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);
  }

530
  if ( !_ini.section_exists( "Groups" ) )
Jan Möbius's avatar
 
Jan Möbius committed
531
    return;
532

Jan Möbius's avatar
 
Jan Möbius committed
533 534
  // Names of all groups
  QStringList groupNames;
535

Jan Möbius's avatar
 
Jan Möbius committed
536 537
  // names of the primary groups
  QStringList rootGroup;
538

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

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

Dirk Wilden's avatar
Dirk Wilden committed
545 546 547
  //list of groups
  QVector< BaseObject* > groups;

Jan Möbius's avatar
 
Jan Möbius committed
548 549 550 551
  // Go over one level of the groups
  while ( rootGroup.size() > 0 ) {
    QString current = rootGroup[0];
    rootGroup.removeFirst();
552

Jan Möbius's avatar
 
Jan Möbius committed
553 554
    QStringList groupChildren;
    QStringList elementChildren;
555

Jan Möbius's avatar
 
Jan Möbius committed
556 557
    _ini.get_entry(elementChildren ,current,"children");
    _ini.get_entry(groupChildren ,current,"subgroups");
558

Jan Möbius's avatar
 
Jan Möbius committed
559 560 561 562 563 564
    // 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 )
565 566 567 568 569 570
        parentItem = PluginFunctions::objectRoot();
    } else
      parentItem = PluginFunctions::objectRoot();

    rootGroup << groupChildren;

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

Jan Möbius's avatar
 
Jan Möbius committed
574 575
    // group does not exist
    if ( !group ) {
576

Jan Möbius's avatar
 
Jan Möbius committed
577
      group = dynamic_cast< BaseObject* >( new GroupObject( current, dynamic_cast< GroupObject* >(parentItem ) ) );
578

Dirk Wilden's avatar
Dirk Wilden committed
579 580 581 582 583 584 585 586 587
      emit emptyObjectAdded( group->id() );

      // 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
588
    }
589

Jan Möbius's avatar
 
Jan Möbius committed
590 591 592 593 594 595 596 597 598 599
    // 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
600 601 602 603

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

/** \brief Save groups to ini file
604
 *
Dirk Wilden's avatar
Dirk Wilden committed
605 606
 * @param _ini an ini file
 */
Jan Möbius's avatar
 
Jan Möbius committed
607
void DataControlPlugin::saveIniFileOptions( INIFile& _ini ) {
608

Jan Möbius's avatar
 
Jan Möbius committed
609 610
  std::queue< BaseObject* > children;
  children.push( PluginFunctions::objectRoot() );
611

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

Jan Möbius's avatar
 
Jan Möbius committed
614
  // Get all groups from the tree
Jan Möbius's avatar
Jan Möbius committed
615
  while ( ! children.empty() ) {
Jan Möbius's avatar
 
Jan Möbius committed
616 617
    BaseObject* item = children.front();
    children.pop();
618 619 620 621 622

    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
623 624 625
    if ( item->dataType(DATA_GROUP) && (item != PluginFunctions::objectRoot() ) )
      groups.push_back(item);
  }
626

Jan Möbius's avatar
 
Jan Möbius committed
627 628
  // Names of all groups
  QStringList groupNames;
629

Jan Möbius's avatar
 
Jan Möbius committed
630 631
  // names of the primary groups
  QStringList rootGroup;
632

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

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
645 646 647 648 649 650 651 652 653
    // 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() );
    }
654

Jan Möbius's avatar
 
Jan Möbius committed
655 656 657
    _ini.add_entry(groups[i]->name(),"subgroups",groupchildren);
    _ini.add_entry(groups[i]->name(),"children",elementchildren);
  }
658

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

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

665 666 667 668 669 670
  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 ());
  }
671

Jan Möbius's avatar
 
Jan Möbius committed
672 673 674
}


Dirk Wilden's avatar
Dirk Wilden committed
675 676 677
//******************************************************************************

/** \brief Recursively update a column up to the root of the tree
678
 *
Jan Möbius's avatar
Jan Möbius committed
679 680
 * @param _obj    Object to start with
 * @param _column Column to propagate
Dirk Wilden's avatar
Dirk Wilden committed
681 682 683
 */
void DataControlPlugin::propagateUpwards(BaseObject* _obj, int _column ){

684
  
Dirk Wilden's avatar
Dirk Wilden committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
  if ( _obj == PluginFunctions::objectRoot() || (!_obj->isGroup()) )
    return;

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


  switch ( _column ){

    case 1: //VISIBILTY

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

      _obj->visible( value );

      changed = true;

      break;

    case 2: //SOURCE-TARGET

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

      if (_obj->source() != value){
        _obj->source( value );
        changed = true;
      }

      if (_obj->target() != value2){
        _obj->target( value2 );
        changed = true;
      }

      break;

    default:
      break;
  }

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

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

/** \brief Recursively update a column up to the root of the tree
737
 *
Jan Möbius's avatar
Jan Möbius committed
738 739
 * @param _obj    object to start with
 * @param _column Column to be propagated
Dirk Wilden's avatar
Dirk Wilden committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
 */
void DataControlPlugin::propagateDownwards(BaseObject* _obj, int _column ){

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

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

    bool changed = false;

    switch ( _column ){

      case 1: //VISIBILTY

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

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

          changed = true;
        }
        break;

      case 2: //SOURCE-TARGET

        if ( current->source() != _obj->source() ){
          current->source( _obj->source() );
          changed = true;
        }

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

        break;

      default:
        break;
    }

Matthias Möller's avatar
Matthias Möller committed
779
    if ( changed || current->isGroup() ){
Dirk Wilden's avatar
Dirk Wilden committed
780 781 782 783 784 785
      propagateDownwards(current, _column);

    }
  }
}

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
//******************************************************************************

/** \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){
834 835 836
  _ini.add_entry("BoundingBox","notSelected",tool_->notSelected->isChecked ());
  _ini.add_entry("BoundingBox","sourceSelected",tool_->sourceSelected->isChecked ());
  _ini.add_entry("BoundingBox","targetSelected",tool_->targetSelected->isChecked ());
837 838
}

Dirk Wilden's avatar
Dirk Wilden committed
839

Jan Möbius's avatar
 
Jan Möbius committed
840 841
Q_EXPORT_PLUGIN2( datacontrolplugin , DataControlPlugin );