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