Salome HOME
no message
authorstv <stv@opencascade.com>
Fri, 11 Nov 2005 14:31:25 +0000 (14:31 +0000)
committerstv <stv@opencascade.com>
Fri, 11 Nov 2005 14:31:25 +0000 (14:31 +0000)
src/CAF/CAF_Application.cxx
src/CAF/CAF_Application.h
src/CAF/CAF_Study.cxx
src/CAF/CAF_Study.h
src/CAF/resources/CAF_msg_en.po
src/STD/STD_Application.cxx

index db05644cf941d3f2cf7679d50c34c97c7d6e3603..4952410410ea5184d1e3cbc9792bfbb3e2e739b6 100755 (executable)
@@ -1,15 +1,16 @@
 #include "CAF_Application.h"
 
+#include "CAF_Tools.h"
 #include "CAF_Study.h"
 
-#include "SUIT_Desktop.h"
-#include "SUIT_Session.h"
-#include "SUIT_ViewModel.h"
-#include "SUIT_Operation.h"
-#include "SUIT_MessageBox.h"
-#include "SUIT_ResourceMgr.h"
+#include <SUIT_Desktop.h>
+#include <SUIT_Session.h>
+#include <SUIT_ViewModel.h>
+#include <SUIT_Operation.h>
+#include <SUIT_MessageBox.h>
+#include <SUIT_ResourceMgr.h>
 
-#include "QtxListAction.h"
+#include <QtxListAction.h>
 
 #include <qtoolbar.h>
 #include <qmenubar.h>
 #include <qstatusbar.h>
 #include <qapplication.h>
 
+#include <Resource_Manager.hxx>
+
+#include <TColStd_SequenceOfExtendedString.hxx>
+
 extern "C" CAF_EXPORT SUIT_Application* createApplication()
 {
   return new CAF_Application();
@@ -27,6 +32,12 @@ CAF_Application::CAF_Application()
 {
 }
 
+CAF_Application::CAF_Application( const Handle(TDocStd_Application)& app )
+: STD_Application(),
+myStdApp( app )
+{
+}
+
 CAF_Application::~CAF_Application()
 {
 }
@@ -36,6 +47,64 @@ QString CAF_Application::applicationName() const
   return QString( "CAFApplication" );
 }
 
