//--------------------------------------------------------------------------- // // 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 . // //--------------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include "../../../dataHandler/WDataSet.h" #include "../../../dataHandler/WSubject.h" #include "../../../dataHandler/WDataHandler.h" #include "../../../dataHandler/exceptions/WDHNoSuchSubject.h" #include "WQtTextureSorter.h" #include "../../../kernel/WModuleFactory.h" WQtTextureSorter::WQtTextureSorter( QWidget* parent ) : QWidget( parent ) { m_textureListWidget = new QListWidget( this ); m_textureListWidget->setToolTip( "List of available textures. Only the upper eight textures will be applied." ); m_layout = new QVBoxLayout(); 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 ); connect( m_upButton, SIGNAL( pressed() ), this, SLOT( moveItemUp() ) ); connect( m_downButton, SIGNAL( pressed() ), this, SLOT( moveItemDown() ) ); m_layout->addWidget( m_textureListWidget ); m_layout->addLayout( buttonLayout ); setLayout( m_layout ); } WQtTextureSorter::~WQtTextureSorter() { } // TODO(wiebel): have a second look on this begin/end read/write mess. void WQtTextureSorter::update() { boost::shared_ptr< WSubject > subject; try { subject = WDataHandler::getDefaultSubject(); } catch( WDHNoSuchSubject ) { return; } if( !subject ) { return; } DatasetAccess ta = textures.getAccessObject(); ta->beginRead(); if( ta->get().empty() ) { ta->endRead(); DatasetAccess da = subject->getAccessObject(); da->beginRead(); for( DatasetContainerType::iterator it = da->get().begin(); it != da->get().end(); ++it ) { if( ( *it )->isTexture() ) { da->endRead(); ta->beginWrite(); ta->get().insert( ta->get().begin(), *it ); ta->endWrite(); da->beginRead(); } } da->endRead(); } else { ta->endRead(); DatasetAccess da = subject->getAccessObject(); da->beginRead(); for( DatasetContainerType::iterator it = da->get().begin(); it != da->get().end(); ++it ) { if( ( *it )->isTexture() ) { ta->beginRead(); if( std::find( ta->get().begin(), ta->get().end(), *it ) == ta->get().end() ) { ta->endRead(); ta->beginWrite(); ta->get().insert( ta->get().begin(), *it ); ta->endWrite(); } else { ta->endRead(); } } } da->endRead(); } int index = m_textureListWidget->currentIndex().row(); m_textureListWidget->clear(); ta->beginRead(); for( DatasetContainerType::iterator it = ta->get().begin(); it != ta->get().end(); ++it ) { std::string name = string_utils::tokenize( ( *it )->getFileName().c_str(), "/" ).back(); m_textureListWidget->addItem( name.c_str() ); } ta->endRead(); m_textureListWidget->setCurrentRow( index ); sort(); } void WQtTextureSorter::moveItemDown() { unsigned int index = m_textureListWidget->currentIndex().row(); DatasetAccess ta = textures.getAccessObject(); ta->beginRead(); if( index < ta->get().size() - 1 ) { ta->endRead(); QListWidgetItem* ci = m_textureListWidget->takeItem( index ); if( ci ) { m_textureListWidget->insertItem( index + 1, ci ); m_textureListWidget->clearSelection(); m_textureListWidget->setCurrentItem( ci ); ci->setSelected( true ); ta->beginWrite(); boost::shared_ptr< WDataSet > tmp = ta->get()[index+1]; ta->get()[index+1] = ta->get()[index]; ta->get()[index] = tmp; ta->endWrite(); } } else { ta->endRead(); } sort(); } void WQtTextureSorter::moveItemUp() { unsigned int index = m_textureListWidget->currentIndex().row(); if( index > 0 ) { QListWidgetItem* ci = m_textureListWidget->takeItem( index ); if( ci ) { DatasetAccess ta = textures.getAccessObject(); ta->beginWrite(); m_textureListWidget->insertItem( index - 1, ci ); m_textureListWidget->clearSelection(); m_textureListWidget->setCurrentItem( ci ); ci->setSelected( true ); boost::shared_ptr< WDataSet > tmp = ta->get()[index-1]; ta->get()[index-1] = ta->get()[index]; ta->get()[index] = tmp; ta->endWrite(); } } sort(); } bool WQtTextureSorter::isLess( boost::shared_ptr< WDataSet > lhs, boost::shared_ptr< WDataSet > rhs ) { DatasetAccess ta = textures.getAccessObject(); ta->beginRead(); DatasetContainerType::iterator itLHS = std::find( ta->get().begin(), ta->get().end(), lhs ); DatasetContainerType::iterator itRHS = std::find( ta->get().begin(), ta->get().end(), rhs ); bool result = itLHS < itRHS; ta->endRead(); return result; } void WQtTextureSorter::sort() { DatasetAccess da = WDataHandler::getDefaultSubject()->getAccessObject(); da->beginWrite(); std::sort( da->get().begin(), da->get().end(), boost::bind( boost::mem_fn( &WQtTextureSorter::isLess ), this, _1, _2 ) ); da->endWrite(); WDataHandler::getDefaultSubject()->getChangeCondition()->notify(); }