Commit fea84e56 authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[ADD] Wpreferences class implements globally available options from the

config file. Currently each call results in a parsing of the file. This
will be fixed in the near future in order to allow caching of already checked
preferences.
parent f8cebe49
# This is a sample configuration file for OpenWalnut
# This is a sample configuration file for OpenWalnut.
# Uncomment the options you are interested in.
[ge] # Settings for the graphics engine
bgColor.r = .9 # background color (red part)
bgColor.g = .9 # background color (green part)
bgColor.b = .9 # background color (blue part)
#[modules.standard]
#ignoreHUD = yes
#[qt4gui]
#hideAxial = yes
#hideCoronal = yes
#hideSagittal = yes
#[ge] # Settings for the graphics engine
#bgColor.r = .9 # background color (red part)
#bgColor.g = .9 # background color (green part)
#bgColor.b = .9 # background color (blue part)
\ No newline at end of file
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// 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.
//
// OpenWalnut is distributed in the hope that it will be useful,
// 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
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WPreferences.h"
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// 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.
//
// OpenWalnut is distributed in the hope that it will be useful,
// 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
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPREFERENCES_H
#define WPREFERENCES_H
#include <string>
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include "../utils/WIOTools.h"
#include "WLogger.h"
/**
* Fetches and caches preferences set in file.
*/
class WPreferences
{
public:
/**
* Get the value of a preference with a given name.
* \param prefName string that identifies the preference inf the config file
* \param retVal pointer to the value belonging to the name
* \return True if value could be found, false otherwise.
*/
template< typename T> static bool getPreference( std::string prefName, T* retVal );
protected:
private:
// TODO(wiebel): preference caching structure
// static WProperties m_preferences; //!< Structure for caching the preferences.
};
template< typename T > bool WPreferences::getPreference( std::string prefName, T* retVal )
{
if( prefName == "" )
{
return false;
}
// TODO(wiebel): insert check for cached value here
namespace po = boost::program_options; // since the namespace is far to big we use a shortcut here
//=====================
// CONFIGURATION FILE
po::options_description configurationDescription( "OpenWalnut Preferences" );
#ifndef _WIN32
// TODO(wiebel): this does not link on windows at the moment. But it should!
configurationDescription.add_options()
( prefName.c_str(), po::value< T >() );
#endif
std::string cfgFileName( "walnut.cfg" );
boost::program_options::variables_map configuration;
if( wiotools::fileExists( cfgFileName ) )
{
wlog::info( "GUI" ) << "Reading config file: " << cfgFileName;
std::ifstream ifs( cfgFileName.c_str(), std::ifstream::in );
try
{
po::store( po::parse_config_file( ifs, configurationDescription, true ), configuration );
}
catch( const po::error &e )
{
std::cerr << "Error in configuration file \"" << cfgFileName << "\": " << e.what() << std::endl;
return false;
}
}
else
{
wlog::info( "Preferences" ) << "No Config file: " << cfgFileName << " found";
}
po::notify( configuration );
if( configuration.count( prefName ) )
{
*retVal = configuration[ prefName ].as< T >();
return true;
}
else
{
return false;
}
}
#endif // WPREFERENCES_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// 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.
//
// OpenWalnut is distributed in the hope that it will be useful,
// 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
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPREFERENCES_TEST_H
#define WPREFERENCES_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WPreferences.h"
/**
* TODO(wiebel): Document this!
*/
class WPreferencesTest : public CxxTest::TestSuite
{
public:
/**
* TODO(wiebel): Document this!
*/
void testSomething( void )
{
}
};
#endif // WPREFERENCES_TEST_H
......@@ -38,24 +38,25 @@
#include <QtGui/QVBoxLayout>
#include "WMainWindow.h"
#include "WCreateCustomDockWidgetEvent.h"
#include "WQtGLWidget.h"
#include "../../kernel/WKernel.h"
#include "WQtNavGLWidget.h"
#include "WCreateCustomDockWidgetEvent.h"
#include "WQtCustomDockWidget.h"
#include "../../common/WColor.h"
#include "../../common/WPreferences.h"
#include "../../kernel/WKernel.h"
#include "../icons/WIcons.h"
WMainWindow::WMainWindow( boost::program_options::variables_map guiConfiguration ) :
WMainWindow::WMainWindow() :
QMainWindow(),
m_iconManager(),
m_propertyManager()
{
setupGUI( guiConfiguration );
setupGUI();
}
void WMainWindow::setupGUI( boost::program_options::variables_map guiConfiguration )
void WMainWindow::setupGUI()
{
m_iconManager.addIcon( std::string( "logo" ), logoIcon_xpm );
......@@ -75,29 +76,45 @@ void WMainWindow::setupGUI( boost::program_options::variables_map guiConfigurati
m_mainGLWidget->initialize();
setCentralWidget( m_mainGLWidget.get() );
// initially 3 views
m_navAxial = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "axial", this, 160, "axialPos" ) );
addDockWidget( Qt::LeftDockWidgetArea, m_navAxial.get() );
m_navCoronal = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "coronal", this, 200, "coronalPos" ) );
addDockWidget( Qt::LeftDockWidgetArea, m_navCoronal.get() );
m_navSagittal = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "sagittal", this, 160, "sagittalPos" ) );
addDockWidget( Qt::LeftDockWidgetArea, m_navSagittal.get() );
connect( m_navAxial.get(), SIGNAL( navSliderValueChanged( QString, int ) ), &m_propertyManager, SLOT( slotIntChanged( QString, int ) ) );
connect( m_navCoronal.get(), SIGNAL( navSliderValueChanged( QString, int ) ), &m_propertyManager, SLOT( slotIntChanged( QString, int ) ) );
connect( m_navSagittal.get(), SIGNAL( navSliderValueChanged( QString, int ) ), &m_propertyManager, SLOT( slotIntChanged( QString, int ) ) );
// initially 3 navigation views
{
bool hideWidget;
if( !( WPreferences::getPreference( "qt4gui.hideAxial", &hideWidget ) && hideWidget) )
{
m_navAxial = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "axial", this, 160, "axialPos" ) );
addDockWidget( Qt::LeftDockWidgetArea, m_navAxial.get() );
connect( m_navAxial.get(), SIGNAL( navSliderValueChanged( QString, int ) ),
&m_propertyManager, SLOT( slotIntChanged( QString, int ) ) );
}
if( !( WPreferences::getPreference( "qt4gui.hideCoronal", &hideWidget ) && hideWidget) )
{
m_navCoronal = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "coronal", this, 200, "coronalPos" ) );
addDockWidget( Qt::LeftDockWidgetArea, m_navCoronal.get() );
connect( m_navCoronal.get(), SIGNAL( navSliderValueChanged( QString, int ) ),
&m_propertyManager, SLOT( slotIntChanged( QString, int ) ) );
}
if( !( WPreferences::getPreference( "qt4gui.hideSagittal", &hideWidget ) && hideWidget) )
{
m_navSagittal = boost::shared_ptr< WQtNavGLWidget >( new WQtNavGLWidget( "sagittal", this, 160, "sagittalPos" ) );
addDockWidget( Qt::LeftDockWidgetArea, m_navSagittal.get() );
connect( m_navSagittal.get(), SIGNAL( navSliderValueChanged( QString, int ) ),
&m_propertyManager, SLOT( slotIntChanged( QString, int ) ) );
}
}
if( guiConfiguration.count( "ge.bgColor.r" ) && guiConfiguration.count( "ge.bgColor.g" ) && guiConfiguration.count( "ge.bgColor.b" ) )
// Default background color from config file
{
WColor bgColor( guiConfiguration["ge.bgColor.r"].as< float >(),
guiConfiguration["ge.bgColor.g"].as< float >(),
guiConfiguration["ge.bgColor.b"].as< float >() );
m_mainGLWidget->setBgColor( bgColor );
m_navAxial->getGLWidget()->setBgColor( bgColor );
m_navCoronal->getGLWidget()->setBgColor( bgColor );
m_navSagittal->getGLWidget()->setBgColor( bgColor );
WColor bgColor;
double r;
double g;
double b;
if( WPreferences::getPreference( "ge.bgColor.r", &r )
&& WPreferences::getPreference( "ge.bgColor.g", &g )
&& WPreferences::getPreference( "ge.bgColor.b", &b ) )
{
bgColor.setRGB( r, g, b );
m_mainGLWidget->setBgColor( bgColor );
}
}
setupRibbonMenu();
......
......@@ -65,15 +65,13 @@ class WMainWindow : public QMainWindow
public:
/**
* Constructor of the main window
* \param guiConfiguration configuration information taken from config file or command line
*/
explicit WMainWindow( boost::program_options::variables_map guiConfiguration );
explicit WMainWindow();
/**
* Set up all widgets menus an buttons in the main window.
* \param guiConfiguration configuration information taken from config file or command line
*/
void setupGUI( boost::program_options::variables_map guiConfiguration );
void setupGUI();
/**
* returns a pointer to the dataset browser object
......
......@@ -84,42 +84,6 @@ bool WQt4Gui::parseOptions()
po::notify( m_optionsMap );
//=====================
// CONFIGURATION FILE
po::options_description guiConfigurationDescription( "GUI configuration" );
#ifndef _WIN32
// TODO(wiebel): this does not link on windows at the moment. But it should!
guiConfigurationDescription.add_options()
( "ge.bgColor.r", po::value< float >() )
( "ge.bgColor.g", po::value< float >() )
( "ge.bgColor.b", po::value< float >() );
#endif
std::string cfgFileName( "walnut.cfg" );
if( wiotools::fileExists( cfgFileName ) )
{
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;
}
}
else
{
wlog::info( "GUI" ) << "No Config file: " << cfgFileName << " found";
}
po::notify( m_guiConfiguration );
// print usage information if command line asks for help.
if( m_optionsMap.count( "help" ) )
{
......@@ -160,7 +124,7 @@ int WQt4Gui::run()
m_kernel = boost::shared_ptr< WKernel >( new WKernel( m_ge, shared_from_this() ) );
m_kernel->run();
// create the window
m_mainWindow = new WMainWindow( m_guiConfiguration );
m_mainWindow = new WMainWindow();
m_mainWindow->show();
// connect out loader signal with krnel
......
......@@ -136,7 +136,6 @@ private:
boost::shared_ptr< WKernel > m_kernel;
boost::program_options::variables_map m_optionsMap; //!< Map storing the program options.
boost::program_options::variables_map m_guiConfiguration; //!< Map storing the configuration of the GUI
/**
* This function defines and parses the valid command line options.
......
......@@ -43,6 +43,7 @@
#include "../common/WCondition.h"
#include "../common/WConditionOneShot.h"
#include "../common/WFlag.h"
#include "../common/WPreferences.h"
#include "../graphicsEngine/WGraphicsEngine.h"
......@@ -143,9 +144,15 @@ void WKernel::threadMain()
m_graphicsEngine->run();
// default modules
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Navigation Slice Module" ) ) , true );
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Coordinate System Module" ) ) , true );
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "HUD" ) ) , true );
{
bool ignore;
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Navigation Slice Module" ) ) , true );
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Coordinate System Module" ) ) , true );
if( !( WPreferences::getPreference( "modules.standard.ignoreHUD", &ignore ) && ignore ) )
{
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "HUD" ) ) , true );
}
}
// actually there is nothing more to do here
waitForStop();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment