WMainWindow.cpp 37.3 KB
Newer Older
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
wiebel's avatar
wiebel committed
7
//
wiebel's avatar
wiebel committed
8
// This file is part of OpenWalnut.
wiebel's avatar
wiebel committed
9
//
wiebel's avatar
wiebel committed
10
// OpenWalnut is free software: you can redistribute it and/or modify
wiebel's avatar
wiebel committed
11 12 13 14
// 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.
//
wiebel's avatar
wiebel committed
15
// OpenWalnut is distributed in the hope that it will be useful,
wiebel's avatar
wiebel committed
16 17 18 19 20
// 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 Lesser General Public License
wiebel's avatar
wiebel committed
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
wiebel's avatar
wiebel committed
22
//
23 24
//---------------------------------------------------------------------------

25
#include <iostream>
Mathias Goldau's avatar
Mathias Goldau committed
26
#include <map>
27 28
#include <string>
#include <vector>
29

30 31
#include <boost/thread.hpp>

32
#include <QtGui/QApplication>
wiebel's avatar
wiebel committed
33
#include <QtGui/QDockWidget>
34
#include <QtGui/QFileDialog>
Mathias Goldau's avatar
Mathias Goldau committed
35 36 37
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
38
#include <QtGui/QMessageBox>
39
#include <QtGui/QShortcut>
Mathias Goldau's avatar
Mathias Goldau committed
40
#include <QtGui/QSlider>
41 42
#include <QtGui/QVBoxLayout>

43 44
#include "../../common/WColor.h"
#include "../../common/WPreferences.h"
Mathias Goldau's avatar
Mathias Goldau committed
45 46 47 48
#include "../../dataHandler/WDataSetFibers.h"
#include "../../dataHandler/WDataSetSingle.h"
#include "../../dataHandler/WEEG2.h"
#include "../../graphicsEngine/WROIBox.h"
schurade's avatar
schurade committed
49
#include "../../graphicsEngine/WGEZoomTrackballManipulator.h"
50
#include "../../kernel/WKernel.h"
51 52 53
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleCombiner.h"
#include "../../kernel/WModuleCombinerTypes.h"
54
#include "../../kernel/WProjectFile.h"
55 56
#include "../../kernel/modules/data/WMData.h"
#include "../../kernel/modules/navSlices/WMNavSlices.h"
57
#include "../icons/WIcons.h"
Mathias Goldau's avatar
Mathias Goldau committed
58 59 60 61
#include "datasetbrowser/WPropertyBoolWidget.h"
#include "events/WEventTypes.h"
#include "events/WModuleCrashEvent.h"
#include "events/WModuleReadyEvent.h"
62
#include "events/WOpenCustomDockWidgetEvent.h"
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
63
#include "guiElements/WQtPropertyBoolAction.h"
Mathias Goldau's avatar
Mathias Goldau committed
64 65 66 67 68
#include "WQtCustomDockWidget.h"
#include "WQtGLWidget.h"
#include "WQtNavGLWidget.h"

#include "WMainWindow.h"
69

70
WMainWindow::WMainWindow() :
71
    QMainWindow(),
72
    m_currentCompatiblesToolbar( NULL ),
73 74
    m_iconManager(),
    m_fibLoaded( false )
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
75 76
{
}
77

