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