WQt4Gui.cpp 6.73 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 35
#include <QtGui/QFileDialog>

Mathias Goldau's avatar
Mathias Goldau committed
36
#include "WMainWindow.h" // this has to be included before any other includes
37
#include "../../graphicsEngine/WGraphicsEngine.h"
Mathias Goldau's avatar
Mathias Goldau committed
38
#include "../../kernel/WKernel.h"
39
#include "../../modules/data/WMData.h"
Mathias Goldau's avatar
Mathias Goldau committed
40
#include "../../utils/WIOTools.h"
41
#include "WCreateCustomDockWidgetEvent.h"
42
#include "../../common/WConditionOneShot.h"
Mathias Goldau's avatar
Mathias Goldau committed
43
#include "WQt4Gui.h"
ebaum's avatar
ebaum committed
44

Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
45 46
WQt4Gui::WQt4Gui( int argc, char** argv )
    : WGUI( argc, argv )
ebaum's avatar
ebaum committed
47 48 49
{
}

50
WQt4Gui::~WQt4Gui()
51
{
ebaum's avatar
ebaum committed
52
}
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
53

Mathias Goldau's avatar
Mathias Goldau committed
54
#ifdef _WIN32
55
// need this on windows to make it link correctly.
56
const unsigned int boost::program_options::options_description::m_default_line_length = 2048;
Mathias Goldau's avatar
Mathias Goldau committed
57
#endif
ebaum's avatar
ebaum committed
58

Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
59
bool WQt4Gui::parseOptions()
60
{
Mathias Goldau's avatar
Mathias Goldau committed
61 62
    namespace po = boost::program_options; // since the namespace is far to big we use a shortcut here
    po::options_description desc( "Allowed options" );
63

64 65
#ifndef _WIN32
// TODO(wiebel): this does not link on windows at the moment. But it should!
66 67
    desc.add_options()
        ( "help,h", "Prints this help message" )
68 69
        ( "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" );
70
#endif
71 72 73 74

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

75 76
    try
    {
Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
77
        po::store( po::command_line_parser( m_argc, m_argv ).options( desc ).positional( p ).run(), m_optionsMap );
78
    }
Mathias Goldau's avatar
Mathias Goldau committed
79
    catch( const po::error &e )
80
    {
Mathias Goldau's avatar
Mathias Goldau committed
81
        std::cerr << e.what() << std::endl;
82 83 84 85 86 87 88 89 90 91
        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;
    }
92 93 94 95 96
    else if( m_optionsMap.count( "timed-output" ) )
    {
        WLogger::getLogger()->setDefaultFormat( "[%t] *%l [%s] %m \n" );
        wlog::info( "GUI" ) << "Timed output enabled";
    }
97 98 99 100 101
    return true;
}

int WQt4Gui::run()
{
Mathias Goldau's avatar
[ADD]  
Mathias Goldau committed
102
    bool parsingSuccessful = parseOptions();
103 104 105

    if( !parsingSuccessful )
    {
106 107
        return 1;
    }
108

109
    // exit as fast as possible if command line asked for help. The ,essage has been printed by parseOptions().
110 111
    if( m_optionsMap.count( "help" ) )
    {
112
        return 1;
113 114
    }

115
    WLogger::getLogger()->run();
116
    WLogger::getLogger()->addLogMessage( "Bringing up GUI", "GUI", LL_INFO );
117

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

120
    // startup graphics engine
121
    m_ge = WGraphicsEngine::getGraphicsEngine();
122

123 124 125 126
    // and startup kernel
    m_kernel = boost::shared_ptr< WKernel >( new WKernel( m_ge, shared_from_this() ) );
    m_kernel->run();
    // create the window
127
    m_mainWindow = new WMainWindow();
128
    m_mainWindow->show();
129

130
    // connect out loader signal with krnel
131
    getLoadButtonSignal()->connect( boost::bind( &WKernel::loadDataSets, m_kernel, _1 ) );
132

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

135
    // bind the GUI's slot with the ready signal
136
    t_ModuleGenericSignalHandlerType f = boost::bind( &WQt4Gui::slotAddDatasetOrModuleToBrowser, this, _1 );
137
    m_kernel->getRootContainer()->addDefaultNotifier( WM_READY, f );
138 139

    // now we are initialized
140
    m_isInitialized( true );
141

142 143 144
    // check if we want to load data due to command line and call the respective function
    if( m_optionsMap.count("input") )
    {
145
        m_kernel->loadDataSets( m_optionsMap["input"].as< std::vector< std::string > >() );
146 147
    }

148
    // run
149 150 151 152 153 154 155
    int qtRetCode = appl.exec();

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

    return qtRetCode;
156 157
}

158
void WQt4Gui::slotAddDatasetOrModuleToBrowser( boost::shared_ptr< WModule > module )
159
{
160
    // get properties from the module and register them
161
    m_mainWindow->getPropertyManager()->connectProperties( module->getProperties() );
162

163 164 165
    // TODO(schurade): is this differentiation between data and "normal" modules really needed?
    if ( boost::shared_dynamic_cast< WMData >( module ).get() )
    {
166
        m_mainWindow->getDatasetBrowser()->addDataset( module, 0 );
167 168 169
    }
    else
    {
170
        m_mainWindow->getDatasetBrowser()->addModule( module );
171
    }
172 173
}

174
std::vector< boost::shared_ptr< WDataSet > > WQt4Gui::getDataSetList( int subjectId, bool onlyTextures )
175
{
176
    return m_mainWindow->getDatasetBrowser()->getDataSetList( subjectId, onlyTextures );
177
}
178

179 180
boost::shared_ptr< WModule > WQt4Gui::getSelectedModule()
{
181
    return m_mainWindow->getDatasetBrowser()->getSelectedModule();
182 183
}

schurade's avatar
schurade committed
184
boost::signals2::signal1< void, std::vector< std::string > >* WQt4Gui::getLoadButtonSignal()
185
{
186
    return m_mainWindow->getLoaderSignal();
187 188
}

schurade's avatar
schurade committed
189 190
boost::signals2::signal1< void, std::string >* WQt4Gui::getPickSignal()
{
191
    return m_mainWindow->getPickSignal();
schurade's avatar
schurade committed
192 193
}

194 195
boost::shared_ptr< WCustomWidget > WQt4Gui::createCustomWidget( std::string title, WGECamera::ProjectionMode projectionMode,
    boost::shared_ptr< WCondition > shutdownCondition )
196
{
197 198 199 200 201 202 203 204 205 206
    boost::shared_ptr< WFlag< boost::shared_ptr< WCustomWidget > > > widgetFlag(
        new WFlag< boost::shared_ptr< WCustomWidget > >( new WConditionOneShot, boost::shared_ptr< WCustomWidget >() ) );
    QCoreApplication::postEvent( m_mainWindow, new WCreateCustomDockWidgetEvent( title, projectionMode, widgetFlag ) );

    WConditionSet conditionSet;
    conditionSet.add( widgetFlag->getCondition() );
    conditionSet.add( shutdownCondition );
    conditionSet.wait();

    return widgetFlag->get();
207 208 209 210
}

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