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.
19 #include "dctoolwindowcellcodeeditor.h"
21 #include "dccodeeditor.h"
22 #include "dccontainer.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"
36 #include <QVBoxLayout>
37 #include <QHBoxLayout>
38 #include <QStackedLayout>
39 #include <QSpacerItem>
42 DCToolWindowCellCodeEditor::DCToolWindowCellCodeEditor(DCCreator *creator) :
43 DCToolWindowBase("CELL CODE:", creator), d_mode(CELLCLASS_EDIT_MODE), d_container(NULL), d_cellCodeClass(NULL), d_ownerCell(NULL)
45 setWindowOpacity(0.95);
46 d_editor = new DCCodeEditor;
48 QHBoxLayout *layout = new QHBoxLayout;
49 layout->addWidget(d_editor);
51 QVBoxLayout *toolLayout = new QVBoxLayout;
52 d_uiClassGroupBox = new QGroupBox(tr("Class"));
54 QGridLayout *cellclassGroupBoxLayoutInnder1 = new QGridLayout;
56 cellclassGroupBoxLayoutInnder1->addWidget(new QLabel(tr("name")), 0,0);
57 cellclassGroupBoxLayoutInnder1->addWidget(new QLabel(tr("type")), 1,0);
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);
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);
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);
81 d_uiClassGroupBox->setLayout(cellclassGroupBoxLayoutOuter);
82 d_uiClassGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
83 toolLayout->addWidget(d_uiClassGroupBox);
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);
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);
99 d_receptors = new DCEditableTreeView(this);
100 d_receptors->setHeaderHidden(true);
101 d_receptors->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
103 d_axonTerminals = new DCEditableTreeView(this);
104 d_axonTerminals->setHeaderHidden(true);
105 d_axonTerminals->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
108 d_receptorItemModel = new DCQtItemModel(QStringList("name"));
109 d_axonTerminalItemModel = new DCQtItemModel(QStringList("name"));
111 d_receptors->setModel(d_receptorItemModel);
112 d_axonTerminals->setModel(d_axonTerminalItemModel);
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);
118 d_uiCellGroupBox->setLayout(cellGroupBoxLayout);
120 toolLayout->addWidget(d_uiCellGroupBox);
122 toolLayout->addItem(new QSpacerItem(10,10, QSizePolicy::Minimum, QSizePolicy::Expanding));
124 layout->addLayout(toolLayout);
126 d_uiCloseButton = new QPushButton("Close");
128 QHBoxLayout *buttonLayout = new QHBoxLayout;
129 buttonLayout->addItem(new QSpacerItem(10,10, QSizePolicy::Expanding));
130 buttonLayout->addWidget(d_uiCloseButton);
132 contentLayout()->addLayout(layout);
133 contentLayout()->addLayout(buttonLayout);
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*)));
142 DCToolWindowCellCodeEditor::~DCToolWindowCellCodeEditor()
144 d_receptors->disconnect(this);
145 d_axonTerminals->disconnect(this);
146 getController()->disconnect(this);
148 if (d_receptorItemModel)
149 d_receptorItemModel->deleteLater();
151 if (d_axonTerminalItemModel)
152 d_axonTerminalItemModel->deleteLater();
155 void DCToolWindowCellCodeEditor::setData(DCContainer *container, DCCellCode *cellCodeClass, DCCell *ownerCell)
157 if (d_ownerCell && d_ownerCell != ownerCell)
159 d_ownerCell->disconnect(this);
162 connect(ownerCell, SIGNAL(cellCodeChanged()), this, SLOT(classForCellChanged()));
164 d_container = container;
165 d_cellCodeClass = cellCodeClass;
166 d_ownerCell = ownerCell;
168 d_uiClassType->setEditingCellCode(cellCodeClass);
169 d_uiCellType->setEditingCell(ownerCell);
175 void DCToolWindowCellCodeEditor::setData(DCContainer *container, DCCell *ownerCell)
177 if (d_ownerCell && d_ownerCell != ownerCell)
179 d_ownerCell->disconnect(this);
182 connect(ownerCell, SIGNAL(cellCodeChanged()), this, SLOT(classForCellChanged()));
184 d_container = container;
185 d_ownerCell = ownerCell;
189 d_cellCodeClass = ownerCell->getCellCode();
192 d_uiClassType->setEditingCellCode(d_cellCodeClass);
193 d_uiCellType->setEditingCell(d_ownerCell);
199 void DCToolWindowCellCodeEditor::showClassScript()
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)
206 d_editor->setPlainText(d_cellCodeClass->getOwnScript());
207 d_editor->setReadOnly(false);
211 d_editor->setPlainText("");
212 d_editor->setReadOnly(true);
216 void DCToolWindowCellCodeEditor::showCustomScript()
218 d_mode = CUSTOMSCRIPT_EDIT_MODE;
219 d_uiClassGroupBox->setVisible(false);
220 d_uiSwitchEditorButton->setText(tr("Switch to Cell Code"));
223 d_editor->setPlainText(d_ownerCell->getCustomScript());
224 d_editor->setReadOnly(false);
228 d_editor->setPlainText("");
229 d_editor->setReadOnly(true);
233 void DCToolWindowCellCodeEditor::updateInformation()
235 d_receptorItemModel->removeAllItems();
236 d_axonTerminalItemModel->removeAllItems();
238 //update cell information
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());
246 d_receptorItemModel->insertString("receptors");
247 d_receptorItemModel->setReadOnly(0,true);
249 d_axonTerminalItemModel->insertString("axonTerminals");
250 d_axonTerminalItemModel->setReadOnly(0,true);
252 const TKReceptorMap *receptors = d_ownerCell->getReceptors();
253 TKReceptorMap::const_iterator it = receptors->begin();
254 QModelIndex rroot = d_receptorItemModel->index(0,0);
256 while( it != receptors->end())
258 QString receptorName = QString::fromStdString((*it).first);
259 d_receptorItemModel->insertString(receptorName,rroot);
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++)
269 DCReceptor *targetReceptor = NULL;
270 DCCell *targetCell = NULL;
271 DCAxonTerminal *tarminal = axon->getTerminalAt(i);
274 targetReceptor = dynamic_cast<DCReceptor*>(tarminal->getTarget());
277 targetCell = dynamic_cast<DCCell*>(targetReceptor->getOwnerCell());
280 QString axonTerminalName;
284 QTextStream(&axonTerminalName)
285 << QString::fromStdString(targetCell->getLocation())
287 << QString::fromStdString(targetCell->getName())
289 << QString::fromStdString(targetCell->getReceptorName(targetReceptor));
293 axonTerminalName = "-";
296 d_axonTerminalItemModel->insertString(axonTerminalName,aroot);
298 d_receptors->expandAll();
299 d_axonTerminals->expandAll();
303 d_uiCellName->setText("");
304 d_uiCellPath->setText("");
305 d_uiCellType->updateSelection();
308 //update cell code information
311 d_uiClassName->setText(QString::fromStdString(d_cellCodeClass->getFQNName()));
312 d_uiClassType->updateSelection();
316 d_uiClassName->setText("");
317 d_uiClassType->updateSelection();
321 // update title and tool visibility
322 QString titleString = "";
325 titleString.append("CELL : ");
326 titleString.append(QString::fromStdString(d_ownerCell->getName()));
327 titleString.append(" ");
329 d_uiCellGroupBox->setVisible(true);
330 if (d_container->isScriptableCell(d_ownerCell))
332 if (d_ownerCell->getIsCellCodeClassAssgined())
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();
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();
354 d_uiClassGroupBox->setVisible(false);
355 d_uiClassGroupBox->layout()->itemAt(0)->widget()->setVisible(false);
356 d_uiClassGroupBox->layout()->itemAt(1)->widget()->setVisible(false);
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();
372 setButtonedWindowTitle(titleString);
377 void DCToolWindowCellCodeEditor::switchEditor()
380 if (d_mode == CELLCLASS_EDIT_MODE)
390 void DCToolWindowCellCodeEditor::saveScriptToFile()
394 case CELLCLASS_EDIT_MODE:
396 d_cellCodeClass->saveScript(d_editor->toPlainText());
399 case CUSTOMSCRIPT_EDIT_MODE:
401 d_ownerCell->saveCustomScript(d_editor->toPlainText());
408 void DCToolWindowCellCodeEditor::finishEditing()
411 getController()->doCommandFinishEditCellCode(this);
414 void DCToolWindowCellCodeEditor::unsetCellCodeButtonClicked()
416 getController()->doCommandUnassignCellCodeClassFromCell(this, d_ownerCell);
419 void DCToolWindowCellCodeEditor::assignCellCodeButtonClicked()
421 DCAssignCellCodeClassDialog dialog(d_container, getController());
423 DCCellCode *cellCode = dialog.getSelectedCellCodeClass();
426 getController()->doCommandAssignCellCodeClassToCell(this, d_ownerCell, cellCode);
430 void DCToolWindowCellCodeEditor::classForCellChanged()
432 setData(d_container, d_ownerCell->getCellCode(), d_ownerCell);
435 void DCToolWindowCellCodeEditor::commandExecuted(const QUndoCommand *executedCommand)