WMainWindow.cpp 53.4 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
#include <boost/thread.hpp>
31
#include <boost/regex.hpp>
32
#include <boost/filesystem.hpp>
33

34
#include <QtGui/QApplication>
35
#include <QtGui/QCloseEvent>
wiebel's avatar
wiebel committed
36
#include <QtGui/QDockWidget>
37
#include <QtGui/QFileDialog>
38
#include <QtGui/QIcon>
Mathias Goldau's avatar
Mathias Goldau committed
39 40
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
41
#include <QtGui/QMessageBox>
42
#include <QtGui/QTextEdit>
43
#include <QtGui/QShortcut>
Mathias Goldau's avatar
Mathias Goldau committed
44
#include <QtGui/QSlider>
45
#include <QtGui/QVBoxLayout>
46
#include <QtGui/QWidget>
47
#include <QtCore/QSettings>
48
#include <QtCore/QUrl>
49
#include <QtGui/QInputDialog>
50

51 52 53 54
#ifndef QT4GUI_NOWEBKIT
    #include <QtWebKit/QWebView>
#endif

55
#include "core/WVersion.h"   // NOTE: this file is auto-generated by CMAKE
56

57 58 59
#include "../icons/WIcons.xpm"
#include "controlPanel/WPropertyBoolWidget.h"
#include "controlPanel/WQtControlPanel.h"
60 61 62
#include "core/common/WColor.h"
#include "core/common/WIOTools.h"
#include "core/common/WPathHelper.h"
63
#include "core/common/WProjectFileIO.h"
64 65 66 67 68
#include "core/dataHandler/WDataSetFibers.h"
#include "core/dataHandler/WDataSetSingle.h"
#include "core/dataHandler/WEEG2.h"
#include "core/graphicsEngine/WGEZoomTrackballManipulator.h"
#include "core/graphicsEngine/WROIBox.h"
69
#include "core/kernel/WDataModule.h"
70 71 72 73 74 75 76
#include "core/kernel/WKernel.h"
#include "core/kernel/WModule.h"
#include "core/kernel/WModuleCombiner.h"
#include "core/kernel/WModuleCombinerTypes.h"
#include "core/kernel/WProjectFile.h"
#include "core/kernel/WROIManager.h"
#include "core/kernel/WSelectionManager.h"
Mathias Goldau's avatar
Mathias Goldau committed
77 78 79
#include "events/WEventTypes.h"
#include "events/WModuleCrashEvent.h"
#include "events/WModuleReadyEvent.h"
80
#include "events/WModuleRemovedEvent.h"
81
#include "events/WOpenCustomDockWidgetEvent.h"
82
#include "events/WCloseCustomDockWidgetEvent.h"
83
#include "events/WLoadFinishedEvent.h"
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
84
#include "guiElements/WQtPropertyBoolAction.h"
85
#include "WQt4Gui.h"
86
#include "WQtCombinerToolbar.h"
Mathias Goldau's avatar
Mathias Goldau committed
87
#include "WQtCustomDockWidget.h"
88
#include "WQtGLDockWidget.h"
89
#include "WQtNavGLWidget.h"
90
#include "WSettingAction.h"
91
#include "WSettingMenu.h"
92
#include "WQtMessageDialog.h"
93
#include "WQtGLScreenCapture.h"
Mathias Goldau's avatar
Mathias Goldau committed
94 95

#include "WMainWindow.h"
96
#include "WMainWindow.moc"
97

98
WMainWindow::WMainWindow( QSplashScreen* splash ):
99
    QMainWindow(),
100
    m_splash( splash ),
101
    m_currentCompatiblesToolbar( NULL ),
102
    m_iconManager()
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
103
{
104
    setAcceptDrops( true ); // enable drag and drop events
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
105
}
106

107 108 109 110 111
WMainWindow::~WMainWindow()
{
    // cleanup
}

