WQtDatasetBrowser.cpp 26.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
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

25
#include <iostream>
26
#include <map>
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
27
#include <set>
28
#include <string>
29
#include <vector>
30

31
#include <QtCore/QList>
32
#include <QtGui/QScrollArea>
schurade's avatar
schurade committed
33
#include <QtGui/QShortcut>
34
#include <QtGui/QMenu>
35

36
#include "../../../common/WLogger.h"
37
#include "../../../common/WPreferences.h"
38

39 40
#include "../../../dataHandler/WDataSet.h"

41 42 43
#include "../../../kernel/WModuleFactory.h"
#include "../../../kernel/WKernel.h"
#include "../../../modules/data/WMData.h"
44
#include "../events/WModuleAssocEvent.h"
schurade's avatar
schurade committed
45
#include "../events/WRoiAssocEvent.h"
46
#include "../events/WRoiRemoveEvent.h"
47
#include "../events/WModuleReadyEvent.h"
48
#include "../events/WModuleDeleteEvent.h"
49
#include "../events/WEventTypes.h"
50
#include "../guiElements/WQtApplyModulePushButton.h"
51
#include "../WMainWindow.h"
52
#include "WQtNumberEdit.h"
53
#include "WQtNumberEditDouble.h"
54
#include "WQtTextureSorter.h"
schurade's avatar
schurade committed
55
#include "WQtBranchTreeItem.h"
56

57
#include "WQtDatasetBrowser.h"
58

59
WQtDatasetBrowser::WQtDatasetBrowser( WMainWindow* parent )
60
    : QDockWidget( "Dataset Browser", parent )
61
{
62 63
    m_mainWindow = parent;

64
    m_panel = new QWidget( this );
65
    m_moduleTreeWidget = new WQtTreeWidget( m_panel );
66
    m_moduleTreeWidget->setContextMenuPolicy( Qt::ActionsContextMenu );
67

68 69 70 71 72 73
    m_moduleTreeWidget->setHeaderLabel( QString( "Dataset Browser" ) );
    m_moduleTreeWidget->setDragEnabled( true );
    m_moduleTreeWidget->viewport()->setAcceptDrops( true );
    m_moduleTreeWidget->setDropIndicatorShown( true );
    m_moduleTreeWidget->setDragDropMode( QAbstractItemView::InternalMove );
    m_moduleTreeWidget->setMinimumHeight( 250 );
74

75 76 77 78 79
    // create context menu for tree items
    QAction* deleteModuleAction = new QAction( "Remove Module", m_moduleTreeWidget );
    connect( deleteModuleAction, SIGNAL( triggered() ), this, SLOT( deleteModuleTreeItem() ) );
    m_moduleTreeWidget->addAction( deleteModuleAction );

80 81 82
    m_textureSorter = new WQtTextureSorter( m_panel );
    m_textureSorter->setToolTip( "Reorder the textures." );

83
    m_tabWidget = new QTabWidget( m_panel );
84
    m_tabWidget2 = new QTabWidget( m_panel );
schurade's avatar
schurade committed
85
    m_tabWidget->setMinimumHeight( 220 );
86 87

    m_layout = new QVBoxLayout();
88
    m_layout->addWidget( m_moduleTreeWidget );
89 90
    m_layout->addWidget( m_tabWidget2 );

Alexander Wiebel's avatar
Alexander Wiebel committed
91
    m_tabWidget2->addTab( m_textureSorter, QString( "Texture Sorter" ) );
92 93 94 95 96 97 98 99

    m_roiTreeWidget = new WQtTreeWidget();
    m_roiTreeWidget->setHeaderLabel( QString( "ROIs" ) );
    m_roiTreeWidget->setHeaderHidden( true );
    m_roiTreeWidget->setDragEnabled( true );
    m_roiTreeWidget->viewport()->setAcceptDrops( true );
    m_roiTreeWidget->setDropIndicatorShown( true );
    m_roiTreeWidget->setDragDropMode( QAbstractItemView::InternalMove );
Alexander Wiebel's avatar
Alexander Wiebel committed
100
    m_tabWidget2->addTab( m_roiTreeWidget, QString( "ROIs" ) );
101 102


103 104 105 106 107 108 109
    m_layout->addWidget( m_tabWidget );

    m_panel->setLayout( m_layout );

    this->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
    this->setFeatures( QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable );
    this->setWidget( m_panel );
110

111
    m_tiModules = new WQtModuleHeaderTreeItem( m_moduleTreeWidget );
schurade's avatar
schurade committed
112
    m_tiModules->setText( 0, QString( "Modules" ) );
113
    m_tiRois = new WQtRoiHeaderTreeItem( m_roiTreeWidget );
Alexander Wiebel's avatar
Alexander Wiebel committed
114
    m_tiRois->setText( 0, QString( "ROIs" ) );
115

116
    connectSlots();
117 118 119 120

    // preset for toolbar text.
    m_showToolBarText = true;
    WPreferences::getPreference( "qt4gui.toolBarIconText", &m_showToolBarText );
schurade's avatar
schurade committed
121

122 123 124 125 126
    // These modules will be allowed to be shown.
    std::string moduleWhiteList;
    WPreferences::getPreference( "modules.whiteList", &moduleWhiteList );
    m_moduleWhiteList = string_utils::tokenize( moduleWhiteList, "," );

127
    QShortcut* shortcut = new QShortcut( QKeySequence( Qt::Key_Delete ), m_roiTreeWidget );
128 129 130
    connect( shortcut, SIGNAL( activated() ), this, SLOT( deleteROITreeItem() ) );
    QShortcut* shortcutDeleteModules = new QShortcut( QKeySequence( Qt::Key_Backspace ), m_moduleTreeWidget );
    connect( shortcutDeleteModules, SIGNAL( activated() ), this, SLOT( deleteModuleTreeItem() ) );
131 132 133 134 135 136 137
}

