]> SALOME platform Git repositories - modules/gui.git/blob - src/VTKViewer/VTKViewer_MarkerWidget.cxx
Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / VTKViewer / VTKViewer_MarkerWidget.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "VTKViewer_MarkerWidget.h"
21 #include "VTKViewer_MarkerUtils.h"
22
23 #include <QtxComboBox.h>
24
25 #include <SUIT_ResourceMgr.h>
26 #include <SUIT_Session.h>
27
28 #include <vtkImageData.h>
29
30 #include <QButtonGroup>
31 #include <QGridLayout>
32 #include <QHBoxLayout>
33 #include <QLabel>
34 #include <QPushButton>
35 #include <QRadioButton>
36 #include <QStackedWidget>
37
38 #define MARGIN  9
39 #define SPACING 6
40
41 /*!
42  * Class       : VTKViewer_MarkerWidget
43  * Description : Widget for specifying point marker parameters
44  */
45
46 /*!
47   Constructor
48 */
49 VTKViewer_MarkerWidget::VTKViewer_MarkerWidget( QWidget* theParent )
50 : QWidget( theParent )
51 {
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 );
57
58   QHBoxLayout* aRadioLayout = new QHBoxLayout;
59   aRadioLayout->setMargin( 0 );
60   aRadioLayout->setSpacing( SPACING );
61   aRadioLayout->addWidget( aStandardTypeRB );
62   aRadioLayout->addWidget( aCustomTypeRB );
63
64   // ---
65
66   myWGStack = new QStackedWidget( this );
67   myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
68
69   // ---
70
71   QWidget* aStdWidget = new QWidget( myWGStack );
72
73   QLabel* aTypeLab  = new QLabel( tr( "TYPE" ),  aStdWidget );
74   QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
75
76   myStdTypeCombo  = new QtxComboBox( aStdWidget );
77   myStdScaleCombo = new QtxComboBox( aStdWidget );
78
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 );
87
88   // ---
89
90   QWidget* aCustomWidget = new QWidget( myWGStack );
91
92   QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
93   myCustomTypeCombo = new QtxComboBox( aCustomWidget );
94   QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
95
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 );
104
105   // ---
106   
107   myWGStack->insertWidget( 0, aStdWidget );
108   myWGStack->insertWidget( 1, aCustomWidget );
109
110   // ---
111
112   QVBoxLayout* aTopLayout = new QVBoxLayout;
113   aTopLayout->setMargin( MARGIN );
114   aTopLayout->setSpacing( SPACING );
115   aTopLayout->addLayout( aRadioLayout );
116   aTopLayout->addWidget( myWGStack );
117   setLayout( aTopLayout );
118
119   // ---
120
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() ) );
124
125   // ---
126
127   aStandardTypeRB->setChecked( true );
128   init();
129 }
130
131 /*!
132   Destructor
133 */
134 VTKViewer_MarkerWidget::~VTKViewer_MarkerWidget()
135 {
136 }
137
138 void VTKViewer_MarkerWidget::setCustomMarkerMap( VTK::MarkerMap theMarkerMap )
139 {
140   myCustomMarkerMap = theMarkerMap;
141
142   VTK::MarkerMap::const_iterator it = theMarkerMap.begin(), itEnd = theMarkerMap.end();
143   for( ; it != itEnd; it++ )
144   {
145     int anId = it->first;
146     VTK::MarkerData aMarkerData = it->second;
147     QPixmap aPixmap = markerFromData( aMarkerData );
148     if( !aPixmap.isNull() )
149     {
150       myCustomTypeCombo->addItem( aPixmap, QString::number( anId ) );
151       myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, anId );
152     }
153   }
154 }
155
156 VTK::MarkerMap VTKViewer_MarkerWidget::getCustomMarkerMap()
157 {
158   return myCustomMarkerMap;
159 }
160
161 void VTKViewer_MarkerWidget::setStandardMarker( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
162 {
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 );
170   }
171 }
172
173 void VTKViewer_MarkerWidget::setCustomMarker( int theId )
174 {
175   if ( theId > 0 ) {
176     myTypeGroup->button( 1 )->setChecked( true );
177     myWGStack->setCurrentIndex( 1 );
178     addTexture( theId );
179     myCustomTypeCombo->setCurrentId( theId );
180   }
181 }
182
183 VTK::MarkerType VTKViewer_MarkerWidget::getMarkerType() const
184 {
185   return myWGStack->currentIndex() == 0 ? (VTK::MarkerType)myStdTypeCombo->currentId() : VTK::MT_USER;
186 }
187
188 VTK::MarkerScale VTKViewer_MarkerWidget::getStandardMarkerScale() const
189 {
190   return myWGStack->currentIndex() == 0 ? (VTK::MarkerScale)myStdScaleCombo->currentId() : VTK::MS_NONE;
191 }
192
193 int VTKViewer_MarkerWidget::getCustomMarkerID() const
194 {
195   return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
196 }
197
198 void VTKViewer_MarkerWidget::addExtraStdMarker( VTK::MarkerType theMarkerType, const QPixmap& thePixmap )
199 {
200   if( myExtraMarkerList.isEmpty() )
201     myStdTypeCombo->insertSeparator( myStdTypeCombo->count() );
202   myStdTypeCombo->addItem( thePixmap, QString() );
203   myStdTypeCombo->setId( myStdTypeCombo->count()-1, theMarkerType );
204
205   myExtraMarkerList.append( theMarkerType );
206 }
207
208 void VTKViewer_MarkerWidget::init()
209 {
210   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
211
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 );
217   }
218
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 );
222   }
223 }
224
225 void VTKViewer_MarkerWidget::addTexture( int id, bool select )
226 {
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 );
235     }
236   }
237 }
238
239 QPixmap VTKViewer_MarkerWidget::markerFromData( const VTK::MarkerData& theMarkerData )
240 {
241   const VTK::MarkerTexture& aMarkerTexture = theMarkerData.second;
242   vtkSmartPointer<vtkImageData> anImageData = VTK::MakeVTKImage( aMarkerTexture, false );
243
244   QImage anImage = VTK::ConvertToQImage( anImageData.GetPointer() );
245   if( anImage.isNull() )
246     return QPixmap();
247
248   return QPixmap::fromImage( anImage );
249 }
250
251 void VTKViewer_MarkerWidget::onStdMarkerChanged( int index )
252 {
253   VTK::MarkerType aMarkerType = (VTK::MarkerType)myStdTypeCombo->id( index );
254   bool anIsExtraMarker = myExtraMarkerList.contains( aMarkerType );
255   myStdScaleCombo->setEnabled( !anIsExtraMarker );
256 }
257
258 void VTKViewer_MarkerWidget::onBrowse()
259 {
260   QStringList filters;
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 );
271     }
272   }
273 }