WQtConfigWidget.cpp 48.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
//---------------------------------------------------------------------------
//
// 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 <list>
#include <vector>
#include <string>

#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>

#include "WCfgOperations.h"
#include "../../kernel/WModuleFactory.h"
#include "../../common/WConditionOneShot.h"

#include "WQtConfigWidget.h"

WQtConfigWidget::WQtConfigWidget() :
        QWidget()
{
41
    this->setWindowTitle( "walnut.cfg Editor" );
42

43
    QVBoxLayout *verticalLayout = new QVBoxLayout( this );
44 45 46 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 81
    m_tabWidget = new QTabWidget();
    verticalLayout->addWidget( m_tabWidget );
    QHBoxLayout *horizontalLayout = new QHBoxLayout();
    verticalLayout->addLayout( horizontalLayout );
    m_okButton = new QPushButton( "Save" );
    m_cancelButton = new QPushButton( "Cancel" );
    m_previewButton = new QPushButton( "Preview" );
    m_resetDefaultButton = new QPushButton( "Reset to defaults" );
    m_resetBackupButton = new QPushButton( "Reset to Loaded" );

    horizontalLayout->addWidget( m_okButton );
    horizontalLayout->addWidget( m_cancelButton );
    horizontalLayout->addWidget( m_previewButton );
    horizontalLayout->addWidget( m_resetDefaultButton );
    horizontalLayout->addWidget( m_resetBackupButton );

    connect( m_okButton,           SIGNAL( clicked() ), this, SLOT( save() ) );
    connect( m_cancelButton,       SIGNAL( clicked() ), this, SLOT( cancel() ) );
    connect( m_previewButton,      SIGNAL( clicked() ), this, SLOT( preview() ) );
    connect( m_resetDefaultButton, SIGNAL( clicked() ), this, SLOT( resetToDefaults() ) );
    connect( m_resetBackupButton,  SIGNAL( clicked() ), this, SLOT( resetToBackup() ) );

    connect( this,                 SIGNAL( closeWidget()   ), this, SLOT( cancel() ) );

    // allocate the propertys
    m_defaultProperties = boost::shared_ptr< WProperties >( new WProperties );
    m_loadedProperties = boost::shared_ptr< WProperties >( new WProperties );
    m_properties = boost::shared_ptr< WProperties >( new WProperties );

    registerComponents();

    m_previewed = false;
}

WQtConfigWidget::~WQtConfigWidget()
{
}

82
void WQtConfigWidget::getAvailableModuleNames()
83 84
{
    m_moduleNames.clear();
85

86 87
    // read all prototypes
    WModuleFactory::PrototypeSharedContainerType::ReadTicket pa = WModuleFactory::getModuleFactory()->getPrototypes();
88
    for ( WModuleFactory::PrototypeContainerConstIteratorType itr = pa->get().begin(); itr != pa->get().end(); ++itr )
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    {
        m_moduleNames.push_back( ( *itr )->getName() );
    }
}

void WQtConfigWidget::updatePropertyGroups( boost::shared_ptr< WProperties > properties, std::string groupName, bool fromConfig )
{
    // Here we assume we already registered our properties
    // then we set them one way or another

    // the bool flag can be used to determine which way the update shall be executed

    // Colors:
    // this may also be updated when either color or one of the values gets updated
    if ( groupName == "ge.bgColor" )
    {
        WPropGroup backgroundColor = properties->findProperty( "ge.bgColor" )->toPropGroup();
        WPropDouble r, g, b;
        r = backgroundColor->findProperty( "ge.bgColor.r" )->toPropDouble();
        g = backgroundColor->findProperty( "ge.bgColor.g" )->toPropDouble();
        b = backgroundColor->findProperty( "ge.bgColor.b" )->toPropDouble();
        WPropColor wColorProp = backgroundColor->findProperty( "ge.sColorWidget" )->toPropColor();

        if ( fromConfig )
        {
            WColor color( static_cast< double >( r->get( true ) ), static_cast< double >( g->get( true ) ), static_cast< double >( b->get( true ) ) );
            wColorProp->set( color, true );
        }
        else
        {
            WColor color = wColorProp->get( true );
            r->set( static_cast< double >( color.getRed() ), true );
            g->set( static_cast< double >( color.getGreen() ), true );
            b->set( static_cast< double >( color.getBlue() ), true );
        }
    }
    size_t i;
    if ( groupName == "modules.whiteListGroup" )
    {
        WPropGroup moduleWhiteList = properties->findProperty( "modules.whiteListGroup" )->toPropGroup();
        WPropString whiteList = moduleWhiteList->findProperty( "modules.whiteList" )->toPropString();

        if ( fromConfig )
        {
            for ( i = 0; i < m_moduleNames.size(); ++i )
            {
                bool isWhiteListed = false;
                std::string whiteString = whiteList->get();
                if ( whiteString.find( m_moduleNames[i] ) != std::string::npos )
                {
                    isWhiteListed = true;
                }
                moduleWhiteList->findProperty( "modules.whitelist." + m_moduleNames[i] )->toPropBool()->set( isWhiteListed, true );
            }
        }
        else
        {
            std::string setStr;

            for ( i = 0; i < m_moduleNames.size(); ++i )
            {
                bool isWhiteListed = moduleWhiteList->findProperty( "modules.whitelist." + m_moduleNames[i] )->toPropBool()->get( true );
                if ( isWhiteListed )
                {
                    if ( setStr.length() > 0 )
                    {
                        setStr += ',';
                    }
                    setStr += m_moduleNames[i];
                }
            }
            whiteList->set( setStr, true );
        }
    }

    if ( groupName == "modules.defaultList" )
    {
        WPropGroup moduleDefaultList = properties->findProperty( "modules.defaultList" )->toPropGroup();
        WPropString defaultList = moduleDefaultList->findProperty( "modules.default" )->toPropString();
        if ( fromConfig )
        {
            for ( i = 0; i < m_moduleNames.size(); ++i )
            {
                bool isDefaultListed = false;
                std::string defaultString = defaultList->get();
                if ( defaultString.find( m_moduleNames[i] ) != std::string::npos )
                {
                    isDefaultListed = true;
                }
                moduleDefaultList->findProperty( "modules.defaultList." + m_moduleNames[i] )->toPropBool()->set( isDefaultListed, true );
            }
        }
        else
        {
            std::string setStr;

            for ( i = 0; i < m_moduleNames.size(); ++i )
            {
                bool isDefaultListed = moduleDefaultList->findProperty( "modules.defaultList." + m_moduleNames[i] )->toPropBool()->get( true );
                if ( isDefaultListed )
                {
                    if ( setStr.length() > 0 )
                    {
                        setStr += ',';
                    }
                    setStr += m_moduleNames[i];
                }
            }
            defaultList->set( setStr, true );
        }
    }
}

