1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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
23 #include "CAF_Application.h"
25 #include "CAF_Tools.h"
26 #include "CAF_Study.h"
28 #include <SUIT_Desktop.h>
29 #include <SUIT_MessageBox.h>
30 #include <SUIT_ResourceMgr.h>
32 #include <QtxAction.h>
33 #include <QtxListAction.h>
36 #include <QStringList>
38 #include <Resource_Manager.hxx>
39 #include <TColStd_SequenceOfExtendedString.hxx>
42 \brief Create new instance of CAF_Application.
43 \return new instance of CAF_Application class
45 extern "C" CAF_EXPORT SUIT_Application* createApplication()
47 return new CAF_Application();
51 \class CAF_Application
52 \brief OCC OCAF-based application.
54 Defines application configuration and behaviour for application using
55 standard OCC OCAF data model. Allows using OCC OCAF serives
56 (for example, undo/redo mechanizm).
60 \brief Default constructor.
62 CAF_Application::CAF_Application()
69 \param app OCAF application
71 CAF_Application::CAF_Application( const Handle( TDocStd_Application )& app )
80 CAF_Application::~CAF_Application()
85 \brief Get application name.
86 \return application name
88 QString CAF_Application::applicationName() const
90 return QString( "CAFApplication" );
94 \brief Get OCAF application.
95 \return handle to OCAF application object
97 Handle( TDocStd_Application ) CAF_Application::stdApp() const
103 \brief Get file extension filter.
105 The file extension filter is used in Open/Save dialog boxes.
107 \return file filters for open/save document dialog box
109 QString CAF_Application::getFileFilter() const
111 if ( stdApp().IsNull() )
114 TColStd_SequenceOfExtendedString formats;
115 stdApp()->Formats( formats );
118 QMap<QString, QStringList> wildCards;
119 Handle(Resource_Manager) resMgr = new Resource_Manager( stdApp()->ResourcesName() );
120 for ( int i = 1; i <= formats.Length(); i++ )
123 QString extResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".FileExtension" );
124 if ( resMgr->Find( extResStr.toLatin1().data() ) )
125 extension = QString( resMgr->Value( extResStr.toLatin1().data() ) );
128 QString descrResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".Description" );
129 if ( resMgr->Find( (char*)descrResStr.toLatin1().data() ) )
130 descr = QString( resMgr->Value( (char*)descrResStr.toLatin1().data() ) );
132 if ( !descr.isEmpty() && !extension.isEmpty() )
134 if ( !wildCards.contains( descr ) )
135 wildCards.insert( descr, QStringList() );
136 wildCards[descr].append( QString( "*.%1" ).arg( extension ) );
137 allWC.append( QString( "*.%1" ).arg( extension ) );
141 if ( wildCards.isEmpty() )
145 for ( QMap<QString, QStringList>::ConstIterator it = wildCards.begin(); it != wildCards.end(); ++it )
146 filters.append( QString( "%1 (%2)" ).arg( it.key() ).arg( it.value().join( "; " ) ) );
148 if ( wildCards.count() > 1 )
149 filters.prepend( QString( "%1 (%2)" ).arg( tr( "INF_ALL_DOCUMENTS_FILTER" ) ).arg( allWC.join( "; " ) ) );
151 if ( !filters.isEmpty() )
152 filters.append( tr( "INF_ALL_FILTER" ) );
154 return filters.join( ";;" );
158 \brief Create menu and toolbars actions.
160 void CAF_Application::createActions()
162 STD_Application::createActions();
164 SUIT_Desktop* desk = desktop();
165 SUIT_ResourceMgr* resMgr = resourceMgr();
167 QtxListAction* editUndo =
168 new QtxListAction( tr( "TOT_APP_EDIT_UNDO" ),
169 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_UNDO" ) ),
170 tr( "MEN_APP_EDIT_UNDO" ), Qt::CTRL+Qt::Key_Z, desk );
171 editUndo->setStatusTip( tr( "PRP_APP_EDIT_UNDO" ) );
172 registerAction( EditUndoId, editUndo );
174 QtxListAction* editRedo =
175 new QtxListAction( tr( "TOT_APP_EDIT_REDO" ),
176 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_REDO" ) ),
177 tr( "MEN_APP_EDIT_REDO" ), Qt::CTRL+Qt::Key_Y, desk );
178 editRedo->setStatusTip( tr( "PRP_APP_EDIT_REDO" ) );
179 registerAction( EditRedoId, editRedo );
181 editUndo->setComment( tr( "INF_APP_UNDOACTIONS" ) );
182 editRedo->setComment( tr( "INF_APP_REDOACTIONS" ) );
184 connect( editUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
185 connect( editRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
187 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
189 createMenu( EditUndoId, editMenu, 0 );
190 createMenu( EditRedoId, editMenu, 0 );
191 createMenu( separator(), editMenu, -1, 0 );
193 int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ), // title (language-dependant)
194 QString( "SalomeStandard" ) ); // name (language-independant)
196 createTool( separator(), stdTBar );
197 createTool( EditUndoId, stdTBar );
198 createTool( EditRedoId, stdTBar );
199 createTool( separator(), stdTBar );
203 \brief Undo latest command operation for specified document.
204 \param doc OCAF document
205 \return \c true on success
207 bool CAF_Application::undo( CAF_Study* doc )
209 bool success = false;
212 success = doc->undo();
220 \brief Redo latest command operation undo for specified document.
221 \param doc OCAF document
222 \return \c true on success
224 bool CAF_Application::redo(CAF_Study* doc)
226 bool success = false;
229 success = doc->redo();
237 \brief Called when user activates "Undo" menu action.
239 Undo operation on the active document.
241 \param numActions undo depth (number of commands)
242 \return \c true on success
244 bool CAF_Application::onUndo( int numActions )
247 while ( numActions > 0 )
249 CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
252 if ( !undo( cafStudy ) )
260 updateCommandsStatus(); /* enable/disable undo/redo */
265 \brief Called when user activates "Redo" menu action.
267 Redo latest undo commands on the active document.
269 \param numActions redo depth (number of commands)
270 \return \c true on success
272 bool CAF_Application::onRedo( int numActions )
275 while ( numActions > 0 )
277 CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
280 if ( !redo( cafStudy ) )
288 updateCommandsStatus(); /* enable/disable undo/redo */
293 \brief Update actions state (Undo/Redo).
295 void CAF_Application::updateCommandsStatus()
297 STD_Application::updateCommandsStatus();
299 CAF_Study* cafStudy = 0;
300 if ( activeStudy() && activeStudy()->inherits( "CAF_Study" ) )
301 cafStudy = (CAF_Study*)activeStudy();
303 QtxListAction* undo = qobject_cast<QtxListAction*>( action( EditUndoId ) );
304 if ( cafStudy && undo )
305 undo->addNames( cafStudy->undoNames() );
307 QtxListAction* redo = qobject_cast<QtxListAction*>( action( EditRedoId ) );
308 if ( cafStudy && redo )
309 redo->addNames( cafStudy->redoNames() );
312 undo->setEnabled( cafStudy && cafStudy->canUndo() );
314 redo->setEnabled( cafStudy && cafStudy->canRedo() );
318 \brief Called when user activatees Help->About main menu command.
320 void CAF_Application::onHelpAbout()
322 SUIT_MessageBox::information( desktop(), tr( "About" ), tr( "ABOUT_INFO" ) );
326 \brief Create new empty study.
329 SUIT_Study* CAF_Application::createNewStudy()
331 return new CAF_Study( this );
335 \brief Set OCAF application.
336 \param app new OCAF application
338 void CAF_Application::setStdApp( const Handle(TDocStd_Application)& app )