OSDN Git Service

03041e0c9ab60687483d02c5b0e2a3e6d7c13046
[dennco/denncoCreator.git] / Source / dccreator.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
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.
7 //
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.
12 //
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/>.
15
16 //
17 //  Created by tkawata on Sep-30, 2012.
18 //
19 #include "TKLog.h"
20 #include "DNGlobal.h"
21 #include "dcconsole.h"
22
23 #include "dccreator.h"
24
25 #include "dccontent.h"
26 #include "dccontainer.h"
27 #include "dcscene.h"
28
29 #include "dccell.h"
30 #include "dcaxon.h"
31 #include "dcaxonterminal.h"
32 #include "dcreceptor.h"
33
34 #include "command/dccommandevent.h"
35 #include "command/dccommand.h"
36
37 #include "utils/dcdialogutil.h"
38 #include "utils/dccommandutil.h"
39 #include "dialog/dcinputreceptornamedialog.h"
40 #include "codeeditor/dccellscriptseditorwindow.h"
41
42 #include <QUndoCommand>
43 #include <map>
44
45 DCCreator::DCCreator(QMainWindow *mainwindow)
46     :  d_mainWindow(mainwindow), d_vcontent(NULL), d_scene(NULL),
47       d_persMode(DC_PERSMODE_PAGEEDIT), d_contentRootPath("")
48 {
49     d_undoStack = new QUndoStack(this);
50
51     d_console = new DCConsole;
52     TKLog::setDestination(d_console);
53 }
54
55 DCCreator::~DCCreator()
56 {
57     emit sceneChanged(this, NULL);
58     d_scene = NULL;
59
60     if (d_vcontent)
61     {
62         delete d_vcontent;
63     }
64     if (d_console)
65     {
66         delete d_console;
67     }
68 }
69
70 bool DCCreator::event(QEvent *event)
71 {
72     if (!d_scene)
73         return false;
74
75     if(event->type() == DCCommandEvent::EVENT_TYPEID)
76     {
77         DCCommandEvent *commandEvent = dynamic_cast<DCCommandEvent*>(event);
78         if (commandEvent)
79         {
80             QUndoCommand *command = commandEvent->getCommand();
81             if (command)
82             {
83                 d_undoStack->push(command);
84                 emit commandExecuted(command);
85             }
86             return true;
87         }
88     }
89     else if (event->type() == DCUndoEvent::EVENT_TYPEID)
90     {
91         d_undoStack->undo();
92         emit commandExecuted(NULL);
93         return true;
94     }
95     return false;
96 }
97
98 void DCCreator::resetVisualizer()
99 {
100     emit sceneChanged(this,0);
101 }
102
103 void DCCreator::initMode()
104 {
105     d_persMode = DC_PERSMODE_NAVIGATION;
106     d_scene->initMode(DCScene::DCV_PERSMODE_NAVIGATION, DCScene::DCV_EDITMODE_LAYOUT);
107 }
108
109 bool DCCreator::loadContent(const QString &contentRoot)
110 {
111     d_console->clearLog();
112
113     d_contentRootPath = contentRoot;
114     if (d_vcontent)
115     {
116         if (d_scene)
117             d_scene->disconnect(this);
118         resetVisualizer();
119         delete d_vcontent;
120     }
121     d_vcontent = new DCContent(this, contentRoot.toStdString());
122
123     if (d_vcontent->isValid())
124     {
125         d_scene = d_vcontent->getContainer()->getScene();
126
127         d_scene->loadSceneAll();
128
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*)));
137
138         emit contentRootPathChanged(this, d_contentRootPath);
139         emit sceneChanged(this, d_scene);
140         initMode();
141
142         getMainWindow()->statusBar()->showMessage(tr("Content loaded"), 2000);
143
144         return true;
145     }
146     else
147     {
148         d_scene = NULL;
149         emit contentRootPathChanged(this, d_contentRootPath);
150         emit sceneChanged(this, NULL);
151
152         if (!dnGlobal()->isErrorStatusNormal())
153         {
154             QMessageBox msgBox(QMessageBox::Warning, QString::fromStdString(dnGlobal()->getMessage1()), QString::fromStdString(dnGlobal()->getMessage2()));
155             msgBox.setInformativeText(d_console->getLog(10));
156             msgBox.exec();
157         }
158         return false;
159     }
160 }
161
162 bool DCCreator::savePage(DCVCPage *page, bool showResultInMessageBox)
163 {
164     bool r = false;
165     if (d_scene && d_vcontent)
166     {
167         DCCellScriptsEditorWindow::saveScriptsBelongTo(page, true);
168         r = d_vcontent->saveForPage(d_vcontent->getContentRootPath(), page);
169     }
170     if (showResultInMessageBox)
171     {
172         QMessageBox msgBox;
173         if (r)
174         {
175             msgBox.setText(tr("Page is saved successfully"));
176         }
177         else
178         {
179             msgBox.setText(tr("Error!! Failed to save the page file"));
180         }
181         msgBox.exec();
182     }
183     return r;
184 }
185
186 bool DCCreator::savePage(const QSet<DCVCPage*> &pages, bool showResultInMessageBox)
187 {
188     bool r = true;
189     QSet<DCVCPage*>::const_iterator i = pages.constBegin();
190      while (i != pages.constEnd())
191      {
192          if (!savePage(*i, showResultInMessageBox))
193          {
194              r = false;
195         }
196          ++i;
197      }
198      return r;
199 }
200
201 bool DCCreator::saveAll(bool showResultInMessageBox)
202 {
203     bool r = false;
204
205     if (DCCellScriptsEditorWindow::getIsVisible())
206     {
207         DCCellScriptsEditorWindow::saveScriptsAll(true);
208     }
209
210     if (d_scene && d_vcontent)
211     {
212         r = d_vcontent->saveAll(d_vcontent->getContentRootPath());
213     }
214     if (showResultInMessageBox)
215     {
216         QMessageBox msgBox;
217         if (r)
218         {
219             msgBox.setText(tr("Content is saved successfully"));
220         }
221         else
222         {
223             msgBox.setText(tr("Error!! Failed to save content files"));
224         }
225         msgBox.exec();
226     }
227     getMainWindow()->statusBar()->showMessage(tr("Content saved"), 2000);
228
229     return r;
230 }
231
232
233 void DCCreator::selectPage(const void *requester, DCVCPage *page, bool multipleSelection)
234 {
235     if (d_scene)
236     {
237         d_scene->selectPage(requester, page, multipleSelection);
238     }
239 }
240
241 void DCCreator::unselectPage(const void *requester, DCVCPage *page)
242 {
243     if (d_scene)
244     {
245         d_scene->unselectPage(requester, page);
246     }
247 }
248
249 bool DCCreator::selectPage(const void *requester, const QString &locationPath, bool multipleSelection)
250 {
251     bool r = false;
252     if (d_scene)
253     {
254         r = d_scene->selectPage(requester, locationPath, multipleSelection);
255     }
256     return r;
257 }
258
259 bool DCCreator::unselectPage(const void *requester, const QString &locationPath)
260 {
261     bool r = false;
262     if (d_scene)
263     {
264         r = d_scene->unselectPage(requester, locationPath);
265     }
266     return r;
267 }
268
269
270 void DCCreator::unselectPageAll(const void *requester)
271 {
272     if (d_scene)
273     {
274         d_scene->unselectPageAll(requester);
275     }
276 }
277
278 void DCCreator::selectCellObject(const void *requester, DCVComponent *object, bool multipleSelection)
279 {
280     if (d_scene)
281         d_scene->selectCellObject(requester, object, multipleSelection);
282 }
283
284 void DCCreator::unselectCellObject(const void *requester, DCVComponent *object)
285 {
286     if (d_scene)
287         d_scene->unselectCellObject(requester, object);
288 }
289
290 void DCCreator::unselectCellObjectAll(const void *requester)
291 {
292     if (d_scene)
293         d_scene->unselectCellObjectAll(requester);
294 }
295
296 void DCCreator::changeSceneScale(const void *requester, float scale)
297 {
298     if (d_scene)
299     {
300         if (scale <= 10) scale =10;
301         if (scale >= 1000) scale = 1000;
302         d_scene->setScale(requester, scale);
303     }
304 }
305
306 void DCCreator::rotateScene(const void *requester, float xangle_delta, float yangle_delta)
307 {
308     if (d_scene)
309     {
310         float xangle = d_scene->getXAngle() + xangle_delta;
311         if (xangle < 0) xangle = 0;
312         if (xangle > 90) xangle = 90;
313
314         float yangle = d_scene->getYAngle() + yangle_delta;
315         while (yangle < 0) yangle += 360;
316         while (yangle > 360) yangle -= 360;
317
318         d_scene->setAngle(requester, xangle,yangle);
319     }
320 }
321
322 void DCCreator::translateBrowsModeScene(const void *requester, float dx, float dy)
323 {
324     if (d_scene)
325     {
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);
330     }
331 }
332
333 void DCCreator::translateEditModeScene(const void *requester, float dx, float dy)
334 {
335     if (d_scene)
336     {
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);
341     }
342 }
343
344 void DCCreator::resetSceneTranslation()
345 {
346
347 }
348
349 bool DCCreator::changePersMode(const void *requester, DCPersMode mode)
350 {
351     bool r = false;
352     if (d_scene)
353     {
354         d_persMode = mode;
355         switch(mode)
356         {
357         case DC_PERSMODE_NAVIGATION:
358             r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_NAVIGATION);
359             break;
360         case DC_PERSMODE_PAGEEDIT:
361             r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_PAGEEDIT);
362             break;
363         }
364         d_scene->updateVisiblity();
365     }
366     return r;
367 }
368
369 void DCCreator::doUndo(const void *requester)
370 {
371     //TODO
372 }
373
374 void DCCreator::doUndoImmidiate()
375 {
376     d_undoStack->undo();
377 }
378
379 void DCCreator::doCommandStartAddAxonTerminalFromAxon(const void *requester, DCCell *axonCell)
380 {
381     DCCommandUtil::postStartAddAxonTerminalCommandFromAxon(requester, this, axonCell);
382 }
383
384 void DCCreator::doCommandStartAddAxonTerminalFromReceptor(const void *requester, DCCell *receptorCell)
385 {
386     DCInputReceptorNameDialog dialog(receptorCell);
387     dialog.exec();
388     if (dialog.getIsOk() && dialog.getInputText().length() > 0)
389     {
390         DCCommandUtil::postStartAddAxonTerminalCommandFromReceptor(requester, this, receptorCell, dialog.getInputText());
391     }
392 }
393
394 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCAxon *axon, DCCell *receptorCell)
395 {
396     DCInputReceptorNameDialog dialog(receptorCell);
397     dialog.exec();
398     if (dialog.getIsOk() && dialog.getInputText().length() > 0)
399     {
400         DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axon, receptorCell, dialog.getInputText());
401     }
402     else
403     {
404         doCommandCancelAddAxonTerminal(requester);
405     }
406 }
407
408 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCCell *axonCell, DCReceptor *receptor)
409 {
410     DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axonCell, receptor);
411 }
412
413 void DCCreator::doCommandCancelAddAxonTerminal(const void *requester)
414 {
415 //TODO:
416     // may need to consider the case multiple commands are executed after
417     // startAddAxon command is executed.
418     DCCommandUtil::postUndoRequestCommand(requester, this);
419 }
420
421 void DCCreator::doCommandRemoveAxonTerminal(const void *requester, DCCell *axonCell, DCAxonTerminal *axonTerminal)
422 {
423     DCReceptor *receptor = axonTerminal->getTarget();
424     if (!receptor)
425         return;
426
427     DCCell *targetCell = receptor->getOwnerCell();
428     if (!targetCell)
429         return;
430
431     QMessageBox msgBox;
432     msgBox.setText(tr("Delete axon terminal"));
433     QString msg;
434     msg.append("Cell ");
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()));
438     msg.append("#");
439     msg.append(QString::fromStdString(targetCell->getName()));
440     msg.append("(");
441     msg.append(QString::fromStdString(targetCell->getReceptorName(receptor)));
442     msg.append(")?");
443     msgBox.setInformativeText(msg);
444     msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
445     msgBox.setDefaultButton(QMessageBox::Cancel);
446     int ret = msgBox.exec();
447
448     if (ret == QMessageBox::Yes)
449     {
450         DCCommandUtil::postRemoveAxonTerminalCommand(requester, this, axonCell, axonTerminal);
451     }
452 }
453
454 void DCCreator::doCommandRemoveAxonTerminal(const void *requester, DCCell *receptorCell, const QString& receptorName)
455 {
456
457     QMessageBox msgBox;
458     msgBox.setText(tr("Delete receptor"));
459     QString msg;
460     msg.append("Cell ");
461     msg.append(QString::fromStdString(receptorCell->getName()));
462     msg.append("\nDo you want to delete receptor ");
463     msg.append(receptorName);
464     msg.append("?\n");
465     msgBox.setInformativeText(msg);
466     msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
467     msgBox.setDefaultButton(QMessageBox::Cancel);
468     int ret = msgBox.exec();
469
470     if (ret == QMessageBox::Yes)
471     {
472         DCCommandUtil::postRemoveAxonTerminalCommand(requester, this, receptorCell, receptorName);
473     }
474 }
475
476 void DCCreator::doCommandAssignCellCodeClassToCell(const void *requester, DCCell *cell, DCCellCode *cellCode)
477 {
478     DCCommandUtil::postAssignCellCodeClassToCellCommand(requester, this, cell, cellCode);
479 }
480
481 void DCCreator::doCommandUnassignCellCodeClassFromCell(const void *requester, DCCell *cell)
482 {
483     DCCommandUtil::postUnassignCellCodeClassFromCellCommand(requester, this, cell);
484 }
485
486 void DCCreator::doCommandAddCellCodeClass(const void *requester, DCContainer *container, const QString& name, const QString& type)
487 {
488     DCCommandUtil::postAddCellCodeClassCommand(requester, this, container, name, type);
489 }
490
491 void DCCreator::doCommandChangeCellCodeClassType(const void *requester, DCCellCode* cellCode, const QString &newType)
492 {
493     DCCommandUtil::postChangeCellCodeClassTypeCommand(requester, this, cellCode, newType);
494 }
495
496 void DCCreator::doCommandAddCell(const void *requester, DCContainer *container, const QString &containerBasedPath, const QString &name, const QString &type, float pageX, float pageY)
497 {
498     DCCommandUtil::postAddCellCommand(requester, this, container, containerBasedPath, name, type, pageX, pageY);
499 }
500
501 void DCCreator::doCommandChangeCellType(const void *requester, DCCell *cell, const QString &newType)
502 {
503     DCCommandUtil::postChangeCellTypeCommand(requester, this, cell, newType);
504 }
505
506 void DCCreator::doCommandRenameCell(const void *requester, DCCell *cell, const QString &newContainerBasedPath, const QString &newName)
507 {
508     DCCommandUtil::postRenameCellCommand(requester, this, cell, newContainerBasedPath, newName);
509 }
510
511 void DCCreator::doCommandRemoveCell(const void *requester, DCContainer *container, DCCell *cell)
512 {
513     DCCommandUtil::postRemoveCellCommand(requester, this, container, cell);
514 }
515
516 void DCCreator::doCommandRemoveCellCode(const void *requester, DCContainer *container, DCCellCode *cellCode)
517 {
518     DCCommandUtil::postRemoveCellCodeCommand(requester, this, container, cellCode);
519 }
520
521 void DCCreator::doCommandAddPage(const void *requester, const QString &containerBasedPath)
522 {
523     DCCommandUtil::postAddPageCommand(requester, this, containerBasedPath);
524 }
525
526 void DCCreator::doCommandMovePage(const void *requester, const QString &oldContainerBasedPath, const QString &newContainerBasedPath)
527 {
528     DCCommandUtil::postMovePageCommand(requester, this, oldContainerBasedPath, newContainerBasedPath);
529 }
530
531 void DCCreator::doCommandRemovePage(const void *requester, DCVCPage *page)
532 {
533     DCCommandUtil::postRemovePageCommand(requester, this, page);
534 }
535
536 void DCCreator::doCommandAddDirectory(const void *requester, const QString &sysFilePath)
537 {
538     DCCommandUtil::postAddDirectoryCommand(requester, this, sysFilePath);
539 }
540
541 void DCCreator::doCommandRenameDirectory(const void *requester, const QString &oldSysFilePath, const QString &newSysFilePath)
542 {
543     DCCommandUtil::postRenameDirectoryCommand(requester, this, oldSysFilePath, newSysFilePath);
544 }
545
546 void DCCreator::doCommandRemoveDirectory(const void *requester, const QString &sysFilePath)
547 {
548     DCCommandUtil::postRemoveDirectoryCommand(requester, this, sysFilePath);
549 }
550
551 bool DCCreator::doCommandRenameReceptorName(const void *requester, DCCell *cell, const QString &oldName, const QString &newName, bool doImmediate)
552 {
553     if (doImmediate)
554     {
555         DCCommand *command = DCCommandUtil::createRenameReceptorNameCommand(requester, this, cell, oldName, newName);
556         d_undoStack->push(command);
557         return command->getCommandResult() == DCCommand::DCCOMMAND_SUCCEEDED;
558     }
559     else
560     {
561         DCCommandUtil::postRenameReceptorNameCommand(requester, this, cell, oldName, newName);
562         return true;
563     }
564 }
565
566 DCContainer* DCCreator::getCurrentContainer() const
567 {
568     if (d_vcontent)
569         return d_vcontent->getContainer();
570     else
571         return NULL;
572 }
573
574 void DCCreator::slotSceneSelectedPageChanged(const void *requester)
575 {
576     emit sceneSelectedPageChanged(requester, d_scene);
577 }
578
579 void DCCreator::slotSceneSelectedCellObjectChanged(const void *requester)
580 {
581     emit sceneSelectedCellChanged(requester, d_scene);
582 }
583
584 void DCCreator::slotSceneViewScaleChanged(const void *requester)
585 {
586     emit sceneViewScaleChanged(requester, d_scene);
587 }
588
589 void DCCreator::slotSceneViewAngleChanged(const void *requester)
590 {
591     emit sceneViewAngleChanged(requester, d_scene);
592 }
593
594 void DCCreator::slotSceneViewCenterChanged(const void *requester)
595 {
596     emit sceneViewCenterChanged(requester, d_scene);
597 }
598
599 void DCCreator::slotSceneViewSettingChanged(const void *requester)
600 {
601     emit sceneViewSettingChanged(requester, d_scene);
602 }
603
604 void DCCreator::slotSceneViewEditModeChanged(const void *requester)
605 {
606     emit sceneViewEditModeChanged(requester, d_scene);
607 }
608