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 "utils/dcdialogutil.h"
34 #include "utils/dccommandutil.h"
35 #include "dialog/dcinputreceptornamedialog.h"
37 #include <QUndoCommand>
40 DCCreator::DCCreator(QMainWindow *mainwindow)
41 : d_mainWindow(mainwindow), d_vcontent(NULL), d_scene(NULL),
42 d_persMode(DC_PERSMODE_PAGEEDIT), d_contentRootPath("")
44 d_undoStack = new QUndoStack(this);
47 DCCreator::~DCCreator()
55 bool DCCreator::event(QEvent *event)
57 if(event->type() == DCCommandEvent::EVENT_TYPEID)
59 DCCommandEvent *commandEvent = dynamic_cast<DCCommandEvent*>(event);
62 QUndoCommand *command = commandEvent->getCommand();
65 d_undoStack->push(command);
66 emit commandExecuted(command);
71 else if (event->type() == DCUndoEvent::EVENT_TYPEID)
74 emit commandExecuted(NULL);
80 void DCCreator::resetVisualizer()
82 emit sceneChanged(this,0);
85 void DCCreator::initMode()
87 d_persMode = DC_PERSMODE_NAVIGATION;
88 d_scene->initMode(DCScene::DCV_PERSMODE_NAVIGATION, DCScene::DCV_EDITMODE_LAYOUT);
91 bool DCCreator::loadContent(const QString &contentRoot)
93 d_contentRootPath = contentRoot;
97 d_scene->disconnect(this);
101 d_vcontent = new DCContent(this, contentRoot.toStdString());
103 if (d_vcontent->isValid())
105 d_scene = d_vcontent->getContainer()->getScene();
107 d_scene->loadSceneAll();
109 //setup signal - slot connection for scene
110 connect(d_scene, SIGNAL(selectedCellObjectChanged(const void*)), this, SLOT(slotSceneSelectedCellObjectChanged(const void*)));
111 connect(d_scene, SIGNAL(selectedPageChanged(const void*)), this, SLOT(slotSceneSelectedPageChanged(const void*)));
112 connect(d_scene, SIGNAL(viewAngleChanged(const void*)), this, SLOT(slotSceneViewAngleChanged(const void*)));
113 connect(d_scene, SIGNAL(viewCenterChanged(const void*)), this, SLOT(slotSceneViewCenterChanged(const void*)));
114 connect(d_scene, SIGNAL(viewScaleChanged(const void*)), this, SLOT(slotSceneViewScaleChanged(const void*)));
115 connect(d_scene, SIGNAL(viewSettingChanged(const void*)), this, SLOT(slotSceneViewSettingChanged(const void*)));
116 connect(d_scene, SIGNAL(viewEditModeChanged(const void*)), this, SLOT(slotSceneViewEditModeChanged(const void*)));
118 emit contentRootPathChanged(this, d_contentRootPath);
119 emit sceneChanged(this, d_scene);
126 emit sceneChanged(this, NULL);
131 bool DCCreator::saveAll()
135 d_vcontent->saveAll(QString::fromStdString(getCurrentContainer()->getContainerRootPath()));
136 return d_scene->saveSceneAll();
142 void DCCreator::selectPage(const void *requester, DCVCPage *page, bool multipleSelection)
146 d_scene->selectPage(requester, page, multipleSelection);
150 void DCCreator::unselectPage(const void *requester, DCVCPage *page)
154 d_scene->unselectPage(requester, page);
158 bool DCCreator::selectPage(const void *requester, const QString &locationPath, bool multipleSelection)
163 r = d_scene->selectPage(requester, locationPath, multipleSelection);
168 bool DCCreator::unselectPage(const void *requester, const QString &locationPath)
173 r = d_scene->unselectPage(requester, locationPath);
179 void DCCreator::unselectPageAll(const void *requester)
183 d_scene->unselectPageAll(requester);
187 void DCCreator::selectCellObject(const void *requester, DCVComponent *object, bool multipleSelection)
190 d_scene->selectCellObject(requester, object, multipleSelection);
193 void DCCreator::unselectCellObject(const void *requester, DCVComponent *object)
196 d_scene->unselectCellObject(requester, object);
199 void DCCreator::unselectCellObjectAll(const void *requester)
202 d_scene->unselectCellObjectAll(requester);
205 void DCCreator::changeSceneScale(const void *requester, float scale)
209 if (scale <= 10) scale =10;
210 if (scale >= 1000) scale = 1000;
211 d_scene->setScale(requester, scale);
215 void DCCreator::rotateScene(const void *requester, float xangle_delta, float yangle_delta)
219 float xangle = d_scene->getXAngle() + xangle_delta;
220 if (xangle < 0) xangle = 0;
221 if (xangle > 90) xangle = 90;
223 float yangle = d_scene->getYAngle() + yangle_delta;
224 while (yangle < 0) yangle += 360;
225 while (yangle > 360) yangle -= 360;
227 d_scene->setAngle(requester, xangle,yangle);
231 void DCCreator::translateBrowsModeScene(const void *requester, float dx, float dy)
235 float scale = d_scene->getScale();
236 float x = d_scene->getCenterX() - dx/scale*2;
237 float y = d_scene->getCenterBrowsModeY() + dy/scale*2;
238 d_scene->setBrowsModeCenter(requester, x,y);
242 void DCCreator::translateEditModeScene(const void *requester, float dx, float dy)
246 float scale = d_scene->getScale();
247 float x = d_scene->getCenterX() - dx/scale*2;
248 float y = d_scene->getCenterEditModeY() - dy/scale*2;
249 d_scene->setPageModeCenter(requester, x,y);
253 void DCCreator::resetSceneTranslation()
258 bool DCCreator::changePersMode(const void *requester, DCPersMode mode)
266 case DC_PERSMODE_NAVIGATION:
267 r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_NAVIGATION);
269 case DC_PERSMODE_PAGEEDIT:
270 r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_PAGEEDIT);
273 d_scene->updateVisiblity();
278 void DCCreator::doUndo(const void *requester)
283 void DCCreator::doUndoImmidiate()
288 void DCCreator::doCommandStartAddAxonTerminalFromAxon(const void *requester, DCCell *axonCell)
290 DCCommandUtil::postStartAddAxonTerminalCommandFromAxon(requester, this, axonCell);
293 void DCCreator::doCommandStartAddAxonTerminalFromReceptor(const void *requester, DCCell *receptorCell)
295 DCInputReceptorNameDialog dialog(receptorCell);
297 if (dialog.getIsOk() && dialog.getInputText().length() > 0)
299 DCCommandUtil::postStartAddAxonTerminalCommandFromReceptor(requester, this, receptorCell, dialog.getInputText());
303 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCAxon *axon, DCCell *receptorCell)
305 DCInputReceptorNameDialog dialog(receptorCell);
307 if (dialog.getIsOk() && dialog.getInputText().length() > 0)
309 DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axon, receptorCell, dialog.getInputText());
313 doCommandCancelAddAxonTerminal(requester);
317 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCCell *axonCell, DCReceptor *receptor)
319 DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axonCell, receptor);
322 void DCCreator::doCommandCancelAddAxonTerminal(const void *requester)
325 // may need to consider the case multiple commands are executed after
326 // startAddAxon command is executed.
327 DCCommandUtil::postUndoRequestCommand(requester, this);
330 void DCCreator::doCommandDeleteAxonTerminal(const void *requester, DCCell *axonCell, DCAxonTerminal *axonTerminal)
332 DCReceptor *receptor = dynamic_cast<DCReceptor*>(axonTerminal->getTarget());
336 DCCell *targetCell = dynamic_cast<DCCell*>(receptor->getOwnerCell());
341 msgBox.setText(tr("Delete axon terminal"));
344 msg.append(QString::fromStdString(axonCell->getName()));
345 msg.append("\nDo you want to delete the connection to ");
346 msg.append(QString::fromStdString(targetCell->getLocation()));
348 msg.append(QString::fromStdString(targetCell->getName()));
350 msg.append(QString::fromStdString(targetCell->getReceptorName(receptor)));
352 msgBox.setInformativeText(msg);
353 msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
354 msgBox.setDefaultButton(QMessageBox::Cancel);
355 int ret = msgBox.exec();
357 if (ret == QMessageBox::Yes)
359 DCCommandUtil::postDeleteAxonTerminalCommand(requester, this, axonCell, axonTerminal);
363 void DCCreator::doCommandDeleteAxonTerminal(const void *requester, DCCell *receptorCell, const QString& receptorName)
367 msgBox.setText(tr("Delete receptor"));
370 msg.append(QString::fromStdString(receptorCell->getName()));
371 msg.append("\nDo you want to delete receptor ");
372 msg.append(receptorName);
374 msgBox.setInformativeText(msg);
375 msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
376 msgBox.setDefaultButton(QMessageBox::Cancel);
377 int ret = msgBox.exec();
379 if (ret == QMessageBox::Yes)
381 DCCommandUtil::postDeleteAxonTerminalCommand(requester, this, receptorCell, receptorName);
385 void DCCreator::doCommandStartEditCellCode(const void *requester, DCCell *cell)
387 DCCommandUtil::postStartCellCodeEditCommand(requester, this, cell);
390 void DCCreator::doCommandStopEditCellCode(const void *requester)
392 DCCommandUtil::postStopCellCodeEditCommand(requester, this);
395 void DCCreator::doCommandSetCellCodeClass(const void *requester, DCCell *cell, DCCellCode *cellCode)
397 DCCommandUtil::postSetCellCodeClassCommand(requester, this, cell, cellCode);
400 void DCCreator::doCommandUnsetCellCodeClass(const void *requester, DCCell *cell)
402 DCCommandUtil::postUnsetCellCodeClassCommand(requester, this, cell);
405 void DCCreator::doCommandAddCellCodeClass(const void *requester, DCContainer *container, const QString& name, const QString& type)
407 DCCommandUtil::postAddCellCodeClassCommand(requester, this, container, name, type);
410 void DCCreator::doCommandAddCell(const void *requester, DCContainer *container, const QString& containerBasedPath, const QString& name, const QString& type)
412 DCCommandUtil::postAddCellCommand(requester, this, container, containerBasedPath, name, type);
415 void DCCreator::doCommandAddPage(const void *requester, const QString &containerBasedPath)
417 DCCommandUtil::postAddPageCommand(requester, this, containerBasedPath);
420 void DCCreator::doCommandAddDirectory(const void *requester, const QString &sysFilePath)
422 DCCommandUtil::postAddDirectoryCommand(requester, this, sysFilePath);
425 void DCCreator::doCommandRemoveDirectory(const void *requester, const QString &sysFilePath)
427 DCCommandUtil::postRemoveDirectoryCommand(requester, this, sysFilePath);
430 bool DCCreator::doCommandRenameDirectoryImmidiate(const void *requester, const QString &containerBasedPath, const QString &oldName, const QString &newName)
434 msgBox.setText("This command isn't implemented yet");
439 DCContainer* DCCreator::getCurrentContainer() const
441 return d_vcontent->getContainer();
444 void DCCreator::slotSceneSelectedPageChanged(const void *requester)
446 emit sceneSelectedPageChanged(requester, d_scene);
449 void DCCreator::slotSceneSelectedCellObjectChanged(const void *requester)
451 emit sceneSelectedCellChanged(requester, d_scene);
454 void DCCreator::slotSceneViewScaleChanged(const void *requester)
456 emit sceneViewScaleChanged(requester, d_scene);
459 void DCCreator::slotSceneViewAngleChanged(const void *requester)
461 emit sceneViewAngleChanged(requester, d_scene);
464 void DCCreator::slotSceneViewCenterChanged(const void *requester)
466 emit sceneViewCenterChanged(requester, d_scene);
469 void DCCreator::slotSceneViewSettingChanged(const void *requester)
471 emit sceneViewSettingChanged(requester, d_scene);
474 void DCCreator::slotSceneViewEditModeChanged(const void *requester)
476 emit sceneViewEditModeChanged(requester, d_scene);