78
void WMainWindow::setupGUI()
79
{
80
    m_iconManager.addIcon( std::string( "load" ), fileopen_xpm );
81
    m_iconManager.addIcon( std::string( "loadProject" ), projOpen_xpm );
82
    m_iconManager.addIcon( std::string( "saveProject" ), projSave_xpm );
83
    m_iconManager.addIcon( std::string( "logo" ), logoIcon_xpm );
84
    m_iconManager.addIcon( std::string( "help" ), question_xpm );
85
    m_iconManager.addIcon( std::string( "quit" ), quit_xpm );
86 87 88
    m_iconManager.addIcon( std::string( "moduleBusy" ), moduleBusy_xpm );
    m_iconManager.addIcon( std::string( "moduleCrashed" ), moduleCrashed_xpm );
    m_iconManager.addIcon( std::string( "remove" ), remove_xpm );
89
    m_iconManager.addIcon( std::string( "config" ), preferences_system_xpm );
90

91

92
    if( objectName().isEmpty() )
93
    {
94
        setObjectName( QString::fromUtf8( "MainWindow" ) );
95
    }
96
    // TODO(all): what is this?
97 98
    resize( 946, 632 );
    setWindowIcon( m_iconManager.getIcon( "logo" ) );
99
    setWindowTitle( QApplication::translate( "MainWindow", "OpenWalnut (development version)", 0, QApplication::UnicodeUTF8 ) );
100

101

102 103 104 105 106 107
    // the dataset browser instance is needed for the menu
    m_datasetBrowser = new WQtDatasetBrowser( this );
    m_datasetBrowser->setFeatures( QDockWidget::AllDockWidgetFeatures );
    addDockWidget( Qt::RightDockWidgetArea, m_datasetBrowser );
    m_datasetBrowser->addSubject( "Default Subject" );

108 109 110 111 112 113
    setupPermanentToolBar();

    // we want the upper most tree item to be selected. This helps to make the always compatible modules
    // show up in the tool bar from the beginning. And ... it doesn't hurt.
    m_datasetBrowser->selectUpperMostEntry();

114 115
    // set the size of the dsb according to config file
    int dsbWidth = 250;
Alexander Wiebel's avatar
Alexander Wiebel committed
116
    if( WPreferences::getPreference( "qt4gui.dsbWidth", &dsbWidth ) )
117 118 119 120 121 122 123
    {
        m_datasetBrowser->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
        m_datasetBrowser->setMinimumWidth( dsbWidth );
    }

    // hide the DSB by default?
    bool dsbInvisibleByDefault = false;
Alexander Wiebel's avatar
Alexander Wiebel committed
124
    if( WPreferences::getPreference( "qt4gui.dsbInvisibleByDefault", &dsbInvisibleByDefault ) )
125 126 127 128 129 130
    {
        m_datasetBrowser->setVisible( !dsbInvisibleByDefault );
    }

    // undock the DSB by default?
    bool dsbFloatingByDefault = false;
Alexander Wiebel's avatar
Alexander Wiebel committed
131
    if( WPreferences::getPreference( "qt4gui.dsbFloatingByDefault", &dsbFloatingByDefault ) )
132 133 134 135
    {
        m_datasetBrowser->setFloating( dsbFloatingByDefault );
    }

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
136 137 138 139
    // NOTE: Please be aware that not every menu needs a shortcut key. If you add a shortcut, you should use one of the
    // QKeySequence::StandardKey defaults and avoid ambiguities like Ctrl-C for the configure dialog is not the best choice as Ctrl-C, for the
    // most users is the Copy shortcut.

140
    m_menuBar = new QMenuBar( this );
141 142 143 144 145 146

    // hide menu?
    bool hideMenu = false;
    WPreferences::getPreference( "qt4gui.hideMenuBar", &hideMenu );
    m_menuBar->setVisible( !hideMenu );

147
    QMenu* fileMenu = m_menuBar->addMenu( "File" );
148

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
149 150
    fileMenu->addAction( m_iconManager.getIcon( "load" ), "Load Dataset", this, SLOT( openLoadDialog() ), QKeySequence(  QKeySequence::Open ) );
    fileMenu->addSeparator();
151
    fileMenu->addAction( m_iconManager.getIcon( "loadProject" ), "Load Project", this, SLOT( projectLoad() ) );
152 153
    QMenu* saveMenu = fileMenu->addMenu( m_iconManager.getIcon( "saveProject" ), "Save Project" );
    saveMenu->addAction( "Save Project", this, SLOT( projectSaveAll() ), QKeySequence::Save );
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
154 155 156 157
    saveMenu->addAction( "Save Modules Only", this, SLOT( projectSaveModuleOnly() ) );
    saveMenu->addAction( "Save Camera Only", this, SLOT( projectSaveCameraOnly() ) );
    saveMenu->addAction( "Save ROIs Only", this, SLOT( projectSaveROIOnly() ) );
    fileMenu->addSeparator();
158
    fileMenu->addAction( m_iconManager.getIcon( "config" ), "Config", this, SLOT( openConfigDialog() ) );
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
159
    fileMenu->addSeparator();
160 161 162
    // TODO(all): If all distributions provide a newer QT version we should use QKeySequence::Quit here
    //fileMenu->addAction( m_iconManager.getIcon( "quit" ), "Quit", this, SLOT( close() ), QKeySequence( QKeySequence::Quit ) );
    fileMenu->addAction( m_iconManager.getIcon( "quit" ), "Quit", this, SLOT( close() ),  QKeySequence( Qt::CTRL + Qt::Key_Q ) );
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
163

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
164
    // This QAction stuff is quite ugly and complicated some times ... There is no nice constructor which takes name, slot keysequence and so on
165 166
    // directly -> set shortcuts, and some further properties using QAction's interface
    QMenu* viewMenu = m_menuBar->addMenu( "View" );
167 168

    QAction* dsbTrigger = m_datasetBrowser->toggleViewAction();
169 170 171 172 173
    QList< QKeySequence > dsbShortcut;
    dsbShortcut.append( QKeySequence( Qt::Key_F9 ) );
    dsbTrigger->setShortcuts( dsbShortcut );
    viewMenu->addAction( dsbTrigger );
    viewMenu->addSeparator();
174
    this->addAction( dsbTrigger );  // this enables the action even if the menu bar is invisible
175

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
176 177 178
    // NOTE: the shortcuts for these view presets should be chosen carefully. Most keysequences have another meaning in the most applications
    // so the user may get confused. It is also not a good idea to take letters as they might be used by OpenSceneGraph widget ( like "S" for
    // statistics ).
179 180 181 182 183 184
    viewMenu->addAction( "Left", this, SLOT( setPresetViewLeft() ),           QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_L ) );
    viewMenu->addAction( "Right", this, SLOT( setPresetViewRight() ),         QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_R ) );
    viewMenu->addAction( "Superior", this, SLOT( setPresetViewSuperior() ),   QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_S ) );
    viewMenu->addAction( "Inferior", this, SLOT( setPresetViewInferior() ),   QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_I ) );
    viewMenu->addAction( "Anterior", this, SLOT( setPresetViewAnterior() ),   QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_A ) );
    viewMenu->addAction( "Posterior", this, SLOT( setPresetViewPosterior() ), QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_P ) );
