1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "VTKViewer_MarkerWidget.h"
21 #include "VTKViewer_MarkerUtils.h"
23 #include <QtxComboBox.h>
25 #include <SUIT_ResourceMgr.h>
26 #include <SUIT_Session.h>
28 #include <vtkImageData.h>
30 #include <QButtonGroup>
31 #include <QGridLayout>
32 #include <QHBoxLayout>
34 #include <QPushButton>
35 #include <QRadioButton>
36 #include <QStackedWidget>
42 * Class : VTKViewer_MarkerWidget
43 * Description : Widget for specifying point marker parameters
49 VTKViewer_MarkerWidget::VTKViewer_MarkerWidget( QWidget* theParent )
50 : QWidget( theParent )
52 QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
53 QRadioButton* aCustomTypeRB = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
54 myTypeGroup = new QButtonGroup( this );
55 myTypeGroup->addButton( aStandardTypeRB, 0 );
56 myTypeGroup->addButton( aCustomTypeRB, 1 );
58 QHBoxLayout* aRadioLayout = new QHBoxLayout;
59 aRadioLayout->setMargin( 0 );
60 aRadioLayout->setSpacing( SPACING );
61 aRadioLayout->addWidget( aStandardTypeRB );
62 aRadioLayout->addWidget( aCustomTypeRB );
66 myWGStack = new QStackedWidget( this );
67 myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
71 QWidget* aStdWidget = new QWidget( myWGStack );
73 QLabel* aTypeLab = new QLabel( tr( "TYPE" ), aStdWidget );
74 QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
76 myStdTypeCombo = new QtxComboBox( aStdWidget );
77 myStdScaleCombo = new QtxComboBox( aStdWidget );
79 QGridLayout* aStdLayout = new QGridLayout;
80 aStdLayout->setMargin( MARGIN );
81 aStdLayout->setSpacing( SPACING );
82 aStdLayout->addWidget( aTypeLab, 0, 0 );
83 aStdLayout->addWidget( myStdTypeCombo, 0, 1 );
84 aStdLayout->addWidget( aScaleLab, 1, 0 );
85 aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
86 aStdWidget->setLayout( aStdLayout );
90 QWidget* aCustomWidget = new QWidget( myWGStack );
92 QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
93 myCustomTypeCombo = new QtxComboBox( aCustomWidget );
94 QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
96 QGridLayout* aCustomLayout = new QGridLayout;
97 aCustomLayout->setMargin( MARGIN );
98 aCustomLayout->setSpacing( SPACING );
99 aCustomLayout->addWidget( aCustomLab, 0, 0 );
100 aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
101 aCustomLayout->addWidget( aBrowseBtn, 0, 2 );
102 aCustomLayout->setRowStretch( 1, 5 );
103 aCustomWidget->setLayout( aCustomLayout );
107 myWGStack->insertWidget( 0, aStdWidget );
108 myWGStack->insertWidget( 1, aCustomWidget );
112 QVBoxLayout* aTopLayout = new QVBoxLayout;
113 aTopLayout->setMargin( MARGIN );
114 aTopLayout->setSpacing( SPACING );
115 aTopLayout->addLayout( aRadioLayout );
116 aTopLayout->addWidget( myWGStack );
117 setLayout( aTopLayout );
121 connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
122 connect( myStdTypeCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onStdMarkerChanged( int ) ) );
123 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
127 aStandardTypeRB->setChecked( true );
134 VTKViewer_MarkerWidget::~VTKViewer_MarkerWidget()
138 void VTKViewer_MarkerWidget::setCustomMarkerMap( VTK::MarkerMap theMarkerMap )
140 myCustomMarkerMap = theMarkerMap;
142 VTK::MarkerMap::const_iterator it = theMarkerMap.begin(), itEnd = theMarkerMap.end();
143 for( ; it != itEnd; it++ )
145 int anId = it->first;
146 VTK::MarkerData aMarkerData = it->second;
147 QPixmap aPixmap = markerFromData( aMarkerData );
148 if( !aPixmap.isNull() )
150 myCustomTypeCombo->addItem( aPixmap, QString::number( anId ) );
151 myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, anId );
156 VTK::MarkerMap VTKViewer_MarkerWidget::getCustomMarkerMap()
158 return myCustomMarkerMap;
161 void VTKViewer_MarkerWidget::setStandardMarker( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
163 if ( ( theMarkerType > VTK::MT_NONE && theMarkerType < VTK::MT_USER ) ||
164 myExtraMarkerList.contains( theMarkerType ) ) {
165 myTypeGroup->button( 0 )->setChecked( true );
166 myWGStack->setCurrentIndex( 0 );
167 myStdTypeCombo->setCurrentId( theMarkerType );
168 int aMarkerScale = std::max( (int)VTK::MS_10, std::min( (int)VTK::MS_70, (int)theMarkerScale ) );
169 myStdScaleCombo->setCurrentId( aMarkerScale );
173 void VTKViewer_MarkerWidget::setCustomMarker( int theId )
176 myTypeGroup->button( 1 )->setChecked( true );
177 myWGStack->setCurrentIndex( 1 );
179 myCustomTypeCombo->setCurrentId( theId );
183 VTK::MarkerType VTKViewer_MarkerWidget::getMarkerType() const
185 return myWGStack->currentIndex() == 0 ? (VTK::MarkerType)myStdTypeCombo->currentId() : VTK::MT_USER;
188 VTK::MarkerScale VTKViewer_MarkerWidget::getStandardMarkerScale() const
190 return myWGStack->currentIndex() == 0 ? (VTK::MarkerScale)myStdScaleCombo->currentId() : VTK::MS_NONE;
193 int VTKViewer_MarkerWidget::getCustomMarkerID() const
195 return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
198 void VTKViewer_MarkerWidget::addExtraStdMarker( VTK::MarkerType theMarkerType, const QPixmap& thePixmap )
200 if( myExtraMarkerList.isEmpty() )
201 myStdTypeCombo->insertSeparator( myStdTypeCombo->count() );
202 myStdTypeCombo->addItem( thePixmap, QString() );
203 myStdTypeCombo->setId( myStdTypeCombo->count()-1, theMarkerType );
205 myExtraMarkerList.append( theMarkerType );
208 void VTKViewer_MarkerWidget::init()
210 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
212 for ( int i = VTK::MT_POINT; i < VTK::MT_USER; i++ ) {
213 QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
214 QPixmap pixmap = resMgr->loadPixmap( "VTKViewer", tr( qPrintable( icoFile ) ) );
215 myStdTypeCombo->addItem( pixmap, QString() );
216 myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
219 for ( int i = VTK::MS_10; i <= VTK::MS_70; i++ ) {
220 myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
221 myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
225 void VTKViewer_MarkerWidget::addTexture( int id, bool select )
227 if ( id > 0 && myCustomTypeCombo->index( id ) == -1 &&
228 myCustomMarkerMap.find( id ) != myCustomMarkerMap.end() ) {
229 VTK::MarkerData aMarkerData = myCustomMarkerMap[ id ];
230 QPixmap pixmap = markerFromData( aMarkerData );
231 if( !pixmap.isNull() ) {
232 myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
233 myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
234 if ( select ) myCustomTypeCombo->setCurrentId( id );
239 QPixmap VTKViewer_MarkerWidget::markerFromData( const VTK::MarkerData& theMarkerData )
241 const VTK::MarkerTexture& aMarkerTexture = theMarkerData.second;
242 vtkSmartPointer<vtkImageData> anImageData = VTK::MakeVTKImage( aMarkerTexture, false );
244 QImage anImage = VTK::ConvertToQImage( anImageData.GetPointer() );
245 if( anImage.isNull() )
248 return QPixmap::fromImage( anImage );
251 void VTKViewer_MarkerWidget::onStdMarkerChanged( int index )
253 VTK::MarkerType aMarkerType = (VTK::MarkerType)myStdTypeCombo->id( index );
254 bool anIsExtraMarker = myExtraMarkerList.contains( aMarkerType );
255 myStdScaleCombo->setEnabled( !anIsExtraMarker );
258 void VTKViewer_MarkerWidget::onBrowse()
261 filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
262 QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
263 if ( !aFileName.isEmpty() ) {
264 VTK::MarkerTexture aMarkerTexture;
265 if ( VTK::LoadTextureData( aFileName, VTK::MS_NONE, aMarkerTexture ) ) {
266 int anId = VTK::GetUniqueId( myCustomMarkerMap );
267 VTK::MarkerData& aMarkerData = myCustomMarkerMap[ anId ];
268 aMarkerData.first = aFileName.toStdString();
269 aMarkerData.second = aMarkerTexture;
270 addTexture( anId, true );