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