1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "SUIT_Application.h"
21 #include "SUIT_Session.h"
22 #include "SUIT_Desktop.h"
23 #include "SUIT_ResourceMgr.h"
27 #include <qstatusbar.h>
29 #include <QtxAction.h>
30 #include <QtxActionMenuMgr.h>
31 #include <QtxActionToolMgr.h>
33 SUIT_Application::SUIT_Application()
41 SUIT_Application::~SUIT_Application()
49 SUIT_Desktop* SUIT_Application::desktop()
54 bool SUIT_Application::isPossibleToClose()
59 void SUIT_Application::closeApplication()
61 emit applicationClosed( this );
64 SUIT_Study* SUIT_Application::activeStudy() const
69 QString SUIT_Application::applicationVersion() const
74 void SUIT_Application::start()
80 bool SUIT_Application::useFile( const QString& theFileName )
83 SUIT_Study* study = activeStudy();
85 bool status = study ? study->openDocument( theFileName ) : false;
96 bool SUIT_Application::useStudy( const QString& theName )
101 void SUIT_Application::createEmptyStudy()
103 if ( !activeStudy() )
104 setActiveStudy( createNewStudy() );
107 int SUIT_Application::getNbStudies() const
109 return activeStudy() ? 1 : 0;
112 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
114 if ( !SUIT_Session::session() )
117 return SUIT_Session::session()->resourceMgr();
120 #define DEFAULT_MESSAGE_DELAY 3000
121 void SUIT_Application::putInfo ( const QString& msg, const int msec )
126 if ( !myStatusLabel )
128 myStatusLabel = new QLabel( desktop()->statusBar() );
129 desktop()->statusBar()->addWidget( myStatusLabel, 1 );
130 myStatusLabel->show();
133 myStatusLabel->setText( msg );
135 QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, myStatusLabel, SLOT( clear() ) );
138 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
140 return startApplication( name(), argc, argv );
143 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
145 SUIT_Session* session = SUIT_Session::session();
149 return session->startApplication( name, argc, argv );
152 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
154 if ( myDesktop == desk )
160 connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
163 SUIT_Study* SUIT_Application::createNewStudy()
165 return new SUIT_Study( this );
168 void SUIT_Application::setActiveStudy( SUIT_Study* study )
170 if ( myStudy == study )
176 int SUIT_Application::createTool( const QString& name )
178 if ( !desktop() || !desktop()->toolMgr() )
181 return desktop()->toolMgr()->createToolBar( name );
184 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
186 if ( !desktop() || !desktop()->toolMgr() )
189 int regId = registerAction( id, a );
190 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
191 return intId != -1 ? regId : -1;
194 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
196 if ( !desktop() || !desktop()->toolMgr() )
199 int regId = registerAction( id, a );
200 int intId = desktop()->toolMgr()->insert( a, tBar, idx );
201 return intId != -1 ? regId : -1;
204 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
206 if ( !desktop() || !desktop()->toolMgr() )
209 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
210 return intId != -1 ? id : -1;
213 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
215 if ( !desktop() || !desktop()->toolMgr() )
218 int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
219 return intId != -1 ? id : -1;
222 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
223 const int id, const int group, const int index )
225 if ( !desktop() || !desktop()->menuMgr() )
228 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
231 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
232 const int id, const int group, const int index )
234 if ( !desktop() || !desktop()->menuMgr() )
237 return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
240 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
242 if ( !a || !desktop() || !desktop()->menuMgr() )
245 int regId = registerAction( id, a );
246 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
247 return intId != -1 ? regId : -1;
250 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
252 if ( !a || !desktop() || !desktop()->menuMgr() )
255 int regId = registerAction( id, a );
256 int intId = desktop()->menuMgr()->insert( a, menu, group, index );
257 return intId != -1 ? regId : -1;
260 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
262 if ( !desktop() || !desktop()->menuMgr() )
265 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
266 return intId != -1 ? id : -1;
269 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
271 if ( !desktop() || !desktop()->menuMgr() )
274 int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
275 return intId != -1 ? id : -1;
278 void SUIT_Application::setMenuShown( QAction* a, const bool on )
280 setMenuShown( actionId( a ), on );
283 void SUIT_Application::setMenuShown( const int id, const bool on )
285 if ( desktop() && desktop()->menuMgr() )
286 desktop()->menuMgr()->setShown( id, on );
289 void SUIT_Application::setToolShown( QAction* a, const bool on )
291 setToolShown( actionId( a ), on );
294 void SUIT_Application::setToolShown( const int id, const bool on )
296 if ( desktop() && desktop()->toolMgr() )
297 desktop()->toolMgr()->setShown( id, on );
300 void SUIT_Application::setActionShown( QAction* a, const bool on )
302 setMenuShown( a, on );
303 setToolShown( a, on );
306 void SUIT_Application::setActionShown( const int id, const bool on )
308 setMenuShown( id, on );
309 setToolShown( id, on );
312 QAction* SUIT_Application::action( const int id ) const
315 if ( myActionMap.contains( id ) )
320 int SUIT_Application::actionId( const QAction* a ) const
323 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin();
324 it != myActionMap.end() && id == -1;
326 if ( it.data() == a )
332 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIconSet& icon,
333 const QString& menu, const QString& tip, const int key,
334 QObject* parent, const bool toggle, QObject* reciever, const char* member )
336 QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
337 a->setStatusTip( tip );
339 if ( reciever && member )
340 connect( a, SIGNAL( activated() ), reciever, member );
342 registerAction( id, a );
347 int SUIT_Application::registerAction( const int id, QAction* a )
349 int ident = actionId( a );
353 static int generatedId = -1;
354 ident = id == -1 ? --generatedId : id;
356 if ( action( ident ) )
357 qWarning( "Action registration id is already in use: %d", ident );
359 myActionMap.insert( ident, a );
361 if ( desktop() && desktop()->menuMgr() )
362 desktop()->menuMgr()->registerAction( a );
364 if ( desktop() && desktop()->toolMgr() )
365 desktop()->toolMgr()->registerAction( a );
370 QAction* SUIT_Application::separator()
372 return QtxActionMgr::separator();
375 void SUIT_Application::onDesktopActivated()
377 emit activated( this );