void WQtConfigWidget::registerComponents()
{
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    getAvailableModuleNames();

    //////////////////////////////////////////////////////////////////////////
    //  Tutorial on how to add properties to the config editor              //
    //////////////////////////////////////////////////////////////////////////
    //  1. Make sure the section you use is registered at the beginning     //
    //  2. Add your Properties to m_defaultProperties with a short          //
    //     description and default value                                    //
    //     Note: You can also groups to show the properties in different    //
    //     ways like the background color group                             //
    //  3. every property that isn't represented in the config file needs   //
    //     to be added to m_skipPropertyWrite                               //
    //  4. to react on changes in groups like the color group, add an if    //
    //     section to updatePropertyGroups() and call them in the           //
    //     initialization and at the condition change if clause in the      //
    //     threadMain()                                                     //
    //  5. to make changes directly to openWalnut you need to make sure     //
    //     that there is an appropriate interface and call it from within   //
    //     setWalnutFromProperties()                                        //
    //////////////////////////////////////////////////////////////////////////

    // Register config properties
    m_registeredSections.push_back( "general" );
    m_registeredSections.push_back( "qt4gui" );
    m_registeredSections.push_back( "ge" );
    m_registeredSections.push_back( "modules" );
230 231 232

    m_skipPropertyWrite.clear();

233 234
    // to set and get properties use the get() and set() methods of WFlag
    // copy properties through getAccessObject()
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );

    // add every property existing with its default values
    m_defaultProperties->addProperty( "ge.zoomTrackballManipulator.allowThrow", "Allow auto rotation", false, m_propCondition );
    m_defaultProperties->addProperty( "ge.multiThreadedViewers", "Multithreaded Viewers", true, m_propCondition );
    m_defaultProperties->addProperty( "qt4gui.hideAxial", "Hide Axial View", false, m_propCondition );
    m_defaultProperties->addProperty( "qt4gui.hideCoronal", "Hide Coronal View", false, m_propCondition );
    m_defaultProperties->addProperty( "qt4gui.hideSagittal", "Hide Sagittal View", false, m_propCondition );

    // those are connected, so we put them into a group
    WPropGroup backgroundColor = m_defaultProperties->addPropertyGroup( "ge.bgColor", "color group" );
    m_skipPropertyWrite.push_back( "ge.bgColor" );
    WPropDouble r, g, b;
    r = backgroundColor->addProperty( "ge.bgColor.r", "red value of Background Color", 0.9, m_propCondition );
    g = backgroundColor->addProperty( "ge.bgColor.g", "green value of Background Color", 0.9, m_propCondition );
    b = backgroundColor->addProperty( "ge.bgColor.b", "blue value of Background Color", 0.9, m_propCondition );
    r->setMin( 0.0 );
    r->setMax( 1.0 );
    g->setMin( 0.0 );
    g->setMax( 1.0 );
    b->setMin( 0.0 );
    b->setMax( 1.0 );
    WColor color( static_cast< double >( r->get() ), static_cast< double >( g->get() ), static_cast< double >( b->get() ) );
    backgroundColor->addProperty( "ge.sColorWidget", "Color pick widget", color, m_propCondition );
    m_skipPropertyWrite.push_back( "ge.sColorWidget" );

    m_defaultProperties->addProperty( "qt4gui.useAutoDisplay", "use Auto Display", true, m_propCondition );
    m_defaultProperties->addProperty( "qt4gui.useToolBarBreak", "use ToolBarBreak", true, m_propCondition );
    m_defaultProperties->addProperty( "general.allowOnlyOneFiberDataSet", "allow only one FiberDataSet", false, m_propCondition );
265
    WPropInt tbs = m_defaultProperties->addProperty( "qt4gui.toolBarStyle", "The style of all toolbars in OpenWalnut", 0, m_propCondition );
