1 //=============================================================================
2 // File : SalomePyQt.cxx
4 // Author : Vadim SANDLER
6 // Copyright : 2003-2005 CEA/DEN, EDF R&D
8 //=============================================================================
10 #include "SalomePyQt.h"
12 #include <qapplication.h>
15 #include "SALOME_Event.hxx"
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"
28 // #include "QAD_Config.h"
29 // #include "QAD_Settings.h"
33 //====================================================================================
35 //====================================================================================
38 Returns active application object [ static ]
40 static SalomeApp_Application* getApplication() {
41 if ( SUIT_Session::session() )
42 return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
48 Gets active study or 0 if there is no study opened [ static ]
50 static SalomeApp_Study* getActiveStudy()
52 if ( getApplication() )
53 return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
57 //====================================================================================
58 // SALOME_Selection class (implemented to keep compatibility with previous SALOME GUI
59 //====================================================================================
60 SALOME_Selection::SALOME_Selection()
65 void SALOME_Selection::Clear()
69 void SALOME_Selection::ClearIObjects()
74 //====================================================================================
76 //====================================================================================
79 SalomePyQt::getDesktop
80 Gets desktop. Returns 0 in error.
82 class TGetDesktopEvent: public SALOME_Event {
84 typedef QWidget* TResult;
86 TGetDesktopEvent() : myResult( 0 ) {}
87 virtual void Execute() {
88 if ( getApplication() )
89 myResult = (QWidget*)( getApplication()->desktop() );
92 QWidget* SalomePyQt::getDesktop()
94 return ProcessEvent( new TGetDesktopEvent() );
98 SalomePyQt::getMainFrame
99 Gets workspace widget. Returns 0 in error.
101 class TGetMainFrameEvent: public SALOME_Event {
103 typedef QWidget* TResult;
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() );
113 QWidget* SalomePyQt::getMainFrame()
115 return ProcessEvent( new TGetMainFrameEvent() );
119 SalomePyQt::getMainMenuBar
120 Gets main menu. Returns 0 in error.
122 class TGetMainMenuBarEvent: public SALOME_Event {
124 typedef QMenuBar* TResult;
126 TGetMainMenuBarEvent() : myResult( 0 ) {}
127 virtual void Execute() {
128 if ( getApplication() ) {
130 // myResult = (QMenuBar*)(QAD_Application::getDesktop()->getMainMenuBar());
134 QMenuBar* SalomePyQt::getMainMenuBar()
136 return ProcessEvent( new TGetMainMenuBarEvent() );
140 SalomePyQt::getPopupMenu
141 Gets an main menu's child popup menu by its id
143 class TGetPopupMenuEvent: public SALOME_Event {
145 typedef QPopupMenu* TResult;
148 TGetPopupMenuEvent( const MenuName menu ) : myResult( 0 ), myMenuName( menu ) {}
149 virtual void Execute() {
150 if ( /*SalomeApp_Study* aStudy =*/ getActiveStudy() ) {
152 //QMenuBar* mainMenu = QAD_Application::getDesktop()->getMainMenuBar();
153 //if ( mainMenu->findItem( menu ) ) {
154 // return mainMenu->findItem( menu )->popup();
160 QPopupMenu* SalomePyQt::getPopupMenu( const MenuName menu )
162 return ProcessEvent( new TGetPopupMenuEvent( menu ) );
166 SalomePyQt::getStudyId
167 Returns active study's ID or 0 if there is no active study.
169 class TGetStudyIdEvent: public SALOME_Event {
173 TGetStudyIdEvent() : myResult( 0 ) {}
174 virtual void Execute() {
175 if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
176 myResult = aStudy->studyDS()->StudyId();
180 int SalomePyQt::getStudyId()
182 return ProcessEvent( new TGetStudyIdEvent() );
186 SalomePyQt::getSelection
187 Creates a Selection object (to provide a compatibility with previous SALOME GUI).
189 class TGetSelectionEvent: public SALOME_Event {
191 typedef SALOME_Selection* TResult;
193 TGetSelectionEvent() : myResult( 0 ) {}
194 virtual void Execute() {
195 if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
197 // return SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveApp()->getActiveStudy()->getSelection());
198 myResult = new SALOME_Selection();
202 SALOME_Selection* SalomePyQt::getSelection()
204 return ProcessEvent( new TGetSelectionEvent() );
209 Puts an information message to the desktop's status bar
210 (with optional delay parameter given in msec)
212 class TPutInfoEvent: public SALOME_Event {
216 TPutInfoEvent( const QString& msg, const int ms = 0 ) : myMsg( msg ), myMsecs( ms ) {}
217 virtual void Execute() {
218 if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
220 // QAD_Application::getDesktop()->putInfo(msg);
222 // QAD_Application::getDesktop()->putInfo(msg, ms);
226 void SalomePyQt::putInfo( const QString& msg )
228 ProcessVoidEvent( new TPutInfoEvent( msg ) );
230 void SalomePyQt::putInfo( const QString& msg, const int ms )
232 ProcessVoidEvent( new TPutInfoEvent( msg, ms ) );
236 SalomePyQt::getActiveComponent
237 Returns an active component name or empty string if there is no active component
239 class TGetActiveComponentEvent: public SALOME_Event {
241 typedef QString TResult;
243 TGetActiveComponentEvent() {}
244 virtual void Execute() {
245 if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
247 // return QAD_Application::getDesktop()->getActiveComponent();
251 const QString SalomePyQt::getActiveComponent()
253 return ProcessEvent( new TGetActiveComponentEvent() );
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.
261 void SalomePyQt::updateObjBrowser( const int studyId, bool updateSelection )
263 class TEvent: public SALOME_Event {
265 bool myUpdateSelection;
267 TEvent( const int studyId, bool updateSelection )
268 : myStudyId( studyId ), myUpdateSelection( updateSelection ) {}
269 virtual void Execute() {
270 if ( SalomeApp_Application* anApp = getApplication() ) {
272 // this implementation is temporary and works only for the active study
273 OB_Browser* browser = anApp->objectBrowser();
275 browser->updateTree();
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 );
288 ProcessVoidEvent( new TEvent( studyId, updateSelection ) );
292 SalomePyQt::addStringSetting
293 Adds a string setting to the application preferences
295 void SalomePyQt::addStringSetting( const QString& name, const QString& value, bool autoValue )
297 class TEvent: public SALOME_Event {
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() ) {
307 // QAD_CONFIG->addSetting(_name, _value, _autoValue);
311 ProcessVoidEvent( new TEvent( name, value, autoValue ) );
315 SalomePyQt::addIntSetting
316 Adds an integer setting to the application preferences
318 void SalomePyQt::addIntSetting( const QString& name, const int value, bool autoValue)
320 class TEvent: public SALOME_Event {
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() ) {
330 // QAD_CONFIG->addSetting(_name, _value, _autoValue);
334 ProcessVoidEvent( new TEvent( name, value, autoValue ) );
338 SalomePyQt::addDoubleSetting
339 Adds an double setting to the application preferences
341 void SalomePyQt::addDoubleSetting( const QString& name, const double value, bool autoValue )
343 class TEvent: public SALOME_Event {
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() ) {
353 // QAD_CONFIG->addSetting(_name, _value, _autoValue);
357 ProcessVoidEvent( new TEvent( name, value, autoValue ) );
361 SalomePyQt::removeSettings
362 Removes a setting from the application preferences
364 void SalomePyQt::removeSettings( const QString& name )
366 class TEvent: public SALOME_Event {
369 TEvent( const QString& name ) : myName( name ) {}
370 virtual void Execute() {
371 if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
373 // QAD_CONFIG->removeSettings( name );
377 ProcessVoidEvent( new TEvent( name ) );
381 SalomePyQt::getSetting
382 Gets a setting value (as string)
384 class TGetSettingEvent: public SALOME_Event {
386 typedef QString TResult;
389 TGetSettingEvent( const QString& name ) : myName( name ) {}
390 virtual void Execute() {
391 if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
393 // myResult = QAD_CONFIG->getSetting(name);
397 QString SalomePyQt::getSetting( const QString& name )
399 return ProcessEvent( new TGetSettingEvent( name ) );
403 SalomePyQt::getFileName
404 Displays 'Open/Save file' dialog box and returns a user's choice (file name)
406 class TGetFileNameEvent: public SALOME_Event {
408 typedef QString TResult;
412 QStringList myFilters;
415 TGetFileNameEvent( QWidget* parent,
416 const QString& initial,
417 const QStringList& filters,
418 const QString& caption,
420 : myParent ( parent ),
421 myInitial( initial ),
422 myFilters( filters ),
423 myCaption( caption ),
425 virtual void Execute() {
426 if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
428 // myResult = QAD_FileDlg::getFileName(parent, initial, filters, caption, open);
432 QString SalomePyQt::getFileName( QWidget* parent,
433 const QString& initial,
434 const QStringList& filters,
435 const QString& caption,
438 return ProcessEvent( new TGetFileNameEvent( parent, initial, filters, caption, open ) );
442 SalomePyQt::getOpenFileNames
443 Displays 'Open files' dialog box and returns a user's choice (a list of file names)
445 class TGetOpenFileNamesEvent: public SALOME_Event {
447 typedef QStringList TResult;
451 QStringList myFilters;
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() ) {
464 // myResult = QAD_FileDlg::getOpenFileNames(parent, initial, filters, caption);
468 QStringList SalomePyQt::getOpenFileNames( QWidget* parent,
469 const QString& initial,
470 const QStringList& filters,
471 const QString& caption )
473 return ProcessEvent( new TGetOpenFileNamesEvent( parent, initial, filters, caption ) );
477 SalomePyQt::getExistingDirectory
478 Displays 'Get Directory' dialog box and returns a user's choice (a directory name)
480 class TGetExistingDirectoryEvent: public SALOME_Event {
482 typedef QString TResult;
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() ) {
496 // myResult = QAD_FileDlg::getExistingDirectory(parent, initial, caption);
500 QString SalomePyQt::getExistingDirectory( QWidget* parent,
501 const QString& initial,
502 const QString& caption )
504 return ProcessEvent( new TGetExistingDirectoryEvent( parent, initial, caption ) );
508 SalomePyQt::helpContext
509 Opens external browser to display 'context help' information
510 VSR: current implementation does nothing.
512 void SalomePyQt::helpContext( const QString& source, const QString& context ) {
513 class TEvent: public SALOME_Event {
517 TEvent( const QString& source, const QString& context )
518 : mySource( source ), myContext( context ) {}
519 virtual void Execute() {
520 if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
522 ////QAD_Application::getDesktop()->helpContext(source, context);
526 ProcessVoidEvent( new TEvent( source, context ) );
531 Dumps the contents of the currently active view to the image file
532 in the given format (JPEG, PNG, BMP are supported)
534 class TDumpViewEvent: public SALOME_Event {
536 typedef bool TResult;
539 TDumpViewEvent( const QString& filename )
540 : myResult ( false ), myFileName( filename ) {}
541 virtual void Execute() {
542 if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
544 // QAD_Study* activeStudy = QAD_Application::getDesktop()->getActiveApp()->getActiveStudy();
545 // if ( !activeStudy )
547 // QAD_ViewFrame* activeViewFrame = activeStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame();
548 // if ( !activeViewFrame )
550 // if ( !activeViewFrame->getViewWidget() )
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" )
561 // bool bOk = px.save( filename, fmt.latin1() );
568 bool SalomePyQt::dumpView( const QString& filename )
570 return ProcessEvent( new TDumpViewEvent( filename ) );