mvdMainWindow.cxx 61.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20 21 22 23 24 25 26 27 28 29 30 31


#include "mvdMainWindow.h"
#include "ui_mvdMainWindow.h"


/*****************************************************************************/
/* INCLUDE SECTION                                                           */

//
// Qt includes (sorted by alphabetic order)
//// Must be included before system/custom includes.
32
#include <QtWidgets>
33 34 35 36 37 38 39 40 41

//
// System includes (sorted by alphabetic order)

//
// ITK includes (sorted by alphabetic order)

//
// OTB includes (sorted by alphabetic order)
42
#include <otbQtAdapters.h>
43 44 45

//
// Monteverdi includes (sorted by alphabetic order)
46
#if defined( OTB_USE_QT ) && USE_OTB_APPS
47 48 49
# include "mvdApplicationLauncher.h"
# include "mvdApplicationsToolBoxController.h"
# include "mvdOTBApplicationsModel.h"
50
# include "mvdQtWidgetView.h"
51 52
#endif
//
53 54 55 56
// #include "mvdDatabaseModel.h"
// #include "mvdDatasetModel.h"
#include "mvdQuicklookModel.h"
#include "mvdVectorImageModel.h"
57
//
58 59 60 61 62 63
#include "mvdApplicationsToolBox.h"
#include "mvdColorSetupController.h"
#include "mvdColorSetupWidget.h"
#include "mvdColorDynamicsController.h"
#include "mvdColorDynamicsWidget.h"
// #include "mvdDatabaseBrowserController.h"
64
#if ENABLE_TREE_WIDGET_TEST
65
// #include "mvdDatabaseBrowserWidgetTest.h"
66
#else // ENABLE_TREE_WIDGET_TEST
67
// #include "mvdDatabaseBrowserWidget.h"
68
#endif // ENABLE_TREE_WIDGET_TEST
69 70 71 72 73 74 75 76 77 78 79 80
// #include "mvdDatasetPropertiesController.h"
// #include "mvdDatasetPropertiesWidget.h"
#include "mvdFilenameDragAndDropEventFilter.h"
#include "mvdHistogramController.h"
#include "mvdHistogramWidget.h"
#include "mvdImageViewManipulator.h"
#include "mvdImageViewRenderer.h"
#include "mvdImageViewWidget.h"
#include "mvdImportSubDatasetDialog.h"
#include "mvdKeymapDialog.h"
#include "mvdLayerStackController.h"
#include "mvdLayerStackWidget.h"
81
#if USE_PIXEL_DESCRIPTION
82
#  include "mvdPixelDescriptionWidget.h"
83
#endif // USE_PIXEL_DESCRIPTION
84 85 86 87
#include "mvdQuicklookViewManipulator.h"
#include "mvdQuicklookViewRenderer.h"
#include "mvdShaderWidget.h"
#include "mvdStatusBarWidget.h"
88
#include "mvdProjectionBarWidget.h"
89 90 91 92
//
#include "mvdApplication.h"
#include "mvdPreferencesDialog.h"