WQtDatasetBrowser::~WQtDatasetBrowser()
{
}


138 139
void WQtDatasetBrowser::connectSlots()
{
140 141
    connect( m_moduleTreeWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( selectTreeItem() ) );
    connect( m_moduleTreeWidget, SIGNAL( itemClicked( QTreeWidgetItem*, int ) ), this, SLOT( changeTreeItem() ) );
schurade's avatar
schurade committed
142 143
    connect( m_roiTreeWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( selectRoiTreeItem() ) );
    connect( m_roiTreeWidget, SIGNAL( itemClicked( QTreeWidgetItem*, int ) ), this, SLOT( changeRoiTreeItem() ) );
144 145 146
}


147 148
WQtSubjectTreeItem* WQtDatasetBrowser::addSubject( std::string name )
{
149
    WQtSubjectTreeItem* subject = new WQtSubjectTreeItem( m_moduleTreeWidget );
150 151 152 153 154
    subject->setText( 0, QString( name.c_str() ) );

    return subject;
}

155 156
bool WQtDatasetBrowser::event( QEvent* event )
{
157 158 159 160 161 162
    // a subject singals a newly registered data set
    if ( event->type() == WQT_UPDATE_TEXTURE_SORTER_EVENT )
    {
        m_textureSorter->update();
    }

163 164 165 166
    // a module got associated with the root container -> add it to the list
    if ( event->type() == WQT_ASSOC_EVENT )
    {
        // convert event to assoc event
schurade's avatar
schurade committed
167 168
        WModuleAssocEvent* e1 = dynamic_cast< WModuleAssocEvent* >( event );     // NOLINT
        if ( e1 )
169
        {
schurade's avatar
schurade committed
170 171
            WLogger::getLogger()->addLogMessage( "Inserting module " + e1->getModule()->getName() + " to dataset browser.",
                                                 "DatasetBrowser", LL_DEBUG );
172

schurade's avatar
schurade committed
173 174 175 176 177 178 179 180 181 182
            // finally add the module
            // TODO(schurade): is this differentiation between data and "normal" modules really needed?
            if ( boost::shared_dynamic_cast< WMData >( e1->getModule() ).get() )
            {
                addDataset( e1->getModule(), 0 );
            }
            else
            {
                addModule( e1->getModule() );
            }
183
        }
schurade's avatar
schurade committed
184 185 186 187 188 189
        return true;
    }
    if ( event->type() == WQT_ROI_ASSOC_EVENT)
    {
        WRoiAssocEvent* e2 = dynamic_cast< WRoiAssocEvent* >( event );     // NOLINT
        if ( e2 )
190
        {
schurade's avatar
schurade committed
191 192
            addRoi( e2->getRoi() );
            WLogger::getLogger()->addLogMessage( "Inserting roi to dataset browser.", "DatasetBrowser", LL_DEBUG );
193 194 195 196
        }

        return true;
    }
197 198 199 200 201 202 203 204 205 206 207
    if( event->type() == WQT_ROI_REMOVE_EVENT )
    {
        WRoiRemoveEvent* e3 = dynamic_cast< WRoiRemoveEvent* >( event );
        if( e3 )
        {
            removeRoi( e3->getRoi() );
            WLogger::getLogger()->addLogMessage( "Removing roi from dataset browser.", "DatasetBrowser", LL_DEBUG );
        }

        return true;
    }
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    // a module tree item should be deleted
    if ( event->type() == WQT_MODULE_REMOVE_EVENT )
    {
        WModuleDeleteEvent* e = dynamic_cast< WModuleDeleteEvent* >( event );     // NOLINT
        if ( !e )
        {
            // this should never happen, since the type is set to WQT_Ready_EVENT.
            WLogger::getLogger()->addLogMessage( "Event is not an WModuleRemoveEvent although its type claims it. Ignoring event.",
                                                 "DatasetBrowser", LL_WARNING );
            return true;
        }

        // grab the module reference and print some info
        boost::shared_ptr< WModule > module = e->getTreeItem()->getModule();
        WLogger::getLogger()->addLogMessage( "Removing module \"" + module->getName() + "\" from Tree.", "DatasetBrowser", LL_DEBUG );

        // remove it from the tree and free last ref count
        m_moduleTreeWidget->deleteItem( e->getTreeItem() );

        // ref count != 1?
        if ( module.use_count() != 1 )
        {
            wlog::warn( "DatasetBrowser" ) << "Removed module has strange usage count: " << module.use_count() << ". Should be 1 here. " <<
                                              "Module reference is held by someone else.";
        }

        return true;
    }

238 239 240 241 242 243 244 245
    // a module changed its state to "ready" -> activate it in dataset browser
    if ( event->type() == WQT_READY_EVENT )
    {
        // convert event to assoc event
        WModuleReadyEvent* e = dynamic_cast< WModuleReadyEvent* >( event );     // NOLINT
        if ( !e )
        {
            // this should never happen, since the type is set to WQT_Ready_EVENT.
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
246 247
            WLogger::getLogger()->addLogMessage( "Event is not an WModueReadyEvent although its type claims it. Ignoring event.",
                                                 "DatasetBrowser", LL_WARNING );
248 249

            return true;
250 251
        }

Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
252 253
        WLogger::getLogger()->addLogMessage( "Activating module " + e->getModule()->getName() + " in dataset browser.",
                                             "DatasetBrowser", LL_DEBUG );
254 255

        // iterate tree items and find proper one
256
        QTreeWidgetItemIterator it( m_moduleTreeWidget );
257 258
        while ( *it )
        {
259
            WQtTreeItem* item = dynamic_cast< WQtTreeItem* >( *it );
260
            boost::shared_ptr< WModule > module = boost::shared_ptr< WModule >();
261
            if ( item )
262
            {
263
                module = item->getModule();
264 265 266 267 268 269 270 271 272 273 274 275 276
            }

            // if the pointer is NULL the item was none of the above
            if ( !module.get() )
            {
                ++it;
                continue;
            }

            // we found it
            if ( e->getModule() == module )
            {
                // activate it
277
                item->setDisabled( false );
278
                selectTreeItem();
279

280 281
                // if the type number is 1 (dataset item) emit change event
                if ( item->type() == 1 )
282 283 284
                {
                    emit dataSetBrowserEvent( QString( "dataSetAdded" ), true );
                }
285 286 287 288
            }

            ++it;
        }
289 290

        return true;
291 292
    }

293 294
    return QDockWidget::event( event );
}
295

