OSDN Git Service

[denncoCreator] Initial implementation for saveAll functionality completed.
[dennco/denncoCreator.git] / Source / dccontainersaver.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
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.
7 //
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.
12 //
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/>.
15
16 //
17 //  Created by tkawata on Oct-2, 2012.
18 //
19 #include "dccontainersaver.h"
20
21 #include "dccontainer.h"
22 #include "dcscene.h"
23 #include "dcvcpage.h"
24 #include "dcvccellcode.h"
25 #include "dccell.h"
26 #include "dccellcode.h"
27 #include "dcreceptor.h"
28 #include "dcaxon.h"
29 #include "dcaxonterminal.h"
30 #include "dcvpagecomponent.h"
31 #include "dcscene.h"
32
33 #include <QDomDocument>
34 #include <QMap>
35 #include <QFile>
36 #include <QDir>
37
38 DCContainerSaver::DCContainerSaver(DCContainer *container) : d_container(container)
39 {
40
41 }
42
43 DCContainerSaver::~DCContainerSaver()
44 {
45
46 }
47
48 bool DCContainerSaver::saveAll(const QString& containerRootPath)
49 {
50     const QMap<QString,DCVCPage*> *pages = d_container->getScene()->getPages();
51     QMapIterator<QString, DCVCPage*> i(*pages);
52     bool r = true;
53     while (i.hasNext())
54     {
55         i.next();
56         DCVCPage *page = i.value();
57         if (!saveForPage(containerRootPath, page))
58         {
59             r = false;
60         }
61     }
62     return true;
63 }
64
65 bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *page)
66 {
67     QString pageFilePath = containerRootPath;
68     pageFilePath.append(page->getLocationPath());
69
70     QDomDocument doc("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");
71     doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
72
73     QDomElement root = doc.createElementNS("http://www.w3.org/1999/xhtml","html");
74     root.setAttribute("lang", "en");
75     QDomElement body = doc.createElement("body");
76     root.appendChild(body);
77
78     const QList<DCVPageComponent*>* vcells = page->getCells();
79     for (int i = 0; i < vcells->length(); i++)
80     {
81         DCCell *cell = vcells->at(i)->getOwnerCell();
82         QString cellName = QString::fromStdString(cell->getName());
83
84         QDomElement aCellTag = doc.createElement("a");
85         aCellTag.setAttribute("define", "cell");
86         aCellTag.setAttribute("name", cellName);
87
88         QDomElement aCellCodeTag = doc.createElement("a");
89         aCellCodeTag.setAttribute("parameter", "cellcode");
90         if (cell->getCellCode() && cell->getCellCode() != d_container->getEmptyCellCodeClass())
91         {
92             aCellCodeTag.setAttribute("href", QString::fromStdString(cell->getCellCode()->getName()));
93         }
94         else
95         {
96             aCellCodeTag.setAttribute("type", cell->getType());
97         }
98         aCellTag.appendChild(aCellCodeTag);
99         DCAxon *axon = cell->getAxon();
100         for (int j = 0; j < axon->getNumberOfTerminals(); j++)
101         {
102             DCAxonTerminal *terminal = axon->getTerminalAt(j);
103             DCReceptor *receptor = dynamic_cast<DCReceptor*>(terminal->getTarget());
104             if (receptor)
105             {
106                 DCCell *targetCell = dynamic_cast<DCCell*>(receptor->getOwnerCell());
107                 if (targetCell)
108                 {
109                     QString connectionPath = QString::fromStdString(targetCell->getLocation());
110                     connectionPath.append("#");
111                     connectionPath.append(QString::fromStdString(targetCell->getName()));
112                     QString receptorName = QString::fromStdString(targetCell->getReceptorName(receptor));
113
114                     QDomElement aConnectionTag = doc.createElement("a");
115                     aConnectionTag.setAttribute("parameter", "connection");
116                     aConnectionTag.setAttribute("href", connectionPath);
117                     aConnectionTag.setAttribute("receptor", receptorName);
118                     aCellTag.appendChild(aConnectionTag);
119                 }
120             }
121         }
122         QDomElement preScriptTag = doc.createElement("pre");
123         preScriptTag.setAttribute("parameter", "script");
124         QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCustomScriptFromWorkFile(cell));
125         preScriptTag.appendChild(scriptCData);
126         aCellTag.appendChild(preScriptTag);
127         body.appendChild(aCellTag);
128     }
129
130     const QList<DCVPageComponent*>* vcellcodeclasses = page->getCellCodeClasses();
131     for (int i = 0; i < vcellcodeclasses->length(); i++)
132     {
133         DCCellCode *cellcodeclass = dynamic_cast<DCVCCellCode*>(vcellcodeclasses->at(i))->getOwnerCellCodeClass();
134         QString cellCodeClassName = QString::fromStdString(cellcodeclass->getName());
135         if (cellCodeClassName.indexOf("#") >= 0)
136             cellCodeClassName = cellCodeClassName.mid(cellCodeClassName.indexOf("#")+1);
137
138         QDomElement aCellCodeTag = doc.createElement("a");
139         aCellCodeTag.setAttribute("define", "cellcode");
140         aCellCodeTag.setAttribute("name", cellCodeClassName);
141         aCellCodeTag.setAttribute("type", QString::fromStdString(cellcodeclass->getCellAPIName()));
142         QDomElement preScriptTag = doc.createElement("pre");
143         preScriptTag.setAttribute("parameter", "script");
144         QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCellCodeScriptFromFile(cellcodeclass));
145         preScriptTag.appendChild(scriptCData);
146         aCellCodeTag.appendChild(preScriptTag);
147         body.appendChild(aCellCodeTag);
148     }
149
150     doc.appendChild(root);
151
152     QFile file(pageFilePath);
153     QFileInfo pathInfo = QFileInfo(pageFilePath);
154     QDir dir = pathInfo.dir();
155     if (!dir.exists())
156         dir.mkpath(dir.absolutePath());
157
158     bool r = false;
159     if (file.open(QFile::WriteOnly | QFile::Truncate))
160     {
161         QTextStream out(&file);
162         doc.save(out, 4);
163         r = true;
164     }
165
166     if (r)
167     {
168         r = d_container->getScene()->saveSceneForPage(containerRootPath, page);
169     }
170     return r;
171 }