WQt4Gui.cpp 7.14 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"
Mathias Goldau's avatar
Mathias Goldau committed
42
#include "WQt4Gui.h"
ebaum's avatar
ebaum committed
43

44 45
WQt4Gui::WQt4Gui( int argc, char** argv ):
    WGUI( argc, argv )
ebaum's avatar
ebaum committed
46 47 48
{
}

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

Mathias Goldau's avatar
Mathias Goldau committed
53 54 55
#ifdef _WIN32
// TODO(wiebel): Hi, this is math. I assume the line below is due to linker errors with boost. I just marked it with this todo
// since we don't forget this. When using Linux or Mac I _think_ we don't need this.
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

59
bool WQt4Gui::parseOptions( int argc, char** argv )
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 68
    desc.add_options()
        ( "help,h", "Prints this help message" )
        ( "input,i", po::value< std::vector< std::string > >(), "Input data files that should be loaded automatically" );
69
#endif
70 71 72 73

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

74 75
    try
    {
76
        po::store( po::command_line_parser( argc, argv ).options( desc ).positional( p ).run(), m_optionsMap );
77
    }
Mathias Goldau's avatar
Mathias Goldau committed
78
    catch( const po::error &e )
79
    {
Mathias Goldau's avatar
Mathias Goldau committed
80
        std::cerr << e.what() << std::endl;
81 82 83 84 85 86 87 88
        return false;
    }

    po::notify( m_optionsMap );

    //=====================
    // CONFIGURATION FILE
    po::options_description guiConfigurationDescription( "GUI configuration" );
Mathias Goldau's avatar
Mathias Goldau committed
89

90 91
#ifndef _WIN32
// TODO(wiebel): this does not link on windows at the moment. But it should!
92 93 94 95
    guiConfigurationDescription.add_options()
        ( "ge.bgColor.r", po::value< float >() )
        ( "ge.bgColor.g", po::value< float >() )
        ( "ge.bgColor.b", po::value< float >() );
96
#endif
97

Mathias Goldau's avatar
Mathias Goldau committed
98
    std::string cfgFileName( "walnut.cfg" );
99

Mathias Goldau's avatar
Mathias Goldau committed
100
    if( wiotools::fileExists( cfgFileName ) )
101
    {
Mathias Goldau's avatar
Mathias Goldau committed
102 103 104 105 106 107 108 109 110 111 112 113
        wlog::info( "GUI" ) << "Reading config file: " << cfgFileName;
        std::ifstream ifs( cfgFileName.c_str(), std::ifstream::in );

        try
        {
            po::store( po::parse_config_file( ifs, guiConfigurationDescription ), m_guiConfiguration );
        }
        catch( const po::error &e )
        {
            std::cerr << "Error in configuration file \"" << cfgFileName << "\": " << e.what() << std::endl;
            return false;
        }
114
    }
Mathias Goldau's avatar
Mathias Goldau committed
115
    else
116
    {
Mathias Goldau's avatar
Mathias Goldau committed
117
        wlog::info( "GUI" ) << "No Config file: " << cfgFileName << " found";
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    }

    po::notify( m_guiConfiguration );

    // print usage information if command line asks for help.
    if( m_optionsMap.count( "help" ) )
    {
        std::cout << desc << std::endl;
    }
    return true;
}

int WQt4Gui::run()
{
    bool parsingSuccessful = false;
    parsingSuccessful = parseOptions( argc, argv );

    if( !parsingSuccessful )
    {
137 138
        return 1;
    }
139

140
    // exit as fast as possible if command line asked for help. The ,essage has been printed by parseOptions().
141 142
    if( m_optionsMap.count( "help" ) )
    {
143
        return 1;
144 145
    }

146
    WLogger::getLogger()->run();
147
    WLogger::getLogger()->addLogMessage( "Bringing up GUI", "GUI", LL_INFO );
148

149
    QApplication appl( argc, argv, true );
150

151
    // startup graphics engine
152
    m_ge = WGraphicsEngine::getGraphicsEngine();
153

154 155 156 157
    // and startup kernel
    m_kernel = boost::shared_ptr< WKernel >( new WKernel( m_ge, shared_from_this() ) );
    m_kernel->run();
    // create the window
158
    m_gui = new WMainWindow( m_guiConfiguration );
159
    m_gui->show();
160

161
    // connect out loader signal with krnel
162
    getLoadButtonSignal()->connect( boost::bind( &WKernel::loadDataSets, m_kernel, _1 ) );
163

164
    m_gui->getModuleButtonSignal()->connect( boost::bind( &WKernel::applyModule, m_kernel, _1, _2 ) );
165

166
    // bind the GUI's slot with the ready signal
167
    t_ModuleGenericSignalHandlerType f = boost::bind( &WQt4Gui::slotAddDatasetOrModuleToBrowser, this, _1 );
168
    m_kernel->getRootContainer()->addDefaultNotifier( WM_READY, f );
169 170

    // now we are initialized
171
    m_isInitialized( true );
172

173 174 175
    // check if we want to load data due to command line and call the respective function
    if( m_optionsMap.count("input") )
    {
176
        m_kernel->loadDataSets( m_optionsMap["input"].as< std::vector< std::string > >() );
177 178
    }

179
    // run
180 181 182 183 184 185 186
    int qtRetCode = appl.exec();

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

    return qtRetCode;
187 188
}

189
void WQt4Gui::slotAddDatasetOrModuleToBrowser( boost::shared_ptr< WModule > module )
190
{
191 192
    // get properties from the module and register them
    m_gui->getPropertyManager()->connectProperties( module->getProperties() );
193

194 195 196 197 198 199 200 201 202
    // TODO(schurade): is this differentiation between data and "normal" modules really needed?
    if ( boost::shared_dynamic_cast< WMData >( module ).get() )
    {
        m_gui->getDatasetBrowser()->addDataset( module, 0 );
    }
    else
    {
        m_gui->getDatasetBrowser()->addModule( module );
    }
203 204
}

205
std::vector< boost::shared_ptr< WDataSet > > WQt4Gui::getDataSetList( int subjectId, bool onlyTextures )
206
{
207
    return m_gui->getDatasetBrowser()->getDataSetList( subjectId, onlyTextures );
208
}
209

210 211 212 213 214
boost::shared_ptr< WModule > WQt4Gui::getSelectedModule()
{
    return m_gui->getDatasetBrowser()->getSelectedModule();
}

schurade's avatar
schurade committed
215
boost::signals2::signal1< void, std::vector< std::string > >* WQt4Gui::getLoadButtonSignal()
216 217 218 219
{
    return m_gui->getLoaderSignal();
}

schurade's avatar
schurade committed
220 221 222 223 224
boost::signals2::signal1< void, std::string >* WQt4Gui::getPickSignal()
{
    return m_gui->getPickSignal();
}

225 226 227 228 229 230 231 232 233
void WQt4Gui::createCustomWidget( std::string title )
{
    QCoreApplication::postEvent( m_gui, new WCreateCustomDockWidgetEvent( title ) );
}

void WQt4Gui::closeCustomWidget( std::string title )
{
    m_gui->closeCustomDockWidget( title );
}