Salome HOME
Preparation of 3.1.0a2 - come back old implementation
[samples/light.git] / src / LIGHTGUI / LIGHTGUI.cxx
1 //  LIGHT : sample (no-corba-engine) SALOME module
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //  Author : Julia DOROVSKIKH
23 //  Date   : 01/01/2005
24 //  $Header$
25
26 using namespace std;
27
28 #include "LIGHTGUI.h"
29
30 #include "LIGHTGUI_Selection.h"
31 #include "LIGHTGUI_DataModel.h"
32 #include "LIGHTGUI_OBSelector.h"
33 #include "LIGHTGUI_DataOwner.h"
34
35 #include <SUIT_MessageBox.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_SelectionMgr.h>
38
39 #include <qinputdialog.h>
40 #include <qstringlist.h>
41
42 //=================================================================================
43 // function : LIGHTGUI()
44 // purpose  : Constructor : sets the default name for the module
45 //=================================================================================
46 LIGHTGUI::LIGHTGUI()
47      : SalomeApp_Module( "LIGHTGUI" )
48 {
49 }
50
51 //=================================================================================
52 // function : initialize()
53 // purpose  : module intitialization: creates menus, prepares context menu, etc.
54 //=================================================================================
55 void LIGHTGUI::initialize ( CAM_Application* app )
56 {
57   SalomeApp_Module::initialize( app );
58
59   QWidget* parent = application()->desktop();
60
61   createAction( lgLoadFile, tr( "TOP_LOAD_FILE" ), QIconSet(), tr( "MEN_LOAD_FILE" ),
62                 tr( "STB_LOAD_FILE" ), 0, parent, false, this, SLOT( onLoadFile() ) );
63   createAction( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIconSet(), tr( "MEN_SAVE_FILE" ),
64                 tr( "STB_SAVE_FILE" ), 0, parent, false, this, SLOT( onSaveFile() ) );
65   createAction( lgEditLine, tr( "TOP_EDIT_LINE" ), QIconSet(), tr( "MEN_EDIT_LINE" ),
66                 tr( "STB_EDIT_LINE" ), 0, parent, false, this, SLOT( onEditLine() ) );
67   createAction( lgAddLine,  tr( "TOP_ADD_LINE" ),  QIconSet(), tr( "MEN_ADD_LINE" ),
68                 tr( "STB_ADD_LINE" ),  0, parent, false, this, SLOT( onAddLine() ) );
69   createAction( lgDelLine,  tr( "TOP_DEL_LINE" ),  QIconSet(), tr( "MEN_DEL_LINE" ),
70                 tr( "STB_DEL_LINE" ),  0, parent, false, this, SLOT( onDelLine() ) );
71   createAction( lgClear,    tr( "TOP_CLEAR_ALL" ), QIconSet(), tr( "MEN_CLEAR_ALL" ),
72                 tr( "STB_CLEAR_ALL" ), 0, parent, false, this, SLOT( onClear() ) );
73
74   int aFileMnu = createMenu( tr( "MEN_FILE" ), -1, -1 );
75   createMenu( separator(), aFileMnu, -1, 10 );
76   createMenu( lgLoadFile,  aFileMnu, 10 );
77   createMenu( lgSaveFile,  aFileMnu, 10 );
78
79   int aLightMnu = createMenu( tr( "MEN_LIGHT" ), -1, -1, 50 );
80   createMenu( lgEditLine,  aLightMnu, 10 );
81   createMenu( lgAddLine,   aLightMnu, 10 );
82   createMenu( separator(), aLightMnu, -1, 10 );
83   createMenu( lgDelLine,   aLightMnu, 10 );
84   createMenu( lgClear,     aLightMnu, 10 );
85
86   // popup for object browser
87   int parentId = -1;
88   QString rule = "client='ObjectBrowser' and selcount=1 and type='TextLine'";
89
90   popupMgr()->insert ( action( lgEditLine ), parentId, 0 );
91   popupMgr()->setRule( action( lgEditLine ), rule, true  );
92
93   popupMgr()->insert ( action( lgAddLine ),  parentId, 0 );
94   popupMgr()->setRule( action( lgAddLine ),  rule, true  );
95
96   popupMgr()->insert ( separator(),          parentId, 0 );
97
98   popupMgr()->insert ( action( lgDelLine ),  parentId, 0 );
99   popupMgr()->setRule( action( lgDelLine ),  rule, true  );
100
101   popupMgr()->insert ( action( lgClear ),    parentId, 0 );
102 }
103
104 //=================================================================================
105 // function : contextMenuPopup()
106 // purpose  : defines context popup menu
107 //=================================================================================
108 void LIGHTGUI::contextMenuPopup ( const QString& client, QPopupMenu* menu, QString& )
109 {
110   LIGHTGUI_Selection sel ( client, getApp()->selectionMgr() );
111   popupMgr()->updatePopup( menu, &sel );
112 }
113
114 //=================================================================================
115 // function : windows()
116 // purpose  : gets a list of compliant dockable GUI elements
117 //=================================================================================
118 void LIGHTGUI::windows ( QMap<int, int>& aMap ) const
119 {
120   aMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft );
121   aMap.insert( SalomeApp_Application::WT_PyConsole, Qt::DockBottom );
122 }
123
124 //=================================================================================
125 // function : engineIOR()
126 // purpose  : Returns empty string. It means that this module
127 //            should use default engine for its persistence.
128 //=================================================================================
129 QString LIGHTGUI::engineIOR() const
130 {
131   return QString( "" );
132 }
133
134 //=================================================================================
135 // function : createDataModel()
136 // purpose  : create data model
137 //=================================================================================
138 CAM_DataModel* LIGHTGUI::createDataModel()
139 {
140   return new LIGHTGUI_DataModel( this );
141 }
142
143 //=================================================================================
144 // function : selectedLine()
145 // purpose  : get the number of selected line (multiple selection is not supported)
146 //=================================================================================
147 int LIGHTGUI::selectedLine()
148 {
149   int aPosition = 0; // bad value
150   
151   // Look for selected lines
152   SalomeApp_Application* app = getApp();
153   SalomeApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
154   if ( mgr ) {
155     SUIT_DataOwnerPtrList anOwnersList;
156     mgr->selected( anOwnersList );
157     
158     // Get index of the single selected line
159     if ( anOwnersList.size() == 1 ) {
160       const LIGHTGUI_DataOwner* owner =
161         dynamic_cast<const LIGHTGUI_DataOwner*>( anOwnersList[0].get() );
162       aPosition = owner->lineNb();
163     }
164   }
165   return aPosition;
166 }
167
168 //=================================================================================
169 // function : activateModule()
170 // purpose  : module's activation
171 //=================================================================================
172 bool LIGHTGUI::activateModule ( SUIT_Study* study )
173 {
174   bool isDone = SalomeApp_Module::activateModule( study );
175   if ( !isDone ) return false;
176
177   setMenuShown( true );
178
179   // enable own ObjectBrowser selector an disable another ones
180   bool isOwnSelector = false;
181   QPtrList<SUIT_Selector> lst;
182   getApp()->selectionMgr()->selectors( lst );
183   for ( QPtrListIterator<SUIT_Selector> it( lst ); it.current(); ++it ) {
184     if ( it.current()->type() == "ObjectBrowser" ) {
185       LIGHTGUI_OBSelector* anOwnSelector = dynamic_cast<LIGHTGUI_OBSelector*>( it.current() );
186       // restore own selector
187       if ( anOwnSelector ) {
188         // enable own
189         anOwnSelector->setEnabled( true );
190         isOwnSelector = true;
191       } 
192       else {
193         // disable other
194         it.current()->setEnabled( false );
195       }
196     }
197   }
198
199   // create own selector, if it was not found in selection manager
200   if ( !isOwnSelector ) {
201     new LIGHTGUI_OBSelector ( getApp()->objectBrowser(), getApp()->selectionMgr() );
202   }
203
204   return isDone;
205 }
206
207 //=================================================================================
208 // function : deactivateModule()
209 // purpose  : module's deactivation
210 //=================================================================================
211 bool LIGHTGUI::deactivateModule ( SUIT_Study* study )
212 {
213   // hide menus
214   setMenuShown( false );
215
216   // disable own ObjectBrowser selector an enable another ones
217   QPtrList<SUIT_Selector> lst;
218   getApp()->selectionMgr()->selectors( lst );
219   for ( QPtrListIterator<SUIT_Selector> it( lst ); it.current(); ++it ) {
220     if ( it.current()->type() == "ObjectBrowser" ) {
221       LIGHTGUI_OBSelector* anOwnSelector = dynamic_cast<LIGHTGUI_OBSelector*>( it.current() );
222       if ( anOwnSelector ) {
223         // disable own selector
224         anOwnSelector->setEnabled( false );
225       } 
226       else {
227         // enable other
228         it.current()->setEnabled( true );
229       }
230     }
231   }
232
233   return SalomeApp_Module::deactivateModule( study );
234 }
235
236 //=================================================================================
237 // function : onLoadFile()
238 // purpose  : "Load File" action slot
239 //=================================================================================
240 void LIGHTGUI::onLoadFile()
241 {
242   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
243   if ( !dm ) return;
244
245   QStringList filtersList;
246   filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
247   filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
248
249   // Select a file to be loaded
250   QString aFileName = getApp()->getFileName( true, QString::null, filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_LOAD" ), 0 );
251   if ( !aFileName.isEmpty() ) {
252     // Load the file
253     if ( dm->loadFile( aFileName ) ) {
254       updateObjBrowser( true );
255     } 
256     else {
257       SUIT_MessageBox::warn1 ( application()->desktop(),
258                                tr( "WRN_WARNING" ),
259                                tr( "WRN_LOAD_FAILED" ),
260                                tr( "BUT_OK" ) );
261       }
262   }
263 }
264
265 //=================================================================================
266 // function : onSaveFile()
267 // purpose  : "Save File" action slot
268 //=================================================================================
269 void LIGHTGUI::onSaveFile()
270 {
271   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
272   if ( !dm ) return;
273
274   QStringList filtersList;
275   filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
276   filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
277
278   // Select a file name to dump the lines into
279   QString aFileName = getApp()->getFileName( false, dm->fileName(), filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_DUMP" ), 0 );
280   if ( !aFileName.isEmpty() ) {
281     // Dump the file
282     if ( !dm->dumpFile( aFileName ) ) {
283       SUIT_MessageBox::warn1 ( application()->desktop(),
284                                tr( "WRN_WARNING" ),
285                                tr( "WRN_DUMP_FAILED" ),
286                                tr( "BUT_OK" ) );
287       }
288   }
289 }
290
291 //=================================================================================
292 // function : onEditLine()
293 // purpose  : "Edit Line" action slot
294 //=================================================================================
295 void LIGHTGUI::onEditLine()
296 {
297   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
298   if ( !dm ) return;
299
300   // Define position
301   int aPosition = selectedLine();
302   
303   // Check, that position is defined
304   if ( !aPosition ) {
305     SUIT_MessageBox::warn1 ( application()->desktop(),
306                              tr( "WRN_WARNING" ),
307                              tr( "WRN_SELECT_LINE" ),
308                              tr( "BUT_OK" ) );
309     return;
310   }
311
312   // Get new text
313   bool isOk;
314   QString aText = QInputDialog::getText( tr( "LIGHTGUI_EDIT_LINE" ),
315                                          QString::null,
316                                          QLineEdit::Normal,
317                                          dm->getLineText( aPosition ),
318                                          &isOk,
319                                          getApp()->desktop() );
320   if ( !isOk ) return;
321   
322   // try to change a text of the selected line
323   isOk = dm->setLineText( aPosition, aText );
324   if ( isOk ) {
325     updateObjBrowser( true );
326   } 
327   else {
328     SUIT_MessageBox::warn1 ( application()->desktop(),
329                              tr( "WRN_WARNING" ),
330                              tr( "WRN_EDIT_FAILED" ),
331                              tr( "BUT_OK") );
332   }
333 }
334
335 //=================================================================================
336 // function : onAddLine()
337 // purpose  : "Insert Line" action slot
338 //=================================================================================
339 void LIGHTGUI::onAddLine()
340 {
341   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
342   if ( !dm ) return;
343
344   // Define position
345   int aPosition = selectedLine();
346   
347   // Get new text
348   bool isOk;
349   QString aText = QInputDialog::getText( tr( "LIGHTGUI_ADD_LINE" ),
350                                          QString::null,
351                                          QLineEdit::Normal,
352                                          QString::null,
353                                          &isOk,
354                                          getApp()->desktop() );
355   if ( !isOk ) return;
356
357   // try to insert/add text line
358   isOk = dm->insertLineBefore( aPosition, aText );
359
360   if ( isOk ) {
361     updateObjBrowser( true );
362   } 
363   else {
364     SUIT_MessageBox::warn1 ( application()->desktop(),
365                              tr( "WRN_WARNING" ),
366                              tr( "WRN_ADD_FAILED" ),
367                              tr( "BUT_OK") );
368   }
369 }
370
371 //=================================================================================
372 // function : onDelLine()
373 // purpose  : "Delete Line" action slot
374 //=================================================================================
375 void LIGHTGUI::onDelLine()
376 {
377   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
378   if ( !dm ) return;
379
380   // Define position
381   int aPosition = selectedLine();
382   
383   // Check, that position is defined
384   if ( !aPosition ) {
385     SUIT_MessageBox::warn1 ( application()->desktop(),
386                              tr( "WRN_WARNING" ),
387                              tr( "WRN_SELECT_LINE" ),
388                              tr( "BUT_OK" ) );
389     return;
390   }
391
392   // try to delete line
393   bool isOk = dm->deleteTextLine( aPosition );
394   if ( isOk ) {
395     updateObjBrowser( true );
396   } 
397   else {
398     SUIT_MessageBox::warn1 ( application()->desktop(),
399                              tr( "WRN_WARNING" ),
400                              tr( "WRN_DELETE_FAILED" ),
401                              tr( "BUT_OK") );
402   }
403 }
404
405 //=================================================================================
406 // function : onClear()
407 // purpose  : "Clear Text" action slot
408 //=================================================================================
409 void LIGHTGUI::onClear()
410 {
411   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
412   if ( !dm ) return;
413
414   dm->clearAll();
415   updateObjBrowser( true );
416 }
417
418 //=================================================================================
419 // function : createModule()
420 // purpose  : exports module object
421 //=================================================================================
422 #ifdef WNT
423 #define LIGHTGUI_EXPORT __declspec(dllexport)
424 #else   // WNT
425 #define LIGHTGUI_EXPORT
426 #endif  // WNT
427
428 extern "C" {
429   LIGHTGUI_EXPORT CAM_Module* createModule() {
430     return new LIGHTGUI();
431   }
432 }