112
void WMainWindow::setupGUI()
113
{
114 115
    wlog::info( "WMainWindow" ) << "Setting up GUI";

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Setting setup
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    WSettingAction* hideMenuAction = new WSettingAction( this, "qt4gui/showMenu",
                                                               "Show Menubar",
                                                               "Allows you to hide the menu. Can be restored using CTRL-M.",
                                                               true,
                                                               false,
                                                               QKeySequence( Qt::CTRL + Qt::Key_M ) );

    WSettingAction* showNavWidgets = new WSettingAction( this, "qt4gui/showNavigationWidgets",
                                                               "Show Navigation Views",
                                                               "Disables the navigation views completely. This can lead to a speed-up and is "
                                                               "recommended for those who do not need them.",
                                                               true,
                                                               true    // this requires a restart
                                                       );
    m_autoDisplaySetting = new WSettingAction( this, "qt4gui/useAutoDisplay",
                                                     "Auto-Display",
                                                     "If enabled, the best matching module is automatically added if some data was loaded.",
                                                     true );

    WSettingAction* mtViews = new WSettingAction( this, "qt4gui/ge/multiThreadedViewer",
Alexander Wiebel's avatar
Alexander Wiebel committed
140
                                                        "Multi-Threaded Views",
141
                                                        "If enabled, the graphic windows are rendered in different threads. This can speed-up "
142
                                                        "rendering on machines with multiple cores. WARNING: can lead to crashes sometimes.",
143 144 145 146 147 148
                                                        false,
                                                        true // require restart
                                                );
    // NOTE: the multi-threading feature needs to be activated BEFORE the first viewer is created. To ensure this we do it here.
    WGraphicsEngine::getGraphicsEngine()->setMultiThreadedViews( mtViews->get() );

149 150 151 152 153 154 155 156 157
    // set the log-level setting.
    // NOTE: see WQt4Gui which reads the setting.
    QList< QString > logOptions;
    logOptions.push_back( "Debug" );
    logOptions.push_back( "Info" );
    logOptions.push_back( "Warning" );
    logOptions.push_back( "Error" );
    WSettingMenu* logLevels = new WSettingMenu( this, "qt4gui/logLevel",
                                                      "Log-Level",
158
                                                      "Allows one to set the log verbosity.",
159 160 161 162 163
                                                      1,    // info is the default
                                                      logOptions
                                              );
    connect( logLevels, SIGNAL( change( unsigned int ) ), this, SLOT( handleLogLevelUpdate( unsigned int ) ) );

164 165 166 167
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // GUI setup
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

168
    m_iconManager.addIcon( std::string( "load" ), fileopen_xpm );
169
    m_iconManager.addIcon( std::string( "loadProject" ), projOpen_xpm );
170
    m_iconManager.addIcon( std::string( "saveProject" ), projSave_xpm );
171
    m_iconManager.addIcon( std::string( "help" ), question_xpm );
172
    m_iconManager.addIcon( std::string( "quit" ), quit_xpm );
173 174 175
    m_iconManager.addIcon( std::string( "moduleBusy" ), moduleBusy_xpm );
    m_iconManager.addIcon( std::string( "moduleCrashed" ), moduleCrashed_xpm );
    m_iconManager.addIcon( std::string( "remove" ), remove_xpm );
176
    m_iconManager.addIcon( std::string( "config" ), preferences_system_xpm );
177
    m_iconManager.addIcon( std::string( "view" ), camera_xpm );
178
    m_iconManager.addIcon( std::string( "missingModule" ), QuestionMarks_xpm );
179 180
    m_iconManager.addIcon( std::string( "none" ), empty_xpm );
    m_iconManager.addIcon( std::string( "DefaultModuleIcon" ), moduleDefault_xpm );
181 182 183
    m_iconManager.addIcon( std::string( "video" ), video_xpm );
    m_iconManager.addIcon( std::string( "image" ), image_xpm );
    m_iconManager.addIcon( std::string( "preferences" ), preferences_xpm );
184

185 186 187 188 189 190 191 192 193 194
    try
    {
        m_iconManager.addIcon( std::string( "logo" ), WPathHelper::getPathHelper()->getSharePath() / ".." / "pixmaps" / "openwalnut.png" );
    }
    catch( const WException& e )
    {
        // ignore this. add a default pixmap
        m_iconManager.addIcon( std::string( "logo" ), QPixmap() );
    }

195
    if( objectName().isEmpty() )
196
    {
197
        setObjectName( QString::fromUtf8( "MainWindow" ) );
198
    }
199 200 201

    // NOTE: this only is an initial size. The state reloaded from QSettings will set it to the value the user had last session.
    resize( 800, 600 );
202
    setWindowIcon( m_iconManager.getIcon( "logo" ) );
203 204
    std::string windowHeading =  std::string( "OpenWalnut " ) + std::string( W_VERSION );
    setWindowTitle( QApplication::translate( "MainWindow", windowHeading.c_str(), 0, QApplication::UnicodeUTF8 ) );
205

206 207
    setDockOptions( QMainWindow::AnimatedDocks |  QMainWindow::AllowNestedDocks | QMainWindow::AllowTabbedDocks );

208
    //network Editor
209
    m_networkEditor = NULL;
210 211
    m_networkEditor = new WQtNetworkEditor( this );
    m_networkEditor->setFeatures( QDockWidget::AllDockWidgetFeatures );
212

213 214 215
    // strangely, the QGraphics* objects do not properly forward drag/drop events. We need to explicitly handle them.
    connect( m_networkEditor->getView(), SIGNAL( dragDrop( QDropEvent* ) ),
             this, SLOT( handleDrop( QDropEvent* ) ) );
216

217 218 219 220
    // the control panel instance is needed for the menu
    m_controlPanel = new WQtControlPanel( this );
    m_controlPanel->setFeatures( QDockWidget::AllDockWidgetFeatures );
    m_controlPanel->addSubject( "Default Subject" );
221

222 223
    // add all docks
    addDockWidget( Qt::RightDockWidgetArea, m_controlPanel->getModuleDock() );
224 225 226 227
    if( m_networkEditor )
    {
        addDockWidget( Qt::RightDockWidgetArea, m_networkEditor );
    }
228

229
    addDockWidget( Qt::RightDockWidgetArea, m_controlPanel->getColormapperDock() );
230
    addDockWidget( Qt::RightDockWidgetArea, m_controlPanel->getRoiDock() );
231 232

    // tabify those panels by default
233 234 235 236
    if( m_networkEditor )
    {
        tabifyDockWidget( m_networkEditor, m_controlPanel->getModuleDock() );
    }
237
    tabifyDockWidget( m_controlPanel->getModuleDock(), m_controlPanel->getColormapperDock() );
238
    tabifyDockWidget( m_controlPanel->getColormapperDock(), m_controlPanel->getRoiDock() );
239

240
    m_glDock = new QMainWindow();
241
    m_glDock->setObjectName( "GLDock" );
242 243 244
    m_glDock->setDockOptions( QMainWindow::AnimatedDocks |  QMainWindow::AllowNestedDocks | QMainWindow::AllowTabbedDocks );
    m_glDock->setDocumentMode( true );
    setCentralWidget( m_glDock );
245
    WQtGLDockWidget* mainGLDock = new WQtGLDockWidget( "Main View", "3D View", m_glDock );
246 247
    mainGLDock->getGLWidget()->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
    m_mainGLWidget = mainGLDock->getGLWidget();
248
    m_mainGLWidgetScreenCapture = m_mainGLWidget->getScreenCapture( this );
249
    m_glDock->addDockWidget( Qt::RightDockWidgetArea, mainGLDock );
250
    addDockWidget( Qt::RightDockWidgetArea, m_mainGLWidgetScreenCapture );
251
    tabifyDockWidget( m_controlPanel->getRoiDock(), m_mainGLWidgetScreenCapture );
252
    connect( m_mainGLWidget.get(), SIGNAL( renderedFirstFrame() ), this, SLOT( handleGLVendor() ) );
253

254 255 256
    addDockWidget( Qt::RightDockWidgetArea, m_controlPanel );

    // by default, the module editor should be in front
257 258 259 260 261 262 263 264
    if( m_networkEditor )
    {
        m_networkEditor->raise();
    }
    else
    {
        m_controlPanel->getModuleDock()->raise();
    }
265

266 267
    // NOTE: we abuse the gl widgets first frame event to handle startup news.
    connect( m_mainGLWidget.get(), SIGNAL( renderedFirstFrame() ), this, SLOT( handleStartMessages() ) );
268
    connect( m_mainGLWidget.get(), SIGNAL( renderedFirstFrame() ), this, SLOT( closeSplash() ) );
269

270
    m_permanentToolBar = new WQtToolBar( "Standard Toolbar", this );
271
    addToolBar( Qt::TopToolBarArea, m_permanentToolBar );
272 273 274 275 276 277 278

    m_iconManager.addIcon( std::string( "ROI icon" ), box_xpm );
    m_iconManager.addIcon( std::string( "Reset icon" ), o_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 );

279
    m_loadButton = new QAction( m_iconManager.getIcon( "load" ), "Load Dataset or Project", m_permanentToolBar );
280
    m_loadButton->setShortcut( QKeySequence(  QKeySequence::Open ) );
281 282 283
    QAction* roiButton = new QAction( m_iconManager.getIcon( "ROI icon" ), "ROI", m_permanentToolBar );
    QAction* resetButton = new QAction( m_iconManager.getIcon( "view" ), "Reset", m_permanentToolBar );
    resetButton->setShortcut( QKeySequence( Qt::Key_Escape ) );
284
    m_saveAction = new QAction( m_iconManager.getIcon( "saveProject" ), "Save Project", m_permanentToolBar );
285 286 287 288

    connect( m_loadButton, SIGNAL(  triggered( bool ) ), this, SLOT( openLoadDialog() ) );
    connect( resetButton, SIGNAL(  triggered( bool ) ), m_mainGLWidget.get(), SLOT( reset() ) );
    connect( roiButton, SIGNAL(  triggered( bool ) ), this, SLOT( newRoi() ) );
289
    connect( m_saveAction, SIGNAL( triggered( bool ) ), this, SLOT( projectSaveAll() ) );
290

291
    m_loadButton->setToolTip( "Load a dataset or project from file" );
292
    resetButton->setToolTip( "Reset main view" );
293
    roiButton->setToolTip( "Create new ROI" );
294
    m_saveAction->setToolTip( "Save current project to file" );
295 296 297

    // 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.
298
    m_controlPanel->selectUpperMostEntry();
299

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
300 301 302 303
    // 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.

304
    m_menuBar = new QMenuBar( this );
305 306

    // hide menu?
307 308 309
    m_menuBar->setVisible( hideMenuAction->get() );
    connect( hideMenuAction, SIGNAL( change( bool ) ), m_menuBar, SLOT( setVisible( bool ) ) );
    addAction( hideMenuAction );
310

311
    QMenu* fileMenu = m_menuBar->addMenu( "File" );
312

313
    fileMenu->addAction( m_loadButton );
314 315 316 317 318 319
    m_saveMenu = fileMenu->addMenu( m_iconManager.getIcon( "saveProject" ), "Save Project" );
    m_saveMenu->addAction( "Save Project", this, SLOT( projectSaveAll() ), QKeySequence::Save );
    m_saveMenu->addAction( "Save Modules Only", this, SLOT( projectSaveModuleOnly() ) );
    m_saveMenu->addAction( "Save Camera Only", this, SLOT( projectSaveCameraOnly() ) );
    // saveMenu->addAction( "Save ROIs Only", this, SLOT( projectSaveROIOnly() ) );
    m_saveAction->setMenu( m_saveMenu );
320

321
    fileMenu->addSeparator();
322 323
    // 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 ) );
324
    m_quitAction = fileMenu->addAction( m_iconManager.getIcon( "quit" ), "Quit", this, SLOT( close() ),  QKeySequence( Qt::CTRL + Qt::Key_Q ) );
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
325

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
326
    // This QAction stuff is quite ugly and complicated some times ... There is no nice constructor which takes name, slot keysequence and so on
327
    // directly -> set shortcuts, and some further properties using QAction's interface
328 329 330 331 332 333 334 335
    m_viewAction = new QAction( "View", this );
    m_viewMenu = m_menuBar->addMenu( "View" );
    m_viewMenu->addAction( hideMenuAction );
    m_viewMenu->addSeparator();
    m_viewMenu->addAction( showNavWidgets );
    m_viewMenu->addSeparator();
    m_viewMenu->addMenu( m_permanentToolBar->getStyleMenu() );
    m_viewAction->setMenu( m_viewMenu );
336 337

    // Camera menu
338 339 340
    QMenu* bgColorMenu = new QMenu( "Background Colors" );
    bgColorMenu->addAction( mainGLDock->getGLWidget()->getBackgroundColorAction() );

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    m_cameraAction = new QAction( "Camera", this );
    m_cameraMenu = m_menuBar->addMenu( "Camera" );
    m_cameraMenu->addAction( mainGLDock->getGLWidget()->getThrowingSetting() );
    m_cameraMenu->addMenu( bgColorMenu );
    m_cameraMenu->addSeparator();
    m_cameraAction->setMenu( m_cameraMenu );

    m_settingsAction = new QAction( "Settings", this );
    m_settingsMenu = m_menuBar->addMenu( "Settings" );
    m_settingsMenu->addAction( m_autoDisplaySetting );
    m_settingsMenu->addAction( m_controlPanel->getModuleConfig().getConfigureAction() );
    m_settingsMenu->addSeparator();
    m_settingsMenu->addAction( mtViews );
    m_settingsMenu->addSeparator();
    m_settingsMenu->addMenu( logLevels );
    m_settingsAction->setMenu( m_settingsMenu );
357 358

    // a separate menu for some presets
359
    QMenu* cameraPresetMenu = m_cameraMenu->addMenu( "Presets" );
360

361 362 363 364 365
    QAction* controlPanelTrigger = m_controlPanel->toggleViewAction();
    QList< QKeySequence > controlPanelShortcut;
    controlPanelShortcut.append( QKeySequence( Qt::Key_F9 ) );
    controlPanelTrigger->setShortcuts( controlPanelShortcut );
    this->addAction( controlPanelTrigger );  // this enables the action even if the menu bar is invisible
366

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
367 368 369
    // 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 ).
370
    // By additionally adding the action to the main window, we ensure the action can be triggered even if the menu bar is hidden.
371
    QAction* tmpAction = cameraPresetMenu->addAction( m_iconManager.getIcon( "sagittal icon" ), "Left", this, SLOT( setPresetViewLeft() ),
372
                                             QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_L ) );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
