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 "dcconsole.h"
23 #include "dccreator.h"
25 #include "dccontent.h"
26 #include "dccontainer.h"
31 #include "dcaxonterminal.h"
32 #include "dcreceptor.h"
34 #include "command/dccommandevent.h"
35 #include "command/dccommand.h"
37 #include "utils/dcdialogutil.h"
38 #include "utils/dccommandutil.h"
39 #include "dialog/dcinputreceptornamedialog.h"
40 #include "codeeditor/dccellscriptseditorwindow.h"
42 #include <QUndoCommand>
45 DCCreator::DCCreator(QMainWindow *mainwindow)
46 : d_mainWindow(mainwindow), d_vcontent(NULL), d_scene(NULL),
47 d_persMode(DC_PERSMODE_PAGEEDIT), d_contentRootPath("")
49 d_undoStack = new QUndoStack(this);
51 d_console = new DCConsole;
52 TKLog::setDestination(d_console);
55 DCCreator::~DCCreator()
57 emit sceneChanged(this, NULL);
70 bool DCCreator::event(QEvent *event)
75 if(event->type() == DCCommandEvent::EVENT_TYPEID)
77 DCCommandEvent *commandEvent = dynamic_cast<DCCommandEvent*>(event);
80 QUndoCommand *command = commandEvent->getCommand();
83 d_undoStack->push(command);
84 emit commandExecuted(command);
89 else if (event->type() == DCUndoEvent::EVENT_TYPEID)
92 emit commandExecuted(NULL);
98 void DCCreator::resetVisualizer()
100 emit sceneChanged(this,0);
103 void DCCreator::initMode()
105 d_persMode = DC_PERSMODE_NAVIGATION;
106 d_scene->initMode(DCScene::DCV_PERSMODE_NAVIGATION, DCScene::DCV_EDITMODE_LAYOUT);
109 bool DCCreator::loadContent(const QString &contentRoot)
111 d_console->clearLog();
113 d_contentRootPath = contentRoot;
117 d_scene->disconnect(this);
121 d_vcontent = new DCContent(this, contentRoot.toStdString());
123 if (d_vcontent->isValid())
125 d_scene = d_vcontent->getContainer()->getScene();
127 d_scene->loadSceneAll();
129 //setup signal - slot connection for scene
130 connect(d_scene, SIGNAL(selectedCellObjectChanged(const void*)), this, SLOT(slotSceneSelectedCellObjectChanged(const void*)));
131 connect(d_scene, SIGNAL(selectedPageChanged(const void*)), this, SLOT(slotSceneSelectedPageChanged(const void*)));
132 connect(d_scene, SIGNAL(viewAngleChanged(const void*)), this, SLOT(slotSceneViewAngleChanged(const void*)));
133 connect(d_scene, SIGNAL(viewCenterChanged(const void*)), this, SLOT(slotSceneViewCenterChanged(const void*)));
134 connect(d_scene, SIGNAL(viewScaleChanged(const void*)), this, SLOT(slotSceneViewScaleChanged(const void*)));
135 connect(d_scene, SIGNAL(viewSettingChanged(const void*)), this, SLOT(slotSceneViewSettingChanged(const void*)));
136 connect(d_scene, SIGNAL(viewEditModeChanged(const void*)), this, SLOT(slotSceneViewEditModeChanged(const void*)));
138 emit contentRootPathChanged(this, d_contentRootPath);
139 emit sceneChanged(this, d_scene);
142 getMainWindow()->statusBar()->showMessage(tr("Content loaded"), 2000);
149 emit contentRootPathChanged(this, d_contentRootPath);
150 emit sceneChanged(this, NULL);
152 if (!dnGlobal()->isErrorStatusNormal())
154 QMessageBox msgBox(QMessageBox::Warning, QString::fromStdString(dnGlobal()->getMessage1()), QString::fromStdString(dnGlobal()->getMessage2()));
155 msgBox.setInformativeText(d_console->getLog(10));
162 bool DCCreator::savePage(DCVCPage *page, bool showResultInMessageBox)
165 if (d_scene && d_vcontent)
167 DCCellScriptsEditorWindow::saveScriptsBelongTo(page, true);
168 r = d_vcontent->saveForPage(d_vcontent->getContentRootPath(), page);
170 if (showResultInMessageBox)
175 msgBox.setText(tr("Page is saved successfully"));
179 msgBox.setText(tr("Error!! Failed to save the page file"));
186 bool DCCreator::savePage(const QSet<DCVCPage*> &pages, bool showResultInMessageBox)
189 QSet<DCVCPage*>::const_iterator i = pages.constBegin();
190 while (i != pages.constEnd())
192 if (!savePage(*i, showResultInMessageBox))
201 bool DCCreator::saveAll(bool showResultInMessageBox)
205 if (DCCellScriptsEditorWindow::getIsVisible())
207 DCCellScriptsEditorWindow::saveScriptsAll(true);
210 if (d_scene && d_vcontent)
212 r = d_vcontent->saveAll(d_vcontent->getContentRootPath());
214 if (showResultInMessageBox)
219 msgBox.setText(tr("Content is saved successfully"));
223 msgBox.setText(tr("Error!! Failed to save content files"));
227 getMainWindow()->statusBar()->showMessage(tr("Content saved"), 2000);
233 void DCCreator::selectPage(const void *requester, DCVCPage *page, bool multipleSelection)
237 d_scene->selectPage(requester, page, multipleSelection);
241 void DCCreator::unselectPage(const void *requester, DCVCPage *page)
245 d_scene->unselectPage(requester, page);
249 bool DCCreator::selectPage(const void *requester, const QString &locationPath, bool multipleSelection)
254 r = d_scene->selectPage(requester, locationPath, multipleSelection);
259 bool DCCreator::unselectPage(const void *requester, const QString &locationPath)
264 r = d_scene->unselectPage(requester, locationPath);
270 void DCCreator::unselectPageAll(const void *requester)
274 d_scene->unselectPageAll(requester);
278 void DCCreator::selectCellObject(const void *requester, DCVComponent *object, bool multipleSelection)
281 d_scene->selectCellObject(requester, object, multipleSelection);
284 void DCCreator::unselectCellObject(const void *requester, DCVComponent *object)
287 d_scene->unselectCellObject(requester, object);
290 void DCCreator::unselectCellObjectAll(const void *requester)
293 d_scene->unselectCellObjectAll(requester);
296 void DCCreator::changeSceneScale(const void *requester, float scale)
300 if (scale <= 10) scale =10;
301 if (scale >= 1000) scale = 1000;
302 d_scene->setScale(requester, scale);
306 void DCCreator::rotateScene(const void *requester, float xangle_delta, float yangle_delta)
310 float xangle = d_scene->getXAngle() + xangle_delta;
311 if (xangle < 0) xangle = 0;
312 if (xangle > 90) xangle = 90;
314 float yangle = d_scene->getYAngle() + yangle_delta;
315 while (yangle < 0) yangle += 360;
316 while (yangle > 360) yangle -= 360;
318 d_scene->setAngle(requester, xangle,yangle);
322 void DCCreator::translateBrowsModeScene(const void *requester, float dx, float dy)
326 float scale = d_scene->getScale();
327 float x = d_scene->getCenterX() - dx/scale*2;
328 float y = d_scene->getCenterBrowsModeY() + dy/scale*2;
329 d_scene->setBrowsModeCenter(requester, x,y);
333 void DCCreator::translateEditModeScene(const void *requester, float dx, float dy)
337 float scale = d_scene->getScale();
338 float x = d_scene->getCenterX() - dx/scale*2;
339 float y = d_scene->getCenterEditModeY() - dy/scale*2;
340 d_scene->setPageModeCenter(requester, x,y);
344 void DCCreator::resetSceneTranslation()
349 bool DCCreator::changePersMode(const void *requester, DCPersMode mode)
357 case DC_PERSMODE_NAVIGATION:
358 r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_NAVIGATION);
360 case DC_PERSMODE_PAGEEDIT:
361 r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_PAGEEDIT);
364 d_scene->updateVisiblity();
369 void DCCreator::doUndo(const void *requester)
374 void DCCreator::doUndoImmidiate()
379 void DCCreator::doCommandStartAddAxonTerminalFromAxon(const void *requester, DCCell *axonCell)
381 DCCommandUtil::postStartAddAxonTerminalCommandFromAxon(requester, this, axonCell);
384 void DCCreator::doCommandStartAddAxonTerminalFromReceptor(const void *requester, DCCell *receptorCell)
386 DCInputReceptorNameDialog dialog(receptorCell);
388 if (dialog.getIsOk() && dialog.getInputText().length() > 0)
390 DCCommandUtil::postStartAddAxonTerminalCommandFromReceptor(requester, this, receptorCell, dialog.getInputText());
394 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCAxon *axon, DCCell *receptorCell)
396 DCInputReceptorNameDialog dialog(receptorCell);
398 if (dialog.getIsOk() && dialog.getInputText().length() > 0)
400 DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axon, receptorCell, dialog.getInputText());
404 doCommandCancelAddAxonTerminal(requester);
408 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCCell *axonCell, DCReceptor *receptor)
410 DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axonCell, receptor);
413 void DCCreator::doCommandCancelAddAxonTerminal(const void *requester)
416 // may need to consider the case multiple commands are executed after
417 // startAddAxon command is executed.
418 DCCommandUtil::postUndoRequestCommand(requester, this);
421 void DCCreator::doCommandRemoveAxonTerminal(const void *requester, DCCell *axonCell, DCAxonTerminal *axonTerminal)
423 DCReceptor *receptor = axonTerminal->getTarget();
427 DCCell *targetCell = receptor->getOwnerCell();
432 msgBox.setText(tr("Delete axon terminal"));
435 msg.append(QString::fromStdString(axonCell->getName()));
436 msg.append("\nDo you want to delete the connection to ");
437 msg.append(QString::fromStdString(targetCell->getLocation()));
439 msg.append(QString::fromStdString(targetCell->getName()));
441 msg.append(QString::fromStdString(targetCell->getReceptorName(receptor)));
443 msgBox.setInformativeText(msg);
444 msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
445 msgBox.setDefaultButton(QMessageBox::Cancel);
446 int ret = msgBox.exec();
448 if (ret == QMessageBox::Yes)
450 DCCommandUtil::postRemoveAxonTerminalCommand(requester, this, axonCell, axonTerminal);
454 void DCCreator::doCommandRemoveAxonTerminal(const void *requester, DCCell *receptorCell, const QString& receptorName)
458 msgBox.setText(tr("Delete receptor"));
461 msg.append(QString::fromStdString(receptorCell->getName()));
462 msg.append("\nDo you want to delete receptor ");
463 msg.append(receptorName);
465 msgBox.setInformativeText(msg);
466 msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
467 msgBox.setDefaultButton(QMessageBox::Cancel);
468 int ret = msgBox.exec();
470 if (ret == QMessageBox::Yes)
472 DCCommandUtil::postRemoveAxonTerminalCommand(requester, this, receptorCell, receptorName);
476 void DCCreator::doCommandAssignCellCodeClassToCell(const void *requester, DCCell *cell, DCCellCode *cellCode)
478 DCCommandUtil::postAssignCellCodeClassToCellCommand(requester, this, cell, cellCode);
481 void DCCreator::doCommandUnassignCellCodeClassFromCell(const void *requester, DCCell *cell)
483 DCCommandUtil::postUnassignCellCodeClassFromCellCommand(requester, this, cell);
486 void DCCreator::doCommandAddCellCodeClass(const void *requester, DCContainer *container, const QString& name, const QString& type)
488 DCCommandUtil::postAddCellCodeClassCommand(requester, this, container, name, type);
491 void DCCreator::doCommandChangeCellCodeClassType(const void *requester, DCCellCode* cellCode, const QString &newType)
493 DCCommandUtil::postChangeCellCodeClassTypeCommand(requester, this, cellCode, newType);
496 void DCCreator::doCommandAddCell(const void *requester, DCContainer *container, const QString &containerBasedPath, const QString &name, const QString &type, float pageX, float pageY)
498 DCCommandUtil::postAddCellCommand(requester, this, container, containerBasedPath, name, type, pageX, pageY);
501 void DCCreator::doCommandChangeCellType(const void *requester, DCCell *cell, const QString &newType)
503 DCCommandUtil::postChangeCellTypeCommand(requester, this, cell, newType);
506 void DCCreator::doCommandRenameCell(const void *requester, DCCell *cell, const QString &newContainerBasedPath, const QString &newName)
508 DCCommandUtil::postRenameCellCommand(requester, this, cell, newContainerBasedPath, newName);
511 void DCCreator::doCommandRemoveCell(const void *requester, DCContainer *container, DCCell *cell)
513 DCCommandUtil::postRemoveCellCommand(requester, this, container, cell);
516 void DCCreator::doCommandRemoveCellCode(const void *requester, DCContainer *container, DCCellCode *cellCode)
518 DCCommandUtil::postRemoveCellCodeCommand(requester, this, container, cellCode);
521 void DCCreator::doCommandAddPage(const void *requester, const QString &containerBasedPath)
523 DCCommandUtil::postAddPageCommand(requester, this, containerBasedPath);
526 void DCCreator::doCommandMovePage(const void *requester, const QString &oldContainerBasedPath, const QString &newContainerBasedPath)
528 DCCommandUtil::postMovePageCommand(requester, this, oldContainerBasedPath, newContainerBasedPath);
531 void DCCreator::doCommandRemovePage(const void *requester, DCVCPage *page)
533 DCCommandUtil::postRemovePageCommand(requester, this, page);
536 void DCCreator::doCommandAddDirectory(const void *requester, const QString &sysFilePath)
538 DCCommandUtil::postAddDirectoryCommand(requester, this, sysFilePath);
541 void DCCreator::doCommandRenameDirectory(const void *requester, const QString &oldSysFilePath, const QString &newSysFilePath)
543 DCCommandUtil::postRenameDirectoryCommand(requester, this, oldSysFilePath, newSysFilePath);
546 void DCCreator::doCommandRemoveDirectory(const void *requester, const QString &sysFilePath)
548 DCCommandUtil::postRemoveDirectoryCommand(requester, this, sysFilePath);
551 bool DCCreator::doCommandRenameReceptorName(const void *requester, DCCell *cell, const QString &oldName, const QString &newName, bool doImmediate)
555 DCCommand *command = DCCommandUtil::createRenameReceptorNameCommand(requester, this, cell, oldName, newName);
556 d_undoStack->push(command);
557 return command->getCommandResult() == DCCommand::DCCOMMAND_SUCCEEDED;
561 DCCommandUtil::postRenameReceptorNameCommand(requester, this, cell, oldName, newName);
566 DCContainer* DCCreator::getCurrentContainer() const
569 return d_vcontent->getContainer();
574 void DCCreator::slotSceneSelectedPageChanged(const void *requester)
576 emit sceneSelectedPageChanged(requester, d_scene);
579 void DCCreator::slotSceneSelectedCellObjectChanged(const void *requester)
581 emit sceneSelectedCellChanged(requester, d_scene);
584 void DCCreator::slotSceneViewScaleChanged(const void *requester)
586 emit sceneViewScaleChanged(requester, d_scene);
589 void DCCreator::slotSceneViewAngleChanged(const void *requester)
591 emit sceneViewAngleChanged(requester, d_scene);
594 void DCCreator::slotSceneViewCenterChanged(const void *requester)
596 emit sceneViewCenterChanged(requester, d_scene);
599 void DCCreator::slotSceneViewSettingChanged(const void *requester)
601 emit sceneViewSettingChanged(requester, d_scene);
604 void DCCreator::slotSceneViewEditModeChanged(const void *requester)
606 emit sceneViewEditModeChanged(requester, d_scene);