1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
10
// OpenWalnut is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
15
// OpenWalnut is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public License
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
23
//---------------------------------------------------------------------------
32
#include <boost/bind.hpp>
34
#include <QtCore/QList>
35
#include <QtGui/QScrollArea>
36
#include <QtGui/QVBoxLayout>
37
#include <QtGui/QListWidgetItem>
38
#include <QtGui/QApplication>
40
#include "core/dataHandler/WDataSet.h"
41
#include "core/dataHandler/WDataHandler.h"
42
#include "core/dataHandler/exceptions/WDHNoSuchSubject.h"
43
#include "core/graphicsEngine/WGEColormapping.h"
44
#include "core/graphicsEngine/WGETexture.h"
45
#include "../events/WUpdateTextureSorterEvent.h"
46
#include "../events/WEventTypes.h"
48
#include "WQtColormapper.h"
49
#include "WQtColormapper.moc"
51
WQtColormapper::WQtColormapper( QWidget* parent )
52
: QDockWidget( "Colormaps", parent )
54
setObjectName( "Colormapper Dock" );
56
m_textureListWidget = new QListWidget( this );
57
m_textureListWidget->setToolTip( "List of available colormaps. Only the upper <b>"
58
+ QString().setNum( WGETexture3D::MAX_NUMBER_OF_TEXTURES )
59
+ "</b> textures will be applied." );
60
this->setAllowedAreas( Qt::AllDockWidgetAreas );
61
this->setFeatures( QDockWidget::AllDockWidgetFeatures );
63
QWidget* panel = new QWidget( this );
65
m_layout = new QVBoxLayout();
67
QHBoxLayout* buttonLayout = new QHBoxLayout();
68
m_downButton = new QPushButton();
69
m_downButton->setText( QString( "down" ) );
70
m_upButton = new QPushButton();
71
m_upButton->setText( QString( "up" ) );
73
buttonLayout->addWidget( m_downButton );
74
buttonLayout->addWidget( m_upButton );
76
connect( m_upButton, SIGNAL( pressed() ), this, SLOT( moveItemUp() ) );
77
connect( m_downButton, SIGNAL( pressed() ), this, SLOT( moveItemDown() ) );
79
m_layout->addWidget( m_textureListWidget );
80
m_layout->addLayout( buttonLayout );
82
connect( m_textureListWidget, SIGNAL( itemClicked( QListWidgetItem* ) ), this, SLOT( handleTextureClicked() ) );
84
panel->setLayout( m_layout );
87
// get the proper subscriptions to the colormapper signals.
88
boost::shared_ptr< WGEColormapping > p = WGEColormapping::instance();
89
m_registerConnection = p->subscribeSignal( WGEColormapping::Registered,
90
static_cast< WGEColormapping::TextureRegisterHandler >( boost::bind( &WQtColormapper::pushUpdateEvent, this ) ) );
91
m_deregisterConnection = p->subscribeSignal( WGEColormapping::Deregistered,
92
static_cast< WGEColormapping::TextureDeregisterHandler >( boost::bind( &WQtColormapper::pushUpdateEvent, this ) ) );
93
m_replaceConnection = p->subscribeSignal( WGEColormapping::Replaced,
94
static_cast< WGEColormapping::TextureReplaceHandler >( boost::bind( &WQtColormapper::pushUpdateEvent, this ) ) );
95
m_sortConnection = p->subscribeSignal( WGEColormapping::Sorted,
96
static_cast< WGEColormapping::TextureSortHandler>( boost::bind( &WQtColormapper::pushUpdateEvent, this ) ) );
99
WQtColormapper::~WQtColormapper()
101
m_registerConnection.disconnect();
102
m_deregisterConnection.disconnect();
103
m_sortConnection.disconnect();
106
WQtColormapper::WQtTextureListItem::WQtTextureListItem( const osg::ref_ptr< WGETexture3D > texture, WQtColormapper* cmapper, QListWidget* parent ):
107
QListWidgetItem( QString::fromStdString( texture->name()->get() ), parent ),
110
// only show the filename. If the name is not a filename, the texture name is used directly
111
std::vector< std::string > names = string_utils::tokenize( texture->name()->get().c_str(), "/" );
114
setText( QString::fromStdString( names.back() ) );
117
// we need to know the name of the texture
118
m_nameConnection = m_texture->name()->getUpdateCondition()->subscribeSignal(
119
boost::bind( &WQtColormapper::pushUpdateEvent, cmapper )
123
WQtColormapper::WQtTextureListItem::~WQtTextureListItem()
125
m_nameConnection.disconnect();
128
const osg::ref_ptr< WGETexture3D > WQtColormapper::WQtTextureListItem::getTexture() const
133
void WQtColormapper::pushUpdateEvent()
135
// create a new event for this and insert it into event queue
136
QCoreApplication::postEvent( this, new WUpdateTextureSorterEvent() );
139
bool WQtColormapper::event( QEvent* event )
141
// a texture added/removed/sorted/moved
142
if( event->type() == WQT_UPDATE_TEXTURE_SORTER_EVENT )
148
return QDockWidget::event( event );
151
void WQtColormapper::update()
153
boost::shared_ptr< WGEColormapping > cm = WGEColormapping::instance();
155
WGEColormapping::TextureContainerType::ReadTicket r = cm->getReadTicket();
157
// we need to store the last selected texture if there was any
158
osg::ref_ptr< WGETexture3D > lastSelected;
159
WQtTextureListItem* item = dynamic_cast< WQtTextureListItem* >( m_textureListWidget->item( m_textureListWidget->currentIndex().row() ) );
162
lastSelected = item->getTexture();
165
// remove all items and rebuild list.
166
m_textureListWidget->clear();
167
for( WGEColormapping::TextureConstIterator iter = r->get().begin(); iter != r->get().end(); ++iter )
169
WQtTextureListItem* item = new WQtTextureListItem( *iter, this, m_textureListWidget );
170
m_textureListWidget->addItem( item ); // the list widget removes the item (and frees the reference to the texture pointer).
172
// is the item the texture that has been selected previously?
173
if( item->getTexture() == lastSelected )
175
m_textureListWidget->setCurrentItem( item );
180
void WQtColormapper::handleTextureClicked()
182
// maybe someone is interested in this signal:
183
emit textureSelectionChanged(
184
static_cast< WQtTextureListItem* >( m_textureListWidget->item( m_textureListWidget->currentIndex().row() ) )->getTexture()
188
void WQtColormapper::moveItemDown()
190
boost::shared_ptr< WGEColormapping > cm = WGEColormapping::instance();
191
WQtTextureListItem* item = dynamic_cast< WQtTextureListItem* >( m_textureListWidget->item( m_textureListWidget->currentIndex().row() ) );
194
cm->moveDown( item->getTexture() );
198
void WQtColormapper::moveItemUp()
200
boost::shared_ptr< WGEColormapping > cm = WGEColormapping::instance();
201
WQtTextureListItem* item = dynamic_cast< WQtTextureListItem* >( m_textureListWidget->item( m_textureListWidget->currentIndex().row() ) );
204
cm->moveUp( item->getTexture() );
208
void WQtColormapper::selectTexture( boost::shared_ptr< WDataSet > dataSet )
210
// simply check each item against the texture in the specified dataset
211
for( int i = 0; i < m_textureListWidget->count(); ++i )
213
WQtTextureListItem* item = dynamic_cast< WQtTextureListItem* >( m_textureListWidget->item( i ) );
214
if(item && dataSet->isTexture() && ( item->getTexture() == dataSet->getTexture() ) )
216
m_textureListWidget->setCurrentItem( item );