Popup.cc 21.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

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




#include "DataControlPlugin.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>

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

Jan Möbius's avatar
 
Jan Möbius committed
53
#include <QInputDialog>
Dirk Wilden's avatar
Dirk Wilden committed
54
#include <QMessageBox>
55
#include "Menu.hh"
Jan Möbius's avatar
 
Jan Möbius committed
56

57
58
#include <ObjectTypes/Light/LightWidget.hh>

Dirk Wilden's avatar
Dirk Wilden committed
59
60
61
62
63
64

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

/** \brief Slot for Remove action in ContextMenu
 * 
 */
Jan Möbius's avatar
 
Jan Möbius committed
65
void DataControlPlugin::slotPopupRemove (  ) {
Dirk Wilden's avatar
Dirk Wilden committed
66

Jan Möbius's avatar
 
Jan Möbius committed
67
  QItemSelectionModel* selection = view_->selectionModel();
Dirk Wilden's avatar
Dirk Wilden committed
68
69

  if (selection == 0) return;
70

Jan Möbius's avatar
 
Jan Möbius committed
71
  // Get all selected rows
72
73
  QModelIndexList indexList = selection->selectedRows();

Dirk Wilden's avatar
Dirk Wilden committed
74
75

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

  QString text;

Dirk Wilden's avatar
Dirk Wilden committed
82
83
84
  for ( int i = 0 ; i < indexList.size() ; ++i)
    text += model_->itemName( indexList[i] ) + "\n";

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

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

Jan Möbius's avatar
 
Jan Möbius committed
90
  for ( int i = 0 ; i < indexList.size() ; ++i) {
91

Dirk Wilden's avatar
Dirk Wilden committed
92
    int id = model_->itemId( indexList[i] );
93
94


Dirk Wilden's avatar
Dirk Wilden committed
95
96
    if ( id != -1 )
      emit deleteObject( id );
Jan Möbius's avatar
 
Jan Möbius committed
97
  }
98

Jan Möbius's avatar
   
Jan Möbius committed
99
  emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
100
101
}

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

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

/** \brief Slot for Ungroup action in ContextMenu
 * 
 */
Jan Möbius's avatar
 
Jan Möbius committed
108
109
void DataControlPlugin::slotUngroup (  ) {
  QItemSelectionModel* selection = view_->selectionModel();
110

Jan Möbius's avatar
 
Jan Möbius committed
111
  // Get all selected rows
112
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
113

Dirk Wilden's avatar
Dirk Wilden committed
114
115
116
  //get the object
  int id = model_->itemId( indexList[0] );
  BaseObject* group = 0;
Jan Möbius's avatar
 
Jan Möbius committed
117

Dirk Wilden's avatar
Dirk Wilden committed
118
119
120
121
  if ( id > 0 && PluginFunctions::getObject(id, group ) ){
    //iterate over children
    for (int i=group->childCount()-1; i >= 0; i--){
      BaseObject* child = group->child(i);
Jan Möbius's avatar
 
Jan Möbius committed
122

Dirk Wilden's avatar
Dirk Wilden committed
123
124
125
      // then change the parent
      child->setParent(group->parent());
    }
Jan Möbius's avatar
 
Jan Möbius committed
126

Dirk Wilden's avatar
Dirk Wilden committed
127
128
129
    //delete the group
    emit deleteObject( group->id() );
  }
Jan Möbius's avatar
 
Jan Möbius committed
130
131
}

Dirk Wilden's avatar
Dirk Wilden committed
132
133
134
135
136
137
138

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

/** \brief Slot for Copy action in ContextMenu
 * 
 */
void DataControlPlugin::slotCopy() {
139
140
141
142
143
144
145
146
147
  QItemSelectionModel* selection = view_->selectionModel();

  if (selection == 0) return;

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

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

Dirk Wilden's avatar
Dirk Wilden committed
148
149
150
    int id = model_->itemId( indexList[i] );

    if ( id > 0){
151

Dirk Wilden's avatar
Dirk Wilden committed
152
      int newObject;
153

Dirk Wilden's avatar
Dirk Wilden committed
154
155
      emit copyObject( id, newObject );
    }
156

157
158
  }

Jan Möbius's avatar
   
Jan Möbius committed
159
  emit updateView();
160
161
}

Dirk Wilden's avatar
Dirk Wilden committed
162
163
164
165
166
167
168

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

/** \brief Slot for Group action in ContextMenu
 * 
 */
