Popup.cc 25.3 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40
41
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42

Jan Möbius's avatar
 
Jan Möbius committed
43
44
45
46
47
48




#include "DataControlPlugin.hh"

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

51
#include "Menu.hh"
Jan Möbius's avatar
 
Jan Möbius committed
52

53
54
#include <ObjectTypes/Light/LightWidget.hh>

55
56
#include <ACG/Scenegraph/MaterialNode.hh>

Dirk Wilden's avatar
Dirk Wilden committed
57
58
59
60
61
//******************************************************************************

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

Jan Möbius's avatar
 
Jan Möbius committed
64
  QItemSelectionModel* selection = view_->selectionModel();
Dirk Wilden's avatar
Dirk Wilden committed
65
66

  if (selection == 0) return;
67

Jan Möbius's avatar
 
Jan Möbius committed
68
  // Get all selected rows
69
70
  QModelIndexList indexList = selection->selectedRows();

Dirk Wilden's avatar
Dirk Wilden committed
71
72

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

  QString text;

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

Dirk Wilden's avatar
Dirk Wilden committed
82
83
84
85
86
  msgBox.setDetailedText(text);

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

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

Dirk Wilden's avatar
Dirk Wilden committed
89
    int id = model_->itemId( indexList[i] );
90
91


Dirk Wilden's avatar
Dirk Wilden committed
92
93
    if ( id != -1 )
      emit deleteObject( id );
Jan Möbius's avatar
 
Jan Möbius committed
94
  }
95

Jan Möbius's avatar
   
Jan Möbius committed
96
  emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
97
98
}

Dirk Wilden's avatar
Dirk Wilden committed
99
100
101
102
103
104

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

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

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

Dirk Wilden's avatar
Dirk Wilden committed
111
112
  //get the object
  int id = model_->itemId( indexList[0] );
113
  unGroupObject(id);
Jan Möbius's avatar
 
Jan Möbius committed
114
115
}

Dirk Wilden's avatar
Dirk Wilden committed
116
117
118
119
120
121
122

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

/** \brief Slot for Copy action in ContextMenu
 * 
 */
void DataControlPlugin::slotCopy() {
123
124
125
126
127
128
129
130
131
  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
132
133
134
    int id = model_->itemId( indexList[i] );

    if ( id > 0){
135

Dirk Wilden's avatar
Dirk Wilden committed
136
      int newObject;
137

Dirk Wilden's avatar
Dirk Wilden committed
138
139
      emit copyObject( id, newObject );
    }
140

141
142
  }

Jan Möbius's avatar
   
Jan Möbius committed
143
  emit updateView();
144
145
}

Dirk Wilden's avatar
Dirk Wilden committed
146
147
148
149
150
151
152

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

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

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

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

Dirk Wilden's avatar
Dirk Wilden committed
159

160
161
162
163
  //get object ids
  IdList ids;
  for (int i = 0; i < indexList.size(); ++i)
    ids.push_back( model_->itemId(indexList[i]) );
164

165

166
167
  //group all objects
  groupObjects(ids);
168
169
  emit objectsGrouped(ids);

Jan Möbius's avatar
 
Jan Möbius committed
170
171
}

Dirk Wilden's avatar
Dirk Wilden committed
172
173
174
175
176
177
178

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

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

182
183
184
  if (!popupIndex_.isValid())
    return;

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

187

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

Jan Möbius's avatar
 
Jan Möbius committed
190
  // Get all selected rows
191
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
192
  int selectedRows = indexList.size();
193

194
  Menu    menu(0);
Jan Möbius's avatar
 
Jan Möbius committed
195
196
197
  QAction* action;
  QIcon icon;
  if ( selectedRows > 1 ) {
198
199
200
    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
201
    menu.addAction(tr("Group"),this,SLOT ( slotGroup() ));
Jan Möbius's avatar
 
Jan Möbius committed
202
    menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
203
    menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
Jan Möbius's avatar
 
Jan Möbius committed
204
205
206
   }else
    // check if the item is a group item
    if ( item->isGroup() ) {
Jan Möbius's avatar
Jan Möbius committed
207
      action = menu.addAction(tr("Zoom to objects"),this,SLOT ( slotZoomTo() ));
Dirk Wilden's avatar
Dirk Wilden committed
208
209
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"zoom-in.png");
      action->setIcon(icon);
Jan Möbius's avatar
Jan Möbius committed
210
      menu.addAction(tr("Ungroup"),this,SLOT ( slotUngroup() ));
Jan Möbius's avatar
 
Jan Möbius committed
211
      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