93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
namespace mvd
{

/*
  TRANSLATOR mvd::MainWindow

  Necessary for lupdate to be aware of C++ namespaces.

  Context comment for translator.
*/


/*****************************************************************************/
/* CONSTANTS                                                                 */

109
#define REFERENCE_LAYER_COMBOBOX_NAME "referenceLayerComboBox"
110

111
#define FORCE_NO_GLSL ( ( defined( OTB_DEBUG ) && 0 ) || 0 )
112

113 114 115 116 117 118 119 120 121
/*****************************************************************************/
/* STATIC IMPLEMENTATION SECTION                                             */


/*****************************************************************************/
/* CLASS IMPLEMENTATION SECTION                                              */

/*****************************************************************************/
MainWindow
Guillaume Pasero's avatar
Guillaume Pasero committed
122 123
::MainWindow( QWidget* p, Qt::WindowFlags flags ) :
  I18nMainWindow( p, flags ),
124 125 126 127 128
  m_UI( new mvd::Ui::MainWindow() ),
  m_ColorDynamicsDock( NULL ),
  m_ColorSetupDock( NULL ),
  // m_DatabaseBrowserDock( NULL ),
  // m_DatasetPropertiesDock(NULL),
129
  m_LayerStackDock( NULL ),
130
#if USE_PIXEL_DESCRIPTION
131
  m_PixelDescriptionDock(NULL),
132
#endif // USE_PIXEL_DESCRIPTION
133
  m_HistogramDock( NULL ),
134
#if defined( OTB_USE_QT ) && USE_OTB_APPS
135 136 137 138
  m_OtbApplicationsBrowserDock(NULL),
#endif
  m_ImageView( NULL ),
  m_QuicklookViewDock( NULL ),
139
#if USE_TABBED_VIEW
140
  m_CentralTabWidget( NULL ),
141
#endif // USE_TABBED_VIEW
142 143
  m_StatusBarWidget( NULL ),
  m_ShaderWidget( NULL ),
144
  m_FilenameDragAndDropEventFilter( NULL ),
145
  m_KeymapDialog( NULL ),
146
  m_ProjectionBarWidget( NULL ),
147 148 149
  m_GLSL140( -2 ),
  m_isGLSLAvailable( false ),
  m_ForceNoGLSL( false )
150 151 152
{
  m_UI->setupUi( this );

153 154 155 156
  //
  //
  m_KeymapDialog = new KeymapDialog( this );

157 158 159 160 161 162
  //
  // Event filters.
  m_FilenameDragAndDropEventFilter = new FilenameDragAndDropEventFilter( this );

  QObject::connect(
    m_FilenameDragAndDropEventFilter,
163
    SIGNAL( FilenamesDropped( const QStringList & ) ),
164 165
    // to:
    this,
166
    SLOT( ImportImages( const QStringList & ) )
167 168 169 170 171 172 173 174 175 176 177 178 179 180
  );
}

/*****************************************************************************/
MainWindow
::~MainWindow()
{
  delete m_UI;
  m_UI = NULL;
}

/*****************************************************************************/
bool
MainWindow
181
::CheckGLCapabilities( bool forceNoGLSL )
182 183 184 185
{
  assert( m_ImageView!=NULL );
  assert( m_ImageView->GetRenderer()!=NULL );

Stéphane Albert's avatar
Stéphane Albert committed
186 187 188 189 190
  // Coverity-19845
  //
  // if( m_ImageView==NULL ||
  //     m_ImageView->GetRenderer()==NULL )
  //   return false;
191

192 193
  m_isGLSLAvailable =
    m_ImageView->GetRenderer()->CheckGLCapabilities( &m_GLSL140 );
194

195
#if FORCE_NO_GLSL
196 197
  qWarning() << "No-GLSL is always forced in this build!";

198 199 200 201
  m_ForceNoGLSL = true;

#else
  m_ForceNoGLSL = forceNoGLSL;
202 203 204

#endif // FORCE_NO_GLSL

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  bool isGLSL = m_isGLSLAvailable && !m_ForceNoGLSL;

  {
    assert( m_UI!=NULL );
    assert( m_UI->action_GLSL!=NULL );

    bool isBlocked = m_UI->action_GLSL->blockSignals( true );

    m_UI->action_GLSL->setEnabled( m_isGLSLAvailable );
    m_UI->action_GLSL->setChecked( isGLSL );

    m_UI->action_GLSL->blockSignals( isBlocked );
  }

  SetGLSLEnabled( isGLSL );

  return ( !m_isGLSLAvailable || m_ForceNoGLSL ) || m_isGLSLAvailable;
222 223 224 225 226 227 228
}

/*****************************************************************************/
void
MainWindow
::SetGLSLEnabled( bool enabled )
{
229
  //
230 231 232
  // Image view
  {
    assert( m_ImageView!=NULL );
233

234 235 236 237 238 239 240 241 242 243 244 245
    AbstractImageViewRenderer * renderer = m_ImageView->GetRenderer();

    assert( renderer!=NULL );

    if( renderer->SetGLSLEnabled( enabled )!=enabled )
      {
      renderer->ClearScene( true );
      renderer->UpdateScene();

      m_ImageView->updateGL();
      }
  }
246

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  {
    ImageViewWidget * quicklookView = GetQuicklookView();
    assert( quicklookView!=NULL );

    // MANTIS-1204
    // {
    //
    // Forward GLSL state to quicklook view.
    assert( GetQuicklookView()->GetRenderer()!=NULL );

    AbstractImageViewRenderer * renderer = quicklookView->GetRenderer();

    assert( renderer!=NULL );

    if( renderer->SetGLSLEnabled( enabled )!=enabled )
      {
      renderer->ClearScene( true );
      renderer->UpdateScene();

      quicklookView->updateGL();
      }
    // }
  }

  //
  // Shader widget
273 274
  assert( m_ShaderWidget!=NULL );

275
  m_ShaderWidget->SetGLSLEnabled( enabled );
276 277
  m_ShaderWidget->SetGLSL140Enabled( m_GLSL140>=0 );

278 279
  //
  // Status bar widget.
280 281
  assert( m_StatusBarWidget!=NULL );

282
  m_StatusBarWidget->SetGLSLEnabled( enabled );
283 284 285 286 287 288 289 290

  //
  // Paint
  // if( mustRefresh )
  //   {
  //   m_ImageView->updateGL();
  //   quicklookView->updateGL();
  //   }
291 292 293 294 295 296 297
}

/*****************************************************************************/
void
MainWindow
::virtual_SetupUI()
{
298 299 300
  assert( m_UI!=NULL );

  setObjectName( PROJECT_NAME );
301

302
#ifdef OTB_DEBUG
303
  setWindowTitle( PROJECT_NAME " (Debug)" );
304
#else // OTB_DEBUG
305
  setWindowTitle( PROJECT_NAME );
306
#endif // OTB_DEBUG
307 308 309 310 311 312

  InitializeCentralWidget();

  InitializeDockWidgets();

  InitializeStatusBarWidgets();
313

314 315
  InitializeProjectionBarWidget();

316
  InitializeRenderToolBar();
317
  InitializeShaderToolBar();
318

319
  if( !RestoreLayout( Monteverdi_UI_VERSION ) )
320 321 322
    {
    qWarning() << "Failed to restore window layout!";
    }
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
}

/*****************************************************************************/
void
MainWindow
::virtual_ConnectUI()
{
  ConnectViewMenu();

  //
  // CHAIN CONTROLLERS.
  // Forward model update signals of color-setup controller...
  QObject::connect(
    m_ColorSetupDock->findChild< AbstractModelController* >(),
    SIGNAL( RgbChannelIndexChanged( RgbwChannel, int ) ),
    // to: ...color-dynamics controller model update signal.
    m_ColorDynamicsDock->findChild< AbstractModelController* >(),
    SLOT( OnRgbChannelIndexChanged( RgbwChannel, int ) )
  );
  QObject::connect(
    m_ColorSetupDock->findChild< AbstractModelController* >(),
    SIGNAL( GrayChannelIndexChanged( int ) ),
    // to: ...color-dynamics controller model update signal.
    m_ColorDynamicsDock->findChild< AbstractModelController* >(),
    SLOT( OnGrayChannelIndexChanged( int ) )
  );
  QObject::connect(
    m_ColorSetupDock->findChild< AbstractModelController* >(),
    SIGNAL( GrayscaleActivated( bool ) ),
    // to: ...color-dynamics controller model update signal.
    m_ColorDynamicsDock->findChild< AbstractModelController* >(),
    SLOT( OnGrayscaleActivated( bool ) )
  );

  QObject::connect(
    m_ColorSetupDock->findChild< AbstractModelController* >(),
    SIGNAL( RgbChannelIndexChanged( RgbwChannel, int ) ),
    // to: ...color-dynamics controller model update signal.
    m_HistogramDock->findChild< AbstractModelController* >(),
    SLOT( OnRgbChannelIndexChanged( RgbwChannel, int ) )
  );
  QObject::connect(
    m_ColorSetupDock->findChild< AbstractModelController* >(),
    SIGNAL( GrayChannelIndexChanged( int ) ),
    // to: ...color-dynamics controller model update signal.
    m_HistogramDock->findChild< AbstractModelController* >(),
    SLOT( OnGrayChannelIndexChanged( int ) )
  );
  QObject::connect(
    m_ColorSetupDock->findChild< AbstractModelController* >(),
    SIGNAL( GrayscaleActivated( bool ) ),
    // to: ...color-dynamics controller model update signal.
    m_HistogramDock->findChild< AbstractModelController* >(),
    SLOT( OnGrayscaleActivated( bool ) )
  );

  QObject::connect(
    m_ColorDynamicsDock->findChild< AbstractModelController* >(),
    SIGNAL( LowIntensityChanged( RgbwChannel, double, bool ) ),
    // to: ...histogram controller model update signal.
    m_HistogramDock->findChild< AbstractModelController* >(),
    SLOT( OnLowIntensityChanged( RgbwChannel, double, bool ) )
  );
  QObject::connect(
    m_ColorDynamicsDock->findChild< AbstractModelController* >(),
    SIGNAL( HighIntensityChanged( RgbwChannel, double, bool ) ),
    // to: ...histogram controller model update signal.
    m_HistogramDock->findChild< AbstractModelController* >(),
    SLOT( OnHighIntensityChanged( RgbwChannel, double, bool ) )
  );
  QObject::connect(
    m_ColorDynamicsDock->findChild< AbstractModelController* >(),
    SIGNAL( HistogramRefreshed() ),
    // to: ...histogram controller model update signal.
    m_HistogramDock->findChild< AbstractModelController* >(),
    SLOT( OnHistogramRefreshed() )
  );

  //
  // close tabs handling
403
#if USE_TABBED_VIEW
404 405 406 407 408 409
  QObject::connect(
    m_CentralTabWidget,
    SIGNAL( tabCloseRequested( int ) ),
    this,
    SLOT( OnTabCloseRequested( int ) )
  );
410
#endif // USE_TABBED_VIEW
411 412

  //
413 414
  // Other connections.

415
  ConnectImageViews();
416 417

  ConnectStatusBar();
418

419 420
  ConnectProjectionBarWidget();

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
  //
  // When everything is connected, install event-filter.
  assert( m_ImageView!=NULL );
  m_ImageView->installEventFilter( m_FilenameDragAndDropEventFilter );

  {
  assert( GetController( m_LayerStackDock )!=NULL );

  LayerStackWidget * layerStackWidget =
    GetController( m_LayerStackDock )->GetWidget< LayerStackWidget >();

  assert( layerStackWidget!=NULL );

  layerStackWidget->InstallEventFilter( m_FilenameDragAndDropEventFilter );
  }
436 437 438 439 440 441 442 443 444 445 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
}

/*****************************************************************************/
void
MainWindow
::ConnectImageViews()
{
  assert( m_ImageView!=NULL );

  QObject::connect(
    this,
    SIGNAL( UserZoomIn() ),
    // to:
    m_ImageView,
    SLOT( ZoomIn() )
  );

  QObject::connect(
    this,
    SIGNAL( UserZoomOut() ),
    // to:
    m_ImageView,
    SLOT( ZoomOut() )
  );

  QObject::connect(
    this,
    SIGNAL( UserZoomExtent() ),
    m_ImageView,
    SLOT( ZoomToExtent() )
  );

  QObject::connect(
    this,
    SIGNAL( UserZoomFull() ),
    m_ImageView,
    SLOT( ZoomToFullResolution() )
  );

475 476 477 478 479 480 481
  QObject::connect(
    this,
    SIGNAL( UserZoomLayer() ),
    m_ImageView,
    SLOT( ZoomToLayerExtent() )
  );

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  //
  // Connect image-views for ROI-changed events.

  const AbstractImageViewManipulator* imageViewManipulator =
    m_ImageView->GetManipulator();

  assert( imageViewManipulator!=NULL );


  ImageViewWidget* quicklookView = GetQuicklookView();
  assert( quicklookView!=NULL );

  const AbstractImageViewManipulator* quicklookManipulator =
    quicklookView->GetManipulator();

  assert( quicklookManipulator!=NULL );


  QObject::connect(
    m_ImageView,
502 503
    SIGNAL( RoiChanged(
        const PointType &, const SizeType &, const SpacingType &, const PointType & )
504 505 506
    ),
    // to:
    quicklookManipulator,
507 508
    SLOT( OnRoiChanged(
        const PointType &, const SizeType &, const SpacingType &, const PointType & )
509 510 511 512 513 514 515 516 517 518 519
    )
  );

  QObject::connect(
    quicklookView,
    SIGNAL( CenterRoiRequested( const PointType& ) ),
    // to:
    imageViewManipulator,
    SLOT( CenterOn( const PointType& ) )
  );

520 521 522 523 524 525 526 527
  // Not needed anymore becaure already done in ImageViewWidget.
  // QObject::connect(
  //   quicklookManipulator,
  //   SIGNAL( RefreshViewRequested() ),
  //   // to:
  //   m_ImageView,
  //   SLOT( updateGL() )
  // );
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

  //
  // Connect controllers to image-views.
  //

  {
  assert( m_LayerStackDock!=NULL );

  AbstractModelController * controller = GetController( m_LayerStackDock );
  assert( controller!=NULL );

  QObject::connect(
    controller,
    SIGNAL( ApplyAllRequested() ),
    // to:
    m_ImageView,
    SLOT( OnApplyAllRequested() )
  );

  QObject::connect(
    controller,
    SIGNAL( ApplyAllRequested() ),
    // to:
    quicklookView,
    SLOT( OnApplyAllRequested() )
  );
554 555 556 557 558 559 560 561

  QObject::connect(
    controller,
    SIGNAL( ResetEffectsRequested() ),
    // to:
    m_ImageView,
    SLOT( OnResetEffectsRequested() )
  );
562
  }
563

564 565 566 567 568 569 570 571
}

/*****************************************************************************/
void
MainWindow
::ConnectViewMenu()
{
  m_UI->menu_View->addAction( m_UI->m_ToolBar->toggleViewAction() );
572
  m_UI->menu_View->addAction( m_UI->m_RenderToolBar->toggleViewAction() );
573 574
  m_UI->menu_View->addAction( m_UI->m_ShaderToolBar->toggleViewAction() );

575 576 577 578 579
  m_UI->menu_View->addSeparator();

  m_UI->menu_View->addAction( m_ColorSetupDock->toggleViewAction() );
  m_UI->menu_View->addAction( m_ColorDynamicsDock->toggleViewAction() );
  m_UI->menu_View->addAction( m_HistogramDock->toggleViewAction() );
580
  m_UI->menu_View->addAction( m_LayerStackDock->toggleViewAction() );
581
  m_UI->menu_View->addAction( m_QuicklookViewDock->toggleViewAction() );
582 583 584

  m_UI->menu_View->addSeparator();

585
  m_UI->menu_View->addAction( m_UI->action_LoadOTBApplications );
586

587
#if USE_PIXEL_DESCRIPTION
588
  m_UI->menu_View->addAction( m_PixelDescriptionDock->toggleViewAction() );
589
#endif // USE_PIXEL_DESCRIPTION
590 591 592 593 594
}

/*****************************************************************************/
void
MainWindow
595
::ConnectStatusBar()
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
{
  assert( m_StatusBarWidget!=NULL );
  assert( m_ImageView!=NULL );

  QObject::connect(
    m_ImageView,
    SIGNAL( ScaleChanged( double, double ) ),
    // to:
    m_StatusBarWidget,
    SLOT( SetScale( double, double ) )
  );

  QObject::connect(
    m_StatusBarWidget,
    SIGNAL( ScaleChanged( double ) ),
    // to:
    m_ImageView->GetManipulator(),
    SLOT( ZoomTo( double ) )
  );

  QObject::connect(
    m_StatusBarWidget,
    SIGNAL( PixelIndexChanged( const IndexType& ) ),
    // to:
    m_ImageView,
621
    SLOT( CenterOnSelected( const IndexType& ) )
622
  );
623 624 625 626 627 628 629 630

  QObject::connect(
    m_ImageView,
    SIGNAL( PixelInfoChanged( const QPoint &, const PointType &, const PixelInfo::Vector & ) ),
    // to:
    this,
    SLOT( OnPixelInfoChanged( const QPoint &, const PointType &, const PixelInfo::Vector & ) )
  );
631 632
}

633 634 635 636 637 638 639 640 641
/*****************************************************************************/
void
MainWindow
::ConnectProjectionBarWidget()
{
  assert( m_ProjectionBarWidget!=NULL );
  assert( m_ImageView!=NULL );

  QObject::connect(
642 643 644 645 646
    m_ImageView,
    SIGNAL( ScaleChanged( double, double ) ),
    // to:
    m_ProjectionBarWidget,
    SLOT( SetProjectionScale( double, double ) )
647 648 649
  );

  QObject::connect(
650 651 652 653 654
    m_ProjectionBarWidget,
    SIGNAL( ProjectionScaleChanged( double ) ),
    // to:
    m_ImageView->GetManipulator(),
    SLOT( ZoomTo( double ) )
655 656 657 658 659
  );


}

660
/*****************************************************************************/
661 662
#if USE_PIXEL_DESCRIPTION

663 664
void
MainWindow
665
::ConnectPixelDescriptionWidget( AbstractLayerModel * model )
666
{
667 668 669 670
  if( model==NULL ||
      !model->inherits( VectorImageModel::staticMetaObject.className() ) )
    return;

671
  // Access vector-image model.
672 673 674 675 676
  VectorImageModel * vectorImageModel =
    qobject_cast< VectorImageModel * >( model );

  assert( vectorImageModel==qobject_cast< VectorImageModel * >( model ) );
  assert( vectorImageModel!=NULL );
677

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
  //
  // send the physical point of the clicked point in screen
  // vectorImageModel is in charge of pixel information computation
  QObject::connect(
    m_ImageView,
    SIGNAL(
      PhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType &
      )
    ),
    // to:
    vectorImageModel,
    SLOT(
      OnPhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType&
      )
    )
  );

  QObject::connect(
    GetQuicklookView(),
    SIGNAL(
      PhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType&
      )
    ),
    // to:
    vectorImageModel,
    SLOT(
      OnPhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType&
      )
    )
  );