373
    tmpAction->setIconVisibleInMenu( true );
374 375
    this->addAction( tmpAction );

376 377
    tmpAction = cameraPresetMenu->addAction( m_iconManager.getIcon( "sagittal icon" ), "Right", this, SLOT( setPresetViewRight() ),
                                       QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_R ) );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
378
    tmpAction->setIconVisibleInMenu( true );
379 380
    this->addAction( tmpAction );

381 382
    tmpAction = cameraPresetMenu->addAction( m_iconManager.getIcon( "axial icon" ), "Superior", this, SLOT( setPresetViewSuperior() ),
                                       QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_S ) );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
383
    tmpAction->setIconVisibleInMenu( true );
384 385
    this->addAction( tmpAction );

386 387
    tmpAction = cameraPresetMenu->addAction( m_iconManager.getIcon( "axial icon" ), "Inferior", this, SLOT( setPresetViewInferior() ),
                                       QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_I ) );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
388
    tmpAction->setIconVisibleInMenu( true );
389 390
    this->addAction( tmpAction );

391 392
    tmpAction = cameraPresetMenu->addAction( m_iconManager.getIcon( "coronal icon" ), "Anterior", this, SLOT( setPresetViewAnterior() ),
                                       QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_A ) );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
393
    tmpAction->setIconVisibleInMenu( true );
394 395
    this->addAction( tmpAction );

396 397
    tmpAction = cameraPresetMenu->addAction( m_iconManager.getIcon( "coronal icon" ), "Posterior", this, SLOT( setPresetViewPosterior() ),
                                       QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_P ) );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
398
    tmpAction->setIconVisibleInMenu( true );
399 400
    this->addAction( tmpAction );

401
    resetButton->setMenu( cameraPresetMenu );
402

403 404 405 406 407 408 409 410 411
    m_helpAction = new QAction( "Help", this );
    m_helpMenu = m_menuBar->addMenu( "Help" );
    m_helpMenu->addAction( m_iconManager.getIcon( "help" ), "OpenWalnut Help", this, SLOT( openOpenWalnutHelpDialog() ),
                           QKeySequence( QKeySequence::HelpContents ) );
    m_helpMenu->addAction( m_iconManager.getIcon( "logo" ), "Welcome to OpenWalnut", this, SLOT( showWelcomeDialog() ) );
    m_helpMenu->addSeparator();
    m_helpMenu->addAction( m_iconManager.getIcon( "logo" ), "About OpenWalnut", this, SLOT( openAboutDialog() ) );
    m_helpMenu->addAction( "About Qt", this, SLOT( openAboutQtDialog() ) );
    m_helpAction->setMenu( m_helpMenu );
