1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_CalculationDlg.h"
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"
35 #include <HYDROData_Document.h>
36 #include <HYDROData_Entity.h>
38 #include <CAM_Application.h>
39 #include <LightApp_DataObject.h>
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>
47 #include <LightApp_Application.h>
48 #include <SUIT_Desktop.h>
49 #include <SUIT_MessageBox.h>
55 #include <QListWidget>
57 #include <QPushButton>
58 #include <QToolButton>
59 #include <QWizardPage>
61 #include <QStackedWidget>
63 HYDROGUI_CalculationDlg::HYDROGUI_CalculationDlg( HYDROGUI_Module* theModule, const QString& theTitle )
64 : HYDROGUI_Wizard( theModule, theTitle )
66 addPage( createObjectsPage() );
67 addPage( createGroupsPage() );
68 addPage( createZonesPage() );
71 HYDROGUI_CalculationDlg::~HYDROGUI_CalculationDlg()
75 void HYDROGUI_CalculationDlg::reset()
77 myObjectName->clear();
78 myGeomObjects->clear();
79 myPolylineName->clear();
80 myAvailableGeomObjects->clear();
83 QWizardPage* HYDROGUI_CalculationDlg::createObjectsPage() {
84 QWizardPage* aPage = new QWizardPage( mainFrame() );
85 QFrame* aFrame = new QFrame( aPage );
88 myObjectName = new QLineEdit( aPage );
89 myValidator = new HYDROGUI_NameValidator(module(), myObjectName);
90 myObjectName->setValidator( myValidator );
92 connect( myValidator, SIGNAL( emptyName() ), SLOT( onEmptyName() ) );
93 connect( myValidator, SIGNAL( alreadyExists( QString ) ), SLOT( onAlreadyExists( QString ) ) );
95 myPolylineName = new QComboBox( aPage );
96 connect( myPolylineName, SIGNAL( activated( const QString & ) ),
97 SIGNAL( boundarySelected( const QString & ) ) );
99 myGeomObjects = new QListWidget( aPage );
100 myGeomObjects->setSelectionMode( QListWidget::ExtendedSelection );
101 myGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
102 myGeomObjects->setViewMode( QListWidget::ListMode );
103 myGeomObjects->setSortingEnabled( true );
105 myAvailableGeomObjects = new QListWidget( aPage );
106 myAvailableGeomObjects->setSelectionMode( QListWidget::ExtendedSelection );
107 myAvailableGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
108 myAvailableGeomObjects->setViewMode( QListWidget::ListMode );
109 myAvailableGeomObjects->setSortingEnabled( true );
111 connect( myGeomObjects, SIGNAL( itemSelectionChanged() ),
112 SIGNAL( objectsSelected() ) );
114 QFrame* anObjectsFrame = new QFrame( aPage );
115 QGridLayout* anObjsLayout = new QGridLayout( anObjectsFrame );
116 anObjsLayout->setMargin( 5 );
117 anObjsLayout->setSpacing( 5 );
118 anObjectsFrame->setLayout( anObjsLayout );
120 QFrame* aBtnsFrame = new QFrame( anObjectsFrame );
121 QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
122 aBtnsLayout->setMargin( 5 );
123 aBtnsLayout->setSpacing( 5 );
124 aBtnsFrame->setLayout( aBtnsLayout );
125 QPushButton* anAddBtn = new QPushButton( tr("INCLUDE"), aBtnsFrame );
126 QPushButton* aRemoveBtn = new QPushButton( tr("EXCLUDE"), aBtnsFrame );
128 // Fill the butons frame with two buttons
129 aBtnsLayout->addWidget( anAddBtn );
130 aBtnsLayout->addWidget( aRemoveBtn );
131 aBtnsLayout->addStretch( 1 );
133 QLabel* anIncludedLabel = new QLabel( tr( "INCLUDED_OBJECTS" ), anObjectsFrame );
134 QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), anObjectsFrame );
136 // Fill the objects frame with two lists, two labels and with buttons frame
137 anObjsLayout->addWidget( anObjectsLabel, 0, 0, Qt::AlignHCenter );
138 anObjsLayout->addWidget( anIncludedLabel, 0, 2, Qt::AlignHCenter );
139 anObjsLayout->addWidget( myAvailableGeomObjects, 1, 0, Qt::AlignHCenter );
140 anObjsLayout->addWidget( aBtnsFrame, 1, 1, Qt::AlignHCenter );
141 anObjsLayout->addWidget( myGeomObjects, 1, 2, Qt::AlignHCenter );
144 QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
145 QLabel* aLimitsLabel = new QLabel( tr( "LIMITS" ), aPage );
148 QGridLayout* aPageLayout = new QGridLayout( aPage );
149 aPageLayout->setMargin( 5 );
150 aPageLayout->setSpacing( 5 );
151 aPageLayout->setVerticalSpacing( 10 );
152 aPageLayout->addWidget( aNameLabel, 0, 0, Qt::AlignHCenter );
153 aPageLayout->addWidget( myObjectName, 0, 1 );
154 aPageLayout->addWidget( aLimitsLabel, 1, 0, Qt::AlignHCenter );
155 aPageLayout->addWidget( myPolylineName, 1, 1 );
156 aPageLayout->addWidget( anObjectsFrame, 2, 0, 1, 2, Qt::AlignHCenter );
158 aPage->setLayout( aPageLayout );
160 connect( anAddBtn, SIGNAL( clicked() ), SIGNAL( addObjects() ) );
161 connect( aRemoveBtn, SIGNAL( clicked() ), SIGNAL( removeObjects() ) );
166 QWizardPage* HYDROGUI_CalculationDlg::createGroupsPage() {
167 QWizardPage* aPage = new QWizardPage( mainFrame() );
168 QFrame* aFrame = new QFrame( aPage );
170 myGroups = new QListWidget( aPage );
171 myGroups->setSelectionMode( QListWidget::ExtendedSelection );
172 myGroups->setEditTriggers( QListWidget::NoEditTriggers );
173 myGroups->setViewMode( QListWidget::ListMode );
174 myGroups->setSortingEnabled( true );
176 myAvailableGroups = new QListWidget( aPage );
177 myAvailableGroups->setSelectionMode( QListWidget::ExtendedSelection );
178 myAvailableGroups->setEditTriggers( QListWidget::NoEditTriggers );
179 myAvailableGroups->setViewMode( QListWidget::ListMode );
180 myAvailableGroups->setSortingEnabled( true );
182 connect( myGroups, SIGNAL( itemSelectionChanged() ),
183 SIGNAL( groupsSelected() ) );
185 QFrame* aGroupsFrame = new QFrame( aPage );
186 QGridLayout* aGroupsLayout = new QGridLayout( aGroupsFrame );
187 aGroupsLayout->setMargin( 5 );
188 aGroupsLayout->setSpacing( 5 );
189 aGroupsFrame->setLayout( aGroupsLayout );
191 QFrame* aBtnsFrame = new QFrame( aGroupsFrame );
192 QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
193 aBtnsLayout->setMargin( 5 );
194 aBtnsLayout->setSpacing( 5 );
195 aBtnsFrame->setLayout( aBtnsLayout );
196 QPushButton* anAddBtn = new QPushButton( tr("INCLUDE"), aBtnsFrame );
197 QPushButton* aRemoveBtn = new QPushButton( tr("EXCLUDE"), aBtnsFrame );
199 // Fill the butons frame with two buttons
200 aBtnsLayout->addWidget( anAddBtn );
201 aBtnsLayout->addWidget( aRemoveBtn );
202 aBtnsLayout->addStretch( 1 );
204 QLabel* anIncludedLabel = new QLabel( tr( "INCLUDED_GROUPS" ), aGroupsFrame );
205 QLabel* anAvailableLabel = new QLabel( tr( "AVAILABLE_GROUPS" ), aGroupsFrame );
207 // Fill the objects frame with two lists, two labels and with buttons frame
208 aGroupsLayout->addWidget( anAvailableLabel, 0, 0, Qt::AlignHCenter );
209 aGroupsLayout->addWidget( anIncludedLabel, 0, 2, Qt::AlignHCenter );
210 aGroupsLayout->addWidget( myAvailableGroups, 1, 0, Qt::AlignHCenter );
211 aGroupsLayout->addWidget( aBtnsFrame, 1, 1, Qt::AlignHCenter );
212 aGroupsLayout->addWidget( myGroups, 1, 2, Qt::AlignHCenter );
215 QGridLayout* aPageLayout = new QGridLayout( aPage );
216 aPageLayout->setMargin( 5 );
217 aPageLayout->setSpacing( 5 );
218 aPageLayout->setVerticalSpacing( 10 );
219 aPageLayout->addWidget( aGroupsFrame, 0, 0, Qt::AlignHCenter );
221 aPage->setLayout( aPageLayout );
223 connect( anAddBtn, SIGNAL( clicked() ), SIGNAL( addGroups() ) );
224 connect( aRemoveBtn, SIGNAL( clicked() ), SIGNAL( removeGroups() ) );
229 QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
230 QWizardPage* aPage = new QWizardPage( mainFrame() );
231 QFrame* aFrame = new QFrame( aPage );
233 QGridLayout* aLayout = new QGridLayout( aPage );
235 myBrowser = new HYDROGUI_DataBrowser( module(), NULL, aPage );
236 myBrowser->setAutoOpenLevel( 3 );
237 aLayout->setMargin( 5 );
238 aLayout->setSpacing( 5 );
240 aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
242 myBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
243 myBathymetryChoice = new QComboBox( aFrame );
245 myBathymetryChoice->setVisible( false );
246 myBatimetryLabel->setVisible( false );
248 aLayout->addWidget( myBatimetryLabel, 1, 0 );
249 aLayout->addWidget( myBathymetryChoice, 1, 1 );
251 aPage->setLayout( aLayout );
253 connect( myBrowser, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
254 connect( myBrowser, SIGNAL( clicked( SUIT_DataObject* ) ), SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ) );
255 connect( myBrowser, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
256 connect( myBathymetryChoice, SIGNAL( activated( int ) ), SLOT( onMergeTypeSelected( int ) ) );
258 SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ),
259 SLOT( onZonesDropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ) );
265 bool HYDROGUI_CalculationDlg::acceptCurrent() const
269 if ( myGeomObjects->count() == 0 )
271 anErrorMsg = tr( "EMPTY_GEOMETRY_OBJECTS" );
274 if ( !anErrorMsg.isEmpty() )
276 anErrorMsg += "\n" + tr( "INPUT_VALID_DATA" );
278 QString aTitle = tr( "INSUFFICIENT_INPUT_DATA" );
279 SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, anErrorMsg );
282 return anErrorMsg.isEmpty();
285 void HYDROGUI_CalculationDlg::onEmptyName()
287 QString aTitle = tr( "INSUFFICIENT_INPUT_DATA" );
288 QString aMessage = tr( "INCORRECT_OBJECT_NAME" ) + "\n" + tr( "INPUT_VALID_DATA" );
289 SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
292 void HYDROGUI_CalculationDlg::onAlreadyExists( QString theName )
294 QString aTitle = tr( "INSUFFICIENT_INPUT_DATA" );
295 QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName ) +
296 "\n" + tr( "INPUT_VALID_DATA" );
297 SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
300 void HYDROGUI_CalculationDlg::onZonesDropped( const QList<SUIT_DataObject*>& theList,
301 SUIT_DataObject* theTargetParent, int theTargetRow, Qt::DropAction theDropAction )
303 QList<SUIT_DataObject*> aZonesList;
304 HYDROGUI_Zone* aZone;
305 // Get a list of dropped zones
306 for ( int i = 0; i < theList.length(); i++ )
308 aZone = dynamic_cast<HYDROGUI_Zone*>( theList.at( i ) );
311 aZonesList.append( aZone );
314 if ( aZonesList.length() > 0 )
316 // Get the target region
317 HYDROGUI_NamedObject* aRegionsRoot = dynamic_cast<HYDROGUI_NamedObject*>(theTargetParent);
320 // Create a new region
321 emit createRegion( aZonesList );
325 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theTargetParent);
328 emit moveZones( theTargetParent, aZonesList );
334 void HYDROGUI_CalculationDlg::onMergeTypeSelected( int theIndex )
336 int aType = myBathymetryChoice->itemData( theIndex ).toInt();
337 QString aText = myBathymetryChoice->itemText( theIndex );
338 emit setMergeType( aType, aText );
341 void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
344 HYDROGUI_Zone* aZone = dynamic_cast<HYDROGUI_Zone*>( theObject );
347 doShow = aZone->isMergingNeed();
352 // Fill the merge type combo box
353 bool prevBlock = myBathymetryChoice->blockSignals( true );
354 myCurrentZone = aZone;
355 myBathymetryChoice->clear();
356 myBathymetryChoice->addItem( tr("MERGE_UNKNOWN"), HYDROData_Zone::Merge_UNKNOWN );
357 myBathymetryChoice->addItem( tr("MERGE_ZMIN"), HYDROData_Zone::Merge_ZMIN );
358 myBathymetryChoice->addItem( tr("MERGE_ZMAX"), HYDROData_Zone::Merge_ZMAX );
359 QStringList aList = aZone->getAltitudes();
360 for ( int i = 0; i < aList.length(); i++ )
362 myBathymetryChoice->addItem( aList.at( i ), HYDROData_Zone::Merge_Object );
364 // Select the current choice if any
366 switch ( aZone->getMergeType() )
368 case HYDROData_Zone::Merge_ZMIN:
371 case HYDROData_Zone::Merge_ZMAX:
374 case HYDROData_Zone::Merge_Object:
375 aCurIndex = 3 + aList.indexOf( aZone->text( HYDROGUI_DataObject::AltitudeObjId ) );
378 aCurIndex = 0; // Select unknown by default
380 myBathymetryChoice->setCurrentIndex( aCurIndex );
381 myBathymetryChoice->blockSignals( prevBlock );
384 myBathymetryChoice->setVisible( doShow );
385 myBatimetryLabel->setVisible( doShow );
388 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
390 myObjectName->setText( theName );
393 QString HYDROGUI_CalculationDlg::getObjectName() const
395 return myObjectName->text();
398 void moveItems( QListWidget* theSource, QListWidget* theDest, const QStringList& theObjects )
400 QList<QListWidgetItem*> aFoundItems;
402 QListWidgetItem* anItem;
404 for ( int i = 0, n = theObjects.length(); i < n; ++i )
406 QString anObjName = theObjects.at( i );
407 aFoundItems = theSource->findItems( anObjName, Qt::MatchExactly );
408 for ( anIdx = 0; anIdx < aFoundItems.length(); anIdx++ )
410 anItem = aFoundItems.at( anIdx );
411 // Remove this object from available objects list
412 anItem = theSource->takeItem( theSource->row( anItem ) );
413 // Add the item to the included objects list
414 theDest->addItem( anItem );
419 void HYDROGUI_CalculationDlg::includeGeomObjects( const QStringList& theObjects )
421 moveItems( myAvailableGeomObjects, myGeomObjects, theObjects );
424 void HYDROGUI_CalculationDlg::excludeGeomObjects( const QStringList& theObjects )
426 moveItems( myGeomObjects, myAvailableGeomObjects, theObjects );
429 void HYDROGUI_CalculationDlg::setBoundary( const QString& theObjName )
431 bool isBlocked = myPolylineName->blockSignals( true );
432 myPolylineName->setCurrentIndex( myPolylineName->findText( theObjName ) );
433 myPolylineName->blockSignals( isBlocked );
436 void HYDROGUI_CalculationDlg::setPolylineNames( const QStringList& theObjects, const QStringList& theObjectsEntries )
438 myPolylineName->clear();
439 myPolylineName->addItem( "", "" ); // No boundary item
441 for ( int i = 0, n = theObjects.length(); i < n; ++i )
443 myPolylineName->addItem( theObjects.at( i ), theObjectsEntries.at( i ) );
446 void HYDROGUI_CalculationDlg::setAllGeomObjects( const QStringList& theObjects, const QStringList& theObjectsEntries )
448 myAvailableGeomObjects->clear();
450 for ( int i = 0, n = theObjects.length(); i < n; ++i )
452 QString anObjName = theObjects.at( i );
454 QListWidgetItem* aListItem = new QListWidgetItem( anObjName, myAvailableGeomObjects );
455 aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
456 aListItem->setData( Qt::UserRole, theObjectsEntries.at( i ) );
460 QStringList getSelected( QListWidget* theWidget )
462 QStringList aResList;
463 QList<QListWidgetItem*> aList = theWidget->selectedItems();
464 for ( int i = 0, n = aList.length(); i < n; ++i )
466 aResList.append( aList.at( i )->text() );
471 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
473 return getSelected( myGeomObjects );
476 QStringList HYDROGUI_CalculationDlg::getSelectedAvailableGeomObjects() const
478 return getSelected( myAvailableGeomObjects );
481 void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_CalculationCase) theCase )
483 myEditedObject = theCase;
484 myValidator->setEditedObject( theCase );
486 // Build the calculation case subtree
487 module()->getDataModel()->buildCaseTree( myBrowser->root(), myEditedObject );
489 myBrowser->updateTree();
490 myBrowser->openLevels();
491 myBrowser->adjustColumnsWidth();
492 myBrowser->setAutoUpdate( true );
493 myBrowser->setUpdateModified( true );
497 HYDROGUI_Zone* HYDROGUI_CalculationDlg::getCurrentZone() const
499 return myCurrentZone;
502 void HYDROGUI_CalculationDlg::refreshZonesBrowser()
504 SUIT_DataObject* aRoot = myBrowser->root();
505 module()->getDataModel()->updateObjectTree( myEditedObject );
506 module()->getDataModel()->buildCaseTree( aRoot, myEditedObject );
507 myBrowser->updateTree( aRoot );
510 void HYDROGUI_CalculationDlg::onDataChanged()
512 SUIT_DataObject* aRoot = myBrowser->root();
513 module()->getDataModel()->buildCaseTree( aRoot, myEditedObject );
514 myBrowser->updateTree( aRoot );
517 void HYDROGUI_CalculationDlg::setAvailableGroups( const QStringList& theGroups )
519 myAvailableGroups->clear();
521 foreach( QString aGroup, theGroups )
522 myAvailableGroups->addItem( aGroup );
525 QStringList HYDROGUI_CalculationDlg::getSelectedGroups() const
527 return getSelected( myGroups );
530 QStringList HYDROGUI_CalculationDlg::getSelectedAvailableGroups() const
532 return getSelected( myAvailableGroups );
535 void HYDROGUI_CalculationDlg::includeGroups( const QStringList& theObjects )
537 moveItems( myAvailableGroups, myGroups, theObjects );
540 void HYDROGUI_CalculationDlg::excludeGroups( const QStringList& theObjects )
542 moveItems( myGroups, myAvailableGroups, theObjects );