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