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 "dccreator.h"
23 #include "dccontent.h"
24 #include "dccontainer.h"
29 #include "dcaxonterminal.h"
30 #include "dcreceptor.h"
32 #include "command/dccommandevent.h"
33 #include "command/dccommand.h"
35 #include "utils/dcdialogutil.h"
36 #include "utils/dccommandutil.h"
37 #include "dialog/dcinputreceptornamedialog.h"
39 #include "dctoolwindowcellcodeeditor.h"
41 #include <QUndoCommand>
44 DCCreator::DCCreator(QMainWindow *mainwindow)
45 : d_mainWindow(mainwindow), d_vcontent(NULL), d_scene(NULL),
46 d_persMode(DC_PERSMODE_PAGEEDIT), d_contentRootPath("")
48 d_undoStack = new QUndoStack(this);
49 d_cellCodeEditor = new DCToolWindowCellCodeEditor(this);
52 DCCreator::~DCCreator()
60 bool DCCreator::event(QEvent *event)
62 if(event->type() == DCCommandEvent::EVENT_TYPEID)
64 DCCommandEvent *commandEvent = dynamic_cast<DCCommandEvent*>(event);
67 QUndoCommand *command = commandEvent->getCommand();
70 d_undoStack->push(command);
71 emit commandExecuted(command);
76 else if (event->type() == DCUndoEvent::EVENT_TYPEID)
79 emit commandExecuted(NULL);
85 void DCCreator::resetVisualizer()
87 emit sceneChanged(this,0);
90 void DCCreator::initMode()
92 d_persMode = DC_PERSMODE_NAVIGATION;
93 d_scene->initMode(DCScene::DCV_PERSMODE_NAVIGATION, DCScene::DCV_EDITMODE_LAYOUT);
96 bool DCCreator::loadContent(const QString &contentRoot)
98 d_contentRootPath = contentRoot;
102 d_scene->disconnect(this);
106 d_vcontent = new DCContent(this, contentRoot.toStdString());
108 if (d_vcontent->isValid())
110 d_scene = d_vcontent->getContainer()->getScene();
112 d_scene->loadSceneAll();
114 //setup signal - slot connection for scene
115 connect(d_scene, SIGNAL(selectedCellObjectChanged(const void*)), this, SLOT(slotSceneSelectedCellObjectChanged(const void*)));
116 connect(d_scene, SIGNAL(selectedPageChanged(const void*)), this, SLOT(slotSceneSelectedPageChanged(const void*)));
117 connect(d_scene, SIGNAL(viewAngleChanged(const void*)), this, SLOT(slotSceneViewAngleChanged(const void*)));
118 connect(d_scene, SIGNAL(viewCenterChanged(const void*)), this, SLOT(slotSceneViewCenterChanged(const void*)));
119 connect(d_scene, SIGNAL(viewScaleChanged(const void*)), this, SLOT(slotSceneViewScaleChanged(const void*)));
120 connect(d_scene, SIGNAL(viewSettingChanged(const void*)), this, SLOT(slotSceneViewSettingChanged(const void*)));
121 connect(d_scene, SIGNAL(viewEditModeChanged(const void*)), this, SLOT(slotSceneViewEditModeChanged(const void*)));
123 emit contentRootPathChanged(this, d_contentRootPath);
124 emit sceneChanged(this, d_scene);
127 getMainWindow()->statusBar()->showMessage(tr("Content loaded"), 2000);
134 emit sceneChanged(this, NULL);
139 bool DCCreator::savePage(DCVCPage *page, bool showResultInMessageBox)
142 if (d_scene && d_vcontent)
144 if (d_scene->getEditMode() == DCScene::DCV_EDITMODE_CELLCODE)
146 if (d_scene->getEditCellCodeCell()->getPageBelonging() == page)
148 getCellCodeEditor()->saveScriptToFile();
151 r = d_vcontent->saveForPage(d_vcontent->getContentRootPath(), page);
153 if (showResultInMessageBox)
158 msgBox.setText(tr("Page is saved successfully"));
162 msgBox.setText(tr("Error!! Failed to save the page file"));
169 bool DCCreator::saveAll(bool showResultInMessageBox)
173 if (getCellCodeEditor()->getIsOnStage())
174 getCellCodeEditor()->saveScriptToFile();
177 if (d_scene && d_vcontent)
179 r = d_vcontent->saveAll(d_vcontent->getContentRootPath());
181 if (showResultInMessageBox)
186 msgBox.setText(tr("Content is saved successfully"));
190 msgBox.setText(tr("Error!! Failed to save content files"));
194 getMainWindow()->statusBar()->showMessage(tr("Content saved"), 2000);
200 void DCCreator::selectPage(const void *requester, DCVCPage *page, bool multipleSelection)
204 d_scene->selectPage(requester, page, multipleSelection);
208 void DCCreator::unselectPage(const void *requester, DCVCPage *page)
212 d_scene->unselectPage(requester, page);
216 bool DCCreator::selectPage(const void *requester, const QString &locationPath, bool multipleSelection)
221 r = d_scene->selectPage(requester, locationPath, multipleSelection);
226 bool DCCreator::unselectPage(const void *requester, const QString &locationPath)
231 r = d_scene->unselectPage(requester, locationPath);
237 void DCCreator::unselectPageAll(const void *requester)
241 d_scene->unselectPageAll(requester);
245 void DCCreator::selectCellObject(const void *requester, DCVComponent *object, bool multipleSelection)
248 d_scene->selectCellObject(requester, object, multipleSelection);
251 void DCCreator::unselectCellObject(const void *requester, DCVComponent *object)
254 d_scene->unselectCellObject(requester, object);
257 void DCCreator::unselectCellObjectAll(const void *requester)
260 d_scene->unselectCellObjectAll(requester);
263 void DCCreator::changeSceneScale(const void *requester, float scale)
267 if (scale <= 10) scale =10;
268 if (scale >= 1000) scale = 1000;
269 d_scene->setScale(requester, scale);
273 void DCCreator::rotateScene(const void *requester, float xangle_delta, float yangle_delta)
277 float xangle = d_scene->getXAngle() + xangle_delta;
278 if (xangle < 0) xangle = 0;
279 if (xangle > 90) xangle = 90;
281 float yangle = d_scene->getYAngle() + yangle_delta;
282 while (yangle < 0) yangle += 360;
283 while (yangle > 360) yangle -= 360;
285 d_scene->setAngle(requester, xangle,yangle);
289 void DCCreator::translateBrowsModeScene(const void *requester, float dx, float dy)
293 float scale = d_scene->getScale();
294 float x = d_scene->getCenterX() - dx/scale*2;
295 float y = d_scene->getCenterBrowsModeY() + dy/scale*2;
296 d_scene->setBrowsModeCenter(requester, x,y);
300 void DCCreator::translateEditModeScene(const void *requester, float dx, float dy)
304 float scale = d_scene->getScale();
305 float x = d_scene->getCenterX() - dx/scale*2;
306 float y = d_scene->getCenterEditModeY() - dy/scale*2;
307 d_scene->setPageModeCenter(requester, x,y);
311 void DCCreator::resetSceneTranslation()
316 bool DCCreator::changePersMode(const void *requester, DCPersMode mode)
324 case DC_PERSMODE_NAVIGATION:
325 r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_NAVIGATION);
327 case DC_PERSMODE_PAGEEDIT:
328 r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_PAGEEDIT);
331 d_scene->updateVisiblity();
336 void DCCreator::doUndo(const void *requester)
341 void DCCreator::doUndoImmidiate()
346 void DCCreator::doCommandStartAddAxonTerminalFromAxon(const void *requester, DCCell *axonCell)
348 DCCommandUtil::postStartAddAxonTerminalCommandFromAxon(requester, this, axonCell);
351 void DCCreator::doCommandStartAddAxonTerminalFromReceptor(const void *requester, DCCell *receptorCell)
353 DCInputReceptorNameDialog dialog(receptorCell);
355 if (dialog.getIsOk() && dialog.getInputText().length() > 0)
357 DCCommandUtil::postStartAddAxonTerminalCommandFromReceptor(requester, this, receptorCell, dialog.getInputText());
361 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCAxon *axon, DCCell *receptorCell)
363 DCInputReceptorNameDialog dialog(receptorCell);
365 if (dialog.getIsOk() && dialog.getInputText().length() > 0)
367 DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axon, receptorCell, dialog.getInputText());
371 doCommandCancelAddAxonTerminal(requester);
375 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCCell *axonCell, DCReceptor *receptor)
377 DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axonCell, receptor);
380 void DCCreator::doCommandCancelAddAxonTerminal(const void *requester)
383 // may need to consider the case multiple commands are executed after
384 // startAddAxon command is executed.
385 DCCommandUtil::postUndoRequestCommand(requester, this);
388 void DCCreator::doCommandRemoveAxonTerminal(const void *requester, DCCell *axonCell, DCAxonTerminal *axonTerminal)
390 DCReceptor *receptor = axonTerminal->getTarget();
394 DCCell *targetCell = receptor->getOwnerCell();
399 msgBox.setText(tr("Delete axon terminal"));
402 msg.append(QString::fromStdString(axonCell->getName()));
403 msg.append("\nDo you want to delete the connection to ");
404 msg.append(QString::fromStdString(targetCell->getLocation()));
406 msg.append(QString::fromStdString(targetCell->getName()));
408 msg.append(QString::fromStdString(targetCell->getReceptorName(receptor)));
410 msgBox.setInformativeText(msg);
411 msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
412 msgBox.setDefaultButton(QMessageBox::Cancel);
413 int ret = msgBox.exec();
415 if (ret == QMessageBox::Yes)
417 DCCommandUtil::postRemoveAxonTerminalCommand(requester, this, axonCell, axonTerminal);
421 void DCCreator::doCommandRemoveAxonTerminal(const void *requester, DCCell *receptorCell, const QString& receptorName)
425 msgBox.setText(tr("Delete receptor"));
428 msg.append(QString::fromStdString(receptorCell->getName()));
429 msg.append("\nDo you want to delete receptor ");
430 msg.append(receptorName);
432 msgBox.setInformativeText(msg);
433 msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
434 msgBox.setDefaultButton(QMessageBox::Cancel);
435 int ret = msgBox.exec();
437 if (ret == QMessageBox::Yes)
439 DCCommandUtil::postRemoveAxonTerminalCommand(requester, this, receptorCell, receptorName);
443 void DCCreator::doCommandStartEditCellCode(const void *requester, DCCell *cell)
445 DCCommandUtil::postStartCellCodeEditCommand(requester, this, cell);
448 void DCCreator::doCommandFinishEditCellCode(const void *requester)
450 DCCommandUtil::postFinishCellCodeEditCommand(requester, this);
453 void DCCreator::doCommandAssignCellCodeClassToCell(const void *requester, DCCell *cell, DCCellCode *cellCode)
455 DCCommandUtil::postAssignCellCodeClassToCellCommand(requester, this, cell, cellCode);
458 void DCCreator::doCommandUnassignCellCodeClassFromCell(const void *requester, DCCell *cell)
460 DCCommandUtil::postUnassignCellCodeClassFromCellCommand(requester, this, cell);
463 void DCCreator::doCommandAddCellCodeClass(const void *requester, DCContainer *container, const QString& name, const QString& type)
465 DCCommandUtil::postAddCellCodeClassCommand(requester, this, container, name, type);
468 void DCCreator::doCommandChangeCellCodeClassType(const void *requester, DCCellCode* cellCode, const QString &newType)
470 DCCommandUtil::postChangeCellCodeClassTypeCommand(requester, this, cellCode, newType);
473 void DCCreator::doCommandAddCell(const void *requester, DCContainer *container, const QString &containerBasedPath, const QString &name, const QString &type, float pageX, float pageY)
475 DCCommandUtil::postAddCellCommand(requester, this, container, containerBasedPath, name, type, pageX, pageY);
478 void DCCreator::doCommandChangeCellType(const void *requester, DCCell *cell, const QString &newType)
480 DCCommandUtil::postChangeCellTypeCommand(requester, this, cell, newType);
483 void DCCreator::doCommandRenameCell(const void *requester, DCCell *cell, const QString &newContainerBasedPath, const QString &newName)
485 DCCommandUtil::postRenameCellCommand(requester, this, cell, newContainerBasedPath, newName);
488 void DCCreator::doCommandRemoveCell(const void *requester, DCContainer *container, DCCell *cell)
490 DCCommandUtil::postRemoveCellCommand(requester, this, container, cell);
493 void DCCreator::doCommandAddPage(const void *requester, const QString &containerBasedPath)
495 DCCommandUtil::postAddPageCommand(requester, this, containerBasedPath);
498 void DCCreator::doCommandMovePage(const void *requester, const QString &oldContainerBasedPath, const QString &newContainerBasedPath)
500 DCCommandUtil::postMovePageCommand(requester, this, oldContainerBasedPath, newContainerBasedPath);
503 void DCCreator::doCommandRemovePage(const void *requester, DCVCPage *page)
505 DCCommandUtil::postRemovePageCommand(requester, this, page);
508 void DCCreator::doCommandAddDirectory(const void *requester, const QString &sysFilePath)
510 DCCommandUtil::postAddDirectoryCommand(requester, this, sysFilePath);
513 void DCCreator::doCommandRenameDirectory(const void *requester, const QString &oldSysFilePath, const QString &newSysFilePath)
515 DCCommandUtil::postRenameDirectoryCommand(requester, this, oldSysFilePath, newSysFilePath);
518 void DCCreator::doCommandRemoveDirectory(const void *requester, const QString &sysFilePath)
520 DCCommandUtil::postRemoveDirectoryCommand(requester, this, sysFilePath);
523 bool DCCreator::doCommandRenameReceptorName(const void *requester, DCCell *cell, const QString &oldName, const QString &newName, bool doImmediate)
527 DCCommand *command = DCCommandUtil::createRenameReceptorNameCommand(requester, this, cell, oldName, newName);
528 d_undoStack->push(command);
529 return command->getCommandResult() == DCCommand::DCCOMMAND_SUCCEEDED;
533 DCCommandUtil::postRenameReceptorNameCommand(requester, this, cell, oldName, newName);
538 DCContainer* DCCreator::getCurrentContainer() const
541 return d_vcontent->getContainer();
546 void DCCreator::slotSceneSelectedPageChanged(const void *requester)
548 emit sceneSelectedPageChanged(requester, d_scene);
551 void DCCreator::slotSceneSelectedCellObjectChanged(const void *requester)
553 emit sceneSelectedCellChanged(requester, d_scene);
556 void DCCreator::slotSceneViewScaleChanged(const void *requester)
558 emit sceneViewScaleChanged(requester, d_scene);
561 void DCCreator::slotSceneViewAngleChanged(const void *requester)
563 emit sceneViewAngleChanged(requester, d_scene);
566 void DCCreator::slotSceneViewCenterChanged(const void *requester)
568 emit sceneViewCenterChanged(requester, d_scene);
571 void DCCreator::slotSceneViewSettingChanged(const void *requester)
573 emit sceneViewSettingChanged(requester, d_scene);
576 void DCCreator::slotSceneViewEditModeChanged(const void *requester)
578 emit sceneViewEditModeChanged(requester, d_scene);