Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / CAF / CAF_Application.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "CAF_Application.h"
20
21 #include "CAF_Tools.h"
22 #include "CAF_Study.h"
23
24 #include <SUIT_Desktop.h>
25 #include <SUIT_Session.h>
26 #include <SUIT_ViewModel.h>
27 #include <SUIT_Operation.h>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_ResourceMgr.h>
30
31 #include <QtxListAction.h>
32
33 #include <qtoolbar.h>
34 #include <qmenubar.h>
35 #include <qpopupmenu.h>
36 #include <qstatusbar.h>
37 #include <qapplication.h>
38
39 #include <Resource_Manager.hxx>
40
41 #include <TColStd_SequenceOfExtendedString.hxx>
42
43 extern "C" CAF_EXPORT SUIT_Application* createApplication()
44 {
45   return new CAF_Application();
46 }
47
48 /*!
49   Default constructor
50 */
51 CAF_Application::CAF_Application()
52 : STD_Application()
53 {
54 }
55
56 /*!
57   Constructor with OCAF application
58   \param app - OCAF application
59 */
60 CAF_Application::CAF_Application( const Handle( TDocStd_Application )& app )
61 : STD_Application(),
62 myStdApp( app )
63 {
64 }
65
66 /*!
67   Destructor
68 */
69 CAF_Application::~CAF_Application()
70 {
71 }
72
73 /*!
74   \return application name
75 */
76 QString CAF_Application::applicationName() const
77 {
78   return QString( "CAFApplication" );
79 }
80
81 /*!
82   \return OCAF application
83 */
84 Handle( TDocStd_Application ) CAF_Application::stdApp() const
85 {
86   return myStdApp;
87 }
88
89 /*!
90   \return file filters for open/save document
91 */
92 QString CAF_Application::getFileFilter() const
93 {
94   if ( stdApp().IsNull() )
95     return QString::null;
96
97   TColStd_SequenceOfExtendedString formats;
98   stdApp()->Formats( formats );
99
100   QStringList allWC;
101   QMap<QString, QStringList> wildCards;
102   Handle(Resource_Manager) resMgr = new Resource_Manager( stdApp()->ResourcesName() );
103   for ( int i = 1; i <= formats.Length(); i++ )
104   {
105     QString extension;
106     QString extResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".FileExtension" );
107     if ( resMgr->Find( (char*)extResStr.latin1() ) )
108       extension = QString( resMgr->Value( (char*)extResStr.latin1() ) );
109
110     QString descr;
111     QString descrResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".Description" );
112     if ( resMgr->Find( (char*)descrResStr.latin1() ) )
113       descr = QString( resMgr->Value( (char*)descrResStr.latin1() ) );
114
115     if ( !descr.isEmpty() && !extension.isEmpty() )
116     {
117       if ( !wildCards.contains( descr ) )
118         wildCards.insert( descr, QStringList() );
119       wildCards[descr].append( QString( "*.%1" ).arg( extension ) );
120       allWC.append( QString( "*.%1" ).arg( extension ) );
121     }
122   }
123
124   if ( wildCards.isEmpty() )
125     return QString::null;
126
127   QStringList filters;
128   for ( QMap<QString, QStringList>::ConstIterator it = wildCards.begin(); it != wildCards.end(); ++it )
129     filters.append( QString( "%1 (%2)" ).arg( it.key() ).arg( it.data().join( "; " ) ) );
130
131   if ( wildCards.count() > 1 )
132     filters.prepend( QString( "%1 (%2)" ).arg( tr( "INF_ALL_DOCUMENTS_FILTER" ) ).arg( allWC.join( "; " ) ) );
133
134   if ( !filters.isEmpty() )
135     filters.append( tr( "INF_ALL_FILTER" ) );
136
137   return filters.join( ";;" );
138 }
139
140 /*!
141   Creates actions of application
142 */
143 void CAF_Application::createActions()
144 {
145   STD_Application::createActions();
146
147   SUIT_Desktop* desk = desktop();
148   SUIT_ResourceMgr* resMgr = resourceMgr();
149
150   QtxListAction* editUndo =
151     new QtxListAction( tr( "TOT_APP_EDIT_UNDO" ), resMgr->loadPixmap( "CAF", tr( "ICON_APP_EDIT_UNDO" ) ),
152                                          tr( "MEN_APP_EDIT_UNDO" ), CTRL+Key_Z, desk );
153   registerAction( EditUndoId, editUndo );
154
155   QtxListAction* editRedo =
156     new QtxListAction( tr( "TOT_APP_EDIT_REDO" ), resMgr->loadPixmap( "CAF", tr( "ICON_APP_EDIT_REDO" ) ),
157                                          tr( "MEN_APP_EDIT_REDO" ), CTRL+Key_Y, desk );
158   registerAction( EditRedoId, editRedo );
159
160   editUndo->setComment( tr( "INF_APP_UNDOACTIONS" ) );
161   editRedo->setComment( tr( "INF_APP_REDOACTIONS" ) );
162
163   connect( editUndo, SIGNAL( activated( int ) ), this, SLOT( onUndo( int ) ) );
164   connect( editRedo, SIGNAL( activated( int ) ), this, SLOT( onRedo( int ) ) );
165
166
167   int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
168
169   createMenu( EditUndoId, editMenu, 0 );
170   createMenu( EditRedoId, editMenu, 0 );
171   createMenu( separator(), editMenu, -1, 0 );
172
173   int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
174
175   createTool( separator(), stdTBar );
176   createTool( EditUndoId, stdTBar );
177   createTool( EditRedoId, stdTBar );
178   createTool( separator(), stdTBar );
179 }
180
181 /*!
182     Undo operation on the given document. [ virtual protected ]
183 */
184 bool CAF_Application::undo( CAF_Study* doc )
185 {
186   bool success = false;
187   if ( doc )
188   {
189     if ( success = doc->undo() )
190       doc->update();
191   }
192   return success;
193 }
194
195 /*!
196     Redo operation on the given document. [ virtual protected ]
197 */
198 bool CAF_Application::redo(CAF_Study* doc)
199 {
200   bool success = false;
201   if ( doc )
202   {
203     if ( success = doc->redo() )
204       doc->update();
205   }
206   return success;
207 }
208
209 /*!
210     Undo operation on the active document. [ virtual protected slot ]
211 */
212 bool CAF_Application::onUndo( int numActions )
213 {
214   bool ok = true;
215   while ( numActions > 0 )
216   {
217           CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
218                 if ( cafStudy )
219     {
220             if ( !undo( cafStudy ) )
221                   {
222                           ok = false;
223                                 break;
224                         }
225                         numActions--;
226                 }
227   }
228   updateCommandsStatus();     /* enable/disable undo/redo */
229   return ok;
230 }
231
232 /*!
233     Redo operation on the active document. [ virtual protected slot ]
234 */
235 bool CAF_Application::onRedo( int numActions )
236 {
237   bool ok = true;
238   while ( numActions > 0 )
239   {
240           CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
241                 if ( cafStudy )
242     {
243                         if ( !redo( cafStudy ) )
244                         {
245               ok = false;
246                     break;
247                         }
248                         numActions--;
249                 }
250   }
251   updateCommandsStatus();     /* enable/disable undo/redo */
252   return ok;
253 }
254
255 /*!
256   Enables / disables the actions according to the application state. [ virtual protected ]
257 */
258 void CAF_Application::updateCommandsStatus()
259 {
260         STD_Application::updateCommandsStatus();
261
262   CAF_Study* cafStudy = 0;
263   if ( activeStudy() && activeStudy()->inherits( "CAF_Study" ) )
264     cafStudy = (CAF_Study*)activeStudy();
265
266   QAction* undo = action( EditUndoId );
267   if ( cafStudy && undo )
268     undo->setProperty( "names", cafStudy->undoNames() );
269
270   QAction* redo = action( EditRedoId );
271   if ( cafStudy && redo )
272     redo->setProperty( "names", cafStudy->redoNames() );
273
274   if ( undo )
275     undo->setEnabled( cafStudy && cafStudy->canUndo() );
276   if ( redo )
277     redo->setEnabled( cafStudy && cafStudy->canRedo() );
278 }
279
280 /*!
281   SLOT: called by clicking on Help->About in main menu
282 */
283 void CAF_Application::onHelpAbout()
284 {
285   SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
286 }
287
288 /*!
289   Creates new study
290 */
291 SUIT_Study* CAF_Application::createNewStudy()
292 {
293   return new CAF_Study( this );
294 }
295
296 /*!
297   Sets OCAF application
298 */
299 void CAF_Application::setStdApp( const Handle(TDocStd_Application)& app )
300 {
301   myStdApp = app;
302 }