WQtDatasetBrowser.cpp 10.4 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>
27
#include <string>
28
#include <vector>
29

30 31
#include <QtCore/QList>

32
#include "../../../common/WLogger.h"
33

34 35
#include "../../../dataHandler/WDataSet.h"

36
#include "WQtDatasetBrowser.h"
37
#include "WQtNumberEdit.h"
38
#include "WQtCheckBox.h"
39
#include "WQtModuleHeaderTreeItem.h"
40

41 42
#include "../WMainWindow.h"

43 44 45
#include "../../../modules/data/WMData.h"


46
WQtDatasetBrowser::WQtDatasetBrowser( WMainWindow* parent )
47 48
    : QDockWidget( parent )
{
49 50
    m_mainWindow = parent;

51
    m_panel = new QWidget( this );
52
    m_treeWidget = new WQtTreeWidget( m_panel );
53 54

    m_treeWidget->setHeaderLabel( QString( "Dataset Browser" ) );
55 56 57 58 59
    m_treeWidget->setDragEnabled( true );
    m_treeWidget->viewport()->setAcceptDrops( true );
    m_treeWidget->setDropIndicatorShown( true );
    m_treeWidget->setDragDropMode( QAbstractItemView::InternalMove );

60 61 62 63
    m_tabWidget = new QTabWidget( m_panel );

    m_layout = new QVBoxLayout();

64 65 66 67 68 69 70 71
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    m_downButton = new QPushButton();
    m_downButton->setText( QString( "down" ) );
    m_upButton = new QPushButton();
    m_upButton->setText( QString( "up" ) );
    buttonLayout->addWidget( m_downButton );
    buttonLayout->addWidget( m_upButton );

72
    m_layout->addWidget( m_treeWidget );
73
    m_layout->addLayout( buttonLayout );
74 75 76 77 78 79 80
    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 );
81

82 83 84
    WQtModuleHeaderTreeItem* tiModules = new WQtModuleHeaderTreeItem( m_treeWidget );
    tiModules->setText( 0, QString( "Modules" ) );

85
    connectSlots();
86 87 88 89 90 91 92
}

WQtDatasetBrowser::~WQtDatasetBrowser()
{
}


93 94
void WQtDatasetBrowser::connectSlots()
{
95 96
    connect( m_treeWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( selectTreeItem() ) );
    connect( m_treeWidget, SIGNAL( itemClicked( QTreeWidgetItem*, int ) ), this, SLOT( changeTreeItem() ) );
97 98
    connect( m_upButton, SIGNAL( pressed() ), this, SLOT( moveTreeItemUp() ) );
    connect( m_downButton, SIGNAL( pressed() ), this, SLOT( moveTreeItemDown() ) );
99 100 101
}


102 103 104 105 106 107 108 109 110
WQtSubjectTreeItem* WQtDatasetBrowser::addSubject( std::string name )
{
    WQtSubjectTreeItem* subject = new WQtSubjectTreeItem( m_treeWidget );
    subject->setText( 0, QString( name.c_str() ) );

    return subject;
}


111
WQtDatasetTreeItem* WQtDatasetBrowser::addDataset( boost::shared_ptr< WModule > module, int subjectId )
112
{
113
    WQtSubjectTreeItem* subject = ( WQtSubjectTreeItem* )m_treeWidget->topLevelItem( subjectId + 1 );
schurade's avatar
schurade committed
114
    subject->setExpanded( true );
115
    emit dataSetBrowserEvent( QString( "textureChanged" ), true );
116
    emit dataSetBrowserEvent( QString( "dataSetAdded" ), true );
117
    return subject->addDatasetItem( module );
118
}
119

120 121 122 123 124 125 126 127
WQtModuleTreeItem* WQtDatasetBrowser::addModule( boost::shared_ptr< WModule > module )
{
    WQtModuleHeaderTreeItem* tiModules = ( WQtModuleHeaderTreeItem* )m_treeWidget->topLevelItem( 0 );
    tiModules->setExpanded( true );
    return tiModules->addModuleItem( module );
}