296
WQtDatasetTreeItem* WQtDatasetBrowser::addDataset( boost::shared_ptr< WModule > module, int subjectId )
297
{
schurade's avatar
schurade committed
298
    int c = getFirstSubject();
299
    WQtSubjectTreeItem* subject = static_cast< WQtSubjectTreeItem* >( m_moduleTreeWidget->topLevelItem( subjectId + c ) );
schurade's avatar
schurade committed
300
    subject->setExpanded( true );
301
    WQtDatasetTreeItem* item = subject->addDatasetItem( module );
302
    m_moduleTreeWidget->setCurrentItem( item );
303
    item->setDisabled( true );
304
    return item;
305
}
306

307 308
WQtModuleTreeItem* WQtDatasetBrowser::addModule( boost::shared_ptr< WModule > module )
{
schurade's avatar
schurade committed
309 310
    m_tiModules->setExpanded( true );
    WQtModuleTreeItem* item = m_tiModules->addModuleItem( module );
311
    m_moduleTreeWidget->setCurrentItem( item );
312 313
    item->setDisabled( true );
    return item;
314 315
}

schurade's avatar
schurade committed
316 317
void WQtDatasetBrowser::addRoi( boost::shared_ptr< WRMROIRepresentation > roi )
{
318 319 320 321 322
    WQtRoiTreeItem* newItem;
    WQtBranchTreeItem* branchItem;

    m_tiRois->setExpanded( true );

323
    if ( m_roiTreeWidget->selectedItems().count() != 0 )
schurade's avatar
schurade committed
324
    {
325
        switch ( m_roiTreeWidget->selectedItems().at( 0 )->type() )
schurade's avatar
schurade committed
326
        {
327
            case ROI :
schurade's avatar
schurade committed
328
            {
329
                branchItem =( static_cast< WQtBranchTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 )->parent() ) );
schurade's avatar
schurade committed
330 331
                break;
            }
schurade's avatar
schurade committed
332
            case ROIBRANCH :
schurade's avatar
schurade committed
333
            {
334
                branchItem =( static_cast< WQtBranchTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 ) ) );
