Salome HOME
Copyrights update
[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     //desktop()->statusBar()->message( msg, msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec );
125     if ( !myStatusLabel ) {
126       myStatusLabel = new QLabel (desktop()->statusBar());
127       desktop()->statusBar()->addWidget(myStatusLabel, /*int stretch = */1);
128     }
129     myStatusLabel->setText(msg);
130     if( msec != -1 )
131       QTimer::singleShot(msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, myStatusLabel, SLOT(clear()));
132   }
133 }
134
135 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
136 {
137   return startApplication( name(), argc, argv );
138 }
139
140 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
141 {
142   SUIT_Session* session = SUIT_Session::session();
143   if ( !session )
144     return 0;
145
146   return session->startApplication( name, argc, argv );
147 }
148
149 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
150 {
151   if ( myDesktop == desk )
152     return;
153
154   delete myDesktop;
155   myDesktop = desk;
156   if ( myDesktop )
157     connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
158 }
159
160 SUIT_Study* SUIT_Application::createNewStudy()
161 {
162   return new SUIT_Study( this );
163 }
164
165 void SUIT_Application::setActiveStudy( SUIT_Study* study )
166 {
167   if ( myStudy == study )
168     return;
169
170   myStudy = study;
171 }
172
173 int SUIT_Application::createTool( const QString& name )
174 {
175   if ( !desktop() || !desktop()->toolMgr() )
176     return -1;
177
178   return desktop()->toolMgr()->createToolBar( name );
179 }
180
181 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
182 {
183   if ( !desktop() || !desktop()->toolMgr() )
184     return -1;
185
186   int regId = desktop()->toolMgr()->registerAction( a, id );
187   return desktop()->toolMgr()->insert( regId, tBar, idx );
188 }
189
190 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
191 {
192   if ( !desktop() || !desktop()->toolMgr() )
193     return -1;
194
195   return desktop()->toolMgr()->insert( a, tBar, idx );
196 }
197
198 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
199 {
200   if ( !desktop() || !desktop()->toolMgr() )
201     return -1;
202
203   return desktop()->toolMgr()->insert( id, tBar, idx );
204 }
205
206 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
207 {
208   if ( !desktop() || !desktop()->toolMgr() )
209     return -1;
210
211   return desktop()->toolMgr()->insert( id, tBar, idx );
212 }
213
214 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
215                                   const int id, const int group, const int index )
216 {
217   if ( !desktop() || !desktop()->menuMgr() )
218     return -1;
219
220   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
221 }
222
223 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
224                                   const int id, const int group, const int index )
225 {
226   if ( !desktop() || !desktop()->menuMgr() )
227     return -1;
228
229   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
230 }
231
232 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
233 {
234   if ( !a || !desktop() || !desktop()->menuMgr() )
235     return -1;
236
237   int regId = desktop()->menuMgr()->registerAction( a, id );
238   return desktop()->menuMgr()->insert( regId, menu, group, index );
239 }
240
241 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
242 {
243   if ( !a || !desktop() || !desktop()->menuMgr() )
244     return -1;
245
246   int regId = desktop()->menuMgr()->registerAction( a, id );
247   return desktop()->menuMgr()->insert( regId, menu, group, index );
248 }
249
250 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
251 {
252   if ( !desktop() || !desktop()->menuMgr() )
253     return -1;
254
255   return desktop()->menuMgr()->insert( id, menu, group, index );
256 }
257
258 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
259 {
260   if ( !desktop() || !desktop()->menuMgr() )
261     return -1;
262
263   return desktop()->menuMgr()->insert( id, menu, group, index );
264 }
265
266 void SUIT_Application::setMenuShown( QAction* a, const bool on )
267 {
268   setMenuShown( actionId( a ), on );
269 }
270
271 void SUIT_Application::setMenuShown( const int id, const bool on )
272 {
273   if ( desktop() && desktop()->menuMgr() )
274     desktop()->menuMgr()->setShown( id, on );
275 }
276
277 void SUIT_Application::setToolShown( QAction* a, const bool on )
278 {
279   setToolShown( actionId( a ), on );
280 }
281
282 void SUIT_Application::setToolShown( const int id, const bool on )
283 {
284   if ( desktop() && desktop()->toolMgr() )
285     desktop()->toolMgr()->setShown( id, on );
286 }
287
288 void SUIT_Application::setActionShown( QAction* a, const bool on )
289 {
290   setMenuShown( a, on );
291   setToolShown( a, on );
292 }
293
294 void SUIT_Application::setActionShown( const int id, const bool on )
295 {
296   setMenuShown( id, on );
297   setToolShown( id, on );
298 }
299
300 QAction* SUIT_Application::action( const int id ) const
301 {
302   SUIT_Application* that = (SUIT_Application*)this;
303   SUIT_Desktop* desk = that->desktop();
304   if ( !desk )
305     return 0;
306
307   QAction* a = 0;
308   if ( desk->menuMgr() )
309     a = desk->menuMgr()->action( id );
310   if ( !a && desk->toolMgr() )
311     a = desk->toolMgr()->action( id );
312   return a;
313 }
314
315 int SUIT_Application::actionId( const QAction* a ) const
316 {
317   SUIT_Application* that = (SUIT_Application*)this;
318   SUIT_Desktop* desk = that->desktop();
319   if ( !desk )
320     return 0;
321
322   int id = -1;
323   if ( desk->menuMgr() )
324     id = desk->menuMgr()->actionId( a );
325   if ( id == -1 && desk->toolMgr() )
326     id = desk->toolMgr()->actionId( a );
327   return id;
328 }
329
330 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIconSet& icon,
331                                          const QString& menu, const QString& tip, const int key,
332                                          QObject* parent, const bool toggle, QObject* reciever, const char* member )
333 {
334   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
335   a->setStatusTip( tip );
336
337   if ( reciever && member )
338     connect( a, SIGNAL( activated() ), reciever, member );
339
340   registerAction( id, a );
341
342   return a;
343 }
344
345 void SUIT_Application::registerAction( const int id, QAction* a )
346 {
347   if ( desktop() && desktop()->menuMgr() )
348     desktop()->menuMgr()->registerAction( a, id );
349
350   if ( desktop() && desktop()->toolMgr() )
351     desktop()->toolMgr()->registerAction( a, id );
352 }
353
354 QAction* SUIT_Application::separator()
355 {
356   return QtxActionMgr::separator();
357 }
358
359 void SUIT_Application::onDesktopActivated()
360 {
361   emit activated( this );
362 }