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 "dctoolwindowcelleditor.h"
21 #include "dceditablelabel.h"
22 #include "dceditabletreeview.h"
24 #include "dccreator.h"
25 #include "dcreceptor.h"
27 #include "dccellcode.h"
28 #include "dcaxonterminal.h"
29 #include "dccontainer.h"
30 #include "utils/dcresources.h"
31 #include "utils/dcqtitemmodel.h"
33 #include <QGridLayout>
36 #include <QSizePolicy>
39 DCToolWindowCellEditor::DCToolWindowCellEditor(DCCreator *creator) :
40 DCToolWindowBase("CELL:", creator), d_cell(NULL)
42 d_layout = new QGridLayout;
43 d_layout->setSpacing(6);
44 d_layout->setContentsMargins(0,0,0,0);
45 d_layout->addWidget(new QLabel(tr("page")),0,0);
46 d_layout->addWidget(new QLabel(tr("type")),1,0);
47 d_layout->addWidget(new QLabel(tr("cell code")),2,0);
48 d_layout->addWidget(new QLabel(tr("custom script")),3,0);
50 d_detailButton = new QPushButton(tr("detail..."));
51 d_detailButton->setStyleSheet("background-color: rgba(255,255,255,60);");
53 d_layout->addWidget(d_detailButton,6,0,1,2);
55 d_textPage = new DCEditableLabel(this, "");
56 d_textType = new DCEditableLabel(this, "");
57 d_layout->addWidget(d_textPage, 0,1);
58 d_layout->addWidget(d_textType, 1,1);
60 d_classButton = new QPushButton("...");
61 QPushButton *customScriptEditButton = new QPushButton(tr("edit..."));
63 d_layout->addWidget(d_classButton, 2,1);
64 d_layout->addWidget(customScriptEditButton, 3,1);
66 d_receptors = new DCEditableTreeView(this);
67 d_receptors->setHeaderHidden(true);
68 d_receptors->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
70 d_axonTerminals = new DCEditableTreeView(this);
71 d_axonTerminals->setHeaderHidden(true);
72 d_axonTerminals->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
75 d_receptorItemModel = new DCQtItemModel(QStringList("name"));
76 d_axonTerminalItemModel = new DCQtItemModel(QStringList("name"));
78 d_receptors->setModel(d_receptorItemModel);
79 d_axonTerminals->setModel(d_axonTerminalItemModel);
81 d_layout->addWidget(d_receptors, 4,0,1,2);
82 d_layout->addWidget(d_axonTerminals, 5, 0, 1, 2);
84 contentLayout()->addLayout(d_layout);
86 connect(d_receptors, SIGNAL(collapsed(QModelIndex)), this, SLOT(slotReceptorTreeCollapsed(QModelIndex)));
87 connect(d_receptors, SIGNAL(expanded(QModelIndex)), this, SLOT(slotReceptorTreeExpanded(QModelIndex)));
88 connect(d_receptors, SIGNAL(clicked(QModelIndex)), this, SLOT(slotReceptorItemClicked(QModelIndex)));
89 connect(d_receptors, SIGNAL(vscrollbarShown()), this, SLOT(adjustTreeColumnWidth()));
90 connect(d_receptors, SIGNAL(vscrollbarHidden()), this, SLOT(adjustTreeColumnWidth()));
91 connect(d_axonTerminals, SIGNAL(collapsed(QModelIndex)), this, SLOT(slotAxonTerminalTreeCollapsed(QModelIndex)));
92 connect(d_axonTerminals, SIGNAL(expanded(QModelIndex)), this, SLOT(slotAxonTerminalTreeExpanded(QModelIndex)));
93 connect(d_axonTerminals, SIGNAL(clicked(QModelIndex)), this, SLOT(slotAxonTerminalItemClicked(QModelIndex)));
94 connect(d_axonTerminals, SIGNAL(vscrollbarShown()), this, SLOT(adjustTreeColumnWidth()));
95 connect(d_axonTerminals, SIGNAL(vscrollbarHidden()), this, SLOT(adjustTreeColumnWidth()));
97 connect(d_classButton, SIGNAL(clicked()), this, SLOT(slotCellCodeEditButtonPressed()));
98 connect(customScriptEditButton, SIGNAL(clicked()), this, SLOT(slotCustomScriptEditButtonPressed()));
102 DCToolWindowCellEditor::~DCToolWindowCellEditor()
104 d_receptors->disconnect(this);
105 d_axonTerminals->disconnect(this);
107 if (d_receptorItemModel)
108 d_receptorItemModel->deleteLater();
110 if (d_axonTerminalItemModel)
111 d_axonTerminalItemModel->deleteLater();
114 void DCToolWindowCellEditor::setCell(DCCell *cell)
117 QString title = "CELL:";
120 title.append(QString::fromStdString(cell->getName()));
121 if (cell->getCellCode() && cell->getCellCode() != getController()->getCurrentContainer()->getEmptyCellCodeClass())
123 d_classButton->setText(QString::fromStdString(cell->getCellCode()->getName()));
127 d_classButton->setText("...");
129 connect(cell, SIGNAL(destroyed(QObject*)), this, SLOT(slotCellDestroyed()));
132 setButtonedWindowTitle(title);
137 void DCToolWindowCellEditor::updateView()
139 d_receptorItemModel->removeAllItems();
140 d_axonTerminalItemModel->removeAllItems();
144 d_textPage->setText(QString::fromStdString(d_cell->getLocation()));
145 d_textType->setText(d_cell->getType());
147 d_receptorItemModel->insertString("receptors");
148 d_receptorItemModel->insertColumns(1,2);
149 d_receptorItemModel->setReadOnly(0,false);
150 d_receptorItemModel->setData(d_receptorItemModel->index(0,1), QVariant::fromValue(DCResources::addItemIcon()));
152 d_axonTerminalItemModel->insertString("axonTerminals");
153 d_axonTerminalItemModel->insertColumns(1,2);
154 d_axonTerminalItemModel->setReadOnly(0,false);
155 d_axonTerminalItemModel->setData(d_axonTerminalItemModel->index(0,1), QVariant::fromValue(DCResources::addItemIcon()));
157 const TKReceptorMap *receptors = d_cell->getReceptors();
158 TKReceptorMap::const_iterator it = receptors->begin();
159 QModelIndex rroot = d_receptorItemModel->index(0,0);
161 while( it != receptors->end())
164 QString receptorName = QString::fromStdString((*it).first);
165 d_receptorItemModel->insertString(receptorName,rroot);
166 d_receptorItemModel->setData(rroot.child(i,2), QVariant::fromValue(DCResources::deleteItemIcon()));
171 QModelIndex aroot = d_axonTerminalItemModel->index(0,0);
172 DCAxon *axon = d_cell->getAxon();
173 int acnt = axon->getNumberOfTerminals();
174 for (int i = 0; i < acnt; i++)
176 DCReceptor *targetReceptor = NULL;
177 DCCell *targetCell = NULL;
178 DCAxonTerminal *tarminal = axon->getTerminalAt(i);
181 targetReceptor = dynamic_cast<DCReceptor*>(tarminal->getTarget());
184 targetCell = dynamic_cast<DCCell*>(targetReceptor->getOwnerCell());
187 QString axonTerminalName;
191 QTextStream(&axonTerminalName)
192 << QString::fromStdString(targetCell->getLocation())
194 << QString::fromStdString(targetCell->getName())
196 << QString::fromStdString(targetCell->getReceptorName(targetReceptor));
200 axonTerminalName = "-";
203 d_axonTerminalItemModel->insertString(axonTerminalName,aroot);
204 d_receptorItemModel->setData(aroot.child(i,2), QVariant::fromValue(DCResources::deleteItemIcon()));
206 d_receptors->expandAll();
207 d_axonTerminals->expandAll();
213 void DCToolWindowCellEditor::slotReceptorTreeCollapsed(const QModelIndex &index)
218 void DCToolWindowCellEditor::slotReceptorTreeExpanded(const QModelIndex &index)
223 void DCToolWindowCellEditor::slotReceptorItemClicked(const QModelIndex &index)
225 switch (index.column())
228 qDebug() << "add item clicked..";
229 if (!index.parent().isValid())
231 getController()->doCommandStartAddAxonTerminalFromReceptor(this, d_cell);
236 qDebug() << "delete item clicked..";
237 if (index.parent().row() == 0)
239 int itemRowIndex = index.row();
240 if (itemRowIndex >= 0)
242 QModelIndex itemIndex = d_receptorItemModel->index(itemRowIndex,0,index.parent());
243 QString receptorName = d_receptorItemModel->data(itemIndex,Qt::DisplayRole).toString();
244 if (receptorName.length()>0)
246 d_receptors->setCurrentIndex(itemIndex);
247 getController()->doCommandRemoveAxonTerminal(this, d_cell, receptorName);
254 qDebug() << "other clicked..";
261 void DCToolWindowCellEditor::slotAxonTerminalTreeCollapsed(const QModelIndex &index)
266 void DCToolWindowCellEditor::slotAxonTerminalTreeExpanded(const QModelIndex &index)
271 void DCToolWindowCellEditor::slotAxonTerminalItemClicked(const QModelIndex &index)
273 switch (index.column())
276 qDebug() << "add item clicked..";
277 if (!index.parent().isValid())
279 getController()->doCommandStartAddAxonTerminalFromAxon(this, d_cell);
284 qDebug() << "delete item clicked..";
285 if (index.parent().row() == 0)
287 int itemIndex = index.row();
290 d_axonTerminals->setCurrentIndex(d_axonTerminalItemModel->index(itemIndex,0,index.parent()));
291 getController()->doCommandRemoveAxonTerminal(this, d_cell, d_cell->getAxon()->getTerminalAt(itemIndex));
297 qDebug() << "other clicked..";
303 void DCToolWindowCellEditor::slotCellCodeEditButtonPressed()
307 getController()->doCommandStartEditCellCode(this, d_cell);
311 void DCToolWindowCellEditor::slotCustomScriptEditButtonPressed()
315 getController()->doCommandStartEditCellCode(this, d_cell);
320 // There may be better / proper way to do this.
321 // The goal is to adjust the window height to fit all inside components
322 // with its desigered size.
323 void DCToolWindowCellEditor::resizeView()
326 int spacing = d_layout->verticalSpacing();
327 for (int i = 0 ; i < d_layout->rowCount(); i++ )
329 int maxHeightInRow = 0;
330 for (int j = 0; j < d_layout->columnCount(); j++)
332 QLayoutItem *item = d_layout->itemAtPosition(i,j);
335 QWidget *pWidget = item->widget();
338 QSize sh = pWidget->sizeHint();
339 pWidget->setFixedHeight(sh.height());
340 if (sh.height() > maxHeightInRow)
342 maxHeightInRow = sh.height();
347 totalHeight += maxHeightInRow + spacing;
351 int left,top,right,bottom;
352 contentLayout()->getContentsMargins(&left,&top,&right,&bottom);
353 s.setHeight(totalHeight + getTitleButtonHeight() + top + bottom);
356 adjustTreeColumnWidth();
359 void DCToolWindowCellEditor::adjustTreeColumnWidth()
361 d_receptors->setColumnWidth(1, 18);
362 d_receptors->setColumnWidth(2, 18);
363 QScrollBar *sb = d_receptors->verticalScrollBar();
364 if (sb && sb->isVisible())
365 d_receptors->setColumnWidth(0, d_receptors->width()-44 - sb->width());
367 d_receptors->setColumnWidth(0, d_receptors->width()-42);
369 d_axonTerminals->setColumnWidth(1, 18);
370 d_axonTerminals->setColumnWidth(2, 18);
371 d_axonTerminals->setColumnWidth(0, d_axonTerminals->width()-42);
372 sb = d_axonTerminals->verticalScrollBar();
373 if (sb && sb->isVisible())
374 d_axonTerminals->setColumnWidth(0, d_axonTerminals->width()-44 - sb->width());
376 d_axonTerminals->setColumnWidth(0, d_axonTerminals->width()-42);
379 void DCToolWindowCellEditor::slotCellDestroyed()