Salome HOME
Using LightApp library
[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( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIconSet(), tr( "MEN_SAVE_FILE" ),
69                 tr( "STB_SAVE_FILE" ), 0, parent, false, this, SLOT( onSaveFile() ) );
70   createAction( lgDisplayLine, tr( "TOP_DISPLAY_LINE" ), QIconSet(), tr( "MEN_DISPLAY_LINE" ),
71                 tr( "STB_DISPLAY_LINE" ), 0, parent, false, this, SLOT( onDisplayLine() ) );
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 : contextMenuPopup()
117 // purpose  : defines context popup menu
118 //=================================================================================
119 void LIGHTGUI::contextMenuPopup ( const QString& client, QPopupMenu* menu, QString& str)
120 {
121   LIGHTGUI_Selection* sel = new LIGHTGUI_Selection();
122   sel->init( client, getApp()->selectionMgr() );
123   popupMgr()->updatePopup( menu, sel );
124   delete sel;
125 }
126
127 //=================================================================================
128 // function : windows()
129 // purpose  : gets a list of compliant dockable GUI elements
130 //=================================================================================
131 void LIGHTGUI::windows ( QMap<int, int>& aMap ) const
132 {
133   aMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::DockLeft );
134   aMap.insert( LightApp_Application::WT_PyConsole, Qt::DockBottom );
135 }
136
137 //=================================================================================
138 // function : createDataModel()
139 // purpose  : create data model
140 //=================================================================================
141 CAM_DataModel* LIGHTGUI::createDataModel()
142 {
143   return new LIGHTGUI_DataModel( this );
144 }
145
146 //=================================================================================
147 // function : selectedLine()
148 // purpose  : get the number of selected line (multiple selection is not supported)
149 //=================================================================================
150 int LIGHTGUI::selectedLine()
151 {
152   int aPosition = 0; // bad value
153   
154   // Look for selected lines
155   LightApp_Application* app = getApp();
156   LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
157   if ( mgr ) {
158     SUIT_DataOwnerPtrList anOwnersList;
159     mgr->selected( anOwnersList );
160     
161     // Get index of the single selected line
162     if ( anOwnersList.size() == 1 ) {
163       const LightApp_DataOwner* owner =
164         dynamic_cast<const LightApp_DataOwner*>( anOwnersList[0].get() );
165       QString anEntry = owner->entry();
166       int anIndex = anEntry.find("_");
167       aPosition = (anEntry.mid(anIndex+1, anEntry.length() - anIndex)).toInt();//lineNb();
168     }
169   }
170   return aPosition;
171 }
172
173 //=================================================================================
174 // function : activateModule()
175 // purpose  : module's activation
176 //=================================================================================
177 bool LIGHTGUI::activateModule ( SUIT_Study* study )
178 {
179   bool isDone = LightApp_Module::activateModule( study );
180   if ( !isDone ) return false;
181
182   setMenuShown( true );
183
184   return isDone;
185 }
186
187 //=================================================================================
188 // function : deactivateModule()
189 // purpose  : module's deactivation
190 //=================================================================================
191 bool LIGHTGUI::deactivateModule ( SUIT_Study* study )
192 {
193   // hide menus
194   setMenuShown( false );
195
196   return LightApp_Module::deactivateModule( study );
197 }
198
199 //=================================================================================
200 // function : onLoadFile()
201 // purpose  : "Load File" action slot
202 //=================================================================================
203 void LIGHTGUI::onLoadFile()
204 {
205   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
206   if ( !dm ) return;
207
208   QStringList filtersList;
209   filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
210   filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
211
212   // Select a file to be loaded
213   QString aFileName = getApp()->getFileName( true, QString::null, filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_LOAD" ), 0 );
214   if ( !aFileName.isEmpty() ) {
215     // Load the file
216     if ( dm->loadFile( aFileName ) ) {
217       updateObjBrowser( true );
218     } 
219     else {
220       SUIT_MessageBox::warn1 ( application()->desktop(),
221                                tr( "WRN_WARNING" ),
222                                tr( "WRN_LOAD_FAILED" ),
223                                tr( "BUT_OK" ) );
224       }
225   }
226 }
227
228 //=================================================================================
229 // function : onSaveFile()
230 // purpose  : "Save File" action slot
231 //=================================================================================
232 void LIGHTGUI::onSaveFile()
233 {
234   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
235   if ( !dm ) return;
236
237   QStringList filtersList;
238   filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
239   filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
240
241   // Select a file name to dump the lines into
242   QString aFileName = getApp()->getFileName( false, dm->fileName(), filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_DUMP" ), 0 );
243   if ( !aFileName.isEmpty() ) {
244     // Dump the file
245     if ( !dm->dumpFile( aFileName ) ) {
246       SUIT_MessageBox::warn1 ( application()->desktop(),
247                                tr( "WRN_WARNING" ),
248                                tr( "WRN_DUMP_FAILED" ),
249                                tr( "BUT_OK" ) );
250       }
251   }
252 }
253 //=================================================================================
254 // function : onDisplayLine()
255 // purpose  : "Display Line" action slot
256 //=================================================================================
257 void LIGHTGUI::onDisplayLine()
258 {
259   OCCViewer_ViewManager* aMgr = (OCCViewer_ViewManager*)getApp()->getViewManager("OCCViewer", true);
260   SOCC_Viewer* aViewer = (SOCC_Viewer*)aMgr->getViewModel();  
261   if ( !aViewer )
262     return;
263
264   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
265   if ( !dm ) return;
266   int aPosition = selectedLine();
267   QString aLine =  dm->getLineText( aPosition );
268   double aX = 0, aY = 0, aZ = 0;
269   QString aStrId = QString("LIGHTGUI_%1").arg(aPosition);
270
271   Handle(LIGHTGUI_TextPrs) aPrs = new LIGHTGUI_TextPrs( aLine, gp_Pnt( aX, aY, aZ ) );
272   aPrs->SetOwner(new SALOME_InteractiveObject( aStrId, "" ) );
273   SOCC_Prs* prs = dynamic_cast<SOCC_Prs*>( aViewer->CreatePrs() ); // aStringID is an "entry"
274   if ( prs ) {
275     aViewer->EraseAll();
276     prs->AddObject( aPrs );
277     aViewer->Display( prs );
278     aViewer->Repaint();
279     getApp()->updateObjectBrowser();
280   } 
281 }
282
283 //=================================================================================
284 // function : onEditLine()
285 // purpose  : "Edit Line" action slot
286 //=================================================================================
287 void LIGHTGUI::onEditLine()
288 {
289   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
290   if ( !dm ) return;
291
292   // Define position
293   int aPosition = selectedLine();
294   
295   // Check, that position is defined
296   if ( !aPosition ) {
297     SUIT_MessageBox::warn1 ( application()->desktop(),
298                              tr( "WRN_WARNING" ),
299                              tr( "WRN_SELECT_LINE" ),
300                              tr( "BUT_OK" ) );
301     return;
302   }
303
304   // Get new text
305   bool isOk;
306   QString aText = QInputDialog::getText( tr( "LIGHTGUI_EDIT_LINE" ),
307                                          QString::null,
308                                          QLineEdit::Normal,
309                                          dm->getLineText( aPosition ),
310                                          &isOk,
311                                          getApp()->desktop() );
312   if ( !isOk ) return;
313   
314   // try to change a text of the selected line
315   isOk = dm->setLineText( aPosition, aText );
316   if ( isOk ) {
317     updateObjBrowser( true );
318   } 
319   else {
320     SUIT_MessageBox::warn1 ( application()->desktop(),
321                              tr( "WRN_WARNING" ),
322                              tr( "WRN_EDIT_FAILED" ),
323                              tr( "BUT_OK") );
324   }
325 }
326
327 //=================================================================================
328 // function : onAddLine()
329 // purpose  : "Insert Line" action slot
330 //=================================================================================
331 void LIGHTGUI::onAddLine()
332 {
333   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
334   if ( !dm ) return;
335
336   // Define position
337   int aPosition = selectedLine();
338   
339   // Get new text
340   bool isOk;
341   QString aText = QInputDialog::getText( tr( "LIGHTGUI_ADD_LINE" ),
342                                          QString::null,
343                                          QLineEdit::Normal,
344                                          QString::null,
345                                          &isOk,
346                                          getApp()->desktop() );
347   if ( !isOk ) return;
348
349   // try to insert/add text line
350   isOk = dm->insertLineBefore( aPosition, aText );
351
352   if ( isOk ) {
353     updateObjBrowser( true );
354   } 
355   else {
356     SUIT_MessageBox::warn1 ( application()->desktop(),
357                              tr( "WRN_WARNING" ),
358                              tr( "WRN_ADD_FAILED" ),
359                              tr( "BUT_OK") );
360   }
361 }
362
363 //=================================================================================
364 // function : onDelLine()
365 // purpose  : "Delete Line" action slot
366 //=================================================================================
367 void LIGHTGUI::onDelLine()
368 {
369   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
370   if ( !dm ) return;
371
372   // Define position
373   int aPosition = selectedLine();
374   
375   // Check, that position is defined
376   if ( !aPosition ) {
377     SUIT_MessageBox::warn1 ( application()->desktop(),
378                              tr( "WRN_WARNING" ),
379                              tr( "WRN_SELECT_LINE" ),
380                              tr( "BUT_OK" ) );
381     return;
382   }
383
384   // try to delete line
385   bool isOk = dm->deleteTextLine( aPosition );
386   if ( isOk ) {
387     updateObjBrowser( true );
388   } 
389   else {
390     SUIT_MessageBox::warn1 ( application()->desktop(),
391                              tr( "WRN_WARNING" ),
392                              tr( "WRN_DELETE_FAILED" ),
393                              tr( "BUT_OK") );
394   }
395 }
396
397 //=================================================================================
398 // function : onClear()
399 // purpose  : "Clear Text" action slot
400 //=================================================================================
401 void LIGHTGUI::onClear()
402 {
403   LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
404   if ( !dm ) return;
405
406   dm->clearAll();
407   updateObjBrowser( true );
408 }
409
410 //=================================================================================
411 // function : createModule()
412 // purpose  : exports module object
413 //=================================================================================
414 #ifdef WNT
415 #define LIGHTGUI_EXPORT __declspec(dllexport)
416 #else   // WNT
417 #define LIGHTGUI_EXPORT
418 #endif  // WNT
419
420 extern "C" {
421   LIGHTGUI_EXPORT CAM_Module* createModule() {
422     return new LIGHTGUI();
423   }
424 }