Salome HOME
Name validator is added to the Calculation Case dialog.
[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
108   QFrame* aBtnsFrame = new QFrame( aPage );
109   QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
110   aBtnsLayout->setMargin( 5 );
111   aBtnsLayout->setSpacing( 5 );
112   aBtnsFrame->setLayout( aBtnsLayout );
113   QPushButton* anAddBtn = new QPushButton( tr("ADD"), aBtnsFrame );
114   QPushButton* aRemoveBtn = new QPushButton( tr("REMOVE"), aBtnsFrame );
115   aBtnsLayout->addWidget( anAddBtn );
116   aBtnsLayout->addWidget( aRemoveBtn );
117   aBtnsLayout->addStretch( 1 );
118
119   QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
120   QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), aPage );
121
122   QGridLayout* aZonesLayout = new QGridLayout( aPage );
123   aZonesLayout->setMargin( 5 );
124   aZonesLayout->setSpacing( 5 );
125   aZonesLayout->setVerticalSpacing( 10 );
126   aZonesLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
127   aZonesLayout->addWidget( myObjectName,   0, 1 );
128   aZonesLayout->addWidget( anObjectsLabel, 1, 0, Qt::AlignHCenter );
129   aZonesLayout->addWidget( myGeomObjects,  1, 1, 2, 1 );
130   aZonesLayout->addWidget( aBtnsFrame,     2, 0, Qt::AlignHCenter );
131
132   //QBoxLayout* aMainLayout = new QVBoxLayout( aFrame );
133   //aMainLayout->setMargin( 0 );
134   //aMainLayout->setSpacing( 5 );
135   //aMainLayout->addWidget( myObjectNameGroup );
136   //aMainLayout->addWidget( anObjectsFrame );
137   //aMainLayout->addStretch();
138
139   //aPage->setLayout( aMainLayout );
140   aPage->setLayout( aZonesLayout );
141
142   return aPage;
143 }
144
145 void HYDROGUI_CalculationDlg::onEmptyName()
146 {
147   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
148   QString aMessage = QObject::tr( "INCORRECT_OBJECT_NAME" );
149   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
150 }
151
152 void HYDROGUI_CalculationDlg::onAlreadyExists( QString theName )
153 {
154   QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
155   QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
156   SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
157 }
158
159 QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
160   QWizardPage* aPage = new QWizardPage( wizard() );
161   QFrame* aFrame = new QFrame( aPage );
162
163   QGridLayout* aLayout = new QGridLayout( aPage );
164   //Handle(HYDROData_Entity) anEntity = module()->getDataModel()->getDocument()->CreateObject( KIND_CALCULATION );
165   
166   myBrowser = new HYDROGUI_DataBrowser( module(), 0/*module()->application()->activeStudy()->root()*/, aPage );
167   myBrowser->setAutoOpenLevel( 3 );
168   aLayout->setMargin( 5 );
169   aLayout->setSpacing( 5 );
170
171   aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
172
173   QLabel* aBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
174   myBathymetryChoice = new QComboBox( aFrame );
175   myBathymetryChoice->addItem( tr( "ZMIN" ) );
176   myBathymetryChoice->addItem( tr( "ZMAX" ) );
177
178   aLayout->addWidget( aBatimetryLabel, 1, 0 );
179   aLayout->addWidget( myBathymetryChoice, 1, 1 );
180
181   aPage->setLayout( aLayout );
182
183   return aPage;
184 }
185
186 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
187 {
188   myObjectName->setText( theName );
189 }
190
191 QString HYDROGUI_CalculationDlg::getObjectName() const
192 {
193   return myObjectName->text();
194 }
195
196 void HYDROGUI_CalculationDlg::setGeomObjects( const QStringList& theObjects )
197 {
198 }
199
200 void HYDROGUI_CalculationDlg::setSelectedGeomObjects( const QStringList& theObjects )
201 {
202   myGeomObjects->clear();
203
204   for ( int i = 0, n = theObjects.length(); i < n; ++i )
205   {
206     QString anObjName = theObjects.at( i );
207
208     QListWidgetItem* aListItem = new QListWidgetItem( anObjName, myGeomObjects );
209     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
210   }
211 }
212
213 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
214 {
215   QStringList aResList;
216
217   for ( int i = 0, n = myGeomObjects->count(); i < n; ++i )
218   {
219     QListWidgetItem* aListItem = myGeomObjects->item( i );
220     if ( !aListItem )
221       continue;
222
223     QString aSelObjName = aListItem->text();
224     aResList.append( aSelObjName );
225   }
226
227   return aResList;
228 }
229
230 void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_CalculationCase) theCase )
231 {
232   myEditedObject = theCase;
233   myValidator->setEditedObject( theCase );
234
235   HYDROGUI_DataObject* anobj = new HYDROGUI_DataObject( 0, NULL, "" );
236   myBrowser->setRoot(anobj);
237
238   LightApp_DataObject* aCaseItem = module()->getDataModel()->createObject( anobj,
239                                      myEditedObject, "", true );
240   myBrowser->updateTree();
241   myBrowser->openLevels();
242 }