185

186
    QMenu* helpMenu = m_menuBar->addMenu( "Help" );
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
187 188 189
    helpMenu->addAction( m_iconManager.getIcon( "help" ), "About OpenWalnut", this, SLOT( openAboutDialog() ),
                         QKeySequence( QKeySequence::HelpContents )
    );
190

191 192
    setMenuBar( m_menuBar );

193
    m_mainGLWidget = boost::shared_ptr< WQtGLWidget >( new WQtGLWidget( "main", this, WGECamera::ORTHOGRAPHIC ) );
194
    setCentralWidget( m_mainGLWidget.get() );
wiebel's avatar
wiebel committed
195

196 197 198 199 200
    // initially 3 navigation views
    {
        bool hideWidget;
        if( !( WPreferences::getPreference( "qt4gui.hideAxial", &hideWidget ) && hideWidget) )
        {
201
            m_navAxial = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "Axial View", this, "Axial Slice" ) );
202
            m_navAxial->setFeatures( QDockWidget::AllDockWidgetFeatures );
203 204 205 206
            addDockWidget( Qt::LeftDockWidgetArea, m_navAxial.get() );
        }
        if( !( WPreferences::getPreference( "qt4gui.hideCoronal", &hideWidget ) && hideWidget) )
        {
207
            m_navCoronal = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "Coronal View", this, "Coronal Slice" ) );
208
            m_navCoronal->setFeatures( QDockWidget::AllDockWidgetFeatures );
209 210 211 212
            addDockWidget( Qt::LeftDockWidgetArea, m_navCoronal.get() );
        }
        if( !( WPreferences::getPreference( "qt4gui.hideSagittal", &hideWidget ) && hideWidget) )
        {
213
            m_navSagittal = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "Sagittal View", this, "Sagittal Slice" ) );
214
            m_navSagittal->setFeatures( QDockWidget::AllDockWidgetFeatures );
215 216 217
            addDockWidget( Qt::LeftDockWidgetArea, m_navSagittal.get() );
        }
    }
218

Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
219 220 221
    // we do not need the dummy widget if there are no other widgets.
    if( m_navAxial || m_navCoronal || m_navSagittal )
    {
222 223 224 225
        m_dummyWidget = new QDockWidget( "Spacer", this );
        QWidget* tmp = new QWidget( m_dummyWidget );
        m_dummyWidget->setTitleBarWidget( tmp );
        m_dummyWidget->setFeatures( QDockWidget::DockWidgetClosable );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
226 227 228 229
        m_dummyWidget->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Ignored );
        addDockWidget( Qt::LeftDockWidgetArea, m_dummyWidget );
    }

230
    // Default background color from config file
231
    {
232 233 234 235 236 237 238 239 240 241
        WColor bgColor;
        double r;
        double g;
        double b;
        if( WPreferences::getPreference( "ge.bgColor.r", &r )
            && WPreferences::getPreference( "ge.bgColor.g", &g )
            && WPreferences::getPreference( "ge.bgColor.b", &b ) )
        {
            bgColor.setRGB( r, g, b );
            m_mainGLWidget->setBgColor( bgColor );
242

Alexander Wiebel's avatar
Alexander Wiebel committed
243
            if( m_navAxial )
244 245 246
            {
                m_navAxial->getGLWidget()->setBgColor( bgColor );
            }
Alexander Wiebel's avatar
Alexander Wiebel committed
247
            if( m_navCoronal )
248 249 250
            {
                m_navCoronal->getGLWidget()->setBgColor( bgColor );
            }
Alexander Wiebel's avatar
Alexander Wiebel committed
251
            if( m_navSagittal )
252 253 254
            {
                m_navSagittal->getGLWidget()->setBgColor( bgColor );
            }
255
        }
256
    }
257
}
258

