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