OSDN Git Service

[denncoCreator] fixed method name.
[dennco/denncoCreator.git] / Source / visualizer / component / dcvccell.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 Sep-30, 2012.
18 //
19 #include "dcvccell.h"
20
21 #include "dccell.h"
22 #include "dccellcode.h"
23 #include "dcaxon.h"
24 #include "dcreceptor.h"
25 #include "dccontainer.h"
26 #include "dcscene.h"
27
28 #include "dccuberenderer.h"
29 #include "dcselectionrenderer.h"
30
31 #include <QtOpenGL>
32
33
34 static const double CUBE_ROUNDRADIUS = 0.05;
35 static const double PI = 3.14159265358979323846264338327950288419717;
36
37 DCVCCell::DCVCCell(DCCell *owner, DCVCPage *page, float size, float height)
38     : d_owner(owner), d_page(page), d_shouldUpdateShape(true),
39       d_dragOffsetX(0), d_dragOffsetY(0), d_dragOffsetZ(0), d_draggingOriginalSize(0), d_isDragging(false), d_isResizingDrag(false), d_draggingOriginalAxonLength(0)
40 {
41     DCContainer *container = dynamic_cast<DCContainer*> (d_owner->getContainer());
42     if (container)
43     {
44         d_shape = new DCCubeRenderer(size,height, CUBE_ROUNDRADIUS,3, true);
45         d_shouldUpdateShape = true;
46     }
47
48     d_selectionRectRenderer = new DCSelectionRenderer(CUBE_ROUNDRADIUS,CUBE_ROUNDRADIUS);
49 }
50
51 DCVCCell::~DCVCCell()
52 {
53     if (d_shape)
54         delete d_shape;
55
56     if (d_selectionRectRenderer)
57         delete d_selectionRectRenderer;
58
59
60 }
61
62 void DCVCCell::unregisterFromPage()
63 {
64     d_page = NULL;
65 }
66
67 void DCVCCell::prepareChildrenForDraw(bool isAnimationInterval)
68 {
69     const TKReceptorMap *receptors = d_owner->getReceptors();
70     TKReceptorMap::const_iterator it = receptors->begin();
71     while( it != receptors->end())
72     {
73         ((DCReceptor*)(*it).second)->getVComponent()->prepareForDraw(isAnimationInterval);
74         ++it;
75     }
76     d_owner->getAxon()->getVComponent()->prepareForDraw(isAnimationInterval);
77 }
78
79 void DCVCCell::drawChildren(bool isAnimationInterval)
80 {
81     const TKReceptorMap *receptors = d_owner->getReceptors();
82     TKReceptorMap::const_iterator it = receptors->begin();
83     while( it != receptors->end())
84     {
85         ((DCReceptor*)(*it).second)->getVComponent()->draw(isAnimationInterval);
86         ++it;
87     }
88     d_owner->getAxon()->getVComponent()->draw(isAnimationInterval);
89 }
90
91 void DCVCCell::drawChildrenForSelection(QList<DCVComponent *> *itemList)
92 {
93     const TKReceptorMap *receptors = d_owner->getReceptors();
94     TKReceptorMap::const_iterator it = receptors->begin();
95     while( it != receptors->end())
96     {
97         ((DCReceptor*)(*it).second)->getVComponent()->draw(false);
98         ++it;
99     }
100     d_owner->getAxon()->getVComponent()->drawForSelection(itemList);
101 }
102
103 void DCVCCell::translate()
104 {
105     glTranslatef(getPageX() , d_owner->getViewHeight()/2.0, getPageY());
106 }
107
108 void DCVCCell::setSelected(bool selected, bool updateChildren)
109 {
110     DCVComponent::setSelected(selected, updateChildren);
111
112     const TKReceptorMap *receptors = d_owner->getReceptors();
113     if (updateChildren)
114     {
115         TKReceptorMap::const_iterator it = receptors->begin();
116         while( it != receptors->end())
117         {
118             ((DCReceptor*)(*it).second)->getVComponent()->setSelected(selected, true);
119             ++it;
120         }
121         d_owner->getAxon()->getVComponent()->setSelected(selected, updateChildren);
122     }
123     d_shouldUpdateShape = true;
124 }
125
126 void DCVCCell::setVisible(DCVVisibility visibleSelf, DCVVisibility visibleChildren)
127 {
128     DCVComponent::setVisible(visibleSelf, visibleChildren);
129
130     const TKReceptorMap *receptors = d_owner->getReceptors();
131     TKReceptorMap::const_iterator it = receptors->begin();
132     while( it != receptors->end())
133     {
134         ((DCReceptor*)(*it).second)->getVComponent()->setVisible(visibleChildren,visibleChildren);
135         ++it;
136     }
137     d_owner->getAxon()->getVComponent()->setVisible(visibleSelf,visibleChildren);
138 }
139
140 void DCVCCell::setSelectable(bool selectable)
141 {
142     DCVComponent::setSelectable(selectable);
143
144     const TKReceptorMap *receptors = d_owner->getReceptors();
145     TKReceptorMap::const_iterator it = receptors->begin();
146     while( it != receptors->end())
147     {
148         ((DCReceptor*)(*it).second)->getVComponent()->setSelectable(selectable);
149         ++it;
150     }
151     d_owner->getAxon()->getVComponent()->setSelectable(selectable);
152 }
153
154 void DCVCCell::renderOwnShape(bool isAnimationInterval, bool renderAsWireframe)
155 {
156     if (renderAsWireframe)
157     {
158         //render bounding box
159         float hwidth = d_owner->getViewSize()/2.0;
160         float hheight = d_owner->getViewHeight()/2.0;
161         glLineWidth(1);
162         glColor4f(1.0, 1.0, 1.0,0.5);
163         glEnable(GL_BLEND);
164         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
165         glEnable( GL_LINE_SMOOTH );
166         glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
167         glBegin(GL_LINE_LOOP);
168         glVertex3f(hwidth, hheight, hwidth);
169         glVertex3f(-hwidth, hheight, hwidth);
170         glVertex3f(-hwidth, hheight, -hwidth);
171         glVertex3f(hwidth, hheight, -hwidth);
172         glEnd();
173         glBegin(GL_LINE_LOOP);
174         glVertex3f(hwidth, -hheight, hwidth);
175         glVertex3f(-hwidth, -hheight, hwidth);
176         glVertex3f(-hwidth, -hheight, -hwidth);
177         glVertex3f(hwidth, -hheight, -hwidth);
178         glEnd();
179         glBegin(GL_LINES);
180         glVertex3f(hwidth, -hheight, hwidth);
181         glVertex3f(hwidth, hheight, hwidth);
182         glVertex3f(-hwidth, -hheight, hwidth);
183         glVertex3f(-hwidth, hheight, hwidth);
184         glVertex3f(-hwidth, -hheight, -hwidth);
185         glVertex3f(-hwidth, hheight, -hwidth);
186         glVertex3f(hwidth, -hheight, -hwidth);
187         glVertex3f(hwidth, hheight, -hwidth);
188         glEnd();
189         glDisable(GL_BLEND);
190     }
191     else
192     {
193         if (d_shouldUpdateShape)
194         {
195             d_shape->setSize(d_owner->getViewSize());
196             d_shape->setHeight(d_owner->getViewHeight());
197
198             QString label1;
199             if (d_owner->getCellCode() && d_owner->getCellCode()->getName() != "_DNEMPTY")
200             {
201                 label1 = QString::fromStdString(d_owner->getCellCode()->getName());
202                 label1 = label1.mid(label1.lastIndexOf("#") + 1);
203             }
204             else
205             {
206                 label1 = "type : " + d_owner->getType();
207             }
208
209             QString label2 = QString::fromStdString(d_owner->getName());
210             d_shape->setTextLabel(label1, label2);
211
212             d_shouldUpdateShape = false;
213         }
214         d_shape->draw();
215
216         if (getIsSelected())
217         {
218             d_selectionRectRenderer->setRectSize(d_owner->getViewSize());
219             glPushMatrix();
220             glTranslatef(0,d_owner->getViewHeight()/2,0);
221             d_selectionRectRenderer->draw(isAnimationInterval && (d_isDragging && !d_isResizingDrag));
222             glPopMatrix();
223             DCScene::setShouldContinueAnimation(true);
224         }
225     }
226 }
227
228 void DCVCCell::updateShape()
229 {
230     d_shouldUpdateShape = true;
231 }
232
233 bool DCVCCell::startDrag(float x, float y, float z, bool isResizingDrag)
234 {
235     d_dragOffsetX = getPageX() - x;
236     d_dragOffsetY = getPageY() - z;
237
238     d_isResizingDrag = isResizingDrag;
239     d_isDragging = true;
240
241     if (isResizingDrag)
242     {
243         d_draggingOriginalSize = d_owner->getViewSize();
244         d_draggingOriginalAxonLength = d_owner->getAxon()->getViewLength();
245     }
246
247     return true;
248 }
249
250 bool DCVCCell::dragging(float x, float y, float z, bool isResizingDrag)
251 {
252     if (isResizingDrag && d_isResizingDrag)
253     {
254         float dx = d_dragOffsetX - getPageX() + x;
255         float dy = d_dragOffsetY - getPageY() + z;
256         float length = sqrt(dx * dx + dy * dy);
257         float dlength = dx < 0 && dy < 0 ? - length : length;
258         float newSize = d_draggingOriginalSize + dlength;
259         d_owner->setViewSize(newSize);
260         float newAxonLength = d_draggingOriginalAxonLength + dlength/2;
261         if (newAxonLength < d_owner->getViewSize()/2)
262             newAxonLength = d_owner->getViewSize()/2;
263         d_owner->getAxon()->setViewLength(newAxonLength);
264     }
265     else
266     {
267         setPageX(d_dragOffsetX + x);
268         setPageY(d_dragOffsetY + z);
269     }
270
271     d_isDragging = true;
272
273     return true;
274 }
275
276 bool DCVCCell::endDrag(float x, float y, float z, bool isResizingDrag)
277 {
278     d_isDragging = false;
279     return true;
280 }
281
282 bool DCVCCell::isResizingArea(float x, float y, float z) const
283 {
284     d_selectionRectRenderer->setRectSize(d_owner->getViewSize());
285     return d_selectionRectRenderer->isResizingArea(x, z);
286 }
287
288 void DCVCCell::saveAttributesToXML(QDomDocument *document, QDomElement* element)
289 {
290     element->setAttribute("x", getPageX());
291     element->setAttribute("y", getPageY());
292     element->setAttribute("size", d_owner->getViewSize());
293     element->setAttribute("height", d_owner->getViewHeight());
294
295     const TKReceptorMap *receptors = d_owner->getReceptors();
296     TKReceptorMap::const_iterator it = receptors->begin();
297     while( it != receptors->end())
298     {
299         QDomElement receptorNode = document->createElement("receptor");
300         receptorNode.setAttribute("name", QString::fromStdString((*it).first));
301         ((DCReceptor*)(*it).second)->getVComponent()->saveAttributesToXML(document, &receptorNode);
302         element->appendChild(receptorNode);
303         ++it;
304     }
305
306     QDomElement axonNode = document->createElement("axon");
307     d_owner->getAxon()->getVComponent()->saveAttributesToXML(document, &axonNode);
308     element->appendChild(axonNode);
309 }
310
311 void DCVCCell::loadAttributesFromXML(QDomElement element)
312 {
313     if (element.hasAttribute("x"))
314         setPageX(element.attribute("x", "0").toFloat());
315
316     if (element.hasAttribute("y"))
317         setPageY(element.attribute("y", "0").toFloat());
318
319     if (element.hasAttribute("size"))
320         d_owner->setViewSize(element.attribute("size", "1.0").toFloat());
321
322     if (element.hasAttribute("height"))
323         d_owner->setViewHeight(element.attribute("height", "1.0").toFloat());
324
325
326     const TKReceptorMap *receptors = d_owner->getReceptors();
327     QDomNodeList children = element.childNodes();
328     for (int i = 0; i < children.size(); i++)
329     {
330         if (!children.at(i).isElement())
331             continue;
332
333         QDomElement e = children.at(i).toElement();
334         QString tName = e.tagName();
335         if (tName == "axon")
336         {
337             d_owner->getAxon()->getVComponent()->loadAttributesFromXML(e);
338         }
339         else if(tName == "receptor")
340         {
341             QString rName = e.attribute("name", "");
342             TKReceptorMap::const_iterator it = receptors->find(rName.toStdString());
343             if (it != receptors->end())
344             {
345                 ((DCReceptor*)(*it).second)->getVComponent()->loadAttributesFromXML(e);
346             }
347         }
348     }
349 }
350