+QString CAF_Application::storageFormat() const
+{
+  return QString( "MDTV-Standard" );
+}
+
+Handle(TDocStd_Application) CAF_Application::stdApp() const
+{
+  return myStdApp;
+}
+
+QString CAF_Application::getFileFilter() const
+{
+  if ( stdApp().IsNull() )
+    return QString::null;
+
+  TColStd_SequenceOfExtendedString formats;
+  stdApp()->Formats( formats );
+
+  QStringList allWC;
+  QMap<QString, QStringList> wildCards;
+  Handle(Resource_Manager) resMgr = new Resource_Manager( stdApp()->ResourcesName() );
+  for ( int i = 1; i <= formats.Length(); i++ )
+  {
+    QString extension;
+    QString extResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".FileExtension" );
+    if ( resMgr->Find( (char*)extResStr.latin1() ) )
+      extension = QString( resMgr->Value( (char*)extResStr.latin1() ) );
+
+    QString descr;
+    QString descrResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".Description" );
+    if ( resMgr->Find( (char*)descrResStr.latin1() ) )
+      descr = QString( resMgr->Value( (char*)descrResStr.latin1() ) );
+
+    if ( !descr.isEmpty() && !extension.isEmpty() )
+    {
+      if ( !wildCards.contains( descr ) )
+        wildCards.insert( descr, QStringList() );
+      wildCards[descr].append( QString( "*.%1" ).arg( extension ) );
+      allWC.append( QString( "*.%1" ).arg( extension ) );
+    }
+  }
+
+  if ( wildCards.isEmpty() )
+    return QString::null;
+
+  QStringList filters;
+  for ( QMap<QString, QStringList>::ConstIterator it = wildCards.begin(); it != wildCards.end(); ++it )
+    filters.append( QString( "%1 (%2)" ).arg( it.key() ).arg( it.data().join( "; " ) ) );
+
+  if ( wildCards.count() > 1 )
+    filters.prepend( QString( "%1 (%2)" ).arg( tr( "INF_ALL_DOCUMENTS_FILTER" ) ).arg( allWC.join( "; " ) ) );
+
+  if ( !filters.isEmpty() )
+    filters.append( tr( "INF_ALL_FILTER" ) );
+
+  return filters.join( ";;" );
+}
+
 void CAF_Application::createActions()
 {
   STD_Application::createActions();
@@ -182,3 +251,8 @@ SUIT_Study* CAF_Application::createNewStudy()
 {
   return new CAF_Study( this );
 }
+
+void CAF_Application::setStdApp( const Handle(TDocStd_Application)& app )
+{
+  myStdApp = app;
+}
index 8f73004598e8e55ac4a00dd3931ecda82d9c401f..9c377a8fae157f4d62f31985340d181f6c507488 100755 (executable)
@@ -8,6 +8,8 @@
 #include <qmap.h>
 #include <qptrlist.h>
 
+#include <TDocStd_Application.hxx>
+
 class QtxAction;
 class CAF_Study;
 
@@ -21,28 +23,40 @@ class CAF_EXPORT CAF_Application : public STD_Application
 
 public:
   CAF_Application();
+  CAF_Application( const Handle(TDocStd_Application)& );
   virtual ~CAF_Application();
 
-  virtual QString     applicationName() const;
+  virtual QString             applicationName() const;
+
+  Handle(TDocStd_Application) stdApp() const;
+
+  virtual QString             storageFormat() const;
+
+  virtual QString             getFileFilter() const;
 
 public slots:  
-  virtual void        onHelpAbout();
+  virtual void                onHelpAbout();
 
 protected slots:
-  virtual bool        onUndo( int );
-  virtual bool        onRedo( int );
+  virtual bool                onUndo( int );
+  virtual bool                onRedo( int );
   
 protected:
   enum {  EditUndoId = STD_Application::UserID, EditRedoId, UserID };
 
 protected:
-  virtual void        createActions();
-  virtual void        updateCommandsStatus();
+  virtual void                createActions();
+  virtual void                updateCommandsStatus();
+
+  virtual SUIT_Study*         createNewStudy();
+
+  bool                                         undo( CAF_Study* doc );
+  bool                                         redo( CAF_Study* doc );
 
-  virtual SUIT_Study* createNewStudy();
+  virtual void                setStdApp( const Handle(TDocStd_Application)& );
 
-  bool                                 undo( CAF_Study* doc );
-  bool                                 redo( CAF_Study* doc );
+private:
+  Handle(TDocStd_Application) myStdApp;
 };
 
 #if defined WIN32
index e9262e4951fe022f7d9716fe47c34e485e02f80f..178182ab5d9bf24377804e33a969fb00d9006aa5 100755 (executable)
@@ -2,11 +2,14 @@
 
 #include "CAF_Tools.h"
 #include "CAF_Operation.h"
+#include "CAF_Application.h"
 
 #include <SUIT_Desktop.h>
 #include <SUIT_MessageBox.h>
 #include <SUIT_Application.h>
 
+#include <qdir.h>
+
 #include <TDF_Delta.hxx>
 #include <TDF_ListIteratorOfDeltaList.hxx>
 
@@ -31,16 +34,93 @@ CAF_Study::~CAF_Study()
 {
 }
 
-Handle(TDocStd_Document) CAF_Study::stdDocument() const
+Handle(TDocStd_Document) CAF_Study::stdDoc() const
 {
   return myStdDoc;
 }
 
-void CAF_Study::setStdDocument( Handle(TDocStd_Document)& aStdDoc )
+void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
 {
   myStdDoc = aStdDoc;
 }
 