412

413 414
    setMenuBar( m_menuBar );

415 416
    // initially 3 navigation views
    {
417
        if( showNavWidgets->get() )
418
        {
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
419
            m_navAxial = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "Axial View", "Axial View", this, "Axial Slice",
420
                                                                                  m_mainGLWidget.get() ) );
421
            m_navAxial->setFeatures( QDockWidget::AllDockWidgetFeatures );
422
            m_navAxial->setSliderProperty( WKernel::getRunningKernel()->getSelectionManager()->getPropAxialPos() );
423
            m_navAxial->getGLWidget()->setCameraManipulator( WQtGLWidget::NO_OP );
424

425
            m_glDock->addDockWidget( Qt::LeftDockWidgetArea, m_navAxial.get() );
426

427 428
            m_navCoronal = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "Coronal View", "Coronal View", this, "Coronal Slice",
                                                                                    m_mainGLWidget.get() ) );
429
            m_navCoronal->setFeatures( QDockWidget::AllDockWidgetFeatures );
430
            m_navCoronal->setSliderProperty( WKernel::getRunningKernel()->getSelectionManager()->getPropCoronalPos() );
431
            m_navCoronal->getGLWidget()->setCameraManipulator( WQtGLWidget::NO_OP );
432

433
            m_glDock->addDockWidget( Qt::LeftDockWidgetArea, m_navCoronal.get() );