266 267
    WPropInt ctbs = m_defaultProperties->addProperty( "qt4gui.compatiblesToolBarStyle", "The style of all compatibles toolbar in OpenWalnut", 0,
                                                      m_propCondition );
268 269 270 271
    tbs->setMin( 0 );
    tbs->setMax( 3 );
    ctbs->setMin( 0 );
    ctbs->setMax( 3 );
272 273
    WPropInt tbpos = m_defaultProperties->addProperty( "qt4gui.toolBarPos", "The position of the toolbar in OpenWalnut", 0,
                                                       m_propCondition );
274 275
    WPropInt ctbpos = m_defaultProperties->addProperty( "qt4gui.compatiblesToolBarPos", "The position of the compatibles toolbar in OpenWalnut", 0,
                                                        m_propCondition );
276 277
    tbpos->setMin( 0 );
    tbpos->setMax( 3 );
278 279
    ctbpos->setMin( 0 );
    ctbpos->setMax( 3 );
280
    m_defaultProperties->addProperty( "qt4gui.hideMenuBar", "Hide the menu bar.", false, m_propCondition );
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

    WPropGroup moduleWhiteList =  m_defaultProperties->addPropertyGroup( "modules.whiteListGroup", "moduleWhiteList" );
    m_skipPropertyWrite.push_back( "modules.whiteListGroup" );
    WPropString whiteList = moduleWhiteList->addProperty( "modules.whiteList", "Modules that are whitelisted",
        std::string( "Isosurface,Bounding Box, Direct Volume Rendering,Distance Map Isosurface,Gauss Filtering,HUD,Vector Plot,Write NIfTI" ),
        m_propCondition, true );
    size_t i;
    for ( i = 0; i < m_moduleNames.size(); ++i )
    {
        bool isWhiteListed = false;
        std::string whiteString = whiteList->toPropString()->get();
        if ( whiteString.find( m_moduleNames[i] ) != std::string::npos )
        {
            isWhiteListed = true;
        }
        moduleWhiteList->addProperty( "modules.whitelist." + m_moduleNames[i],
            "Module " + m_moduleNames[i] + " is on whitelist", isWhiteListed, m_propCondition );
        m_skipPropertyWrite.push_back( "modules.whitelist." + m_moduleNames[i] );
    }

    WPropGroup moduleDefaultList = m_defaultProperties->addPropertyGroup( "modules.defaultList", "default Modules List" );
    m_skipPropertyWrite.push_back( "modules.defaultList" );
    WPropString defaultList = moduleDefaultList->addProperty( "modules.default", "default Modules", std::string( "" ), m_propCondition, true );
    for ( i = 0; i < m_moduleNames.size(); ++i )
    {
        bool isDefaultListed = false;
        std::string defaultString = defaultList->toPropString()->get();
        if ( defaultString.find( m_moduleNames[i] ) != std::string::npos )
        {
            isDefaultListed = true;
        }
        moduleDefaultList->addProperty( "modules.defaultList." + m_moduleNames[i],
            "Module " + m_moduleNames[i] + " is on defaultlist", isDefaultListed, m_propCondition );
        m_skipPropertyWrite.push_back( "modules.defaultList." + m_moduleNames[i] );
    }

    m_defaultProperties->addProperty( "modules.MC.isoValue", "Marching Cubes isoValue", 100.0, m_propCondition );
}

320
boost::shared_ptr< WProperties > WQtConfigWidget::copyProperties( boost::shared_ptr< WProperties > from )
321
{
322
    return boost::shared_static_cast< WProperties >( from->clone() );
323 324
}

325
void WQtConfigWidget::copyPropertiesContents( boost::shared_ptr< WProperties > from, boost::shared_ptr< WProperties > to, bool lock )
326 327 328 329 330 331
{
    // we assume from and to got the same structure
    // this is needed to keep the pointers, only set the values
    WProperties::PropertyAccessType accesObject = from->getAccessObject();
    WProperties::PropertyAccessType accesObject2 = to->getAccessObject();

332 333 334
    // in recursive calls, this avoids concurrent locks (even if concurrent read locks are allowed).
    if ( lock )
    {
335 336 337
        // Temporarily disabled since locking causes several problems here :-/
        // accesObject->beginRead();
        // accesObject2->beginRead();
338
    }
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    WProperties::PropertyIterator iter;
    WProperties::PropertyIterator iter2;
    for ( iter = accesObject->get().begin(), iter2 = accesObject2->get().begin();
          iter != accesObject->get().end() && iter2 != accesObject2->get().end();
          ++iter, ++iter2 )
    {
        // so far we only need those 5 types
        switch( (*iter)->getType() )
        {
        case PV_GROUP:
            {
                // recurse all groups
                copyPropertiesContents( ( *iter )->toPropGroup(), ( *iter2 )->toPropGroup() );
                break;
            }
        default:
            {
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
357
                // copy value
358
                ( *iter2 )->set( *iter );
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
359

360 361 362 363
                break;
            }
        }
    }
364

365 366
    if ( lock )
    {
367 368 369
        // Temporarily disabled since locking causes several problems here :-/
        // accesObject2->endRead();
        // accesObject->endRead();
370
    }
371 372 373 374
}