725
  // get the PixelDescription widget
726
  // TODO: Make a widget controller!
727
  PixelDescriptionWidget * pixelDescriptionWidget =
728 729
    qobject_cast< PixelDescriptionWidget * >(
      m_PixelDescriptionDock->findChild< PixelDescriptionWidget * >()
730 731
    );

732
  assert( pixelDescriptionWidget!=NULL );
733

734 735 736 737 738 739 740
  QObject::connect(
    vectorImageModel,
    SIGNAL( CurrentPhysicalUpdated( const QStringList & ) ),
    // to:
    pixelDescriptionWidget,
    SLOT( OnCurrentPhysicalUpdated( const QStringList & ) )
  );
741

742 743 744 745 746 747 748 749 750 751
  QObject::connect(
    vectorImageModel,
    SIGNAL( CurrentGeographicUpdated( const QStringList & ) ),
    // to:
    pixelDescriptionWidget,
    SLOT( OnCurrentGeographicUpdated( const QStringList & ) )
  );

  QObject::connect(
    vectorImageModel,
752
    SIGNAL( CurrentPixelValueUpdated(const VectorImageType::PixelType &,
753 754 755
                                     const QStringList & ) ),
    // to:
    pixelDescriptionWidget,
756
    SLOT( OnCurrentPixelValueUpdated(const VectorImageType::PixelType &,
757 758
                                     const QStringList & ) )
  );