void DataControlPlugin::slotGroup() {
Dirk Wilden's avatar
Dirk Wilden committed
169

Jan Möbius's avatar
 
Jan Möbius committed
170
  QItemSelectionModel* selection = view_->selectionModel();
171

Jan Möbius's avatar
 
Jan Möbius committed
172
  // Get all selected rows
173
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
174

175
  //check if all objects have the same parent
Jan Möbius's avatar
 
Jan Möbius committed
176
  //abort if the parents differ
177

Dirk Wilden's avatar
Dirk Wilden committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  int id = model_->itemId( indexList[0] );
  BaseObject* obj0 = 0;

  if (id == -1 || !PluginFunctions::getObject(id, obj0) ){
    std::cerr << "Group Objects: Unable to get object" << std::endl;
    return;
  }

  bool visible = obj0->visible();
  bool target  = obj0->target();
  bool source  = obj0->source();

  BaseObject* parent = obj0->parent();

Jan Möbius's avatar
 
Jan Möbius committed
192
  for ( int i = 1 ; i < indexList.size() ; ++i) {
Dirk Wilden's avatar
Dirk Wilden committed
193
194
195
196
197
198
199
200
201

    //get an object
    id = model_->itemId( indexList[i] );
    BaseObject* item = 0;

    PluginFunctions::getObject(id, item );

    //compare parent
    if ( parent != item->parent() ){
Jan Möbius's avatar
Jan Möbius committed
202
      emit log(tr("Cannot group Objects with different parents"));
Jan Möbius's avatar
 
Jan Möbius committed
203
204
      return;
    }
205
206

    //remember if at least on child was target/source
Dirk Wilden's avatar
Dirk Wilden committed
207
208
209
    visible |= item->visible();
    target  |= item->target();
    source  |= item->source();
Jan Möbius's avatar
 
Jan Möbius committed
210
211
212
213
214
  }

  //create new group
  if (parent == 0)
    parent = PluginFunctions::objectRoot();
Jan Möbius's avatar
Jan Möbius committed
215
216
  GroupObject* groupItem = new GroupObject( tr("newGroup"), dynamic_cast< GroupObject* >(parent));
  groupItem->setName(tr("newGroup ") + QString::number(groupItem->id()));
Jan Möbius's avatar
 
Jan Möbius committed
217
218
219
  parent->appendChild( dynamic_cast< BaseObject* >( groupItem ) );
  groupItem->setParent( parent );

Dirk Wilden's avatar
Dirk Wilden committed
220
221
  emit emptyObjectAdded( groupItem->id() );

Jan Möbius's avatar
 
Jan Möbius committed
222
223
  //append new children to group
  for ( int i = 0 ; i < indexList.size() ; ++i) {
Dirk Wilden's avatar
Dirk Wilden committed
224
225
226
227
228
229
230
231

    //get an object
    id = model_->itemId( indexList[i] );
    BaseObject* item = 0;

    PluginFunctions::getObject(id, item );

    //and move it into the group
Jan Möbius's avatar
 
Jan Möbius committed
232
233
    item->setParent( dynamic_cast< BaseObject* >( groupItem )  );
  }
234

235
  //update target/source state
Dirk Wilden's avatar
Dirk Wilden committed
236
  groupItem->visible(visible);
237
238
  groupItem->target(target);
  groupItem->source(source);
Jan Möbius's avatar
 
Jan Möbius committed
239
240
}

Dirk Wilden's avatar
Dirk Wilden committed
241
242
243
244
245
246
247

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

/** \brief ContextMenu requested - creates the contextMenu
 * 
 * @param _pos Position where it was requested
 */
Jan Möbius's avatar
 
Jan Möbius committed
248
249
void DataControlPlugin::slotCustomContextMenuRequested ( const QPoint & _pos ) {
  popupIndex_ = view_->indexAt(_pos);
250

251
252
253
  if (!popupIndex_.isValid())
    return;

Dirk Wilden's avatar
Dirk Wilden committed
254
  TreeItem* item = model_->getItem(popupIndex_);
255

256

Jan Möbius's avatar
 
Jan Möbius committed
257
  QItemSelectionModel* selection = view_->selectionModel();
258

Jan Möbius's avatar
 
Jan Möbius committed
259
  // Get all selected rows
260
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
261
  int selectedRows = indexList.size();
262

263
  Menu    menu(0);
Jan Möbius's avatar
 
Jan Möbius committed
264
265
266
  QAction* action;
  QIcon icon;
  if ( selectedRows > 1 ) {
267
268
269
    action = menu.addAction("Copy",this,SLOT ( slotCopy() ));
    icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-copy.png");
    action->setIcon(icon);
Jan Möbius's avatar
Jan Möbius committed
270
    menu.addAction(tr("Group"),this,SLOT ( slotGroup() ));
Jan Möbius's avatar
 
Jan Möbius committed
271
    menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
272
    menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
Jan Möbius's avatar
 
Jan Möbius committed
273
274
275
   }else
    // check if the item is a group item
    if ( item->isGroup() ) {
Jan Möbius's avatar
Jan Möbius committed
276
      action = menu.addAction(tr("Zoom to objects"),this,SLOT ( slotZoomTo() ));
Dirk Wilden's avatar
Dirk Wilden committed
277
278
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"zoom-in.png");
      action->setIcon(icon);
Jan Möbius's avatar
Jan Möbius committed
279
      menu.addAction(tr("Ungroup"),this,SLOT ( slotUngroup() ));
Jan Möbius's avatar
 
Jan Möbius committed
280
      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
281
      action = menu.addAction(tr("Rename"),this,SLOT ( slotRename() ));
Jan Möbius's avatar
 
Jan Möbius committed
282
283
284
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-rename.png");
      action->setIcon(icon);
      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
285
      action = menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
Jan Möbius's avatar
   
Jan Möbius committed
286
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-delete-item.png");
Jan Möbius's avatar
 
Jan Möbius committed
287
288
      action->setIcon(icon);
    } else {
289
290
291
292
293
294
295
296
297
298
        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);