void WQtConfigWidget::addLineToProperty( boost::shared_ptr< WProperties > var, size_t lineNumber )
{
375
    LinePropertySet::iterator lPSitr;
376 377 378 379 380 381 382 383 384 385 386 387 388
    lPSitr = m_lineAssociationList.find( var );
    if ( lPSitr != m_lineAssociationList.end() )
    {
        lPSitr->second.push_back( lineNumber );
    }
    else
    {
        std::vector< size_t > lineList;
        lineList.push_back( lineNumber );
        m_lineAssociationList[ var ] = lineList;
    }
}

389
boost::shared_ptr< WPropertyBase > WQtConfigWidget::findPropertyRecursive( boost::shared_ptr< WProperties > searchIn, std::string name, bool lock )
390 391
{
    WProperties::PropertyAccessType accesObject = searchIn->getAccessObject();
392 393 394 395

    // avoid concurrent locks in recursive calls (even if concurrent read locks are allowed)
    if ( lock )
    {
396 397
        // Temporarily disabled since locking causes several problems here :-/
        // accesObject->beginRead();
398
    }
399 400 401

    boost::shared_ptr< WPropertyBase > result = boost::shared_ptr< WPropertyBase >();

402
    // TODO(ledig): WProperties::findProperty already does the job of searching recursively. Better use this.
403 404 405 406 407 408
    WProperties::PropertyIterator iter;
    for ( iter = accesObject->get().begin(); iter != accesObject->get().end(); ++iter )
    {
        if ( ( *iter )->getName() == name )
        {
            result = ( *iter )->toPropGroup();
409 410 411
            // avoid concurrent locks in recursive calls (even if concurrent read locks are allowed)
            if ( lock )
            {
412 413
                // Temporarily disabled since locking causes several problems here :-/
                // accesObject->endRead();
414
            }
415 416 417 418 419 420 421
            return result;
        }
        if ( ( *iter )->getType() == PV_GROUP )
        {
            boost::shared_ptr< WPropertyBase > tmp = findPropertyRecursive( ( *iter )->toPropGroup(), name );
            if ( tmp )
            {
422 423 424
                // avoid concurrent locks in recursive calls (even if concurrent read locks are allowed)
                if ( lock )
                {
425 426
                    // Temporarily disabled since locking causes several problems here :-/
                    // accesObject->endRead();
427
                }
428 429 430 431
                return tmp->toPropGroup();
            }
        }
    }
432

433 434 435
     // avoid concurrent locks in recursive calls (even if concurrent read locks are allowed)
    if ( lock )
    {
436 437
        // Temporarily disabled since locking causes several problems here :-/
        // accesObject->endRead();
438
    }
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    return result;
}

void WQtConfigWidget::createLineAssociation()
{
    m_lineAssociationList.clear();
    // this property gets all lines associated that can't be associated with a property
    boost::shared_ptr< WProperties > noVar = boost::shared_ptr< WProperties >();

    // iterate over every line
    // if its a comment without an assignment or a section just add it to the "noVar"
    // otherwise add it the fitting Property from m_loadedProperties, make sure to traverse groups
    // also set the read value if its not commented

    std::string lastSection = std::string( "" );

    size_t i;
    for ( i = 0; i < m_configLines.size(); ++i )
    {
        if ( !WCfgOperations::isAssignment( m_configLines[i] ) &&
             !WCfgOperations::isCommentedAssignment( m_configLines[i] ) )
        {
            if ( WCfgOperations::isSection( m_configLines[i] ) )
            {
                lastSection = WCfgOperations::getSectionName( m_configLines[i] );
            }

            addLineToProperty( noVar, i );
        }
        else
        {
            std::string curLine = m_configLines[i];
            std::string propertyVal;
            bool commentedAssignment = WCfgOperations::isCommentedAssignment( curLine );
            if ( commentedAssignment )
            {
                curLine = WCfgOperations::uncommentLine( curLine );
            }
            std::string propertyNameInConfig;
            WCfgOperations::getAssignementComponents( curLine, &propertyNameInConfig, &propertyVal );
            std::string propertyName = lastSection + '.' + propertyNameInConfig;

            boost::shared_ptr< WPropertyBase > foundProperty = findPropertyRecursive( m_loadedProperties, propertyName );

            if ( !foundProperty )
            {
                addLineToProperty( noVar, i );
            }
            else
            {
                addLineToProperty( foundProperty->toPropGroup() , i );
                if ( !commentedAssignment )
                {
                    // set the variable
                    switch ( foundProperty->getType() )
                    {
                    case PV_BOOL:
                        {
                            if ( WCfgOperations::isBool( propertyVal ) )
                            {
                                foundProperty->toPropBool()->set( WCfgOperations::getAsBool( propertyVal ), true );
                            }
                            break;
                        }
                    case PV_INT:
                        {
                            if ( WCfgOperations::isInt( propertyVal ) )
                            {
                                foundProperty->toPropInt()->set( WCfgOperations::getAsInt( propertyVal ), true );
                            }
                            break;
                        }
                    case PV_DOUBLE:
                        {
                            if ( WCfgOperations::isDouble( propertyVal ) )
                            {
                                foundProperty->toPropDouble()->set( WCfgOperations::getAsDouble( propertyVal ), true );
                            }
                            break;
                        }
                    case PV_STRING:
                        {
                            if ( WCfgOperations::isString( propertyVal ) )
                            {
                                foundProperty->toPropString()->set( WCfgOperations::getAsString( propertyVal ), true );
                            }
                            break;
                        }
                    default:
                        {
                            break;
                        }
                    }
                }
            }
        }
    }
}

