WQt4Gui.cpp 8.37 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
7
//
wiebel's avatar
wiebel committed
8
// This file is part of OpenWalnut.
9
//
wiebel's avatar
wiebel committed
10
// OpenWalnut is free software: you can redistribute it and/or modify
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,
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/>.
22 23 24
//
//---------------------------------------------------------------------------

ebaum's avatar
ebaum committed
25
#include <iostream>
26
#include <fstream>
27 28
#include <string>
#include <vector>
29

30
#include <boost/program_options.hpp>
31
#include <boost/shared_ptr.hpp>
32

33
#include <QtGui/QApplication>
34
#include <QtGui/QFileDialog>
35
#include <QtGui/QMessageBox>
36

Mathias Goldau's avatar
Mathias Goldau committed
37
#include "WMainWindow.h" // this has to be included before any other includes
38 39
#include "../../common/WConditionOneShot.h"
#include "../../common/WIOTools.h"
40
#include "../../graphicsEngine/WGraphicsEngine.h"
Mathias Goldau's avatar
Mathias Goldau committed
41
#include "../../kernel/WKernel.h"
42
#include "../../kernel/WModuleProjectFileCombiner.h"
43
#include "WOpenCustomDockWidgetEvent.h"
Mathias Goldau's avatar
Mathias Goldau committed
44
#include "WQt4Gui.h"
45
#include "events/WModuleAssocEvent.h"
schurade's avatar
schurade committed
46
#include "events/WRoiAssocEvent.h"
47
#include "events/WModuleReadyEvent.h"
48
#include "events/WModuleCrashEvent.h"
ebaum's avatar
ebaum committed
49

Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
50 51
WQt4Gui::WQt4Gui( int argc, char** argv )
    : WGUI( argc, argv )
ebaum's avatar
ebaum committed
52 53 54
{
}

55
WQt4Gui::~WQt4Gui()
56
{
ebaum's avatar
ebaum committed
57
}
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
58

Mathias Goldau's avatar
Mathias Goldau committed
59
#ifdef _WIN32
60
// need this on windows to make it link correctly.
ledig's avatar
ledig committed
61
//const unsigned int boost::program_options::options_description::m_default_line_length = 2048;
Mathias Goldau's avatar
Mathias Goldau committed
62
#endif
ebaum's avatar
ebaum committed
63

Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
64
bool WQt4Gui::parseOptions()
65
{
Mathias Goldau's avatar
Mathias Goldau committed
66 67
    namespace po = boost::program_options; // since the namespace is far to big we use a shortcut here
    po::options_description desc( "Allowed options" );
68

ledig's avatar
ledig committed
69
//#ifndef _WIN32
70
// TODO(wiebel): this does not link on windows at the moment. But it should!
71 72
    desc.add_options()
        ( "help,h", "Prints this help message" )
73
        ( "project,p", po::value< std::string >(), "Project file to be loaded on startup." )
74 75
        ( "input,i", po::value< std::vector< std::string > >(), "Input data files that should be loaded automatically" )
        ( "timed-output,t", "Flag indicating if all log strings should have a time string preceding" );
ledig's avatar
ledig committed
76
//#endif
77 78 79 80

    po::positional_options_description p;
    p.add( "input", -1 );

81 82
    try
    {
Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
83
        po::store( po::command_line_parser( m_argc, m_argv ).options( desc ).positional( p ).run(), m_optionsMap );
84
    }
Mathias Goldau's avatar
Mathias Goldau committed
85
    catch( const po::error &e )
86
    {
Mathias Goldau's avatar
Mathias Goldau committed
87
        std::cerr << e.what() << std::endl;
88 89 90 91 92 93 94 95 96 97
        return false;
    }

    po::notify( m_optionsMap );

    // print usage information if command line asks for help.
    if( m_optionsMap.count( "help" ) )
    {
        std::cout << desc << std::endl;
    }
98 99 100 101 102
    else if( m_optionsMap.count( "timed-output" ) )
    {
        WLogger::getLogger()->setDefaultFormat( "[%t] *%l [%s] %m \n" );
        wlog::info( "GUI" ) << "Timed output enabled";
    }
103 104 105
    return true;
}

106 107 108 109 110
void WQt4Gui::moduleError( boost::shared_ptr< WModule > module, const WException& exception )
{
    QCoreApplication::postEvent( m_mainWindow, new WModuleCrashEvent( module, exception.what() ) );
}

