OSDN Git Service

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