From: vsv Date: Mon, 22 Oct 2018 10:05:47 +0000 (+0300) Subject: Use compressing for project files X-Git-Tag: V9_2_0a2~8 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=88d231a4a027ec8e2dc0dc91cb114b9b7ddf9688;p=modules%2Fshaper.git Use compressing for project files --- diff --git a/src/XGUI/CMakeLists.txt b/src/XGUI/CMakeLists.txt index 6c7d19fe4..502bd7d04 100644 --- a/src/XGUI/CMakeLists.txt +++ b/src/XGUI/CMakeLists.txt @@ -63,6 +63,7 @@ SET(PROJECT_HEADERS XGUI_Workshop.h XGUI_WorkshopListener.h XGUI_InspectionPanel.h + XGUI_CompressFiles.h ) SET(PROJECT_MOC_HEADERS @@ -130,6 +131,7 @@ SET(PROJECT_SOURCES XGUI_Workshop.cpp XGUI_WorkshopListener.cpp XGUI_InspectionPanel.cpp + XGUI_CompressFiles.cpp ) SET(PROJECT_RESOURCES diff --git a/src/XGUI/XGUI_CompressFiles.cpp b/src/XGUI/XGUI_CompressFiles.cpp new file mode 100644 index 000000000..49682b461 --- /dev/null +++ b/src/XGUI/XGUI_CompressFiles.cpp @@ -0,0 +1,97 @@ +// Copyright (C) 2014-2017 CEA/DEN, EDF 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, or (at your option) any later version. +// +// 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/ or +// email : webmaster.salome@opencascade.com +// + +#include "XGUI_CompressFiles.h" + +#include +#include +#include +#include + + + + +bool XGUI_CompressFiles::compress(const QString& theFile, std::list theFiles) +{ + QFile aOutFile(theFile); + if (aOutFile.open(QIODevice::WriteOnly)) { + QDataStream aOutStream(&aOutFile); + + std::list::const_iterator aIt; + for (aIt = theFiles.cbegin(); aIt != theFiles.cend(); ++aIt) { + QString aPathName((*aIt).c_str()); + QFile aSrcFile(aPathName); + if (aSrcFile.open(QIODevice::ReadOnly)) { + QFileInfo aInfo(aSrcFile); + quint64 aSize = aInfo.size(); + QString aName = + aPathName.right(aPathName.length() - aPathName.lastIndexOf(QDir::separator()) - 1); + + aOutStream << aName << aSize; + aOutStream << qCompress(aSrcFile.readAll()); + aSrcFile.close(); + } + else { + aOutFile.close(); + return false; + } + } + aOutFile.close(); + } + else + return false; + + return true; +} + + +bool XGUI_CompressFiles::uncompress(const QString& theFile, const QString& theDir) +{ + QFile aInFile(theFile); + QDir aDir(theDir); + if (!aDir.exists()) + aDir.mkpath(theDir); + if (!aDir.isEmpty()) { + if (!aDir.removeRecursively()) + return false; + aDir.mkdir(theDir); + } + + if (aInFile.open(QIODevice::ReadOnly)) { + QDataStream aInStream(&aInFile); + do { + QString aFileName; + quint64 aSize; + aInStream >> aFileName; + aInStream >> aSize; + + QFile aOutFile(theDir + QDir::separator() + aFileName); + if (aOutFile.open(QIODevice::WriteOnly)) { + QByteArray aContent; + aContent.resize(aSize); + aInStream >> aContent; + aOutFile.write(qUncompress(aContent)); + aOutFile.close(); + } + } while (!aInStream.atEnd()); + aInFile.close(); + } + return true; +} \ No newline at end of file diff --git a/src/XGUI/XGUI_CompressFiles.h b/src/XGUI/XGUI_CompressFiles.h new file mode 100644 index 000000000..e95b4ab04 --- /dev/null +++ b/src/XGUI/XGUI_CompressFiles.h @@ -0,0 +1,50 @@ +// Copyright (C) 2014-2017 CEA/DEN, EDF 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, or (at your option) any later version. +// +// 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/ or +// email : webmaster.salome@opencascade.com +// + +#ifndef XGUI_CompressFiles_H +#define XGUI_CompressFiles_H + +#include "XGUI.h" + +#include + +#include + +/** +* \ingroup GUI +* The class implements static methods for compressing/uncompressing files +*/ +class XGUI_CompressFiles +{ +public: + + /// Compress list of file to an archive + /// \param theFile a name of compressed output file + /// \param theFiles a list of input files to compress + static bool compress(const QString& theFile, std::list theFiles); + + /// Uncompress files + /// \param theFile a compressed file + /// \param theDir an output directory for uncompressed files + static bool uncompress(const QString& theFile, const QString& theDir); + +}; + +#endif \ No newline at end of file diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 313d66d1f..030a3b896 100755 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #ifndef HAVE_SALOME #include @@ -169,10 +170,13 @@ QString XGUI_Workshop::MOVE_TO_END_COMMAND = QObject::tr("Move to the end"); //#define DEBUG_FEATURE_NAME //#define DEBUG_CLEAN_HISTORY + +static QString MyFilter(QObject::tr("OpenParts files (*.opp)")); + + //****************************************************** XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector) : QObject(), - myCurrentDir(QString()), myModule(NULL), mySalomeConnector(theConnector), myPropertyPanel(0), @@ -285,6 +289,8 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector) //IMP: an attempt to use result selection with other selection modes myViewerSelMode.append(ModuleBase_ResultPrs::Sel_Result);//TopAbs_VERTEX); myViewerSelMode.append(TopAbs_COMPSOLID); + + qDebug("### Tmp: %s", qPrintable(myTmpDir.path())); } //****************************************************** @@ -299,6 +305,7 @@ XGUI_Workshop::~XGUI_Workshop(void) delete myDisplayer; delete myDataModelXMLReader; + clearTemporaryDir(); } //****************************************************** @@ -913,25 +920,26 @@ void XGUI_Workshop::onOpen() } else if (anAnswer == QMessageBox::Cancel) { return; } - myCurrentDir = ""; + myCurrentFile = QString(); } //show file dialog, check if readable and open - QString aDirectory = QFileDialog::getExistingDirectory(desktop(), tr("Select directory")); - openDirectory(aDirectory); + QString aFile = QFileDialog::getOpenFileName(desktop(), tr("Open file"), QString(), MyFilter); + if (!aFile.isNull()) + openFile(aFile); } //****************************************************** -void XGUI_Workshop::openDirectory(const QString& theDirectory) +void XGUI_Workshop::openFile(const QString& theDirectory) { - myCurrentDir = theDirectory; - if (myCurrentDir.isEmpty()) + myCurrentFile = theDirectory; + if (myCurrentFile.isEmpty()) return; - QFileInfo aFileInfo(myCurrentDir); + QFileInfo aFileInfo(myCurrentFile); if (!aFileInfo.exists() || !aFileInfo.isReadable()) { QMessageBox::critical(desktop(), tr("Warning"), tr("Unable to open the file.")); - myCurrentDir = ""; + myCurrentFile = QString(); return; } @@ -939,7 +947,11 @@ void XGUI_Workshop::openDirectory(const QString& theDirectory) module()->closeDocument(); SessionPtr aSession = ModelAPI_Session::get(); aSession->closeAll(); - aSession->load(myCurrentDir.toLatin1().constData()); + + clearTemporaryDir(); + XGUI_CompressFiles::uncompress(myCurrentFile, myTmpDir.path()); + + aSession->load(myTmpDir.path().toLatin1().constData()); myObjectBrowser->rebuildDataTree(); // Open first level of data tree @@ -950,7 +962,7 @@ void XGUI_Workshop::openDirectory(const QString& theDirectory) updateCommandStatus(); #ifndef HAVE_SALOME - myMainWindow->setCurrentDir(myCurrentDir, true); + myMainWindow->setCurrentDir(myCurrentFile, true); #endif #ifdef _DEBUG @@ -1046,7 +1058,7 @@ bool XGUI_Workshop::onSave() { if(!myOperationMgr->abortAllOperations(XGUI_OperationMgr::XGUI_InformationMessage)) return false; - if (myCurrentDir.isEmpty()) { + if (myCurrentFile.isEmpty()) { return onSaveAs(); } SessionPtr aMgr = ModelAPI_Session::get(); @@ -1054,7 +1066,9 @@ bool XGUI_Workshop::onSave() aMgr->blockAutoUpdate(false); std::list aFiles; - saveDocument(myCurrentDir, aFiles); + saveDocument(myTmpDir.path(), aFiles); + XGUI_CompressFiles::compress(myCurrentFile, aFiles); + updateCommandStatus(); #ifndef HAVE_SALOME myMainWindow->setModifiedState(false); @@ -1067,35 +1081,13 @@ bool XGUI_Workshop::onSaveAs() { if(!myOperationMgr->abortAllOperations(XGUI_OperationMgr::XGUI_InformationMessage)) return false; - QFileDialog dialog(desktop()); - dialog.setWindowTitle(tr("Select directory to save files...")); - dialog.setFileMode(QFileDialog::Directory); - dialog.setFilter(QDir::AllDirs); - dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly); - dialog.setViewMode(QFileDialog::Detail); - - if (!dialog.exec()) { - return false; - } - - QString aTempDir = dialog.selectedFiles().first(); - QDir aDir(aTempDir); - if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) { - int answer = QMessageBox::question( - desktop(), - // Title of the dialog which asks user if he wants to save study - // in existing non-empty folder - tr("Save"), - tr("The directory already contains some files, save anyway?"), - QMessageBox::Save | QMessageBox::Cancel); - if (answer == QMessageBox::Cancel) { - return false; - } - } - myCurrentDir = aTempDir; #ifndef HAVE_SALOME - myMainWindow->setCurrentDir(myCurrentDir, false); + myCurrentFile = QFileDialog::getSaveFileName(desktop(), tr("Select name to save file..."), + QString(), MyFilter); + if (!myCurrentFile.isNull()) { + myMainWindow->setCurrentDir(myCurrentFile, false); myMainWindow->setModifiedState(false); + } #endif return onSave(); } @@ -2855,3 +2847,16 @@ void XGUI_Workshop::updateAutoComputeState() QIcon(":pictures/autoapply_start.png")); #endif } + + +void XGUI_Workshop::clearTemporaryDir() +{ + QDir aDir(myTmpDir.path()); + if (!aDir.isEmpty()) { + QStringList aEntries; + aDir.entryList(aEntries); + foreach(QString aFile, aEntries) { + aDir.remove(aFile); + } + } +} \ No newline at end of file diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index f9504dfee..2955efab2 100755 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -36,6 +36,7 @@ #include #include #include +#include #ifndef HAVE_SALOME class AppElements_Command; @@ -233,11 +234,11 @@ Q_OBJECT /// Returns current module ModuleBase_IModule* module() const { return myModule; } - /// Returns current directory which contains data files - QString currentDataDir() const { return myCurrentDir; } + /// Returns current file + QString currentDataFile() const { return myCurrentFile; } - /// Returns current directory which contains data files - void setCurrentDataDir(const QString& theDir) { myCurrentDir = theDir; } + /// Returns current file + void setCurrentDataFile(const QString& theDir) { myCurrentFile = theDir; } /// Save the current document into a directory /// \param theName - path to the directory @@ -318,7 +319,7 @@ Q_OBJECT /// Closes all in the current session and load the directory /// \param theDirectory a path to directory - void openDirectory(const QString& theDirectory); + void openFile(const QString& theDirectory); void updateAutoComputeState(); @@ -510,6 +511,9 @@ private: /// \param theTimes number of applies the given action void processUndoRedo(const ModuleBase_ActionType theActionType, int theTimes); + /// Clear content of temporary directory + void clearTemporaryDir(); + private: #ifndef HAVE_SALOME AppElements_MainWindow* myMainWindow; ///< desktop window @@ -533,10 +537,11 @@ private: XGUI_ContextMenuMgr* myContextMenuMgr; ///< manager of context menu build XGUI_ModuleConnector* myModuleConnector; ///< implementation of ModuleBase_IWorkshop XGUI_WorkshopListener* myEventsListener; ///< processing of events - QString myCurrentDir; ///< cached the last open directory + QString myCurrentFile; ///< cached the last open directory QIntList myViewerSelMode; ///< selection modes set in the viewer Config_DataModelReader* myDataModelXMLReader; ///< XML reader of data model XGUI_InspectionPanel* myInspectionPanel; ///< container of feature attributes widgets + QTemporaryDir myTmpDir; ///< a direcory for uncompressed files }; #endif