Salome HOME
- Merge type combo box in Calculation case dialog is filled and connected to get...
[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
33 #include <HYDROData_Document.h>
34 #include <HYDROData_Entity.h>
35 #include <HYDROGUI_Zone.h>
36
37 #include <CAM_Application.h>
38 #include <LightApp_DataObject.h>
39
40 #include <SUIT_DataObject.h>
41 #include <SUIT_FileDlg.h>
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_Session.h>
44 #include <SUIT_Study.h>
45
46 #include <LightApp_Application.h>
47 #include <SUIT_Desktop.h>
48 #include <SUIT_MessageBox.h>
49
50 #include <QGroupBox>
51 #include <QLabel>
52 #include <QLayout>
53 #include <QLineEdit>
54 #include <QListWidget>
55 #include <QPicture>
56 #include <QPushButton>
57 #include <QToolButton>
58 #include <QWizardPage>
59 #include <QComboBox>
60
61 HYDROGUI_CalculationDlg::HYDROGUI_CalculationDlg( HYDROGUI_Module* theModule, const QString& theTitle )
62 : HYDROGUI_Wizard( theModule, theTitle )
63 {
64   QWizard* aWizard = wizard();
65   aWizard->addPage( createObjectsPage() );
66   aWizard->addPage( createZonesPage() );
67   connect( aWizard->button( QWizard::NextButton ), SIGNAL( clicked() ), SIGNAL( splitZones() ) );
68   aWizard->show();
69 }
70
71 HYDROGUI_CalculationDlg::~HYDROGUI_CalculationDlg()
72 {
73 }
74
75 void HYDROGUI_CalculationDlg::reset()
76 {
77   myObjectName->clear();
78   myGeomObjects->clear();
79 }
80
81 QWizardPage* HYDROGUI_CalculationDlg::createObjectsPage() {
82   QWizardPage* aPage = new QWizardPage( wizard() );
83   QFrame* aFrame = new QFrame( aPage );
84
85   // Calculation name
86   myObjectName = new QLineEdit( aPage );
87   myValidator = new HYDROGUI_NameValidator(module(), myObjectName);
88   myObjectName->setValidator( myValidator );
89
90   connect( myValidator, SIGNAL( emptyName() ), this, SLOT( onEmptyName() ) );
91   connect( myValidator, SIGNAL( alreadyExists( QString ) ), this, SLOT( onAlreadyExists( QString ) ) );
92
93   myGeomObjects = new QListWidget( aPage );
94   myGeomObjects->setSelectionMode( QListWidget::SingleSelection );
95   myGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
96   myGeomObjects->setViewMode( QListWidget::ListMode );
97   myGeomObjects->setSortingEnabled( true );
98
99   QFrame* aBtnsFrame = new QFrame( aPage );
100   QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
101   aBtnsLayout->setMargin( 5 );
102   aBtnsLayout->setSpacing( 5 );
103   aBtnsFrame->setLayout( aBtnsLayout );
104   QPushButton* anAddBtn = new QPushButton( tr("ADD"), aBtnsFrame );
105   QPushButton* aRemoveBtn = new QPushButton( tr("REMOVE"), aBtnsFrame );
106   aBtnsLayout->addWidget( anAddBtn );
107   aBtnsLayout->addWidget( aRemoveBtn );
108   aBtnsLayout->addStretch( 1 );
109
110   QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
111   QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), aPage );
112
113   QGridLayout* aZonesLayout = new QGridLayout( aPage );
114   aZonesLayout->setMargin( 5 );
115   aZonesLayout->setSpacing( 5 );
116   aZonesLayout->setVerticalSpacing( 10 );
117   aZonesLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
118   aZonesLayout->addWidget( myObjectName,   0, 1 );
119   aZonesLayout->addWidget( anObjectsLabel, 1, 0, Qt::AlignHCenter );
120   aZonesLayout->addWidget( myGeomObjects,  1, 1, 2, 1 );
121   aZonesLayout->addWidget( aBtnsFrame,     2, 0, Qt::AlignHCenter );
122
123   aPage->setLayout( aZonesLayout );
124
125   connect( anAddBtn, SIGNAL( clicked() ), this, SIGNAL( addObjects() ) );
126   connect( aRemoveBtn, SIGNAL( clicked() ), this, SIGNAL( removeObjects() ) );
127
128   return aPage;
129 }
130
131 void HYDROGUI_CalculationDlg::onEmptyName()
132 {
133   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
134   QString aMessage = QObject::tr( "INCORRECT_OBJECT_NAME" );
135   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
136 }
137
138 void HYDROGUI_CalculationDlg::onAlreadyExists( QString theName )
139 {
140   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
141   QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
142   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
143 }
144
145 QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
146   QWizardPage* aPage = new QWizardPage( wizard() );
147   QFrame* aFrame = new QFrame( aPage );
148
149   QGridLayout* aLayout = new QGridLayout( aPage );
150   
151   myBrowser = new HYDROGUI_DataBrowser( module(), 0, aPage );
152   myBrowser->setAutoOpenLevel( 3 );
153   aLayout->setMargin( 5 );
154   aLayout->setSpacing( 5 );
155
156   aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
157
158   myBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
159   myBathymetryChoice = new QComboBox( aFrame );
160
161   myBathymetryChoice->setVisible( false );
162   myBatimetryLabel->setVisible( false );
163
164   aLayout->addWidget( myBatimetryLabel, 1, 0 );
165   aLayout->addWidget( myBathymetryChoice, 1, 1 );
166
167   aPage->setLayout( aLayout );
168
169   connect( myBrowser, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
170   connect( myBathymetryChoice, SIGNAL( activated( int ) ), SLOT( onMergeTypeSelected( int ) ) );
171
172   return aPage;
173 }
174
175 void HYDROGUI_CalculationDlg::onMergeTypeSelected( int theIndex )
176 {
177   int aType = myBathymetryChoice->itemData( theIndex ).toInt();
178   QString aText = myBathymetryChoice->itemText( theIndex );
179   emit setMergeType( aType, aText );
180 }
181
182 void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
183 {
184   bool doShow = false;
185   HYDROGUI_Zone* aZone = dynamic_cast<HYDROGUI_Zone*>( theObject );
186   if ( aZone )
187   {
188     doShow = aZone->isMergingNeed();
189   }
190
191   if ( doShow )
192   {
193     // Fill the merge type combo box
194     bool prevBlock = myBathymetryChoice->blockSignals( true );
195     myCurrentZone = aZone;
196     myBathymetryChoice->clear();
197     myBathymetryChoice->addItem( QObject::tr("MERGE_UNKNOWN"), HYDROData_Zone::Merge_UNKNOWN );
198     myBathymetryChoice->addItem( QObject::tr("MERGE_ZMIN"), HYDROData_Zone::Merge_ZMIN );
199     myBathymetryChoice->addItem( QObject::tr("MERGE_ZMAX"), HYDROData_Zone::Merge_ZMAX );
200     QStringList aList = aZone->getBathymetries();
201     for ( int i = 0; i < aList.length(); i++ )
202     {
203       myBathymetryChoice->addItem( aList.at( i ), HYDROData_Zone::Merge_Object );
204     }
205     // Select the current choice if any
206     int aCurIndex = 0;
207     switch ( aZone->getMergeType() )
208     {
209       case HYDROData_Zone::Merge_ZMIN:
210         aCurIndex = 1;
211         break;
212       case HYDROData_Zone::Merge_ZMAX:
213         aCurIndex = 2;
214         break;
215       case HYDROData_Zone::Merge_Object:
216         aCurIndex = aList.indexOf( aZone->text( HYDROGUI_DataObject::BathymetryId ) );
217         break;
218       default:
219         aCurIndex = 0; // Select unknown by default
220     }
221     myBathymetryChoice->setCurrentIndex( aCurIndex );
222     myBathymetryChoice->blockSignals( prevBlock );
223   }
224
225   myBathymetryChoice->setVisible( doShow );
226   myBatimetryLabel->setVisible( doShow );
227 }
228
229 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
230 {
231   myObjectName->setText( theName );
232 }
233
234 QString HYDROGUI_CalculationDlg::getObjectName() const
235 {
236   return myObjectName->text();
237 }
238
239 void HYDROGUI_CalculationDlg::setGeomObjects( const QStringList& theObjects )
240 {
241 }
242
243 void HYDROGUI_CalculationDlg::setSelectedGeomObjects( const QStringList& theObjects )
244 {
245   myGeomObjects->clear();
246
247   for ( int i = 0, n = theObjects.length(); i < n; ++i )
248   {
249     QString anObjName = theObjects.at( i );
250
251     QListWidgetItem* aListItem = new QListWidgetItem( anObjName, myGeomObjects );
252     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
253   }
254 }
255
256 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
257 {
258   QStringList aResList;
259   QList<QListWidgetItem*> aList = myGeomObjects->selectedItems();
260   for ( int i = 0, n = aList.length(); i < n; ++i )
261   {
262     aResList.append( aList.at( i )->text() );
263   }
264   return aResList;
265 }
266
267 void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_CalculationCase) theCase )
268 {
269   myEditedObject = theCase;
270   myValidator->setEditedObject( theCase );
271
272   HYDROGUI_DataObject* anobj = new HYDROGUI_DataObject( 0, NULL, "" );
273   myBrowser->setRoot(anobj);
274
275   LightApp_DataObject* aCaseItem = module()->getDataModel()->createObject( anobj,
276                                      myEditedObject, "", true );
277   myBrowser->updateTree();
278   myBrowser->openLevels();
279 }
280
281 HYDROGUI_Zone* HYDROGUI_CalculationDlg::getCurrentZone() const
282 {
283   return myCurrentZone;
284 }