299
            menu.addSeparator();
300
            action = menu.addAction(tr("Switch On/Off"),this,SLOT ( slotSwitchLight() ));
301
            icon = typeIcon(DATA_LIGHT);
302
            action->setIcon(icon);
303
            action = menu.addAction(tr("Edit Light"),this,SLOT ( slotEditLight() ));
304
            icon = typeIcon(DATA_LIGHT);
305
306
            action->setIcon(icon);
            
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
        } 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);
            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("Material Properties"),this,SLOT ( slotMaterialProperties() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-material.png");
            action->setIcon(icon);
            menu.addAction(tr("Group"),this,SLOT ( slotGroup() ));
            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);
        }
Jan Möbius's avatar
 
Jan Möbius committed
326
    }
327
328
329



Jan Möbius's avatar
 
Jan Möbius committed
330
  menu.exec(view_->mapToGlobal( _pos) );
331

Jan Möbius's avatar
 
Jan Möbius committed
332
333
}

Dirk Wilden's avatar
Dirk Wilden committed
334
335
336
337
338
339
340

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

/** \brief Generate a ContextMenu for the header of the TreeView
 * 
 * @param _pos Position where the Menu has to be created
 */
Jan Möbius's avatar
 
Jan Möbius committed
341
void DataControlPlugin::slotHeaderCustomContextMenuRequested ( const QPoint & _pos ) {
342

Jan Möbius's avatar
 
Jan Möbius committed
343
  headerPopupType_ = viewHeader_->logicalIndexAt( _pos );
344

Jan Möbius's avatar
 
Jan Möbius committed
345
  QMenu menu(0);
346

Jan Möbius's avatar
 
Jan Möbius committed
347
  QIcon icon;
348

349
350
  //get all used types
  QVector<DataType> types;
351

352
353
354
355
356
357
  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;
358

Jan Möbius's avatar
 
Jan Möbius committed
359
360
361
  switch (headerPopupType_) {
//     case 0 :
//       std::cerr << "0";
362
//       break;
Jan Möbius's avatar
 
Jan Möbius committed
363
364
365
    //Show / Hide
    case 1 :
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"drawModes.png");
Jan Möbius's avatar
Jan Möbius committed
366
      action = menu.addAction(icon,tr("Show all"),this,SLOT ( showAll() ));
367
368
369
370
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
376
      action = menu.addAction(tr("Hide all"),this,SLOT ( hideAll() ));
377
378
379
380
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
385
386
387
      break;
    // Source
    case 2 :
Jan Möbius's avatar
Jan Möbius committed
388
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllSource() ));
389
390
391
392
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
398
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllSource() ));
399
400
401
402
      action->setData( QVariant() );

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

407
408
      break;
    // Target
Jan Möbius's avatar
 
Jan Möbius committed
409
    case 3 :
Jan Möbius's avatar
Jan Möbius committed
410
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllTarget() ));
411
412
413
414
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
420
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllTarget() ));
421
422
423
424
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
429
430
431
432
433
      break;
    default :
//       std::cerr << "def";
      break;
  }
434

Jan Möbius's avatar
 
Jan Möbius committed
435
436
437
  menu.exec(viewHeader_->mapToGlobal( _pos ) );
}

Dirk Wilden's avatar
Dirk Wilden committed
438
439
440
441
442
443

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

/** \brief show a rename dialog for object names
 * 
 */
Jan Möbius's avatar
 
