Salome HOME
- The bug is fixed: iterator on an invalid sequence which is out of scope.
[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 <HYDROData_Object.h>
36
37 #include <CAM_Application.h>
38 #include <LightApp_DataObject.h>
39
40 #include <SUIT_FileDlg.h>
41 #include <SUIT_ResourceMgr.h>
42 #include <SUIT_Session.h>
43 #include <SUIT_Study.h>
44
45 #include <LightApp_Application.h>
46 #include <SUIT_Desktop.h>
47 #include <SUIT_MessageBox.h>
48
49 #include <QGroupBox>
50 #include <QLabel>
51 #include <QLayout>
52 #include <QLineEdit>
53 #include <QListWidget>
54 #include <QPicture>
55 #include <QPushButton>
56 #include <QToolButton>
57 #include <QWizardPage>
58 #include <QComboBox>
59
60 HYDROGUI_CalculationDlg::HYDROGUI_CalculationDlg( HYDROGUI_Module* theModule, const QString& theTitle )
61 : HYDROGUI_Wizard( theModule, theTitle )
62 {
63   QWizard* aWizard = wizard();
64   aWizard->addPage( createObjectsPage() );
65   aWizard->addPage( createZonesPage() );
66   aWizard->show();
67 }
68
69 HYDROGUI_CalculationDlg::~HYDROGUI_CalculationDlg()
70 {
71 }
72
73 void HYDROGUI_CalculationDlg::reset()
74 {
75   myObjectName->clear();
76   myGeomObjects->clear();
77 }
78
79 QWizardPage* HYDROGUI_CalculationDlg::createObjectsPage() {
80   QWizardPage* aPage = new QWizardPage( wizard() );
81   QFrame* aFrame = new QFrame( aPage );
82   // Calculation name
83 //  myObjectNameGroup = new QGroupBox( tr( "CALCULATION_NAME" ), aFrame );
84
85 //  myObjectName = new QLineEdit( myObjectNameGroup );
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   //QBoxLayout* aNameLayout = new QHBoxLayout( myObjectNameGroup );
94   //aNameLayout->setMargin( 5 );
95   //aNameLayout->setSpacing( 5 );
96   //aNameLayout->addWidget( new QLabel( tr( "NAME" ), myObjectNameGroup ) );
97   //aNameLayout->addWidget( myObjectName );
98
99   // Calculation zones
100   //QFrame* anObjectsFrame = new QFrame( aFrame );
101
102   //myGeomObjects = new QListWidget( anObjectsFrame );
103   myGeomObjects = new QListWidget( aPage );
104   myGeomObjects->setSelectionMode( QListWidget::SingleSelection );
105   myGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
106   myGeomObjects->setViewMode( QListWidget::ListMode );
107   myGeomObjects->setSortingEnabled( true );
108
109   QFrame* aBtnsFrame = new QFrame( aPage );
110   QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
111   aBtnsLayout->setMargin( 5 );
112   aBtnsLayout->setSpacing( 5 );
113   aBtnsFrame->setLayout( aBtnsLayout );
114   QPushButton* anAddBtn = new QPushButton( tr("ADD"), aBtnsFrame );
115   QPushButton* aRemoveBtn = new QPushButton( tr("REMOVE"), aBtnsFrame );
116   aBtnsLayout->addWidget( anAddBtn );
117   aBtnsLayout->addWidget( aRemoveBtn );
118   aBtnsLayout->addStretch( 1 );
119
120   QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
121   QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), aPage );
122
123   QGridLayout* aZonesLayout = new QGridLayout( aPage );
124   aZonesLayout->setMargin( 5 );
125   aZonesLayout->setSpacing( 5 );
126   aZonesLayout->setVerticalSpacing( 10 );
127   aZonesLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
128   aZonesLayout->addWidget( myObjectName,   0, 1 );
129   aZonesLayout->addWidget( anObjectsLabel, 1, 0, Qt::AlignHCenter );
130   aZonesLayout->addWidget( myGeomObjects,  1, 1, 2, 1 );
131   aZonesLayout->addWidget( aBtnsFrame,     2, 0, Qt::AlignHCenter );
132
133   aPage->setLayout( aZonesLayout );
134
135   connect( anAddBtn, SIGNAL( clicked() ), this, SIGNAL( addObjects() ) );
136   connect( aRemoveBtn, SIGNAL( clicked() ), this, SIGNAL( removeObjects() ) );
137
138   return aPage;
139 }
140
141 void HYDROGUI_CalculationDlg::onEmptyName()
142 {
143   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
144   QString aMessage = QObject::tr( "INCORRECT_OBJECT_NAME" );
145   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
146 }
147
148 void HYDROGUI_CalculationDlg::onAlreadyExists( QString theName )
149 {
150   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
151   QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
152   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
153 }
154
155 QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
156   QWizardPage* aPage = new QWizardPage( wizard() );
157   QFrame* aFrame = new QFrame( aPage );
158
159   QGridLayout* aLayout = new QGridLayout( aPage );
160   //Handle(HYDROData_Entity) anEntity = module()->getDataModel()->getDocument()->CreateObject( KIND_CALCULATION );
161   
162   myBrowser = new HYDROGUI_DataBrowser( module(), 0/*module()->application()->activeStudy()->root()*/, aPage );
163   myBrowser->setAutoOpenLevel( 3 );
164   aLayout->setMargin( 5 );
165   aLayout->setSpacing( 5 );
166
167   aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
168
169   QLabel* aBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
170   myBathymetryChoice = new QComboBox( aFrame );
171   myBathymetryChoice->addItem( tr( "ZMIN" ) );
172   myBathymetryChoice->addItem( tr( "ZMAX" ) );
173
174   aLayout->addWidget( aBatimetryLabel, 1, 0 );
175   aLayout->addWidget( myBathymetryChoice, 1, 1 );
176
177   aPage->setLayout( aLayout );
178
179   return aPage;
180 }
181
182 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
183 {
184   myObjectName->setText( theName );
185 }
186
187 QString HYDROGUI_CalculationDlg::getObjectName() const
188 {
189   return myObjectName->text();
190 }
191
192 void HYDROGUI_CalculationDlg::setGeomObjects( const QStringList& theObjects )
193 {
194 }
195
196 void HYDROGUI_CalculationDlg::setSelectedGeomObjects( const QStringList& theObjects )
197 {
198   myGeomObjects->clear();
199
200   for ( int i = 0, n = theObjects.length(); i < n; ++i )
201   {
202     QString anObjName = theObjects.at( i );
203
204     QListWidgetItem* aListItem = new QListWidgetItem( anObjName, myGeomObjects );
205     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
206   }
207 }
208
209 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
210 {
211   QStringList aResList;
212   QList<QListWidgetItem*> aList = myGeomObjects->selectedItems();
213   for ( int i = 0, n = aList.length(); i < n; ++i )
214   {
215     aResList.append( aList.at( i )->text() );
216   }
217   return aResList;
218 }
219
220 void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_CalculationCase) theCase )
221 {
222   myEditedObject = theCase;
223   myValidator->setEditedObject( theCase );
224
225   HYDROGUI_DataObject* anobj = new HYDROGUI_DataObject( 0, NULL, "" );
226   myBrowser->setRoot(anobj);
227
228   LightApp_DataObject* aCaseItem = module()->getDataModel()->createObject( anobj,
229                                      myEditedObject, "", true );
230   myBrowser->updateTree();
231   myBrowser->openLevels();
232 }