1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on Oct-2, 2012.
19 #include "dccontainersaver.h"
21 #include "dccontainer.h"
24 #include "dcvccellcode.h"
26 #include "dccellcode.h"
27 #include "dcreceptor.h"
29 #include "dcaxonterminal.h"
30 #include "dcvpagecomponent.h"
32 #include "utils/dcutil.h"
34 #include <QDomDocument>
39 DCContainerSaver::DCContainerSaver(DCContainer *container) : d_container(container)
44 DCContainerSaver::~DCContainerSaver()
49 bool DCContainerSaver::saveAll(const QString& containerRootPath)
51 const QMap<QString,DCVCPage*> *pages = d_container->getScene()->getPages();
52 QMapIterator<QString, DCVCPage*> i(*pages);
57 DCVCPage *page = i.value();
58 if (!saveForPage(containerRootPath, page))
66 bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *page)
68 QString pageFilePath = containerRootPath;
69 pageFilePath.append(page->getLocationPath());
71 QDomDocument doc("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");
72 doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
74 QDomElement root = doc.createElementNS("http://www.w3.org/1999/xhtml","html");
75 root.setAttribute("lang", "en");
76 QDomElement body = doc.createElement("body");
77 root.appendChild(body);
79 const QList<DCVPageComponent*>* vcells = page->getCells();
80 for (int i = 0; i < vcells->length(); i++)
82 DCCell *cell = vcells->at(i)->getOwnerCell();
83 QString cellName = QString::fromStdString(cell->getName());
85 QDomElement aCellTag = doc.createElement("a");
86 aCellTag.setAttribute("define", "cell");
87 aCellTag.setAttribute("name", cellName);
89 QDomElement aCellCodeTag = doc.createElement("a");
90 aCellCodeTag.setAttribute("parameter", "cellcode");
91 if (cell->getIsCellCodeClassAssgined())
93 aCellCodeTag.setAttribute("href", QString::fromStdString(cell->getCellCode()->getFQNName()));
97 aCellCodeTag.setAttribute("type", cell->getType());
99 aCellTag.appendChild(aCellCodeTag);
100 DCAxon *axon = cell->getAxon();
101 for (int j = 0; j < axon->getNumberOfTerminals(); j++)
103 DCAxonTerminal *terminal = axon->getTerminalAt(j);
104 DCReceptor *receptor = dynamic_cast<DCReceptor*>(terminal->getTarget());
107 DCCell *targetCell = dynamic_cast<DCCell*>(receptor->getOwnerCell());
110 QString connectionPath = QString::fromStdString(targetCell->getLocation());
111 connectionPath.append("#");
112 connectionPath.append(QString::fromStdString(targetCell->getName()));
113 QString receptorName = QString::fromStdString(targetCell->getReceptorName(receptor));
115 QDomElement aConnectionTag = doc.createElement("a");
116 aConnectionTag.setAttribute("parameter", "connection");
117 aConnectionTag.setAttribute("href", connectionPath);
118 aConnectionTag.setAttribute("receptor", receptorName);
119 aCellTag.appendChild(aConnectionTag);
123 QDomElement preScriptTag = doc.createElement("pre");
124 preScriptTag.setAttribute("parameter", "script");
125 QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCustomScriptFromWorkFile(cell));
126 preScriptTag.appendChild(scriptCData);
127 aCellTag.appendChild(preScriptTag);
128 body.appendChild(aCellTag);
131 const QList<DCVPageComponent*>* vcellcodeclasses = page->getCellCodeClasses();
132 for (int i = 0; i < vcellcodeclasses->length(); i++)
134 DCCellCode *cellcodeclass = dynamic_cast<DCVCCellCode*>(vcellcodeclasses->at(i))->getOwnerCellCodeClass();
135 QString cellCodeClassName = DCUtil::getNameFromFQNPath(QString::fromStdString(cellcodeclass->getFQNName()));
137 QDomElement aCellCodeTag = doc.createElement("a");
138 aCellCodeTag.setAttribute("define", "cellcode");
139 aCellCodeTag.setAttribute("name", cellCodeClassName);
140 aCellCodeTag.setAttribute("type", QString::fromStdString(cellcodeclass->getCellAPIName()));
141 QDomElement preScriptTag = doc.createElement("pre");
142 preScriptTag.setAttribute("parameter", "script");
143 QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCellCodeScriptFromFile(cellcodeclass));
144 preScriptTag.appendChild(scriptCData);
145 aCellCodeTag.appendChild(preScriptTag);
146 body.appendChild(aCellCodeTag);
149 doc.appendChild(root);
151 QFile file(pageFilePath);
152 QFileInfo pathInfo = QFileInfo(pageFilePath);
153 QDir dir = pathInfo.dir();
155 dir.mkpath(dir.absolutePath());
158 if (file.open(QFile::WriteOnly | QFile::Truncate))
160 QTextStream out(&file);
168 r = d_container->getScene()->saveSceneForPage(containerRootPath, page);