1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "CAF_Application.h"
24 #include "CAF_Tools.h"
25 #include "CAF_Study.h"
27 #include <SUIT_Desktop.h>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_ResourceMgr.h>
31 #include <QtxAction.h>
32 #include <QtxListAction.h>
35 #include <QStringList>
37 #include <Resource_Manager.hxx>
38 #include <TColStd_SequenceOfExtendedString.hxx>
41 \brief Create new instance of CAF_Application.
42 \return new instance of CAF_Application class
44 extern "C" CAF_EXPORT SUIT_Application* createApplication()
46 return new CAF_Application();
50 \class CAF_Application
51 \brief OCC OCAF-based application.
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).
59 \brief Default constructor.
61 CAF_Application::CAF_Application()
68 \param app OCAF application
70 CAF_Application::CAF_Application( const Handle( TDocStd_Application )& app )
79 CAF_Application::~CAF_Application()
84 \brief Get application name.
85 \return application name
87 QString CAF_Application::applicationName() const
89 return QString( "CAFApplication" );
93 \brief Get OCAF application.
94 \return handle to OCAF application object
96 Handle( TDocStd_Application ) CAF_Application::stdApp() const
102 \brief Get file extension filter.
104 The file extension filter is used in Open/Save dialog boxes.
106 \return file filters for open/save document dialog box
108 QString CAF_Application::getFileFilter() const
110 if ( stdApp().IsNull() )
113 TColStd_SequenceOfExtendedString formats;
114 stdApp()->Formats( formats );
117 QMap<QString, QStringList> wildCards;
118 Handle(Resource_Manager) resMgr = new Resource_Manager( stdApp()->ResourcesName() );
119 for ( int i = 1; i <= formats.Length(); i++ )
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() ) );
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() ) );
131 if ( !descr.isEmpty() && !extension.isEmpty() )
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 ) );
140 if ( wildCards.isEmpty() )
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( "; " ) ) );
147 if ( wildCards.count() > 1 )
148 filters.prepend( QString( "%1 (%2)" ).arg( tr( "INF_ALL_DOCUMENTS_FILTER" ) ).arg( allWC.join( "; " ) ) );
150 if ( !filters.isEmpty() )
151 filters.append( tr( "INF_ALL_FILTER" ) );
153 return filters.join( ";;" );
157 \brief Create menu and toolbars actions.
159 void CAF_Application::createActions()
161 STD_Application::createActions();
163 SUIT_Desktop* desk = desktop();
164 SUIT_ResourceMgr* resMgr = resourceMgr();
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 );
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 );
180 editUndo->setComment( tr( "INF_APP_UNDOACTIONS" ) );
181 editRedo->setComment( tr( "INF_APP_REDOACTIONS" ) );
183 connect( editUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
184 connect( editRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
186 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
188 createMenu( EditUndoId, editMenu, 0 );
189 createMenu( EditRedoId, editMenu, 0 );
190 createMenu( separator(), editMenu, -1, 0 );
192 int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
194 createTool( separator(), stdTBar );
195 createTool( EditUndoId, stdTBar );
196 createTool( EditRedoId, stdTBar );
197 createTool( separator(), stdTBar );
201 \brief Undo latest command operation for specified document.
202 \param doc OCAF document
203 \return \c true on success
205 bool CAF_Application::undo( CAF_Study* doc )
207 bool success = false;
210 if ( success = doc->undo() )
217 \brief Redo latest command operation undo for specified document.
218 \param doc OCAF document
219 \return \c true on success
221 bool CAF_Application::redo(CAF_Study* doc)
223 bool success = false;
226 if ( success = doc->redo() )
233 \brief Called when user activates "Undo" menu action.
235 Undo operation on the active document.
237 \param numActions undo depth (number of commands)
238 \return \c true on success
240 bool CAF_Application::onUndo( int numActions )
243 while ( numActions > 0 )
245 CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
248 if ( !undo( cafStudy ) )
256 updateCommandsStatus(); /* enable/disable undo/redo */
261 \brief Called when user activates "Redo" menu action.
263 Redo latest undo commands on the active document.
265 \param numActions redo depth (number of commands)
266 \return \c true on success
268 bool CAF_Application::onRedo( int numActions )
271 while ( numActions > 0 )
273 CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
276 if ( !redo( cafStudy ) )
284 updateCommandsStatus(); /* enable/disable undo/redo */
289 \brief Update actions state (Undo/Redo).
291 void CAF_Application::updateCommandsStatus()
293 STD_Application::updateCommandsStatus();
295 CAF_Study* cafStudy = 0;
296 if ( activeStudy() && activeStudy()->inherits( "CAF_Study" ) )
297 cafStudy = (CAF_Study*)activeStudy();
299 QtxListAction* undo = qobject_cast<QtxListAction*>( action( EditUndoId ) );
300 if ( cafStudy && undo )
301 undo->addNames( cafStudy->undoNames() );
303 QtxListAction* redo = qobject_cast<QtxListAction*>( action( EditRedoId ) );
304 if ( cafStudy && redo )
305 redo->addNames( cafStudy->redoNames() );
308 undo->setEnabled( cafStudy && cafStudy->canUndo() );
310 redo->setEnabled( cafStudy && cafStudy->canRedo() );
314 \brief Called when user activatees Help->About main menu command.
316 void CAF_Application::onHelpAbout()
318 SUIT_MessageBox::information( desktop(), tr( "About" ), tr( "ABOUT_INFO" ) );
322 \brief Create new empty study.
325 SUIT_Study* CAF_Application::createNewStudy()
327 return new CAF_Study( this );
331 \brief Set OCAF application.
332 \param app new OCAF application
334 void CAF_Application::setStdApp( const Handle(TDocStd_Application)& app )