]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_Application.cxx
Salome HOME
bf0daebe671b6feb7ffcc9baf0c68d02096b10aa
[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 = registerAction( id, a );
190   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
191   return intId != -1 ? regId : -1;
192 }
193
194 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
195 {
196   if ( !desktop() || !desktop()->toolMgr() )
197     return -1;
198
199   int regId = registerAction( id, a );
200   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
201   return intId != -1 ? regId : -1;
202 }
203
204 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
205 {
206   if ( !desktop() || !desktop()->toolMgr() )
207     return -1;
208
209   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
210   return intId != -1 ? id : -1;
211 }
212
213 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
214 {
215   if ( !desktop() || !desktop()->toolMgr() )
216     return -1;
217
218   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
219   return intId != -1 ? id : -1;
220 }
221
222 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
223                                   const int id, const int group, const int index )
224 {
225   if ( !desktop() || !desktop()->menuMgr() )
226     return -1;
227
228   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
229 }
230
231 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
232                                   const int id, const int group, const int index )
233 {
234   if ( !desktop() || !desktop()->menuMgr() )
235     return -1;
236
237   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
238 }
239
240 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
241 {
242   if ( !a || !desktop() || !desktop()->menuMgr() )
243     return -1;
244
245   int regId = registerAction( id, a );
246   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
247   return intId != -1 ? regId : -1;
248 }
249
250 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
251 {
252   if ( !a || !desktop() || !desktop()->menuMgr() )
253     return -1;
254
255   int regId = registerAction( id, a );
256   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
257   return intId != -1 ? regId : -1;
258 }
259
260 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
261 {
262   if ( !desktop() || !desktop()->menuMgr() )
263     return -1;
264
265   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
266   return intId != -1 ? id : -1;
267 }
268
269 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
270 {
271   if ( !desktop() || !desktop()->menuMgr() )
272     return -1;
273
274   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
275   return intId != -1 ? id : -1;
276 }
277
278 void SUIT_Application::setMenuShown( QAction* a, const bool on )
279 {
280   setMenuShown( actionId( a ), on );
281 }
282
283 void SUIT_Application::setMenuShown( const int id, const bool on )
284 {
285   if ( desktop() && desktop()->menuMgr() )
286     desktop()->menuMgr()->setShown( id, on );
287 }
288
289 void SUIT_Application::setToolShown( QAction* a, const bool on )
290 {
291   setToolShown( actionId( a ), on );
292 }
293
294 void SUIT_Application::setToolShown( const int id, const bool on )
295 {
296   if ( desktop() && desktop()->toolMgr() )
297     desktop()->toolMgr()->setShown( id, on );
298 }
299
300 void SUIT_Application::setActionShown( QAction* a, const bool on )
301 {
302   setMenuShown( a, on );
303   setToolShown( a, on );
304 }
305
306 void SUIT_Application::setActionShown( const int id, const bool on )
307 {
308   setMenuShown( id, on );
309   setToolShown( id, on );
310 }
311
312 QAction* SUIT_Application::action( const int id ) const
313 {
314   QAction* a = 0;
315   if ( myActionMap.contains( id ) )
316     a = myActionMap[id];
317   return a;
318 }
319
320 int SUIT_Application::actionId( const QAction* a ) const
321 {
322   int id = -1;
323   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); 
324         it != myActionMap.end() && id == -1;
325         ++it ) {
326     if ( it.data() == a )
327       id = it.key();
328   }
329   return id;
330 }
331
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 )
335 {
336   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
337   a->setStatusTip( tip );
338
339   if ( reciever && member )
340     connect( a, SIGNAL( activated() ), reciever, member );
341
342   registerAction( id, a );
343
344   return a;
345 }
346
347 int SUIT_Application::registerAction( const int id, QAction* a )
348 {
349   int ident = actionId( a );
350   if ( ident != -1 )
351     return ident;
352
353   static int generatedId = -1;
354   ident = id == -1 ? --generatedId : id;
355
356   if ( action( ident ) ) 
357     qWarning( "Action registration id is already in use: %d", ident );
358
359   myActionMap.insert( ident, a );
360
361   if ( desktop() && desktop()->menuMgr() )
362     desktop()->menuMgr()->registerAction( a );
363
364   if ( desktop() && desktop()->toolMgr() )
365     desktop()->toolMgr()->registerAction( a );
366
367   return ident;
368 }
369
370 QAction* SUIT_Application::separator()
371 {
372   return QtxActionMgr::separator();
373 }
374
375 void SUIT_Application::onDesktopActivated()
376 {
377   emit activated( this );
378 }