Salome HOME
Fix for bug 10438: Crash during Explode on Blocks operation (Global selection on...
[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     if( msec != -1 )
103       QTimer::singleShot(msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, myStatusLabel, SLOT(clear()));
104   }
105 }
106
107 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
108 {
109   return startApplication( name(), argc, argv );
110 }
111
112 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
113 {
114   SUIT_Session* session = SUIT_Session::session();
115   if ( !session )
116     return 0;
117
118   return session->startApplication( name, argc, argv );
119 }
120
121 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
122 {
123   if ( myDesktop == desk )
124     return;
125
126   delete myDesktop;
127   myDesktop = desk;
128   if ( myDesktop )
129     connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
130 }
131
132 SUIT_Study* SUIT_Application::createNewStudy()
133 {
134   return new SUIT_Study( this );
135 }
136
137 void SUIT_Application::setActiveStudy( SUIT_Study* study )
138 {
139   if ( myStudy == study )
140     return;
141
142   myStudy = study;
143 }
144
145 int SUIT_Application::createTool( const QString& name )
146 {
147   if ( !desktop() || !desktop()->toolMgr() )
148     return -1;
149
150   return desktop()->toolMgr()->createToolBar( name );
151 }
152
153 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
154 {
155   if ( !desktop() || !desktop()->toolMgr() )
156     return -1;
157
158   int regId = desktop()->toolMgr()->registerAction( a, id );
159   return desktop()->toolMgr()->insert( regId, tBar, idx );
160 }
161
162 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
163 {
164   if ( !desktop() || !desktop()->toolMgr() )
165     return -1;
166
167   return desktop()->toolMgr()->insert( a, tBar, idx );
168 }
169
170 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
171 {
172   if ( !desktop() || !desktop()->toolMgr() )
173     return -1;
174
175   return desktop()->toolMgr()->insert( id, tBar, idx );
176 }
177
178 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
179 {
180   if ( !desktop() || !desktop()->toolMgr() )
181     return -1;
182
183   return desktop()->toolMgr()->insert( id, tBar, idx );
184 }
185
186 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
187                                   const int id, const int group, const int index )
188 {
189   if ( !desktop() || !desktop()->menuMgr() )
190     return -1;
191
192   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
193 }
194
195 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
196                                   const int id, const int group, const int index )
197 {
198   if ( !desktop() || !desktop()->menuMgr() )
199     return -1;
200
201   return desktop()->menuMgr()->insert( subMenu, menu, group, index );
202 }
203
204 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
205 {
206   if ( !a || !desktop() || !desktop()->menuMgr() )
207     return -1;
208
209   int regId = desktop()->menuMgr()->registerAction( a, id );
210   return desktop()->menuMgr()->insert( regId, menu, group, index );
211 }
212
213 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
214 {
215   if ( !a || !desktop() || !desktop()->menuMgr() )
216     return -1;
217
218   int regId = desktop()->menuMgr()->registerAction( a, id );
219   return desktop()->menuMgr()->insert( regId, menu, group, index );
220 }
221
222 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
223 {
224   if ( !desktop() || !desktop()->menuMgr() )
225     return -1;
226
227   return desktop()->menuMgr()->insert( id, menu, group, index );
228 }
229
230 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
231 {
232   if ( !desktop() || !desktop()->menuMgr() )
233     return -1;
234
235   return desktop()->menuMgr()->insert( id, menu, group, index );
236 }
237
238 void SUIT_Application::setMenuShown( QAction* a, const bool on )
239 {
240   setMenuShown( actionId( a ), on );
241 }
242
243 void SUIT_Application::setMenuShown( const int id, const bool on )
244 {
245   if ( desktop() && desktop()->menuMgr() )
246     desktop()->menuMgr()->setShown( id, on );
247 }
248
249 void SUIT_Application::setToolShown( QAction* a, const bool on )
250 {
251   setToolShown( actionId( a ), on );
252 }
253
254 void SUIT_Application::setToolShown( const int id, const bool on )
255 {
256   if ( desktop() && desktop()->toolMgr() )
257     desktop()->toolMgr()->setShown( id, on );
258 }
259
260 QAction* SUIT_Application::action( const int id ) const
261 {
262   SUIT_Application* that = (SUIT_Application*)this;
263   SUIT_Desktop* desk = that->desktop();
264   if ( !desk )
265     return 0;
266
267   QAction* a = 0;
268   if ( desk->menuMgr() )
269     a = desk->menuMgr()->action( id );
270   if ( !a && desk->toolMgr() )
271     a = desk->toolMgr()->action( id );
272   return a;
273 }
274
275 int SUIT_Application::actionId( const QAction* a ) const
276 {
277   SUIT_Application* that = (SUIT_Application*)this;
278   SUIT_Desktop* desk = that->desktop();
279   if ( !desk )
280     return 0;
281
282   int id = -1;
283   if ( desk->menuMgr() )
284     id = desk->menuMgr()->actionId( a );
285   if ( id == -1 && desk->toolMgr() )
286     id = desk->toolMgr()->actionId( a );
287   return id;
288 }
289
290 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIconSet& icon,
291                                          const QString& menu, const QString& tip, const int key,
292                                          QObject* parent, const bool toggle, QObject* reciever, const char* member )
293 {
294   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
295   a->setStatusTip( tip );
296
297   if ( reciever && member )
298     connect( a, SIGNAL( activated() ), reciever, member );
299
300   registerAction( id, a );
301
302   return a;
303 }
304
305 void SUIT_Application::registerAction( const int id, QAction* a )
306 {
307   if ( desktop() && desktop()->menuMgr() )
308     desktop()->menuMgr()->registerAction( a, id );
309
310   if ( desktop() && desktop()->toolMgr() )
311     desktop()->toolMgr()->registerAction( a, id );
312 }
313
314 QAction* SUIT_Application::separator()
315 {
316   return QtxActionMgr::separator();
317 }
318
319 void SUIT_Application::onDesktopActivated()
320 {
321   emit activated( this );
322 }