759 760
}

761 762
#endif // USE_PIXEL_DESCRIPTION

763
/*****************************************************************************/
764 765
#if USE_PIXEL_DESCRIPTION

766 767
void
MainWindow
768
::DisconnectPixelDescriptionWidget( const AbstractLayerModel * model )
769
{
770 771 772
  if( model==NULL ||
      !model->inherits( VectorImageModel::staticMetaObject.className() ) )
    return;
773 774

  // Access vector-image model.
775 776 777 778 779
  const VectorImageModel * vectorImageModel =
    qobject_cast< const VectorImageModel * >( model );

  assert( vectorImageModel==qobject_cast< const VectorImageModel * >( model ) );
  assert( vectorImageModel!=NULL );
780

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
  //
  // send the physical point of the clicked point in screen
  // vectorImageModel is in charge of pixel information computation
  QObject::disconnect(
    m_ImageView,
    SIGNAL(
      PhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType &
      )
    ),
    // to:
    vectorImageModel,
    SLOT(
      OnPhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType &
      )
    )
  );

  QObject::disconnect(
    GetQuicklookView(),
    SIGNAL(
      PhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType &
      )
    ),
    // to:
    vectorImageModel,
    SLOT(
      OnPhysicalCursorPositionChanged(
	const QPoint &,
	const PointType &,
	const PointType &,
	const DefaultImageType::PixelType&
      )
    )
  );

828
  // get the PixelDescription widget
829
  // TODO: Make a widget controller!
830
  PixelDescriptionWidget * pixelDescriptionWidget =
831 832
    qobject_cast< PixelDescriptionWidget * >(
      m_PixelDescriptionDock->findChild< PixelDescriptionWidget * >()
833 834
    );

835
  assert( pixelDescriptionWidget!=NULL );
836

837 838 839 840 841 842 843
  QObject::disconnect(
    vectorImageModel,
    SIGNAL( CurrentPhysicalUpdated( const QStringList & ) ),
    // to:
    pixelDescriptionWidget,
    SLOT( OnCurrentPhysicalUpdated( const QStringList & ) )
  );
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
  QObject::disconnect(
    vectorImageModel,
    SIGNAL( CurrentGeographicUpdated( const QStringList & ) ),
    // to:
    pixelDescriptionWidget,
    SLOT( OnCurrentGeographicUpdated( const QStringList & ) )
  );

  QObject::connect(
    vectorImageModel,
    SIGNAL(
      CurrentPixelValueUpdated(
        const VectorImageType::PixelType &,
        const QStringList &
      )
    ),
    // to:
    pixelDescriptionWidget,
    SLOT(
      OnCurrentPixelValueUpdated(
        const VectorImageType::PixelType &,
        const QStringList &
      )
    )
  );
870 871
}