128 129
void WQtDatasetBrowser::selectTreeItem()
{
130 131
    // TODO(schurade): qt doc says clear() doesn't delete tabs so this is possibly a memory leak
    m_tabWidget->clear();
132

133 134
    if ( m_treeWidget->selectedItems().size() == 0 || m_treeWidget->selectedItems().at( 0 )->type() == 0 ||
            m_treeWidget->selectedItems().at( 0 )->type() == 2 )
135
    {
136 137
        return;
    }
138 139 140 141 142 143 144 145 146 147
    boost::shared_ptr< WModule >module;

    if ( m_treeWidget->selectedItems().at( 0 )->type() == 1 )
    {
        module = ( ( WQtDatasetTreeItem* ) m_treeWidget->selectedItems().at( 0 ) )->getModule();
    }
    else
    {
        module = ( ( WQtModuleTreeItem* ) m_treeWidget->selectedItems().at( 0 ) )->getModule();
    }
148
    std::vector < WProperty* >props = module->getProperties()->getPropertyVector();
149

150
    WQtDSBWidget* tab1 = new WQtDSBWidget( "settings" );
151

152 153 154
    for ( size_t i = 0; i < props.size(); ++i )
    {
        if ( !props.at(i)->isHidden() )
155
        {
156
            switch ( props.at(i)->getType() )
157
            {
158
                case P_BOOL:
159
                {
160 161 162 163
                    QString name = QString( props.at( i )->getName().c_str() );
                    WQtCheckBox* box = tab1->addCheckBox( name, props.at( i )->getValue<bool>() );
                    connect( box, SIGNAL( checkBoxStateChanged( QString, bool ) ), this, SLOT( slotSetBoolProperty( QString, bool ) ) );
                    break;
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
                case P_CHAR:
                    break;
                case P_UNSIGNED_CHAR:
                    break;
                case P_INT:
                {
                    QString name = QString( props.at( i )->getName().c_str() );
                    WQtSliderWithEdit* slider = tab1->addSliderInt( name, props.at( i )->getValue<int>(),
                            props.at( i )->getMin<int>(), props.at( i )->getMax<int>() );
                    connect( slider, SIGNAL( signalNumberWithName( QString, int ) ), this, SLOT( slotSetIntProperty( QString, int ) ) );
                    break;
                }
                case P_UNSIGNED_INT:
                    break;
                case P_FLOAT:
                    break;
                case P_DOUBLE:
                    break;
                case P_STRING:
                {
                    QString name = QString( props.at( i )->getName().c_str() );
                    QString text = QString( props.at( i )->getValue<std::string>().c_str() );
                    WQtLineEdit* edit = tab1->addLineEdit( name, text );
                    connect( edit, SIGNAL( lineEditStateChanged( QString, QString ) ), this, SLOT( slotSetStringProperty( QString, QString ) ) );
                    break;
                }
                default:
                    break;
193 194 195
            }
        }
    }
196 197

    tab1->addSpacer();
198
    addTabWidgetContent( tab1 );
199 200
}

201

202 203
void WQtDatasetBrowser::changeTreeItem()
{
schurade's avatar
schurade committed
204
    if ( m_treeWidget->selectedItems().size() == 1 && m_treeWidget->selectedItems().at( 0 )->type() == 1 )
205
    {
schurade's avatar
schurade committed
206 207 208 209 210 211 212 213 214 215
        boost::shared_ptr< WModule >module =( ( WQtDatasetTreeItem* ) m_treeWidget->selectedItems().at( 0 ) )->getModule();
        if ( m_treeWidget->selectedItems().at( 0 )->checkState( 0 ) )
        {
            module->getProperties()->setValue<bool>( "active", true );
        }
        else
        {
            module->getProperties()->setValue<bool>( "active", false );
        }
        emit dataSetBrowserEvent( QString( "textureChanged" ), true );
216
    }
schurade's avatar
schurade committed
217
    else if ( m_treeWidget->selectedItems().size() == 1 && m_treeWidget->selectedItems().at( 0 )->type() == 3 )
218
    {
schurade's avatar
schurade committed
219 220 221 222 223 224 225 226 227
        boost::shared_ptr< WModule >module =( ( WQtModuleTreeItem* ) m_treeWidget->selectedItems().at( 0 ) )->getModule();
        if ( m_treeWidget->selectedItems().at( 0 )->checkState( 0 ) )
        {
            module->getProperties()->setValue<bool>( "active", true );
        }
        else
        {
            module->getProperties()->setValue<bool>( "active", false );
        }
228 229 230
    }
}

231 232 233 234
void WQtDatasetBrowser::addTabWidgetContent( WQtDSBWidget* content )
{
    m_tabWidget->addTab( content, content->getName() );
}
235

236
void WQtDatasetBrowser::slotSetIntProperty( QString name, int value )
237 238
{
    boost::shared_ptr< WModule >module =( ( WQtDatasetTreeItem* ) m_treeWidget->selectedItems().at( 0 ) )->getModule();
239
    module->getProperties()->setValue<int>( name.toStdString(), value );
240

241
    emit dataSetBrowserEvent( QString( "textureChanged" ), true );
242 243
}

244
void WQtDatasetBrowser::slotSetBoolProperty( QString name, bool value )
245 246
{
    boost::shared_ptr< WModule >module =( ( WQtDatasetTreeItem* ) m_treeWidget->selectedItems().at( 0 ) )->getModule();
247
    module->getProperties()->setValue<bool>( name.toStdString(), value );
248

249
    emit dataSetBrowserEvent( QString( "textureChanged" ), true );
250 251
}

252
void WQtDatasetBrowser::slotSetStringProperty( QString name, QString value )
253 254
{
    boost::shared_ptr< WModule >module =( ( WQtDatasetTreeItem* ) m_treeWidget->selectedItems().at( 0 ) )->getModule();
255
    module->getProperties()->setValue<std::string>( name.toStdString(), value.toStdString() );
256 257 258

    if ( name == "name")
    {
259
        m_treeWidget->selectedItems().at( 0 )->setText( 0, value );
260 261
    }
}
262

263
std::vector< boost::shared_ptr< WDataSet > > WQtDatasetBrowser::getDataSetList( int subjectId, bool onlyTextures )
264
{
265
    std::vector< boost::shared_ptr< WDataSet > >moduleList;
266 267 268 269 270

    if ( m_treeWidget->invisibleRootItem()->childCount() < subjectId + 1)
    {
        return moduleList;
    }
271
    int count = m_treeWidget->invisibleRootItem()->child( subjectId + 1 )->childCount();
272 273 274

    for ( int i = 0 ; i < count ; ++i )
    {
275 276 277 278 279 280 281 282 283 284
        boost::shared_ptr< WMData > dm = boost::shared_dynamic_cast< WMData >( ( ( WQtDatasetTreeItem* )m_treeWidget->invisibleRootItem()->child(
                        subjectId + 1 )->child( i ) )->getModule() );

        if ( !onlyTextures || dm->getDataSet()->isTexture() )
        {
            if ( dm->getProperties()->getValue<bool>( "active" ) )
            {
                moduleList.push_back( dm->getDataSet() );
            }
        }
285 286 287
    }
    return moduleList;
}
288 289 290 291 292 293 294 295 296 297 298 299 300

void WQtDatasetBrowser::moveTreeItemDown()
{
    m_treeWidget->moveTreeItemDown();
    emit dataSetBrowserEvent( QString( "textureChanged" ), true );
}

void WQtDatasetBrowser::moveTreeItemUp()
{
    m_treeWidget->moveTreeItemUp();
    emit dataSetBrowserEvent( QString( "textureChanged" ), true );
}