Salome HOME
Modify creation of curves: 1) using QDockWidget instead of QDialog; 2) selection...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ZoneDlg.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ZoneDlg.h"
24
25 #include "HYDROGUI_ColorWidget.h"
26 #include "HYDROGUI_Tool.h"
27
28 #include <QComboBox>
29 #include <QGroupBox>
30 #include <QLabel>
31 #include <QLayout>
32 #include <QLineEdit>
33 #include <QListWidget>
34 #include <QRadioButton>
35
36 HYDROGUI_ZoneDlg::HYDROGUI_ZoneDlg( HYDROGUI_Module* theModule, const QString& theTitle )
37 : HYDROGUI_InputPanel( theModule, theTitle )
38 {
39   // Zone name
40   myObjectNameGroup = new QGroupBox( tr( "ZONE_NAME" ), mainFrame() );
41
42   myObjectName = new QLineEdit( myObjectNameGroup );
43
44   QBoxLayout* aNameLayout = new QHBoxLayout( myObjectNameGroup );
45   aNameLayout->setMargin( 5 );
46   aNameLayout->setSpacing( 5 );
47   aNameLayout->addWidget( new QLabel( tr( "NAME" ), myObjectNameGroup ) );
48   aNameLayout->addWidget( myObjectName );
49
50
51   QGroupBox* aParamGroup = new QGroupBox( tr( "ZONE_PARAMETERS" ), mainFrame() );
52
53   QFrame* aPolylineFrame = new QFrame( aParamGroup );
54
55   myPolylines = new QComboBox( aPolylineFrame );
56   myPolylines->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
57
58   QBoxLayout* aPolyLayout = new QHBoxLayout( aPolylineFrame );
59   aPolyLayout->setMargin( 0 );
60   aPolyLayout->setSpacing( 5 );
61   aPolyLayout->addWidget( new QLabel( tr( "ZONE_POLYLINE" ), aPolylineFrame ) );
62   aPolyLayout->addWidget( myPolylines );
63
64
65   QFrame* aFillingFrame = new QFrame( aParamGroup );
66   QLabel* aFillingLabel = new QLabel( tr( "FILLING_COLOR" ), aFillingFrame );
67   myFillingTransparent = new QRadioButton( tr( "TRANSPARENT" ), aFillingFrame );
68   myFillingTransparent->setChecked( true );
69   myFillingColor = new QRadioButton( tr( "COLOR" ), aFillingFrame );
70   myFillingColorBox = new HYDROGUI_ColorWidget( aFillingFrame );
71
72   QGridLayout* aFillingLayout = new QGridLayout( aFillingFrame );
73   aFillingLayout->setMargin( 0 );
74   aFillingLayout->setSpacing( 5 );
75   aFillingLayout->addWidget( aFillingLabel, 0, 0, 2, 1 );
76   aFillingLayout->addWidget( myFillingTransparent, 0, 1 );
77   aFillingLayout->addWidget( myFillingColor,       1, 1 );
78   aFillingLayout->addWidget( myFillingColorBox,    1, 2 );
79
80
81   myBorderColorGroup = new QGroupBox( tr( "BORDER_COLOR" ), mainFrame() );
82   myBorderColorGroup->setCheckable( true );
83
84   myBorderColorBox = new HYDROGUI_ColorWidget( myBorderColorGroup );
85
86   QBoxLayout* aBorderColorLayout = new QHBoxLayout( myBorderColorGroup );
87   aBorderColorLayout->setMargin( 5 );
88   aBorderColorLayout->setSpacing( 5 );
89   aBorderColorLayout->addWidget( new QLabel( tr( "COLOR" ), myBorderColorGroup ) );
90   aBorderColorLayout->addWidget( myBorderColorBox );
91
92
93   QBoxLayout* aParamLayout = new QVBoxLayout( aParamGroup );
94   aParamLayout->setMargin( 5 );
95   aParamLayout->setSpacing( 5 );
96   aParamLayout->addWidget( aPolylineFrame );
97   aParamLayout->addWidget( aFillingFrame );
98   aParamLayout->addWidget( myBorderColorGroup );
99
100
101   QGroupBox* aBathGroup = new QGroupBox( tr( "ZONE_BATHYMETRIES" ), mainFrame() );
102
103   myBathymetries = new QListWidget( aBathGroup );
104   myBathymetries->setSelectionMode( QListWidget::SingleSelection );
105   myBathymetries->setEditTriggers( QListWidget::NoEditTriggers );
106   myBathymetries->setViewMode( QListWidget::ListMode );
107
108   QBoxLayout* aBathLayout = new QHBoxLayout( aBathGroup );
109   aBathLayout->setMargin( 5 );
110   aBathLayout->setSpacing( 5 );
111   aBathLayout->addWidget( myBathymetries );
112
113
114   // Common
115   addWidget( myObjectNameGroup );
116   addWidget( aParamGroup );
117   addWidget( aBathGroup );
118
119   addStretch();
120
121
122   // Connect signals and slots
123   connect( myPolylines, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onZoneDefChanged() ) );
124   connect( myFillingTransparent, SIGNAL( toggled( bool ) ), this, SLOT( onZoneDefChanged() ) );
125   connect( myFillingColor, SIGNAL( toggled( bool ) ), this, SLOT( onZoneDefChanged() ) );
126   connect( myFillingColorBox, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( onFillingColorChanged() ) );
127   connect( myBorderColorGroup, SIGNAL( toggled( bool ) ), this, SLOT( onZoneDefChanged() ) );
128   connect( myBorderColorBox, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( onZoneDefChanged() ) );
129 }
130
131 HYDROGUI_ZoneDlg::~HYDROGUI_ZoneDlg()
132 {
133 }
134
135 void HYDROGUI_ZoneDlg::reset()
136 {
137   bool isBlocked = blockSignals( true );
138
139   myObjectName->clear();
140
141   myPolylines->clear();
142   myBathymetries->clear();
143
144   myFillingTransparent->setChecked( true );
145   myFillingColorBox->resetColor();
146
147   myBorderColorGroup->setChecked( false );
148   myBorderColorBox->resetColor();
149
150   blockSignals( isBlocked );
151
152   onZoneDefChanged();
153 }
154
155 void HYDROGUI_ZoneDlg::setObjectName( const QString& theName )
156 {
157   myObjectName->setText( theName );
158 }
159
160 QString HYDROGUI_ZoneDlg::getObjectName() const
161 {
162   return myObjectName->text();
163 }
164
165 void HYDROGUI_ZoneDlg::setPolylineNames( const QStringList& thePolylines )
166 {
167   bool isBlocked = blockSignals( true );
168
169   myPolylines->clear();
170   myPolylines->addItems( thePolylines );
171
172   blockSignals( isBlocked );
173 }
174
175 void HYDROGUI_ZoneDlg::setPolylineName( const QString& theName )
176 {
177   int aNewIdx = myPolylines->findText( theName );
178   if ( aNewIdx != myPolylines->currentIndex() )
179   {
180     myPolylines->setCurrentIndex( aNewIdx );
181   }
182   else
183   {
184     onZoneDefChanged();
185   }
186 }
187
188 QString HYDROGUI_ZoneDlg::getPolylineName() const
189 {
190   return myPolylines->currentText();
191 }
192
193 void HYDROGUI_ZoneDlg::setBathymetries( const QStringList& theBathymetries )
194 {
195   myBathymetries->clear();
196
197   for ( int i = 0, n = theBathymetries.length(); i < n; ++i )
198   {
199     QString aBathymetryName = theBathymetries.at( i );
200
201     QListWidgetItem* aListItem = new QListWidgetItem( aBathymetryName, myBathymetries );
202     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
203     aListItem->setCheckState( Qt::Unchecked );
204   }
205 }
206
207 void HYDROGUI_ZoneDlg::setSelectedBathymetries( const QStringList& theBathymetries )
208 {
209   for ( int i = 0, n = theBathymetries.length(); i < n; ++i )
210   {
211     QString aBathymetryName = theBathymetries.at( i );
212
213     QList<QListWidgetItem *> anItems = 
214       myBathymetries->findItems( aBathymetryName, Qt::MatchFixedString | Qt::MatchCaseSensitive );
215     if ( anItems.isEmpty() )
216       continue;
217
218     QListWidgetItem* aListItem = anItems.first();
219     if ( !aListItem )
220       continue;
221
222     aListItem->setCheckState( Qt::Checked );
223   }
224 }
225
226 QStringList HYDROGUI_ZoneDlg::getSelectedBathymetries() const
227 {
228   QStringList aResList;
229
230   for ( int i = 0, n = myBathymetries->count(); i < n; ++i )
231   {
232     QListWidgetItem* aListItem = myBathymetries->item( i );
233     if ( !aListItem || aListItem->checkState() != Qt::Checked )
234       continue;
235
236     QString aSelBathName = aListItem->text();
237     aResList.append( aSelBathName );
238   }
239
240   return aResList;
241 }
242
243 void HYDROGUI_ZoneDlg::setFillingColor( const QColor& theColor )
244 {
245   bool isBlocked = blockSignals( true );
246
247   if( theColor.alpha() == 0 ) // transparent
248     myFillingTransparent->setChecked( true );
249   else
250     myFillingColor->setChecked( true );
251
252   myFillingColorBox->setColor( theColor );
253
254   blockSignals( isBlocked );
255
256   onZoneDefChanged();
257 }
258
259 QColor HYDROGUI_ZoneDlg::getFillingColor() const
260 {
261   QColor aColor( 255, 255, 255, 0 ); // transparent
262   if( myFillingColor->isChecked() )
263     aColor = myFillingColorBox->color();
264   return aColor;
265 }
266
267 void HYDROGUI_ZoneDlg::setBorderColor( const QColor& theColor )
268 {
269   bool isBlocked = blockSignals( true );
270
271   bool isTransparent = theColor.alpha() == 0;
272   myBorderColorGroup->setChecked( !isTransparent );
273   myBorderColorBox->setColor( !isTransparent ? theColor : QColor( Qt::black ) );
274
275   blockSignals( isBlocked );
276
277   onZoneDefChanged();
278 }
279
280 QColor HYDROGUI_ZoneDlg::getBorderColor() const
281 {
282   QColor aColor( Qt::transparent ); // transparent
283   if( myBorderColorGroup->isChecked() )
284     aColor = myBorderColorBox->color();
285   return aColor;
286 }
287
288 void HYDROGUI_ZoneDlg::onFillingColorChanged()
289 {
290   if ( !myFillingColor->isChecked() )
291     return;
292
293   onZoneDefChanged();
294 }
295
296 void HYDROGUI_ZoneDlg::onZoneDefChanged()
297 {
298   if ( signalsBlocked() )
299     return;
300
301   QString aPolylineName = getPolylineName();
302   emit CreatePreview( aPolylineName );
303 }
304
305