bool WQtConfigWidget::isPropertyEqual( boost::shared_ptr< WProperties > prop1, boost::shared_ptr< WProperties > prop2, std::string propName )
{
    boost::shared_ptr< WPropertyBase > found1 = findPropertyRecursive( prop1, propName );
    boost::shared_ptr< WPropertyBase > found2 = findPropertyRecursive( prop2, propName );

    bool result = false;

    if ( !( found1.get() && found2.get() ) )
    {
        return result;
    }

    switch ( found1->getType() )
    {
    case PV_BOOL:
        {
            result = found1->toPropBool()->get() == found2->toPropBool()->get();
            break;
        }
    case PV_DOUBLE:
        {
            result = found1->toPropDouble()->get() == found2->toPropDouble()->get();
            break;
        }
    case PV_INT:
        {
            result = found1->toPropInt()->get() == found2->toPropInt()->get();
            break;
        }
    case PV_STRING:
        {
            result = found1->toPropString()->get() == found2->toPropString()->get();
            break;
        }
    default:
        {
            result = false;
            break;
        }
    }

    return result;
}

void WQtConfigWidget::saveToConfig()
{
    // empty property pointer
    boost::shared_ptr< WProperties > noVar = boost::shared_ptr< WProperties >();

    // keeps track of the property names which got added to the out file
    std::vector< std::string > propertyWriten;

    // the lines of the config file to write
    std::vector< std::string > configOut;

    std::string current_section = std::string( "" );

    // keep track of the already written sections
    std::vector< std::string > sectionsWriten;

    // a little helper to associate lines to properties
    std::vector< boost::shared_ptr< WProperties > > propertyInLine;

    propertyInLine.resize( m_configLines.size() );

    // first transform into another representation
604
    LinePropertySet::iterator itr;
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    size_t i;
    for ( itr = m_lineAssociationList.begin(); itr != m_lineAssociationList.end(); ++itr )
    {
        for ( i = 0; i < itr->second.size(); ++i )
        {
            propertyInLine[ itr->second[i] ] = itr->first;
        }
    }

    // current line
    size_t cLine = 0;

    bool finished = false;

    // stack implementation to avoid recursion of property groups
    std::list< boost::shared_ptr< WProperties > > propertyStack;
    std::list< WProperties::PropertyIterator > iteratorStack;

    // to properly fulfill finished we need to make sure:
    //  - all lines from the config have been written
    //  - all registered sections have been written
    //  - all from default different properties have been written

    while ( !finished )
    {
        // not associated with a property, so either its a comment (without assignment), a section or an empty line
        if ( cLine < propertyInLine.size() )
        {
            if ( propertyInLine[cLine].get() == noVar.get() )
            {
                if ( WCfgOperations::isSection( m_configLines[cLine] ) )
                {
                    // check whether we switch from empty section or another section
                    if ( current_section != std::string( "" ) )
                    {
                        // find all so far unwritten none-default properties from the previous section
                        propertyStack.push_back( m_properties );

                        WProperties::PropertyIterator iter;
644
                        WProperties::PropertyAccessType accessObject = propertyStack.back()->getAccessObject();
645 646
                        // Temporarily disabled since locking causes several problems here :-/
                        // accessObject->beginRead();
647 648

                        iter = accessObject->get().begin();
649 650 651 652 653
                        iteratorStack.push_back( iter );

                        while ( !propertyStack.empty() )
                        {
                            // check if at the end of group, if so pop and continue
654
                            if ( iteratorStack.back() == propertyStack.back()->getAccessObject()->get().end() )
655 656 657 658 659 660 661 662 663 664 665
                            {
                                propertyStack.pop_back();
                                iteratorStack.pop_back();
                                continue;
                            }

                            // add group to stack, and move iterator
                            if ( ( *( iteratorStack.back() ) )->getType() == PV_GROUP )
                            {
                                propertyStack.push_back( ( *iteratorStack.back() )->toPropGroup() );
                                ++iteratorStack.back();
666
                                iteratorStack.push_back( propertyStack.back()->getAccessObject()->get().begin() );
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
                                continue;
                            }
                            else
                            {
                                // check the property and write it in case its valid
                                boost::shared_ptr< WProperties > prop = ( *( iteratorStack.back() ) )->toPropGroup();
                                std::string propName = prop->getName();
                                std::string sectionName = propName;
                                sectionName.erase( sectionName.find( '.' ), sectionName.length() - sectionName.find( '.' ) );
                                if ( sectionName == current_section && !isPropertyEqual( m_properties, m_defaultProperties, propName ) )
                                {
                                    size_t j;
                                    bool skip = false;
                                    for ( j = 0; j < m_skipPropertyWrite.size(); ++j )
                                    {
                                        if ( propName == m_skipPropertyWrite[j] )
                                        {
                                            skip = true;
                                            break;
                                        }
                                    }
                                    bool written = false;
                                    for ( j = 0; j < propertyWriten.size(); ++j )
                                    {
                                        if ( propName == propertyWriten[j] )
                                        {
                                            written = true;
                                            break;
                                        }
                                    }
                                    // only write the variable if its not already written, is not in the skip list
                                    // has the same section name as the section we just left and its value does not equal the default value
                                    if ( !skip && !written )
                                    {
701
                                        propertyWriten.push_back( propName );
702 703 704 705 706 707 708 709 710 711
                                        propName = propName.erase( 0, propName.find( '.' ) + 1 );
                                        configOut.push_back( propName + " = " + WCfgOperations::getPropValAsString( prop ) );
                                    }
                                }
                            } // END property is not a group

                            ++iteratorStack.back();
                        } // LOOP over properties

                        sectionsWriten.push_back( current_section );
712

713 714
                        // Temporarily disabled since locking causes several problems here :-/
                        // accessObject->endRead();
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
                    } // IF switch from none-empty section

                    std::string sn = WCfgOperations::getSectionName( m_configLines[cLine] );
                    if ( sn != std::string( "" ) )
                    {
                        current_section = sn;
                    }
                } // IF isSectionSwitch
                // if it was an empty section don't write the line
                if ( m_configLines[cLine] != std::string( "[]" ) )
                {
                    configOut.push_back( m_configLines[cLine] );
                }
                ++cLine;
                //continue;
            } // IF line not associated to property
            else
            {
                // -> line associated to property
                // though the line is associated we can assume the following:
                // - the line is in the correct section
                // - the property is not in the skip list
                boost::shared_ptr< WProperties > curProp = propertyInLine[cLine];
                std::string curPropName = curProp->getName();
                // get the same properties as the one we are at from default and currently set
                boost::shared_ptr< WProperties > propDefault = findPropertyRecursive( m_defaultProperties, curPropName )->toPropGroup();
                boost::shared_ptr< WProperties > propSet = findPropertyRecursive( m_properties, curPropName )->toPropGroup();
                std::string propDefaultValAsString = WCfgOperations::getPropValAsString( propDefault );
                std::string propSetValAsString = WCfgOperations::getPropValAsString( propSet );
                bool isLastLineOfProperty = false;
                // that cryptic line does the following:
                // m_lineAssociationList[ curProp ] = is the linelist which corresponds to the property we are at
                // m_lineAssociationList[ curProp ].size() - 1 = is the last index of the linelist
                // so if the last index, which must be at least 1 (because the line has to be associated at least once here),
                // is the same as the current line number, we are at the last line of the property
750 751 752

                // TODO(ledig): use this information to count the comments for a property
                // and therefore reduce them to a special count so the cfg file won't explode of comments
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
                if ( m_lineAssociationList[ curProp ][ m_lineAssociationList[ curProp ].size() - 1 ] == cLine )
                {
                    isLastLineOfProperty = true;
                }

                // to prevent multiple comments we just keep track of the current property
                bool isWritten = false;
                size_t j;
                for ( j = 0; j < propertyWriten.size(); ++j )
                {
                    if ( propertyWriten[j] == curPropName )
                    {
                        isWritten = true;
                        break;
                    }
                }

                // is comment:
                if ( WCfgOperations::isComment( m_configLines[cLine] ) )
                {
                    //  - comment val equals default -> keep as comment, but mark as written if the current value also is equal to default
                    //  - comment val equals current val, write it uncommented and mark as written
                    //  - otherwise just keep the commented line
                    std::string lineUncommented = WCfgOperations::uncommentLine( m_configLines[cLine] );
                    std::string propVal;
                    std::string dummy;
                    WCfgOperations::getAssignementComponents( lineUncommented, &dummy, &propVal );

                    if ( propVal == propDefaultValAsString )
                    {
                        configOut.push_back( m_configLines[cLine] );

                        if ( propDefaultValAsString == propSetValAsString && !isWritten )
                        {
                            propertyWriten.push_back( curPropName );
                            isWritten = true;
                        }
                    }
                    else
                    {
                        if ( propVal == propSetValAsString )
                        {
                            configOut.push_back( lineUncommented );
                            if ( !isWritten )
                            {
                                propertyWriten.push_back( curPropName );
                                isWritten = true;
                            }
                        }
                        else
                        {
                            configOut.push_back( m_configLines[cLine] );
                        }
                    }
                } // IF is comment
                else
                {
                    // is assignment:
                    //  - equal to default -> write as comment if not written
                    //  - equal to property value -> write and mark as written
                    //  - otherwise -> write as comment
                    // - has been last line associated to the property but not yet written
                    //  -> write if unequal to default and in both cases mark as written
                    std::string line = m_configLines[cLine];
                    std::string propVal;
                    std::string dummy;
                    WCfgOperations::getAssignementComponents( line, &dummy, &propVal );

                    if ( propVal == propDefaultValAsString )
                    {
                        if ( !isWritten )
                        {
                            configOut.push_back( "# " + line );
                            if ( propDefaultValAsString == propSetValAsString && !isWritten )
                            {
                                propertyWriten.push_back( curPropName );
                                isWritten = true;
                            }
                        }
                    }
                    else
                    {
                        if ( propVal == propSetValAsString )
                        {
                            configOut.push_back( line );
                            if ( !isWritten )
                            {
                                propertyWriten.push_back( curPropName );
                                isWritten = true;
                            }
                        }
                        else
                        {
                            configOut.push_back( "# " + line );
                        }
                    }
                    // if we didn't write the property so far, we will write it here, because we are at the only assignment line
                    if ( !isWritten )
                    {
                        std::string writePropName = curPropName;
                        writePropName = writePropName.erase( 0, writePropName.find( '.' ) + 1 );
                        configOut.push_back( writePropName + " = " + propSetValAsString );
                        propertyWriten.push_back( curPropName );
                    }
                } // end of assignment
                ++cLine;
            }
        } // IF not all lines written

        if ( cLine == propertyInLine.size() )
        {
            // at the end of the previous config file make sure
            // - we wrote all registered sections
            // - and if previous that we wrote all relevant properties, if so we are finished

            // could be accomplished with a little hack:
            // add all those sections not yet written and an empty section to the end
            // so that the section switch check will take all the work to write the leftover properties
            // -> here we just need to check that all sections are written at the end of the old config and we are finished

            size_t j, k;
            bool allSectionsWritten = true;
875
            for ( j = 0; j < m_registeredSections.size(); ++j )
876 877 878 879 880
            {
                // check if all registered sections are written
                bool sectionWritten = false;
                for ( k = 0; k < sectionsWriten.size(); ++k )
                {
881
                    if ( m_registeredSections[j] == sectionsWriten[k] )
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
                    {
                        sectionWritten = true;
                        break;
                    }
                }
                // is this sections not written?
                if ( !sectionWritten )
                {
                    allSectionsWritten = false;
                    // add the missing section and an empty section to the config file which will invoke flush of all unwritten
                    // properties for that section

                    // if current section is not yet registered just close it, otherwise force the
                    // section that didn't get written to flush
                    bool currentSectionWriten = false;
                    for ( k = 0; k < sectionsWriten.size(); ++k )
                    {
                        if ( sectionsWriten[k] == current_section )
                        {
                            currentSectionWriten = true;
                            break;
                        }
                    }

906
                    if ( currentSectionWriten || current_section == std::string( "" ) )
907
                    {
908
                        m_configLines.push_back( "[" + m_registeredSections[j] + "]" );
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
                        propertyInLine.push_back( noVar );
                    }
                    m_configLines.push_back( "[]" );
                    propertyInLine.push_back( noVar );
                    break;
                }
            }
            if ( allSectionsWritten )
            {
                finished = true;
            }
        }
    } // END write not finished loop

    WCfgOperations::writeCfg( "walnut.cfg", configOut );
}