259
void WMainWindow::setupPermanentToolBar()
260
{
261
    m_permanentToolBar = new WQtToolBar( "Permanent Toolbar", this );
262

263 264 265 266
    // Set the style of the toolbar
    // NOTE: this only works if the toolbar is used with QActions instead of buttons and other widgets
    m_permanentToolBar->setToolButtonStyle( getToolbarStyle() );

267 268 269 270
    m_iconManager.addIcon( std::string( "ROI icon" ), box_xpm );
    m_iconManager.addIcon( std::string( "axial icon" ), axial_xpm );
    m_iconManager.addIcon( std::string( "coronal icon" ), cor_xpm );
    m_iconManager.addIcon( std::string( "sagittal icon" ), sag_xpm );
271

272
    // TODO(all): this should be QActions to allow the toolbar style to work properly
273
    m_loadButton = new WQtPushButton( m_iconManager.getIcon( "load" ), "load", m_permanentToolBar );
274
    WQtPushButton* roiButton = new WQtPushButton( m_iconManager.getIcon( "ROI icon" ), "ROI", m_permanentToolBar );
275 276
    WQtPushButton* projectLoadButton = new WQtPushButton( m_iconManager.getIcon( "loadProject" ), "loadProject", m_permanentToolBar );
    WQtPushButton* projectSaveButton = new WQtPushButton( m_iconManager.getIcon( "saveProject" ), "saveProject", m_permanentToolBar );
277

278 279 280
    // setup save button
    QMenu* saveMenu = new QMenu( "Save Project", projectSaveButton );
    saveMenu->addAction( "Save Project", this, SLOT( projectSaveAll() ) );
281
    saveMenu->addAction( "Save Modules", this, SLOT( projectSaveModuleOnly() ) );
282 283 284 285 286
    saveMenu->addAction( "Save Camera", this, SLOT( projectSaveCameraOnly() ) );
    saveMenu->addAction( "Save ROIs", this, SLOT( projectSaveROIOnly() ) );
    projectSaveButton->setPopupMode( QToolButton::MenuButtonPopup );
    projectSaveButton->setMenu( saveMenu );

287
    connect( m_loadButton, SIGNAL( pressed() ), this, SLOT( openLoadDialog() ) );
288
    connect( roiButton, SIGNAL( pressed() ), this, SLOT( newRoi() ) );
289
    connect( projectLoadButton, SIGNAL( pressed() ), this, SLOT( projectLoad() ) );
290
    connect( projectSaveButton, SIGNAL( pressed() ), this, SLOT( projectSaveAll() ) );
291

292
    m_loadButton->setToolTip( "Load Data" );
293
    roiButton->setToolTip( "Create New ROI" );
294 295
    projectLoadButton->setToolTip( "Load a project from file" );
    projectSaveButton->setToolTip( "Save current project to file" );
296

297
    m_permanentToolBar->addWidget( m_loadButton );
298
    m_permanentToolBar->addSeparator();
299 300
    m_permanentToolBar->addWidget( projectLoadButton );
    m_permanentToolBar->addWidget( projectSaveButton );
301
    m_permanentToolBar->addSeparator();
302 303
    m_permanentToolBar->addWidget( roiButton );
    m_permanentToolBar->addSeparator();
304

Alexander Wiebel's avatar
Alexander Wiebel committed
305
    if( getToolbarPos() == InDSB )
306 307 308 309
    {
        m_datasetBrowser->addToolbar( m_permanentToolBar );
        //m_datasetBrowser->setTitleBarWidget( m_permanentToolBar );
    }
Alexander Wiebel's avatar
Alexander Wiebel committed
310
    else if( getToolbarPos() == Hide )
311 312 313 314 315 316 317 318
    {
        m_permanentToolBar->setVisible( false );
        addToolBar( Qt::TopToolBarArea, m_permanentToolBar );
    }
    else
    {
        addToolBar( toQtToolBarArea( getToolbarPos() ), m_permanentToolBar );
    }
319
}
320

321 322 323
void WMainWindow::autoAdd( boost::shared_ptr< WModule > module, std::string proto )
{
    // get the prototype.
Alexander Wiebel's avatar
Alexander Wiebel committed
324
    if( !WKernel::getRunningKernel()->getRootContainer()->applyModule( module, proto, true ) )
325 326 327 328 329 330
    {
        WLogger::getLogger()->addLogMessage( "Auto Display active but module " + proto + " could not be added.",
                                             "GUI", LL_ERROR );
    }
}