872 873
#endif // USE_PIXEL_DESCRIPTION

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
/*****************************************************************************/
void
MainWindow
::InitializeDockWidgets()
{
#define ENABLE_QTOOLBAR_TEST 0
#if ENABLE_QTOOLBAR_TEST
  // XP: QToolBar test.
  QDockWidget* dock =
    AddWidgetToDock(
      m_UI->m_ToolBar,
      "TOOL_BAR",
      tr( "Tool bar" ),
      Qt::TopDockWidgetArea
    );

  m_UI->menu_View->addAction( dock->toggleViewAction() );
#endif

  //
  // Left pane.

  //
  // Right pane.

  // Quicklook-view dock-widget
  assert( m_QuicklookViewDock==NULL );
  assert( m_ImageView!=NULL );
  m_QuicklookViewDock = AddWidgetToDock(
    CreateQuicklookViewWidget( m_ImageView ),
    "QUICKLOOK_VIEW",
    tr( "Quicklook view" ),
    Qt::RightDockWidgetArea
  );

909
  // Histogram-view.
910 911 912 913 914 915
  assert( m_HistogramDock==NULL );
  m_HistogramDock =
    AddDockWidget
    < HistogramWidget, HistogramController, QDockWidget >
    ( "HISTOGRAM",
      tr( "Histogram" ),
916
      Qt::RightDockWidgetArea
917 918
    );

919 920
  tabifyDockWidget( m_QuicklookViewDock, m_HistogramDock );

921 922
#if USE_PIXEL_DESCRIPTION

923 924 925 926 927 928 929 930 931 932 933 934 935
  // Pixel Description (no controller needed here / direct update of
  // the pixel description through signals from VectorImageModel)
  assert( m_PixelDescriptionDock==NULL );
  m_PixelDescriptionDock =
    AddDockWidget
    < PixelDescriptionWidget, QDockWidget >
    ( "CURRENT_PIXEL_DESCRIPTION",
      tr( "Pixel Description" ),
      Qt::RightDockWidgetArea
    );

  tabifyDockWidget( m_PixelDescriptionDock, m_HistogramDock );

936 937
#endif // USE_PIXEL_DESCRIPTION

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
  // Color-setup.
  assert( m_ColorSetupDock==NULL );
  m_ColorSetupDock =
    AddDockWidget
    < ColorSetupWidget, ColorSetupController, QDockWidget >
    ( "COLOR_SETUP",
      tr( "Color setup" ),
      Qt::RightDockWidgetArea
    );

  // Color-dynamics.
  assert( m_ColorDynamicsDock==NULL );
  m_ColorDynamicsDock =
    AddDockWidget
    < ColorDynamicsWidget, ColorDynamicsController, QDockWidget >
    ( "COLOR_DYNAMICS",
      tr( "Color dynamics" ),
955 956
      Qt::RightDockWidgetArea,
      I18nMainWindow::DOCK_LAYOUT_SCROLLABLE
957 958 959 960
    );

  // Tabify dock-widgets.
  tabifyDockWidget( m_ColorSetupDock, m_ColorDynamicsDock );
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975


  //
  // Bottom pane
  //

  // Layer-stack editor.
  assert( m_LayerStackDock==NULL );
  m_LayerStackDock = AddDockWidget<
    LayerStackWidget, LayerStackController, QDockWidget >(
      "LAYER_STACK",
      tr( "Layer stack" ),
      Qt::BottomDockWidgetArea
    );

976 977 978 979 980 981 982
}

/*****************************************************************************/
void
MainWindow
::InitializeCentralWidget()
{
983
#if USE_TABBED_VIEW
984 985 986 987 988 989 990 991 992 993 994 995 996
  // Create central tab-widget for multi-view support.
  assert( m_CentralTabWidget==NULL );
  m_CentralTabWidget = new QTabWidget( this );

  // Customize it.
  m_CentralTabWidget->setTabsClosable( true );

  // Set-it up as central widget.
  setCentralWidget( m_CentralTabWidget );

  //
  // access to the quicklook tabBar to remove the close button
  QTabBar* tabBar = m_CentralTabWidget->findChild< QTabBar* >();
997
#endif // USE_TABBED_VIEW
998 999 1000 1001 1002

  // Initialize image-view.
  assert( m_ImageView==NULL );
  m_ImageView = CreateImageViewWidget();

1003
#if USE_TABBED_VIEW
1004 1005 1006 1007 1008 1009 1010 1011
  // Add first tab: image-view.
  int index = m_CentralTabWidget->addTab(
    m_ImageView,
    tr( "Image view" )
  );

  tabBar->setTabButton( index, QTabBar::RightSide, 0);
  tabBar->setTabButton( index, QTabBar::LeftSide, 0);
1012 1013 1014 1015 1016

#else // USE_TABBED_VIEW
  setCentralWidget( m_ImageView );

#endif // USE_TABBED_VIEW
1017 1018
}

1019 1020 1021 1022 1023
/*****************************************************************************/
void
MainWindow
::InitializeRenderToolBar()
{
1024 1025 1026
  m_UI->m_RenderToolBar->addSeparator();

  m_UI->m_RenderToolBar->addWidget(
1027
    new QLabel( tr( "Proj" ) )
1028 1029
  );

1030 1031 1032
  QComboBox * comboBox =
    new QComboBox( m_UI->m_RenderToolBar );

1033 1034
  comboBox->setToolTip( tr( "Select projection used as reference for the view." ) );

1035 1036
  assert( comboBox!=NULL );

1037
  comboBox->setObjectName( REFERENCE_LAYER_COMBOBOX_NAME );
1038 1039
  comboBox->setMinimumSize(
    QSize(
1040
#ifdef OTB_DEBUG
1041 1042 1043 1044 1045 1046 1047
      116,
#else
      128,
#endif
      0
    )
  );
1048 1049

  m_UI->m_RenderToolBar->addWidget( comboBox );
1050
  m_UI->m_RenderToolBar->addWidget( m_ProjectionBarWidget );
1051
}
1052

1053 1054 1055 1056 1057 1058
/*****************************************************************************/
void
MainWindow
::InitializeShaderToolBar()
{
  assert( m_ShaderWidget==NULL );
1059

1060
  {
1061 1062 1063
  m_UI->m_ShaderToolBar->addWidget(
    new QLabel( tr( "Layer FX" ) )
  );
1064

1065
  m_ShaderWidget = new ShaderWidget( m_UI->m_ShaderToolBar );
1066

1067
  m_UI->m_ShaderToolBar->addWidget( m_ShaderWidget );
1068 1069 1070
  }
}

1071 1072 1073 1074 1075 1076 1077 1078 1079
/*****************************************************************************/
void
MainWindow
::InitializeProjectionBarWidget()
{
  assert( m_ProjectionBarWidget==NULL );
  m_ProjectionBarWidget = new ProjectionBarWidget( m_UI->m_ShaderToolBar );
}

1080 1081 1082 1083 1084
/*****************************************************************************/
void
MainWindow
::InitializeStatusBarWidgets()
{
1085 1086 1087 1088 1089 1090 1091 1092
  // MANTIS-
  assert( m_StatusBarWidget==NULL );

  m_StatusBarWidget = new StatusBarWidget( statusBar() );

  statusBar()->addPermanentWidget( m_StatusBarWidget, 1 );

  m_StatusBarWidget->setEnabled( false );
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
}

/*****************************************************************************/
ImageViewWidget*
MainWindow
::CreateImageViewWidget( QGLWidget* sharedGlWidget )
{
  ImageViewRenderer* renderer =
    new ImageViewRenderer( this );

  ImageViewManipulator* manipulator =
    new ImageViewManipulator(
#if USE_VIEW_SETTINGS_SIDE_EFFECT
      renderer->GetViewSettings(),
#endif // USE_VIEW_SETTINGS_SIDE_EFFECT
      this
    );

  ImageViewWidget* imageView = new ImageViewWidget(
    manipulator, // (will be reparented.)
    renderer, // (will be reparented.)
    this,
    sharedGlWidget
  );

  imageView->setMinimumWidth( 256 );

  return imageView;
}

