Salome HOME
f254ad449b9bce6f8b8091717daec0fc1215d92e
[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_DataObject.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   // Calculation name
85 //  myObjectNameGroup = new QGroupBox( tr( "CALCULATION_NAME" ), aFrame );
86
87 //  myObjectName = new QLineEdit( myObjectNameGroup );
88   myObjectName = new QLineEdit( aPage );
89   myValidator = new HYDROGUI_NameValidator(module(), myObjectName);
90   myObjectName->setValidator( myValidator );
91
92   connect( myValidator, SIGNAL( emptyName() ), this, SLOT( onEmptyName() ) );
93   connect( myValidator, SIGNAL( alreadyExists( QString ) ), this, SLOT( onAlreadyExists( QString ) ) );
94
95   //QBoxLayout* aNameLayout = new QHBoxLayout( myObjectNameGroup );
96   //aNameLayout->setMargin( 5 );
97   //aNameLayout->setSpacing( 5 );
98   //aNameLayout->addWidget( new QLabel( tr( "NAME" ), myObjectNameGroup ) );
99   //aNameLayout->addWidget( myObjectName );
100
101   // Calculation zones
102   //QFrame* anObjectsFrame = new QFrame( aFrame );
103
104   //myGeomObjects = new QListWidget( anObjectsFrame );
105   myGeomObjects = new QListWidget( aPage );
106   myGeomObjects->setSelectionMode( QListWidget::SingleSelection );
107   myGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
108   myGeomObjects->setViewMode( QListWidget::ListMode );
109   myGeomObjects->setSortingEnabled( true );
110
111   QFrame* aBtnsFrame = new QFrame( aPage );
112   QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
113   aBtnsLayout->setMargin( 5 );
114   aBtnsLayout->setSpacing( 5 );
115   aBtnsFrame->setLayout( aBtnsLayout );
116   QPushButton* anAddBtn = new QPushButton( tr("ADD"), aBtnsFrame );
117   QPushButton* aRemoveBtn = new QPushButton( tr("REMOVE"), aBtnsFrame );
118   aBtnsLayout->addWidget( anAddBtn );
119   aBtnsLayout->addWidget( aRemoveBtn );
120   aBtnsLayout->addStretch( 1 );
121
122   QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
123   QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), aPage );
124
125   QGridLayout* aZonesLayout = new QGridLayout( aPage );
126   aZonesLayout->setMargin( 5 );
127   aZonesLayout->setSpacing( 5 );
128   aZonesLayout->setVerticalSpacing( 10 );
129   aZonesLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
130   aZonesLayout->addWidget( myObjectName,   0, 1 );
131   aZonesLayout->addWidget( anObjectsLabel, 1, 0, Qt::AlignHCenter );
132   aZonesLayout->addWidget( myGeomObjects,  1, 1, 2, 1 );
133   aZonesLayout->addWidget( aBtnsFrame,     2, 0, Qt::AlignHCenter );
134
135   aPage->setLayout( aZonesLayout );
136
137   connect( anAddBtn, SIGNAL( clicked() ), this, SIGNAL( addObjects() ) );
138   connect( aRemoveBtn, SIGNAL( clicked() ), this, SIGNAL( removeObjects() ) );
139
140   return aPage;
141 }
142
143 void HYDROGUI_CalculationDlg::onEmptyName()
144 {
145   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
146   QString aMessage = QObject::tr( "INCORRECT_OBJECT_NAME" );
147   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
148 }
149
150 void HYDROGUI_CalculationDlg::onAlreadyExists( QString theName )
151 {
152   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
153   QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
154   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
155 }
156
157 QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
158   QWizardPage* aPage = new QWizardPage( wizard() );
159   QFrame* aFrame = new QFrame( aPage );
160
161   QGridLayout* aLayout = new QGridLayout( aPage );
162   //Handle(HYDROData_Entity) anEntity = module()->getDataModel()->getDocument()->CreateObject( KIND_CALCULATION );
163   
164   myBrowser = new HYDROGUI_DataBrowser( module(), 0/*module()->application()->activeStudy()->root()*/, aPage );
165   myBrowser->setAutoOpenLevel( 3 );
166   aLayout->setMargin( 5 );
167   aLayout->setSpacing( 5 );
168
169   aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
170
171   myBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
172   myBathymetryChoice = new QComboBox( aFrame );
173   myBathymetryChoice->addItem( tr( "ZMIN" ) );
174   myBathymetryChoice->addItem( tr( "ZMAX" ) );
175
176   myBathymetryChoice->setVisible( false );
177   myBatimetryLabel->setVisible( false );
178
179   aLayout->addWidget( myBatimetryLabel, 1, 0 );
180   aLayout->addWidget( myBathymetryChoice, 1, 1 );
181
182   aPage->setLayout( aLayout );
183
184   connect( myBrowser, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
185
186   return aPage;
187 }
188
189 void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
190 {
191   bool doShow = false;
192   HYDROGUI_DataObject* anObject = dynamic_cast<HYDROGUI_DataObject*>( theObject );
193   if ( anObject )
194   {
195     doShow = anObject->isMergingNeed();
196   }
197   myBathymetryChoice->setVisible( doShow );
198   myBatimetryLabel->setVisible( doShow );
199 }
200
201 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
202 {
203   myObjectName->setText( theName );
204 }
205
206 QString HYDROGUI_CalculationDlg::getObjectName() const
207 {
208   return myObjectName->text();
209 }
210
211 void HYDROGUI_CalculationDlg::setGeomObjects( const QStringList& theObjects )
212 {
213 }
214
215 void HYDROGUI_CalculationDlg::setSelectedGeomObjects( const QStringList& theObjects )
216 {
217   myGeomObjects->clear();
218
219   for ( int i = 0, n = theObjects.length(); i < n; ++i )
220   {
221     QString anObjName = theObjects.at( i );
222
223     QListWidgetItem* aListItem = new QListWidgetItem( anObjName, myGeomObjects );
224     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
225   }
226 }
227
228 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
229 {
230   QStringList aResList;
231   QList<QListWidgetItem*> aList = myGeomObjects->selectedItems();
232   for ( int i = 0, n = aList.length(); i < n; ++i )
233   {
234     aResList.append( aList.at( i )->text() );
235   }
236   return aResList;
237 }
238
239 void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_CalculationCase) theCase )
240 {
241   myEditedObject = theCase;
242   myValidator->setEditedObject( theCase );
243
244   HYDROGUI_DataObject* anobj = new HYDROGUI_DataObject( 0, NULL, "" );
245   myBrowser->setRoot(anobj);
246
247   LightApp_DataObject* aCaseItem = module()->getDataModel()->createObject( anobj,
248                                      myEditedObject, "", true );
249   myBrowser->updateTree();
250   myBrowser->openLevels();
251 }