331 332
void WMainWindow::moduleSpecificSetup( boost::shared_ptr< WModule > module )
{
333 334 335 336 337 338
    // Add all special handlings here. This method is called whenever a module is marked "ready". You can set up the gui for special modules,
    // load certain modules for datasets and so on.

    // The Data Modules also play an special role. To have modules being activated when certain data got loaded, we need to hook it up here.
    bool useAutoDisplay = true;
    WPreferences::getPreference( "qt4gui.useAutoDisplay", &useAutoDisplay );
Alexander Wiebel's avatar
Alexander Wiebel committed
339
    if( useAutoDisplay && module->getType() == MODULE_DATA )
340 341 342 343 344 345 346 347 348
    {
        WLogger::getLogger()->addLogMessage( "Auto Display active and Data module added. The proper module will be added.",
                                             "GUI", LL_DEBUG );

        // data modules contain an member denoting the real data type. Currently we only have one data module and a not very modulated data
        // structures.
        boost::shared_ptr< WMData > dataModule = boost::shared_static_cast< WMData >( module );

        // grab data and identify type
Alexander Wiebel's avatar
Alexander Wiebel committed
349
        if( dataModule->getDataSet()->isA< WDataSetSingle >() && dataModule->getDataSet()->isTexture() )
350 351 352 353
        {
            // it is a dataset single
            // load a nav slice module if a WDataSetSingle is available!?

354
            // if it not already is running: add it
Alexander Wiebel's avatar
Alexander Wiebel committed
355
            if( !WMNavSlices::isRunning() )
356
            {
357
                autoAdd( module, "Navigation Slices" );
358
            }
359
        }
Alexander Wiebel's avatar
Alexander Wiebel committed
360
        else if( dataModule->getDataSet()->isA< WDataSetFibers >() )
361 362
        {
            // it is a fiber dataset -> add the FiberDisplay module
363 364

            // if it not already is running: add it
Alexander Wiebel's avatar
Alexander Wiebel committed
365
            if( !WMFiberDisplay::isRunning() )
366 367 368
            {
                autoAdd( module, "Fiber Display" );
            }
369
        }
Alexander Wiebel's avatar
Alexander Wiebel committed
370
        else if( dataModule->getDataSet()->isA< WEEG2 >() )
371
        {
372
            // it is a eeg dataset -> add the eegView module
373 374 375 376
            autoAdd( module, "EEG View" );
        }
    }

377
    // nav slices use separate buttons for slice on/off switching
Alexander Wiebel's avatar
Alexander Wiebel committed
378
    if( module->getName() == "Navigation Slices" )
379
    {
380
        boost::shared_ptr< WPropertyBase > prop = module->getProperties()->findProperty( "showAxial" );
Alexander Wiebel's avatar
Alexander Wiebel committed
381
        if( !prop )
382 383
        {
               WLogger::getLogger()->
384
                   addLogMessage( "Navigation Slices module does not provide the property \"showAxial\", which is required by the GUI.", "GUI",
385 386 387 388
                                  LL_ERROR );
        }
        else
        {
389 390 391
            WQtPropertyBoolAction* a = new WQtPropertyBoolAction( prop->toPropBool(), m_permanentToolBar );
            a->setToolTip( "Toggle Axial Slice" );
            a->setText( "Toggle Axial Slice" );
392
            a->setIcon( m_iconManager.getIcon( "axial icon" ) );
393
            m_permanentToolBar->addAction( a );
394
        }
395

396
        prop = module->getProperties()->findProperty( "showCoronal" );
Alexander Wiebel's avatar
Alexander Wiebel committed
397
        if( !prop )
398 399
        {
               WLogger::getLogger()->
400
                   addLogMessage( "Navigation Slices module does not provide the property \"showCoronal\", which is required by the GUI.", "GUI",
401 402 403 404
                                  LL_ERROR );
        }
        else
        {
405 406 407
            WQtPropertyBoolAction* a = new WQtPropertyBoolAction( prop->toPropBool(), m_permanentToolBar );
            a->setToolTip( "Toggle Coronal Slice" );
            a->setText( "Toggle Coronal Slice" );
408
            a->setIcon( m_iconManager.getIcon( "coronal icon" ) );
409
            m_permanentToolBar->addAction( a );
410
        }
411

412
        prop = module->getProperties()->findProperty( "showSagittal" );
Alexander Wiebel's avatar
Alexander Wiebel committed
413
        if( !prop )
414 415
        {
               WLogger::getLogger()->
416
                   addLogMessage( "Navigation Slices module does not provide the property \"showSagittal\", which is required by the GUI.", "GUI",
417 418 419 420
                                  LL_ERROR );
        }
        else
        {
421 422 423
            WQtPropertyBoolAction* a = new WQtPropertyBoolAction( prop->toPropBool(), m_permanentToolBar );
            a->setToolTip( "Toggle Saggital Slice" );
            a->setText( "Toggle Saggital Slice" );
424
            a->setIcon( m_iconManager.getIcon( "sagittal icon" ) );
425
            m_permanentToolBar->addAction( a );
426
        }
427 428

        // now setup the nav widget sliders
429
        prop = module->getProperties()->findProperty( "Axial Slice" );
Alexander Wiebel's avatar
Alexander Wiebel committed
430
        if( !prop )
431 432
        {
               WLogger::getLogger()->
433
                   addLogMessage( "Navigation Slices module does not provide the property \"Axial Slice\", which is required by the GUI.", "GUI",
434 435 436 437
                                  LL_ERROR );
        }
        else
        {
438 439 440 441
            if( m_navAxial )
            {
                m_navAxial->setSliderProperty( prop->toPropInt() );
            }
442 443
        }

444
        prop = module->getProperties()->findProperty( "Coronal Slice" );
Alexander Wiebel's avatar
Alexander Wiebel committed
445
        if( !prop )
446 447
        {
               WLogger::getLogger()->
448
                   addLogMessage( "Navigation Slices module does not provide the property \"Coronal Slice\", which is required by the GUI.", "GUI",
449 450 451 452
                                  LL_ERROR );
        }
        else
        {
453 454 455 456
            if( m_navCoronal )
            {
                m_navCoronal->setSliderProperty( prop->toPropInt() );
            }
457 458
        }

459
        prop = module->getProperties()->findProperty( "Sagittal Slice" );
Alexander Wiebel's avatar
Alexander Wiebel committed
460
        if( !prop )
461 462
        {
               WLogger::getLogger()->
463
                   addLogMessage( "Navigation Slices module does not provide the property \"Sagittal Slice\", which is required by the GUI.", "GUI",
464 465 466 467
                                  LL_ERROR );
        }
        else
        {
468 469 470 471
            if( m_navSagittal )
            {
               m_navSagittal->setSliderProperty( prop->toPropInt() );
            }
472
        }
473
    }
wiebel's avatar
wiebel committed
474
}
475

