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