schurade's avatar
schurade committed
335 336 337 338
                break;
            }
            default:
            {
339
                branchItem = m_tiRois->addBranch( roi->getBranch() );
schurade's avatar
schurade committed
340 341 342 343 344 345
                break;
            }
        }
    }
    else
    {
346
        branchItem = m_tiRois->addBranch( roi->getBranch() );
schurade's avatar
schurade committed
347
    }
348 349 350 351 352 353

    m_tabWidget2->setCurrentIndex( m_tabWidget2->indexOf( m_roiTreeWidget ) );
    branchItem->setExpanded( true );
    newItem = branchItem->addRoiItem( roi );
    newItem->setDisabled( false );
    newItem->setSelected( true );
schurade's avatar
schurade committed
354 355
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
void WQtDatasetBrowser::removeRoi( boost::shared_ptr< WRMROIRepresentation > roi )
{
    for( int branchID = 0; branchID < m_tiRois->childCount(); ++branchID )
    {
        QTreeWidgetItem* branchTreeItem = m_tiRois->child( branchID );
        for( int roiID = 0; roiID < branchTreeItem->childCount(); ++roiID )
        {
            WQtRoiTreeItem* roiTreeItem = dynamic_cast< WQtRoiTreeItem* >( branchTreeItem->child( roiID ) );
            if( roiTreeItem && roiTreeItem->getRoi() == roi )
            {
                delete roiTreeItem;

                if( branchTreeItem->childCount() == 0 )
                {
                    delete branchTreeItem;
                }

                break;
            }
        }
    }
}

