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