+void CAF_Study::createDocument()
+{
+  SUIT_Study::createDocument();
+
+  CAF_Application* app = cafApplication();
+  if ( app && !app->stdApp().IsNull() )
+  {
+    try {
+      TColStd_SequenceOfExtendedString formats;
+           app->stdApp()->Formats( formats );
+      if ( !formats.IsEmpty() )
+        app->stdApp()->NewDocument( formats.First(), myStdDoc );
+    }
+    catch ( Standard_Failure ) {
+    }
+  }
+}
+
+void CAF_Study::closeDocument( bool permanent )
+{
+  Handle(TDocStd_Application) app = stdApp();
+  if ( !app.IsNull() && !stdDoc().IsNull() )
+    app->Close( stdDoc() );
+
+  SUIT_Study::closeDocument( permanent );
+}
+
+bool CAF_Study::openDocument( const QString& fname )
+{
+  Handle(TDocStd_Application) app = stdApp();
+  if ( app.IsNull() )
+    return false;
+
+  try {
+    app->Open( CAF_Tools::toExtString( fname ), myStdDoc );
+  }
+  catch ( Standard_Failure ) {
+    return false;
+  }
+
+  return SUIT_Study::openDocument( fname );
+}
+
+bool CAF_Study::saveDocumentAs( const QString& fname )
+{
+  Handle(TDocStd_Application) app = stdApp();
+  if ( app.IsNull() )
+    return false;
+
+  bool save = false;
+  if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
+  {
+    QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
+    save = path == QDir::convertSeparators( fname );
+  }
+
+  try {
+    if ( save )
+      app->Save( stdDoc() );
+    else
+    {
+      TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
+      app->Format( path, format );
+
+      if ( format.Length() )
+        stdDoc()->ChangeStorageFormat( format );
+
+      app->SaveAs( stdDoc(), path );
+    }
+  }
+  catch ( Standard_Failure ) {
+    return false;
+  }
+
+  return SUIT_Study::saveDocumentAs( fname );
+}
+
 bool CAF_Study::startOperation()
 {
        if ( myStdDoc.IsNull() )
@@ -53,7 +133,7 @@ bool CAF_Study::startOperation()
 
     myStdDoc->OpenCommand();
   }
-  catch( Standard_Failure ) {
+  catch ( Standard_Failure ) {
     res = false;
   }
 
@@ -237,3 +317,23 @@ QStringList CAF_Study::redoNames() const
   }
   return names;
 }
+
+/*!
+    Returns the standard OCAF application from owner application. [ protected ]
+*/
+Handle(TDocStd_Application) CAF_Study::stdApp() const
+{
+  Handle(TDocStd_Application) stdApp;
+  CAF_Application* app = cafApplication();
+  if ( app )
+    stdApp = app->stdApp();
+  return stdApp;
+}
+
+/*!
+    Returns the application casted to type CAF_Application. [ protected ]
+*/
+CAF_Application* CAF_Study::cafApplication() const
+{
+  return ::qt_cast<CAF_Application*>( application() );
+}
index dc1f7fe5fdfa4ed7f53777ad7ea416bb99c73a01..88ae24cba98d72492bb2bd52bd85cf3aba657b52 100755 (executable)
@@ -6,9 +6,11 @@
 #include "SUIT_Study.h"
 
 #include <qobject.h>
-#include <qptrstack.h>
 
 #include <TDocStd_Document.hxx>
+#include <TDocStd_Application.hxx>
+
+class CAF_Application;
 
 #if defined WNT
 #pragma warning ( disable: 4251 )
@@ -23,29 +25,40 @@ public:
        CAF_Study( SUIT_Application* theApp, Handle(TDocStd_Document)& aStdDoc );
        virtual ~CAF_Study();
 
-       virtual bool             startOperation();
-       virtual void             abortOperation();
-       virtual void             commitOperation();
+  virtual void                createDocument();
+  virtual void                closeDocument( bool = true );
+  virtual bool                openDocument( const QString& );
+
+  virtual bool                saveDocumentAs( const QString& );
+
+       virtual bool                startOperation();
+       virtual void                abortOperation();
+       virtual void                commitOperation();
 
-       bool                     isSaved() const;
-       bool                     isModified() const;
-       void                     doModified( bool undoable = true);
-       void                     undoModified();
-       void                     clearModified();
+       bool                        isSaved() const;
+       bool                        isModified() const;
+       void                        doModified( bool = true );
+       void                        undoModified();
+       void                        clearModified();
 
-  bool                     undo();
-       bool                     redo();
-       bool                     canUndo() const;
-       bool                     canRedo() const;
-       QStringList              undoNames() const;
-       QStringList              redoNames() const;
+  bool                        undo();
+       bool                        redo();
+       bool                        canUndo() const;
+       bool                        canRedo() const;
+       QStringList                 undoNames() const;
+       QStringList                 redoNames() const;
 