379 380
boost::shared_ptr< WModule > WQtDatasetBrowser::getSelectedModule()
{
381
    if ( m_moduleTreeWidget->selectedItems().at( 0 )->type() == 1 )
382
    {
383
        return ( static_cast< WQtDatasetTreeItem* >( m_moduleTreeWidget->selectedItems().at( 0 ) )->getModule() );
384
    }
385
    else if ( m_moduleTreeWidget->selectedItems().at( 0 )->type() == 3 )
386
    {
387
        return ( static_cast< WQtModuleTreeItem* >( m_moduleTreeWidget->selectedItems().at( 0 ) )->getModule() );
388 389 390 391
    }

    return boost::shared_ptr< WModule >();
}
392

393 394
void WQtDatasetBrowser::selectTreeItem()
{
395 396
    // TODO(schurade): qt doc says clear() doesn't delete tabs so this is possibly a memory leak
    m_tabWidget->clear();
397
    m_mainWindow->getCompatiblesToolBar()->clearButtons();
398

399
    boost::shared_ptr< WModule > module;
400
    boost::shared_ptr< WProperties > props;
401
    boost::shared_ptr< WProperties > infoProps;
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
402

403
    if ( m_moduleTreeWidget->selectedItems().size() != 0  )
404
    {
405
        switch ( m_moduleTreeWidget->selectedItems().at( 0 )->type() )
schurade's avatar
schurade committed
406
        {
407
            case SUBJECT:
408 409 410 411 412
            case MODULEHEADER:
                // Here we just take a prototype module with no output connectors
                // to get the modules with no input connector.
                module = WModuleFactory::getModuleFactory()->getPrototypeByName( "HUD" );
                createCompatibleButtons( module );
schurade's avatar
schurade committed
413
                break;
414
            case DATASET:
415
                module = ( static_cast< WQtDatasetTreeItem* >( m_moduleTreeWidget->selectedItems().at( 0 ) ) )->getModule();
416
                props = module->getProperties();
417
                infoProps = module->getInformationProperties();
schurade's avatar
schurade committed
418 419
                createCompatibleButtons( module );
                break;
420
            case MODULE:
421
                module = ( static_cast< WQtModuleTreeItem* >( m_moduleTreeWidget->selectedItems().at( 0 ) ) )->getModule();
422
                props = module->getProperties();
423
                infoProps = module->getInformationProperties();
424 425
                createCompatibleButtons( module );
                break;
426 427 428 429 430 431 432 433
            case ROIHEADER:
            case ROIBRANCH:
            case ROI:
                break;
            default:
                break;
        }
    }
434
    buildPropTab( props, infoProps );
435 436 437 438 439 440 441 442 443
}

void WQtDatasetBrowser::selectRoiTreeItem()
{
    // TODO(schurade): qt doc says clear() doesn't delete tabs so this is possibly a memory leak
    m_tabWidget->clear();
    m_mainWindow->getCompatiblesToolBar()->clearButtons();

    boost::shared_ptr< WModule > module;
444
    boost::shared_ptr< WProperties > props;
445 446 447 448 449 450 451 452 453

    if ( m_roiTreeWidget->selectedItems().size() != 0  )
    {
        switch ( m_roiTreeWidget->selectedItems().at( 0 )->type() )
        {
            case SUBJECT:
            case DATASET:
            case MODULEHEADER:
            case MODULE:
454
            case ROIHEADER:
schurade's avatar
schurade committed
455
                break;
schurade's avatar
schurade committed
456
            case ROIBRANCH:
457
                props = ( static_cast< WQtBranchTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 ) ) )->getBranch()->getProperties();
schurade's avatar
schurade committed
458
                break;
459
            case ROI:
460
                props = ( static_cast< WQtRoiTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 ) ) )->getRoi()->getProperties();
schurade's avatar
schurade committed
461 462 463 464
                break;
            default:
                break;
        }
465
    }
466
    WKernel::getRunningKernel()->getRoiManager()->setSelectedRoi( getFirstRoiInSelectedBranch() );
467
    buildPropTab( props, boost::shared_ptr< WProperties >() );
468
}
469