476 477 478 479 480
Qt::ToolButtonStyle WMainWindow::getToolbarStyle() const
{
    // this sets the toolbar style
    int toolBarStyle = 0;
    WPreferences::getPreference( "qt4gui.toolBarStyle", &toolBarStyle );
Alexander Wiebel's avatar
Alexander Wiebel committed
481
    if( ( toolBarStyle < 0 ) || ( toolBarStyle > 3 ) ) // ensure a valid value
482 483 484 485 486 487 488 489
    {
        toolBarStyle = 0;
    }

    // cast and return
    return static_cast< Qt::ToolButtonStyle >( toolBarStyle );
}

490 491 492 493 494 495 496 497
WMainWindow::ToolBarPosition WMainWindow::getToolbarPos()
{
    int toolbarPos = 0;
    WPreferences::getPreference( "qt4gui.toolBarPos", &toolbarPos );
    return static_cast< ToolBarPosition >( toolbarPos );
}

WMainWindow::ToolBarPosition WMainWindow::getCompatiblesToolbarPos()
498
{
499
    int compatiblesToolbarPos = 0;
Alexander Wiebel's avatar
Alexander Wiebel committed
500
    if( !WPreferences::getPreference( "qt4gui.compatiblesToolBarPos", &compatiblesToolbarPos ) )
501
    {
502 503 504 505 506 507 508 509 510 511 512 513
        return getToolbarPos();
    }

    return static_cast< ToolBarPosition >( compatiblesToolbarPos );
}

Qt::ToolBarArea WMainWindow::toQtToolBarArea( ToolBarPosition pos )
{
    switch ( pos )
    {
        case Top:
            return Qt::TopToolBarArea;
514
            break;
515 516
        case Bottom:
            return Qt::BottomToolBarArea;
517
            break;
518 519
        case Left:
            return Qt::LeftToolBarArea;
520
            break;
521 522
        case Right:
            return Qt::RightToolBarArea;
523
            break;
524 525
        case InDSB:
            return Qt::RightToolBarArea;
526
        default:
527
            return Qt::NoToolBarArea;
528
            break;
529
      }
530 531 532 533
}

void WMainWindow::setCompatiblesToolbar( WQtCombinerToolbar* toolbar )
{
Alexander Wiebel's avatar
Alexander Wiebel committed
534
    if( m_currentCompatiblesToolbar )
535 536 537 538 539
    {
        delete m_currentCompatiblesToolbar;
    }
    m_currentCompatiblesToolbar = toolbar;

540
    // hide it?
Alexander Wiebel's avatar
Alexander Wiebel committed
541
    if( getCompatiblesToolbarPos() == Hide )
542
    {
Alexander Wiebel's avatar
Alexander Wiebel committed
543
        if( toolbar )
544 545 546 547 548 549
        {
            toolbar->setVisible( false );
        }
        return;
    }

Alexander Wiebel's avatar
Alexander Wiebel committed
550
    if( !toolbar )
551 552 553
    {
        // ok, reset the toolbar
        // So create a dummy to permanently reserve the space
554
        m_currentCompatiblesToolbar = new WQtCombinerToolbar( this, WCombinerTypes::WCompatiblesList() );
555 556 557 558 559 560 561 562
    }

    // optional toolbar break
    {
        bool useToolBarBreak = true;
        WPreferences::getPreference( "qt4gui.useToolBarBreak", &useToolBarBreak );
        if( useToolBarBreak )
        {
563
            addToolBarBreak( toQtToolBarArea( getCompatiblesToolbarPos() ) );
564 565 566 567
        }
    }

    // and the position of the toolbar
568
    addToolBar( toQtToolBarArea( getCompatiblesToolbarPos() ), m_currentCompatiblesToolbar );
569
}
570

571
WQtDatasetBrowser* WMainWindow::getDatasetBrowser()
572
{
573
    return m_datasetBrowser;
574
}
575