434

ledig's avatar
ledig committed
435
            m_navSagittal =
436 437
                boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "Sagittal View", "Sagittal View", this, "Sagittal Slice",
                                                                         m_mainGLWidget.get() ) );
438
            m_navSagittal->setFeatures( QDockWidget::AllDockWidgetFeatures );
439
            m_navSagittal->setSliderProperty( WKernel::getRunningKernel()->getSelectionManager()->getPropSagittalPos() );
440
            m_navSagittal->getGLWidget()->setCameraManipulator( WQtGLWidget::NO_OP );
441

442
            m_glDock->addDockWidget( Qt::LeftDockWidgetArea, m_navSagittal.get() );
443

444 445 446
            bgColorMenu->addAction( m_navAxial->getGLWidget()->getBackgroundColorAction() );
            bgColorMenu->addAction( m_navCoronal->getGLWidget()->getBackgroundColorAction() );
            bgColorMenu->addAction( m_navSagittal->getGLWidget()->getBackgroundColorAction() );
447 448 449
        }
    }

450 451 452
    // create the show/hide actions using the selection manager's props
    WQtPropertyBoolAction* showAxial = new WQtPropertyBoolAction( WKernel::getRunningKernel()->getSelectionManager()->getPropAxialShow(),
                                                                  m_permanentToolBar );
453
    showAxial->setToolTip( "Toggle axial slice" );
454 455 456 457 458
    showAxial->setText( "Toggle Axial Slice" );
    showAxial->setIcon( m_iconManager.getIcon( "axial icon" ) );

    WQtPropertyBoolAction* showCoronal = new WQtPropertyBoolAction( WKernel::getRunningKernel()->getSelectionManager()->getPropCoronalShow(),
                                                                    m_permanentToolBar );
459
    showCoronal->setToolTip( "Toggle coronal slice" );
460 461 462 463 464
    showCoronal->setText( "Toggle Coronal Slice" );
    showCoronal->setIcon( m_iconManager.getIcon( "coronal icon" ) );

    WQtPropertyBoolAction* showSagittal = new WQtPropertyBoolAction( WKernel::getRunningKernel()->getSelectionManager()->getPropSagittalShow(),
                                                                     m_permanentToolBar );
465
    showSagittal->setToolTip( "Toggle sagittal slice" );
466 467 468
    showSagittal->setText( "Toggle Sagittal Slice" );
    showSagittal->setIcon( m_iconManager.getIcon( "sagittal icon" ) );

469
    // setup permanent toolbar
470
    m_permanentToolBar->addAction( m_loadButton );
471
    m_permanentToolBar->addAction( m_saveAction );
472
    m_permanentToolBar->addSeparator();
473 474
    m_permanentToolBar->addAction( m_mainGLWidgetScreenCapture->getScreenshotTrigger() );
    m_permanentToolBar->addSeparator();
475 476
    m_permanentToolBar->addAction( resetButton );
    m_permanentToolBar->addAction( roiButton );
477
    m_permanentToolBar->addSeparator();
478 479 480 481
    m_permanentToolBar->addAction( showAxial );
    m_permanentToolBar->addAction( showCoronal );
    m_permanentToolBar->addAction( showSagittal );
    m_permanentToolBar->addSeparator();
482

483 484 485
    // allow the control panel to complete setup
    m_controlPanel->completeGuiSetup();

486 487
    // after creating the GUI, restore its saved state
    restoreSavedState();
