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