576
void WMainWindow::projectSave( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer )
577 578 579 580
{
    QFileDialog fd;
    fd.setWindowTitle( "Save Project as" );
    fd.setFileMode( QFileDialog::AnyFile );
581
    fd.setAcceptMode( QFileDialog::AcceptSave );
582 583 584 585 586 587 588

    QStringList filters;
    filters << "Project File (*.owproj)"
            << "Any files (*)";
    fd.setNameFilters( filters );
    fd.setViewMode( QFileDialog::Detail );
    QStringList fileNames;
Alexander Wiebel's avatar
Alexander Wiebel committed
589
    if( fd.exec() )
590 591 592 593 594
    {
        fileNames = fd.selectedFiles();
    }

    QStringList::const_iterator constIterator;
Alexander Wiebel's avatar
Alexander Wiebel committed
595
    for( constIterator = fileNames.constBegin(); constIterator != fileNames.constEnd(); ++constIterator )
596 597 598 599 600
    {
        boost::shared_ptr< WProjectFile > proj = boost::shared_ptr< WProjectFile >(
                new WProjectFile( ( *constIterator ).toStdString() )
        );

601 602 603
        try
        {
            // This call is synchronous.
Alexander Wiebel's avatar
Alexander Wiebel committed
604
            if( writer.empty() )
605 606 607 608 609 610 611
            {
                proj->save();
            }
            else
            {
                proj->save( writer );
            }
612 613 614 615 616 617 618 619
        }
        catch( const std::exception& e )
        {
            QString title = "Problem while saving project file.";
            QString message = "<b>Problem while saving project file.</b><br/><br/><b>File:  </b>" + ( *constIterator ) +
                              "<br/><b>Message:  </b>" + QString::fromStdString( e.what() );
            QMessageBox::critical( this, title, message );
        }
620 621 622
    }
}

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
void WMainWindow::projectSaveAll()
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    // an empty list equals "all"
    projectSave( w );
}

void WMainWindow::projectSaveCameraOnly()
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    w.push_back( WProjectFile::getCameraWriter() );
    projectSave( w );
}

void WMainWindow::projectSaveROIOnly()
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    w.push_back( WProjectFile::getROIWriter() );
    projectSave( w );
}

644 645 646 647 648 649 650
void WMainWindow::projectSaveModuleOnly()
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    w.push_back( WProjectFile::getModuleWriter() );
    projectSave( w );
}

651 652 653 654 655 656
void WMainWindow::projectLoad()
{
    QFileDialog fd;
    fd.setFileMode( QFileDialog::ExistingFiles );

    QStringList filters;
657
    filters << "Simple Project File (*.owproj)"
658 659 660 661
            << "Any files (*)";
    fd.setNameFilters( filters );
    fd.setViewMode( QFileDialog::Detail );
    QStringList fileNames;
Alexander Wiebel's avatar
Alexander Wiebel committed
662
    if( fd.exec() )
663 664 665 666 667
    {
        fileNames = fd.selectedFiles();
    }

    QStringList::const_iterator constIterator;
Alexander Wiebel's avatar
Alexander Wiebel committed
668
    for( constIterator = fileNames.constBegin(); constIterator != fileNames.constEnd(); ++constIterator )
669
    {
670 671
        boost::shared_ptr< WProjectFile > proj = boost::shared_ptr< WProjectFile >(
                new WProjectFile( ( *constIterator ).toStdString() )
672
        );
673 674

        // This call is asynchronous. It parses the file and the starts a thread to actually do all the stuff
675
        proj->load();
676 677 678
    }
}

679
void WMainWindow::openLoadDialog()
680
{
681 682
    QFileDialog fd;
    fd.setFileMode( QFileDialog::ExistingFiles );
wiebel's avatar
wiebel committed
683 684

    QStringList filters;
685 686
    filters << "Known file types (*.cnt *.edf *.asc *.nii *.nii.gz *.fib)"
            << "EEG files (*.cnt *.edf *.asc)"
wiebel's avatar
wiebel committed
687
            << "NIfTI (*.nii *.nii.gz)"
688 689
            << "Fibers (*.fib)"
            << "Any files (*)";
wiebel's avatar
wiebel committed
690
    fd.setNameFilters( filters );
691 692
    fd.setViewMode( QFileDialog::Detail );
    QStringList fileNames;
Alexander Wiebel's avatar
Alexander Wiebel committed
693
    if( fd.exec() )
694 695 696 697
    {
        fileNames = fd.selectedFiles();
    }

698
    std::vector< std::string > stdFileNames;
699 700

    QStringList::const_iterator constIterator;
Alexander Wiebel's avatar
Alexander Wiebel committed
701
    for( constIterator = fileNames.constBegin(); constIterator != fileNames.constEnd(); ++constIterator )
702 703 704
    {
        stdFileNames.push_back( ( *constIterator ).toLocal8Bit().constData() );
    }
705 706

    //
Alexander Wiebel's avatar
Alexander Wiebel committed
707
    // WE KNOW THAT THIS IS KIND OF A HACK. It is only provided to prevent naive users from having trouble.
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    //
    bool allowOnlyOneFiberDataSet = false;
    bool doubleFibersFound = false; // have we detected the multiple loading of fibers?
    if( WPreferences::getPreference( "general.allowOnlyOneFiberDataSet", &allowOnlyOneFiberDataSet ) && allowOnlyOneFiberDataSet )
    {
        for( std::vector< std::string >::iterator it = stdFileNames.begin(); it != stdFileNames.end(); ++it )
        {
            using wiotools::getSuffix;
            std::string suffix = getSuffix( *it );
            bool isFib = ( suffix == ".fib" );
            if( m_fibLoaded && isFib )
            {
                QCoreApplication::postEvent( this, new WModuleCrashEvent(
                                                 WModuleFactory::getModuleFactory()->getPrototypeByName( "Data Module" ),
                                                 std::string( "Tried to load two fiber data sets. This is not allowed by your preferences." ) ) );
                doubleFibersFound = true;
            }
            m_fibLoaded |= isFib;
        }
    }

    if( !doubleFibersFound )
    {
        m_loaderSignal( stdFileNames );
    }
733

ledig's avatar
[STYLE]  
ledig committed
734
    // walkaround that a button keeps his down state after invoking a dialog
735
    m_loadButton->setDown( false );
736
}
737

