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