DlgProjectUtility.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
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         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #include <sstream>
00026 #include <QDir>
00027 #include <QMessageBox>
00028 
00029 #include <App/Document.h>
00030 #include <App/PropertyStandard.h>
00031 
00032 #include "DlgProjectUtility.h"
00033 #include "Application.h"
00034 #include "ui_DlgProjectUtility.h"
00035 
00036 
00037 using namespace Gui::Dialog;
00038 
00039 // taken from the script doctools.py
00040 const char* doctools = 
00041 "import os,sys,string\n"
00042 "import xml.sax\n"
00043 "import xml.sax.handler\n"
00044 "import xml.sax.xmlreader\n"
00045 "import zipfile\n"
00046 "\n"
00047 "# SAX handler to parse the Document.xml\n"
00048 "class DocumentHandler(xml.sax.handler.ContentHandler):\n"
00049 "       def __init__(self, dirname):\n"
00050 "               self.files = []\n"
00051 "               self.dirname = dirname\n"
00052 "\n"
00053 "       def startElement(self, name, attributes):\n"
00054 "               item=attributes.get(\"file\")\n"
00055 "               if item != None:\n"
00056 "                       self.files.append(os.path.join(self.dirname,str(item)))\n"
00057 "\n"
00058 "       def characters(self, data):\n"
00059 "               return\n"
00060 "\n"
00061 "       def endElement(self, name):\n"
00062 "               return\n"
00063 "\n"
00064 "def extractDocument(filename, outpath):\n"
00065 "       zfile=zipfile.ZipFile(filename)\n"
00066 "       files=zfile.namelist()\n"
00067 "\n"
00068 "       for i in files:\n"
00069 "               data=zfile.read(i)\n"
00070 "               dirs=i.split(\"/\")\n"
00071 "               if len(dirs) > 1:\n"
00072 "                       dirs.pop()\n"
00073 "                       curpath=outpath\n"
00074 "                       for j in dirs:\n"
00075 "                               curpath=curpath+\"/\"+j\n"
00076 "                               os.mkdir(curpath)\n"
00077 "               output=open(outpath+\"/\"+i,\'wb\')\n"
00078 "               output.write(data)\n"
00079 "               output.close()\n"
00080 "\n"
00081 "def createDocument(filename, outpath):\n"
00082 "       files=getFilesList(filename)\n"
00083 "       compress=zipfile.ZipFile(outpath,\'w\',zipfile.ZIP_DEFLATED)\n"
00084 "       for i in files:\n"
00085 "               dirs=os.path.split(i)\n"
00086 "               #print i, dirs[-1]\n"
00087 "               compress.write(i,dirs[-1],zipfile.ZIP_DEFLATED)\n"
00088 "       compress.close()\n"
00089 "\n"
00090 "def getFilesList(filename):\n"
00091 "       dirname=os.path.dirname(filename)\n"
00092 "       handler=DocumentHandler(dirname)\n"
00093 "       parser=xml.sax.make_parser()\n"
00094 "       parser.setContentHandler(handler)\n"
00095 "       parser.parse(filename)\n"
00096 "\n"
00097 "       files=[]\n"
00098 "       files.append(filename)\n"
00099 "       files.extend(iter(handler.files))\n"
00100 "       dirname=os.path.join(dirname,\"GuiDocument.xml\")\n"
00101 "       if os.path.exists(dirname):\n"
00102 "               files.append(dirname)\n"
00103 "       return files\n"
00104 ;
00105 
00106 
00107 
00108 /* TRANSLATOR Gui::Dialog::DlgProjectUtility */
00109 
00110 DlgProjectUtility::DlgProjectUtility(QWidget* parent, Qt::WFlags fl)
00111   : QDialog(parent, fl), ui(new Ui_DlgProjectUtility)
00112 {
00113     ui->setupUi(this);
00114 }
00115 
00119 DlgProjectUtility::~DlgProjectUtility()
00120 {
00121   // no need to delete child widgets, Qt does it all for us
00122 }
00123 
00124 void DlgProjectUtility::on_extractButton_clicked()
00125 {
00126     QString source = ui->extractSource->fileName();
00127     QString dest = ui->extractDest->fileName();
00128     if (source.isEmpty()) {
00129         QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
00130         return;
00131     }
00132     if (dest.isEmpty()) {
00133         QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
00134         return;
00135     }
00136 
00137     std::stringstream str;
00138     str << doctools << "\n";
00139     str << "extractDocument(\"" << (const char*)source.toUtf8()
00140         << "\", \"" << (const char*)dest.toUtf8() << "\")";
00141     Application::Instance->runPythonCode(str.str().c_str());
00142 }
00143 
00144 void DlgProjectUtility::on_createButton_clicked()
00145 {
00146     QString source = ui->createSource->fileName();
00147     QString dest = ui->createDest->fileName();
00148     if (source.isEmpty()) {
00149         QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
00150         return;
00151     }
00152     if (dest.isEmpty()) {
00153         QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
00154         return;
00155     }
00156 
00157     dest = QDir(dest).absoluteFilePath(QString::fromUtf8("project.fcstd"));
00158 
00159     std::stringstream str;
00160     str << doctools << "\n";
00161     str << "createDocument(\"" << (const char*)source.toUtf8()
00162         << "\", \"" << (const char*)dest.toUtf8() << "\")";
00163     Application::Instance->runPythonCode(str.str().c_str());
00164 
00165     if (ui->checkLoadProject->isChecked())
00166         Application::Instance->open((const char*)dest.toUtf8(),"FreeCAD");
00167 }
00168 
00169 #include "moc_DlgProjectUtility.cpp"

Generated on Wed Nov 23 19:00:06 2011 for FreeCAD by  doxygen 1.6.1