Popup.cc 25.7 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
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
\*===========================================================================*/
49 50 51 52 53 54




#include "DataControlPlugin.hh"

Dirk Wilden's avatar
Dirk Wilden committed
55 56
#include <ACG/QtWidgets/QtMaterialDialog.hh>

57
#include "Menu.hh"
58

59 60
#include <ObjectTypes/Light/LightWidget.hh>

Dirk Wilden's avatar
Dirk Wilden committed
61 62 63 64 65
//******************************************************************************

/** \brief Slot for Remove action in ContextMenu
 * 
 */
66
void DataControlPlugin::slotPopupRemove (  ) {
67

68
  QItemSelectionModel* selection = view_->selectionModel();
Dirk Wilden's avatar
Dirk Wilden committed
69 70

  if (selection == 0) return;
71

72
  // Get all selected rows
73 74
  QModelIndexList indexList = selection->selectedRows();

Dirk Wilden's avatar
Dirk Wilden committed
75 76

  QMessageBox msgBox;
77
  msgBox.setText(tr("Do you really want to remove the selected objects?"));
Dirk Wilden's avatar
Dirk Wilden committed
78
  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
79
  msgBox.setDefaultButton(QMessageBox::Yes);
Dirk Wilden's avatar
Dirk Wilden committed
80 81 82

  QString text;

83 84 85
  for ( int i = 0 ; i < indexList.size() ; ++i)
    text += model_->itemName( indexList[i] ) + "\n";

Dirk Wilden's avatar
Dirk Wilden committed
86 87 88 89 90
  msgBox.setDetailedText(text);

  if ( msgBox.exec() == QMessageBox::No)
    return;

91
  for ( int i = 0 ; i < indexList.size() ; ++i) {
92

93
    int id = model_->itemId( indexList[i] );
94 95


96 97
    if ( id != -1 )
      emit deleteObject( id );
98
  }
99

100
  emit updateView();
101 102
}

Dirk Wilden's avatar
Dirk Wilden committed
103 104 105 106 107 108

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

/** \brief Slot for Ungroup action in ContextMenu
 * 
 */
109 110
void DataControlPlugin::slotUngroup (  ) {
  QItemSelectionModel* selection = view_->selectionModel();
111

112
  // Get all selected rows
113
  QModelIndexList indexList = selection->selectedRows ( 0 );
114

115 116
  //get the object
  int id = model_->itemId( indexList[0] );
117
  unGroupObject(id);
118 119
}

Dirk Wilden's avatar
Dirk Wilden committed
120 121 122 123 124 125 126

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

/** \brief Slot for Copy action in ContextMenu
 * 
 */
void DataControlPlugin::slotCopy() {
127 128 129 130 131 132 133 134 135
  QItemSelectionModel* selection = view_->selectionModel();

  if (selection == 0) return;

  // Get all selected rows
  QModelIndexList indexList = selection->selectedRows();

  for ( int i = 0 ; i < indexList.size() ; ++i) {

136 137 138
    int id = model_->itemId( indexList[i] );

    if ( id > 0){
139

140
      int newObject;
141

142 143
      emit copyObject( id, newObject );
    }
144

145 146
  }

147
  emit updateView();
148 149
}

Dirk Wilden's avatar
Dirk Wilden committed
150 151 152 153 154 155 156

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

/** \brief Slot for Group action in ContextMenu
 * 
 */
void DataControlPlugin::slotGroup() {
157

158
  QItemSelectionModel* selection = view_->selectionModel();
159

160
  // Get all selected rows
161
  QModelIndexList indexList = selection->selectedRows ( 0 );
162

163

164 165 166 167
  //get object ids
  IdList ids;
  for (int i = 0; i < indexList.size(); ++i)
    ids.push_back( model_->itemId(indexList[i]) );
168

169

170 171
  //group all objects
  groupObjects(ids);
172 173
  emit objectsGrouped(ids);

174 175
}

Dirk Wilden's avatar
Dirk Wilden committed
176 177 178 179 180 181 182

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

/** \brief ContextMenu requested - creates the contextMenu
 * 
 * @param _pos Position where it was requested
 */