488
}
489

490
void WMainWindow::autoAdd( boost::shared_ptr< WModule > module, std::string proto, bool onlyOnce )
491
{
492 493 494 495 496 497
    // if only one module should be added, and there already is one --- skip.
    if( onlyOnce && !WKernel::getRunningKernel()->getRootContainer()->getModules( proto ).empty() )
    {
        return;
    }

498
    // get the prototype.
Alexander Wiebel's avatar
Alexander Wiebel committed
499
    if( !WKernel::getRunningKernel()->getRootContainer()->applyModule( module, proto, true ) )
500 501 502 503 504 505
    {
        WLogger::getLogger()->addLogMessage( "Auto Display active but module " + proto + " could not be added.",
                                             "GUI", LL_ERROR );
    }
}

506
void WMainWindow::moduleSpecificCleanup( boost::shared_ptr< WModule > /* module */ )
507
{
508
    // called for each removed module. Use this to undo modifications done due to added modules (moduleSpecificSetup)
509 510
}

511 512
void WMainWindow::moduleSpecificSetup( boost::shared_ptr< WModule > module )
{
513 514 515 516
    // 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.
517
    bool useAutoDisplay = m_autoDisplaySetting->get();
Alexander Wiebel's avatar
Alexander Wiebel committed
518
    if( useAutoDisplay && module->getType() == MODULE_DATA )
519 520 521 522 523 524
    {
        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.
525
        boost::shared_ptr< WDataModule > dataModule = boost::shared_static_cast< WDataModule >( module );
526 527

        // grab data and identify type
Alexander Wiebel's avatar
Alexander Wiebel committed
528
        if( dataModule->getDataSet()->isA< WDataSetSingle >() && dataModule->getDataSet()->isTexture() )
529 530 531
        {
            // it is a dataset single
            // load a nav slice module if a WDataSetSingle is available!?
532
            autoAdd( module, "Navigation Slices", true );
533
        }
Alexander Wiebel's avatar
Alexander Wiebel committed
534
        else if( dataModule->getDataSet()->isA< WDataSetFibers >() )
535 536
        {
            // it is a fiber dataset -> add the FiberDisplay module
537
            autoAdd( module, "Fiber Display" );
538
        }
Alexander Wiebel's avatar
Alexander Wiebel committed
539
        else if( dataModule->getDataSet()->isA< WEEG2 >() )
540
        {
541
            // it is a eeg dataset -> add the eegView module
542 543 544
            autoAdd( module, "EEG View" );
        }
    }
wiebel's avatar
wiebel committed
545
}
546

547 548
void WMainWindow::setCompatiblesToolbar( WQtCombinerToolbar* toolbar )
{
Alexander Wiebel's avatar
Alexander Wiebel committed
549
    if( m_currentCompatiblesToolbar )
550 551 552 553 554
    {
        delete m_currentCompatiblesToolbar;
    }
    m_currentCompatiblesToolbar = toolbar;

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

562 563 564 565 566
    // we want to keep the tool-button styles in sync
    m_currentCompatiblesToolbar->setToolButtonStyle( m_permanentToolBar->toolButtonStyle() );
    connect( m_permanentToolBar, SIGNAL( toolButtonStyleChanged( Qt::ToolButtonStyle ) ),
             m_currentCompatiblesToolbar, SLOT( setToolButtonStyle( Qt::ToolButtonStyle ) ) );

567
    // and the position of the toolbar
568
    addToolBar( Qt::TopToolBarArea, m_currentCompatiblesToolbar );
569
}
570

571 572 573 574 575
WQtCombinerToolbar* WMainWindow::getCompatiblesToolbar()
{
    return m_currentCompatiblesToolbar;
}

576
WQtControlPanel* WMainWindow::getControlPanel()
577
{
578
    return m_controlPanel;
579
}
580

581 582 583 584 585
WQtNetworkEditor* WMainWindow::getNetworkEditor()
{
    return m_networkEditor;
}

