~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/qt4gui/qt4/controlPanel/WQtColormapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
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.
 
14
//
 
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.
 
19
//
 
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/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include <iostream>
 
26
#include <map>
 
27
#include <set>
 
28
#include <string>
 
29
#include <vector>
 
30
#include <algorithm>
 
31
 
 
32
#include <boost/bind.hpp>
 
33
 
 
34
#include <QtCore/QList>
 
35
#include <QtGui/QScrollArea>
 
36
#include <QtGui/QVBoxLayout>
 
37
#include <QtGui/QListWidgetItem>
 
38
#include <QtGui/QApplication>
 
39
 
 
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"
 
47
 
 
48
#include "WQtColormapper.h"
 
49
#include "WQtColormapper.moc"
 
50
 
 
51
WQtColormapper::WQtColormapper( QWidget* parent )
 
52
    : QDockWidget( "Colormaps", parent )
 
53
{
 
54
    setObjectName( "Colormapper Dock" );
 
55
 
 
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 );
 
62
 
 
63
    QWidget* panel = new QWidget( this );
 
64
 
 
65
    m_layout = new QVBoxLayout();
 
66
 
 
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" ) );
 
72
 
 
73
    buttonLayout->addWidget( m_downButton );
 
74
    buttonLayout->addWidget( m_upButton );
 
75
 
 
76
    connect( m_upButton, SIGNAL( pressed() ), this, SLOT( moveItemUp() ) );
 
77
    connect( m_downButton, SIGNAL( pressed() ), this, SLOT( moveItemDown() ) );
 
78
 
 
79
    m_layout->addWidget( m_textureListWidget );
 
80
    m_layout->addLayout( buttonLayout );
 
81
 
 
82
    connect( m_textureListWidget, SIGNAL( itemClicked( QListWidgetItem* ) ), this, SLOT( handleTextureClicked() ) );
 
83
 
 
84
    panel->setLayout( m_layout );
 
85
    setWidget( panel );
 
86
 
 
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 ) ) );
 
97
}
 
98
 
 
99
WQtColormapper::~WQtColormapper()
 
100
{
 
101
    m_registerConnection.disconnect();
 
102
    m_deregisterConnection.disconnect();
 
103
    m_sortConnection.disconnect();
 
104
}
 
105
 
 
106
WQtColormapper::WQtTextureListItem::WQtTextureListItem( const osg::ref_ptr< WGETexture3D > texture, WQtColormapper* cmapper, QListWidget* parent ):
 
107
    QListWidgetItem( QString::fromStdString( texture->name()->get() ), parent ),
 
108
    m_texture( texture )
 
109
{
 
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(), "/" );
 
112
    if( names.size() )
 
113
    {
 
114
        setText( QString::fromStdString( names.back() ) );
 
115
    }
 
116
 
 
117
    // we need to know the name of the texture
 
118
    m_nameConnection = m_texture->name()->getUpdateCondition()->subscribeSignal(
 
119
        boost::bind( &WQtColormapper::pushUpdateEvent, cmapper )
 
120
    );
 
121
}
 
122
 
 
123
WQtColormapper::WQtTextureListItem::~WQtTextureListItem()
 
124
{
 
125
    m_nameConnection.disconnect();
 
126
}
 
127
 
 
128
const osg::ref_ptr< WGETexture3D > WQtColormapper::WQtTextureListItem::getTexture() const
 
129
{
 
130
    return m_texture;
 
131
}
 
132
 
 
133
void WQtColormapper::pushUpdateEvent()
 
134
{
 
135
    // create a new event for this and insert it into event queue
 
136
    QCoreApplication::postEvent( this,  new WUpdateTextureSorterEvent() );
 
137
}
 
138
 
 
139
bool WQtColormapper::event( QEvent* event )
 
140
{
 
141
    // a texture added/removed/sorted/moved
 
142
    if( event->type() == WQT_UPDATE_TEXTURE_SORTER_EVENT )
 
143
    {
 
144
        update();
 
145
        return true;
 
146
    }
 
147
 
 
148
    return QDockWidget::event( event );
 
149
}
 
150
 
 
151
void WQtColormapper::update()
 
152
{
 
153
    boost::shared_ptr< WGEColormapping > cm = WGEColormapping::instance();
 
154
 
 
155
    WGEColormapping::TextureContainerType::ReadTicket r = cm->getReadTicket();
 
156
 
 
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() ) );
 
160
    if( item )
 
161
    {
 
162
        lastSelected = item->getTexture();
 
163
    }
 
164
 
 
165
    // remove all items and rebuild list.
 
166
    m_textureListWidget->clear();
 
167
    for( WGEColormapping::TextureConstIterator iter = r->get().begin(); iter != r->get().end(); ++iter )
 
168
    {
 
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).
 
171
 
 
172
        // is the item the texture that has been selected previously?
 
173
        if( item->getTexture() == lastSelected )
 
174
        {
 
175
            m_textureListWidget->setCurrentItem( item );
 
176
        }
 
177
    }
 
178
}
 
179
 
 
180
void WQtColormapper::handleTextureClicked()
 
181
{
 
182
    // maybe someone is interested in this signal:
 
183
    emit textureSelectionChanged(
 
184
        static_cast< WQtTextureListItem* >( m_textureListWidget->item( m_textureListWidget->currentIndex().row() ) )->getTexture()
 
185
    );
 
186
}
 
187
 
 
188
void WQtColormapper::moveItemDown()
 
189
{
 
190
    boost::shared_ptr< WGEColormapping > cm = WGEColormapping::instance();
 
191
    WQtTextureListItem* item = dynamic_cast< WQtTextureListItem* >( m_textureListWidget->item( m_textureListWidget->currentIndex().row() ) );
 
192
    if( item )
 
193
    {
 
194
        cm->moveDown( item->getTexture() );
 
195
    }
 
196
}
 
197
 
 
198
void WQtColormapper::moveItemUp()
 
199
{
 
200
    boost::shared_ptr< WGEColormapping > cm = WGEColormapping::instance();
 
201
    WQtTextureListItem* item = dynamic_cast< WQtTextureListItem* >( m_textureListWidget->item( m_textureListWidget->currentIndex().row() ) );
 
202
    if( item )
 
203
    {
 
204
        cm->moveUp( item->getTexture() );
 
205
    }
 
206
}
 
207
 
 
208
void WQtColormapper::selectTexture( boost::shared_ptr< WDataSet > dataSet )
 
209
{
 
210
    // simply check each item against the texture in the specified dataset
 
211
    for( int i = 0; i < m_textureListWidget->count(); ++i )
 
212
    {
 
213
        WQtTextureListItem* item = dynamic_cast< WQtTextureListItem* >( m_textureListWidget->item( i ) );
 
214
        if(item && dataSet->isTexture() && ( item->getTexture() == dataSet->getTexture() ) )
 
215
        {
 
216
            m_textureListWidget->setCurrentItem( item );
 
217
        }
 
218
    }
 
219
}
 
220