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.
21 #include "dcglvisualizerwidget.h"
22 #include "dcvcomponent.h"
23 #include "dcvpagecomponent.h"
26 #include "dccreator.h"
28 #include "dcanimationtimer.h"
31 #include "dcreceptor.h"
32 #include "utils/dccomponentutil.h"
33 #include "dcglwidget.h"
35 #include "dctoolwindowviewcontrol.h"
36 #include "dctoolwindowcelleditor.h"
37 #include "dctoolwindowterminalfromaxoneditor.h"
38 #include "dctoolwindowterminalfromreceptoreditor.h"
40 #include "eventhandler/dcveventhandler.h"
41 #include "utils/dcveventhandlerutil.h"
42 #include "codeeditor/dccellscriptseditorwindow.h"
56 #ifndef GL_MULTISAMPLE
57 #define GL_MULTISAMPLE 0x809D
60 static QGraphicsView *s_uiView = NULL;
63 DCGLVisualizerWidget::DCGLVisualizerWidget(DCCreator *creator, QWidget *parent) : QGraphicsView(parent)
65 QGraphicsScene *scene = new DCUIGraphicsScene(creator, parent);
68 setViewport(DCGLWidget::singleton());
69 setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
71 setContextMenuPolicy(Qt::CustomContextMenu);
72 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),scene, SLOT(showContextMenu(const QPoint&)));
77 DCGLVisualizerWidget::~DCGLVisualizerWidget()
81 QPointF DCGLVisualizerWidget::globalPointToLocal(const QPoint &point)
83 if (s_uiView && s_uiView->scene())
85 return s_uiView->mapToScene(s_uiView->mapFromGlobal(point));
90 bool toolWindowPositionLessThan(const DCGraphicsProxyWidget *t1, const DCGraphicsProxyWidget *t2)
92 if (t1->geometry().right() < t2->geometry().left())
96 else if (t2->geometry().right() < t1->geometry().left())
101 if (t1->pos().y() < t2->pos().y())
105 else if (t1->pos().y() == t2->pos().y())
107 if (t1->getPosPriority() < t2->getPosPriority())
115 void DCGLVisualizerWidget::resizeEvent(QResizeEvent *event) {
118 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
120 QGraphicsView::resizeEvent(event);
123 void DCGLVisualizerWidget::setupViewport(QWidget *viewport)
125 QGLWidget *glWidget = dynamic_cast<QGLWidget*>(viewport);
126 glWidget->setAutoFillBackground(false);
127 glWidget->setMouseTracking(true);
130 glWidget->updateGL();
133 void DCGLVisualizerWidget::setVisible(bool visible)
135 QGraphicsView::setVisible(visible);
138 int DCGraphicsProxyWidget::getPosPriority() const
140 DCToolWindowBase *window = dynamic_cast<DCToolWindowBase*>(widget());
143 return window->getPosPriority();
148 DCGraphicsProxyWidget::DCGraphicsProxyWidget(DCSticky sticky, QGraphicsItem *parent, Qt::WindowFlags wFlags)
149 : QGraphicsProxyWidget(parent, wFlags), d_sticky(sticky), d_hasTargetPos(false), d_targetX(0), d_targetY(0),d_embeddedToolWindow(NULL)
153 DCGraphicsProxyWidget::~DCGraphicsProxyWidget()
158 void DCGraphicsProxyWidget::setTargetPos(float targetX, float targetY)
162 d_hasTargetPos = true;
165 void DCGraphicsProxyWidget::doAnimation()
167 const static int MINSPEED = 60;
169 if (!d_embeddedToolWindow)
171 d_embeddedToolWindow = dynamic_cast<DCToolWindowBase*>(widget());
172 if (!d_embeddedToolWindow)
179 float dx = d_targetX - p.x();
180 float dy = d_targetY - p.y();
181 float d = sqrt(dx * dx + dy * dy);
184 d_hasTargetPos = false;
185 setPos(d_targetX, d_targetY);
189 float speed = MINSPEED + d/3;
190 float sr = speed / d;
191 moveBy(dx * sr, dy * sr);
195 d_embeddedToolWindow->doAnimation();
198 QVariant DCGraphicsProxyWidget::itemChange(GraphicsItemChange change, const QVariant &value)
200 DCUIGraphicsScene *dcGraphicScene = dynamic_cast<DCUIGraphicsScene*>(scene());
202 if (change == ItemPositionChange && dcGraphicScene) {
203 QRectF rect = boundingRect();
204 QPointF pos = value.toPointF();
206 bool adjusted = false;
208 QRectF sceneRect = dcGraphicScene->sceneRect();
210 if (d_sticky == DC_STICKYWIDGET_LEFT && pos.x() + rect.left() > dcGraphicScene->getStickyAreaLeftWidth())
212 d_sticky = DC_STICKYWIDGET_NONE;
214 else if (d_sticky == DC_STICKYWIDGET_RIGHT && pos.x() + rect.width() < sceneRect.right() - dcGraphicScene->getStickyAreaRightWidth())
216 d_sticky = DC_STICKYWIDGET_NONE;
219 if (pos.x() + rect.left() < sceneRect.left() + 2)
221 d_sticky = DC_STICKYWIDGET_LEFT;
223 pos.setX(sceneRect.left() - rect.left() + 2);
225 else if (pos.x() + rect.right() >= sceneRect.right() - 2)
227 d_sticky = DC_STICKYWIDGET_RIGHT;
229 pos.setX(sceneRect.right() - rect.right() - 2);
232 if (pos.y() + rect.top() < sceneRect.top() + 2)
235 pos.setY(sceneRect.top() - rect.top() + 2);
237 else if (pos.y() + rect.bottom() >= sceneRect.bottom() - 2)
240 pos.setY(sceneRect.bottom() - rect.bottom() - 2);
245 widget()->move(pos.x(), pos.y());
249 return QGraphicsProxyWidget::itemChange(change, value);
252 void DCGraphicsProxyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
254 QGraphicsProxyWidget::resizeEvent(event);
258 DCUIGraphicsScene::DCUIGraphicsScene(DCCreator *creator, QWidget *parent)
259 : QGraphicsScene(parent), d_creator(creator), d_scene(NULL), d_eventHandler(NULL), d_isPaintForAnimation(false), d_requestRedraw(false),
260 d_inResigingDrag(false), d_dragStartX(0), d_dragStartY(0),
261 d_requestLayoutToolWindows(false), d_stickyAreaLeftWidth(10), d_stickyAreaRightWidth(10),
262 d_terminalFromAxonToolWindow(NULL),d_terminalFromReceptorToolWindow(NULL)
264 connect(d_creator, SIGNAL(sceneChanged(const void*, DCScene*)), this, SLOT(sceneChanged(const void*, DCScene*)));
265 connect(d_creator, SIGNAL(sceneSelectedPageChanged(const void*, const DCScene*)), this, SLOT(sceneSelectedPageChanged(const void*, const DCScene*)));
266 connect(d_creator, SIGNAL(sceneSelectedCellChanged(const void*, const DCScene*)), this, SLOT(sceneSelectedCellChanged(const void*, const DCScene*)));
267 connect(d_creator, SIGNAL(sceneViewAngleChanged(const void*,const DCScene*)), this, SLOT(sceneAngleChanged(const void*, const DCScene*)));
268 connect(d_creator, SIGNAL(sceneViewCenterChanged(const void*,const DCScene*)), this, SLOT(sceneCenterChanged(const void*, const DCScene*)));
269 connect(d_creator, SIGNAL(sceneViewScaleChanged(const void*, const DCScene*)), this, SLOT(sceneScaleChanged(const void*, const DCScene*)));
270 connect(d_creator, SIGNAL(sceneViewSettingChanged(const void*, const DCScene*)), this, SLOT(sceneSettingChanged(const void*, const DCScene*)));
271 connect(d_creator, SIGNAL(sceneViewEditModeChanged(const void*,const DCScene*)), this, SLOT(sceneEditModeChanged(const void*, const DCScene*)));
272 connect(d_creator, SIGNAL(destroyed(QObject*)), this, SLOT(creatorDestroyed()));
273 connect(d_creator, SIGNAL(commandExecuted(const QUndoCommand *)), this, SLOT(creatorCommandExecuted()));
274 connect(DCAnimationTimer::instance(), SIGNAL(goNextFrame()), this, SLOT(doAnimation()));
275 connect(this, SIGNAL(sceneRectChanged(QRectF)), this, SLOT(resizeEvent(QRectF)));
276 d_viewControlTool = new DCToolWindowViewControl(d_creator);
280 DCUIGraphicsScene::~DCUIGraphicsScene()
284 d_creator->disconnect(this);
286 DCAnimationTimer::instance()->disconnect(this);
288 foreach (QGraphicsItem *item, items())
293 while (d_cellEditToolWindowPool.length() > 0)
295 DCToolWindowBase *window = d_cellEditToolWindowPool.takeFirst();
299 while (d_cellEditToolWindowList.length() > 0)
301 DCToolWindowBase *window = d_cellEditToolWindowList.takeFirst();
302 removeToolWindow(window);
306 if (d_terminalFromAxonToolWindow)
308 removeToolWindow(d_terminalFromAxonToolWindow);
309 delete d_terminalFromAxonToolWindow;
314 void DCUIGraphicsScene::addToolWindow(DCToolWindowBase *window, int x, int y, DCGraphicsProxyWidget::DCSticky sticky )
316 DCGraphicsProxyWidget *proxy = new DCGraphicsProxyWidget(sticky);
317 proxy->setWidget(window);
318 proxy->setZValue(1e30);
319 proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache); // QGraphicsItem::ItemCoordinateCache
320 proxy->setFlag(QGraphicsItem::ItemIsMovable);
321 proxy->setFlag(QGraphicsItem::ItemIsSelectable, true);
322 proxy->setFlag(QGraphicsItem::ItemIsFocusable, true);
326 connect(window, SIGNAL(dragMoveFinished()), this, SLOT(requestLayoutToolWindows()));
327 connect(proxy, SIGNAL(heightChanged()), this, SLOT(requestLayoutToolWindows()));
328 connect(proxy, SIGNAL(widthChanged()), this, SLOT(requestLayoutToolWindows()));
330 requestLayoutToolWindows();
333 void DCUIGraphicsScene::removeToolWindow(DCToolWindowBase *window)
335 for (int i = items().length() - 1; i >= 0; i-- )
337 DCGraphicsProxyWidget *proxy = dynamic_cast<DCGraphicsProxyWidget*>(items().at(i));
338 if (proxy && proxy->widget() == window)
340 proxy->setWidget(NULL);
346 requestLayoutToolWindows();
349 void DCUIGraphicsScene::layoutToolWindows()
351 QList<DCGraphicsProxyWidget*> stickyListLeft;
352 QList<DCGraphicsProxyWidget*> stickyListRight;
354 foreach (QGraphicsItem *item, items())
356 DCGraphicsProxyWidget *proxy = dynamic_cast<DCGraphicsProxyWidget*>(item);
359 switch(proxy->getSticky())
361 case DCGraphicsProxyWidget::DC_STICKYWIDGET_LEFT:
362 stickyListLeft.append(proxy);
364 case DCGraphicsProxyWidget::DC_STICKYWIDGET_RIGHT:
365 stickyListRight.append(proxy);
372 qSort(stickyListLeft.begin(), stickyListLeft.end(), toolWindowPositionLessThan);
373 qSort(stickyListRight.begin(), stickyListRight.end(), toolWindowPositionLessThan);
375 QRectF srect = sceneRect();
378 for (int i = 0; i < stickyListLeft.length(); i++)
380 DCGraphicsProxyWidget *item = stickyListLeft.at(i);
381 const QRectF rect = item->boundingRect();
382 float tx = pos.x() - rect.x();
383 float ty = pos.y() - rect.y();
384 if (ty + rect.height() > srect.height())
388 tx = pos.x() - rect.x();
389 ty = pos.y() - rect.y();
391 item->setTargetPos(tx, ty);
392 if (tx + rect.width() > xmax)
393 xmax = tx + rect.width();
394 pos += QPointF(0, 10 + rect.height());
396 d_stickyAreaLeftWidth = xmax;
398 pos.setX(srect.width() - 10);
399 pos.setY(srect.height() - 10);
400 float xmin = srect.width() - 10;
401 for (int i = stickyListRight.length() - 1; i >= 0 ; i--)
403 DCGraphicsProxyWidget *item = stickyListRight.at(i);
404 const QRectF rect = item->boundingRect();
405 float tx = pos.x() - rect.width() - rect.x();
406 float ty = pos.y() - rect.height() - rect.y();
410 pos.setY(srect.height() - 10);
411 tx = pos.x() - rect.width() - rect.x();
412 ty = pos.y() - rect.height() - rect.y();
414 item->setTargetPos(tx, ty);
417 pos -= QPointF(0, 10 + rect.height());
419 d_stickyAreaRightWidth = srect.width() - xmin;
421 d_requestLayoutToolWindows = false;
424 void DCUIGraphicsScene::creatorDestroyed()
429 void DCUIGraphicsScene::sceneChanged(const void *requester, DCScene *scene)
431 DCScene::lockScene();
434 delete d_eventHandler;
435 d_eventHandler = NULL;
439 d_scene->disconnect(d_viewControlTool);
440 d_scene->disconnect(this);
445 d_viewControlTool->setDCScene(d_scene);
446 connect(d_scene, SIGNAL(viewPersModeChanged(const void*)), d_viewControlTool, SLOT(changeMode(const void*)));
447 connect(d_scene, SIGNAL(modeInitialized()), this, SLOT(initMode()));
448 connect(d_scene, SIGNAL(terminalEditCursorBelongingPageChagend(DCVCPage*)), this, SLOT(sceneTerminalEditPageBelongingChanged(DCVCPage*)));
449 connect(d_scene, SIGNAL(terminalEditCursorDropTargetChanged(DCVComponent*)), this, SLOT(sceneTerminalEditDropTargetChanged(DCVComponent*)));
452 while (!d_cellEditToolWindowList.isEmpty())
454 DCToolWindowBase *toolWindow = d_cellEditToolWindowList.takeFirst();
455 removeToolWindow(toolWindow);
458 DCScene::unlockScene();
462 void DCUIGraphicsScene::sceneAngleChanged(const void* requester, const DCScene *scene)
464 if (d_scene != scene)
467 d_requestRedraw = true;
470 void DCUIGraphicsScene::sceneCenterChanged(const void *requester, const DCScene *scene)
472 if (d_scene != scene)
475 d_requestRedraw = true;
478 void DCUIGraphicsScene::sceneScaleChanged(const void* requester, const DCScene *scene)
480 if (d_scene != scene)
483 d_requestRedraw = true;
486 void DCUIGraphicsScene::sceneSettingChanged(const void* requester, const DCScene *scene)
488 if (d_scene != scene)
491 d_requestRedraw = true;
494 void DCUIGraphicsScene::sceneSelectedPageChanged(const void* requester, const DCScene *scene)
496 if (d_scene != scene)
500 d_eventHandler->selectedPageChanged(requester);
502 d_requestRedraw = true;
506 void DCUIGraphicsScene::sceneSelectedCellChanged(const void* requester, const DCScene *scene)
508 if (d_scene != scene)
511 switch (scene->getEditMode())
513 case DCScene::DCV_EDITMODE_LAYOUT:
514 updateCellEditorToolWindows();
517 case DCScene::DCV_EDITMODE_TERMINAL_FROM_AXON:
520 case DCScene::DCV_EDITMODE_TERMINAL_FROM_RECEPTOR:
525 d_requestRedraw = true;
528 void DCUIGraphicsScene::sceneEditModeChanged(const void *requester, const DCScene *scene)
531 if (d_scene != scene)
534 if (d_eventHandler && scene->getEditMode() == d_eventHandler->getHandlerType() && d_scene == d_eventHandler->getScene())
539 delete d_eventHandler;
540 d_eventHandler = NULL;
543 switch (scene->getEditMode())
545 case DCScene::DCV_EDITMODE_LAYOUT:
546 closeTerminalFromAxonToolWindow();
547 closeTerminalFromReceptorToolWindow();
548 showCellEditorToolWindows();
549 d_eventHandler = DCVEventHandlerUtil::createLayoutModeHandler(this, d_creator, d_scene);
552 case DCScene::DCV_EDITMODE_TERMINAL_FROM_AXON:
553 closeAllCellEditorToolWindows();
554 closeTerminalFromReceptorToolWindow();
555 showTerminalFromAxonToolWindow();
556 d_eventHandler = DCVEventHandlerUtil::createTerminalFromAxonModeHandler(this, d_creator, d_scene, scene->getEditAxon(), scene->getTerminalEditCursor());
559 case DCScene::DCV_EDITMODE_TERMINAL_FROM_RECEPTOR:
560 closeAllCellEditorToolWindows();
561 closeTerminalFromAxonToolWindow();
562 showTerminalFromReceptorToolWindow();
563 d_eventHandler = DCVEventHandlerUtil::createTerminalFromReceptorModeHandler(this, d_creator, d_scene, scene->getEditReceptor(), scene->getTerminalEditCursor());
567 requestRedraw(false);
570 void DCUIGraphicsScene::sceneTerminalEditPageBelongingChanged(DCVCPage *page)
572 switch (d_scene->getEditMode())
574 case DCScene::DCV_EDITMODE_LAYOUT:
577 case DCScene::DCV_EDITMODE_TERMINAL_FROM_AXON:
578 updateTerminalFromAxonToolWindow();
581 case DCScene::DCV_EDITMODE_TERMINAL_FROM_RECEPTOR:
582 updateTerminalFromReceptorToolWindow();
587 d_requestRedraw = true;
590 void DCUIGraphicsScene::sceneTerminalEditDropTargetChanged(DCVComponent *dropTarget)
592 switch (d_scene->getEditMode())
594 case DCScene::DCV_EDITMODE_LAYOUT:
597 case DCScene::DCV_EDITMODE_TERMINAL_FROM_AXON:
598 updateTerminalFromAxonToolWindow();
601 case DCScene::DCV_EDITMODE_TERMINAL_FROM_RECEPTOR:
602 updateTerminalFromReceptorToolWindow();
607 d_requestRedraw = true;
610 void DCUIGraphicsScene::setupProjection()
612 if (!d_creator || !d_scene)
615 float _width = width();
616 float _height = height();
617 float _scale = d_scene->getScale();
618 glViewport(0, 0, _width, _height);
619 glMatrixMode(GL_PROJECTION);
621 glOrtho(-_width / _scale, _width / _scale, -_height / _scale, _height / _scale, -100.0, 100.0);
625 glGetDoublev(GL_PROJECTION_MATRIX, d_projectionMatrix);
626 glGetIntegerv(GL_VIEWPORT, d_viewportMatrix);
630 void DCUIGraphicsScene::setupCamera()
632 glTranslatef(- d_scene->getCenterX(), d_scene->getCenterEditModeY(), 0);
633 glRotatef(d_scene->getXAngle(),1,0,0);
634 glRotatef(d_scene->getYAngle(),0,1,0);
635 glTranslatef(0, - d_scene->getCenterBrowsModeY(), 0);
638 void DCUIGraphicsScene::updateView(bool isAnimationInterval)
640 DCScene::lockScene();
642 DCGLWidget::singleton()->makeCurrent();
644 d_isPaintForAnimation = isAnimationInterval;
645 d_requestRedraw = false;
650 void DCUIGraphicsScene::requestRedraw(bool immediate)
655 d_requestRedraw = true;
658 DCVComponent* DCUIGraphicsScene::getObjectAt(int viewX, int viewY, DCVComponent *priorityObject)
660 if (!d_creator || !d_scene)
663 DCScene::lockScene();
665 DCGLWidget::singleton()->makeCurrent();
667 float _width = width();
668 float _height = height();
669 float _scale = d_scene->getScale();
671 GLuint selectionBuffer[512];
672 glSelectBuffer(512, selectionBuffer);
673 glRenderMode(GL_SELECT);
677 glMatrixMode(GL_PROJECTION);
681 memcpy(viewportMat, d_viewportMatrix, sizeof(viewportMat));
682 gluPickMatrix(viewX,viewportMat[3] - viewY, 4,4,viewportMat);
683 glOrtho(-_width / _scale, _width / _scale, -_height / _scale, _height / _scale, -100.0, 100.0);
687 QList<DCVComponent *> itemList;
688 glMatrixMode(GL_MODELVIEW);
690 d_scene->drawForSelection(&itemList);
693 glMatrixMode(GL_PROJECTION);
697 glMatrixMode(GL_MODELVIEW);
699 int items = glRenderMode(GL_RENDER);
700 DCVComponent *selectedObject = NULL;
701 DCVComponent *selectedPage = NULL;
703 unsigned int minDepthPage = UINT_MAX;
704 unsigned int minDepthObject = UINT_MAX;
707 for (int i = 0; i < items; i++)
709 unsigned int nameLen = selectionBuffer[idx];
710 unsigned int min = selectionBuffer[idx + 1];
711 unsigned int nameId = selectionBuffer[idx + 3];
713 if (nameLen == 1 && nameId < (unsigned int) itemList.length())
715 DCVComponent *obj = itemList[nameId];
716 if (priorityObject == obj)
718 selectedObject = obj;
721 if (obj->getPageBelonging() == obj)
723 if (min < minDepthPage)
731 if (obj->getIsSelectable() && min < minDepthObject)
733 minDepthObject = min;
734 selectedObject = obj;
743 for (int i = 0; i < items; i++)
745 unsigned int nameLen = selectionBuffer[idx];
746 unsigned int min = selectionBuffer[idx + 1];
747 unsigned int nameId = selectionBuffer[idx + 3];
749 if (nameLen == 1 && nameId < (unsigned int) itemList.length())
751 DCVComponent *obj = itemList[nameId];
752 if (obj->getPageBelonging() == obj)
754 if (min < minDepthPage)
762 if (obj->getIsSelectable() && min < minDepthObject)
764 minDepthObject = min;
765 selectedObject = obj;
773 DCScene::unlockScene();
775 return selectedObject ? selectedObject : selectedPage;
779 void DCUIGraphicsScene::initMode()
781 sceneEditModeChanged(NULL, d_scene);
785 void DCUIGraphicsScene::doAnimation()
787 if (d_requestLayoutToolWindows)
792 foreach (QGraphicsItem *item, items())
794 DCGraphicsProxyWidget *proxy = dynamic_cast<DCGraphicsProxyWidget*>(item);
797 proxy->doAnimation();
801 if (!d_creator || !d_scene)
804 if (DCScene::getShouldContinueAnimation())
806 bool r = d_scene->doAnimation();
807 DCScene::setShouldContinueAnimation(r);
810 else if (d_requestRedraw)
817 void DCUIGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
819 painter->beginNativePainting();
825 painter->endNativePainting();
828 void DCUIGraphicsScene::setStates()
830 glPushAttrib(GL_ALL_ATTRIB_BITS);
832 glMatrixMode(GL_PROJECTION);
836 glMatrixMode(GL_MODELVIEW);
841 glBindTexture(GL_TEXTURE_2D, 0);
845 void DCUIGraphicsScene::defaultStates()
849 glMatrixMode(GL_MODELVIEW);
852 glMatrixMode(GL_PROJECTION);
857 void DCUIGraphicsScene::initializeGL()
859 glClearColor(0.0, 0.0, 0.0, 1.0);
860 glEnable(GL_TEXTURE_2D);
861 glEnable(GL_MULTISAMPLE);
862 glEnable(GL_CULL_FACE);
863 glEnable(GL_DEPTH_TEST);
864 glDisable(GL_LIGHTING);
865 glDisable(GL_COLOR_MATERIAL);
866 glDisable(GL_LIGHT0);
867 glDisable(GL_NORMALIZE);
870 void DCUIGraphicsScene::paintGL()
872 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
876 DCScene::lockScene();
877 glMatrixMode(GL_MODELVIEW);
881 d_scene->draw(d_isPaintForAnimation);
884 d_isPaintForAnimation = false;
886 DCScene::unlockScene();
889 void DCUIGraphicsScene::resizeEvent(const QRectF & rect)
892 requestLayoutToolWindows();
895 void DCUIGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
897 QGraphicsScene::mousePressEvent(event);
898 if (event->isAccepted()) return;
900 if (!d_creator || !d_scene || d_scene->getIsInModeTransition())
903 QPointF mousePos = event->scenePos();
904 d_dragStartX = mousePos.x();
905 d_dragStartY = mousePos.y();
910 d_eventHandler->mousePressEvent(event);
913 void DCUIGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
915 QGraphicsScene::mousePressEvent(event);
916 if (event->isAccepted()) return;
918 if (!d_creator || !d_scene || d_scene->getIsInModeTransition())
922 d_eventHandler->mouseDoubleClickEvent(event);
925 void DCUIGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
927 QGraphicsScene::mouseMoveEvent(event);
928 if (event->isAccepted()) return;
930 if (!d_creator || !d_scene || d_scene->getIsInModeTransition())
934 d_eventHandler->mouseMoveEvent(event, d_clickTime.elapsed());
937 void DCUIGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
939 QGraphicsScene::mouseReleaseEvent(event);
940 if (event->isAccepted()) return;
941 if (!d_creator || !d_scene || d_scene->getIsInModeTransition())
944 QPointF mousePos = event->scenePos();
946 int dx = mousePos.x() - d_dragStartX;
947 int dy = mousePos.y() - d_dragStartY;
949 bool wasDrag = !(d_clickTime.elapsed() < 500 && dx * dx + dy * dy < 400);
951 DCGLWidget::singleton()->setCursor(Qt::ArrowCursor);
954 d_eventHandler->mouseReleaseEvent(event, wasDrag);
957 void DCUIGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event)
959 QGraphicsScene::wheelEvent(event);
961 if (!d_creator || !d_scene)
965 d_eventHandler->wheelEvent(event);
968 void DCUIGraphicsScene::keyPressEvent(QKeyEvent *event)
970 QGraphicsScene::keyPressEvent(event);
971 if (event->isAccepted()) return;
973 if (!d_creator || !d_scene)
977 d_eventHandler->keyPressEvent(event);
980 void DCUIGraphicsScene::keyReleaseEvent(QKeyEvent *event)
982 QGraphicsScene::keyReleaseEvent(event);
983 if (event->isAccepted()) return;
986 d_eventHandler->keyReleaseEvent(event);
989 void DCUIGraphicsScene::showContextMenu(const QPoint &pos)
991 if (!d_creator || !d_scene)
996 d_eventHandler->showContextMenu(pos);
1000 void DCUIGraphicsScene::menuActionEditThisPage()
1002 if (!d_creator || !d_scene)
1005 d_creator->changePersMode(this, DCCreator::DC_PERSMODE_NAVIGATION);
1008 void DCUIGraphicsScene::menuActionEditCellCode()
1010 if (!d_creator || !d_scene)
1013 DCCell *cell = NULL;
1014 bool isSingleCellSelection = false;
1015 QList<DCVComponent*> list = d_scene->getSelectedCellObjects();
1016 for (int i = 0; i < list.size(); i++)
1018 if (cell && cell != list.at(i)->getOwnerCell())
1020 isSingleCellSelection = false;
1023 else if (cell == NULL)
1025 cell = list.at(i)->getOwnerCell();
1028 DCCellScriptsEditorWindow::startEditing(cell);
1031 void DCUIGraphicsScene::menuActionFinishEditMode()
1033 if (!d_creator || !d_scene)
1036 d_creator->changePersMode(this, DCCreator::DC_PERSMODE_PAGEEDIT);
1039 void DCUIGraphicsScene::menuActionEditReceptor()
1041 if (!d_creator || !d_scene)
1047 void DCUIGraphicsScene::menuActionEditAxonTerminal()
1049 if (!d_creator || !d_scene)
1055 void DCUIGraphicsScene::menuActionSaveChangeAll()
1057 if (!d_creator || !d_scene)
1060 d_creator->saveAll(true);
1063 void DCUIGraphicsScene::creatorCommandExecuted()
1065 switch (d_scene->getEditMode())
1067 case DCScene::DCV_EDITMODE_LAYOUT:
1068 updateCellEditorToolWindows();
1071 case DCScene::DCV_EDITMODE_TERMINAL_FROM_AXON:
1074 case DCScene::DCV_EDITMODE_TERMINAL_FROM_RECEPTOR:
1079 d_requestRedraw = true;
1082 bool DCUIGraphicsScene::toObjectCoordinateXZ(float viewX, float viewY, DCVComponent *object, float *pLocalX, float *pLocalZ)
1084 double localX0, localY0, localZ0;
1085 double localX1, localY1, localZ1;
1087 gluUnProject(viewX, d_viewportMatrix[3] - viewY, 0, object->getModelViewMatrix(), d_projectionMatrix, d_viewportMatrix, &localX0, &localY0, &localZ0);
1088 gluUnProject(viewX, d_viewportMatrix[3] - viewY, 1, object->getModelViewMatrix(), d_projectionMatrix, d_viewportMatrix, &localX1, &localY1, &localZ1);
1089 double dx = localX1 - localX0;
1090 double dy = localY1 - localY0;
1091 double dz = localZ1 - localZ0;
1100 *pLocalX = -(dx * localY0 - dy * localX0)/dy;
1101 *pLocalZ = -(dz * localY0 - dy * localZ0)/dy;
1106 void DCUIGraphicsScene::showCellEditorToolWindows()
1108 updateCellEditorToolWindows();
1111 void DCUIGraphicsScene::updateCellEditorToolWindows()
1113 QList<DCVComponent*> selectedList = d_scene->getSelectedCellObjects();
1115 for (int i = d_cellEditToolWindowList.length() - 1; i >= 0 ; i--)
1117 DCToolWindowCellEditor *window = d_cellEditToolWindowList.at(i);
1118 if (window->getCell() == NULL || !window->getCell()->getVComponent()->getIsSelected())
1120 removeToolWindow(window);
1121 d_cellEditToolWindowList.takeAt(i);
1122 d_cellEditToolWindowPool.push_back(window);
1126 window->updateView();
1127 requestLayoutToolWindows();
1131 for (int i = 0; i < selectedList.length(); i++)
1134 for (int j = 0 ; j < d_cellEditToolWindowList.length(); j++)
1136 if (d_cellEditToolWindowList.at(j)->getCell() == selectedList.at(i)->getOwnerCell())
1144 DCToolWindowCellEditor *window = NULL;
1145 if (d_cellEditToolWindowPool.length() > 0)
1147 window = d_cellEditToolWindowPool.takeFirst();
1151 window = new DCToolWindowCellEditor(d_creator);
1153 addToolWindow(window);
1154 window->setCell(selectedList.at(i)->getOwnerCell());
1155 d_cellEditToolWindowList.append(window);
1160 void DCUIGraphicsScene::closeAllCellEditorToolWindows()
1162 while (d_cellEditToolWindowList.length() > 0)
1164 DCToolWindowCellEditor *window = d_cellEditToolWindowList.takeFirst();
1165 removeToolWindow(window);
1166 d_cellEditToolWindowPool.push_back(window);
1168 requestLayoutToolWindows();
1171 void DCUIGraphicsScene::showTerminalFromAxonToolWindow()
1173 if (d_terminalFromAxonToolWindow == NULL)
1175 d_terminalFromAxonToolWindow = new DCToolWindowTerminalFromAxon(d_creator);
1177 updateTerminalFromAxonToolWindow();
1178 addToolWindow(d_terminalFromAxonToolWindow);
1181 void DCUIGraphicsScene::updateTerminalFromAxonToolWindow()
1183 if (d_terminalFromAxonToolWindow)
1185 d_terminalFromAxonToolWindow->updateData(d_scene->getEditAxon()->getOwnerCell(),
1186 d_scene->getTerminalEditCursorLocationString(),
1187 d_scene->getTerminalEditCursorDropTarget() ? d_scene->getTerminalEditCursorDropTarget()->getOwnerCell() : NULL);
1191 void DCUIGraphicsScene::closeTerminalFromAxonToolWindow()
1193 if (d_terminalFromAxonToolWindow)
1195 removeToolWindow(d_terminalFromAxonToolWindow);
1199 void DCUIGraphicsScene::showTerminalFromReceptorToolWindow()
1201 if (d_terminalFromReceptorToolWindow == NULL)
1203 d_terminalFromReceptorToolWindow = new DCToolWindowTerminalFromReceptor(d_creator);
1205 updateTerminalFromReceptorToolWindow();
1206 addToolWindow(d_terminalFromReceptorToolWindow);
1209 void DCUIGraphicsScene::updateTerminalFromReceptorToolWindow()
1211 if (d_terminalFromReceptorToolWindow)
1213 DCReceptor *receptor = d_scene->getEditReceptor();
1215 d_terminalFromReceptorToolWindow->updateData(receptor->getOwnerCell(),
1217 d_scene->getTerminalEditCursorLocationString(),
1218 d_scene->getTerminalEditCursorDropTarget() ? d_scene->getTerminalEditCursorDropTarget()->getOwnerCell() : NULL);
1222 void DCUIGraphicsScene::closeTerminalFromReceptorToolWindow()
1224 if (d_terminalFromReceptorToolWindow)
1226 d_terminalFromReceptorToolWindow->resetData();
1227 removeToolWindow(d_terminalFromReceptorToolWindow);