586
bool WMainWindow::projectSave( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer )
587 588 589 590
{
    QFileDialog fd;
    fd.setWindowTitle( "Save Project as" );
    fd.setFileMode( QFileDialog::AnyFile );
591
    fd.setAcceptMode( QFileDialog::AcceptSave );
592

593 594
    // My Mac OSX Lion automatically appends .owproj to the file name
    // if no extension is given.
595
    QStringList filters;
596
    filters << "Project File (*.owproj *.owp)";
597 598
    fd.setNameFilters( filters );
    fd.setViewMode( QFileDialog::Detail );
599
    QStringList filenames;
Alexander Wiebel's avatar
Alexander Wiebel committed
600
    if( fd.exec() )
601
    {
602
        filenames = fd.selectedFiles();
603
    }
604 605 606 607
    else
    {
        return false; // the user canceled, no files, so nothing saved
    }
608

609
    bool success = true;
610
    QStringList::const_iterator constIterator;
611
    for( constIterator = filenames.constBegin(); constIterator != filenames.constEnd(); ++constIterator )
612
    {
613
        std::string filename = ( *constIterator ).toStdString();
614

615
        // append owp if suffix is not present, yet
616 617
        if( filename.rfind( ".owp" ) != filename.size() - 4
         && filename.rfind( ".owproj" ) != filename.size() - 7 )
618 619 620 621
        {
            filename += ".owp";
        }

622
        boost::shared_ptr< WProjectFile > proj = boost::shared_ptr< WProjectFile >(
623
                new WProjectFile( filename )
624 625
        );

626 627 628
        try
        {
            // This call is synchronous.
Alexander Wiebel's avatar
Alexander Wiebel committed
629
            if( writer.empty() )
630 631 632 633 634 635 636
            {
                proj->save();
            }
            else
            {
                proj->save( writer );
            }
637 638 639 640 641 642 643
        }
        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 );
644
            success = false;
645
        }
646
    }
647
    return success;
648 649
}

650
bool WMainWindow::projectSaveAll()
651 652 653
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    // an empty list equals "all"
654
    return projectSave( w );
655 656
}

657
bool WMainWindow::projectSaveCameraOnly()
658 659 660
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    w.push_back( WProjectFile::getCameraWriter() );
661
    return projectSave( w );
662 663
}

664
bool WMainWindow::projectSaveROIOnly()
665 666 667
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    w.push_back( WProjectFile::getROIWriter() );
668
    return projectSave( w );
669 670
}

671
bool WMainWindow::projectSaveModuleOnly()
672 673 674
{
    std::vector< boost::shared_ptr< WProjectFileIO > > w;
    w.push_back( WProjectFile::getModuleWriter() );
675
    return projectSave( w );
676 677
}

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

    QStringList filters;
684 685
    filters << "Known file types (*.cnt *.edf *.asc *.nii *.nii.gz *.fib *.owproj *.owp)"
            << "Simple Project File (*.owproj *.owp)"
686
            << "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
    fd.setViewMode( QFileDialog::Detail );
692
    QStringList filenames;
Alexander Wiebel's avatar
Alexander Wiebel committed
693
    if( fd.exec() )
694
    {
695
        filenames = fd.selectedFiles();
696 697
    }

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

    QStringList::const_iterator constIterator;
701
    for( constIterator = filenames.constBegin(); constIterator != filenames.constEnd(); ++constIterator )
702
    {
703 704 705 706 707 708
        boost::filesystem::path fn( ( *constIterator ).toLocal8Bit().constData() );
        std::string suffix = getSuffix( fn );

        // is this a project file?
        if( ( suffix == ".owp" ) || ( suffix == ".owproj" ) )
        {
709
            asyncProjectLoad( fn.string() );
710 711 712 713 714 715
        }
        else
        {
            // this is not a project. So we assume it is a data file
            loadDataFilenames.push_back( fn.string() );
        }
716
    }
717

718
    m_loaderSignal( loadDataFilenames );
719
}
720

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
void WMainWindow::asyncProjectLoad( std::string filename )
{
    WProjectFile::SPtr proj( new WProjectFile( filename, boost::bind( &WMainWindow::slotLoadFinished, this, _1, _2 ) ) );
    proj->load();
}

void WMainWindow::slotLoadFinished( boost::filesystem::path file, std::vector< std::string > errors )
{
    // as this function might be called from outside the gui thread, use an event:
    QCoreApplication::postEvent( this, new WLoadFinishedEvent( file, errors ) );

    if( errors.size() )
    {
        wlog::warn( "MainWindow" ) << "Async load error occurred. Informing user.";
    }
}

738 739 740 741
void WMainWindow::openAboutQtDialog()
{
    QMessageBox::aboutQt( this, "About Qt" );
}
742

743 744
void WMainWindow::openAboutDialog()
{