470
WQtDSBWidget*  WQtDatasetBrowser::buildPropWidget( boost::shared_ptr< WProperties > props )
471
{
472
    WQtDSBWidget* tab = new WQtDSBWidget( props->getName() );
473 474

    if ( props.get() )
475
    {
476
        WProperties::PropertyAccessType propAccess = props->getAccessObject();
477 478
        propAccess->beginRead();

479 480
        tab->setName( QString::fromStdString( props->getName() ) );

481
        // iterate all properties. This Locks the properties set -> use endIteration to unlock
482
        for ( WProperties::PropertyConstIterator iter = propAccess->get().begin(); iter != propAccess->get().end(); ++iter )
483
        {
484
            if ( !( *iter )->isHidden() )
485
            {
486
                switch ( ( *iter )->getType() )
487
                {
488
                    case PV_BOOL:
489
                        tab->addProp( ( *iter )->toPropBool() );
490 491
                        break;
                    case PV_INT:
492
                        tab->addProp( ( *iter )->toPropInt() );
493 494
                        break;
                    case PV_DOUBLE:
495
                        tab->addProp( ( *iter )->toPropDouble() );
496 497
                        break;
                    case PV_STRING:
498
                        tab->addProp( ( *iter )->toPropString() );
499 500
                        break;
                    case PV_PATH:
501
                        tab->addProp( ( *iter )->toPropFilename() );
502
                        break;
503 504
                    case PV_SELECTION:
                        WLogger::getLogger()->addLogMessage( "This property type \"PV_SELECTION\" is not yet supported by the GUI.", "DatasetBrowser",
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
505 506 507
                                LL_WARNING );
                        break;
                    case PV_COLOR:
508
                        tab->addProp( ( *iter )->toPropColor() );
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
509 510 511 512
                        break;
                    case PV_POSITION:
                        WLogger::getLogger()->addLogMessage( "This property type \"PV_POSITION\" is not yet supported by the GUI.", "DatasetBrowser",
                                LL_WARNING );
513
                        break;
514 515 516
                    case PV_TRIGGER:
                        tab->addProp( ( *iter )->toPropTrigger() );
                        break;
517 518 519
                    case PV_GROUP:
                        tab->addGroup( buildPropWidget( ( *iter )->toPropGroup() ) );
                        break;
520 521 522
                    default:
                        WLogger::getLogger()->addLogMessage( "This property type is not yet supported.", "DatasetBrowser", LL_WARNING );
                        break;
523
                }
524 525
            }
        }
526
        propAccess->endRead();
527
    }
528

529
    tab->addSpacer();
530 531 532
    return tab;
}

533
void WQtDatasetBrowser::buildPropTab( boost::shared_ptr< WProperties > props, boost::shared_ptr< WProperties > infoProps )
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
    WQtDSBWidget* tab = NULL;
    WQtDSBWidget* infoTab = NULL;
    if ( props )
    {
        tab = buildPropWidget( props );
        tab->setName( "Settings" );
    }
    if ( infoProps )
    {
        infoTab = buildPropWidget( infoProps );
        infoTab->setName( "Information" );
    }

    int infoIdx = addTabWidgetContent( infoTab );
    int propIdx = addTabWidgetContent( tab );

    // select the property widget preferably
    if ( propIdx != -1 )
    {
        m_tabWidget->setCurrentIndex( propIdx );
    }
    else if ( infoIdx != -1 )
    {
        m_tabWidget->setCurrentIndex( infoIdx );
    }
560 561
}

schurade's avatar
schurade committed
562 563 564
void WQtDatasetBrowser::createCompatibleButtons( boost::shared_ptr< WModule >module )
{
    // every module may have compatibles: create ribbon menu entry
565
    std::vector< boost::shared_ptr< WApplyPrototypeCombiner > > comps = WModuleFactory::getModuleFactory()->getCompatiblePrototypes( module );
566

567
    for ( std::vector< boost::shared_ptr< WApplyPrototypeCombiner > >::const_iterator iter = comps.begin(); iter != comps.end(); ++iter )
schurade's avatar
schurade committed
568
    {
569 570
        if( !m_moduleWhiteList.empty() )
        {
571
            const std::string tmpName = ( *iter )->getTargetPrototype()->getName();
572 573
            if( std::find( m_moduleWhiteList.begin(), m_moduleWhiteList.end(), tmpName ) == m_moduleWhiteList.end() )
            {
574
                continue; // do nothing for modules that are not in white list
575 576 577
            }
        }

578 579 580 581
        WQtApplyModulePushButton* button = new WQtApplyModulePushButton( m_mainWindow->getCompatiblesToolBar(), m_mainWindow->getIconManager(),
                                                                         *iter, m_showToolBarText
        );
        m_mainWindow->getCompatiblesToolBar()->addWidget( button );
schurade's avatar
schurade committed
582 583 584
    }
}

