viewerControl.cc 32.4 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           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
\*===========================================================================*/
42
43
44
45
46
47
48
49
50
51
52
53
54
55



//=============================================================================
//
//  CLASS CoreWidget - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

#include "CoreWidget.hh"

56
#include <ACG/Scenegraph/CoordsysNode.hh>
57
58
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
59
#include <OpenFlipper/widgets/snapshotDialog/SnapshotDialog.hh>
60
61
62
63
64
65
66
67
68
69
70


//== IMPLEMENTATION ==========================================================

//=============================================================================

void CoreWidget::slotToggleStereoMode()
{
  stereoActive_ = !stereoActive_;

  if ( stereoActive_ ) {
71
    statusBar_->showMessage(tr("Stereo enabled"));
Dirk Wilden's avatar
Dirk Wilden committed
72
    stereoButton_->setIcon( QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"stereo.png") );
73
  } else {
74
    statusBar_->showMessage(tr("Stereo disabled"));
Dirk Wilden's avatar
Dirk Wilden committed
75
    stereoButton_->setIcon( QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"mono.png") );
76
77
  }

78
  cursorPainter_->setEnabled (stereoActive_);
79

80
81
82
83
  for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
    examiner_widgets_[i]->setStereoMode(stereoActive_);
}

Jan Möbius's avatar
Jan Möbius committed
84
85
86
87
88
89
90
void CoreWidget::slotSetGlobalBackgroundColor() {
  ACG::Vec4f bc = PluginFunctions::viewerProperties().backgroundColor() * 255.0;

  QColor backCol((int)bc[0], (int)bc[1], (int)bc[2], (int)bc[3]);
  QColor c = QColorDialog::getColor(backCol,this);

  if (c != backCol && c.isValid())
91
    for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets(); ++i )
Jan Möbius's avatar
Jan Möbius committed
92
93
94
95
96
      PluginFunctions::viewerProperties(i).backgroundColor(ACG::Vec4f(((double) c.redF())   ,
                                                                      ((double) c.greenF()) ,
                                                                      ((double) c.blueF())  ,
                                                                        1.0));

97
  OpenFlipperSettings().setValue("Core/Gui/glViewer/defaultBackgroundColor",c);
Jan Möbius's avatar
Jan Möbius committed
98
99
100

}

101
void CoreWidget::slotSetContextBackgroundColor() {
Jan Möbius's avatar
Jan Möbius committed
102
103
104
105
106
107
108
109
110
111
112
113
114
  ACG::Vec4f bc = PluginFunctions::viewerProperties().backgroundColor() * 255.0;

  QColor backCol((int)bc[0], (int)bc[1], (int)bc[2], (int)bc[3]);
  QColor c = QColorDialog::getColor(backCol,this);

  if (c != backCol && c.isValid())
    PluginFunctions::viewerProperties().backgroundColor(ACG::Vec4f(((double) c.redF())   ,
                                                                    ((double) c.greenF()) ,
                                                                    ((double) c.blueF())  ,
                                                                     1.0));
}


115
116
117
118
119
/// Set the viewer to home position
void CoreWidget::slotContextHomeView() {
  examiner_widgets_[PluginFunctions::activeExaminer()]->home();
}

120
121
/// Show / hide wheels in examiner windows
void CoreWidget::slotSwitchWheels(bool _state) {
122
  std::vector< glViewer* >::iterator it = examiner_widgets_.begin();
123

124
  for(; it != examiner_widgets_.end(); it++)
125
    _state ? (*it)->slotShowWheels() : (*it)->slotHideWheels();
126
}
127

128
129
130
131
132
/// Switch navigation mode
void CoreWidget::slotSwitchNavigation(bool _egomode) {
  std::vector< glViewer* >::iterator it = examiner_widgets_.begin();

  for(; it != examiner_widgets_.end(); it++) {
Mike Kremer's avatar
Mike Kremer committed
133
    _egomode ? (*it)->navigationMode(glViewer::FIRSTPERSON_NAVIGATION) :
134
135
136
137
    	(*it)->navigationMode(glViewer::NORMAL_NAVIGATION);
  }
}

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/// Set the viewer to home position
void CoreWidget::slotGlobalHomeView() {
  for ( int i = 0 ; i < PluginFunctions::viewers() ; ++i )
    examiner_widgets_[i]->home();
}

/// Set the viewers home position
void CoreWidget::slotContextSetHomeView() {
  examiner_widgets_[PluginFunctions::activeExaminer()]->setHome();
}


/// Set the home position for all viewers
void CoreWidget::slotGlobalSetHomeView() {
  for ( int i = 0 ; i < PluginFunctions::viewers() ; ++i )
    examiner_widgets_[i]->setHome();
}

/// Change view on active viewer to view complete scene
void CoreWidget::slotContextViewAll() {
  examiner_widgets_[PluginFunctions::activeExaminer()]->viewAll();
}


/// Change view on all viewers to view complete scene
void CoreWidget::slotGlobalViewAll() {
  for ( int i = 0 ; i < PluginFunctions::viewers() ; ++i )
    examiner_widgets_[i]->viewAll();
}