738 739 740 741
void WMainWindow::openAboutDialog()
{
    QMessageBox::about( this, "About OpenWalnut",
                        "OpenWalnut ( http://www.openwalnut.org )\n\n"
742
                        "Copyright 2009-2010 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS. "
743 744 745 746 747 748 749 750 751 752
                        "For more information see http://www.openwalnut.org/copying.\n\n"
                        "This program comes with ABSOLUTELY NO WARRANTY. "
                        "This is free software, and you are welcome to redistribute it "
                        "under the terms of the GNU Lesser General Public License. "
                        "You should have received a copy of the GNU Lesser General Public License "
                        "along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.\n"
                        "\n"
                        "Thank you for using OpenWalnut." );
}

753 754 755 756
void WMainWindow::setPresetViewLeft()
{
    boost::shared_ptr< WGEViewer > viewer;
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
schurade's avatar
schurade committed
757 758 759
    osg::ref_ptr<osgGA::TrackballManipulator>  cm = osg::dynamic_pointer_cast<osgGA::TrackballManipulator>( viewer->getCameraManipulator() );
    osg::Quat q( 0.5, -0.5, -0.5, 0.5 );
    cm->setRotation( q );
760 761 762 763 764 765
}

void WMainWindow::setPresetViewRight()
{
    boost::shared_ptr< WGEViewer > viewer;
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
schurade's avatar
schurade committed
766 767 768
    osg::ref_ptr<osgGA::TrackballManipulator>  cm = osg::dynamic_pointer_cast<osgGA::TrackballManipulator>( viewer->getCameraManipulator() );
    osg::Quat q( -0.5, -0.5, -0.5, -0.5 );
    cm->setRotation( q );
769 770 771 772 773 774
}

void WMainWindow::setPresetViewSuperior()
{
    boost::shared_ptr< WGEViewer > viewer;
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
schurade's avatar
schurade committed
775 776 777
    osg::ref_ptr<osgGA::TrackballManipulator>  cm = osg::dynamic_pointer_cast<osgGA::TrackballManipulator>( viewer->getCameraManipulator() );
    osg::Quat q( 0., 0., 0., 1 );
    cm->setRotation( q );
778 779 780 781 782 783
}

void WMainWindow::setPresetViewInferior()
{
    boost::shared_ptr< WGEViewer > viewer;
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
schurade's avatar
schurade committed
784 785 786
    osg::ref_ptr<osgGA::TrackballManipulator>  cm = osg::dynamic_pointer_cast<osgGA::TrackballManipulator>( viewer->getCameraManipulator() );
    osg::Quat q( 0., -1., 0., 0. );
    cm->setRotation( q );
787 788 789 790 791 792
}

void WMainWindow::setPresetViewAnterior()
{
    boost::shared_ptr< WGEViewer > viewer;
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
schurade's avatar
schurade committed
793 794 795
    osg::ref_ptr<osgGA::TrackballManipulator>  cm = osg::dynamic_pointer_cast<osgGA::TrackballManipulator>( viewer->getCameraManipulator() );
    osg::Quat q( 0., -0.707107, -0.707107, 0. );
    cm->setRotation( q );
796 797 798 799 800 801
}

void WMainWindow::setPresetViewPosterior()
{
    boost::shared_ptr< WGEViewer > viewer;
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
schurade's avatar
schurade committed
802 803 804
    osg::ref_ptr<osgGA::TrackballManipulator>  cm = osg::dynamic_pointer_cast<osgGA::TrackballManipulator>( viewer->getCameraManipulator() );
    osg::Quat q( 0.707107, 0., 0., 0.707107 );
    cm->setRotation( q );
805 806
}

807 808 809 810 811 812 813
void WMainWindow::openNotImplementedDialog()
{
    QMessageBox::information( this, "Not yet implemented!",
                              "This functionality is planned for future versions of OpenWalnut. "
                              "It is not yet implemented." );
}

schurade's avatar
schurade committed
814
boost::signals2::signal1< void, std::vector< std::string > >* WMainWindow::getLoaderSignal()
815 816 817 818
{
    return &m_loaderSignal;
}

819 820 821
WIconManager*  WMainWindow::getIconManager()
{
    return &m_iconManager;
822
}
823 824 825 826 827 828 829 830

void WMainWindow::closeEvent( QCloseEvent* e )
{
    // use some "Really Close?" Dialog here
    bool reallyClose = true;


    // handle close event
Alexander Wiebel's avatar
Alexander Wiebel committed
831
    if( reallyClose )
832
    {
833 834
        // signal everybody to shut down properly.
        WKernel::getRunningKernel()->finalize();
835 836 837 838

        // now nobody acesses the osg anymore
        // clean up gl widgets
        m_mainGLWidget->close();
839 840 841 842 843