212
      action = menu.addAction(tr("Rename"),this,SLOT ( slotRename() ));
Jan Möbius's avatar
 
Jan Möbius committed
213
214
215
      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
216
      action = menu.addAction(tr("Remove"),this,SLOT ( slotPopupRemove() ));
Jan Möbius's avatar
   
Jan Möbius committed
217
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-delete-item.png");
Jan Möbius's avatar
 
Jan Möbius committed
218
219
      action->setIcon(icon);
    } else {
Jan Möbius's avatar
Jan Möbius committed
220
221
222
223
224
225
226
227
228
229
230
      
        //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();
        
231
232
233
234
235
236
237
238
239
240
        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);
241
            menu.addSeparator();
242
243
244
245
246
247
248
249
250
251
252
253
254
255
	    
	    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");
	    }
256
            action->setIcon(icon);
257
	    
258
            action = menu.addAction(tr("Edit Light"),this,SLOT ( slotEditLight() ));
259
            icon = typeIcon(DATA_LIGHT);
260
261
            action->setIcon(icon);
            
262
263
264
265
        } 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);
266

267
268
269
            action = menu.addAction(tr("Copy"),this,SLOT ( slotCopy() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"edit-copy.png");
            action->setIcon(icon);
270

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

275
276
277
            action = menu.addAction(tr("Material Properties"),this,SLOT ( slotMaterialProperties() ));
            icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-material.png");
            action->setIcon(icon);
278

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

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

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

291
            menu.addAction(tr("Group"),this,SLOT ( slotGroup() ));
292

293
294
295
296
297
            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
298
    }
299
300
301



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

Jan Möbius's avatar
 
Jan Möbius committed
304
305
}

Dirk Wilden's avatar
Dirk Wilden committed
306
307
308
309
310
311
312

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
317
  QMenu menu(0);
318

Jan Möbius's avatar
 
Jan Möbius committed
319
  QIcon icon;
320

321
322
  //get all used types
  QVector<DataType> types;
323

324
325
326
327
328
329
  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;
330

Jan Möbius's avatar
 
Jan Möbius committed
331
332
333
  switch (headerPopupType_) {
//     case 0 :
//       std::cerr << "0";
334
//       break;
Jan Möbius's avatar
 
Jan Möbius committed
335
336
337
    //Show / Hide
    case 1 :
      icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"drawModes.png");
Jan Möbius's avatar
Jan Möbius committed
338
      action = menu.addAction(icon,tr("Show all"),this,SLOT ( showAll() ));
339
340
341
342
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
348
      action = menu.addAction(tr("Hide all"),this,SLOT ( hideAll() ));
349
350
351
352
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
357
358
359
      break;
    // Source
    case 2 :
Jan Möbius's avatar
Jan Möbius committed
360
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllSource() ));
361
362
363
364
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
370
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllSource() ));
371
372
373
374
      action->setData( QVariant() );

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

379
380
      break;
    // Target
Jan Möbius's avatar
 
Jan Möbius committed
381
    case 3 :
Jan Möbius's avatar
Jan Möbius committed
382
      action = menu.addAction(tr("Select all"),this,SLOT ( setAllTarget() ));
383
384
385
386
      action->setData( QVariant() );

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

      menu.addSeparator();
Jan Möbius's avatar
Jan Möbius committed
392
      action = menu.addAction(tr("Deselect all"),this,SLOT ( clearAllTarget() ));
393
394
395
396
      action->setData( QVariant() );

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

Jan Möbius's avatar
 
Jan Möbius committed
401
402
403
404
405
      break;
    default :
//       std::cerr << "def";
      break;
  }
406

Jan Möbius's avatar
 
Jan Möbius committed
407
408
409
  menu.exec(viewHeader_->mapToGlobal( _pos ) );
}

Dirk Wilden's avatar
Dirk Wilden committed
410
411
412
413

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

/** \brief show a rename dialog for object names
414
 *
Dirk Wilden's avatar
Dirk Wilden committed
415
 */
Jan Möbius's avatar
 
Jan Möbius committed
416
417
void DataControlPlugin::slotRename(){
  QItemSelectionModel* selection = view_->selectionModel();
418

Jan Möbius's avatar
 
Jan Möbius committed
419
  // Get all selected rows
420
  QModelIndexList indexList = selection->selectedRows ( 0 );
Jan Möbius's avatar
 
Jan Möbius committed
421
422
  int selectedRows = indexList.size();
  if (selectedRows == 1){
Dirk Wilden's avatar
Dirk Wilden committed
423
424
425
426
427
428
429
430
431
432
433
    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
434
        view_->resizeColumnToContents(0);
Dirk Wilden's avatar
Dirk Wilden committed
435
      }
Dirk Wilden's avatar
Dirk Wilden committed
436
    }