/*****************************************************************************/
ImageViewWidget*
MainWindow
::CreateQuicklookViewWidget( QGLWidget* sharedGlWidget )
{
  QuicklookViewRenderer* renderer =
    new QuicklookViewRenderer( this );

  QuicklookViewManipulator* manipulator =
    new QuicklookViewManipulator(
#if USE_VIEW_SETTINGS_SIDE_EFFECT
      renderer->GetViewSettings(),
#endif // USE_VIEW_SETTINGS_SIDE_EFFECT
      this
    );

  ImageViewWidget* quicklookView = new ImageViewWidget(
    manipulator, // (will be reparented.)
    renderer, // (will be reparented.)
    this,
    sharedGlWidget
  );

1146
  quicklookView->SetPickingEnabled( false );
1147
  quicklookView->SetPickingDefaultStatus( false );
1148

1149 1150
  quicklookView->setMinimumSize(  32,  32 );
  quicklookView->setMaximumSize( 1024, 1024 );
1151 1152 1153 1154 1155 1156
  quicklookView->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );

  return quicklookView;
}

/*****************************************************************************/
1157
CountType
1158
MainWindow
1159
::ImportImage( const QString& filename, StackedLayerModel::SizeType index )
1160
{
1161
  // qDebug() << this << "::ImportImage(" << filename << "," << index << ")";
1162 1163 1164

  //
  // Get stacked-layer.
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
  assert( Application::Instance() );
  assert(
    Application::Instance()->GetModel()==
    Application::Instance()->GetModel< StackedLayerModel >()
  );

  StackedLayerModel* stackedLayerModel =
    Application::Instance()->GetModel< StackedLayerModel >();

  assert( stackedLayerModel!=NULL );

1176 1177 1178
  //
  // CDS import
  {
1179
    ImportSubDatasetDialog * importDialog = new ImportSubDatasetDialog( filename, this );
1180 1181 1182 1183 1184 1185

    assert( importDialog!=NULL );

    if( importDialog->HasSubDatasets() )
      {
      if( importDialog->exec()==QDialog::Rejected )
1186 1187 1188 1189
        {
        delete importDialog;
        return 0;
        }
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

      IntVector::size_type count = 0;
      IntVector indices;

      importDialog->GetSubDatasets( indices );

      for( IntVector::size_type i=0;
	   i<indices.size();
	   ++ i )
	count +=
	  ImportImage(
1201
	    QString( "%1%2&sdataidx=%3" ).arg( filename ).arg( filename.count(QChar('?')) ? "" : "?" ).arg( indices[ i ] ),
1202 1203 1204
	    index + count
	  );

1205
      delete importDialog;
1206 1207
      return count;
      }
1208 1209 1210 1211
    else
      {
      delete importDialog;
      }
1212 1213 1214 1215
  }
  // CDS import.
  //

1216 1217 1218 1219 1220 1221 1222 1223 1224
  //
  // Count images regarding their spatial-reference type.
  size_t gcs = 0; // Geo/Carto/Sensor
  size_t unk = 0; // Unknown

  stackedLayerModel->CountSRT( unk, gcs, gcs, gcs );

  //
  //
1225 1226 1227 1228 1229 1230
  // Coverity-112757
  // {
  SpatialReferenceType srt = SRT_UNKNOWN;

  try
    {
1231 1232 1233
    // qDebug()
    //   << this << "\n"
    //   << "\tQString:" << filename;
1234

1235 1236 1237
    // std::cout
    //   << "\tstd::string: " << QFile::encodeName( filename ).constData()
    //   << std::endl;
1238 1239

    srt = GetSpatialReferenceType( QFile::encodeName( filename ).constData() );
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    }
  catch( const std::exception & exception )
    {
    QMessageBox::warning(
      this,
      "Warning!",
      tr( "Exception caught while checking Spatial Reference Type of image-file '%1':\n\n%2" )
      .arg( filename )
      .arg( exception.what() )
    );
    }
  // }
  // Coverity-112757.
1253 1254 1255 1256 1257 1258 1259 1260

  QMessageBox::StandardButton button = QMessageBox::NoButton;

  if( unk==0 && gcs>0 && srt==SRT_UNKNOWN )
    {
    button =
      QMessageBox::warning(
	this,
1261
	tr( "Warning!" ),
1262 1263 1264 1265 1266 1267 1268
	tr( "No projection information (geographical or cartographical coordinate-system or sensor model) has been found for image. While already loaded images all have some, they are displayed in a geo-referenced view.\n\nLoading '%1' will cause the display to switch to a non geo-referenced view (where images are displayed relatively regarding their origin and spacing)." )
	.arg( filename ),
	QMessageBox::Ok | QMessageBox::Cancel
      );
    }

  if( button==QMessageBox::Cancel )
1269
    return 0;
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293

  /*
  switch( button )
    {
    case QMessageBox::Cancel:
      return;
      break;

    case QMessageBox::Ok:
      // Disable reference-layer combo-box.
      // Disable layer-stack projection-button.
      break;

    default:
      assert( false && "Unhandled QMessageBox::StandardButton value!" );
      break;
    }
  */

  //
  // Import image file.
  VectorImageModel * imageModel = ImportImage( filename, -1, -1 );

  if( imageModel==NULL )
1294
    return 0;
1295

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
  //
  // Bypass rendering of image-views.
  assert( m_ImageView!=NULL );
  bool bypassImageView = m_ImageView->SetBypassRenderingEnabled( true );

  ImageViewWidget * quicklookView = GetQuicklookView();
  assert( quicklookView!=NULL );

  bool bypassQuicklookView = quicklookView->SetBypassRenderingEnabled( true );

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
  //
  // Block some signals.
  //
  // BUGFIX: MANTIS-1120
  // {
  assert( m_UI!=NULL );
  assert( m_UI->m_RenderToolBar!=NULL );

  QComboBox * referenceLayerComboBox =
    m_UI->m_RenderToolBar->findChild< QComboBox * >(
      REFERENCE_LAYER_COMBOBOX_NAME
    );

  assert( referenceLayerComboBox!=NULL );

1321
  bool blocked = referenceLayerComboBox->blockSignals( true );
1322 1323
  // }

1324
  //
1325
  // Store image-mode in layer-stack.
1326
  stackedLayerModel->Insert( imageModel, index );
1327

1328 1329
  imageModel->setParent( stackedLayerModel );

1330 1331
  stackedLayerModel->SetCurrent( imageModel );

1332 1333 1334 1335 1336 1337
  if( gcs==0 )
    {
    if( unk==0 )
      {
      if( srt!=SRT_UNKNOWN )
	stackedLayerModel->SetReference( imageModel  );
1338

1339 1340 1341 1342
      UserZoomExtent();
      }
    }
  else if( unk==0 && srt==SRT_UNKNOWN )
1343 1344
    stackedLayerModel->SetReference( StackedLayerModel::NIL_INDEX );

1345
  //
1346
  // Re-activate rendering of image-views.
1347 1348
  m_ImageView->SetBypassRenderingEnabled( bypassImageView );
  quicklookView->SetBypassRenderingEnabled( bypassQuicklookView );
1349 1350 1351 1352 1353 1354

  //
  // Un-block some signals.
  //
  // BUGFIX: MANTIS-1120
  // {
1355
  referenceLayerComboBox->blockSignals( blocked );
1356 1357
  // }
  //
1358 1359

  return 1;
1360
}
1361

