Back to index

salome-gui  6.5.0
CAF_Application.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "CAF_Application.h"
00024 
00025 #include "CAF_Tools.h"
00026 #include "CAF_Study.h"
00027 
00028 #include <SUIT_Desktop.h>
00029 #include <SUIT_MessageBox.h>
00030 #include <SUIT_ResourceMgr.h>
00031 
00032 #include <QtxAction.h>
00033 #include <QtxListAction.h>
00034 
00035 #include <QMap>
00036 #include <QStringList>
00037 
00038 #include <Resource_Manager.hxx>
00039 #include <TColStd_SequenceOfExtendedString.hxx>
00040 
00045 extern "C" CAF_EXPORT SUIT_Application* createApplication()
00046 {
00047   return new CAF_Application();
00048 }
00049 
00062 CAF_Application::CAF_Application()
00063 : STD_Application()
00064 {
00065 }
00066 
00071 CAF_Application::CAF_Application( const Handle( TDocStd_Application )& app )
00072 : STD_Application(),
00073   myStdApp( app )
00074 {
00075 }
00076 
00080 CAF_Application::~CAF_Application()
00081 {
00082 }
00083 
00088 QString CAF_Application::applicationName() const
00089 {
00090   return QString( "CAFApplication" );
00091 }
00092 
00097 Handle( TDocStd_Application ) CAF_Application::stdApp() const
00098 {
00099   return myStdApp;
00100 }
00101 
00109 QString CAF_Application::getFileFilter() const
00110 {
00111   if ( stdApp().IsNull() )
00112     return QString();
00113 
00114   TColStd_SequenceOfExtendedString formats;
00115   stdApp()->Formats( formats );
00116 
00117   QStringList allWC;
00118   QMap<QString, QStringList> wildCards;
00119   Handle(Resource_Manager) resMgr = new Resource_Manager( stdApp()->ResourcesName() );
00120   for ( int i = 1; i <= formats.Length(); i++ )
00121   {
00122     QString extension;
00123     QString extResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".FileExtension" );
00124     if ( resMgr->Find( extResStr.toLatin1().data() ) )
00125       extension = QString( resMgr->Value( extResStr.toLatin1().data() ) );
00126 
00127     QString descr;
00128     QString descrResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".Description" );
00129     if ( resMgr->Find( (char*)descrResStr.toLatin1().data() ) )
00130       descr = QString( resMgr->Value( (char*)descrResStr.toLatin1().data() ) );
00131 
00132     if ( !descr.isEmpty() && !extension.isEmpty() )
00133     {
00134       if ( !wildCards.contains( descr ) )
00135         wildCards.insert( descr, QStringList() );
00136       wildCards[descr].append( QString( "*.%1" ).arg( extension ) );
00137       allWC.append( QString( "*.%1" ).arg( extension ) );
00138     }
00139   }
00140 
00141   if ( wildCards.isEmpty() )
00142     return QString();
00143 
00144   QStringList filters;
00145   for ( QMap<QString, QStringList>::ConstIterator it = wildCards.begin(); it != wildCards.end(); ++it )
00146     filters.append( QString( "%1 (%2)" ).arg( it.key() ).arg( it.value().join( "; " ) ) );
00147 
00148   if ( wildCards.count() > 1 )
00149     filters.prepend( QString( "%1 (%2)" ).arg( tr( "INF_ALL_DOCUMENTS_FILTER" ) ).arg( allWC.join( "; " ) ) );
00150 
00151   if ( !filters.isEmpty() )
00152     filters.append( tr( "INF_ALL_FILTER" ) );
00153 
00154   return filters.join( ";;" );
00155 }
00156 
00160 void CAF_Application::createActions()
00161 {
00162   STD_Application::createActions();
00163 
00164   SUIT_Desktop* desk = desktop();
00165   SUIT_ResourceMgr* resMgr = resourceMgr();
00166 
00167   QtxListAction* editUndo = 
00168     new QtxListAction( tr( "TOT_APP_EDIT_UNDO" ), 
00169                        resMgr->loadPixmap( "STD", tr( "ICON_EDIT_UNDO" ) ),
00170                        tr( "MEN_APP_EDIT_UNDO" ), Qt::CTRL+Qt::Key_Z, desk );
00171   editUndo->setStatusTip( tr( "PRP_APP_EDIT_UNDO" ) );
00172   registerAction( EditUndoId, editUndo );
00173 
00174   QtxListAction* editRedo =
00175     new QtxListAction( tr( "TOT_APP_EDIT_REDO" ), 
00176                        resMgr->loadPixmap( "STD", tr( "ICON_EDIT_REDO" ) ),
00177                        tr( "MEN_APP_EDIT_REDO" ), Qt::CTRL+Qt::Key_Y, desk );
00178   editRedo->setStatusTip( tr( "PRP_APP_EDIT_REDO" ) );
00179   registerAction( EditRedoId, editRedo );
00180 
00181   editUndo->setComment( tr( "INF_APP_UNDOACTIONS" ) );
00182   editRedo->setComment( tr( "INF_APP_REDOACTIONS" ) );
00183 
00184   connect( editUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
00185   connect( editRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
00186 
00187   int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
00188 
00189   createMenu( EditUndoId, editMenu, 0 );
00190   createMenu( EditRedoId, editMenu, 0 );
00191   createMenu( separator(), editMenu, -1, 0 );
00192 
00193   int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
00194 
00195   createTool( separator(), stdTBar );
00196   createTool( EditUndoId, stdTBar );
00197   createTool( EditRedoId, stdTBar );
00198   createTool( separator(), stdTBar );
00199 }
00200 
00206 bool CAF_Application::undo( CAF_Study* doc )
00207 {
00208   bool success = false;
00209   if ( doc )
00210   {
00211     success = doc->undo();
00212     if ( success )
00213       doc->update();
00214   }
00215   return success;
00216 }
00217 
00223 bool CAF_Application::redo(CAF_Study* doc)
00224 {
00225   bool success = false;
00226   if ( doc )
00227   {
00228     success = doc->redo();
00229     if ( success )
00230       doc->update();
00231   }
00232   return success;
00233 }
00234 
00243 bool CAF_Application::onUndo( int numActions )
00244 {
00245   bool ok = true;
00246   while ( numActions > 0 )
00247   {
00248     CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
00249     if ( cafStudy )
00250     {
00251       if ( !undo( cafStudy ) )
00252       {
00253         ok = false;
00254         break;
00255       }
00256       numActions--;
00257     }
00258   }
00259   updateCommandsStatus();     /* enable/disable undo/redo */
00260   return ok;
00261 }
00262 
00271 bool CAF_Application::onRedo( int numActions )
00272 {
00273   bool ok = true;
00274   while ( numActions > 0 )
00275   {
00276     CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
00277     if ( cafStudy )
00278     {
00279       if ( !redo( cafStudy ) )
00280       {
00281         ok = false;
00282         break;
00283       }
00284       numActions--;
00285     }
00286   }
00287   updateCommandsStatus();     /* enable/disable undo/redo */
00288   return ok;
00289 }
00290 
00294 void CAF_Application::updateCommandsStatus()
00295 {
00296   STD_Application::updateCommandsStatus();
00297 
00298   CAF_Study* cafStudy = 0;
00299   if ( activeStudy() && activeStudy()->inherits( "CAF_Study" ) )
00300     cafStudy = (CAF_Study*)activeStudy();
00301 
00302   QtxListAction* undo = qobject_cast<QtxListAction*>( action( EditUndoId ) );
00303   if ( cafStudy && undo )
00304     undo->addNames( cafStudy->undoNames() );
00305 
00306   QtxListAction* redo = qobject_cast<QtxListAction*>( action( EditRedoId ) );
00307   if ( cafStudy && redo )
00308     redo->addNames( cafStudy->redoNames() );
00309 
00310   if ( undo )
00311     undo->setEnabled( cafStudy && cafStudy->canUndo() );
00312   if ( redo )
00313     redo->setEnabled( cafStudy && cafStudy->canRedo() );
00314 }
00315 
00319 void CAF_Application::onHelpAbout()
00320 {
00321   SUIT_MessageBox::information( desktop(), tr( "About" ), tr( "ABOUT_INFO" ) );
00322 }
00323 
00328 SUIT_Study* CAF_Application::createNewStudy()
00329 {
00330   return new CAF_Study( this );
00331 }
00332 
00337 void CAF_Application::setStdApp( const Handle(TDocStd_Application)& app )
00338 {
00339   myStdApp = app;
00340 }