void WQtConfigWidget::loadConfigFile()
{
928 929 930 931 932
    namespace fs = boost::filesystem;
    if ( fs::exists( fs::path( std::string( "walnut.cfg" ) ) ) )
    {
        m_configLines = WCfgOperations::readCfg( "walnut.cfg" );
    }
933
    // copy all default properties into the loaded properties
934
    m_loadedProperties = copyProperties( m_defaultProperties );
935 936 937 938
    // parse the config file
    createLineAssociation();
    // update the loaded properties
    updateGui( m_loadedProperties );
939
    // copy all loaded properties into the current properties where we create the gui from
940
    m_properties = copyProperties( m_loadedProperties );
941 942 943

    m_configState.setResetable( true, true );
    m_configState.add( m_propCondition );
ledig's avatar
ledig committed
944
    m_configState.add( m_shutdownFlag.getCondition() );
945 946 947 948 949 950 951 952 953 954 955 956
}

void WQtConfigWidget::updateGui( boost::shared_ptr< WProperties > properties )
{
    updatePropertyGroups( properties, "ge.bgColor", true );
    updatePropertyGroups( properties, "modules.whiteListGroup", true );
    updatePropertyGroups( properties, "modules.defaultList", true );
}

WQtDSBWidget *WQtConfigWidget::createTabForSection( boost::shared_ptr< WProperties > from, std::string sectionName )
{
    WProperties::PropertyAccessType accesObject = from->getAccessObject();
957 958
    // Temporarily disabled since locking causes several problems here :-/
    // accesObject->beginRead();
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

    std::string name = std::string( "" );
    if ( from->getType() == PV_GROUP )
    {
        name = from->getName();
    }

    WQtDSBWidget* tab = new WQtDSBWidget( name );

    WProperties::PropertyIterator iter;
    for ( iter = accesObject->get().begin(); iter != accesObject->get().end(); ++iter )
    {
        std::string propName = ( *iter )->getName();
        if ( propName.find( '.' ) == std::string::npos || ( *iter )->isHidden() )
        {
            continue;
        }
        std::string propSectionName = propName.erase( propName.find( '.' ), propName.length() - propName.find( '.' ) );
        if ( sectionName == propSectionName )
        {
            switch ( ( *iter )->getType() )
            {
            case PV_BOOL:
                tab->addProp( ( *iter )->toPropBool() );
                break;
            case PV_INT:
                tab->addProp( ( *iter )->toPropInt() );
                break;
            case PV_DOUBLE:
                tab->addProp( ( *iter )->toPropDouble() );
                break;
            case PV_STRING:
                tab->addProp( ( *iter )->toPropString() );
                break;
            case PV_PATH:
                tab->addProp( ( *iter )->toPropFilename() );
                break;
            case PV_SELECTION:
                WLogger::getLogger()->addLogMessage( "This property type \"PV_SELECTION\" is not yet supported by the GUI.", "DatasetBrowser",
                    LL_WARNING );
                break;
            case PV_COLOR:
                tab->addProp( ( *iter )->toPropColor() );
                break;
            case PV_POSITION:
                WLogger::getLogger()->addLogMessage( "This property type \"PV_POSITION\" is not yet supported by the GUI.", "DatasetBrowser",
                    LL_WARNING );
                break;
            case PV_TRIGGER:
                tab->addProp( ( *iter )->toPropTrigger() );
                break;
            case PV_GROUP:
                tab->addGroup( createTabForSection( ( *iter )->toPropGroup(), sectionName ), true );
                break;
            default:
                WLogger::getLogger()->addLogMessage( "This property type is not yet supported.", "DatasetBrowser", LL_WARNING );
                break;
            }
        }
    }

1020 1021
    // Temporarily disabled since locking causes several problems here :-/
    // accesObject->endRead();
1022

1023 1024 1025 1026 1027 1028 1029
    tab->addSpacer();
    return tab;
}