183 184
void DataControlPlugin::slotCustomContextMenuRequested ( const QPoint & _pos ) {
  popupIndex_ = view_->indexAt(_pos);
185

186 187 188
  if (!popupIndex_.isValid())
    return;

189
  TreeItem* item = model_->getItem(popupIndex_);
190

191

192
  QItemSelectionModel* selection = view_->selectionModel();
193

194
  // Get all selected rows
195
  QModelIndexList indexList = selection->selectedRows ( 0 );
196
  int selectedRows = indexList.size();
197

198
  Menu    menu(0);
199 200 201
  QAction* action;
  QIcon icon;
  if ( selectedRows > 1 ) {
202 203 204
    action = menu.addAction("Copy",this,SLOT ( slotCopy() ));
    icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-copy.png");
    action->setIcon(icon);
205
    menu.addAction(tr("Group"),this,SLOT ( slotGroup() ));
206
    menu.addSeparator();
207
    menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
208 209 210
   }else
    // check if the item is a group item
    if ( item->isGroup() ) {
211
      action = menu.addAction(tr("Zoom to objects"),this,SLOT ( slotZoomTo() ));
Dirk Wilden's avatar
Dirk Wilden committed
212 213
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"zoom-in.png");
      action->setIcon(icon);
214
      menu.addAction(tr("Ungroup"),this,SLOT ( slotUngroup() ));
215
      menu.addSeparator();
216
      action = menu.addAction(tr("Rename"),this,SLOT ( slotRename() ));
217 218 219
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-rename.png");
      action->setIcon(icon);
      menu.addSeparator();
220
      action = menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
221
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-delete-item.png");
222 223
      action->setIcon(icon);
    } else {
224 225 226 227 228 229 230 231 232 233 234
      
        //show object name and id in first entry of the menu
        int id = model_->itemId( indexList[0] );
        BaseObject* item = 0;
        if ( id != -1 && PluginFunctions::getObject(id,item) ) {
          QAction* header = menu.addAction(tr("Object ")+QString::number(id)+": "+item->name());
          header->setIcon( typeIcon(item->dataType()) );
        }
        
        menu.addSeparator();
        
235 236 237 238 239 240 241 242 243 244
        if(item->dataType() == DATA_LIGHT) {
            action = menu.addAction(tr("Copy"),this,SLOT ( slotCopy() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-copy.png");
            action->setIcon(icon);
            action = menu.addAction(tr("Rename"),this,SLOT ( slotRename() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-rename.png");
            action->setIcon(icon);
            action = menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-delete-item.png");
            action->setIcon(icon);
245
            menu.addSeparator();
246 247 248 249 250 251 252 253 254 255 256 257 258 259
	    
	    LightObject* light = 0;
	    if ( item->id() != -1 && PluginFunctions::getObject( item->id(), light ) ) {
		if (light != 0 && !light->lightSource()->enabled()) {
		
		    action = menu.addAction(tr("Switch On"),this,SLOT ( slotSwitchLight() ));
		    icon = typeIcon(DATA_LIGHT);
		}
	    }
	    if (light == 0 || light->lightSource()->enabled())
	    {
		action = menu.addAction(tr("Switch Off"),this,SLOT ( slotSwitchLight() ));
		icon = QIcon (OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"LightOff.png");
	    }
260
            action->setIcon(icon);
261
	    
262
            action = menu.addAction(tr("Edit Light"),this,SLOT ( slotEditLight() ));
263
            icon = typeIcon(DATA_LIGHT);
264 265
            action->setIcon(icon);
            
266 267 268 269
        } else {
            action = menu.addAction(tr("Zoom to object"),this,SLOT ( slotZoomTo() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"zoom-in.png");
            action->setIcon(icon);
270

271 272 273
            action = menu.addAction(tr("Copy"),this,SLOT ( slotCopy() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-copy.png");
            action->setIcon(icon);
274

275 276 277
            action = menu.addAction(tr("Rename"),this,SLOT ( slotRename() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-rename.png");
            action->setIcon(icon);
278

279 280 281
            action = menu.addAction(tr("Material Properties"),this,SLOT ( slotMaterialProperties() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-material.png");
            action->setIcon(icon);
282

Matthias Möller's avatar
Matthias Möller committed
283 284 285 286 287 288 289 290 291 292 293
            action = menu.addAction(tr("Copy Material Properties to Targeted Objects"), this, SLOT ( slotCopyMaterialToTargeted() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-copyToTargets-material.png");
            action->setIcon(icon);

            action = menu.addAction(tr("Copy Material Properties to Clipboard"), this, SLOT ( slotCopyMaterialToClipboard() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-copy-material.png");
            action->setIcon(icon);

            action = menu.addAction(tr("Paste Material Properties from Clipboard"), this, SLOT ( slotPasteMaterialFromClipboard() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-paste-material.png");
            action->setIcon(icon);
294

295
            menu.addAction(tr("Group"),this,SLOT ( slotGroup() ));
296

297 298 299 300 301
            menu.addSeparator();
            action = menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-delete-item.png");
            action->setIcon(icon);
        }
302
    }
303 304 305



306
  menu.exec(view_->mapToGlobal( _pos) );
307

308 309
}

Dirk Wilden's avatar
Dirk Wilden committed
310 311 312 313 314 315 316

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

/** \brief Generate a ContextMenu for the header of the TreeView
 * 
 * @param _pos Position where the Menu has to be created
 */
317
void DataControlPlugin::slotHeaderCustomContextMenuRequested ( const QPoint & _pos ) {
318

319
  headerPopupType_ = viewHeader_->logicalIndexAt( _pos );
320

321
  QMenu menu(0);
322

323
  QIcon icon;
324

325 326
  //get all used types
  QVector<DataType> types;
327

328 329 330 331 332 333
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
                                    o_it != PluginFunctions::objectsEnd(); ++o_it)
    if ( !types.contains( o_it->dataType() ) )
      types.push_back( o_it->dataType() );

  QAction* action;
334

335 336 337
  switch (headerPopupType_) {
//     case 0 :
//       std::cerr << "0";
338
//       break;
339 340 341
    //Show / Hide
    case 1 :
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"drawModes.png");
342
      action = menu.addAction(icon,tr("Show all"),this,SLOT ( showAll() ));
343 344 345 346
      action->setData( QVariant() );

      if (types.count() > 1)
      for (int i=0; i < types.count(); i++){
347
        action = menu.addAction(icon, tr("Show all of type '") + dataTypeName(types[i]) + "'",this,SLOT ( showAll() ));
348
        action->setData( QVariant( types[i].value() ) );
349 350 351
      }

      menu.addSeparator();
352
      action = menu.addAction(tr("Hide all"),this,SLOT ( hideAll() ));
353 354 355 356
      action->setData( QVariant() );

      if (types.count() > 1)
      for (int i=0; i < types.count(); i++){
357
        action = menu.addAction(tr("Hide all of type '") + dataTypeName(types[i]) + "'",this,SLOT ( hideAll() ));
358
        action->setData( QVariant( types[i].value() ) );
359 360
      }

361 362 363
      break;
    // Source
    case 2 :
364
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllSource() ));
365 366 367 368
      action->setData( QVariant() );

      if (types.count() > 1)
      for (int i=0; i < types.count(); i++){
369
        action = menu.addAction(icon, tr("Select all of type '") + dataTypeName(types[i]) + "'",this,SLOT ( setAllSource() ));
370
        action->setData( QVariant( types[i].value() ) );
371 372 373
      }

      menu.addSeparator();
374
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllSource() ));
375 376 377 378
      action->setData( QVariant() );

      if (types.count() > 1)
      for (int i=0; i < types.count(); i++){
379
        action = menu.addAction(icon, tr("Deselect all of type '") + dataTypeName(types[i]) + "'",this,SLOT ( clearAllSource() ));
380
        action->setData( QVariant( types[i].value() ) );
381 382
      }

383 384
      break;
    // Target
385
    case 3 :
386
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllTarget() ));
387 388 389 390
      action->setData( QVariant() );

      if (types.count() > 1)
      for (int i=0; i < types.count(); i++){
391
        action = menu.addAction(icon, tr("Select all of type '") + dataTypeName(types[i]) + "'",this,SLOT ( setAllTarget() ));
392
        action->setData( QVariant( types[i].value() ) );
393 394 395
      }

      menu.addSeparator();
396
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllTarget() ));
397 398 399 400
      action->setData( QVariant() );

      if (types.count() > 1)
      for (int i=0; i < types.count(); i++){
401
        action = menu.addAction(icon, tr("Deselect all of type '") + dataTypeName(types[i]) + "'",this,SLOT ( clearAllTarget() ));
402
        action->setData( QVariant( types[i].value() ) );
403 404
      }

405 406 407 408 409
      break;
    default :
//       std::cerr << "def";
      break;
  }
410

411 412 413
  menu.exec(viewHeader_->mapToGlobal( _pos ) );
}

Dirk Wilden's avatar
Dirk Wilden committed
414 415 416 417

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

/** \brief show a rename dialog for object names
418
 *
Dirk Wilden's avatar
Dirk Wilden committed
419
 */
420 421
void DataControlPlugin::slotRename(){
  QItemSelectionModel* selection = view_->selectionModel();
422

423
  // Get all selected rows
424
  QModelIndexList indexList = selection->selectedRows ( 0 );
425 426
  int selectedRows = indexList.size();
  if (selectedRows == 1){
427 428 429 430 431 432 433 434 435 436 437
    int id = model_->itemId( indexList[0]);
    BaseObject* item = 0;

    if ( id != -1 && PluginFunctions::getObject(id,item) ){

      bool ok;
      QString newName = QInputDialog::getText(0, tr("Rename"),
                                              tr("Enter a new name:"), QLineEdit::Normal,
                                              item->name(), &ok);
      if (ok && !newName.isEmpty()){
        item->setName(newName);
438
        view_->resizeColumnToContents(0);
439
      }
Dirk Wilden's avatar
Dirk Wilden committed
440
    }
441 442
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
443

Dirk Wilden's avatar
Dirk Wilden committed
444 445 446 447 448 449

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

/** \brief show the material properties dialog
 * 
 */
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
void DataControlPlugin::slotCopyMaterialToTargeted() {
    BaseObject* item = 0;

    //check if it was called from object contextMenu or from the toolBox
    QAction* action = dynamic_cast< QAction* > ( sender() );

    if ( action ){
      bool ok = false;

      int id = action->data().toInt(&ok);

      if ( ok && id > 0 )
        PluginFunctions::getObject(id,item);
    }

    if ( item == 0 ){
      // the slot was called from toolbox
      QItemSelectionModel* selection = view_->selectionModel();

      // Get all selected rows
      QModelIndexList indexList = selection->selectedRows ( 0 );
      int selectedRows = indexList.size();
      if (selectedRows == 1){
        int id = model_->itemId( indexList[0] );

        if ( id > 0 )
          PluginFunctions::getObject(id,item);
      }
    }

    if ( item != 0 ){

      BaseObjectData* itemData = dynamic_cast< BaseObjectData* > (item);
      const ACG::SceneGraph::Material &sourceMaterial = itemData->materialNode()->material();

      for (PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS);
              o_it != PluginFunctions::objectsEnd(); ++o_it) {

          MaterialNode * const materialNode = o_it->materialNode();
          if (materialNode) {
              materialNode->material() = sourceMaterial;
          }
      }

      emit updateView();
    }
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
void DataControlPlugin::slotCopyMaterialToClipboard() {
    BaseObject* item = 0;

    //check if it was called from object contextMenu or from the toolBox
    QAction* action = dynamic_cast< QAction* > ( sender() );

    if ( action ){
      bool ok = false;

      int id = action->data().toInt(&ok);

      if ( ok && id > 0 )
        PluginFunctions::getObject(id,item);
    }

    if ( item == 0 ){
      // the slot was called from toolbox
      QItemSelectionModel* selection = view_->selectionModel();

      // Get all selected rows
      QModelIndexList indexList = selection->selectedRows ( 0 );
      int selectedRows = indexList.size();
      if (selectedRows == 1){
        int id = model_->itemId( indexList[0] );

        if ( id > 0 )
          PluginFunctions::getObject(id,item);
      }
    }

    if ( item != 0 ){

      BaseObjectData* itemData = dynamic_cast< BaseObjectData* > (item);
      const ACG::SceneGraph::Material &sourceMaterial = itemData->materialNode()->material();

      const QString materialJson = sourceMaterial.serializeToJson();
      QApplication::clipboard()->setText(materialJson);
    }
}

void DataControlPlugin::slotPasteMaterialFromClipboard() {
    BaseObject* item = 0;

    //check if it was called from object contextMenu or from the toolBox
    QAction* action = dynamic_cast< QAction* > ( sender() );

    if ( action ){
      bool ok = false;

      int id = action->data().toInt(&ok);

      if ( ok && id > 0 )
        PluginFunctions::getObject(id,item);
    }

    if ( item == 0 ){
      // the slot was called from toolbox
      QItemSelectionModel* selection = view_->selectionModel();

      // Get all selected rows
      QModelIndexList indexList = selection->selectedRows ( 0 );
      int selectedRows = indexList.size();
      if (selectedRows == 1){
        int id = model_->itemId( indexList[0] );

        if ( id > 0 )
          PluginFunctions::getObject(id,item);
      }
    }

    if ( item != 0 ){

      BaseObjectData* itemData = dynamic_cast< BaseObjectData* > (item);
      ACG::SceneGraph::Material &destMaterial = itemData->materialNode()->material();

      destMaterial.deserializeFromJson(QApplication::clipboard()->text());

      emit updateView();
    }
}

Dirk Wilden's avatar
Dirk Wilden committed
579 580
void DataControlPlugin::slotMaterialProperties(){

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
  BaseObject* item = 0;

  //check if it was called from object contextMenu or from the toolBox
  QAction* action = dynamic_cast< QAction* > ( sender() );

  if ( action ){
    bool ok = false;

    int id = action->data().toInt(&ok);

    if ( ok && id > 0 )
      PluginFunctions::getObject(id,item);
  }

  if ( item == 0 ){
    // the slot was called from toolbox
    QItemSelectionModel* selection = view_->selectionModel();

    // Get all selected rows
    QModelIndexList indexList = selection->selectedRows ( 0 );
    int selectedRows = indexList.size();
602 603 604 605 606 607
    if (selectedRows == 1){
      int id = model_->itemId( indexList[0] );

      if ( id > 0 )
        PluginFunctions::getObject(id,item);
    }
608 609 610
  }

  if ( item != 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

    BaseObjectData* itemData = dynamic_cast< BaseObjectData* > (item);

    ACG::QtWidgets::QtMaterialDialog* dialog = new ACG::QtWidgets::QtMaterialDialog( 0, itemData->materialNode() );

    dialog->setWindowFlags(dialog->windowFlags() | Qt::WindowStaysOnTopHint);

    connect(dialog, SIGNAL(signalNodeChanged(ACG::SceneGraph::BaseNode*)),
            this,   SLOT(slotNodeChanged(ACG::SceneGraph::BaseNode*)) );

    dialog->setWindowIcon( QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-material.png"));

    dialog->show();

  }
}

Dirk Wilden's avatar
Dirk Wilden committed
628 629 630

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

Dirk Wilden's avatar
Dirk Wilden committed
631
void DataControlPlugin::slotNodeChanged( ACG::SceneGraph::BaseNode* /*_node*/ ){
632
  emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
633 634
}

Dirk Wilden's avatar
Dirk Wilden committed
635 636
//******************************************************************************

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
/**
 * \brief Switch light source on or off
 */
void DataControlPlugin::slotSwitchLight() {
    
    QItemSelectionModel* selection = view_->selectionModel();
    
    // Get all selected rows
    QModelIndexList indexList = selection->selectedRows ( 0 );
    int selectedRows = indexList.size();
    if (selectedRows == 1) {
        
        LightObject* light = 0;
        int id = model_->itemId( indexList[0]);
        
        if (id == -1 || !PluginFunctions::getObject( id, light ) )
            return;
        
        if(light == 0) return;
        
        light->lightSource()->enabled() ?
            light->lightSource()->disable() :
            light->lightSource()->enable();
            
661
        emit updatedObject(id, UPDATE_ALL);
662 663 664 665 666
    }
}

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

667
/**
668
* \brief Edit the light source's parameters
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
*/
void DataControlPlugin::slotEditLight() {
 
  QItemSelectionModel* selection = view_->selectionModel();
  
  // Get all selected rows
  QModelIndexList indexList = selection->selectedRows ( 0 );
  int selectedRows = indexList.size();
  if (selectedRows == 1) {
    
    LightObject* light = 0;
    int id = model_->itemId( indexList[0]);
    
    if (id == -1 || !PluginFunctions::getObject( id, light ) )
      return;
    
    if(light == 0) return;
    
    LightWidget* widget = new LightWidget( light->lightNode() );
688
    widget->setWindowFlags(Qt::WindowStaysOnTopHint);
689 690 691 692 693 694
    widget->show();
  }
}

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

Dirk Wilden's avatar
Dirk Wilden committed
695 696 697
/** \brief Zoom to an object
 * 
 */
Dirk Wilden's avatar
Dirk Wilden committed
698 699
void DataControlPlugin::slotZoomTo(){
  QItemSelectionModel* selection = view_->selectionModel();
700

Dirk Wilden's avatar
Dirk Wilden committed
701
  // Get all selected rows
702
  QModelIndexList indexList = selection->selectedRows ( 0 );
Dirk Wilden's avatar
Dirk Wilden committed
703 704
  int selectedRows = indexList.size();
  if (selectedRows == 1){
705 706 707 708 709
    BaseObject* item = 0;
    int id = model_->itemId( indexList[0]);

    if (id == -1 || !PluginFunctions::getObject( id, item) )
      return;
Dirk Wilden's avatar
Dirk Wilden committed
710 711 712 713 714 715 716

    if ( item->isGroup() ) {
      //zoom to all objects in this group
      QList< BaseObject* > children = item->getLeafs();

      //compute boundingBox
      bool firstRound = true;
717 718
      ACG::Vec3d bbmin(FLT_MAX,FLT_MAX,FLT_MAX);
      ACG::Vec3d bbmax(FLT_MIN,FLT_MIN,FLT_MIN);
Dirk Wilden's avatar
Dirk Wilden committed
719 720 721 722

      for (int i=0; i < children.size(); i++){
        BaseObjectData* child = dynamic_cast< BaseObjectData* > (children[i]);
        if (child){
723 724 725 726 727

          if ( !child->visible() ){
            child->show();
          }

Dirk Wilden's avatar
Dirk Wilden committed
728 729
          ACG::Vec3d cur_min;
          ACG::Vec3d cur_max;
730

Dirk Wilden's avatar
Dirk Wilden committed
731
          child->getBoundingBox(cur_min, cur_max);
732

Dirk Wilden's avatar
Dirk Wilden committed
733 734 735 736 737 738 739 740 741 742 743 744
          if (firstRound){
            bbmin = cur_min;
            bbmax = cur_max;
            firstRound = false;
          }else{
            bbmin[0] = std::min( bbmin[0], cur_min[0]);
            bbmin[1] = std::min( bbmin[1], cur_min[1]);
            bbmin[2] = std::min( bbmin[2], cur_min[2]);
            bbmax[0] = std::max( bbmax[0], cur_max[0]);
            bbmax[1] = std::max( bbmax[0], cur_max[1]);
            bbmax[2] = std::max( bbmax[0], cur_max[2]);
          }
745

Dirk Wilden's avatar
Dirk Wilden committed
746 747 748
        }
      }
      //zoom to objects
Dirk Wilden's avatar
Dirk Wilden committed
749
      ACG::Vec3d bbcenter = (bbmax + bbmin) * 0.5;
750

Dirk Wilden's avatar
Dirk Wilden committed
751
      double bbradius = (bbmax - bbmin).norm();
752

Dirk Wilden's avatar
Dirk Wilden committed
753
      ACG::Vec3d eye = bbcenter + (PluginFunctions::eyePos() - bbcenter).normalize() * bbradius ;
754

Dirk Wilden's avatar
Dirk Wilden committed
755
      PluginFunctions::flyTo(eye, bbcenter );
756

Dirk Wilden's avatar
Dirk Wilden committed
757 758 759
    }else{
      //zoom to object
      BaseObjectData* obj = dynamic_cast< BaseObjectData* >(item);
760

Dirk Wilden's avatar
Dirk Wilden committed
761
      if (obj){
762

763 764 765
        if ( !obj->visible() )
          obj->show();

Dirk Wilden's avatar
Dirk Wilden committed
766 767
        ACG::Vec3d bbmin;
        ACG::Vec3d bbmax;
768

Dirk Wilden's avatar
Dirk Wilden committed
769
        obj->getBoundingBox(bbmin, bbmax);
770

771
        if ((bbmin[0] > bbmax[0]) || (bbmin[1] > bbmax[1]) || (bbmin[2] > bbmax[2])){
Dirk Wilden's avatar
Dirk Wilden committed
772
          std::cerr << "Error while computing bounding box!";
773 774
          return;
        }
775

Dirk Wilden's avatar
Dirk Wilden committed
776
        ACG::Vec3d bbcenter = (bbmax + bbmin) * 0.5;
777

Dirk Wilden's avatar
Dirk Wilden committed
778
        double bbradius = (bbmax - bbmin).norm();
779

Dirk Wilden's avatar
Dirk Wilden committed
780
        ACG::Vec3d eye = bbcenter + (PluginFunctions::eyePos() - bbcenter).normalize() * bbradius ;
781

Dirk Wilden's avatar
Dirk Wilden committed
782 783 784 785 786
        PluginFunctions::flyTo(eye, bbcenter );
      }
    }
  }
}