OSDN Git Service

c5e88a7c2d2227df61f36fad228c4eb326752512
[dennco/denncoCreator.git] / Source / visualizer / toolwindow / dctoolwindowcellcodeeditor.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 "dctoolwindowcellcodeeditor.h"
20
21 #include "dccodeeditor.h"
22 #include "dccontainer.h"
23 #include "dccell.h"
24 #include "dcaxon.h"
25 #include "dcaxonterminal.h"
26 #include "dcreceptor.h"
27 #include "dccellcode.h"
28 #include "dccreator.h"
29 #include "dceditablelabel.h"
30 #include "dceditabletreeview.h"
31 #include "dccellcodetypecombobox.h"
32 #include "dccelltypecombobox.h"
33 #include "utils/dcqtitemmodel.h"
34 #include "dialog/dcassigncellcodeclassdialog.h"
35
36 #include <QVBoxLayout>
37 #include <QHBoxLayout>
38 #include <QStackedLayout>
39 #include <QSpacerItem>
40
41
42 DCToolWindowCellCodeEditor::DCToolWindowCellCodeEditor(DCCreator *creator) :
43     DCToolWindowBase("CELL CODE:", creator), d_mode(CELLCLASS_EDIT_MODE), d_container(NULL), d_cellCodeClass(NULL), d_ownerCell(NULL)
44 {
45     setWindowOpacity(0.95);
46     d_editor = new DCCodeEditor;
47
48     QHBoxLayout *layout = new QHBoxLayout;
49     layout->addWidget(d_editor);
50
51     QVBoxLayout *toolLayout = new QVBoxLayout;
52     d_uiClassGroupBox = new QGroupBox(tr("Class"));
53
54     QGridLayout *cellclassGroupBoxLayoutInnder1 = new QGridLayout;
55
56     cellclassGroupBoxLayoutInnder1->addWidget(new QLabel(tr("name")), 0,0);
57     cellclassGroupBoxLayoutInnder1->addWidget(new QLabel(tr("type")), 1,0);
58
59     d_uiClassName = new DCEditableLabel(this, "");
60     d_uiClassType = new DCCellCodeTypeComboBox(creator, this);
61     d_uiClassUnsetButton = new QPushButton(tr("Unset class"));
62     cellclassGroupBoxLayoutInnder1->addWidget(d_uiClassName, 0,1);
63     cellclassGroupBoxLayoutInnder1->addWidget(d_uiClassType, 1,1);
64     cellclassGroupBoxLayoutInnder1->addWidget(d_uiClassUnsetButton, 2,0,1,2);
65
66     QVBoxLayout *cellclassGroupBoxLayoutInnder2 = new QVBoxLayout;
67     d_uiClassAssignButton = new QPushButton(tr("Assign a class"));
68     cellclassGroupBoxLayoutInnder2->addWidget(new QLabel(tr("No Cell Code class is assinged.")));
69     cellclassGroupBoxLayoutInnder2->addWidget(d_uiClassAssignButton);
70
71     QWidget *uiCellCode1 = new QWidget(d_uiClassGroupBox);
72     QWidget *uiCellCode2 = new QWidget(d_uiClassGroupBox);
73     uiCellCode1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
74     uiCellCode2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
75     uiCellCode1->setLayout(cellclassGroupBoxLayoutInnder1);
76     uiCellCode2->setLayout(cellclassGroupBoxLayoutInnder2);
77     QVBoxLayout *cellclassGroupBoxLayoutOuter = new QVBoxLayout;
78     cellclassGroupBoxLayoutOuter->addWidget(uiCellCode1);
79     cellclassGroupBoxLayoutOuter->addWidget(uiCellCode2);
80
81     d_uiClassGroupBox->setLayout(cellclassGroupBoxLayoutOuter);
82     d_uiClassGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
83     toolLayout->addWidget(d_uiClassGroupBox);
84
85     d_uiCellGroupBox = new QGroupBox(tr("Cell"));
86     QGridLayout *cellGroupBoxLayout = new QGridLayout;
87     cellGroupBoxLayout->addWidget(new QLabel(tr("name")), 0,0);
88     cellGroupBoxLayout->addWidget(new QLabel(tr("path")), 1,0);
89     cellGroupBoxLayout->addWidget(new QLabel(tr("type")), 2,0);
90
91     d_uiCellName = new DCEditableLabel(this, "");
92     d_uiCellPath = new DCEditableLabel(this, "");
93     d_uiCellType = new DCCellTypeComboBox(creator, this);
94     d_uiSwitchEditorButton = new QPushButton(tr("Open custom script"));
95     cellGroupBoxLayout->addWidget(d_uiCellName, 0,1);
96     cellGroupBoxLayout->addWidget(d_uiCellPath, 1,1);
97     cellGroupBoxLayout->addWidget(d_uiCellType, 2,1);
98
99     d_receptors = new DCEditableTreeView(this);
100     d_receptors->setHeaderHidden(true);
101     d_receptors->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
102
103     d_axonTerminals = new DCEditableTreeView(this);
104     d_axonTerminals->setHeaderHidden(true);
105     d_axonTerminals->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
106
107
108     d_receptorItemModel = new DCQtItemModel(QStringList("name"));
109     d_axonTerminalItemModel = new DCQtItemModel(QStringList("name"));
110
111     d_receptors->setModel(d_receptorItemModel);
112     d_axonTerminals->setModel(d_axonTerminalItemModel);
113
114     cellGroupBoxLayout->addWidget(d_receptors, 3,0,1,2);
115     cellGroupBoxLayout->addWidget(d_axonTerminals, 4, 0, 1, 2);
116     cellGroupBoxLayout->addWidget(d_uiSwitchEditorButton, 5, 0,1,2);
117
118     d_uiCellGroupBox->setLayout(cellGroupBoxLayout);
119
120     toolLayout->addWidget(d_uiCellGroupBox);
121
122     toolLayout->addItem(new QSpacerItem(10,10, QSizePolicy::Minimum, QSizePolicy::Expanding));
123
124     layout->addLayout(toolLayout);
125
126     d_uiCloseButton = new QPushButton("Close");
127
128     QHBoxLayout *buttonLayout = new QHBoxLayout;
129     buttonLayout->addItem(new QSpacerItem(10,10, QSizePolicy::Expanding));
130     buttonLayout->addWidget(d_uiCloseButton);
131
132     contentLayout()->addLayout(layout);
133     contentLayout()->addLayout(buttonLayout);
134
135     connect(d_uiCloseButton, SIGNAL(clicked()), this, SLOT(finishEditing()));
136     connect(d_uiSwitchEditorButton, SIGNAL(clicked()), this, SLOT(switchEditor()));
137     connect(d_uiClassUnsetButton, SIGNAL(clicked()), this, SLOT(unsetCellCodeButtonClicked()));
138     connect(d_uiClassAssignButton, SIGNAL(clicked()), this, SLOT(assignCellCodeButtonClicked()));
139     connect(getController(), SIGNAL(commandExecuted(const QUndoCommand*)), this, SLOT(commandExecuted(const QUndoCommand*)));
140 }
141
142 DCToolWindowCellCodeEditor::~DCToolWindowCellCodeEditor()
143 {
144     d_receptors->disconnect(this);
145     d_axonTerminals->disconnect(this);
146     getController()->disconnect(this);
147
148     if (d_receptorItemModel)
149         d_receptorItemModel->deleteLater();
150
151     if (d_axonTerminalItemModel)
152         d_axonTerminalItemModel->deleteLater();
153 }
154
155 void DCToolWindowCellCodeEditor::setData(DCContainer *container, DCCellCode *cellCodeClass, DCCell *ownerCell)
156 {
157     if (d_ownerCell && d_ownerCell != ownerCell)
158     {
159         d_ownerCell->disconnect(this);
160     }
161     if (ownerCell)
162         connect(ownerCell, SIGNAL(cellCodeChanged()), this, SLOT(classForCellChanged()));
163
164     d_container = container;
165     d_cellCodeClass = cellCodeClass;
166     d_ownerCell = ownerCell;
167
168     d_uiClassType->setEditingCellCode(cellCodeClass);
169     d_uiCellType->setEditingCell(ownerCell);
170     updateInformation();
171
172     showClassScript();
173 }
174
175 void DCToolWindowCellCodeEditor::setData(DCContainer *container, DCCell *ownerCell)
176 {
177     if (d_ownerCell && d_ownerCell != ownerCell)
178     {
179         d_ownerCell->disconnect(this);
180     }
181     if (ownerCell)
182         connect(ownerCell, SIGNAL(cellCodeChanged()), this, SLOT(classForCellChanged()));
183
184     d_container = container;
185     d_ownerCell = ownerCell;
186
187     if (ownerCell)
188     {
189         d_cellCodeClass = ownerCell->getCellCode();
190     }
191
192     d_uiClassType->setEditingCellCode(d_cellCodeClass);
193     d_uiCellType->setEditingCell(d_ownerCell);
194     updateInformation();
195
196     showCustomScript();
197 }
198
199 void DCToolWindowCellCodeEditor::showClassScript()
200 {
201     d_mode = CELLCLASS_EDIT_MODE;
202     d_uiClassGroupBox->setVisible(true);
203     d_uiSwitchEditorButton->setText(tr("Switch to Custom Script"));
204     if (d_ownerCell->getIsCellCodeClassAssgined() && d_container)
205     {
206         d_editor->setPlainText(d_cellCodeClass->getOwnScript());
207         d_editor->setReadOnly(false);
208     }
209     else
210     {
211         d_editor->setPlainText("");
212         d_editor->setReadOnly(true);
213     }
214 }
215
216 void DCToolWindowCellCodeEditor::showCustomScript()
217 {
218     d_mode = CUSTOMSCRIPT_EDIT_MODE;
219     d_uiClassGroupBox->setVisible(false);
220     d_uiSwitchEditorButton->setText(tr("Switch to Cell Code"));
221     if (d_ownerCell)
222     {
223         d_editor->setPlainText(d_ownerCell->getCustomScript());
224         d_editor->setReadOnly(false);
225     }
226     else
227     {
228         d_editor->setPlainText("");
229         d_editor->setReadOnly(true);
230     }
231 }
232
233 void DCToolWindowCellCodeEditor::updateInformation()
234 {
235     d_receptorItemModel->removeAllItems();
236     d_axonTerminalItemModel->removeAllItems();
237
238     //update cell information
239     if (d_ownerCell)
240     {
241         d_uiCellName->setText(QString::fromStdString(d_ownerCell->getName()));
242         d_uiCellPath->setText(QString::fromStdString(d_ownerCell->getLocation()));
243         d_uiCellType->updateSelection();
244         d_uiCellType->setEnabled(!d_ownerCell->getIsCellCodeClassAssgined());
245
246         d_receptorItemModel->insertString("receptors");
247         d_receptorItemModel->setReadOnly(0,true);
248
249         d_axonTerminalItemModel->insertString("axonTerminals");
250         d_axonTerminalItemModel->setReadOnly(0,true);
251
252         const TKReceptorMap *receptors = d_ownerCell->getReceptors();
253         TKReceptorMap::const_iterator it = receptors->begin();
254         QModelIndex rroot = d_receptorItemModel->index(0,0);
255         int i = 0;
256         while( it != receptors->end())
257         {
258             QString receptorName = QString::fromStdString((*it).first);
259             d_receptorItemModel->insertString(receptorName,rroot);
260             ++it;
261             ++i;
262         }
263
264         QModelIndex aroot = d_axonTerminalItemModel->index(0,0);
265         DCAxon *axon = d_ownerCell->getAxon();
266         int acnt = axon->getNumberOfTerminals();
267         for (int i = 0; i < acnt; i++)
268         {
269             DCReceptor *targetReceptor = NULL;
270             DCCell *targetCell = NULL;
271             DCAxonTerminal *tarminal = axon->getTerminalAt(i);
272             if (tarminal)
273             {
274                 targetReceptor = dynamic_cast<DCReceptor*>(tarminal->getTarget());
275                 if (targetReceptor)
276                 {
277                     targetCell = dynamic_cast<DCCell*>(targetReceptor->getOwnerCell());
278                 }
279             }
280             QString axonTerminalName;
281
282             if (targetCell)
283             {
284                 QTextStream(&axonTerminalName)
285                         << QString::fromStdString(targetCell->getLocation())
286                         << "/"
287                         << QString::fromStdString(targetCell->getName())
288                         << "#"
289                         << QString::fromStdString(targetCell->getReceptorName(targetReceptor));
290             }
291             else
292             {
293                 axonTerminalName = "-";
294             }
295
296             d_axonTerminalItemModel->insertString(axonTerminalName,aroot);
297         }
298         d_receptors->expandAll();
299         d_axonTerminals->expandAll();
300     }
301     else
302     {
303         d_uiCellName->setText("");
304         d_uiCellPath->setText("");
305         d_uiCellType->updateSelection();
306     }
307
308     //update cell code information
309     if (d_cellCodeClass)
310     {
311         d_uiClassName->setText(QString::fromStdString(d_cellCodeClass->getFQNName()));
312         d_uiClassType->updateSelection();
313     }
314     else
315     {
316         d_uiClassName->setText("");
317         d_uiClassType->updateSelection();
318     }
319
320
321     // update title and tool visibility
322     QString titleString = "";
323     if (d_ownerCell)
324     {
325         titleString.append("CELL : ");
326         titleString.append(QString::fromStdString(d_ownerCell->getName()));
327         titleString.append("  ");
328
329         d_uiCellGroupBox->setVisible(true);
330         if (d_container->isScriptableCell(d_ownerCell))
331         {
332             if (d_ownerCell->getIsCellCodeClassAssgined())
333             {
334                 titleString.append("CELL CODE : ");
335                 titleString.append(QString::fromStdString(d_cellCodeClass->getFQNName()));
336                 titleString.append(" (");
337                 titleString.append(QString::fromStdString(d_cellCodeClass->getCellAPIName()));
338                 titleString.append(")");
339                 d_uiClassGroupBox->setVisible(true);
340                 d_uiClassGroupBox->layout()->itemAt(0)->widget()->setVisible(true);
341                 d_uiClassGroupBox->layout()->itemAt(1)->widget()->setVisible(false);
342                 d_uiClassGroupBox->adjustSize();
343             }
344             else
345             {
346                 d_uiClassGroupBox->setVisible(true);
347                 d_uiClassGroupBox->layout()->itemAt(0)->widget()->setVisible(false);
348                 d_uiClassGroupBox->layout()->itemAt(1)->widget()->setVisible(true);
349                 d_uiClassGroupBox->adjustSize();
350             }
351         }
352         else
353         {
354             d_uiClassGroupBox->setVisible(false);
355             d_uiClassGroupBox->layout()->itemAt(0)->widget()->setVisible(false);
356             d_uiClassGroupBox->layout()->itemAt(1)->widget()->setVisible(false);
357         }
358     }
359     else
360     {
361         titleString.append("CELL CODE : ");
362         titleString.append(QString::fromStdString(d_cellCodeClass->getFQNName()));
363         titleString.append(" (");
364         titleString.append(QString::fromStdString(d_cellCodeClass->getCellAPIName()));
365         titleString.append(")");
366         d_uiCellGroupBox->setVisible(false);
367         d_uiClassGroupBox->setVisible(true);
368         d_uiClassGroupBox->layout()->itemAt(0)->widget()->setVisible(true);
369         d_uiClassGroupBox->layout()->itemAt(1)->widget()->setVisible(false);
370         d_uiClassGroupBox->adjustSize();
371     }
372     setButtonedWindowTitle(titleString);
373
374 }
375
376
377 void DCToolWindowCellCodeEditor::switchEditor()
378 {
379     saveScriptToFile();
380     if (d_mode == CELLCLASS_EDIT_MODE)
381     {
382         showCustomScript();
383     }
384     else
385     {
386         showClassScript();
387     }
388 }
389
390 void DCToolWindowCellCodeEditor::saveScriptToFile()
391 {
392     switch(d_mode)
393     {
394     case CELLCLASS_EDIT_MODE:
395         if (d_cellCodeClass)
396             d_cellCodeClass->saveScript(d_editor->toPlainText());
397         break;
398
399     case CUSTOMSCRIPT_EDIT_MODE:
400         if (d_ownerCell)
401             d_ownerCell->saveCustomScript(d_editor->toPlainText());
402         break;
403
404     }
405
406 }
407
408 void DCToolWindowCellCodeEditor::finishEditing()
409 {
410     saveScriptToFile();
411     getController()->doCommandFinishEditCellCode(this);
412 }
413
414 void DCToolWindowCellCodeEditor::unsetCellCodeButtonClicked()
415 {
416     getController()->doCommandUnassignCellCodeClassFromCell(this, d_ownerCell);
417 }
418
419 void DCToolWindowCellCodeEditor::assignCellCodeButtonClicked()
420 {
421     DCAssignCellCodeClassDialog dialog(d_container, getController());
422     dialog.exec();
423     DCCellCode *cellCode = dialog.getSelectedCellCodeClass();
424     if (cellCode)
425     {
426         getController()->doCommandAssignCellCodeClassToCell(this, d_ownerCell, cellCode);
427     }
428 }
429
430 void DCToolWindowCellCodeEditor::classForCellChanged()
431 {
432     setData(d_container, d_ownerCell->getCellCode(), d_ownerCell);
433 }
434
435 void DCToolWindowCellCodeEditor::commandExecuted(const QUndoCommand *executedCommand)
436 {
437     if (getIsOnStage())
438         updateInformation();
439 }