Salome HOME
PAL10870 - to improve export of Plot2d to PostScript
[modules/gui.git] / src / CAF / CAF_Study.cxx
index f35a76f0cb99de05193f343c8ad0c2372ec98819..bb20185cae9f7be97b9101280189f25b8be767c5 100755 (executable)
@@ -1,15 +1,38 @@
+// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either 
+// version 2.1 of the License.
+// 
+// This library is distributed in the hope that it will be useful 
+// but WITHOUT ANY WARRANTY; without even the implied warranty of 
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public  
+// License along with this library; if not, write to the Free Software 
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/
+//
 #include "CAF_Study.h"
 
 #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>
 
+#include <Standard_ErrorHandler.hxx>
+
 //////////////////////////////////////////////////////////////////////
 // Construction/Destruction
 //////////////////////////////////////////////////////////////////////
@@ -31,7 +54,96 @@ CAF_Study::~CAF_Study()
 {
 }
 
-bool CAF_Study::startOperation()
+Handle(TDocStd_Document) CAF_Study::stdDoc() const
+{
+  return myStdDoc;
+}
+
+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;
+
+  bool status = false;
+  try {
+    status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
+  }
+  catch ( Standard_Failure ) {
+    status = false;
+  }
+
+  return status && 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 );
+  }
+
+  bool status = false;
+  try {
+    if ( save )
+      status = app->Save( stdDoc() ) == CDF_SS_OK;
+    else
+    {
+      TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
+      app->Format( path, format );
+
+      if ( format.Length() )
+        stdDoc()->ChangeStorageFormat( format );
+
+      status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
+    }
+  }
+  catch ( Standard_Failure ) {
+    status = false;
+  }
+
+  return status && SUIT_Study::saveDocumentAs( fname );
+}
+
+bool CAF_Study::openTransaction()
 {
        if ( myStdDoc.IsNull() )
     return false;
@@ -43,47 +155,57 @@ bool CAF_Study::startOperation()
 
     myStdDoc->OpenCommand();
   }
-  catch( Standard_Failure ) {
+  catch ( Standard_Failure ) {
     res = false;
   }
 
   return res;
 }
 
-void CAF_Study::abortOperation()
+bool CAF_Study::abortTransaction()
 {
        if ( myStdDoc.IsNull() )
-    return;
+    return false;
 
+  bool res = true;
        try {
     myStdDoc->AbortCommand();
                update();
   }
   catch ( Standard_Failure ) {
+    res = false;
   }
+  return res;
 }
 
-void CAF_Study::commitOperation()
+bool CAF_Study::commitTransaction( const QString& name )
 {
        if ( myStdDoc.IsNull() )
-    return;
+    return false;
 
+  bool res = true;
        try {
     myStdDoc->CommitCommand();
 
     if ( canUndo() )
     {
-      CAF_Operation* cafOp = 0;
-      if ( activeOperation() && activeOperation()->inherits( "CAF_Operation" ) )
-        cafOp = (CAF_Operation*)activeOperation();
-
       Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
-                       if ( cafOp && !d.IsNull() )
-        d->SetName( CAF_Tools::toExtString( cafOp->getName() ) );
+                       if ( !d.IsNull() )
+        d->SetName( CAF_Tools::toExtString( name ) );
     }
   }
   catch ( Standard_Failure ) {
+    res = false;
   }
+  return res;
+}
+
+bool CAF_Study::hasTransaction() const
+{
+       if ( myStdDoc.IsNull() )
+    return false;
+
+  return myStdDoc->HasOpenCommand();
 }
 
 /*!
@@ -227,3 +349,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() );
+}