585 586
void WQtDatasetBrowser::changeTreeItem()
{
587
    if ( m_moduleTreeWidget->selectedItems().size() == 1 && m_moduleTreeWidget->selectedItems().at( 0 )->type() == DATASET )
588
    {
589 590
        boost::shared_ptr< WModule > module =( static_cast< WQtDatasetTreeItem* >( m_moduleTreeWidget->selectedItems().at( 0 ) ) )->getModule();
        module->getProperties()->getProperty( "active" )->toPropBool()->set( m_moduleTreeWidget->selectedItems().at( 0 )->checkState( 0 ) );
591
    }
592
    else if ( m_moduleTreeWidget->selectedItems().size() == 1 && m_moduleTreeWidget->selectedItems().at( 0 )->type() == MODULE )
593
    {
594
        boost::shared_ptr< WModule > module =( static_cast< WQtModuleTreeItem* >( m_moduleTreeWidget->selectedItems().at( 0 ) ) )->getModule();
595

596
        module->getProperties()->getProperty( "active" )->toPropBool()->set( m_moduleTreeWidget->selectedItems().at( 0 )->checkState( 0 ) );
597 598 599
    }
}

schurade's avatar
schurade committed
600 601 602 603 604 605 606 607 608
void WQtDatasetBrowser::changeRoiTreeItem()
{
    if ( m_roiTreeWidget->selectedItems().size() == 1 && m_roiTreeWidget->selectedItems().at( 0 )->type() == ROI )
    {
        boost::shared_ptr< WRMROIRepresentation > roi =( static_cast< WQtRoiTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 ) ) )->getRoi();
        roi->getProperties()->getProperty( "active" )->toPropBool()->set( m_roiTreeWidget->selectedItems().at( 0 )->checkState( 0 ) );
    }
}

609
int WQtDatasetBrowser::addTabWidgetContent( WQtDSBWidget* content )
610
{
611
    if ( !content || content->isEmpty() )
612 613 614 615
    {
        return -1;
    }

616 617 618 619
    QScrollArea* sa = new QScrollArea();
    sa->setWidget( content );
    sa->setWidgetResizable( true );

620
    return m_tabWidget->addTab( sa, content->getName() );
621
}
622

623 624
void WQtDatasetBrowser::moveTreeItemDown()
{
625
    m_moduleTreeWidget->moveTreeItemDown();
626 627 628 629
}

void WQtDatasetBrowser::moveTreeItemUp()
{
630
    m_moduleTreeWidget->moveTreeItemUp();
631 632
}

schurade's avatar
schurade committed
633 634 635
int WQtDatasetBrowser::getFirstSubject()
{
    int c = 0;
636
    for ( int i = 0; i < m_moduleTreeWidget->topLevelItemCount() ; ++i )
schurade's avatar
schurade committed
637
    {
638
        if ( m_moduleTreeWidget->topLevelItem( i )->type() == SUBJECT )
schurade's avatar
schurade committed
639 640 641 642 643 644 645 646 647 648 649
        {
            break;
        }
        ++c;
    }
    return c;
}

boost::shared_ptr< WRMROIRepresentation > WQtDatasetBrowser::getSelectedRoi()
{
    boost::shared_ptr< WRMROIRepresentation >roi;
650
    if ( m_roiTreeWidget->selectedItems().count() == 0 )
schurade's avatar
schurade committed
651 652 653
    {
        return roi;
    }
654
    if ( m_roiTreeWidget->selectedItems().at( 0 )->type() == ROI )
schurade's avatar
schurade committed
655
    {
656
        roi =( static_cast< WQtRoiTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 ) ) )->getRoi();