void WQtConfigWidget::createGuiFromProperties( boost::shared_ptr< WProperties > from )
{
    // iterate over all sections
1030
    for ( size_t i = 0; i < m_registeredSections.size(); ++i )
1031 1032 1033
    {
        // iterate over all properties and use those which are in this section

1034
        m_tabWidget->addTab( createTabForSection( from, m_registeredSections[i] ), QString::fromStdString( m_registeredSections[i] ) );
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    }
}

void WQtConfigWidget::initAndShow()
{
    loadConfigFile();
    // create the widgets and their connections in the corresponding tabs
    m_tabWidget->clear();

    createGuiFromProperties( m_properties );

1046
    this->show();
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

    run();
}

void WQtConfigWidget::threadMain()
{
    size_t i;
    while ( !m_shutdownFlag() )
    {
        m_configState.wait();

        if ( m_shutdownFlag() )
        {
            break;
        }

        // react on the changes of gui elements which influence other gui values
        if ( findPropertyRecursive( m_properties, "ge.bgColor.r" )->toPropDouble()->changed() ||
             findPropertyRecursive( m_properties, "ge.bgColor.g" )->toPropDouble()->changed() ||
             findPropertyRecursive( m_properties, "ge.bgColor.b" )->toPropDouble()->changed() )
        {
            updatePropertyGroups( m_properties, "ge.bgColor", true );
        }
        if ( findPropertyRecursive( m_properties, "ge.sColorWidget" )->toPropColor()->changed() )
        {
            updatePropertyGroups( m_properties, "ge.bgColor", false );
        }
        for ( i = 0; i < m_moduleNames.size(); ++i )
        {
            if ( findPropertyRecursive( m_properties, "modules.whitelist." + m_moduleNames[i] )->toPropBool()->changed() )
            {
                updatePropertyGroups( m_properties, "modules.whiteListGroup", false );
            }
            if ( findPropertyRecursive( m_properties, "modules.defaultList." + m_moduleNames[i] )->toPropBool()->changed() )
            {
                updatePropertyGroups( m_properties, "modules.defaultList", false );
            }
        }
    }
}