Jan Möbius committed
444
445
void DataControlPlugin::slotRename(){
  QItemSelectionModel* selection = view_->selectionModel();
446

Jan Möbius's avatar
 
Jan Möbius committed
447
  // Get all selected rows
448
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
449
450
  int selectedRows = indexList.size();
  if (selectedRows == 1){
Dirk Wilden's avatar
Dirk Wilden committed
451
452
453
454
455
456
457
458
459
460
461
462
    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);
      }
Dirk Wilden's avatar
Dirk Wilden committed
463
    }
Jan Möbius's avatar
 
Jan Möbius committed
464
465
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
466

Dirk Wilden's avatar
Dirk Wilden committed
467
468
469
470
471
472

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

/** \brief show the material properties dialog
 * 
 */
Dirk Wilden's avatar
Dirk Wilden committed
473
474
void DataControlPlugin::slotMaterialProperties(){

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  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();
Dirk Wilden's avatar
Dirk Wilden committed
496
497
498
499
500
501
    if (selectedRows == 1){
      int id = model_->itemId( indexList[0] );

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

  if ( item != 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

    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
522
523
524
525
526
527
528

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

/** \brief Called when the material properties were changed inside the material dialog
 * 
 * @param  unused
 */
Dirk Wilden's avatar
Dirk Wilden committed
529
void DataControlPlugin::slotNodeChanged( ACG::SceneGraph::BaseNode* /*_node*/ ){
Jan Möbius's avatar
   
Jan Möbius committed
530
  emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
531
532
}

Dirk Wilden's avatar
Dirk Wilden committed
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
/**
 * \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();
            
559
        emit updatedObject(id, UPDATE_ALL);
560
561
562
563
564
    }
}

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

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* \brief Switch light source on or off
*/
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() );
    widget->show();
  }
}

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

Dirk Wilden's avatar
Dirk Wilden committed
592
593
594
/** \brief Zoom to an object
 * 
 */
Dirk Wilden's avatar
Dirk Wilden committed
595
596
void DataControlPlugin::slotZoomTo(){
  QItemSelectionModel* selection = view_->selectionModel();
597

Dirk Wilden's avatar
Dirk Wilden committed
598
  // Get all selected rows
599
  QModelIndexList indexList = selection->selectedRows ( 0 );
Dirk Wilden's avatar
Dirk Wilden committed
600
601
  int selectedRows = indexList.size();
  if (selectedRows == 1){
Dirk Wilden's avatar
Dirk Wilden committed
602
603
604
605
606
    BaseObject* item = 0;
    int id = model_->itemId( indexList[0]);

    if (id == -1 || !PluginFunctions::getObject( id, item) )
      return;
Dirk Wilden's avatar
Dirk Wilden committed
607
608
609
610
611
612
613
614
615
616
617
618
619

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

      //compute boundingBox
      bool firstRound = true;
      ACG::Vec3d bbmin;
      ACG::Vec3d bbmax;

      for (int i=0; i < children.size(); i++){
        BaseObjectData* child = dynamic_cast< BaseObjectData* > (children[i]);
        if (child){
620
621
622
623
624

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

Dirk Wilden's avatar
Dirk Wilden committed
625
626
          ACG::Vec3d cur_min;
          ACG::Vec3d cur_max;
627

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

Dirk Wilden's avatar
Dirk Wilden committed
630
631
632
633
634
635
636
637
638
639
640
641
          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]);
          }
642

Dirk Wilden's avatar
Dirk Wilden committed
643
644
645
        }
      }
      //zoom to objects
Dirk Wilden's avatar
Dirk Wilden committed
646
      ACG::Vec3d bbcenter = (bbmax + bbmin) * 0.5;
647

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

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

Dirk Wilden's avatar
Dirk Wilden committed
652
      PluginFunctions::flyTo(eye, bbcenter );
653

Dirk Wilden's avatar
Dirk Wilden committed
654
655
656
    }else{
      //zoom to object
      BaseObjectData* obj = dynamic_cast< BaseObjectData* >(item);
657

658
659
660
661
      if ( !obj->visible() ){
        obj->show();
      }

Dirk Wilden's avatar
Dirk Wilden committed
662
      if (obj){
663

Dirk Wilden's avatar
Dirk Wilden committed
664
665
        ACG::Vec3d bbmin;
        ACG::Vec3d bbmax;
666

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

669
        if ((bbmin[0] > bbmax[0]) || (bbmin[1] > bbmax[1]) || (bbmin[2] > bbmax[2])){
Dirk Wilden's avatar
Dirk Wilden committed
670
          std::cerr << "Error while computing bounding box!";
671
672
          return;
        }
673

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

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

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

Dirk Wilden's avatar
Dirk Wilden committed
680
681
682
683
684
        PluginFunctions::flyTo(eye, bbcenter );
      }
    }
  }
}