Salome HOME
Minor change.
[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
62 HYDROGUI_CalculationDlg::HYDROGUI_CalculationDlg( HYDROGUI_Module* theModule, const QString& theTitle )
63 : HYDROGUI_Wizard( theModule, theTitle )
64 {
65   QWizard* aWizard = wizard();
66   aWizard->addPage( createObjectsPage() );
67   aWizard->addPage( createZonesPage() );
68   connect( aWizard->button( QWizard::NextButton ), SIGNAL( clicked() ), SIGNAL( splitZones() ) );
69   aWizard->show();
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( wizard() );
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() ), this, SLOT( onEmptyName() ) );
92   connect( myValidator, SIGNAL( alreadyExists( QString ) ), this, SLOT( onAlreadyExists( QString ) ) );
93
94   myGeomObjects = new QListWidget( aPage );
95   myGeomObjects->setSelectionMode( QListWidget::SingleSelection );
96   myGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
97   myGeomObjects->setViewMode( QListWidget::ListMode );
98   myGeomObjects->setSortingEnabled( true );
99
100   QFrame* aBtnsFrame = new QFrame( aPage );
101   QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
102   aBtnsLayout->setMargin( 5 );
103   aBtnsLayout->setSpacing( 5 );
104   aBtnsFrame->setLayout( aBtnsLayout );
105   QPushButton* anAddBtn = new QPushButton( tr("ADD"), aBtnsFrame );
106   QPushButton* aRemoveBtn = new QPushButton( tr("REMOVE"), aBtnsFrame );
107   aBtnsLayout->addWidget( anAddBtn );
108   aBtnsLayout->addWidget( aRemoveBtn );
109   aBtnsLayout->addStretch( 1 );
110
111   QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
112   QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), aPage );
113
114   QGridLayout* aZonesLayout = new QGridLayout( aPage );
115   aZonesLayout->setMargin( 5 );
116   aZonesLayout->setSpacing( 5 );
117   aZonesLayout->setVerticalSpacing( 10 );
118   aZonesLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
119   aZonesLayout->addWidget( myObjectName,   0, 1 );
120   aZonesLayout->addWidget( anObjectsLabel, 1, 0, Qt::AlignHCenter );
121   aZonesLayout->addWidget( myGeomObjects,  1, 1, 2, 1 );
122   aZonesLayout->addWidget( aBtnsFrame,     2, 0, Qt::AlignHCenter );
123
124   aPage->setLayout( aZonesLayout );
125
126   connect( anAddBtn, SIGNAL( clicked() ), this, SIGNAL( addObjects() ) );
127   connect( aRemoveBtn, SIGNAL( clicked() ), this, SIGNAL( removeObjects() ) );
128
129   return aPage;
130 }
131
132 void HYDROGUI_CalculationDlg::onEmptyName()
133 {
134   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
135   QString aMessage = QObject::tr( "INCORRECT_OBJECT_NAME" );
136   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
137 }
138
139 void HYDROGUI_CalculationDlg::onAlreadyExists( QString theName )
140 {
141   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
142   QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
143   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
144 }
145
146 QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
147   QWizardPage* aPage = new QWizardPage( wizard() );
148   QFrame* aFrame = new QFrame( aPage );
149
150   QGridLayout* aLayout = new QGridLayout( aPage );
151   
152   myBrowser = new HYDROGUI_DataBrowser( module(), 0, aPage );
153   myBrowser->setAutoOpenLevel( 3 );
154   aLayout->setMargin( 5 );
155   aLayout->setSpacing( 5 );
156
157   aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
158
159   myBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
160   myBathymetryChoice = new QComboBox( aFrame );
161
162   myBathymetryChoice->setVisible( false );
163   myBatimetryLabel->setVisible( false );
164
165   aLayout->addWidget( myBatimetryLabel, 1, 0 );
166   aLayout->addWidget( myBathymetryChoice, 1, 1 );
167
168   aPage->setLayout( aLayout );
169
170   connect( myBrowser, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
171   connect( myBathymetryChoice, SIGNAL( activated( int ) ), SLOT( onMergeTypeSelected( int ) ) );
172   connect( myBrowser, 
173       SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ),
174       SLOT( onZonesDropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ) );
175
176   return aPage;
177 }
178
179 void HYDROGUI_CalculationDlg::onZonesDropped( const QList<SUIT_DataObject*>& theList, 
180     SUIT_DataObject* theTargetParent, int theTargetRow, Qt::DropAction theDropAction )
181 {
182   QList<SUIT_DataObject*> aZonesList;
183   HYDROGUI_Zone* aZone;
184   // Get a list of dropped zones
185   for ( int i = 0; i < theList.length(); i++ )
186   {
187     aZone = dynamic_cast<HYDROGUI_Zone*>( theList.at( i ) );
188     if ( aZone )
189     {
190       aZonesList.append( aZone );
191     }
192   }
193   if ( aZonesList.length() > 0 )
194   {
195     // Get the target region
196     HYDROGUI_NamedObject* aRegionsRoot = dynamic_cast<HYDROGUI_NamedObject*>(theTargetParent);
197     if ( aRegionsRoot )
198     {
199       // Create a new region
200       emit createRegion( aZonesList );
201     }
202     else
203     {
204       HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theTargetParent);
205       if ( aRegion )
206       {
207         emit moveZones( theTargetParent, aZonesList );
208       }
209     }
210   }
211 }
212
213 void HYDROGUI_CalculationDlg::onMergeTypeSelected( int theIndex )
214 {
215   int aType = myBathymetryChoice->itemData( theIndex ).toInt();
216   QString aText = myBathymetryChoice->itemText( theIndex );
217   emit setMergeType( aType, aText );
218 }
219
220 void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
221 {
222   bool doShow = false;
223   HYDROGUI_Zone* aZone = dynamic_cast<HYDROGUI_Zone*>( theObject );
224   if ( aZone )
225   {
226     doShow = aZone->isMergingNeed();
227   }
228
229   if ( doShow )
230   {
231     // Fill the merge type combo box
232     bool prevBlock = myBathymetryChoice->blockSignals( true );
233     myCurrentZone = aZone;
234     myBathymetryChoice->clear();
235     myBathymetryChoice->addItem( tr("MERGE_UNKNOWN"), HYDROData_Zone::Merge_UNKNOWN );
236     myBathymetryChoice->addItem( tr("MERGE_ZMIN"), HYDROData_Zone::Merge_ZMIN );
237     myBathymetryChoice->addItem( tr("MERGE_ZMAX"), HYDROData_Zone::Merge_ZMAX );
238     QStringList aList = aZone->getBathymetries();
239     for ( int i = 0; i < aList.length(); i++ )
240     {
241       myBathymetryChoice->addItem( aList.at( i ), HYDROData_Zone::Merge_Object );
242     }
243     // Select the current choice if any
244     int aCurIndex = 0;
245     switch ( aZone->getMergeType() )
246     {
247       case HYDROData_Zone::Merge_ZMIN:
248         aCurIndex = 1;
249         break;
250       case HYDROData_Zone::Merge_ZMAX:
251         aCurIndex = 2;
252         break;
253       case HYDROData_Zone::Merge_Object:
254         aCurIndex = 3 + aList.indexOf( aZone->text( HYDROGUI_DataObject::BathymetryId ) );
255         break;
256       default:
257         aCurIndex = 0; // Select unknown by default
258     }
259     myBathymetryChoice->setCurrentIndex( aCurIndex );
260     myBathymetryChoice->blockSignals( prevBlock );
261   }
262
263   myBathymetryChoice->setVisible( doShow );
264   myBatimetryLabel->setVisible( doShow );
265 }
266
267 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
268 {
269   myObjectName->setText( theName );
270 }
271
272 QString HYDROGUI_CalculationDlg::getObjectName() const
273 {
274   return myObjectName->text();
275 }
276
277 void HYDROGUI_CalculationDlg::setGeomObjects( const QStringList& theObjects )
278 {
279 }
280
281 void HYDROGUI_CalculationDlg::setSelectedGeomObjects( const QStringList& theObjects )
282 {
283   myGeomObjects->clear();
284
285   for ( int i = 0, n = theObjects.length(); i < n; ++i )
286   {
287     QString anObjName = theObjects.at( i );
288
289     QListWidgetItem* aListItem = new QListWidgetItem( anObjName, myGeomObjects );
290     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
291   }
292 }
293
294 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
295 {
296   QStringList aResList;
297   QList<QListWidgetItem*> aList = myGeomObjects->selectedItems();
298   for ( int i = 0, n = aList.length(); i < n; ++i )
299   {
300     aResList.append( aList.at( i )->text() );
301   }
302   return aResList;
303 }
304
305 void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_CalculationCase) theCase )
306 {
307   myEditedObject = theCase;
308   myValidator->setEditedObject( theCase );
309
310   HYDROGUI_DataObject* anobj = new HYDROGUI_DataObject( 0, NULL, "" );
311   myBrowser->setRoot(anobj);
312
313   module()->getDataModel()->buildCaseTree( anobj, myEditedObject );
314   myBrowser->updateTree();
315   myBrowser->openLevels();
316   myBrowser->adjustColumnsWidth();
317 }
318
319 HYDROGUI_Zone* HYDROGUI_CalculationDlg::getCurrentZone() const
320 {
321   return myCurrentZone;
322 }
323
324 void HYDROGUI_CalculationDlg::refreshZonesBrowser()
325 {
326   myBrowser->updateTree();
327 }