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