111 112
int WQt4Gui::run()
{
Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
113
    bool parsingSuccessful = parseOptions();
114 115 116

    if( !parsingSuccessful )
    {
117 118
        return 1;
    }
119

120
    // exit as fast as possible if command line asked for help. The ,essage has been printed by parseOptions().
121 122
    if( m_optionsMap.count( "help" ) )
    {
123
        return 1;
124 125
    }

126
    WLogger::getLogger()->run();
127
    WLogger::getLogger()->addLogMessage( "Bringing up GUI", "GUI", LL_INFO );
128

Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
129
    QApplication appl( m_argc, m_argv, true );
130

131
    // startup graphics engine
132
    m_ge = WGraphicsEngine::getGraphicsEngine();
133

134 135 136
    // and startup kernel
    m_kernel = boost::shared_ptr< WKernel >( new WKernel( m_ge, shared_from_this() ) );
    m_kernel->run();
137 138 139
    t_ModuleErrorSignalHandlerType func = boost::bind( &WQt4Gui::moduleError, this, _1, _2 );
    m_kernel->getRootContainer()->addDefaultNotifier( WM_ERROR, func );

140
    // create the window
141
    m_mainWindow = new WMainWindow();
142
    m_mainWindow->show();
143

144
    // connect out loader signal with kernel
145 146 147
#ifdef _WIN32
    getLoadButtonSignal()->connect( boost::bind( &WKernel::loadDataSetsSynchronously, m_kernel, _1 ) );
#else
148
    getLoadButtonSignal()->connect( boost::bind( &WKernel::loadDataSets, m_kernel, _1 ) );
149
#endif
150

151
    m_mainWindow->getModuleButtonSignal()->connect( boost::bind( &WKernel::applyModule, m_kernel, _1, _2 ) );
152

153
    // bind the GUI's slot with the ready signal
154 155
    t_ModuleGenericSignalHandlerType assocSignal = boost::bind( &WQt4Gui::slotAddDatasetOrModuleToBrowser, this, _1 );
    m_kernel->getRootContainer()->addDefaultNotifier( WM_ASSOCIATED, assocSignal );
156 157
    t_ModuleGenericSignalHandlerType readySignal = boost::bind( &WQt4Gui::slotActivateDatasetOrModuleInBrowser, this, _1 );
    m_kernel->getRootContainer()->addDefaultNotifier( WM_READY, readySignal );
schurade's avatar
[STYLE]  
schurade committed
158
    boost::function< void( boost::shared_ptr< WRMROIRepresentation > ) > assocRoiSignal = boost::bind( &WQt4Gui::slotAddRoiToBrowser, this, _1 );
schurade's avatar
schurade committed
159
    m_kernel->getRoiManager()->addDefaultNotifier( assocRoiSignal );
160 161

    // now we are initialized
162
    m_isInitialized( true );
163

164 165 166
    // check if we want to load data due to command line and call the respective function
    if( m_optionsMap.count("input") )
    {
167
        m_kernel->loadDataSets( m_optionsMap["input"].as< std::vector< std::string > >() );
168 169
    }

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    // Load project file
    if( m_optionsMap.count("project") )
    {
        try
        {
            WModuleProjectFileCombiner proj = WModuleProjectFileCombiner( m_optionsMap["project"].as< std::string >() );
            proj.apply();
        }
        catch ( const WException& e )
        {
            wlog::error( "GUI" ) << "Project file \"" << m_optionsMap["project"].as< std::string >() << "\" could not be loaded. Message: " <<
                e.what();
        }
    }

185
    // run
186 187 188 189 190 191 192
    int qtRetCode = appl.exec();

    // signal everybody to shut down properly.
    WKernel::getRunningKernel()->wait( true );
    WKernel::getRunningKernel()->getGraphicsEngine()->wait( true );

    return qtRetCode;
193 194
}

195
void WQt4Gui::slotAddDatasetOrModuleToBrowser( boost::shared_ptr< WModule > module )
196
{
197
    // create a new event for this and insert it into event queue
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
198
    QCoreApplication::postEvent( m_mainWindow->getDatasetBrowser(), new WModuleAssocEvent( module ) );
199 200
}

schurade's avatar
schurade committed
201 202 203 204 205
void WQt4Gui::slotAddRoiToBrowser( boost::shared_ptr< WRMROIRepresentation > roi )
{
    QCoreApplication::postEvent( m_mainWindow->getDatasetBrowser(), new WRoiAssocEvent( roi ) );
}

206 207 208
void WQt4Gui::slotActivateDatasetOrModuleInBrowser( boost::shared_ptr< WModule > module )
{
    // create a new event for this and insert it into event queue
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
209
    QCoreApplication::postEvent( m_mainWindow->getDatasetBrowser(), new WModuleReadyEvent( module ) );
210
    QCoreApplication::postEvent( m_mainWindow, new WModuleReadyEvent( module ) );
211 212
}

213 214
boost::shared_ptr< WModule > WQt4Gui::getSelectedModule()
{
215
    return m_mainWindow->getDatasetBrowser()->getSelectedModule();
216 217
}

schurade's avatar
schurade committed
218
boost::signals2::signal1< void, std::vector< std::string > >* WQt4Gui::getLoadButtonSignal()
219
{
220
    return m_mainWindow->getLoaderSignal();
221 222
}

223
boost::shared_ptr< WCustomWidget > WQt4Gui::openCustomWidget( std::string title, WGECamera::ProjectionMode projectionMode,
224
    boost::shared_ptr< WCondition > shutdownCondition )
225
{
226 227 228 229
    WConditionSet conditionSet;
    conditionSet.setResetable( true, false );
    conditionSet.add( shutdownCondition );

230 231
    boost::shared_ptr< WFlag< boost::shared_ptr< WCustomWidget > > > widgetFlag(
        new WFlag< boost::shared_ptr< WCustomWidget > >( new WConditionOneShot, boost::shared_ptr< WCustomWidget >() ) );
232 233
    conditionSet.add( widgetFlag->getCondition() );

234
    QCoreApplication::postEvent( m_mainWindow, new WOpenCustomDockWidgetEvent( title, projectionMode, widgetFlag ) );
235 236 237

    conditionSet.wait();
    return widgetFlag->get();
238 239 240 241
}

void WQt4Gui::closeCustomWidget( std::string title )
{
242
    m_mainWindow->closeCustomDockWidget( title );
243
}