WQt4Gui.cpp 4.9 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 27
#include <string>
#include <vector>
28

29 30
#include <boost/program_options.hpp>

31
#include <QtGui/QApplication>
32 33 34
#include <QtGui/QFileDialog>

#include "WMainWindow.h"
35 36
#include "../../kernel/WKernel.h"
#include "../../graphicsEngine/WGraphicsEngine.h"
37

38
#include "WQt4Gui.h"
ebaum's avatar
ebaum committed
39

40 41
WQt4Gui::WQt4Gui( int argc, char** argv ):
    WGUI( argc, argv )
ebaum's avatar
ebaum committed
42 43 44
{
}

45
WQt4Gui::~WQt4Gui()
46
{
ebaum's avatar
ebaum committed
47 48
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
 * This function defines and parses the valid command line options.
 * This might once be put in a separate class like WOptionHandler.
 * At the moment it seems reasonable that differnet GUIs might have
 * different command line options, thus we implement their parsing
 * in the GUI implemntation itself, i.e. here.
 */
boost::program_options::variables_map parseOptions( int argc, char** argv )
{
    // since the namespace is far to big we use a shortcut here
    namespace po = boost::program_options;

    po::variables_map optionsMap;
    po::options_description desc( "Allowed options" );
    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" );

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

    po::store( po::command_line_parser( argc, argv ).options( desc ).positional( p ).run(), optionsMap );
    po::notify( optionsMap );

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

81
int WQt4Gui::run()
ebaum's avatar
ebaum committed
82
{
83 84 85 86 87 88 89 90 91
    try
    {
        m_optionsMap = parseOptions( argc, argv );
    }
    catch( boost::program_options::unknown_option e )
    {
        std::cout << e.what() << std::endl;
        return 1;
    }
92 93 94 95 96 97 98 99 100

    // exit as fast as possible if command line asks for help.
    if( m_optionsMap.count( "help" ) )
    {
        return 0;
    }

    // TODO(all): In my (alex) opinion the WLogger should be started here and not before. This allows to generate a simple help message.

101
    WLogger::getLogger()->addLogMessage( "Bringing up GUI", "GUI", LL_INFO );
102

103
    QApplication appl( argc, argv, true );
104

105 106
    // startup graphics engine
    m_ge = boost::shared_ptr< WGraphicsEngine >( new WGraphicsEngine() );
107

108 109 110 111 112 113
    // and startup kernel
    m_kernel = boost::shared_ptr< WKernel >( new WKernel( m_ge, shared_from_this() ) );
    m_kernel->run();
    // create the window
    m_gui = new WMainWindow;
    m_gui->show();
114

115 116
    // connect out loader signal with krnel
    getLoadButtonSignal()->connect( boost::bind( &WKernel::doLoadDataSets, m_kernel, _1 ) );
117

118 119 120 121 122
    // bind the GUI's slot with the ready signal
    t_ModuleGenericSignalHandlerType f = boost::bind( &WGUI::slotAddDatasetToBrowser, this, _1 );
    m_kernel->getRootContainer()->addDefaultNotifier( READY, f );

    // now we are initialized
123
    m_isInitialized( true );
124

125 126 127 128 129 130
    // check if we want to load data due to command line and call the respective function
    if( m_optionsMap.count("input") )
    {
        m_kernel->doLoadDataSets( m_optionsMap["input"].as< std::vector< std::string > >() );
    }

131
    // run
132 133 134 135 136 137 138
    int qtRetCode = appl.exec();

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

    return qtRetCode;
139 140
}

141
void WQt4Gui::addDatasetToBrowser( boost::shared_ptr< WModule > module, int subjectId )
142
{
143
    m_gui->getDatasetBrowser()->addDataset( module, subjectId );
144 145
}

146 147 148 149
std::vector< boost::shared_ptr< WModule > >WQt4Gui::getDataSetList( int subjectId )
{
    return m_gui->getDatasetBrowser()->getDataSetList( subjectId );
}
150

151 152 153 154 155
boost::signal1< void, std::vector< std::string > >* WQt4Gui::getLoadButtonSignal()
{
    return m_gui->getLoaderSignal();
}

156
void WQt4Gui::connectProperties( boost::shared_ptr<WProperties> properties )
157
{
158
    m_gui->getPropertyManager()->connectProperties( properties );
159
}