OSDN Git Service

[denncoCreator] Implemented change cell and cell code type command functionality.
[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 #include "utils/dcutil.h"
33
34 #include <QDomDocument>
35 #include <QMap>
36 #include <QFile>
37 #include <QDir>
38
39 DCContainerSaver::DCContainerSaver(DCContainer *container) : d_container(container)
40 {
41
42 }
43
44 DCContainerSaver::~DCContainerSaver()
45 {
46
47 }
48
49 bool DCContainerSaver::saveAll(const QString& containerRootPath)
50 {
51     const QMap<QString,DCVCPage*> *pages = d_container->getScene()->getPages();
52     QMapIterator<QString, DCVCPage*> i(*pages);
53     bool r = true;
54     while (i.hasNext())
55     {
56         i.next();
57         DCVCPage *page = i.value();
58         if (!saveForPage(containerRootPath, page))
59         {
60             r = false;
61         }
62     }
63     return true;
64 }
65
66 bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *page)
67 {
68     QString pageFilePath = containerRootPath;
69     pageFilePath.append(page->getLocationPath());
70
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\"");
73
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);
78
79     const QList<DCVPageComponent*>* vcells = page->getCells();
80     for (int i = 0; i < vcells->length(); i++)
81     {
82         DCCell *cell = vcells->at(i)->getOwnerCell();
83         QString cellName = QString::fromStdString(cell->getName());
84
85         QDomElement aCellTag = doc.createElement("a");
86         aCellTag.setAttribute("define", "cell");
87         aCellTag.setAttribute("name", cellName);
88
89         QDomElement aCellCodeTag = doc.createElement("a");
90         aCellCodeTag.setAttribute("parameter", "cellcode");
91         if (cell->getIsCellCodeClassAssgined())
92         {
93             aCellCodeTag.setAttribute("href", QString::fromStdString(cell->getCellCode()->getFQNName()));
94         }
95         else
96         {
97             aCellCodeTag.setAttribute("type", cell->getType());
98         }
99         aCellTag.appendChild(aCellCodeTag);
100         DCAxon *axon = cell->getAxon();
101         for (int j = 0; j < axon->getNumberOfTerminals(); j++)
102         {
103             DCAxonTerminal *terminal = axon->getTerminalAt(j);
104             DCReceptor *receptor = dynamic_cast<DCReceptor*>(terminal->getTarget());
105             if (receptor)
106             {
107                 DCCell *targetCell = dynamic_cast<DCCell*>(receptor->getOwnerCell());
108                 if (targetCell)
109                 {
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));
114
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);
120                 }
121             }
122         }
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);
129     }
130
131     const QList<DCVPageComponent*>* vcellcodeclasses = page->getCellCodeClasses();
132     for (int i = 0; i < vcellcodeclasses->length(); i++)
133     {
134         DCCellCode *cellcodeclass = dynamic_cast<DCVCCellCode*>(vcellcodeclasses->at(i))->getOwnerCellCodeClass();
135         QString cellCodeClassName = DCUtil::getNameFromFQNPath(QString::fromStdString(cellcodeclass->getFQNName()));
136
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);
147     }
148
149     doc.appendChild(root);
150
151     QFile file(pageFilePath);
152     QFileInfo pathInfo = QFileInfo(pageFilePath);
153     QDir dir = pathInfo.dir();
154     if (!dir.exists())
155         dir.mkpath(dir.absolutePath());
156
157     bool r = false;
158     if (file.open(QFile::WriteOnly | QFile::Truncate))
159     {
160         QTextStream out(&file);
161         doc.save(out, 4);
162         r = true;
163     }
164     file.close();
165
166     if (r)
167     {
168         r = d_container->getScene()->saveSceneForPage(containerRootPath, page);
169     }
170     return r;
171 }