Salome HOME
Fix a bug : avoid Qt's warning
[modules/gui.git] / src / SUIT / SUIT_Application.cxx
1 #include "SUIT_Application.h"
2
3 #include "SUIT_Session.h"
4 #include "SUIT_Desktop.h"
5 #include "SUIT_ResourceMgr.h"
6
7 #include <qstatusbar.h>
8
9 #include <QtxAction.h>
10 #include <QtxActionMenuMgr.h>
11 #include <QtxActionToolMgr.h>
12
13 SUIT_Application::SUIT_Application()
14 : QObject( 0 ),
15 myStudy( 0 ),
16 myDesktop( 0 )
17
18 }
19
20 SUIT_Application::~SUIT_Application() 
21 {
22   delete myStudy;
23   myStudy = 0;
24 }
25
26 SUIT_Desktop* SUIT_Application::desktop()
27 {
28   return myDesktop;
29 }
30
31 bool SUIT_Application::isPossibleToClose()
32 {
33   return true;
34 }
35
36 void SUIT_Application::closeApplication()
37 {
38   emit applicationClosed( this );
39 }
40
41 SUIT_Study* SUIT_Application::activeStudy() const
42 {
43   return myStudy;
44 }
45
46 QString SUIT_Application::applicationVersion() const
47 {
48   return QString::null;
49 }
50
51 void SUIT_Application::start()
52 {
53   if ( desktop() )
54     desktop()->show();
55 }
56
57 bool SUIT_Application::useFile( const QString& theFileName )
58 {
59   createEmptyStudy();
60   if ( activeStudy() )
61     return activeStudy()->openDocument( theFileName );
62 }
63
64 void SUIT_Application::createEmptyStudy()
65 {
66   if ( !activeStudy() )
67     setActiveStudy( createNewStudy() );
68 }
69
70 int SUIT_Application::getNbStudies() const
71 {
72   return activeStudy() ? 1 : 0;
73 }
74
75 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
76 {
77   if ( !SUIT_Session::session() )
78     return 0;
79
80   return SUIT_Session::session()->resourceMgr();
81 }
82
83 #define DEFAULT_MESSAGE_DELAY 3000
84 void SUIT_Application::putInfo ( const QString& msg, const int msec )
85 {
86   if ( desktop() )
87     desktop()->statusBar()->message( msg, msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec );
88 }
89
90 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
91 {
92   return startApplication( name(), argc, argv );
93 }
94
95 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
96 {
97   SUIT_Session* session = SUIT_Session::session();
98   if ( !session )
99     return 0;
100
101   return session->startApplication( name, argc, argv );
102 }
103
104 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
105 {
106   if ( myDesktop == desk )
107     return;
108
109   delete myDesktop;
110   myDesktop = desk;
111   if ( myDesktop )
112     connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
113 }
114
115 SUIT_Study* SUIT_Application::createNewStudy()
116 {
117   return new SUIT_Study( this );
118 }
119
120 void SUIT_Application::setActiveStudy( SUIT_Study* study )
121 {
122   if ( myStudy == study )
123     return;
124
125   myStudy = study;
126 }
127
128 int SUIT_Application::createTool( const QString& name )
129 {
130   if ( !desktop() || !desktop()->toolMgr() )
131     return -1;
132
133   return desktop()->toolMgr()->createToolBar( name );
134 }
135
136 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
137 {
138   if ( !desktop() || !desktop()->toolMgr() )
139     return -1;
140
141   int regId = desktop()->toolMgr()->registerAction( a, id );
142   return desktop()->toolMgr()->insert( regId, tBar, idx );
143 }
144
145 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
146 {
147   if ( !desktop() || !desktop()->toolMgr() )
148     return -1;
149
150   return desktop()->toolMgr()->insert( a, tBar, idx );
151 }
152
153 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
154 {
155   if ( !desktop() || !desktop()->toolMgr() )
156     return -1;
157
158   return desktop()->toolMgr()->insert( id, tBar, idx );
159 }
160
161 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
162 {
163   if ( !desktop() || !desktop()->toolMgr() )
164     return -1;
165
166   return desktop()->toolMgr()->insert( id, tBar, idx );
167 }
168
169 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
170                                   const int id, const int group, const int index )
171 {
172   if ( !desktop() || !desktop()->menuMgr() )
173     return -1;
174
175   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
176 }
177
178 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
179                                   const int id, const int group, const int index )
180 {
181   if ( !desktop() || !desktop()->menuMgr() )
182     return -1;
183
184   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
185 }
186
187 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
188 {
189   if ( !a || !desktop() || !desktop()->menuMgr() )
190     return -1;
191
192   int regId = desktop()->menuMgr()->registerAction( a, id );
193   return desktop()->menuMgr()->insert( regId, menu, group, index );
194 }
195
196 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
197 {
198   if ( !a || !desktop() || !desktop()->menuMgr() )
199     return -1;
200
201   int regId = desktop()->menuMgr()->registerAction( a, id );
202   return desktop()->menuMgr()->insert( regId, menu, group, index );
203 }
204
205 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
206 {
207   if ( !desktop() || !desktop()->menuMgr() )
208     return -1;
209
210   return desktop()->menuMgr()->insert( id, menu, group, index );
211 }
212
213 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
214 {
215   if ( !desktop() || !desktop()->menuMgr() )
216     return -1;
217
218   return desktop()->menuMgr()->insert( id, menu, group, index );
219 }
220
221 void SUIT_Application::setMenuShown( QAction* a, const bool on )
222 {
223   setMenuShown( actionId( a ), on );
224 }
225
226 void SUIT_Application::setMenuShown( const int id, const bool on )
227 {
228   if ( desktop() && desktop()->menuMgr() )
229     desktop()->menuMgr()->setShown( id, on );
230 }
231
232 void SUIT_Application::setToolShown( QAction* a, const bool on )
233 {
234   setToolShown( actionId( a ), on );
235 }
236
237 void SUIT_Application::setToolShown( const int id, const bool on )
238 {
239   if ( desktop() && desktop()->toolMgr() )
240     desktop()->toolMgr()->setShown( id, on );
241 }
242
243 QAction* SUIT_Application::action( const int id ) const
244 {
245   SUIT_Application* that = (SUIT_Application*)this;
246   SUIT_Desktop* desk = that->desktop();
247   if ( !desk )
248     return 0;
249
250   QAction* a = 0;
251   if ( desk->menuMgr() )
252     a = desk->menuMgr()->action( id );
253   if ( !a && desk->toolMgr() )
254     a = desk->toolMgr()->action( id );
255   return a;
256 }
257
258 int SUIT_Application::actionId( const QAction* a ) const
259 {
260   SUIT_Application* that = (SUIT_Application*)this;
261   SUIT_Desktop* desk = that->desktop();
262   if ( !desk )
263     return 0;
264
265   int id = -1;
266   if ( desk->menuMgr() )
267     id = desk->menuMgr()->actionId( a );
268   if ( id == -1 && desk->toolMgr() )
269     id = desk->toolMgr()->actionId( a );
270   return id;
271 }
272
273 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIconSet& icon,
274                                          const QString& menu, const QString& tip, const int key,
275                                          QObject* parent, const bool toggle, QObject* reciever, const char* member )
276 {
277   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
278   a->setStatusTip( tip );
279
280   if ( reciever && member )
281     connect( a, SIGNAL( activated() ), reciever, member );
282
283   registerAction( id, a );
284
285   return a;
286 }
287
288 void SUIT_Application::registerAction( const int id, QAction* a )
289 {
290   if ( desktop() && desktop()->menuMgr() )
291     desktop()->menuMgr()->registerAction( a, id );
292
293   if ( desktop() && desktop()->toolMgr() )
294     desktop()->toolMgr()->registerAction( a, id );
295 }
296
297 QAction* SUIT_Application::separator()
298 {
299   return QtxActionMgr::separator();
300 }
301
302 void SUIT_Application::onDesktopActivated()
303 {
304   emit activated( this );
305 }