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.
19 #include "dcvlayoutmodehandler.h"
21 #include "dcglvisualizerwidget.h"
22 #include "dcvcomponent.h"
26 #include "dccreator.h"
27 #include "dcglwidget.h"
29 #include "dccontainer.h"
32 #include "dialog/dcaddcelldialog.h"
33 #include "dialog/dcinputnewpagenamedialog.h"
34 #include "dialog/dcrenamecelldialog.h"
35 #include "codeeditor/dccellscriptseditorwindow.h"
40 DCVLayoutModeHandler::DCVLayoutModeHandler(DCUIGraphicsScene *widget, DCCreator *creator, DCScene *scene) :
41 DCVEventHandler(widget, creator, scene), d_inResigingDrag(false)
46 DCVLayoutModeHandler::~DCVLayoutModeHandler()
51 int DCVLayoutModeHandler::getHandlerType() const
53 return DCScene::DCV_EDITMODE_LAYOUT;
56 void DCVLayoutModeHandler::mousePressEvent(QGraphicsSceneMouseEvent *event)
58 bool shift = event->modifiers() & Qt::ShiftModifier;
60 QPointF mousePos = event->scenePos();
62 DCVComponent *obj = getView()->getObjectAt(mousePos.x(), mousePos.y());
66 if (!obj->getIsSelectable())
69 DCVCPage *page = dynamic_cast<DCVCPage*>(obj->getPageBelonging());
73 switch(getScene()->getPersMode())
75 case DCScene::DCV_PERSMODE_NAVIGATION:
80 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), page, &pageX, &pageZ);
81 getController()->selectPage(this, page, shift);
82 getController()->selectCellObject(this, obj);
83 if (obj->startDrag(pageX, 0, pageZ, false))
85 getView()->requestRedraw(true);
86 d_draggedObject.assign(obj);
87 d_inResigingDrag = false;
92 d_draggedObject.assign(obj);
93 d_inResigingDrag = false;
98 case DCScene::DCV_PERSMODE_PAGEEDIT:
102 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), obj, &objX, &objZ);
103 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), page, &pageX, &pageZ);
104 d_inResigingDrag = obj->isResizingArea(objX,0,objZ);
105 getController()->selectCellObject(this, obj);
106 if (obj->startDrag(pageX, 0, pageZ, d_inResigingDrag))
108 getView()->requestRedraw(true);
110 d_draggedObject.assign(obj);
118 void DCVLayoutModeHandler::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
120 DCVComponent *obj = d_draggedObject.ref();
125 if (obj->getOwnerCell() && !obj->getOwnerCell()->getVComponent()->getIsSelectable())
128 DCVComponent *page = obj->getPageBelonging();
132 switch(getScene()->getPersMode())
134 case DCScene::DCV_PERSMODE_NAVIGATION:
136 getController()->changePersMode(this, DCCreator::DC_PERSMODE_PAGEEDIT);
137 d_draggedObject.assign(NULL);
142 case DCScene::DCV_PERSMODE_PAGEEDIT:
146 getController()->changePersMode(this, DCCreator::DC_PERSMODE_NAVIGATION);
147 d_draggedObject.assign(NULL);
151 if (typeid(*obj) == typeid(DCVCAxon))
153 getController()->doCommandStartAddAxonTerminalFromAxon(this, obj->getOwnerCell());
157 DCCellScriptsEditorWindow::startEditing(d_draggedObject.ref()->getOwnerCell());
167 void DCVLayoutModeHandler::mouseMoveEvent(QGraphicsSceneMouseEvent *event, int clickTimeElapsed)
169 QPointF mousePos = event->scenePos();
170 QPointF mousePrevPos = event->lastScenePos();
172 // This method will be called continuously while the mouse is moving.
173 // Some process we want to execute here may become heavyer and it will affect the performance.
174 // To privent this to affect the performance, we only execute the potentially heavyer
175 // process 12.5 times in a second in maximum.
176 static int lastCheckTime = 0;
177 bool canProcessHeavyTask = clickTimeElapsed - lastCheckTime < 0 || clickTimeElapsed - lastCheckTime >= 80;
178 if (canProcessHeavyTask)
179 lastCheckTime = clickTimeElapsed;
182 if (event->buttons() == Qt::LeftButton)
185 bool shift = event->modifiers() & Qt::ShiftModifier;
186 float dx = mousePos.x() - mousePrevPos.x();
187 float dy = mousePos.y() - mousePrevPos.y();
189 switch(getScene()->getPersMode())
191 case DCScene::DCV_PERSMODE_NAVIGATION:
192 if (getScene()->getNavModeSelectedCellMovable() && d_draggedObject.ref() && d_draggedObject.ref()->getPageBelonging() && d_draggedObject.ref() != d_draggedObject.ref()->getPageBelonging())
195 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), d_draggedObject.ref()->getPageBelonging(), &pageX, &pageZ);
197 if (d_draggedObject.ref()->dragging(pageX, 0, pageZ, false))
199 DCGLWidget::singleton()->setCursor(Qt::OpenHandCursor);
200 getView()->requestRedraw(false);
205 getController()->rotateScene(this, dy / 5.0, dx / 5.0);
209 getController()->translateBrowsModeScene(this, dx, dy);
213 case DCScene::DCV_PERSMODE_PAGEEDIT:
214 if (d_draggedObject.ref() && d_draggedObject.ref()->getPageBelonging())
216 if (d_draggedObject.ref() == d_draggedObject.ref()->getPageBelonging())
218 getController()->translateEditModeScene(this, dx, dy);
223 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), d_draggedObject.ref()->getPageBelonging(), &pageX, &pageZ);
224 if (d_draggedObject.ref()->dragging(pageX, 0, pageZ, d_inResigingDrag))
226 if (d_inResigingDrag)
228 DCGLWidget::singleton()->setCursor(Qt::SizeFDiagCursor);
232 DCGLWidget::singleton()->setCursor(Qt::OpenHandCursor);
234 getView()->requestRedraw(false);
244 if (canProcessHeavyTask)
246 DCVComponent *obj = getView()->getObjectAt(mousePos.x(), mousePos.y());
249 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), obj, &objX, &objZ);
250 if (obj && obj->isResizingArea(objX,0,objZ))
252 DCGLWidget::singleton()->setCursor(Qt::SizeFDiagCursor);
256 DCGLWidget::singleton()->setCursor(Qt::ArrowCursor);
264 void DCVLayoutModeHandler::mouseReleaseEvent(QGraphicsSceneMouseEvent *event, bool wasDrag)
266 bool shift = event->modifiers() & Qt::ShiftModifier;
267 QPointF mousePos = event->scenePos();
269 switch(getScene()->getPersMode())
271 case DCScene::DCV_PERSMODE_NAVIGATION:
273 DCVCPage *page = NULL;
274 if (d_draggedObject.ref())
276 page = d_draggedObject.ref()->getPageBelonging();
278 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), page, &pageX, &pageZ);
279 if (d_draggedObject.ref()->endDrag(pageX, 0, pageZ, d_inResigingDrag))
281 getView()->requestRedraw(true);
289 getController()->selectPage(this, page, shift);
293 getController()->unselectPageAll(this);
299 case DCScene::DCV_PERSMODE_PAGEEDIT:
300 if (d_draggedObject.ref() && d_draggedObject.ref()->getPageBelonging())
303 getView()->toObjectCoordinateXZ(mousePos.x(), mousePos.y(), d_draggedObject.ref()->getPageBelonging(), &pageX, &pageZ);
304 if (d_draggedObject.ref()->endDrag(pageX, 0, pageZ, d_inResigingDrag))
306 getView()->requestRedraw(true);
314 void DCVLayoutModeHandler::wheelEvent(QGraphicsSceneWheelEvent *event)
316 if (event->isAccepted()) return;
318 getController()->changeSceneScale(this, getScene()->getScale() + event->delta()/5);
323 void DCVLayoutModeHandler::keyPressEvent(QKeyEvent *event)
330 switch(getScene()->getPersMode())
332 case DCScene::DCV_PERSMODE_NAVIGATION:
333 if (getScene()->getSelectedPages().length() > 0)
335 getController()->changePersMode(this, DCCreator::DC_PERSMODE_PAGEEDIT);
339 case DCScene::DCV_PERSMODE_PAGEEDIT:
340 if (getScene()->getSelectedCellObjects().length() == 1)
342 DCCellScriptsEditorWindow::startEditing(getScene()->getSelectedCellObjects().at(0)->getOwnerCell());
352 switch(getScene()->getPersMode())
354 case DCScene::DCV_PERSMODE_NAVIGATION:
355 getController()->unselectPageAll(this);
358 case DCScene::DCV_PERSMODE_PAGEEDIT:
359 getController()->changePersMode(this, DCCreator::DC_PERSMODE_NAVIGATION);
367 void DCVLayoutModeHandler::keyReleaseEvent(QKeyEvent *event)
372 void DCVLayoutModeHandler::showContextMenu(const QPoint &pos)
374 QPoint globalPos = DCGLWidget::singleton()->mapToGlobal(pos);
376 QAction *actionRenameCell = NULL;
377 QAction *actinAddPage = NULL;
378 QAction *actionEditPage = NULL;
379 QAction *actionRemovePage = NULL;
380 QAction *actionSavePage = NULL;
381 QAction *actionSaveAll = NULL;
382 QAction *actionEditCellCode = NULL;
383 QAction *actionAddCell = NULL;
384 QAction *actionRemoveCell = NULL;
385 QAction *actionFinishEditMode = NULL;
387 DCCell* selectedCell = NULL;
388 DCVCPage* selectedPage = NULL;
390 DCVComponent *obj = getView()->getObjectAt(pos.x(), pos.y());
391 if (obj && obj->getIsSelectable())
393 selectedCell = obj->getOwnerCell();
394 selectedPage = obj->getPageBelonging();
398 switch(getScene()->getPersMode())
400 case DCScene::DCV_PERSMODE_NAVIGATION:
401 actionAddCell = menu.addAction(tr("Add cell ..."));
404 actionRenameCell = menu.addAction(tr("Rename / move cell..."));
406 actionRemoveCell = menu.addAction(tr("Remove cell"));
409 actinAddPage = menu.addAction(tr("Add page ..."));
412 actionEditPage = menu.addAction(tr("Edit page ..."));
413 actionRemovePage = menu.addAction(tr("Remove page ..."));
414 actionSavePage = menu.addAction(tr("Save page"));
417 actionSaveAll = menu.addAction(tr("Save all"));
421 case DCScene::DCV_PERSMODE_PAGEEDIT:
423 actionAddCell = menu.addAction(tr("Add cell ..."));
426 actionRenameCell = menu.addAction(tr("Rename / move cell..."));
427 actionEditCellCode = menu.addAction(tr("Edit cell code..."));
429 actionRemoveCell = menu.addAction(tr("Remove cell"));
433 actionSavePage = menu.addAction(tr("Save page"));
435 actionSaveAll = menu.addAction(tr("Save all"));
437 actionFinishEditMode = menu.addAction(tr("Finish edit mode"));
442 QAction* selectedItem = menu.exec(globalPos);
445 DCContainer *container = getController()->getCurrentContainer();
446 if (selectedItem == actinAddPage)
448 QString sysFilePath = QString::fromStdString(container->getContainerRootPath());
452 sysFilePath = QFileInfo(sysFilePath + selectedPage->getLocationPath()).dir().absolutePath();
454 QString containerBasedPath = container->sysFilePathToContainerBasedPath(sysFilePath);
456 DCInputNewPageNameDialog dialog(sysFilePath, tr("Add page"));
459 QString pageName = dialog.getName();
460 if (pageName.length() > 0)
462 QString pathName = containerBasedPath;
463 if (containerBasedPath.length()>1)
464 pathName.append("/");
465 pathName.append(pageName);
466 getController()->doCommandAddPage(this, pathName);
470 else if (selectedItem == actionRenameCell)
474 DCRenameCellDialog dialog(getController()->getCurrentContainer(), getController(), selectedCell);
479 else if (selectedItem == actionEditPage)
481 getController()->changePersMode(this, DCCreator::DC_PERSMODE_PAGEEDIT);
483 else if (selectedItem == actionRemovePage)
485 if (getScene()->getSelectedPages().length() > 0)
487 QList<DCVCPage*> pages = getScene()->getSelectedPages();
488 for (int i = 0; i < pages.length(); i++)
490 getController()->doCommandRemovePage(this, pages.at(i));
494 else if (selectedItem == actionSavePage)
496 if (getScene()->getSelectedPages().length() > 0)
499 QList<DCVCPage*> pages = getScene()->getSelectedPages();
500 if (!getController()->savePage(pages.toSet()))
507 msgBox.setText(tr("Page is saved successfully"));
511 msgBox.setText(tr("Error!! Failed to save the page file"));
516 else if (selectedItem == actionSaveAll)
518 getController()->saveAll(true);
521 else if (selectedItem == actionEditCellCode)
525 DCCellScriptsEditorWindow::startEditing(selectedCell);
528 else if (selectedItem == actionAddCell)
534 getView()->toObjectCoordinateXZ(pos.x(), pos.y(),selectedPage, &pageX, &pageY );
535 DCAddCellDialog dialog(getController()->getCurrentContainer(), getController(), selectedPage->getLocationPath(), pageX, pageY);
539 else if (selectedItem == actionRemoveCell)
541 getController()->doCommandRemoveCell(this, getController()->getCurrentContainer(), selectedCell);
543 else if (selectedItem == actionFinishEditMode)
545 getController()->changePersMode(this, DCCreator::DC_PERSMODE_NAVIGATION);
550 // nothing was chosen
554 void DCVLayoutModeHandler::selectedPageChanged(const void *requester)