]> SALOME platform Git repositories - modules/gui.git/blob - src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx
Salome HOME
e0aaf8d72f5ce0c7ad8d062d7af8b0317b31f994
[modules/gui.git] / src / SALOME_PYQT / SalomePyQt / SalomePyQt.cxx
1 //=============================================================================
2 // File      : SalomePyQt.cxx
3 // Created   : 25/04/05
4 // Author    : Vadim SANDLER
5 // Project   : SALOME
6 // Copyright : 2003-2005 CEA/DEN, EDF R&D
7 // $Header   : $
8 //=============================================================================
9
10 #include "SalomePyQt.h"
11
12 #include <qapplication.h>
13 #include <qmenubar.h>
14
15 #include "SALOME_Event.hxx"
16
17 #include "SUIT_Session.h"
18 #include "SUIT_Desktop.h"
19 #include "STD_MDIDesktop.h"
20 #include "SalomeApp_Application.h"
21 #include "SalomeApp_Study.h"
22 #include "OB_Browser.h"
23 // #include "QAD_FileDlg.h"
24 // #include "QAD_ViewFrame.h"
25 // #include "QAD_RightFrame.h"
26 // #include "QAD_Tools.h"
27
28 // #include "QAD_Config.h"
29 // #include "QAD_Settings.h"
30
31 using namespace std;
32
33 //====================================================================================
34 // static functions
35 //====================================================================================
36 /*!
37   getApplication()
38   Returns active application object [ static ]
39 */
40 static SalomeApp_Application* getApplication() {
41   if ( SUIT_Session::session() )
42     return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
43   return NULL;
44 }
45
46 /*!
47   getActiveStudy()
48   Gets active study or 0 if there is no study opened [ static ]
49 */
50 static SalomeApp_Study* getActiveStudy()
51 {
52   if ( getApplication() )
53     return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
54   return 0;
55 }
56
57 //====================================================================================
58 // SALOME_Selection class (implemented to keep compatibility with previous SALOME GUI
59 //====================================================================================
60 SALOME_Selection::SALOME_Selection()
61 {
62   // VSR: TODO...
63 }
64
65 void SALOME_Selection::Clear()
66 {
67   // VSR: TODO...
68 }
69 void SALOME_Selection::ClearIObjects()
70 {
71   // VSR: TODO...
72 }
73
74 //====================================================================================
75 // SalomePyQt class
76 //====================================================================================
77
78 /*!
79   SalomePyQt::getDesktop
80   Gets desktop. Returns 0 in error.
81 */
82 class TGetDesktopEvent: public SALOME_Event {
83 public:
84   typedef QWidget* TResult;
85   TResult myResult;
86   TGetDesktopEvent() : myResult( 0 ) {}
87   virtual void Execute() {
88     if ( getApplication() )
89       myResult = (QWidget*)( getApplication()->desktop() );
90   }
91 };
92 QWidget* SalomePyQt::getDesktop()
93 {
94   return ProcessEvent( new TGetDesktopEvent() );
95 }
96
97 /*!
98   SalomePyQt::getMainFrame
99   Gets workspace widget. Returns 0 in error.
100 */
101 class TGetMainFrameEvent: public SALOME_Event {
102 public:
103   typedef QWidget* TResult;
104   TResult myResult;
105   TGetMainFrameEvent() : myResult( 0 ) {}
106   virtual void Execute() {
107     if ( getApplication() ) {
108       STD_MDIDesktop* aDesktop = dynamic_cast<STD_MDIDesktop*>( getApplication()->desktop() );
109       myResult = (QWidget*)( aDesktop->workspace() );
110     }
111   }
112 };
113 QWidget* SalomePyQt::getMainFrame()
114 {
115   return ProcessEvent( new TGetMainFrameEvent() );
116 }
117
118 /*!
119   SalomePyQt::getMainMenuBar
120   Gets main menu. Returns 0 in error.
121 */
122 class TGetMainMenuBarEvent: public SALOME_Event {
123 public:
124   typedef QMenuBar* TResult;
125   TResult myResult;
126   TGetMainMenuBarEvent() : myResult( 0 ) {}
127   virtual void Execute() {
128     if ( getApplication() ) {
129       // VSR: TODO
130       // myResult = (QMenuBar*)(QAD_Application::getDesktop()->getMainMenuBar());
131     }
132   }
133 };
134 QMenuBar* SalomePyQt::getMainMenuBar() 
135 {
136   return ProcessEvent( new TGetMainMenuBarEvent() );
137 }
138
139 /*!
140   SalomePyQt::getPopupMenu
141   Gets an main menu's child popup menu by its id
142 */
143 class TGetPopupMenuEvent: public SALOME_Event {
144 public:
145   typedef QPopupMenu* TResult;
146   TResult  myResult;
147   MenuName myMenuName;
148   TGetPopupMenuEvent( const MenuName menu ) : myResult( 0 ), myMenuName( menu ) {}
149   virtual void Execute() {
150     if ( /*SalomeApp_Study* aStudy =*/ getActiveStudy() ) {
151       // VSR: TODO
152       //QMenuBar* mainMenu = QAD_Application::getDesktop()->getMainMenuBar();
153       //if ( mainMenu->findItem( menu ) ) {
154       //  return mainMenu->findItem( menu )->popup();
155       //}
156       //return 0;
157     }
158   }
159 };
160 QPopupMenu* SalomePyQt::getPopupMenu( const MenuName menu )
161 {
162   return ProcessEvent( new TGetPopupMenuEvent( menu ) );
163 }
164
165 /*!
166   SalomePyQt::getStudyId
167   Returns active study's ID or 0 if there is no active study.
168 */
169 class TGetStudyIdEvent: public SALOME_Event {
170 public:
171   typedef int TResult;
172   TResult myResult;
173   TGetStudyIdEvent() : myResult( 0 ) {}
174   virtual void Execute() {
175     if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
176       myResult = aStudy->studyDS()->StudyId();
177     }
178   }
179 };
180 int SalomePyQt::getStudyId()
181 {
182   return ProcessEvent( new TGetStudyIdEvent() );
183 }
184
185 /*!
186   SalomePyQt::getSelection
187   Creates a Selection object (to provide a compatibility with previous SALOME GUI).
188 */
189 class TGetSelectionEvent: public SALOME_Event {
190 public:
191   typedef SALOME_Selection* TResult;
192   TResult myResult;
193   TGetSelectionEvent() : myResult( 0 ) {}
194   virtual void Execute() {
195     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
196       // VSR: TODO
197       // return SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveApp()->getActiveStudy()->getSelection());
198       myResult = new SALOME_Selection();
199     }
200   }
201 };
202 SALOME_Selection* SalomePyQt::getSelection()
203 {
204   return ProcessEvent( new TGetSelectionEvent() );
205 }
206
207 /*!
208   SalomePyQt::putInfo
209   Puts an information message to the desktop's status bar
210   (with optional delay parameter given in msec)
211 */
212 class TPutInfoEvent: public SALOME_Event {
213   QString myMsg;
214   int     myMsecs;
215 public:
216   TPutInfoEvent( const QString& msg, const int ms = 0 ) : myMsg( msg ), myMsecs( ms ) {}
217   virtual void Execute() {
218     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
219       // VSR: TODO
220       // QAD_Application::getDesktop()->putInfo(msg);
221       // ... or ...
222       // QAD_Application::getDesktop()->putInfo(msg, ms);
223     }
224   }
225 };
226 void SalomePyQt::putInfo( const QString& msg )
227 {
228   ProcessVoidEvent( new TPutInfoEvent( msg ) );
229 }
230 void SalomePyQt::putInfo( const QString& msg, const int ms )
231 {
232   ProcessVoidEvent( new TPutInfoEvent( msg, ms ) );
233 }
234
235 /*!
236   SalomePyQt::getActiveComponent
237   Returns an active component name or empty string if there is no active component
238 */
239 class TGetActiveComponentEvent: public SALOME_Event {
240 public:
241   typedef QString TResult;
242   TResult myResult;
243   TGetActiveComponentEvent() {}
244   virtual void Execute() {
245     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
246       // VSR: TODO
247       //   return QAD_Application::getDesktop()->getActiveComponent();
248     }
249   }
250 };
251 const QString SalomePyQt::getActiveComponent()
252 {
253   return ProcessEvent( new TGetActiveComponentEvent() );
254 }
255
256 /*!
257   SalomePyQt::updateObjBrowser
258   Updates an Object Browser of a given study
259   VSR: updateSelection parameter is currently not used. Will be implemented or removed lately.
260 */
261 void SalomePyQt::updateObjBrowser( const int studyId, bool updateSelection )
262 {  
263   class TEvent: public SALOME_Event {
264     int  myStudyId;
265     bool myUpdateSelection;
266   public:
267     TEvent( const int studyId, bool updateSelection ) 
268       : myStudyId( studyId ), myUpdateSelection( updateSelection ) {}
269     virtual void Execute() {
270       if ( SalomeApp_Application* anApp = getApplication() ) {
271         // VSR: TODO
272         // this implementation is temporary and works only for the active study
273         OB_Browser* browser = anApp->objectBrowser();
274         if ( browser )
275           browser->updateTree();
276         ///
277 //      QList<QAD_Study>& studies = QAD_Application::getDesktop()->getActiveApp()->getStudies();
278 //      for ( QAD_Study* study = studies.first(); study; study = studies.next() )  {
279 //        if ( study->getStudyId() == studyId ) {
280 //          study->updateObjBrowser( updateSelection );
281 //          break;
282 //        }
283 //      }
284         ///
285       }
286     }
287   };
288   ProcessVoidEvent( new TEvent( studyId, updateSelection ) );
289 }
290
291 /*!
292   SalomePyQt::addStringSetting
293   Adds a string setting to the application preferences
294 */
295 void SalomePyQt::addStringSetting( const QString& name, const QString& value, bool autoValue )
296 {
297   class TEvent: public SALOME_Event {
298     QString myName;
299     QString myValue;
300     bool    myAutoValue;
301   public:
302     TEvent( const QString& name, const QString& value, bool autoValue ) 
303       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
304     virtual void Execute() {
305       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
306         // VSR: TODO
307         // QAD_CONFIG->addSetting(_name, _value, _autoValue);
308       }
309     }
310   };
311   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
312 }
313
314 /*!
315   SalomePyQt::addIntSetting
316   Adds an integer setting to the application preferences
317 */
318 void SalomePyQt::addIntSetting( const QString& name, const int value, bool autoValue)
319 {
320   class TEvent: public SALOME_Event {
321     QString myName;
322     int     myValue;
323     bool    myAutoValue;
324   public:
325     TEvent( const QString& name, const int value, bool autoValue ) 
326       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
327     virtual void Execute() {
328       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
329         // VSR: TODO
330         // QAD_CONFIG->addSetting(_name, _value, _autoValue);
331       }
332     }
333   };
334   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
335 }
336
337 /*!
338   SalomePyQt::addDoubleSetting
339   Adds an double setting to the application preferences
340 */
341 void SalomePyQt::addDoubleSetting( const QString& name, const double value, bool autoValue )
342 {
343   class TEvent: public SALOME_Event {
344     QString myName;
345     double  myValue;
346     bool    myAutoValue;
347   public:
348     TEvent( const QString& name, const double value, bool autoValue ) 
349       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
350     virtual void Execute() {
351       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
352         // VSR: TODO
353         // QAD_CONFIG->addSetting(_name, _value, _autoValue);
354       }
355     }
356   };
357   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
358 }
359
360 /*!
361   SalomePyQt::removeSettings
362   Removes a setting from the application preferences
363 */
364 void SalomePyQt::removeSettings( const QString& name )
365 {
366   class TEvent: public SALOME_Event {
367     QString myName;
368   public:
369     TEvent( const QString& name ) : myName( name ) {}
370     virtual void Execute() {
371       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
372         // VSR: TODO
373         // QAD_CONFIG->removeSettings( name );
374       }
375     }
376   };
377   ProcessVoidEvent( new TEvent( name ) );
378 }
379
380 /*!
381   SalomePyQt::getSetting
382   Gets a setting value (as string)
383 */
384 class TGetSettingEvent: public SALOME_Event {
385 public:
386   typedef QString TResult;
387   TResult myResult;
388   QString myName;
389   TGetSettingEvent( const QString& name ) : myName( name ) {}
390   virtual void Execute() {
391     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
392       // VSR: TODO
393       // myResult = QAD_CONFIG->getSetting(name);
394     }
395   }
396 };
397 QString SalomePyQt::getSetting( const QString& name )
398 {
399   return ProcessEvent( new TGetSettingEvent( name ) );
400 }
401
402 /*!
403   SalomePyQt::getFileName
404   Displays 'Open/Save file' dialog box and returns a user's choice (file name)
405 */
406 class TGetFileNameEvent: public SALOME_Event {
407 public:
408   typedef QString TResult;
409   TResult     myResult;
410   QWidget*    myParent;
411   QString     myInitial;
412   QStringList myFilters;
413   QString     myCaption;
414   bool        myOpen;
415   TGetFileNameEvent( QWidget*           parent, 
416                      const QString&     initial, 
417                      const QStringList& filters, 
418                      const QString&     caption,
419                      bool               open ) 
420     : myParent ( parent ), 
421       myInitial( initial ), 
422       myFilters( filters ), 
423       myCaption( caption ), 
424       myOpen ( open ) {}
425   virtual void Execute() {
426     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
427       // VSR: TODO
428       // myResult = QAD_FileDlg::getFileName(parent, initial, filters, caption, open);
429     }
430   }
431 };
432 QString SalomePyQt::getFileName( QWidget*           parent, 
433                                  const QString&     initial, 
434                                  const QStringList& filters, 
435                                  const QString&     caption,
436                                  bool               open )
437 {
438   return ProcessEvent( new TGetFileNameEvent( parent, initial, filters, caption, open ) );
439 }
440
441 /*!
442   SalomePyQt::getOpenFileNames
443   Displays 'Open files' dialog box and returns a user's choice (a list of file names)
444 */
445 class TGetOpenFileNamesEvent: public SALOME_Event {
446 public:
447   typedef QStringList TResult;
448   TResult     myResult;
449   QWidget*    myParent;
450   QString     myInitial;
451   QStringList myFilters;
452   QString     myCaption;
453   TGetOpenFileNamesEvent( QWidget*           parent, 
454                           const QString&     initial, 
455                           const QStringList& filters, 
456                           const QString&     caption ) 
457     : myParent ( parent ), 
458       myInitial( initial ), 
459       myFilters( filters ), 
460       myCaption( caption ) {}
461   virtual void Execute() {
462     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
463       // VSR: TODO
464       // myResult = QAD_FileDlg::getOpenFileNames(parent, initial, filters, caption);
465     }
466   }
467 };
468 QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
469                                           const QString&     initial, 
470                                           const QStringList& filters, 
471                                           const QString&     caption )
472 {
473   return ProcessEvent( new TGetOpenFileNamesEvent( parent, initial, filters, caption ) );
474 }
475
476 /*!
477   SalomePyQt::getExistingDirectory
478   Displays 'Get Directory' dialog box and returns a user's choice (a directory name)
479 */
480 class TGetExistingDirectoryEvent: public SALOME_Event {
481 public:
482   typedef QString TResult;
483   TResult     myResult;
484   QWidget*    myParent;
485   QString     myInitial;
486   QString     myCaption;
487   TGetExistingDirectoryEvent( QWidget*           parent, 
488                               const QString&     initial, 
489                               const QString&     caption ) 
490     : myParent ( parent ), 
491       myInitial( initial ), 
492       myCaption( caption ) {}
493   virtual void Execute() {
494     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
495       // VSR: TODO
496       // myResult = QAD_FileDlg::getExistingDirectory(parent, initial, caption);
497     }
498   }
499 };
500 QString SalomePyQt::getExistingDirectory( QWidget*       parent,
501                                           const QString& initial,
502                                           const QString& caption )
503 {
504   return ProcessEvent( new TGetExistingDirectoryEvent( parent, initial, caption ) );
505 }
506
507 /*!
508   SalomePyQt::helpContext
509   Opens external browser to display 'context help' information
510   VSR: current implementation does nothing.
511 */
512 void SalomePyQt::helpContext( const QString& source, const QString& context ) {
513   class TEvent: public SALOME_Event {
514     QString mySource;
515     QString myContext;
516   public:
517     TEvent( const QString& source, const QString& context ) 
518       : mySource( source ), myContext( context ) {}
519     virtual void Execute() {
520       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
521         // VSR: TODO
522 ////QAD_Application::getDesktop()->helpContext(source, context);
523       }
524     }
525   };
526   ProcessVoidEvent( new TEvent( source, context ) );
527 }
528
529 /*!
530   SalomePyQt::dumpView
531   Dumps the contents of the currently active view to the image file 
532   in the given format (JPEG, PNG, BMP are supported)
533 */
534 class TDumpViewEvent: public SALOME_Event {
535 public:
536   typedef bool TResult;
537   TResult myResult;
538   QString myFileName;
539   TDumpViewEvent( const QString& filename ) 
540     : myResult ( false ), myFileName( filename ) {}
541   virtual void Execute() {
542     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
543       // VSR: TODO
544 //   QAD_Study* activeStudy = QAD_Application::getDesktop()->getActiveApp()->getActiveStudy();
545 //   if ( !activeStudy )
546 //     return false;
547 //   QAD_ViewFrame* activeViewFrame = activeStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame();
548 //   if ( !activeViewFrame )
549 //     return false;
550 //   if ( !activeViewFrame->getViewWidget() )
551 //     return false;
552
553 //   qApp->processEvents();
554 //   QPixmap px = QPixmap::grabWindow( activeViewFrame->getViewWidget()->winId() );
555 //   if ( !filename.isNull() ) {
556 //     QString fmt = QAD_Tools::getFileExtensionFromPath( filename ).upper();
557 //     if ( fmt.isEmpty() )
558 //       fmt = QString( "PNG" ); // default format
559 //     if ( fmt == "JPG" )
560 //       fmt = "JPEG";
561 //     bool bOk = px.save( filename, fmt.latin1() );
562 //     return bOk;
563 //   }
564 //   return false;
565     }
566   }
567 };
568 bool SalomePyQt::dumpView( const QString& filename )
569 {
570   return ProcessEvent( new TDumpViewEvent( filename ) );
571 }