-  Handle(TDocStd_Document) stdDocument() const;
-  void                     setStdDocument( Handle(TDocStd_Document)& );
+  Handle(TDocStd_Document)    stdDoc() const;
 
 protected:
-       Handle(TDocStd_Document) myStdDoc;
-       int                      myModifiedCnt;
+  Handle(TDocStd_Application) stdApp() const;
+  CAF_Application*            cafApplication() const;
+
+  virtual void                setStdDoc( Handle(TDocStd_Document)& );
+
+private:
+       Handle(TDocStd_Document)    myStdDoc;
+       int                         myModifiedCnt;
 
   friend class CAF_Operation;
 };
index fc7188c362a8782e8f24f1ab4e6019629067e15e..59119c2410415a24c5a9c083fe57e4f18eff4ad8 100755 (executable)
@@ -41,3 +41,9 @@ msgstr " Undoes %1 action(s) "
 
 msgid "CAF_Application::INF_APP_REDOACTIONS"
 msgstr " Redoes %1 action(s) "
+
+msgid "CAF_Application::INF_ALL_DOCUMENTS_FILTER"
+msgstr "All Readable Documents"
+
+msgid "CAF_Application::INF_ALL_FILTER"
+msgstr "All Files (*.*)"
index 2b5759931ddb0da045e8b6d424fc0c431f2d270a..20b8f0837d9942d58cd2725e3a3c1b20bd782789 100755 (executable)
@@ -699,6 +699,8 @@ void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextM
   delete popup;
 }
 
+#include <qregexp.h>
+
 /*!\retval QString - return file name from dialog.*/
 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters,
                                      const QString& caption, QWidget* parent )
@@ -719,42 +721,45 @@ QString STD_Application::getFileName( bool open, const QString& initial, const Q
     while ( !isOk )
     {
       // It is preferrable to use OS-specific file dialog box here !!!
-      aName = QFileDialog::getSaveFileName( anOldPath, filters, parent,
-                                           0, caption, &aUsedFilter);
+      aName = QFileDialog::getSaveFileName( anOldPath, filters, parent, 0, caption, &aUsedFilter );
 
       if ( aName.isNull() )
         isOk = true;
       else
       {
-        if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
+             int aEnd = aUsedFilter.findRev( ')' );
+             int aStart = aUsedFilter.findRev( '(', aEnd );
+             QString wcStr = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
+
+        int idx = 0;
+        QStringList extList;
+        QRegExp rx( "[\b\\*]*\\.([\\w]+)" );
+        while ( ( idx = rx.search( wcStr, idx ) ) != -1 )
         {
-         int aStart = aUsedFilter.find( '*' );
-         int aEnd = aUsedFilter.find( ')', aStart + 1 );
-         QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
-         if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
-         {
-           // Check that there is an extension at the end of the name
-           QString aNameTail = aName.right( aExt.length() );
-           if ( aNameTail != aExt )
-              aName += aExt;
-         }
+          extList.append( rx.cap( 1 ) );
+          idx += rx.matchedLength();
         }
-       if ( QFileInfo( aName ).exists() )
+
+        if ( !extList.isEmpty() && !extList.contains( QFileInfo( aName ).extension() ) )
+          aName += QString( ".%1" ).arg( extList.first() );
+
+             if ( QFileInfo( aName ).exists() )
         {
-         int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
-                                               tr( "MSG_FILE_EXISTS" ).arg( aName ),
-                                               tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
-         if ( aAnswer == 3 ) {     // cancelled
+               int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
+                                                                             tr( "MSG_FILE_EXISTS" ).arg( aName ),
+                                                                             tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
+               if ( aAnswer == 3 )
+          {     // cancelled
             aName = QString::null;
-           isOk = true;
+                 isOk = true;
           }
-         else if ( aAnswer == 2 ) // not save to this file
-           anOldPath = aName;             // not to return to the same initial dir at each "while" step
-         else                     // overwrite the existing file
-           isOk = true;
+               else if ( aAnswer == 2 ) // not save to this file
+                 anOldPath = aName;             // not to return to the same initial dir at each "while" step
+               else                     // overwrite the existing file
+                 isOk = true;
         }
-       else
-         isOk = true;
+             else
+               isOk = true;
       }
     }
     return aName;