/
DlgProjectUtility.cpp
172 lines (153 loc) · 5.89 KB
/
DlgProjectUtility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/***************************************************************************
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#include <sstream>
#include <QDir>
#include <QMessageBox>
#include <App/Document.h>
#include <App/PropertyStandard.h>
#include "DlgProjectUtility.h"
#include "Application.h"
#include "Command.h"
#include "ui_DlgProjectUtility.h"
using namespace Gui::Dialog;
// taken from the script doctools.py
std::string DlgProjectUtility::doctools =
"import os,sys,string\n"
"import xml.sax\n"
"import xml.sax.handler\n"
"import xml.sax.xmlreader\n"
"import zipfile\n"
"\n"
"# SAX handler to parse the Document.xml\n"
"class DocumentHandler(xml.sax.handler.ContentHandler):\n"
" def __init__(self, dirname):\n"
" self.files = []\n"
" self.dirname = dirname\n"
"\n"
" def startElement(self, name, attributes):\n"
" item=attributes.get(\"file\")\n"
" if item != None:\n"
" self.files.append(os.path.join(self.dirname,str(item)))\n"
"\n"
" def characters(self, data):\n"
" return\n"
"\n"
" def endElement(self, name):\n"
" return\n"
"\n"
"def extractDocument(filename, outpath):\n"
" zfile=zipfile.ZipFile(filename)\n"
" files=zfile.namelist()\n"
"\n"
" for i in files:\n"
" data=zfile.read(i)\n"
" dirs=i.split(\"/\")\n"
" if len(dirs) > 1:\n"
" dirs.pop()\n"
" curpath=outpath\n"
" for j in dirs:\n"
" curpath=curpath+\"/\"+j\n"
" os.mkdir(curpath)\n"
" output=open(outpath+\"/\"+i,\'wb\')\n"
" output.write(data)\n"
" output.close()\n"
"\n"
"def createDocument(filename, outpath):\n"
" files=getFilesList(filename)\n"
" dirname=os.path.dirname(filename)\n"
" guixml=os.path.join(dirname,\"GuiDocument.xml\")\n"
" if os.path.exists(guixml):\n"
" files.extend(getFilesList(guixml))\n"
" compress=zipfile.ZipFile(outpath,\'w\',zipfile.ZIP_DEFLATED)\n"
" for i in files:\n"
" dirs=os.path.split(i)\n"
" #print i, dirs[-1]\n"
" compress.write(i,dirs[-1],zipfile.ZIP_DEFLATED)\n"
" compress.close()\n"
"\n"
"def getFilesList(filename):\n"
" dirname=os.path.dirname(filename)\n"
" handler=DocumentHandler(dirname)\n"
" parser=xml.sax.make_parser()\n"
" parser.setContentHandler(handler)\n"
" parser.parse(filename)\n"
"\n"
" files=[]\n"
" files.append(filename)\n"
" files.extend(iter(handler.files))\n"
" return files\n"
;
/* TRANSLATOR Gui::Dialog::DlgProjectUtility */
DlgProjectUtility::DlgProjectUtility(QWidget* parent, Qt::WindowFlags fl)
: QDialog(parent, fl), ui(new Ui_DlgProjectUtility)
{
ui->setupUi(this);
ui->extractSource->setFilter(QString::fromLatin1("%1 (*.fcstd)").arg(tr("Project file")));
}
/**
* Destroys the object and frees any allocated resources
*/
DlgProjectUtility::~DlgProjectUtility()
{
// no need to delete child widgets, Qt does it all for us
}
void DlgProjectUtility::on_extractButton_clicked()
{
QString source = ui->extractSource->fileName();
QString dest = ui->extractDest->fileName();
if (source.isEmpty()) {
QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
return;
}
if (dest.isEmpty()) {
QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
return;
}
std::stringstream str;
str << doctools << "\n";
str << "extractDocument(\"" << (const char*)source.toUtf8()
<< "\", \"" << (const char*)dest.toUtf8() << "\")";
Gui::Command::runCommand(Gui::Command::App, str.str().c_str());
}
void DlgProjectUtility::on_createButton_clicked()
{
QString source = ui->createSource->fileName();
QString dest = ui->createDest->fileName();
if (source.isEmpty()) {
QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
return;
}
if (dest.isEmpty()) {
QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
return;
}
dest = QDir(dest).absoluteFilePath(QString::fromUtf8("project.fcstd"));
std::stringstream str;
str << doctools << "\n";
str << "createDocument(\"" << (const char*)source.toUtf8()
<< "\", \"" << (const char*)dest.toUtf8() << "\")";
Gui::Command::runCommand(Gui::Command::App, str.str().c_str());
if (ui->checkLoadProject->isChecked())
Application::Instance->open((const char*)dest.toUtf8(),"FreeCAD");
}
#include "moc_DlgProjectUtility.cpp"