1362 1363 1364 1365 1366 1367 1368 1369
/*****************************************************************************/
void
MainWindow
::ImportImages( const QStringList & filenames )
{
  if( filenames.isEmpty() )
    return;

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
  assert( I18nCoreApplication::Instance()!=NULL );

  {
    QVariant value(
      I18nApplication::Instance()->RetrieveSettingsKey(
	I18nCoreApplication::SETTINGS_KEY_OVERVIEWS_ENABLED
      )
    );

    if( !( value.isValid() ? value.toBool() : OVERVIEWS_ENABLED_DEFAULT ) ||
	!BuildGDALOverviews( filenames ) )
      return;
  }
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397

  if( filenames.count()==1 )
    ImportImage( filenames.front(), 0 );

  else
    {
    StackedLayerModel::SizeType i = 0;

    for( QStringList::const_iterator it( filenames.constBegin() );
         it!=filenames.end();
	 ++it )
      {
        ImportImage( *it, i ++ );
      }
    }
1398 1399 1400 1401 1402 1403 1404 1405

  assert( m_ImageView!=NULL );
  m_ImageView->updateGL();

  ImageViewWidget * quicklookView = GetQuicklookView();
  assert( quicklookView!=NULL );

  quicklookView->updateGL();
1406 1407 1408 1409 1410
}

/*****************************************************************************/
void
MainWindow
1411
::closeEvent( QCloseEvent * e )
1412
{
1413
  assert( e!=NULL );
1414

1415 1416 1417 1418
  {
    //
    // List OTB-application widgets.
    typedef QList< mvd::Wrapper::QtWidgetView * > QtWidgetViewList;
1419

1420
    QtWidgetViewList c( findChildren< mvd::Wrapper::QtWidgetView * >() );
1421

1422
    QStringList names;
1423

1424 1425
    //
    // Find out which OTB-applications are running.
1426 1427
    for( QtWidgetViewList::iterator it( c.begin() );
	 it!=c.end();
1428
	 ++ it )
1429
      {
1430 1431 1432 1433 1434
      assert( *it );

      if( !( *it )->IsClosable() )
	{
	assert( !( *it )->GetApplication().IsNull() );
1435

1436
	// qDebug() << "OTB-application:" << ( *it )->GetApplication()->GetDocName();
1437

1438 1439
	names.push_back( ( *it )->GetApplication()->GetDocName() );
	}
1440 1441
      }

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
    //
    // If some OTB-application is running, display warning, names and
    // prevent to close.
    if( !names.isEmpty() )
      {
      QMessageBox::warning(
	this,
	tr( "Warning!" ),
	tr(
	  PROJECT_NAME
	  " cannot exit while some OTB-application is running!\n\n"
	  "Please wait for following OTB-applicatio(s) to exit:\n- %1"
	)
	.arg( names.join( "\n- " ) )
      );
1457

1458
      e->ignore();
1459

1460 1461 1462
      return;
      }
  }
1463

1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485

  {
    assert( I18nCoreApplication::Instance()!=NULL );
    assert( I18nCoreApplication::Instance()->GetModel()==
	    I18nCoreApplication::Instance()->GetModel< StackedLayerModel >() );

    StackedLayerModel * stackedLayerModel =
      I18nCoreApplication::Instance()->GetModel< StackedLayerModel >();

    if( stackedLayerModel!=NULL &&
	!stackedLayerModel->IsEmpty() )
      {
      QMessageBox::StandardButton button =
	QMessageBox::question(
	  this,
	  PROJECT_NAME,
	  tr( "Some files have been loaded. Are you sure you want to quit?" ),
	  QMessageBox::Yes | QMessageBox::No
	);

      if( button==QMessageBox::No )
	{
1486
	e->ignore();
1487 1488 1489 1490 1491 1492

	return;
	}
      }
  }

1493

1494
  SaveLayout( Monteverdi_UI_VERSION );
1495 1496


1497 1498
  //
  // Otherwise, close.
1499
  I18nMainWindow::closeEvent( e );
1500 1501 1502
}

/*****************************************************************************/
1503 1504 1505 1506 1507 1508
void
MainWindow
::virtual_InitializeUI()
{
  assert( Application::Instance() );

1509
  Application::Instance()->SetModel( new StackedLayerModel() );
1510
}
1511

1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
/*****************************************************************************/
void
MainWindow
::ConnectReferenceLayerComboBox( StackedLayerModel * model )
{
  assert( model!=NULL );

  QComboBox * comboBox =
    m_UI->m_RenderToolBar->findChild< QComboBox * >(
      REFERENCE_LAYER_COMBOBOX_NAME
    );

  assert( comboBox!=NULL );

  QObject::connect(
    comboBox,
    SIGNAL( currentIndexChanged( int ) ),
    // to:
    this,
    SLOT( OnReferenceLayerCurrentIndexChanged( int ) )
  );

  QObject::connect(
    model,
    SIGNAL( ContentChanged() ),
    // to:
    this,
    SLOT( RefreshReferenceLayerComboBox() )
  );

1542 1543 1544 1545 1546 1547 1548 1549
  QObject::connect(
    model,
    SIGNAL( ContentReset() ),
    // to:
    this,
    SLOT( RefreshReferenceLayerComboBox() )
  );

1550 1551 1552 1553 1554 1555 1556
  QObject::connect(
    model,
    SIGNAL( OrderChanged() ),
    // to:
    this,
    SLOT( RefreshReferenceLayerComboBox() )
  );
1557 1558 1559 1560 1561 1562

  QObject::connect(
    model,
    SIGNAL( ReferenceChanged( size_t ) ),
    // to:
    this,
1563
    SLOT( OnReferenceLayerChanged( size_t ) )
1564
  );
1565 1566 1567 1568 1569 1570 1571 1572

  QObject::connect(
    model,
    SIGNAL( LayerRenamed() ),
    // to:
    this,
    SLOT( RefreshReferenceLayerComboBox() )
  );
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
}

/*****************************************************************************/
void
MainWindow
::SetupReferenceLayerComboBox( StackedLayerModel * model )
{
  QComboBox * comboBox =
    m_UI->m_RenderToolBar->findChild< QComboBox * >(
      REFERENCE_LAYER_COMBOBOX_NAME
    );

  assert( comboBox!=NULL );

1587

1588
  bool blocked = comboBox->blockSignals( true );
1589
  {
1590 1591
  comboBox->clear();

1592
  comboBox->addItem( "None" );
1593
  }
1594
  comboBox->blockSignals( blocked );
1595

1596

1597 1598 1599
  if( model==NULL )
    return;

1600

1601 1602 1603
  for( StackedLayerModel::SizeType i=0;
       i<model->GetCount();
       ++i )
1604
    {
1605 1606 1607
    AbstractLayerModel * layer = model->At( i );
    assert( layer!=NULL );

1608 1609 1610 1611 1612
    comboBox->addItem(
      QString( "%1 (%2)" )
      .arg( layer->GetAuthorityCode( true ).c_str()  )
      .arg( layer->GetName() )
    );
1613 1614
    }

1615 1616 1617 1618 1619 1620 1621 1622
  {
  size_t gcs = 0; // Geo/Carto/Sensor
  size_t unk = 0; // Unknown

  model->CountSRT( unk, gcs, gcs, gcs );

  comboBox->setEnabled( model->GetCount()>0 && unk==0 );
  }
1623

1624 1625 1626
  comboBox->setCurrentIndex(
    model->GetReferenceIndex()>=model->GetCount()
    ? 0 // comboBox->count() - 1
1627
    : model->GetReferenceIndex() + 1
1628
  );
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
}

