Salome HOME
Boundary polyline can be selected now in the Calculation case dialog.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationDlg.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_CalculationDlg.h"
24
25 #include "HYDROGUI_ObjSelector.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_DataBrowser.h"
28 #include "HYDROGUI_DataModel.h"
29 #include "HYDROGUI_Module.h"
30 #include "HYDROGUI_DataObject.h"
31 #include "HYDROGUI_NameValidator.h"
32 #include "HYDROGUI_Region.h"
33 #include "HYDROGUI_Zone.h"
34
35 #include <HYDROData_Document.h>
36 #include <HYDROData_Entity.h>
37
38 #include <CAM_Application.h>
39 #include <LightApp_DataObject.h>
40
41 #include <SUIT_DataObject.h>
42 #include <SUIT_FileDlg.h>
43 #include <SUIT_ResourceMgr.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_Study.h>
46
47 #include <LightApp_Application.h>
48 #include <SUIT_Desktop.h>
49 #include <SUIT_MessageBox.h>
50
51 #include <QGroupBox>
52 #include <QLabel>
53 #include <QLayout>
54 #include <QLineEdit>
55 #include <QListWidget>
56 #include <QPicture>
57 #include <QPushButton>
58 #include <QToolButton>
59 #include <QWizardPage>
60 #include <QComboBox>
61 #include <QStackedWidget>
62
63 HYDROGUI_CalculationDlg::HYDROGUI_CalculationDlg( HYDROGUI_Module* theModule, const QString& theTitle )
64 : HYDROGUI_Wizard( theModule, theTitle )
65 {
66   addPage( createObjectsPage() );
67   addPage( createZonesPage() );
68   connect( button( QWizard::NextButton ), SIGNAL( clicked() ), SIGNAL( splitZones() ) );
69   connect( button( QWizard::BackButton ), SIGNAL( clicked() ), SIGNAL( hideZones() ) );
70 }
71
72 HYDROGUI_CalculationDlg::~HYDROGUI_CalculationDlg()
73 {
74 }
75
76 void HYDROGUI_CalculationDlg::reset()
77 {
78   myObjectName->clear();
79   myGeomObjects->clear();
80   myPolylineName->clear();
81   myAvailableGeomObjects->clear();
82 }
83
84 QWizardPage* HYDROGUI_CalculationDlg::createObjectsPage() {
85   QWizardPage* aPage = new QWizardPage( mainFrame() );
86   QFrame* aFrame = new QFrame( aPage );
87
88   // Calculation name
89   myObjectName = new QLineEdit( aPage );
90   myValidator = new HYDROGUI_NameValidator(module(), myObjectName);
91   myObjectName->setValidator( myValidator );
92
93   connect( myValidator, SIGNAL( emptyName() ), SLOT( onEmptyName() ) );
94   connect( myValidator, SIGNAL( alreadyExists( QString ) ), SLOT( onAlreadyExists( QString ) ) );
95
96   myPolylineName = new QComboBox( aPage );
97   connect( myPolylineName, SIGNAL( activated( const QString & ) ), 
98     SIGNAL( boundarySelected( const QString & ) ) );
99
100   myGeomObjects = new QListWidget( aPage );
101   myGeomObjects->setSelectionMode( QListWidget::SingleSelection );
102   myGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
103   myGeomObjects->setViewMode( QListWidget::ListMode );
104   myGeomObjects->setSortingEnabled( true );
105
106   myAvailableGeomObjects = new QListWidget( aPage );
107   myAvailableGeomObjects->setSelectionMode( QListWidget::SingleSelection );
108   myAvailableGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
109   myAvailableGeomObjects->setViewMode( QListWidget::ListMode );
110   myAvailableGeomObjects->setSortingEnabled( true );
111
112   connect( myGeomObjects, SIGNAL( currentTextChanged( const QString & ) ), 
113     SIGNAL( objectSelected( const QString & ) ) );
114
115   QFrame* anObjectsFrame = new QFrame( aPage );
116   QGridLayout* anObjsLayout = new QGridLayout( anObjectsFrame );
117   anObjsLayout->setMargin( 5 );
118   anObjsLayout->setSpacing( 5 );
119   anObjectsFrame->setLayout( anObjsLayout );
120
121   QFrame* aBtnsFrame = new QFrame( anObjectsFrame );
122   QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
123   aBtnsLayout->setMargin( 5 );
124   aBtnsLayout->setSpacing( 5 );
125   aBtnsFrame->setLayout( aBtnsLayout );
126   QPushButton* anAddBtn = new QPushButton( tr("INCLUDE"), aBtnsFrame );
127   QPushButton* aRemoveBtn = new QPushButton( tr("EXCLUDE"), aBtnsFrame );
128
129   // Fill the butons frame with two buttons
130   aBtnsLayout->addWidget( anAddBtn );
131   aBtnsLayout->addWidget( aRemoveBtn );
132   aBtnsLayout->addStretch( 1 );
133
134   QLabel* anIncludedLabel = new QLabel( tr( "INCLUDED_OBJECTS" ), anObjectsFrame );
135   QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), anObjectsFrame );
136
137   // Fill the objects frame with two lists, two labels and with buttons frame
138   anObjsLayout->addWidget( anObjectsLabel, 0, 0, Qt::AlignHCenter );
139   anObjsLayout->addWidget( anIncludedLabel, 0, 2, Qt::AlignHCenter );
140   anObjsLayout->addWidget( myAvailableGeomObjects, 1, 0, Qt::AlignHCenter );
141   anObjsLayout->addWidget( aBtnsFrame, 1, 1, Qt::AlignHCenter );
142   anObjsLayout->addWidget( myGeomObjects, 1, 2, Qt::AlignHCenter );
143
144
145   QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
146   QLabel* aLimitsLabel = new QLabel( tr( "LIMITS" ), aPage );
147
148   // Fill the page
149   QGridLayout* aPageLayout = new QGridLayout( aPage );
150   aPageLayout->setMargin( 5 );
151   aPageLayout->setSpacing( 5 );
152   aPageLayout->setVerticalSpacing( 10 );
153   aPageLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
154   aPageLayout->addWidget( myObjectName,   0, 1 );
155   aPageLayout->addWidget( aLimitsLabel,   1, 0, Qt::AlignHCenter );
156   aPageLayout->addWidget( myPolylineName, 1, 1 );
157   aPageLayout->addWidget( anObjectsFrame, 2, 0, 1, 2, Qt::AlignHCenter );
158
159   aPage->setLayout( aPageLayout );
160
161   connect( anAddBtn, SIGNAL( clicked() ), SIGNAL( addObjects() ) );
162   connect( aRemoveBtn, SIGNAL( clicked() ), SIGNAL( removeObjects() ) );
163
164   return aPage;
165 }
166
167 void HYDROGUI_CalculationDlg::onEmptyName()
168 {
169   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
170   QString aMessage = QObject::tr( "INCORRECT_OBJECT_NAME" );
171   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
172 }
173
174 void HYDROGUI_CalculationDlg::onAlreadyExists( QString theName )
175 {
176   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
177   QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
178   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
179 }
180
181 QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
182   QWizardPage* aPage = new QWizardPage( mainFrame() );
183   QFrame* aFrame = new QFrame( aPage );
184
185   QGridLayout* aLayout = new QGridLayout( aPage );
186   
187   myBrowser = new HYDROGUI_DataBrowser( module(), 0, aPage );
188   myBrowser->setAutoOpenLevel( 3 );
189   aLayout->setMargin( 5 );
190   aLayout->setSpacing( 5 );
191
192   aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
193
194   myBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
195   myBathymetryChoice = new QComboBox( aFrame );
196
197   myBathymetryChoice->setVisible( false );
198   myBatimetryLabel->setVisible( false );
199
200   aLayout->addWidget( myBatimetryLabel, 1, 0 );
201   aLayout->addWidget( myBathymetryChoice, 1, 1 );
202
203   aPage->setLayout( aLayout );
204
205   connect( myBrowser, SIGNAL( clicked( SUIT_DataObject* ) ), SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ) );
206   connect( myBrowser, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
207   connect( myBathymetryChoice, SIGNAL( activated( int ) ), SLOT( onMergeTypeSelected( int ) ) );
208   connect( myBrowser, 
209       SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ),
210       SLOT( onZonesDropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ) );
211
212   return aPage;
213 }
214
215 void HYDROGUI_CalculationDlg::onZonesDropped( const QList<SUIT_DataObject*>& theList, 
216     SUIT_DataObject* theTargetParent, int theTargetRow, Qt::DropAction theDropAction )
217 {
218   QList<SUIT_DataObject*> aZonesList;
219   HYDROGUI_Zone* aZone;
220   // Get a list of dropped zones
221   for ( int i = 0; i < theList.length(); i++ )
222   {
223     aZone = dynamic_cast<HYDROGUI_Zone*>( theList.at( i ) );
224     if ( aZone )
225     {
226       aZonesList.append( aZone );
227     }
228   }
229   if ( aZonesList.length() > 0 )
230   {
231     // Get the target region
232     HYDROGUI_NamedObject* aRegionsRoot = dynamic_cast<HYDROGUI_NamedObject*>(theTargetParent);
233     if ( aRegionsRoot )
234     {
235       // Create a new region
236       emit createRegion( aZonesList );
237     }
238     else
239     {
240       HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theTargetParent);
241       if ( aRegion )
242       {
243         emit moveZones( theTargetParent, aZonesList );
244       }
245     }
246   }
247 }
248
249 void HYDROGUI_CalculationDlg::onMergeTypeSelected( int theIndex )
250 {
251   int aType = myBathymetryChoice->itemData( theIndex ).toInt();
252   QString aText = myBathymetryChoice->itemText( theIndex );
253   emit setMergeType( aType, aText );
254 }
255
256 void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
257 {
258   bool doShow = false;
259   HYDROGUI_Zone* aZone = dynamic_cast<HYDROGUI_Zone*>( theObject );
260   if ( aZone )
261   {
262     doShow = aZone->isMergingNeed();
263   }
264
265   if ( doShow )
266   {
267     // Fill the merge type combo box
268     bool prevBlock = myBathymetryChoice->blockSignals( true );
269     myCurrentZone = aZone;
270     myBathymetryChoice->clear();
271     myBathymetryChoice->addItem( tr("MERGE_UNKNOWN"), HYDROData_Zone::Merge_UNKNOWN );
272     myBathymetryChoice->addItem( tr("MERGE_ZMIN"), HYDROData_Zone::Merge_ZMIN );
273     myBathymetryChoice->addItem( tr("MERGE_ZMAX"), HYDROData_Zone::Merge_ZMAX );
274     QStringList aList = aZone->getBathymetries();
275     for ( int i = 0; i < aList.length(); i++ )
276     {
277       myBathymetryChoice->addItem( aList.at( i ), HYDROData_Zone::Merge_Object );
278     }
279     // Select the current choice if any
280     int aCurIndex = 0;
281     switch ( aZone->getMergeType() )
282     {
283       case HYDROData_Zone::Merge_ZMIN:
284         aCurIndex = 1;
285         break;
286       case HYDROData_Zone::Merge_ZMAX:
287         aCurIndex = 2;
288         break;
289       case HYDROData_Zone::Merge_Object:
290         aCurIndex = 3 + aList.indexOf( aZone->text( HYDROGUI_DataObject::BathymetryId ) );
291         break;
292       default:
293         aCurIndex = 0; // Select unknown by default
294     }
295     myBathymetryChoice->setCurrentIndex( aCurIndex );
296     myBathymetryChoice->blockSignals( prevBlock );
297   }
298
299   myBathymetryChoice->setVisible( doShow );
300   myBatimetryLabel->setVisible( doShow );
301 }
302
303 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
304 {
305   myObjectName->setText( theName );
306 }
307
308 QString HYDROGUI_CalculationDlg::getObjectName() const
309 {
310   return myObjectName->text();
311 }
312
313 void HYDROGUI_CalculationDlg::includeGeomObjects( const QStringList& theObjects )
314 {
315   QList<QListWidgetItem*> aFoundItems;
316   int anIdx;
317   QListWidgetItem* anItem;
318
319   for ( int i = 0, n = theObjects.length(); i < n; ++i )
320   {
321     QString anObjName = theObjects.at( i );
322     aFoundItems = myAvailableGeomObjects->findItems( anObjName, Qt::MatchExactly );
323     for ( anIdx = 0; anIdx < aFoundItems.length(); anIdx++ )
324     {
325       anItem = aFoundItems.at( anIdx );
326       // Remove this object from available objects list
327       anItem = myAvailableGeomObjects->takeItem( myAvailableGeomObjects->row( anItem ) );
328       // Add the item to the included objects list
329       myGeomObjects->addItem( anItem );
330     }
331   }
332 }
333
334 void HYDROGUI_CalculationDlg::excludeGeomObjects( const QStringList& theObjects )
335 {
336   QList<QListWidgetItem*> aFoundItems;
337   int anIdx;
338   QListWidgetItem* anItem;
339
340   for ( int i = 0, n = theObjects.length(); i < n; ++i )
341   {
342     QString anObjName = theObjects.at( i );
343     aFoundItems = myGeomObjects->findItems( anObjName, Qt::MatchExactly );
344     for ( anIdx = 0; anIdx < aFoundItems.length(); anIdx++ )
345     {
346       anItem = aFoundItems.at( anIdx );
347       // Remove this object from included objects list
348       anItem = myGeomObjects->takeItem( myGeomObjects->row( anItem ) );
349       // Add the item to the excluded objects list
350       myAvailableGeomObjects->addItem( anItem );
351     }
352   }
353 }
354
355 void HYDROGUI_CalculationDlg::setBoundary( QString& theObjName )
356 {
357   bool isBlocked = myPolylineName->blockSignals( true );
358   myPolylineName->setCurrentIndex( myPolylineName->findText( theObjName ) );
359   myPolylineName->blockSignals( isBlocked );
360 }
361
362 void HYDROGUI_CalculationDlg::setPolylineNames( const QStringList& theObjects, const QStringList& theObjectsEntries )
363 {
364   myPolylineName->clear();
365   myPolylineName->addItem( "", "" ); // No boundary item
366
367   for ( int i = 0, n = theObjects.length(); i < n; ++i )
368   {
369     myPolylineName->addItem( theObjects.at( i ), theObjectsEntries.at( i ) );
370   }
371 }
372 void HYDROGUI_CalculationDlg::setAllGeomObjects( const QStringList& theObjects, const QStringList& theObjectsEntries )
373 {
374   myAvailableGeomObjects->clear();
375
376   for ( int i = 0, n = theObjects.length(); i < n; ++i )
377   {
378     QString anObjName = theObjects.at( i );
379
380     QListWidgetItem* aListItem = new QListWidgetItem( anObjName, myAvailableGeomObjects );
381     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
382     aListItem->setData( Qt::UserRole, theObjectsEntries.at( i ) );
383   }
384 }
385
386 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
387 {
388   QStringList aResList;
389   QList<QListWidgetItem*> aList = myGeomObjects->selectedItems();
390   for ( int i = 0, n = aList.length(); i < n; ++i )
391   {
392     aResList.append( aList.at( i )->text() );
393   }
394   return aResList;
395 }
396
397 QStringList HYDROGUI_CalculationDlg::getSelectedAvailableGeomObjects() const
398 {
399   QStringList aResList;
400   QList<QListWidgetItem*> aList = myAvailableGeomObjects->selectedItems();
401   for ( int i = 0, n = aList.length(); i < n; ++i )
402   {
403     aResList.append( aList.at( i )->text() );
404   }
405   return aResList;
406 }
407
408 void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_CalculationCase) theCase )
409 {
410   myEditedObject = theCase;
411   myValidator->setEditedObject( theCase );
412
413   HYDROGUI_DataObject* anobj = new HYDROGUI_DataObject( 0, NULL, "" );
414   myBrowser->setRoot(anobj);
415
416   module()->getDataModel()->buildCaseTree( anobj, myEditedObject );
417   myBrowser->updateTree();
418   myBrowser->openLevels();
419   myBrowser->adjustColumnsWidth();
420 }
421
422 HYDROGUI_Zone* HYDROGUI_CalculationDlg::getCurrentZone() const
423 {
424   return myCurrentZone;
425 }
426
427 void HYDROGUI_CalculationDlg::refreshZonesBrowser()
428 {
429   myBrowser->updateTree();
430 }