Salome HOME
Merge from OCC_development_generic_2006
[modules/gui.git] / src / SUIT / SUIT_Application.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "SUIT_Application.h"
20
21 #include "SUIT_Session.h"
22 #include "SUIT_Desktop.h"
23 #include "SUIT_ResourceMgr.h"
24
25 #include <qlabel.h>
26 #include <qtimer.h>
27 #include <qstatusbar.h>
28
29 #include <QtxAction.h>
30 #include <QtxActionMenuMgr.h>
31 #include <QtxActionToolMgr.h>
32
33 SUIT_Application::SUIT_Application()
34 : QObject( 0 ),
35 myStudy( 0 ),
36 myDesktop( 0 ),
37 myStatusLabel( 0 )
38
39 }
40
41 SUIT_Application::~SUIT_Application() 
42 {
43   delete myStudy;
44   myStudy = 0;
45
46   setDesktop( 0 );
47 }
48
49 SUIT_Desktop* SUIT_Application::desktop()
50 {
51   return myDesktop;
52 }
53
54 bool SUIT_Application::isPossibleToClose()
55 {
56   return true;
57 }
58
59 void SUIT_Application::closeApplication()
60 {
61   emit applicationClosed( this );
62 }
63
64 SUIT_Study* SUIT_Application::activeStudy() const
65 {
66   return myStudy;
67 }
68
69 QString SUIT_Application::applicationVersion() const
70 {
71   return QString::null;
72 }
73
74 void SUIT_Application::start()
75 {
76   if ( desktop() )
77     desktop()->show();
78 }
79
80 bool SUIT_Application::useFile( const QString& theFileName )
81 {
82   createEmptyStudy();
83   SUIT_Study* study = activeStudy();
84
85   bool status = study ? study->openDocument( theFileName ) : false;
86
87   if ( !status )
88   {
89     setActiveStudy( 0 );
90     delete study;
91   }
92
93   return status;
94 }
95
96 bool SUIT_Application::useStudy( const QString& theName )
97 {
98   return false;
99 }
100
101 void SUIT_Application::createEmptyStudy()
102 {
103   if ( !activeStudy() )
104     setActiveStudy( createNewStudy() );
105 }
106
107 int SUIT_Application::getNbStudies() const
108 {
109   return activeStudy() ? 1 : 0;
110 }
111
112 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
113 {
114   if ( !SUIT_Session::session() )
115     return 0;
116
117   return SUIT_Session::session()->resourceMgr();
118 }
119
120 #define DEFAULT_MESSAGE_DELAY 3000
121 void SUIT_Application::putInfo ( const QString& msg, const int msec )
122 {
123   if ( !desktop() )
124     return;
125
126   if ( !myStatusLabel )
127   {
128     myStatusLabel = new QLabel( desktop()->statusBar() );
129     desktop()->statusBar()->addWidget( myStatusLabel, 1 );
130     myStatusLabel->show();
131   }
132
133   myStatusLabel->setText( msg );
134   if ( msec != -1 )
135     QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, myStatusLabel, SLOT( clear() ) );
136 }
137
138 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
139 {
140   return startApplication( name(), argc, argv );
141 }
142
143 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
144 {
145   SUIT_Session* session = SUIT_Session::session();
146   if ( !session )
147     return 0;
148
149   return session->startApplication( name, argc, argv );
150 }
151
152 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
153 {
154   if ( myDesktop == desk )
155     return;
156
157   delete myDesktop;
158   myDesktop = desk;
159   if ( myDesktop )
160     connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
161 }
162
163 SUIT_Study* SUIT_Application::createNewStudy()
164 {
165   return new SUIT_Study( this );
166 }
167
168 void SUIT_Application::setActiveStudy( SUIT_Study* study )
169 {
170   if ( myStudy == study )
171     return;
172
173   myStudy = study;
174 }
175
176 int SUIT_Application::createTool( const QString& name )
177 {
178   if ( !desktop() || !desktop()->toolMgr() )
179     return -1;
180
181   return desktop()->toolMgr()->createToolBar( name );
182 }
183
184 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
185 {
186   if ( !desktop() || !desktop()->toolMgr() )
187     return -1;
188
189   int regId = desktop()->toolMgr()->registerAction( a, id );
190   return desktop()->toolMgr()->insert( regId, tBar, idx );
191 }
192
193 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
194 {
195   if ( !desktop() || !desktop()->toolMgr() )
196     return -1;
197
198   return desktop()->toolMgr()->insert( a, tBar, idx );
199 }
200
201 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
202 {
203   if ( !desktop() || !desktop()->toolMgr() )
204     return -1;
205
206   return desktop()->toolMgr()->insert( id, tBar, idx );
207 }
208
209 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
210 {
211   if ( !desktop() || !desktop()->toolMgr() )
212     return -1;
213
214   return desktop()->toolMgr()->insert( id, tBar, idx );
215 }
216
217 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
218                                   const int id, const int group, const int index )
219 {
220   if ( !desktop() || !desktop()->menuMgr() )
221     return -1;
222
223   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
224 }
225
226 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
227                                   const int id, const int group, const int index )
228 {
229   if ( !desktop() || !desktop()->menuMgr() )
230     return -1;
231
232   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
233 }
234
235 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
236 {
237   if ( !a || !desktop() || !desktop()->menuMgr() )
238     return -1;
239
240   int regId = desktop()->menuMgr()->registerAction( a, id );
241   return desktop()->menuMgr()->insert( regId, menu, group, index );
242 }
243
244 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
245 {
246   if ( !a || !desktop() || !desktop()->menuMgr() )
247     return -1;
248
249   int regId = desktop()->menuMgr()->registerAction( a, id );
250   return desktop()->menuMgr()->insert( regId, menu, group, index );
251 }
252
253 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
254 {
255   if ( !desktop() || !desktop()->menuMgr() )
256     return -1;
257
258   return desktop()->menuMgr()->insert( id, menu, group, index );
259 }
260
261 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
262 {
263   if ( !desktop() || !desktop()->menuMgr() )
264     return -1;
265
266   return desktop()->menuMgr()->insert( id, menu, group, index );
267 }
268
269 void SUIT_Application::setMenuShown( QAction* a, const bool on )
270 {
271   setMenuShown( actionId( a ), on );
272 }
273
274 void SUIT_Application::setMenuShown( const int id, const bool on )
275 {
276   if ( desktop() && desktop()->menuMgr() )
277     desktop()->menuMgr()->setShown( id, on );
278 }
279
280 void SUIT_Application::setToolShown( QAction* a, const bool on )
281 {
282   setToolShown( actionId( a ), on );
283 }
284
285 void SUIT_Application::setToolShown( const int id, const bool on )
286 {
287   if ( desktop() && desktop()->toolMgr() )
288     desktop()->toolMgr()->setShown( id, on );
289 }
290
291 void SUIT_Application::setActionShown( QAction* a, const bool on )
292 {
293   setMenuShown( a, on );
294   setToolShown( a, on );
295 }
296
297 void SUIT_Application::setActionShown( const int id, const bool on )
298 {
299   setMenuShown( id, on );
300   setToolShown( id, on );
301 }
302
303 QAction* SUIT_Application::action( const int id ) const
304 {
305   SUIT_Application* that = (SUIT_Application*)this;
306   SUIT_Desktop* desk = that->desktop();
307   if ( !desk )
308     return 0;
309
310   QAction* a = 0;
311   if ( desk->menuMgr() )
312     a = desk->menuMgr()->action( id );
313   if ( !a && desk->toolMgr() )
314     a = desk->toolMgr()->action( id );
315   return a;
316 }
317
318 int SUIT_Application::actionId( const QAction* a ) const
319 {
320   SUIT_Application* that = (SUIT_Application*)this;
321   SUIT_Desktop* desk = that->desktop();
322   if ( !desk )
323     return 0;
324
325   int id = -1;
326   if ( desk->menuMgr() )
327     id = desk->menuMgr()->actionId( a );
328   if ( id == -1 && desk->toolMgr() )
329     id = desk->toolMgr()->actionId( a );
330   return id;
331 }
332
333 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIconSet& icon,
334                                          const QString& menu, const QString& tip, const int key,
335                                          QObject* parent, const bool toggle, QObject* reciever, const char* member )
336 {
337   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
338   a->setStatusTip( tip );
339
340   if ( reciever && member )
341     connect( a, SIGNAL( activated() ), reciever, member );
342
343   registerAction( id, a );
344
345   return a;
346 }
347
348 void SUIT_Application::registerAction( const int id, QAction* a )
349 {
350   if ( desktop() && desktop()->menuMgr() )
351     desktop()->menuMgr()->registerAction( a, id );
352
353   if ( desktop() && desktop()->toolMgr() )
354     desktop()->toolMgr()->registerAction( a, id );
355 }
356
357 QAction* SUIT_Application::separator()
358 {
359   return QtxActionMgr::separator();
360 }
361
362 void SUIT_Application::onDesktopActivated()
363 {
364   emit activated( this );
365 }