schurade's avatar
schurade committed
657
    }
schurade's avatar
schurade committed
658 659 660 661 662 663
    return roi;
}

boost::shared_ptr< WRMROIRepresentation > WQtDatasetBrowser::getFirstRoiInSelectedBranch()
{
    boost::shared_ptr< WRMROIRepresentation >roi;
664
    if ( m_roiTreeWidget->selectedItems().count() == 0 )
schurade's avatar
schurade committed
665
    {
schurade's avatar
schurade committed
666 667
        return roi;
    }
668
    if ( m_roiTreeWidget->selectedItems().at( 0 )->type() == ROI )
schurade's avatar
schurade committed
669
    {
670
        WQtBranchTreeItem* branch = ( static_cast< WQtBranchTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 )->parent() ) );
schurade's avatar
schurade committed
671 672
        roi =( static_cast< WQtRoiTreeItem* >( branch->child( 0 ) ) )->getRoi();
    }
673
    if ( m_roiTreeWidget->selectedItems().at( 0 )->type() == ROIBRANCH )
schurade's avatar
schurade committed
674
    {
675
        WQtBranchTreeItem* branch = ( static_cast< WQtBranchTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 ) ) );
schurade's avatar
schurade committed
676 677 678 679
        if ( branch->childCount() > 0 )
        {
            roi =( static_cast< WQtRoiTreeItem* >( branch->child( 0 ) ) )->getRoi();
        }
schurade's avatar
schurade committed
680 681 682
    }
    return roi;
}
schurade's avatar
schurade committed
683

684
void WQtDatasetBrowser::deleteModuleTreeItem()
schurade's avatar
schurade committed
685
{
686 687
    if ( m_moduleTreeWidget->selectedItems().count() > 0 )
    {
688 689 690 691 692 693 694 695
        if ( ( m_moduleTreeWidget->selectedItems().at( 0 )->type() == MODULE ) ||
             ( m_moduleTreeWidget->selectedItems().at( 0 )->type() == DATASET ) )
        {
            // instead of deleting the tree item directly -> inform the tree item and let it do the job:
            static_cast< WQtTreeItem* >( m_moduleTreeWidget->selectedItems().at( 0 ) )->deleteSelf();
            // select another item
            m_moduleTreeWidget->setCurrentItem( m_moduleTreeWidget->topLevelItem( 0 ) );
        }
696 697 698 699 700 701
    }
}

void WQtDatasetBrowser::deleteROITreeItem()
{
    boost::shared_ptr< WRMROIRepresentation >roi;
702
    if ( m_roiTreeWidget->selectedItems().count() > 0 )
schurade's avatar
schurade committed
703
    {
704
        if ( m_roiTreeWidget->selectedItems().at( 0 )->type() == ROIBRANCH )
schurade's avatar
schurade committed
705
        {
706 707 708 709 710
            roi = getFirstRoiInSelectedBranch();
            if ( roi )
            {
                WKernel::getRunningKernel()->getRoiManager()->removeBranch( roi );
            }
711
            delete m_roiTreeWidget->selectedItems().at( 0 );
schurade's avatar
schurade committed
712
        }
713 714

        else if ( m_roiTreeWidget->selectedItems().at( 0 )->type() == ROI )
schurade's avatar
schurade committed
715
        {
716 717 718 719
            roi =( static_cast< WQtRoiTreeItem* >( m_roiTreeWidget->selectedItems().at( 0 ) ) )->getRoi();
            if ( roi )
            {
                WKernel::getRunningKernel()->getRoiManager()->removeRoi( roi );
720
                // Removing the roi from the tree widget is also done by WROIManagerFibers::removeRoi().
721
            }
schurade's avatar
schurade committed
722 723
        }
    }
724
    WKernel::getRunningKernel()->getRoiManager()->setSelectedRoi( getFirstRoiInSelectedBranch() );
schurade's avatar
schurade committed
725
}