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