bool WQtConfigWidget::setWalnutFromProperties()
{
    bool result = false;

    boost::shared_ptr< WPropertyBase > propertyToSet, propertyLoaded;

    std::string propertyName = "ge.sColorWidget";
    propertyToSet = findPropertyRecursive( m_properties, propertyName );
    propertyLoaded = findPropertyRecursive( m_loadedProperties, propertyName );

    if ( propertyToSet->toPropColor()->get() != propertyLoaded->toPropColor()->get() )
    {
        // set the color

        result = true;
    }
    WColor color = propertyToSet->toPropColor()->get();

    boost::shared_ptr< WGEViewer > view;
    view = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
    if ( view )
    {
        view->setBgColor( color );
    }
    view = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "axial" );
    if ( view )
    {
        view->setBgColor( color );
    }
    view = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "coronal" );
    if ( view )
    {
        view->setBgColor( color );
    }
    view = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "sagittal" );
    if ( view )
    {
        view->setBgColor( color );
    }

    return result;
    // check if
}


void WQtConfigWidget::preview()
{
    m_previewed = setWalnutFromProperties();
}

void WQtConfigWidget::resetToDefaults()
{
    // load the default value of every property and display it
    copyPropertiesContents( m_defaultProperties, m_properties );
    updateGui( m_properties );

    //
    if ( m_previewed )
    {
        // reset the preview
        setWalnutFromProperties();

        // this is also some sort of preview because when we cancel we want the loaded config still active
        //m_previewed = false;
    }
}

void WQtConfigWidget::resetToBackup()
{
    // get the backup of the loaded file and show its values
    copyPropertiesContents( m_loadedProperties, m_properties );
    updateGui( m_properties );

    if ( m_previewed )
    {
        // reset the preview
        setWalnutFromProperties();
        m_previewed = false;
    }
}

void WQtConfigWidget::save()
{
    // save the config changes
    // and use every trigger that can be executed
    saveToConfig();
    setWalnutFromProperties();
1175
    this->hide();
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    requestStop();
}

void WQtConfigWidget::cancel()
{
    // reset to the loaded state
    if ( m_previewed )
    {
        copyPropertiesContents( m_loadedProperties, m_properties );

        // reset the preview
        setWalnutFromProperties();
        m_previewed = false;
    }

    // and hide the window
1192
    this->hide();
1193 1194 1195 1196 1197 1198 1199 1200 1201

    requestStop();
}

void WQtConfigWidget::closeEvent( QCloseEvent *event )
{
    emit closeWidget();
    event->accept();
}