Popup.cc 22.8 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
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
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2010 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/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

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




#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 {
Jan Möbius's avatar
Jan Möbius committed
289
290
291
292
293
294
295
296
297
298
299
      
        //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();
        
300
301
302
303
304
305
306
307
308
309
        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);
310
            menu.addSeparator();
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	    
	    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");
	    }
325
            action->setIcon(icon);
326
	    
327
            action = menu.addAction(tr("Edit Light"),this,SLOT ( slotEditLight() ));
328
            icon = typeIcon(DATA_LIGHT);
329
330
            action->setIcon(icon);
            
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
        } 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
350
    }
351
352
353



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

Jan Möbius's avatar
 
Jan Möbius committed
356
357
}

Dirk Wilden's avatar
Dirk Wilden committed
358
359
360
361
362
363
364

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
369
  QMenu menu(0);
370

Jan Möbius's avatar
 
Jan Möbius committed
371
  QIcon icon;
372

373
374
  //get all used types
  QVector<DataType> types;
375

376
377
378
379
380
381
  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;
382

Jan Möbius's avatar
 
Jan Möbius committed
383
384
385
  switch (headerPopupType_) {
//     case 0 :
//       std::cerr << "0";
386
//       break;
Jan Möbius's avatar
 
Jan Möbius committed
387
388
389
    //Show / Hide
    case 1 :
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"drawModes.png");
Jan Möbius's avatar
Jan Möbius committed
390
      action = menu.addAction(icon,tr("Show all"),this,SLOT ( showAll() ));
391
392
393
394
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
400
      action = menu.addAction(tr("Hide all"),this,SLOT ( hideAll() ));
401
402
403
404
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
409
410
411
      break;
    // Source
    case 2 :
Jan Möbius's avatar
Jan Möbius committed
412
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllSource() ));
413
414
415
416
      action->setData( QVariant() );

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

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

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

431
432
      break;
    // Target
Jan Möbius's avatar
 
Jan Möbius committed
433
    case 3 :
Jan Möbius's avatar
Jan Möbius committed
434
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllTarget() ));
435
436
437
438
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
444
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllTarget() ));
445
446
447
448
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
453
454
455
456
457
      break;
    default :
//       std::cerr << "def";
      break;
  }
458

Jan Möbius's avatar
 
Jan Möbius committed
459
460
461
  menu.exec(viewHeader_->mapToGlobal( _pos ) );
}

Dirk Wilden's avatar
Dirk Wilden committed
462
463
464
465
466
467

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

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

Jan Möbius's avatar
 
Jan Möbius committed
471
  // Get all selected rows
472
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
473
474
  int selectedRows = indexList.size();
  if (selectedRows == 1){
Dirk Wilden's avatar
Dirk Wilden committed
475
476
477
478
479
480
481
482
483
484
485
    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
486
        view_->resizeColumnToContents(0);
Dirk Wilden's avatar
Dirk Wilden committed
487
      }
Dirk Wilden's avatar
Dirk Wilden committed
488
    }
Jan Möbius's avatar
 
Jan Möbius committed
489
490
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
491

Dirk Wilden's avatar
Dirk Wilden committed
492
493
494
495
496
497

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

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

500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  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
521
522
523
524
525
526
    if (selectedRows == 1){
      int id = model_->itemId( indexList[0] );

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

  if ( item != 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

    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
547
548
549
550
551
552
553

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

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

Dirk Wilden's avatar
Dirk Wilden committed
558
559
//******************************************************************************

560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
 * \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();
            
584
        emit updatedObject(id, UPDATE_ALL);
585
586
587
588
589
    }
}

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

590
/**
591
* \brief Edit the light source's parameters
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
*/
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() );
611
    widget->setWindowFlags(Qt::WindowStaysOnTopHint);
612
613
614
615
616
617
    widget->show();
  }
}

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

Dirk Wilden's avatar
Dirk Wilden committed
618
619
620
/** \brief Zoom to an object
 * 
 */
Dirk Wilden's avatar
Dirk Wilden committed
621
622
void DataControlPlugin::slotZoomTo(){
  QItemSelectionModel* selection = view_->selectionModel();
623

Dirk Wilden's avatar
Dirk Wilden committed
624
  // Get all selected rows
625
  QModelIndexList indexList = selection->selectedRows ( 0 );
Dirk Wilden's avatar
Dirk Wilden committed
626
627
  int selectedRows = indexList.size();
  if (selectedRows == 1){
Dirk Wilden's avatar
Dirk Wilden committed
628
629
630
631
632
    BaseObject* item = 0;
    int id = model_->itemId( indexList[0]);

    if (id == -1 || !PluginFunctions::getObject( id, item) )
      return;
Dirk Wilden's avatar
Dirk Wilden committed
633
634
635
636
637
638
639
640
641
642
643
644
645

    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){
646
647
648
649
650

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

Dirk Wilden's avatar
Dirk Wilden committed
651
652
          ACG::Vec3d cur_min;
          ACG::Vec3d cur_max;
653

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

Dirk Wilden's avatar
Dirk Wilden committed
656
657
658
659
660
661
662
663
664
665
666
667
          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]);
          }
668

Dirk Wilden's avatar
Dirk Wilden committed
669
670
671
        }
      }
      //zoom to objects
Dirk Wilden's avatar
Dirk Wilden committed
672
      ACG::Vec3d bbcenter = (bbmax + bbmin) * 0.5;
673

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

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

Dirk Wilden's avatar
Dirk Wilden committed
678
      PluginFunctions::flyTo(eye, bbcenter );
679

Dirk Wilden's avatar
Dirk Wilden committed
680
681
682
    }else{
      //zoom to object
      BaseObjectData* obj = dynamic_cast< BaseObjectData* >(item);
683

684
685
686
687
      if ( !obj->visible() ){
        obj->show();
      }

Dirk Wilden's avatar
Dirk Wilden committed
688
      if (obj){
689

Dirk Wilden's avatar
Dirk Wilden committed
690
691
        ACG::Vec3d bbmin;
        ACG::Vec3d bbmax;
692

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

695
        if ((bbmin[0] > bbmax[0]) || (bbmin[1] > bbmax[1]) || (bbmin[2] > bbmax[2])){
Dirk Wilden's avatar
Dirk Wilden committed
696
          std::cerr << "Error while computing bounding box!";
697
698
          return;
        }
699

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

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

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

Dirk Wilden's avatar
Dirk Wilden committed
706
707
708
709
710
        PluginFunctions::flyTo(eye, bbcenter );
      }
    }
  }
}