OSDN Git Service

[denncoCreator] Implementing save functionality. The work is still in progress.
[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 "utils/dcdialogutil.h"
34 #include "utils/dccommandutil.h"
35 #include "dialog/dcinputreceptornamedialog.h"
36
37 #include <QUndoCommand>
38 #include <map>
39
40 DCCreator::DCCreator(QMainWindow *mainwindow)
41     :  d_mainWindow(mainwindow), d_vcontent(NULL), d_scene(NULL),
42       d_persMode(DC_PERSMODE_PAGEEDIT), d_contentRootPath("")
43 {
44     d_undoStack = new QUndoStack(this);
45 }
46
47 DCCreator::~DCCreator()
48 {
49     if (d_vcontent)
50     {
51         delete d_vcontent;
52     }
53 }
54
55 bool DCCreator::event(QEvent *event)
56 {
57     if(event->type() == DCCommandEvent::EVENT_TYPEID)
58     {
59         DCCommandEvent *commandEvent = dynamic_cast<DCCommandEvent*>(event);
60         if (commandEvent)
61         {
62             QUndoCommand *command = commandEvent->getCommand();
63             if (command)
64             {
65                 d_undoStack->push(command);
66                 emit commandExecuted(command);
67             }
68             return true;
69         }
70     }
71     else if (event->type() == DCUndoEvent::EVENT_TYPEID)
72     {
73         d_undoStack->undo();
74         emit commandExecuted(NULL);
75         return true;
76     }
77     return false;
78 }
79
80 void DCCreator::resetVisualizer()
81 {
82     emit sceneChanged(this,0);
83 }
84
85 void DCCreator::initMode()
86 {
87     d_persMode = DC_PERSMODE_NAVIGATION;
88     d_scene->initMode(DCScene::DCV_PERSMODE_NAVIGATION, DCScene::DCV_EDITMODE_LAYOUT);
89 }
90
91 bool DCCreator::loadContent(const QString &contentRoot)
92 {
93     d_contentRootPath = contentRoot;
94     if (d_vcontent)
95     {
96         if (d_scene)
97             d_scene->disconnect(this);
98         resetVisualizer();
99         delete d_vcontent;
100     }
101     d_vcontent = new DCContent(this, contentRoot.toStdString());
102
103     if (d_vcontent->isValid())
104     {
105         d_scene = d_vcontent->getContainer()->getScene();
106
107         d_scene->loadSceneAll();
108
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*)));
117
118         emit contentRootPathChanged(this, d_contentRootPath);
119         emit sceneChanged(this, d_scene);
120         initMode();
121         return true;
122     }
123     else
124     {
125         d_scene = NULL;
126         emit sceneChanged(this, NULL);
127         return false;
128     }
129 }
130
131 bool DCCreator::saveAll()
132 {
133     if (d_scene)
134     {
135         d_vcontent->saveAll(QString::fromStdString(getCurrentContainer()->getContainerRootPath()));
136         return d_scene->saveSceneAll();
137     }
138     return false;
139 }
140
141
142 void DCCreator::selectPage(const void *requester, DCVCPage *page, bool multipleSelection)
143 {
144     if (d_scene)
145     {
146         d_scene->selectPage(requester, page, multipleSelection);
147     }
148 }
149
150 void DCCreator::unselectPage(const void *requester, DCVCPage *page)
151 {
152     if (d_scene)
153     {
154         d_scene->unselectPage(requester, page);
155     }
156 }
157
158 bool DCCreator::selectPage(const void *requester, const QString &locationPath, bool multipleSelection)
159 {
160     bool r = false;
161     if (d_scene)
162     {
163         r = d_scene->selectPage(requester, locationPath, multipleSelection);
164     }
165     return r;
166 }
167
168 bool DCCreator::unselectPage(const void *requester, const QString &locationPath)
169 {
170     bool r = false;
171     if (d_scene)
172     {
173         r = d_scene->unselectPage(requester, locationPath);
174     }
175     return r;
176 }
177
178
179 void DCCreator::unselectPageAll(const void *requester)
180 {
181     if (d_scene)
182     {
183         d_scene->unselectPageAll(requester);
184     }
185 }
186
187 void DCCreator::selectCellObject(const void *requester, DCVComponent *object, bool multipleSelection)
188 {
189     if (d_scene)
190         d_scene->selectCellObject(requester, object, multipleSelection);
191 }
192
193 void DCCreator::unselectCellObject(const void *requester, DCVComponent *object)
194 {
195     if (d_scene)
196         d_scene->unselectCellObject(requester, object);
197 }
198
199 void DCCreator::unselectCellObjectAll(const void *requester)
200 {
201     if (d_scene)
202         d_scene->unselectCellObjectAll(requester);
203 }
204
205 void DCCreator::changeSceneScale(const void *requester, float scale)
206 {
207     if (d_scene)
208     {
209         if (scale <= 10) scale =10;
210         if (scale >= 1000) scale = 1000;
211         d_scene->setScale(requester, scale);
212     }
213 }
214
215 void DCCreator::rotateScene(const void *requester, float xangle_delta, float yangle_delta)
216 {
217     if (d_scene)
218     {
219         float xangle = d_scene->getXAngle() + xangle_delta;
220         if (xangle < 0) xangle = 0;
221         if (xangle > 90) xangle = 90;
222
223         float yangle = d_scene->getYAngle() + yangle_delta;
224         while (yangle < 0) yangle += 360;
225         while (yangle > 360) yangle -= 360;
226
227         d_scene->setAngle(requester, xangle,yangle);
228     }
229 }
230
231 void DCCreator::translateBrowsModeScene(const void *requester, float dx, float dy)
232 {
233     if (d_scene)
234     {
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);
239     }
240 }
241
242 void DCCreator::translateEditModeScene(const void *requester, float dx, float dy)
243 {
244     if (d_scene)
245     {
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);
250     }
251 }
252
253 void DCCreator::resetSceneTranslation()
254 {
255
256 }
257
258 bool DCCreator::changePersMode(const void *requester, DCPersMode mode)
259 {
260     bool r = false;
261     if (d_scene)
262     {
263         d_persMode = mode;
264         switch(mode)
265         {
266         case DC_PERSMODE_NAVIGATION:
267             r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_NAVIGATION);
268             break;
269         case DC_PERSMODE_PAGEEDIT:
270             r = d_scene->changePersMode(requester, DCScene::DCV_PERSMODE_PAGEEDIT);
271             break;
272         }
273         d_scene->updateVisiblity();
274     }
275     return r;
276 }
277
278 void DCCreator::doUndo(const void *requester)
279 {
280     //TODO
281 }
282
283 void DCCreator::doUndoImmidiate()
284 {
285     d_undoStack->undo();
286 }
287
288 void DCCreator::doCommandStartAddAxonTerminalFromAxon(const void *requester, DCCell *axonCell)
289 {
290     DCCommandUtil::postStartAddAxonTerminalCommandFromAxon(requester, this, axonCell);
291 }
292
293 void DCCreator::doCommandStartAddAxonTerminalFromReceptor(const void *requester, DCCell *receptorCell)
294 {
295     DCInputReceptorNameDialog dialog(receptorCell);
296     dialog.exec();
297     if (dialog.getIsOk() && dialog.getInputText().length() > 0)
298     {
299         DCCommandUtil::postStartAddAxonTerminalCommandFromReceptor(requester, this, receptorCell, dialog.getInputText());
300     }
301 }
302
303 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCAxon *axon, DCCell *receptorCell)
304 {
305     DCInputReceptorNameDialog dialog(receptorCell);
306     dialog.exec();
307     if (dialog.getIsOk() && dialog.getInputText().length() > 0)
308     {
309         DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axon, receptorCell, dialog.getInputText());
310     }
311     else
312     {
313         doCommandCancelAddAxonTerminal(requester);
314     }
315 }
316
317 void DCCreator::doCommandCommitAddAxonTerminal(const void *requester, DCCell *axonCell, DCReceptor *receptor)
318 {
319     DCCommandUtil::postCommitAddAxonTerminalCommand(requester, this, axonCell, receptor);
320 }
321
322 void DCCreator::doCommandCancelAddAxonTerminal(const void *requester)
323 {
324 //TODO:
325     // may need to consider the case multiple commands are executed after
326     // startAddAxon command is executed.
327     DCCommandUtil::postUndoRequestCommand(requester, this);
328 }
329
330 void DCCreator::doCommandDeleteAxonTerminal(const void *requester, DCCell *axonCell, DCAxonTerminal *axonTerminal)
331 {
332     DCReceptor *receptor = dynamic_cast<DCReceptor*>(axonTerminal->getTarget());
333     if (!receptor)
334         return;
335
336     DCCell *targetCell = dynamic_cast<DCCell*>(receptor->getOwnerCell());
337     if (!targetCell)
338         return;
339
340     QMessageBox msgBox;
341     msgBox.setText(tr("Delete axon terminal"));
342     QString msg;
343     msg.append("Cell ");
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()));
347     msg.append("#");
348     msg.append(QString::fromStdString(targetCell->getName()));
349     msg.append("(");
350     msg.append(QString::fromStdString(targetCell->getReceptorName(receptor)));
351     msg.append(")?");
352     msgBox.setInformativeText(msg);
353     msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
354     msgBox.setDefaultButton(QMessageBox::Cancel);
355     int ret = msgBox.exec();
356
357     if (ret == QMessageBox::Yes)
358     {
359         DCCommandUtil::postDeleteAxonTerminalCommand(requester, this, axonCell, axonTerminal);
360     }
361 }
362
363 void DCCreator::doCommandDeleteAxonTerminal(const void *requester, DCCell *receptorCell, const QString& receptorName)
364 {
365
366     QMessageBox msgBox;
367     msgBox.setText(tr("Delete receptor"));
368     QString msg;
369     msg.append("Cell ");
370     msg.append(QString::fromStdString(receptorCell->getName()));
371     msg.append("\nDo you want to delete receptor ");
372     msg.append(receptorName);
373     msg.append("?\n");
374     msgBox.setInformativeText(msg);
375     msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
376     msgBox.setDefaultButton(QMessageBox::Cancel);
377     int ret = msgBox.exec();
378
379     if (ret == QMessageBox::Yes)
380     {
381         DCCommandUtil::postDeleteAxonTerminalCommand(requester, this, receptorCell, receptorName);
382     }
383 }
384
385 void DCCreator::doCommandStartEditCellCode(const void *requester, DCCell *cell)
386 {
387     DCCommandUtil::postStartCellCodeEditCommand(requester, this, cell);
388 }
389
390 void DCCreator::doCommandStopEditCellCode(const void *requester)
391 {
392     DCCommandUtil::postStopCellCodeEditCommand(requester, this);
393 }
394
395 void DCCreator::doCommandSetCellCodeClass(const void *requester, DCCell *cell, DCCellCode *cellCode)
396 {
397     DCCommandUtil::postSetCellCodeClassCommand(requester, this, cell, cellCode);
398 }
399
400 void DCCreator::doCommandUnsetCellCodeClass(const void *requester, DCCell *cell)
401 {
402     DCCommandUtil::postUnsetCellCodeClassCommand(requester, this, cell);
403 }
404
405 void DCCreator::doCommandAddCellCodeClass(const void *requester, DCContainer *container, const QString& name, const QString& type)
406 {
407     DCCommandUtil::postAddCellCodeClassCommand(requester, this, container, name, type);
408 }
409
410 void DCCreator::doCommandAddCell(const void *requester, DCContainer *container, const QString& containerBasedPath, const QString& name, const QString& type)
411 {
412     DCCommandUtil::postAddCellCommand(requester, this, container, containerBasedPath, name, type);
413 }
414
415 void DCCreator::doCommandAddPage(const void *requester, const QString &containerBasedPath)
416 {
417     DCCommandUtil::postAddPageCommand(requester, this, containerBasedPath);
418 }
419
420 void DCCreator::doCommandAddDirectory(const void *requester, const QString &sysFilePath)
421 {
422     DCCommandUtil::postAddDirectoryCommand(requester, this, sysFilePath);
423 }
424
425 void DCCreator::doCommandRemoveDirectory(const void *requester, const QString &sysFilePath)
426 {
427     DCCommandUtil::postRemoveDirectoryCommand(requester, this, sysFilePath);
428 }
429
430 bool DCCreator::doCommandRenameDirectoryImmidiate(const void *requester, const QString &containerBasedPath, const QString &oldName, const QString &newName)
431 {
432     //TODO
433     QMessageBox msgBox;
434     msgBox.setText("This command isn't implemented yet");
435     msgBox.exec();
436     return false;
437 }
438
439 DCContainer* DCCreator::getCurrentContainer() const
440 {
441     return d_vcontent->getContainer();
442 }
443
444 void DCCreator::slotSceneSelectedPageChanged(const void *requester)
445 {
446     emit sceneSelectedPageChanged(requester, d_scene);
447 }
448
449 void DCCreator::slotSceneSelectedCellObjectChanged(const void *requester)
450 {
451     emit sceneSelectedCellChanged(requester, d_scene);
452 }
453
454 void DCCreator::slotSceneViewScaleChanged(const void *requester)
455 {
456     emit sceneViewScaleChanged(requester, d_scene);
457 }
458
459 void DCCreator::slotSceneViewAngleChanged(const void *requester)
460 {
461     emit sceneViewAngleChanged(requester, d_scene);
462 }
463
464 void DCCreator::slotSceneViewCenterChanged(const void *requester)
465 {
466     emit sceneViewCenterChanged(requester, d_scene);
467 }
468
469 void DCCreator::slotSceneViewSettingChanged(const void *requester)
470 {
471     emit sceneViewSettingChanged(requester, d_scene);
472 }
473
474 void DCCreator::slotSceneViewEditModeChanged(const void *requester)
475 {
476     emit sceneViewEditModeChanged(requester, d_scene);
477 }
478