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 Sep-30, 2012.
22 #include "dccellcode.h"
24 #include "dcreceptor.h"
25 #include "dccontainer.h"
28 #include "dccuberenderer.h"
29 #include "dcselectionrenderer.h"
34 static const double CUBE_ROUNDRADIUS = 0.05;
35 static const double PI = 3.14159265358979323846264338327950288419717;
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)
41 DCContainer *container = dynamic_cast<DCContainer*> (d_owner->getContainer());
44 d_shape = new DCCubeRenderer(size,height, CUBE_ROUNDRADIUS,3, true);
45 d_shouldUpdateShape = true;
48 d_selectionRectRenderer = new DCSelectionRenderer(CUBE_ROUNDRADIUS,CUBE_ROUNDRADIUS);
56 if (d_selectionRectRenderer)
57 delete d_selectionRectRenderer;
62 void DCVCCell::unregisterFromPage()
67 void DCVCCell::prepareChildrenForDraw(bool isAnimationInterval)
69 const TKReceptorMap *receptors = d_owner->getReceptors();
70 TKReceptorMap::const_iterator it = receptors->begin();
71 while( it != receptors->end())
73 ((DCReceptor*)(*it).second)->getVComponent()->prepareForDraw(isAnimationInterval);
76 d_owner->getAxon()->getVComponent()->prepareForDraw(isAnimationInterval);
79 void DCVCCell::drawChildren(bool isAnimationInterval)
81 const TKReceptorMap *receptors = d_owner->getReceptors();
82 TKReceptorMap::const_iterator it = receptors->begin();
83 while( it != receptors->end())
85 ((DCReceptor*)(*it).second)->getVComponent()->draw(isAnimationInterval);
88 d_owner->getAxon()->getVComponent()->draw(isAnimationInterval);
91 void DCVCCell::drawChildrenForSelection(QList<DCVComponent *> *itemList)
93 const TKReceptorMap *receptors = d_owner->getReceptors();
94 TKReceptorMap::const_iterator it = receptors->begin();
95 while( it != receptors->end())
97 ((DCReceptor*)(*it).second)->getVComponent()->draw(false);
100 d_owner->getAxon()->getVComponent()->drawForSelection(itemList);
103 void DCVCCell::translate()
105 glTranslatef(getPageX() , d_owner->getViewHeight()/2.0, getPageY());
108 void DCVCCell::setSelected(bool selected, bool updateChildren)
110 DCVComponent::setSelected(selected, updateChildren);
112 const TKReceptorMap *receptors = d_owner->getReceptors();
115 TKReceptorMap::const_iterator it = receptors->begin();
116 while( it != receptors->end())
118 ((DCReceptor*)(*it).second)->getVComponent()->setSelected(selected, true);
121 d_owner->getAxon()->getVComponent()->setSelected(selected, updateChildren);
123 d_shouldUpdateShape = true;
126 void DCVCCell::setVisible(DCVVisibility visibleSelf, DCVVisibility visibleChildren)
128 DCVComponent::setVisible(visibleSelf, visibleChildren);
130 const TKReceptorMap *receptors = d_owner->getReceptors();
131 TKReceptorMap::const_iterator it = receptors->begin();
132 while( it != receptors->end())
134 ((DCReceptor*)(*it).second)->getVComponent()->setVisible(visibleChildren,visibleChildren);
137 d_owner->getAxon()->getVComponent()->setVisible(visibleSelf,visibleChildren);
140 void DCVCCell::setSelectable(bool selectable)
142 DCVComponent::setSelectable(selectable);
144 const TKReceptorMap *receptors = d_owner->getReceptors();
145 TKReceptorMap::const_iterator it = receptors->begin();
146 while( it != receptors->end())
148 ((DCReceptor*)(*it).second)->getVComponent()->setSelectable(selectable);
151 d_owner->getAxon()->getVComponent()->setSelectable(selectable);
154 void DCVCCell::renderOwnShape(bool isAnimationInterval, bool renderAsWireframe)
156 if (renderAsWireframe)
158 //render bounding box
159 float hwidth = d_owner->getViewSize()/2.0;
160 float hheight = d_owner->getViewHeight()/2.0;
162 glColor4f(1.0, 1.0, 1.0,0.5);
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);
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);
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);
193 if (d_shouldUpdateShape)
195 d_shape->setSize(d_owner->getViewSize());
196 d_shape->setHeight(d_owner->getViewHeight());
199 if (d_owner->getCellCode() && d_owner->getCellCode()->getName() != "_DNEMPTY")
201 label1 = QString::fromStdString(d_owner->getCellCode()->getName());
202 label1 = label1.mid(label1.lastIndexOf("#") + 1);
206 label1 = "type : " + d_owner->getType();
209 QString label2 = QString::fromStdString(d_owner->getName());
210 d_shape->setTextLabel(label1, label2);
212 d_shouldUpdateShape = false;
218 d_selectionRectRenderer->setRectSize(d_owner->getViewSize());
220 glTranslatef(0,d_owner->getViewHeight()/2,0);
221 d_selectionRectRenderer->draw(isAnimationInterval && (d_isDragging && !d_isResizingDrag));
223 DCScene::setShouldContinueAnimation(true);
228 void DCVCCell::updateShape()
230 d_shouldUpdateShape = true;
233 bool DCVCCell::startDrag(float x, float y, float z, bool isResizingDrag)
235 d_dragOffsetX = getPageX() - x;
236 d_dragOffsetY = getPageY() - z;
238 d_isResizingDrag = isResizingDrag;
243 d_draggingOriginalSize = d_owner->getViewSize();
244 d_draggingOriginalAxonLength = d_owner->getAxon()->getViewLength();
250 bool DCVCCell::dragging(float x, float y, float z, bool isResizingDrag)
252 if (isResizingDrag && d_isResizingDrag)
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);
267 setPageX(d_dragOffsetX + x);
268 setPageY(d_dragOffsetY + z);
276 bool DCVCCell::endDrag(float x, float y, float z, bool isResizingDrag)
278 d_isDragging = false;
282 bool DCVCCell::isResizingArea(float x, float y, float z) const
284 d_selectionRectRenderer->setRectSize(d_owner->getViewSize());
285 return d_selectionRectRenderer->isResizingArea(x, z);
288 void DCVCCell::saveAttributesToXML(QDomDocument *document, QDomElement* element)
290 element->setAttribute("x", getPageX());
291 element->setAttribute("y", getPageY());
292 element->setAttribute("size", d_owner->getViewSize());
293 element->setAttribute("height", d_owner->getViewHeight());
295 const TKReceptorMap *receptors = d_owner->getReceptors();
296 TKReceptorMap::const_iterator it = receptors->begin();
297 while( it != receptors->end())
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);
306 QDomElement axonNode = document->createElement("axon");
307 d_owner->getAxon()->getVComponent()->saveAttributesToXML(document, &axonNode);
308 element->appendChild(axonNode);
311 void DCVCCell::loadAttributesFromXML(QDomElement element)
313 if (element.hasAttribute("x"))
314 setPageX(element.attribute("x", "0").toFloat());
316 if (element.hasAttribute("y"))
317 setPageY(element.attribute("y", "0").toFloat());
319 if (element.hasAttribute("size"))
320 d_owner->setViewSize(element.attribute("size", "1.0").toFloat());
322 if (element.hasAttribute("height"))
323 d_owner->setViewHeight(element.attribute("height", "1.0").toFloat());
326 const TKReceptorMap *receptors = d_owner->getReceptors();
327 QDomNodeList children = element.childNodes();
328 for (int i = 0; i < children.size(); i++)
330 if (!children.at(i).isElement())
333 QDomElement e = children.at(i).toElement();
334 QString tName = e.tagName();
337 d_owner->getAxon()->getVComponent()->loadAttributesFromXML(e);
339 else if(tName == "receptor")
341 QString rName = e.attribute("name", "");
342 TKReceptorMap::const_iterator it = receptors->find(rName.toStdString());
343 if (it != receptors->end())
345 ((DCReceptor*)(*it).second)->getVComponent()->loadAttributesFromXML(e);