Salome HOME
Porting to Qt4.
[modules/geom.git] / src / GEOMBase / GEOMBase_Skeleton.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : GEOMBase_Skeleton.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GEOMBase_Skeleton.h"
30 #include "GEOMBase.h"
31
32 #include "GeometryGUI.h"
33
34 #include "SalomeApp_Application.h"
35 #include "LightApp_Application.h"
36 #include "LightApp_SelectionMgr.h"
37 #include "SUIT_Desktop.h"
38 #include "SUIT_ResourceMgr.h"
39 #include "SUIT_Session.h"
40 #include "SUIT_MessageBox.h"
41
42 #include <QKeyEvent>
43
44 using namespace std;
45
46 //=================================================================================
47 // class    : GEOMBase_Skeleton()
48 // purpose  : Constructs a GEOMBase_Skeleton which is a child of 'parent', with the 
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 GEOMBase_Skeleton::GEOMBase_Skeleton(GeometryGUI* theGeometryGUI, QWidget* parent,
54                                      const char* name, bool modal, Qt::WindowFlags fl)
55   : QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ), 
56     GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) ),
57     myGeomGUI( theGeometryGUI ),
58     myRBGroup( 0 )
59 {
60   setupUi(this);
61
62   if (!name)
63     setObjectName("GEOMBase_Skeleton");
64   else
65     setObjectName(name);
66
67   setModal( modal );
68   setAttribute( Qt::WA_DeleteOnClose );
69
70   buttonCancel->setText(tr("GEOM_BUT_CLOSE"));
71   buttonOk->setText(tr("GEOM_BUT_OK"));
72   buttonApply->setText(tr("GEOM_BUT_APPLY"));
73   buttonHelp->setText(tr("GEOM_BUT_HELP"));
74
75   GroupMedium->setAttribute( Qt::WA_DeleteOnClose );
76   GroupMedium->close();
77   resize(0, 0);
78
79   if ( GroupConstructors != NULL ) {
80     myRBGroup = new QButtonGroup(GroupConstructors);
81     QList<QObject*> aRadioButtons = GroupConstructors->children();
82     QListIterator<QObject*> it( aRadioButtons );
83     int anId = 0;
84     while ( it.hasNext() )
85       if ( QRadioButton* aRB = ::qobject_cast<QRadioButton*>( it.next() ) ) {
86         myRBGroup->addButton( aRB, anId );
87         anId++;
88       }
89   }
90
91   Init();
92 }
93
94
95 //=================================================================================
96 // function : ~GEOMBase_Skeleton()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 GEOMBase_Skeleton::~GEOMBase_Skeleton()
100 {
101   if (myGeomGUI)
102     myGeomGUI->SetActiveDialogBox( 0 );
103 }
104
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void GEOMBase_Skeleton::Init()
111 {
112   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
113   if (!myGeomGUI && app)
114     myGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
115
116   /* init variables */
117   myGeomGUI->SetActiveDialogBox(this);
118
119   /* signals and slots connections */
120   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
121   if (myGeomGUI) 
122   {
123     connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
124     connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
125   }
126
127   // connect help button on a private slot that displays help information
128   connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
129
130   /* displays Dialog */
131   RadioButton1->setChecked(TRUE);
132   RadioButton4->hide();
133
134   return;
135 }
136
137
138 //=================================================================================
139 // function : ClickOnCancel()
140 // purpose  :
141 //=================================================================================
142 void GEOMBase_Skeleton::ClickOnCancel()
143 {
144   close();
145 }
146
147
148 //=================================================================================
149 // function : LineEditReturnPressed()
150 // purpose  :
151 //=================================================================================
152 void GEOMBase_Skeleton::LineEditReturnPressed()
153 {
154   if ( !myEditCurrentArgument )
155     return;
156
157   /* User name of object input management                          */
158   /* If successfull the selection is changed and signal emitted... */
159   /* so SelectionIntoArgument() is automatically called.           */
160   const QString objectUserName = myEditCurrentArgument->text();
161   QWidget* thisWidget = (QWidget*)this;
162   
163   if(GEOMBase::SelectionByNameInDialogs(thisWidget, objectUserName, selectedIO()))
164      myEditCurrentArgument->setText(objectUserName);
165
166   return;
167 }
168
169
170 //=================================================================================
171 // function : DeactivateActiveDialog()
172 // purpose  :
173 //=================================================================================
174 void GEOMBase_Skeleton::DeactivateActiveDialog()
175 {
176   this->setEnabled(false);
177   globalSelection();
178   disconnect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
179   if (myGeomGUI) myGeomGUI->SetActiveDialogBox(0);
180     
181   return;
182 }
183
184
185 //=================================================================================
186 // function : ActivateThisDialog()
187 // purpose  :
188 //=================================================================================
189 void GEOMBase_Skeleton::ActivateThisDialog()
190 {
191   /* Emit a signal to deactivate the active dialog */
192   if (myGeomGUI) myGeomGUI->EmitSignalDeactivateDialog();
193   this->setEnabled(true);
194   if (myGeomGUI) myGeomGUI->SetActiveDialogBox((QDialog*)this);
195   return;
196 }
197
198
199 //=================================================================================
200 // function : closeEvent()
201 // purpose  : same than click on cancel button
202 //=================================================================================
203 void GEOMBase_Skeleton::closeEvent(QCloseEvent* e)
204 {
205   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
206   if(app) {
207     disconnect( app->selectionMgr(), 0, this, 0);
208     app->updateActions();
209   }
210   QDialog::closeEvent( e );
211 }
212
213 //=================================================================================
214 // function : initName()
215 // purpose  : initialize the Name field with a string "thePrefix_X" (Vertex_3)
216 //=================================================================================
217 void GEOMBase_Skeleton::initName( const char* thePrefix )
218 {
219   if ( thePrefix )
220     setPrefix( thePrefix );
221   ResultName->setText( GEOMBase::GetDefaultName( getPrefix() ) );
222 }
223
224 //=================================================================================
225 // function : getNewObjectName()
226 // purpose  : returns contents of Name field
227 //=================================================================================
228 const char* GEOMBase_Skeleton::getNewObjectName() const
229 {
230   return ResultName->text().toStdString().c_str();
231 }
232
233 //=================================================================================
234 // function : getConstructorId()
235 // purpose  :
236 //=================================================================================
237 int GEOMBase_Skeleton::getConstructorId() const
238 {
239   /*if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
240     return GroupConstructors->id( GroupConstructors->selected() );
241     return -1;*/
242
243   if ( myRBGroup != NULL )
244     return myRBGroup->checkedId();
245   return -1;
246 }
247
248 //=================================================================================
249 // function : ClickOnHelp()
250 // purpose  :
251 //=================================================================================
252 void GEOMBase_Skeleton::ClickOnHelp()
253 {
254   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
255   if (app) 
256     app->onHelpContextModule(myGeomGUI ? app->moduleName(myGeomGUI->moduleName()) : QString(""), myHelpFileName);
257   else {
258                 QString platform;
259 #ifdef WIN32
260                 platform = "winapplication";
261 #else
262                 platform = "application";
263 #endif
264     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
265                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
266                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
267                              QObject::tr("BUT_OK"));
268   }
269 }
270 //=================================================================================
271 //  function : setHelpFileName()
272 //  purpose  : set name for help file html
273 //=================================================================================
274
275 void GEOMBase_Skeleton::setHelpFileName(const QString& theName)
276 {
277     myHelpFileName = theName;
278 }
279
280 //=================================================================================
281 // function : keyPressEvent()
282 // purpose  :
283 //=================================================================================
284 void GEOMBase_Skeleton::keyPressEvent( QKeyEvent* e )
285 {
286   QDialog::keyPressEvent( e );
287   if ( e->isAccepted() )
288     return;
289
290   if ( e->key() == Qt::Key_F1 )
291     {
292       e->accept();
293       ClickOnHelp();
294     }
295 }