168
/// Toggle projection mode of the active viewer
169
170
171
172
void CoreWidget::slotContextSwitchProjection() {
  examiner_widgets_[PluginFunctions::activeExaminer()]->toggleProjectionMode();
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
/// Toggle coordsys projection mode of the active viewer
ACG::SceneGraph::CoordsysNode::ProjectionMode CoreWidget::getCoordsysProjection() {  
    // Find coordsys node
    ACG::SceneGraph::BaseNode* node = 0;
    node = PluginFunctions::getSceneGraphRootNode()->find("Core Coordsys Node");
    if (node != 0) {
	return dynamic_cast<ACG::SceneGraph::CoordsysNode*> (node)->getProjectionMode();
    } else {
        emit statusMessage(QString(tr("getCoordsysProjection(): Could not find coordsys node. Assuming default orthographic projection.")));
	return ACG::SceneGraph::CoordsysNode::ORTHOGRAPHIC_PROJECTION;
    }
}

/// Toggle coordsys projection mode of the active viewer
187
void CoreWidget::slotContextSwitchCoordsysProjection() {
188
189
190
191
    // Find coordsys node
    ACG::SceneGraph::BaseNode* node = 0;
    node = PluginFunctions::getSceneGraphRootNode()->find("Core Coordsys Node");
    if (node != 0) {
192
193
194
195
196
197
198
        ACG::SceneGraph::CoordsysNode* cnode = dynamic_cast<ACG::SceneGraph::CoordsysNode*> (node);
        if (cnode->getProjectionMode() == ACG::SceneGraph::CoordsysNode::PERSPECTIVE_PROJECTION) {
            cnode->setProjectionMode(ACG::SceneGraph::CoordsysNode::ORTHOGRAPHIC_PROJECTION);
        }
        else {
            cnode->setProjectionMode(ACG::SceneGraph::CoordsysNode::PERSPECTIVE_PROJECTION);
        }
199
200
201
    } else {
        emit statusMessage(QString(tr("slotContextSwitchCoordsysProjection(): Could not find coordsys node, thus its projection mode will not be toggled.")));
    }
202
203
204

    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
        examiner_widgets_[i]->updateGL();
205
206
}

207
/// Toggle projection mode of all viewers to perspective projection
208
209
210
211
212
void CoreWidget::slotGlobalPerspectiveProjection() {
  for ( int i = 0 ; i < PluginFunctions::viewers() ; ++i )
    examiner_widgets_[i]->perspectiveProjection();
}

213
/// Toggle projection mode of all viewers to orthogonal projection
214
215
216
217
218
void CoreWidget::slotGlobalOrthographicProjection() {
  for ( int i = 0 ; i < PluginFunctions::viewers() ; ++i )
    examiner_widgets_[i]->orthographicProjection();
}

219
220
221
222
223
224
225
226
227
228
229
230
231
232

/// If animation is disabled in all viewers, enable it in all viewers. Otherwise disable it.
void CoreWidget::slotGlobalToggleAnimation() {
  int enabledCount  = 0;

  for ( int i = 0 ; i< PluginFunctions::viewers(); ++i ) {
    if ( PluginFunctions::viewerProperties(i).animation() )
      enabledCount++;
  }
  
  slotGlobalChangeAnimation (enabledCount == 0);
}

/// Set the animation mode for all viewers
233
234
235
236
237
void CoreWidget::slotGlobalChangeAnimation(bool _animation){
  for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
    PluginFunctions::viewerProperties(i).animation(_animation);
}

238
/// Set the animation mode for active viewer
239
240
241
242
void CoreWidget::slotLocalChangeAnimation(bool _animation){
  PluginFunctions::viewerProperties().animation(_animation);
}

243
244
245
246
247
248
249
250
251
252
253
254
255
256

/// If backface culling is disabled in all viewers, enable it in all viewers. Otherwise disable it.
void CoreWidget::slotGlobalToggleBackFaceCulling() {
  int enabledCount  = 0;

  for ( int i = 0 ; i< PluginFunctions::viewers(); ++i ) {
    if ( PluginFunctions::viewerProperties(i).backFaceCulling() )
      enabledCount++;
  }
  
  slotGlobalChangeBackFaceCulling (enabledCount == 0);
}

/// Set backface culling for all viewers
257
258
259
void CoreWidget::slotGlobalChangeBackFaceCulling(bool _backFaceCulling){
  for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
    PluginFunctions::viewerProperties(i).backFaceCulling(_backFaceCulling);
260

261
262
}

263
/// Set backface culling for active viewer
264
265
266
267
268
void CoreWidget::slotLocalChangeBackFaceCulling(bool _backFaceCulling){
  PluginFunctions::viewerProperties().backFaceCulling(_backFaceCulling);
}


269
270
271
272
273
274
275
276
277
278
279
280
281
/// If two-sided lighting is disabled in all viewers, enable it in all viewers. Otherwise disable it.
void CoreWidget::slotGlobalToggleTwoSidedLighting() {
  int enabledCount  = 0;

  for ( int i = 0 ; i< PluginFunctions::viewers(); ++i ) {
    if ( PluginFunctions::viewerProperties(i).twoSidedLighting() )
      enabledCount++;
  }
  
  slotGlobalChangeTwoSidedLighting (enabledCount == 0);
}

/// Set two-sided lighting for all viewers
282
283
284
285
286
void CoreWidget::slotGlobalChangeTwoSidedLighting(bool _lighting) {
  for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
    PluginFunctions::viewerProperties(i).twoSidedLighting(_lighting);
}

287
/// Set two-sided lighting for active viewer
288
289
290
291
void CoreWidget::slotLocalChangeTwoSidedLighting(bool _lighting) {
  PluginFunctions::viewerProperties().twoSidedLighting(_lighting);
}

292
293
294
295
296
297
298
299
300
301
302
303
304
305

/// If multisampling is disabled in all viewers, enable it in all viewers. Otherwise disable it.
void CoreWidget::slotGlobalToggleMultisampling() {
  int enabledCount  = 0;

  for ( int i = 0 ; i< PluginFunctions::viewers(); ++i ) {
    if ( PluginFunctions::viewerProperties(i).multisampling() )
      enabledCount++;
  }
  
  slotGlobalChangeMultisampling (enabledCount == 0);
}

/// Set multisampling for all viewers
306
307
308
309
310
void CoreWidget::slotGlobalChangeMultisampling(bool _multisampling) {
  for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
    PluginFunctions::viewerProperties(i).multisampling( _multisampling );
}

311
/// Set multisampling for active viewer
312
313
314
315
void CoreWidget::slotLocalChangeMultisampling(bool _multisampling) {
  PluginFunctions::viewerProperties().multisampling( _multisampling );
}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

/// If mipmapping is disabled in all viewers, enable it in all viewers. Otherwise disable it.
void CoreWidget::slotGlobalToggleMipmapping() {
  int enabledCount  = 0;

  for ( int i = 0 ; i< PluginFunctions::viewers(); ++i ) {
    if ( PluginFunctions::viewerProperties(i).mipmapping() )
      enabledCount++;
  }
  
  slotGlobalChangeMipmapping (enabledCount == 0);
}

/// Set mipmapping for all viewers
void CoreWidget::slotGlobalChangeMipmapping(bool _mipmapping) {
  for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
    PluginFunctions::viewerProperties(i).mipmapping( _mipmapping );
}

/// Set mipmapping for active viewer
Mike Kremer's avatar
Mike Kremer committed
336
337
338
339
void CoreWidget::slotLocalChangeMipmapping(bool _mipmapping) {
  PluginFunctions::viewerProperties().mipmapping( _mipmapping );
}

Dirk Wilden's avatar
Dirk Wilden committed
340
/// Take a snapshot of the current Viewer
341
void CoreWidget::slotExaminerSnapshot() {
Dirk Wilden's avatar
Dirk Wilden committed
342
343
344
345

  QFileInfo fi(PluginFunctions::viewerProperties().snapshotName());
  int counter = PluginFunctions::viewerProperties().snapshotCounter();

Jan Möbius's avatar
Jan Möbius committed
346
347
348
349
350
351
    // Add leading zeros
  QString number = QString::number(counter);
  while ( number.size() < 7 )
    number = "0" + number;

  QString suggest = fi.baseName() + "." + number + ".";
Dirk Wilden's avatar
Dirk Wilden committed
352
353
354
355
356
357

  QString format="png";

  if (fi.completeSuffix() == "ppm")
    format="ppmraw";

Jan Möbius's avatar
Jan Möbius committed
358
359
360
  if (fi.completeSuffix() == "jpg")
    format="jpg";

Dirk Wilden's avatar
Dirk Wilden committed
361
362
  suggest += format;

Dirk Wilden's avatar
Dirk Wilden committed
363
364
365
  QFileDialog dialog(this);
  dialog.setFileMode(QFileDialog::AnyFile);
  dialog.setDefaultSuffix("png");
366
  dialog.setNameFilter(tr("Images (*.png *.ppm *.jpg)"));
Dirk Wilden's avatar
Dirk Wilden committed
367
368
369
370
371
  dialog.setFileMode(QFileDialog::AnyFile);
  dialog.setConfirmOverwrite(true);
  dialog.setDirectory( fi.path() );
  dialog.selectFile( suggest );
  dialog.setAcceptMode(QFileDialog::AcceptSave);
372
  dialog.setWindowTitle(tr("Save Snapshot"));
Dirk Wilden's avatar
Dirk Wilden committed
373

Dirk Wilden's avatar
Dirk Wilden committed
374
375
376
377
378
 if (dialog.exec()){
     QString newName = dialog.selectedFiles()[0];

      if (newName != fi.path() + OpenFlipper::Options::dirSeparator() + suggest)
        PluginFunctions::viewerProperties().snapshotBaseFileName(newName);
Dirk Wilden's avatar
Dirk Wilden committed
379

Dirk Wilden's avatar
Dirk Wilden committed
380
381
      QImage image;
      examiner_widgets_[PluginFunctions::activeExaminer()]->snapshot(image);
Dirk Wilden's avatar
Dirk Wilden committed
382

Dirk Wilden's avatar
Dirk Wilden committed
383
      image.save(newName);
Dirk Wilden's avatar
Dirk Wilden committed
384
385
386
387
388
389
390
  }
}

///Take a snapshot of the whole application
void CoreWidget::applicationSnapshotDialog() {

  QFileInfo fi(snapshotName_);
391
392
393
  
  if (snapshotName_ == "")
    fi.setFile( OpenFlipperSettings().value("Core/CurrentDir").toString() + QDir::separator() + "snap.png" );
Dirk Wilden's avatar
Dirk Wilden committed
394

Jan Möbius's avatar
Jan Möbius committed
395
396
397
398
399
  // Add leading zeros
  QString number = QString::number(snapshotCounter_);
  while ( number.size() < 7 )
    number = "0" + number;

400
  QString suggest = fi.path() + QDir::separator() + fi.baseName() + "." + number + ".";
Dirk Wilden's avatar
Dirk Wilden committed
401
402
403
404
405
406

  QString format="png";

  if (fi.completeSuffix() == "ppm")
    format="ppmraw";

Jan Möbius's avatar
Jan Möbius committed
407
408
409
  if (fi.completeSuffix() == "jpg")
    format="jpg";

Dirk Wilden's avatar
Dirk Wilden committed
410
411
  suggest += format;

412
413
414
  int w = width();
  int h = height();

415
416
  SnapshotDialog dialog(suggest, false, w, h, 0);
  
417
418
419
  connect(&dialog, SIGNAL(resizeApplication(int,int)), this, SIGNAL(resizeApplication(int,int)) );

  bool ok = dialog.exec();
420
421

  if ( ok ){
422
    QString newName = dialog.filename->text();
Dirk Wilden's avatar
Dirk Wilden committed
423

424
425
426
427
    if (newName != suggest){
      
      OpenFlipperSettings().setValue("Core/CurrentDir", QFileInfo(newName).absolutePath() );
      
Dirk Wilden's avatar
Dirk Wilden committed
428
      snapshotName_ = newName;
429
      snapshotCounter_ = 1;
Dirk Wilden's avatar
Dirk Wilden committed
430
431
432
433
434
435
436
    }else
      snapshotCounter_++;

    QPixmap pic = QPixmap::grabWindow( winId() );

    pic.save(newName);
  }
437
438

  emit resizeApplication(w,h);
Dirk Wilden's avatar
Dirk Wilden committed
439
440
441
442
443
444
445
}

///Take a snapshot of the whole application
void CoreWidget::applicationSnapshot() {

  QFileInfo fi(snapshotName_);

Jan Möbius's avatar
Jan Möbius committed
446
447
448
449
450
451
  // Add leading zeros
  QString number = QString::number(snapshotCounter_++);
  while ( number.size() < 7 )
    number = "0" + number;

  QString suggest = fi.path() + QDir::separator() +fi.baseName() + "." + number + ".";
Dirk Wilden's avatar
Dirk Wilden committed
452
453
454
455
456
457

  QString format="png";

  if (fi.completeSuffix() == "ppm")
    format="ppmraw";

458
459
460
  if (fi.completeSuffix() == "jpg")
    format="jpg";

Dirk Wilden's avatar
Dirk Wilden committed
461
462
  suggest += format;

463
464
465
  // Write image asynchronously
  QPixmap* pic = new QPixmap(QPixmap::grabWindow( winId() ));
  writeImageAsynchronously(pic, suggest);
466
467
}

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

///Take a snapshot of all viewers
void CoreWidget::viewerSnapshotDialog() {

  QFileInfo fi(snapshotName_);
473
474
475
  
  if (snapshotName_ == "")
    fi.setFile( OpenFlipperSettings().value("Core/CurrentDir").toString() + QDir::separator() + "snap.png" );
Dirk Wilden's avatar
Dirk Wilden committed
476

Jan Möbius's avatar
Jan Möbius committed
477
478
479
480
481
  // Add leading zeros
  QString number = QString::number(snapshotCounter_);
  while ( number.size() < 7 )
    number = "0" + number;

482
  QString suggest = fi.path() + QDir::separator() + fi.baseName() + "." + number + ".";
Dirk Wilden's avatar
Dirk Wilden committed
483
484
485
486
487
488

  QString format="png";

  if (fi.completeSuffix() == "ppm")
    format="ppmraw";

Jan Möbius's avatar
Jan Möbius committed
489
490
491
  if (fi.completeSuffix() == "jpg")
    format="jpg";

Dirk Wilden's avatar
Dirk Wilden committed
492
493
  suggest += format;

494
495
  int w = glView_->width();
  int h = glView_->height();
496
  
497
498
  SnapshotDialog dialog(suggest, true, w, h, 0);
  
499
500
501
502
  bool ok = dialog.exec();

  if (ok){
    QString newName = dialog.filename->text();
Dirk Wilden's avatar
Dirk Wilden committed
503

504
505
506
507
    if (newName != suggest){
      
      OpenFlipperSettings().setValue("Core/CurrentDir", QFileInfo(newName).absolutePath() );
      
Dirk Wilden's avatar
Dirk Wilden committed
508
      snapshotName_ = newName;
509
510
      snapshotCounter_ = 1;
      
Dirk Wilden's avatar
Dirk Wilden committed
511
512
    }else
      snapshotCounter_++;
513
    
Dirk Wilden's avatar
Dirk Wilden committed
514
515
516
517
518
519
520
    //now take the snapshot
    switch ( baseLayout_->mode() ){

      case QtMultiViewLayout::SingleView:
      {
        QImage finalImage;

521
        examiner_widgets_[PluginFunctions::activeExaminer()]->snapshot(finalImage,
Jan Möbius's avatar
Jan Möbius committed
522
523
524
            dialog.snapWidth->value(), dialog.snapHeight->value(),
            dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked(),
            dialog.multisampling->isChecked() ? dialog.num_samples->value() : 1);
Dirk Wilden's avatar
Dirk Wilden committed
525
526
527
528
529

        finalImage.save(newName);

        break;
      }
Jan Möbius's avatar
Jan Möbius committed
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
      case QtMultiViewLayout::DoubleView:
      {
    	int w = dialog.snapHeight->value();

    	double relSizeW = static_cast<double>( examiner_widgets_[0]->glWidth() / static_cast<double>( glScene_->width() ) );

    	//Get the images
    	QImage img[2];
    	examiner_widgets_[0]->snapshot(img[0], static_cast<int>(relSizeW * w) , dialog.snapWidth->value(),
    								   dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
    	examiner_widgets_[1]->snapshot(img[1], static_cast<int>(relSizeW * w) , dialog.snapWidth->value(),
    	    						   dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());

    	QImage finalImage(img[0].width() + img[1].width() +2, img[0].height(), QImage::Format_ARGB32_Premultiplied);

    	QPainter painter(&finalImage);

    	painter.fillRect(0,0,finalImage.width(), finalImage.height(), QBrush(Qt::gray));

    	painter.drawImage(QRectF(           0,             0, img[0].width(), img[0].height()),img[0],
    	                  QRectF(           0,             0, img[0].width(), img[0].height()) );
    	painter.drawImage(QRectF(img[0].width()+2,         0, img[1].width(), img[1].height()),img[1],
    	                  QRectF(           0,             0, img[1].width(), img[1].height()) );

    	finalImage.save(newName);

    	break;
      }

Dirk Wilden's avatar
Dirk Wilden committed
559
560
      case QtMultiViewLayout::Grid:
      {
561
562
563
564
565
566
567
568
        // Compute size of each viewer
        int w = dialog.snapWidth->value();
        int h = dialog.snapHeight->value();
        
        // Relative size of first viewer (in relation to the other viewers
        double relSizeW = (double)examiner_widgets_[0]->glWidth() / (double)glScene_->width();
        double relSizeH = (double)examiner_widgets_[0]->glHeight() / (double)glScene_->height();
          
Dirk Wilden's avatar
Dirk Wilden committed
569
570
        QImage img0,img1,img2,img3;

571
572
573
574
575
576
577
578
        examiner_widgets_[0]->snapshot(img0, (int)((double)w * relSizeW),           (int)((double)h * relSizeH),
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
        examiner_widgets_[1]->snapshot(img1, (int)((double)w * (1.0 - relSizeW)),   (int)((double)h * relSizeH),
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
        examiner_widgets_[2]->snapshot(img2, (int)((double)w * relSizeW),           (int)((double)h * (1.0 - relSizeH)),
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
        examiner_widgets_[3]->snapshot(img3, (int)((double)w * (1.0 - relSizeW)),   (int)((double)h * (1.0 - relSizeH)),
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
Dirk Wilden's avatar
Dirk Wilden committed
579

580
        QImage finalImage(img0.width() + img1.width()+2, img0.height() + img2.height()+2, QImage::Format_ARGB32_Premultiplied);
Dirk Wilden's avatar
Dirk Wilden committed
581
582

        QPainter painter(&finalImage);
583
584
585

        painter.fillRect(0,0,finalImage.width(), finalImage.height(), QBrush(Qt::gray));

Dirk Wilden's avatar
Dirk Wilden committed
586
587
        painter.drawImage(QRectF(           0,             0, img0.width(), img0.height()),img0,
                          QRectF(           0,             0, img0.width(), img0.height()) );
588
        painter.drawImage(QRectF(img0.width()+2,           0, img1.width(), img1.height()),img1,
Dirk Wilden's avatar
Dirk Wilden committed
589
                          QRectF(           0,             0, img1.width(), img1.height()) );
590
        painter.drawImage(QRectF(          0,img0.height()+2, img2.width(), img2.height()),img2,
Dirk Wilden's avatar
Dirk Wilden committed
591
                          QRectF(           0,             0, img2.width(), img2.height()) );
592
        painter.drawImage(QRectF(img0.width()+2, img0.height()+2, img3.width(), img3.height()),img3,
Dirk Wilden's avatar
Dirk Wilden committed
593
594
595
596
597
598
599
600
                          QRectF(           0,             0, img3.width(), img3.height()) );

        finalImage.save(newName);

        break;
      }
      case QtMultiViewLayout::HSplit:
      {
601
602
603
604
605
606
607
608
609
610
        // Compute size of each viewer
        int w = dialog.snapWidth->value();
        int h = dialog.snapHeight->value();
        
        // Relative size of first viewer (in relation to the other viewers
        double relSizeW = (double)examiner_widgets_[0]->glWidth() / (double)glScene_->width();
        double relSizeH1 = (double)examiner_widgets_[1]->glHeight() / (double)glScene_->height();
        double relSizeH2 = (double)examiner_widgets_[2]->glHeight() / (double)glScene_->height();
        double relSizeH3 = (double)examiner_widgets_[3]->glHeight() / (double)glScene_->height();
          
Dirk Wilden's avatar
Dirk Wilden committed
611
612
        QImage img0,img1,img2,img3;

613
614
615
616
617
618
619
620
        examiner_widgets_[0]->snapshot(img0, (int)((double)w * relSizeW), h,
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
        examiner_widgets_[1]->snapshot(img1, (int)((double)w * (1.0 - relSizeW)), relSizeH1 * (double)h,
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
        examiner_widgets_[2]->snapshot(img2, (int)((double)w * (1.0 - relSizeW)), relSizeH2 * (double)h,
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
        examiner_widgets_[3]->snapshot(img3, (int)((double)w * (1.0 - relSizeW)), relSizeH3 * (double)h,
                                       dialog.transparent->isChecked(), dialog.hideCoordsys->isChecked());
Dirk Wilden's avatar
Dirk Wilden committed
621

622
        QImage finalImage(img0.width() + img1.width() +2, img0.height(), QImage::Format_ARGB32_Premultiplied);
Dirk Wilden's avatar
Dirk Wilden committed
623
624

        QPainter painter(&finalImage);
625
626
627

        painter.fillRect(0,0,finalImage.width(), finalImage.height(), QBrush(Qt::gray));

Dirk Wilden's avatar
Dirk Wilden committed
628
629
        painter.drawImage(QRectF(           0,             0, img0.width(), img0.height()),img0,
                          QRectF(           0,             0, img0.width(), img0.height()) );
630
        painter.drawImage(QRectF(img0.width()+2,             0, img1.width(), img1.height()),img1,
Dirk Wilden's avatar
Dirk Wilden committed
631
                          QRectF(           0,             0, img1.width(), img1.height()) );
632
        painter.drawImage(QRectF(img0.width()+2, img1.height()+2, img2.width(), img2.height()),img2,
Dirk Wilden's avatar
Dirk Wilden committed
633
                          QRectF(           0,             0, img2.width(), img2.height()) );
634
        painter.drawImage(QRectF(img0.width()+2, img1.height()+img2.height()+4, img3.width(),img3.height()),img3,
Dirk Wilden's avatar
Dirk Wilden committed
635
636
637
638
639
640
641
642
643
644
                          QRectF(           0,             0, img3.width(), img3.height()) );

        finalImage.save(newName);

        break;
      }
      default: break;

    }
  }
645
  //glView_->resize(w, h);
Dirk Wilden's avatar
Dirk Wilden committed
646
647
}

Dirk Wilden's avatar
Dirk Wilden committed
648
///Take a snapshot of all viewers
Dirk Wilden's avatar
Dirk Wilden committed
649
650
651
652
void CoreWidget::viewerSnapshot() {

  QFileInfo fi(snapshotName_);

Jan Möbius's avatar
Jan Möbius committed
653
654
655
656
657
658
  // Add leading zeros
  QString number = QString::number(snapshotCounter_++);
  while ( number.size() < 7 )
    number = "0" + number;

  QString suggest = fi.path() + QDir::separator() +fi.baseName() + "." + number + ".";
Dirk Wilden's avatar
Dirk Wilden committed
659
660
661
662
663
664

  QString format="png";

  if (fi.completeSuffix() == "ppm")
    format="ppmraw";

Jan Möbius's avatar
Jan Möbius committed
665
666
667
  if (fi.completeSuffix() == "jpg")
    format="jpg";

Dirk Wilden's avatar
Dirk Wilden committed
668
669
670
671
672
673
674
  suggest += format;


  switch ( baseLayout_->mode() ){

    case QtMultiViewLayout::SingleView:
    {
675
      QImage* finalImage = new QImage();
Dirk Wilden's avatar
Dirk Wilden committed
676

677
      examiner_widgets_[PluginFunctions::activeExaminer()]->snapshot(*finalImage);
Dirk Wilden's avatar
Dirk Wilden committed
678

679
      writeImageAsynchronously(finalImage, suggest);
Dirk Wilden's avatar
Dirk Wilden committed
680
681
682

      break;
    }
Jan Möbius's avatar
Jan Möbius committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
    case QtMultiViewLayout::DoubleView:
    {
    	//Get the images
    	QImage img[2];
    	examiner_widgets_[0]->snapshot(img[0]);
    	examiner_widgets_[1]->snapshot(img[1]);

    	QImage finalImage(img[0].width() + img[1].width() +2, img[0].height(), QImage::Format_ARGB32_Premultiplied);

    	QPainter painter(&finalImage);

    	painter.fillRect(0,0,finalImage.width(), finalImage.height(), QBrush(Qt::gray));

    	painter.drawImage(QRectF(           0,             0, img[0].width(), img[0].height()),img[0],
    			QRectF(           0,             0, img[0].width(), img[0].height()) );
    	painter.drawImage(QRectF(img[0].width()+2,         0, img[1].width(), img[1].height()),img[1],
    			QRectF(           0,             0, img[1].width(), img[1].height()) );

    	writeImageAsynchronously(finalImage, suggest);

    	break;
    }

Dirk Wilden's avatar
Dirk Wilden committed
706
707
    case QtMultiViewLayout::Grid:
    {
Dirk Wilden's avatar
Dirk Wilden committed
708
      QImage img0,img1,img2,img3;
709
      
Dirk Wilden's avatar
Dirk Wilden committed
710
711
712
713
714
      examiner_widgets_[0]->snapshot(img0);
      examiner_widgets_[1]->snapshot(img1);
      examiner_widgets_[2]->snapshot(img2);
      examiner_widgets_[3]->snapshot(img3);

715
      QImage* finalImage = new QImage(img0.width() + img1.width() + 2, img0.height() + img2.height() + 2, QImage::Format_ARGB32_Premultiplied);
Dirk Wilden's avatar
Dirk Wilden committed
716

717
      QPainter painter(finalImage);
718

719
      painter.fillRect(0,0,finalImage->width(), finalImage->height(), QBrush(Qt::gray));
720

Dirk Wilden's avatar
Dirk Wilden committed
721
722
      painter.drawImage(QRectF(           0,             0, img0.width(), img0.height()),img0,
                        QRectF(           0,             0, img0.width(), img0.height()) );
723
      painter.drawImage(QRectF(img0.width()+2,           0, img1.width(), img1.height()),img1,
Dirk Wilden's avatar
Dirk Wilden committed
724
                        QRectF(           0,             0, img1.width(), img1.height()) );
725
      painter.drawImage(QRectF(           0, img0.height()+2, img2.width(), img2.height()),img2,
Dirk Wilden's avatar
Dirk Wilden committed
726
                        QRectF(           0,             0, img2.width(), img2.height()) );
727
      painter.drawImage(QRectF(img0.width()+2, img0.height()+2, img3.width(), img3.height()),img3,
Dirk Wilden's avatar
Dirk Wilden committed
728
729
                        QRectF(           0,             0, img3.width(), img3.height()) );

730
      writeImageAsynchronously(finalImage, suggest);
Dirk Wilden's avatar
Dirk Wilden committed
731
732
733
734
735
736
737
738
739
740
741
742

      break;
    }
    case QtMultiViewLayout::HSplit:
    {
      QImage img0,img1,img2,img3;

      examiner_widgets_[0]->snapshot(img0);
      examiner_widgets_[1]->snapshot(img1);
      examiner_widgets_[2]->snapshot(img2);
      examiner_widgets_[3]->snapshot(img3);

743
      QImage* finalImage = new QImage(img0.width() + img1.width() + 2, img0.height(), QImage::Format_ARGB32_Premultiplied);
Dirk Wilden's avatar
Dirk Wilden committed
744

745
      QPainter painter(finalImage);
746

747
      painter.fillRect(0,0,finalImage->width(), finalImage->height(), QBrush(Qt::gray));
748

Dirk Wilden's avatar
Dirk Wilden committed
749
750
      painter.drawImage(QRectF(           0,             0, img0.width(), img0.height()),img0,
                        QRectF(           0,             0, img0.width(), img0.height()) );
751
      painter.drawImage(QRectF(img0.width()+2,             0, img1.width(), img1.height()),img1,
Dirk Wilden's avatar
Dirk Wilden committed
752
                        QRectF(           0,             0, img1.width(), img1.height()) );
753
      painter.drawImage(QRectF(img0.width()+2, img1.height()+2, img2.width(), img2.height()),img2,
Dirk Wilden's avatar
Dirk Wilden committed
754
                        QRectF(           0,             0, img2.width(), img2.height()) );
755
      painter.drawImage(QRectF(img0.width()+2, img1.height()+img2.height()+4, img3.width(),img3.height()),img3,
Dirk Wilden's avatar
Dirk Wilden committed
756
                        QRectF(           0,             0, img3.width(), img3.height()) );
Dirk Wilden's avatar
Dirk Wilden committed
757

758
      writeImageAsynchronously(finalImage, suggest);
Dirk Wilden's avatar
Dirk Wilden committed
759
760
761
762
763
764
765

      break;
    }
    default: break;

  }
}
Dirk Wilden's avatar
Dirk Wilden committed
766
767
768
769
770
771
772

void CoreWidget::applicationSnapshotName(QString _name) {

  snapshotName_ = _name;
  snapshotCounter_ = 0;
}

773
774
775
void CoreWidget::writeImageAsynchronously(QPixmap* _pixmap, const QString _name) {

    QFuture<void>* future = new QFuture<void>();
776
    *future = QtConcurrent::run(this, &CoreWidget::writeImageQPixmap, _pixmap, _name);
777
778
779
780
781
782
783
784
    QFutureWatcher<void>* watcher = new QFutureWatcher<void>();
    watcher->setFuture(*future);

    watcher_garbage_.insert(std::pair<QFutureWatcher<void>*,QFuture<void>*>(watcher, future));

    connect(watcher, SIGNAL(finished()), this, SLOT(delete_garbage()));
}

785
void CoreWidget::writeImageQPixmap(QPixmap* _pixmap, const QString _name) const {
786
787
788
789
790

    _pixmap->save(_name);
    delete _pixmap;
}

791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
void CoreWidget::writeImageAsynchronously(QImage* _image, const QString _name) {

    QFuture<void>* future = new QFuture<void>();
    *future = QtConcurrent::run(this, &CoreWidget::writeImageQImage, _image, _name);
    QFutureWatcher<void>* watcher = new QFutureWatcher<void>();
    watcher->setFuture(*future);

    watcher_garbage_.insert(std::pair<QFutureWatcher<void>*,QFuture<void>*>(watcher, future));

    connect(watcher, SIGNAL(finished()), this, SLOT(delete_garbage()));
}

void CoreWidget::writeImageQImage(QImage* _image, const QString _name) const {

    _image->save(_name);
    delete _image;
}

809
810
811
812
813
814
815
void CoreWidget::delete_garbage() {

    QObject* obj = QObject::sender();
    QFutureWatcher<void>* watcher = dynamic_cast<QFutureWatcher<void>*>(obj);
    if(!watcher) {
        return;
    }
816
817
818

    map_mutex_.lock();

819
820
821
822
823
824
825
    std::map<QFutureWatcher<void>*,QFuture<void>*>::iterator f;
    f = watcher_garbage_.find(watcher);
    if(f != watcher_garbage_.end()) {
        delete f->second;
        delete f->first;
        watcher_garbage_.erase(f);
    }
826
827

    map_mutex_.unlock();
828
}
Dirk Wilden's avatar
Dirk Wilden committed
829

830
831
832
833
834
835
836
837
void CoreWidget::slotPasteView( ) {
  examiner_widgets_[PluginFunctions::activeExaminer()]->actionPasteView();
}

void CoreWidget::slotCopyView( ) {
  examiner_widgets_[PluginFunctions::activeExaminer()]->actionCopyView();
}

Dirk Wilden's avatar
Dirk Wilden committed
838
void CoreWidget::slotCoordSysVisibility(bool _visible){
839

Dirk Wilden's avatar
Dirk Wilden committed
840
841
842
843
  ACG::SceneGraph::BaseNode* root = PluginFunctions::getSceneGraphRootNode();
  ACG::SceneGraph::BaseNode* coordSys = root->find("Core Coordsys Node");

  if (coordSys == 0){
844
    emit log( LOGERR, tr("CoordSys Node not found"));
Dirk Wilden's avatar
Dirk Wilden committed
845
846
847
    return;
  }

848
if (_visible)
Dirk Wilden's avatar
Dirk Wilden committed
849
    coordSys->show();
850
  else
Dirk Wilden's avatar
Dirk Wilden committed
851
    coordSys->hide();
852

853
854
  for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
    examiner_widgets_[i]->updateGL();
855

Dirk Wilden's avatar
Dirk Wilden committed
856
}
Jan Möbius's avatar
Jan Möbius committed
857

858
859
860
void CoreWidget::slotSetViewingDirection(QAction* _action) {

  PluginFunctions::setFixedView( _action->data().toInt() );
861
862
863
864
865
  if (_action->data().toInt() != PluginFunctions::VIEW_FREE)
    PluginFunctions::allowRotation( false, PluginFunctions::activeExaminer() );
  
    // Update view
    examiner_widgets_[PluginFunctions::activeExaminer()]->updateGL();
866
}
867

868
869
870
void CoreWidget::slotLockRotation(bool _lock) {

  PluginFunctions::allowRotation( !_lock, PluginFunctions::activeExaminer() );
871
872
  if (!_lock)
    PluginFunctions::setFixedView( PluginFunctions::VIEW_FREE );
873
874
}

875
void CoreWidget::moveBack() {
Mike Kremer's avatar
Mike Kremer committed
876
    examiner_widgets_[PluginFunctions::activeExaminer()]->moveBack();
877
}
Mike Kremer's avatar
Mike Kremer committed
878

879
void CoreWidget::moveForward() {
Mike Kremer's avatar
Mike Kremer committed
880
    examiner_widgets_[PluginFunctions::activeExaminer()]->moveForward();
881
}
Mike Kremer's avatar
Mike Kremer committed
882

883
void CoreWidget::strafeLeft() {
Mike Kremer's avatar
Mike Kremer committed
884
    examiner_widgets_[PluginFunctions::activeExaminer()]->strafeLeft();
885
}
Mike Kremer's avatar
Mike Kremer committed
886

887
void CoreWidget::strafeRight() {
Mike Kremer's avatar
Mike Kremer committed
888
    examiner_widgets_[PluginFunctions::activeExaminer()]->strafeRight();
889
890
}

891
//=============================================================================