Jan Möbius's avatar
 
Jan Möbius committed
437
438
  }
}
Dirk Wilden's avatar
Dirk Wilden committed
439

Dirk Wilden's avatar
Dirk Wilden committed
440
441
442
443
444
445

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

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

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

    if ( action ){
      bool ok = false;

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

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

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

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

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

    if ( item != 0 ){

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

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

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

      emit updateView();
    }
}

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

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

    if ( action ){
      bool ok = false;

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

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

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

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

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

    if ( item != 0 ){

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

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

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

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

    if ( action ){
      bool ok = false;

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

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

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

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

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

    if ( item != 0 ){

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

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

      emit updateView();
    }
}

Dirk Wilden's avatar
Dirk Wilden committed
575
576
void DataControlPlugin::slotMaterialProperties(){

577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
  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
598
599
600
601
602
603
    if (selectedRows == 1){
      int id = model_->itemId( indexList[0] );

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

  if ( item != 0 ){
Dirk Wilden's avatar
Dirk Wilden committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623

    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
624
625
626

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

Dirk Wilden's avatar
Dirk Wilden committed
627
void DataControlPlugin::slotNodeChanged( ACG::SceneGraph::BaseNode* /*_node*/ ){
Jan Möbius's avatar
   
Jan Möbius committed
628
  emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
629
630
}

Dirk Wilden's avatar
Dirk Wilden committed
631
632
//******************************************************************************

633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/**
 * \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();
            
657
        emit updatedObject(id, UPDATE_ALL);
658
659
660
661
662
    }
}

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

663
/**
664
* \brief Edit the light source's parameters
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
*/
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() );
684
    widget->setWindowFlags(Qt::WindowStaysOnTopHint);
685
686
687
688
689
690
    widget->show();
  }
}

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

Dirk Wilden's avatar
Dirk Wilden committed
691
692
693
/** \brief Zoom to an object
 * 
 */
Dirk Wilden's avatar
Dirk Wilden committed
694
695
void DataControlPlugin::slotZoomTo(){
  QItemSelectionModel* selection = view_->selectionModel();
696

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

    if (id == -1 || !PluginFunctions::getObject( id, item) )
      return;
Dirk Wilden's avatar
Dirk Wilden committed
706
707
708
709
710
711
712

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

      //compute boundingBox
      bool firstRound = true;
713
714
      ACG::Vec3d bbmin(FLT_MAX,FLT_MAX,FLT_MAX);
      ACG::Vec3d bbmax(FLT_MIN,FLT_MIN,FLT_MIN);
Dirk Wilden's avatar
Dirk Wilden committed
715
716
717
718

      for (int i=0; i < children.size(); i++){
        BaseObjectData* child = dynamic_cast< BaseObjectData* > (children[i]);
        if (child){
719
720
721
722
723

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

Dirk Wilden's avatar
Dirk Wilden committed
724
725
          ACG::Vec3d cur_min;
          ACG::Vec3d cur_max;
726

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

Dirk Wilden's avatar
Dirk Wilden committed
729
730
731
732
733
734
735
736
737
738
739
740
          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]);
          }
741

Dirk Wilden's avatar
Dirk Wilden committed
742
743
744
        }
      }
      //zoom to objects
Dirk Wilden's avatar
Dirk Wilden committed
745
      ACG::Vec3d bbcenter = (bbmax + bbmin) * 0.5;
746

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

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

Dirk Wilden's avatar
Dirk Wilden committed
751
      PluginFunctions::flyTo(eye, bbcenter );
752

Dirk Wilden's avatar
Dirk Wilden committed
753
754
755
    }else{
      //zoom to object
      BaseObjectData* obj = dynamic_cast< BaseObjectData* >(item);
756

Dirk Wilden's avatar
Dirk Wilden committed
757
      if (obj){
758

Jan Möbius's avatar
Jan Möbius committed
759
760
761
        if ( !obj->visible() )
          obj->show();

Dirk Wilden's avatar
Dirk Wilden committed
762
763
        ACG::Vec3d bbmin;
        ACG::Vec3d bbmax;
764

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

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

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

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

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

Dirk Wilden's avatar
Dirk Wilden committed
778
779
780
781
782
        PluginFunctions::flyTo(eye, bbcenter );
      }
    }
  }
}