Popup.cc 22.2 KB
Newer Older
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
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
 *                           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
	//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) )
	menu.addAction(tr("Object ")+QString::number(id)+": "+item->name());
294
	menu.addSeparator();
295
	
296
297
298
299
300
301
302
303
304
305
        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);
306
            menu.addSeparator();
307
            action = menu.addAction(tr("Switch On/Off"),this,SLOT ( slotSwitchLight() ));
308
            icon = typeIcon(DATA_LIGHT);
309
            action->setIcon(icon);
310
            action = menu.addAction(tr("Edit Light"),this,SLOT ( slotEditLight() ));
311
            icon = typeIcon(DATA_LIGHT);
312
313
            action->setIcon(icon);
            
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
        } 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
333
    }
334
335
336



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

Jan Möbius's avatar
 
Jan Möbius committed
339
340
}

Dirk Wilden's avatar
Dirk Wilden committed
341
342
343
344
345
346
347

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

/** \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
348
void DataControlPlugin::slotHeaderCustomContextMenuRequested ( const QPoint & _pos ) {
349

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

Jan Möbius's avatar
 
Jan Möbius committed
352
  QMenu menu(0);
353

Jan Möbius's avatar
 
Jan Möbius committed
354
  QIcon icon;
355

356
357
  //get all used types
  QVector<DataType> types;
358

359
360
361
362
363
364
  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;
365

Jan Möbius's avatar
 
Jan Möbius committed
366
367
368
  switch (headerPopupType_) {
//     case 0 :
//       std::cerr << "0";
369
//       break;
Jan Möbius's avatar
 
Jan Möbius committed
370
371
372
    //Show / Hide
    case 1 :
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"drawModes.png");
Jan Möbius's avatar
Jan Möbius committed
373
      action = menu.addAction(icon,tr("Show all"),this,SLOT ( showAll() ));
374
375
376
377
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
383
      action = menu.addAction(tr("Hide all"),this,SLOT ( hideAll() ));
384
385
386
387
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
392
393
394
      break;
    // Source
    case 2 :
Jan Möbius's avatar
Jan Möbius committed
395
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllSource() ));
396
397
398
399
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
405
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllSource() ));
406
407
408
409
      action->setData( QVariant() );

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

414
415
      break;
    // Target
Jan Möbius's avatar
 
Jan Möbius committed
416
    case 3 :
Jan Möbius's avatar
Jan Möbius committed
417
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllTarget() ));
418
419
420
421
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
427
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllTarget() ));
428
429
430
431
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
436
437
438
439
440
      break;
    default :
//       std::cerr << "def";
      break;
  }
441

Jan Möbius's avatar
 
Jan Möbius committed
442
443
444
  menu.exec(viewHeader_->mapToGlobal( _pos ) );
}

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
454
  // Get all selected rows
455
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
456
457
  int selectedRows = indexList.size();
  if (selectedRows == 1){
Dirk Wilden's avatar
Dirk Wilden committed
458
459
460
461
462
463
464
465
466
467
468
469
    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
470
    }
Jan Möbius's avatar
 
Jan Möbius committed
471
472
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
473

Dirk Wilden's avatar
Dirk Wilden committed
474
475
476
477
478
479

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

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

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  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
503
504
505
506
507
508
    if (selectedRows == 1){
      int id = model_->itemId( indexList[0] );

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

  if ( item != 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528

    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
529
530
531
532
533
534
535

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

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

Dirk Wilden's avatar
Dirk Wilden committed
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
/**
 * \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();
            
566
        emit updatedObject(id, UPDATE_ALL);
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
592
/**
* \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() );
593
    widget->setWindowFlags(Qt::WindowStaysOnTopHint);
594
595
596
597
598
599
    widget->show();
  }
}

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

Dirk Wilden's avatar
Dirk Wilden committed
600
601
602
/** \brief Zoom to an object
 * 
 */
Dirk Wilden's avatar
Dirk Wilden committed
603
604
void DataControlPlugin::slotZoomTo(){
  QItemSelectionModel* selection = view_->selectionModel();
605

Dirk Wilden's avatar
Dirk Wilden committed
606
  // Get all selected rows
607
  QModelIndexList indexList = selection->selectedRows ( 0 );
Dirk Wilden's avatar
Dirk Wilden committed
608
609
  int selectedRows = indexList.size();
  if (selectedRows == 1){
Dirk Wilden's avatar
Dirk Wilden committed
610
611
612
613
614
    BaseObject* item = 0;
    int id = model_->itemId( indexList[0]);

    if (id == -1 || !PluginFunctions::getObject( id, item) )
      return;
Dirk Wilden's avatar
Dirk Wilden committed
615
616
617
618
619
620
621
622
623
624
625
626
627

    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){
628
629
630
631
632

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

Dirk Wilden's avatar
Dirk Wilden committed
633
634
          ACG::Vec3d cur_min;
          ACG::Vec3d cur_max;
635

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

Dirk Wilden's avatar
Dirk Wilden committed
638
639
640
641
642
643
644
645
646
647
648
649
          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]);
          }
650

Dirk Wilden's avatar
Dirk Wilden committed
651
652
653
        }
      }
      //zoom to objects
Dirk Wilden's avatar
Dirk Wilden committed
654
      ACG::Vec3d bbcenter = (bbmax + bbmin) * 0.5;
655

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

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

Dirk Wilden's avatar
Dirk Wilden committed
660
      PluginFunctions::flyTo(eye, bbcenter );
661

Dirk Wilden's avatar
Dirk Wilden committed
662
663
664
    }else{
      //zoom to object
      BaseObjectData* obj = dynamic_cast< BaseObjectData* >(item);
665

666
667
668
669
      if ( !obj->visible() ){
        obj->show();
      }

Dirk Wilden's avatar
Dirk Wilden committed
670
      if (obj){
671

Dirk Wilden's avatar
Dirk Wilden committed
672
673
        ACG::Vec3d bbmin;
        ACG::Vec3d bbmax;
674

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

677
        if ((bbmin[0] > bbmax[0]) || (bbmin[1] > bbmax[1]) || (bbmin[2] > bbmax[2])){
Dirk Wilden's avatar
Dirk Wilden committed
678
          std::cerr << "Error while computing bounding box!";
679
680
          return;
        }
681

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

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

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

Dirk Wilden's avatar
Dirk Wilden committed
688
689
690
691
692
        PluginFunctions::flyTo(eye, bbcenter );
      }
    }
  }
}