/*****************************************************************************/
void
MainWindow
::DisconnectReferenceLayerComboBox( StackedLayerModel * model )
{
  QComboBox * comboBox =
    m_UI->m_RenderToolBar->findChild< QComboBox * >(
      REFERENCE_LAYER_COMBOBOX_NAME
    );

  assert( comboBox!=NULL );

  QObject::disconnect(
    comboBox,
    SIGNAL( currentIndexChanged( int ) ),
    // to:
    this,
    SLOT( OnReferenceLayerCurrentIndexChanged( int ) )
  );

  if( model!=NULL )
    {
    QObject::disconnect(
      model,
      SIGNAL( ContentChanged() ),
      // to:
      this,
      SLOT( RefreshReferenceLayerComboBox() )
    );

1661 1662 1663 1664 1665 1666 1667 1668
    QObject::disconnect(
      model,
      SIGNAL( ContentReset() ),
      // to:
      this,
      SLOT( RefreshReferenceLayerComboBox() )
    );

1669 1670 1671 1672 1673 1674 1675
    QObject::disconnect(
      model,
      SIGNAL( OrderChanged() ),
      // to:
      this,
      SLOT( RefreshReferenceLayerComboBox() )
    );
1676 1677 1678 1679 1680 1681

    QObject::disconnect(
      model,
      SIGNAL( ReferenceChanged( size_t ) ),
      // to:
      this,
1682
      SLOT( OnReferenceLayerChanged( size_t ) )
1683
    );
1684 1685 1686 1687 1688 1689 1690 1691

    QObject::connect(
      model,
      SIGNAL( LayerRenamed() ),
      // to:
      this,
      SLOT( RefreshReferenceLayerComboBox() )
    );
1692 1693 1694 1695 1696 1697
    }

  comboBox->clear();
  comboBox->setEnabled( false );
}

1698 1699 1700 1701 1702 1703 1704
/****************************************************************************/
#if USE_OTB_APPS

void
MainWindow
::SetupOTBApplications()
{
1705 1706
  // qDebug() << this << "::SetupOTBApplications()";
  // qDebug() << "{";
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716

  assert( m_OtbApplicationsBrowserDock==NULL );

  //
  // Load OTB-applications and create browser in dock-widget .
  m_OtbApplicationsBrowserDock =
    AddDockWidget
    < ApplicationsToolBox, ApplicationsToolBoxController, QDockWidget >
    ( "APPLICATIONS_BROWSER",
      tr( "OTB-Applications browser" ),
1717
      Qt::RightDockWidgetArea
1718 1719
    );

1720
  tabifyDockWidget( m_HistogramDock, m_OtbApplicationsBrowserDock );
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756

  SetControllerModel(
    m_OtbApplicationsBrowserDock,
    Application::Instance()->GetOTBApplicationsModel()
  );

  //
  // Connect signals.
  //
  // need to get the ApplicationToolBox widget to setup connections.
  // a double click on the tree widget should trigger a signal connected
  // to this MainWindow slot. this slot will be in charge of getting the
  // widget of the application selected, and show it in the
  // MainWindow centralView.

  // # Step 1 : get the ApplicationToolBoxWidget
  ApplicationsToolBox* appWidget =
    qobject_cast< ApplicationsToolBox * >(
      m_OtbApplicationsBrowserDock->findChild< ApplicationsToolBox* >()
    );
  assert( appWidget!=NULL );

  // # Step 2 : setup connections
  QObject::connect(
    appWidget,
    SIGNAL( ApplicationToLaunchSelected(const QString &, const QString &) ),
    this,
    SLOT( OnApplicationToLaunchSelected(const QString &, const QString &) )
  );

  // # Step 3 : connect close slots
  // TODO

  //
  // Update main-window UI.
  m_UI->action_LoadOTBApplications->setEnabled( false );
1757
  m_UI->action_LoadOTBApplications->setVisible( false );
1758 1759 1760 1761 1762

  m_UI->menu_View->addAction(
    m_OtbApplicationsBrowserDock->toggleViewAction()
  );

1763
  // qDebug() << "}";
1764 1765 1766 1767
}

#endif // USE_OTB_APPS

1768 1769
/*****************************************************************************/
/* SLOTS                                                                     */
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
/*****************************************************************************/
void
MainWindow
::on_action_GLSL_triggered( bool checked )
{
  // qDebug() << this << "::on_action_GLSL_triggered(" << checked << ")";

  SetGLSLEnabled( m_isGLSLAvailable && !m_ForceNoGLSL && checked );
}

1780 1781 1782 1783 1784
/*****************************************************************************/
void
MainWindow
::on_action_Keymap_triggered()
{
1785 1786 1787 1788
  assert( m_KeymapDialog!=NULL );

  if( m_KeymapDialog->isVisible() )
    return;
1789

1790
  m_KeymapDialog->show();
1791 1792
}

1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
/****************************************************************************/
void
MainWindow
::on_action_LoadOTBApplications_triggered()
{
#if USE_OTB_APPS
  SetupOTBApplications();
#endif // USE_OTB_APPS
}

1803 1804 1805
/*****************************************************************************/
void
MainWindow
1806
::on_action_OpenImage_triggered()
1807 1808 1809
{
  //
  // Select filename.
1810
  QString caption(tr("Open file..."));
1811
  ImportImages(
1812
    otb::GetOpenFileNames( this, caption )
1813 1814 1815 1816 1817 1818
  );
}

/*****************************************************************************/
void
MainWindow
1819
::on_action_Preferences_triggered()
1820 1821 1822 1823 1824 1825
{
  PreferencesDialog prefDialog( this );

  prefDialog.exec();
}

1826 1827 1828 1829 1830 1831 1832
/*****************************************************************************/
void
MainWindow
::on_action_SaveScreenshot_triggered()
{
  assert( m_ImageView!=NULL );

1833
  m_ImageView->SaveScreenshot( false );
1834 1835
}

1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
/*****************************************************************************/
void
MainWindow
::on_action_ZoomIn_triggered()
{
  emit UserZoomIn();
}

/*****************************************************************************/
void
MainWindow
::on_action_ZoomOut_triggered()
{
  emit UserZoomOut();
}

/*****************************************************************************/
void
MainWindow
::on_action_ZoomExtent_triggered()
{
  emit UserZoomExtent();
}

/*****************************************************************************/
void
MainWindow
::on_action_ZoomFull_triggered()
{
  emit UserZoomFull();
}