Bug Summary

File:OMEdit/OMEditLIB/OMS/BusDialog.cpp
Warning:line 675, column 9
Value stored to 'pConnectorItem' is never read

Annotated Source Code

[?] Use j/k keys for keyboard navigation

1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
13 * OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
14 *
15 * The OpenModelica software and the Open Source Modelica
16 * Consortium (OSMC) Public License (OSMC-PL) are obtained
17 * from OSMC, either from the above address,
18 * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
19 * http://www.openmodelica.org, and in the OpenModelica distribution.
20 * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This program is distributed WITHOUT ANY WARRANTY; without
23 * even the implied warranty of MERCHANTABILITY or FITNESS
24 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
25 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
26 *
27 * See the full OSMC Public License conditions for more details.
28 *
29 */
30/*
31 * @author Adeel Asghar <adeel.asghar@liu.se>
32 */
33
34#include "BusDialog.h"
35#include "Util/Helper.h"
36#include "Modeling/ModelWidgetContainer.h"
37#include "Component/Component.h"
38#include "Modeling/ItemDelegate.h"
39#include "Modeling/Commands.h"
40
41#include <QMessageBox>
42
43/*!
44 * \class ConnectorItem
45 * \brief Contains the information about the Connector component.
46 */
47/*!
48 * \brief ConnectorItem::ConnectorItem
49 * \param pComponent
50 * \param pParent
51 */
52ConnectorItem::ConnectorItem(Component *pComponent, ConnectorItem *pParent)
53{
54 mText = "";
55 mpComponent = pComponent;
56 mTLMType = "";
57 mTLMTypeDescription = "";
58 mpParentConnectorItem = pParent;
59 mChecked = false;
60}
61
62Qt::CheckState ConnectorItem::checkState() const
63{
64 if (mpComponent) {
65 return isChecked() ? Qt::Checked : Qt::Unchecked;
66 } else {
67 if (mChildren.isEmpty()) {
68 return Qt::Unchecked;
69 }
70 Qt::CheckState state = mChildren.first()->checkState();
71 foreach (ConnectorItem *pConnectorItem, mChildren)for (auto _container_ = QtPrivate::qMakeForeachContainer(mChildren
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (ConnectorItem
*pConnectorItem = *_container_.i; _container_.control; _container_
.control = 0)
{
72 Qt::CheckState connectorState = pConnectorItem->checkState();
73 if (connectorState != state) {
74 return Qt::PartiallyChecked;
75 }
76 }
77 return state;
78 }
79}
80
81/*!
82 * \brief ConnectorItem::row
83 * Returns the row number corresponding to ConnectorItem.
84 * \return
85 */
86int ConnectorItem::row() const
87{
88 if (mpParentConnectorItem) {
89 return mpParentConnectorItem->mChildren.indexOf(const_cast<ConnectorItem*>(this));
90 }
91
92 return 0;
93}
94
95/*!
96 * \class ConnectorsModel
97 * \brief A model for Connector components.
98 */
99/*!
100 * \brief ConnectorsModel::ConnectorsModel
101 * \param parent
102 */
103ConnectorsModel::ConnectorsModel(QObject *parent)
104 : QAbstractItemModel(parent)
105{
106 mpRootConnectorItem = new ConnectorItem(0, 0);
107 mColumnCount = 1;
108 mTLMTypes.clear();
109 mTLMTypesDescriptions.clear();
110}
111
112/*!
113 * \brief ConnectorsModel::columnCount
114 * Returns the number of columns for the children of the given parent.
115 * \param parent
116 * \return
117 */
118int ConnectorsModel::columnCount(const QModelIndex &parent) const
119{
120 Q_UNUSED(parent)(void)parent;;
121 return mColumnCount;
122}
123
124/*!
125 * \brief ConnectorsModel::rowCount
126 * Returns the number of rows under the given parent.
127 * When the parent is valid it means that rowCount is returning the number of children of parent.
128 * \param parent
129 * \return
130 */
131int ConnectorsModel::rowCount(const QModelIndex &parent) const
132{
133 ConnectorItem *pParentConnectorItem;
134 if (parent.column() > 0) {
135 return 0;
136 }
137
138 if (!parent.isValid()) {
139 pParentConnectorItem = mpRootConnectorItem;
140 } else {
141 pParentConnectorItem = static_cast<ConnectorItem*>(parent.internalPointer());
142 }
143 return pParentConnectorItem->childrenSize();
144}
145
146/*!
147 * \brief ConnectorsModel::index
148 * Returns the index of the item in the model specified by the given row, column and parent index.
149 * \param row
150 * \param column
151 * \param parent
152 * \return
153 */
154QModelIndex ConnectorsModel::index(int row, int column, const QModelIndex &parent) const
155{
156 ConnectorItem *pParentConnectorItem;
157 if (!parent.isValid()) {
158 pParentConnectorItem = mpRootConnectorItem;
159 } else {
160 pParentConnectorItem = static_cast<ConnectorItem*>(parent.internalPointer());
161 }
162
163 ConnectorItem *pChildConnectorItem = pParentConnectorItem->child(row);
164 if (pChildConnectorItem) {
165 return createIndex(row, column, pChildConnectorItem);
166 } else {
167 return QModelIndex();
168 }
169}
170
171/*!
172 * \brief ConnectorsModel::parent
173 * Finds the parent for QModelIndex
174 * \param index
175 * \return
176 */
177QModelIndex ConnectorsModel::parent(const QModelIndex &index) const
178{
179 if (!index.isValid()) {
180 return QModelIndex();
181 }
182
183 ConnectorItem *pChildConnectorItem = static_cast<ConnectorItem*>(index.internalPointer());
184 ConnectorItem *pParentConnectorItem = pChildConnectorItem->parent();
185 if (pParentConnectorItem == mpRootConnectorItem)
186 return QModelIndex();
187
188 return createIndex(pParentConnectorItem->row(), 0, pParentConnectorItem);
189}
190
191/*!
192 * \brief ConnectorsModel::setData
193 * Updates the model data.
194 * \param index
195 * \param value
196 * \param role
197 * \return
198 */
199bool ConnectorsModel::setData(const QModelIndex &index, const QVariant &value, int role)
200{
201 ConnectorItem *pConnectorItem = static_cast<ConnectorItem*>(index.internalPointer());
202 if (!pConnectorItem) {
203 return false;
204 }
205
206 if (index.column() == 0 && role == Qt::CheckStateRole) {
207 if (pConnectorItem->getComponent()) {
208 if (value.toInt() == Qt::Checked) {
209 pConnectorItem->setChecked(true);
210 } else if (value.toInt() == Qt::Unchecked) {
211 pConnectorItem->setChecked(false);
212 }
213 emit dataChanged(index, index);
214 const QModelIndex parentIndex = createIndex(pConnectorItem->parent()->row(), 0, pConnectorItem->parent());
215 emit dataChanged(parentIndex, parentIndex);
216 } else {
217 for (int i = 0 ; i < pConnectorItem->childrenSize() ; i++) {
218 ConnectorItem *pChildConnectorItem = pConnectorItem->childAt(i);
219 QModelIndex childIndex = createIndex(pChildConnectorItem->row(), 0, pChildConnectorItem);
220 setData(childIndex, value, role);
221 }
222 }
223 return true;
224 } else if (index.column() == 1 && role == Qt::EditRole) {
225 pConnectorItem->setTLMType(value.toString());
226 emit dataChanged(index, index);
227 return true;
228 }
229 return QAbstractItemModel::setData(index, value, role);
230}
231
232/*!
233 * \brief ConnectorsModel::data
234 * Returns the ConnectorItem data.
235 * \param index
236 * \param role
237 * \return
238 */
239QVariant ConnectorsModel::data(const QModelIndex &index, int role) const
240{
241 if (!index.isValid()) {
242 return QVariant();
243 }
244
245 ConnectorItem *pConnectorItem = static_cast<ConnectorItem*>(index.internalPointer());
246
247 switch (index.column()) {
248 case 0:
249 switch (role) {
250 case Qt::DisplayRole:
251 if (pConnectorItem->getComponent()) {
252 if (pConnectorItem->getComponent()->getLibraryTreeItem()) {
253 return pConnectorItem->getComponent()->getLibraryTreeItem()->getName();
254 } else {
255 return "";
256 }
257 } else {
258 return pConnectorItem->getText();
259 }
260 case Qt::ToolTipRole:
261 if (pConnectorItem->getComponent()) {
262 if (pConnectorItem->getComponent()->getLibraryTreeItem()) {
263 return pConnectorItem->getComponent()->getLibraryTreeItem()->getNameStructure();
264 } else {
265 return "";
266 }
267 } else {
268 return pConnectorItem->getText();
269 }
270 case Qt::CheckStateRole:
271 return pConnectorItem->checkState();
272 default:
273 return QVariant();
274 }
275 case 1:
276 switch (role) {
277 case Qt::DisplayRole:
278 if (pConnectorItem->getComponent()) {
279 return pConnectorItem->getTLMType();
280 } else {
281 return QVariant();
282 }
283 case Qt::ToolTipRole:
284 if (pConnectorItem->getComponent()) {
285 return pConnectorItem->getTLMTypeDescription();
286 } else {
287 return QVariant();
288 }
289 default:
290 return QVariant();
291 }
292 default:
293 return QVariant();
294 }
295}
296
297/*!
298 * \brief ConnectorsModel::flags
299 * Returns the ConnectorItem flags.
300 * \param index
301 * \return
302 */
303Qt::ItemFlags ConnectorsModel::flags(const QModelIndex &index) const
304{
305 if (!index.isValid()) {
306 return 0;
307 } else {
308 Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
309 ConnectorItem *pConnectorItem = static_cast<ConnectorItem*>(index.internalPointer());
310 if (index.column() == 0) {
311 if (!pConnectorItem->getComponent() && pConnectorItem->childrenSize() == 0) {
312 return flags;
313 } else {
314 return flags | Qt::ItemIsUserCheckable;
315 }
316 } else if (index.column() == 1 && pConnectorItem->getComponent()) {
317 return flags | Qt::ItemIsEditable;
318 } else {
319 return flags;
320 }
321 }
322}
323
324/*!
325 * \brief ConnectorsModel::connectorItemIndex
326 * Finds the QModelIndex attached to ConnectorItem.
327 * \param pConnectorItem
328 * \return
329 */
330QModelIndex ConnectorsModel::connectorItemIndex(const ConnectorItem *pConnectorItem, const int column) const
331{
332 return connectorItemIndexHelper(pConnectorItem, column, mpRootConnectorItem, QModelIndex());
333}
334
335/*!
336 * \brief ConnectorsModel::createConnectorItem
337 * Creates the ConnectorItem and returns it.
338 * \param pComponent
339 * \param pParent
340 * \return
341 */
342ConnectorItem* ConnectorsModel::createConnectorItem(Component *pComponent, ConnectorItem *pParent)
343{
344 int row = pParent->childrenSize();
345 beginInsertRows(connectorItemIndex(pParent), row, row);
346 ConnectorItem *pConnectorItem = new ConnectorItem(pComponent, pParent);
347 pParent->insertChild(row, pConnectorItem);
348 endInsertRows();
349 return pConnectorItem;
350}
351
352/*!
353 * \brief ConnectorsModel::connectorItemIndexHelper
354 * Helper function for ConnectorsModel::connectorItemIndex()
355 * \param pConnectorItem
356 * \param pParentConnectorItem
357 * \param parentIndex
358 * \return
359 */
360QModelIndex ConnectorsModel::connectorItemIndexHelper(const ConnectorItem *pConnectorItem, const int column,
361 const ConnectorItem *pParentConnectorItem, const QModelIndex &parentIndex) const
362{
363 if (pConnectorItem == pParentConnectorItem) {
364 return parentIndex;
365 }
366 for (int i = pParentConnectorItem->childrenSize(); --i >= 0; ) {
367 const ConnectorItem *childItem = pParentConnectorItem->childAt(i);
368 QModelIndex childIndex = index(i, column, parentIndex);
369 QModelIndex index = connectorItemIndexHelper(pConnectorItem, column, childItem, childIndex);
370 if (index.isValid()) {
371 return index;
372 }
373 }
374 return QModelIndex();
375}
376
377/*!
378 * \class ConnectorsTreeView
379 * \brief A treeview for connectors.
380 */
381/*!
382 * \brief ConnectorsTreeView::ConnectorsTreeView
383 * \param pParent
384 */
385ConnectorsTreeView::ConnectorsTreeView(QWidget *pParent)
386 : QTreeView(pParent)
387{
388 setItemDelegate(new ItemDelegate(this));
389 setHeaderHidden(true);
390 setIndentation(Helper::treeIndentation);
391 setTextElideMode(Qt::ElideMiddle);
392 setUniformRowHeights(true);
393}
394
395/*!
396 * \class AddBusDialog
397 * \brief A dialog for creating a bus.
398 */
399/*!
400 * \brief AddBusDialog::AddBusDialog
401 * \param components
402 * \param pLibraryTreeItem
403 * \param pGraphicsView
404 */
405AddBusDialog::AddBusDialog(QList<Component *> components, LibraryTreeItem *pLibraryTreeItem, GraphicsView *pGraphicsView)
406 : QDialog(pGraphicsView)
407{
408 setAttribute(Qt::WA_DeleteOnClose);
409 setWindowTitle(QString("%1 - %2").arg(Helper::applicationName).arg(pLibraryTreeItem ? Helper::editBus : Helper::addBus));
410 setMinimumWidth(400);
411 mpLibraryTreeItem = pLibraryTreeItem;
412 mpGraphicsView = pGraphicsView;
413 // set heading
414 mpHeading = mpLibraryTreeItem ? Utilities::getHeadingLabel(Helper::editBus) : Utilities::getHeadingLabel(Helper::addBus);
415 // set separator line
416 mpHorizontalLine = Utilities::getHeadingLine();
417 // name
418 mpNameLabel = new Label(Helper::name);
419 mpNameTextBox = new QLineEdit(mpLibraryTreeItem ? mpLibraryTreeItem->getName() : "");
420 // input connectors
421 mpInputConnectorsTreeModel = new ConnectorsModel(this);
422 mpInputConnectorsTreeView = new ConnectorsTreeView;
423 mpInputConnectorsTreeView->setModel(mpInputConnectorsTreeModel);
424 // inputs item
425 ConnectorItem *pInputsConnectorItem = mpInputConnectorsTreeModel->createConnectorItem(0, mpInputConnectorsTreeModel->getRootConnectorItem());
426 pInputsConnectorItem->setText("Input Connectors");
427 // output connectors
428 mpOutputConnectorsTreeModel = new ConnectorsModel(this);
429 mpOutputConnectorsTreeView = new ConnectorsTreeView;
430 mpOutputConnectorsTreeView->setModel(mpOutputConnectorsTreeModel);
431 // outputs item
432 ConnectorItem *pOutputsConnectorItem = mpInputConnectorsTreeModel->createConnectorItem(0, mpOutputConnectorsTreeModel->getRootConnectorItem());
433 pOutputsConnectorItem->setText("Output Connectors");
434 // add the connectors to input and output connectors tree views
435 foreach (Component* pComponent, mpGraphicsView->getComponentsList())for (auto _container_ = QtPrivate::qMakeForeachContainer(mpGraphicsView
->getComponentsList()); _container_.control && _container_
.i != _container_.e; ++_container_.i, _container_.control ^= 1
) for (Component* pComponent = *_container_.i; _container_.control
; _container_.control = 0)
{
436 if (pComponent->getLibraryTreeItem() && pComponent->getLibraryTreeItem()->getOMSConnector()
437 && (!pComponent->isInBus() || pComponent->getBusComponent()->getLibraryTreeItem() == mpLibraryTreeItem)) {
438 ConnectorItem *pConnectorItem = 0;
439 if (pComponent->getLibraryTreeItem()->getOMSConnector()->causality == oms_causality_input) {
440 pConnectorItem = mpInputConnectorsTreeModel->createConnectorItem(pComponent, pInputsConnectorItem);
441 } else if (pComponent->getLibraryTreeItem()->getOMSConnector()->causality == oms_causality_output) {
442 pConnectorItem = mpOutputConnectorsTreeModel->createConnectorItem(pComponent, pOutputsConnectorItem);
443 } else {
444 qDebugQMessageLogger(nullptr, 0, nullptr).debug() << "AddBusDialog::AddBusDialog() unknown causality" << pComponent->getLibraryTreeItem()->getOMSConnector()->causality;
445 Q_UNUSED(pConnectorItem)(void)pConnectorItem;;
446 }
447 }
448 }
449 // check which input connectors are already part of the bus
450 markExistingBusConnectors(pInputsConnectorItem, components);
451 // check which input connectors are already part of the bus
452 markExistingBusConnectors(pOutputsConnectorItem, components);
453 mpInputConnectorsTreeView->expandAll();
454 mpOutputConnectorsTreeView->expandAll();
455 // buttons
456 mpOkButton = new QPushButton(Helper::ok);
457 mpOkButton->setAutoDefault(true);
458 connect(mpOkButton, SIGNAL(clicked())"2""clicked()", SLOT(addBus())"1""addBus()");
459 mpCancelButton = new QPushButton(Helper::cancel);
460 mpCancelButton->setAutoDefault(false);
461 connect(mpCancelButton, SIGNAL(clicked())"2""clicked()", SLOT(reject())"1""reject()");
462 // add buttons to the button box
463 mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
464 mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
465 mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
466 // set the layout
467 QGridLayout *pMainLayout = new QGridLayout;
468 pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
469 pMainLayout->addWidget(mpHeading, 0, 0, 1, 2);
470 pMainLayout->addWidget(mpHorizontalLine, 1, 0, 1, 2);
471 pMainLayout->addWidget(mpNameLabel, 2, 0);
472 pMainLayout->addWidget(mpNameTextBox, 2, 1);
473 pMainLayout->addWidget(mpInputConnectorsTreeView, 3, 0, 1, 2);
474 pMainLayout->addWidget(mpOutputConnectorsTreeView, 4, 0, 1, 2);
475 pMainLayout->addWidget(mpButtonBox, 5, 0, 1, 2, Qt::AlignRight);
476 setLayout(pMainLayout);
477}
478
479/*!
480 * \brief AddBusDialog::markExistingBusConnectors
481 * Mark the existing bus connectors checked.
482 * \param pParentConnectorItem
483 * \param components
484 */
485void AddBusDialog::markExistingBusConnectors(ConnectorItem *pParentConnectorItem, QList<Component *> components)
486{
487 for (int i = 0 ; i < pParentConnectorItem->childrenSize() ; i++) {
488 ConnectorItem *pConnectorItem = pParentConnectorItem->childAt(i);
489 if (mpLibraryTreeItem && mpLibraryTreeItem->getOMSBusConnector() && mpLibraryTreeItem->getOMSBusConnector()->connectors) {
490 for (int j = 0 ; mpLibraryTreeItem->getOMSBusConnector()->connectors[j] ; j++) {
491 if (pConnectorItem->getComponent()->getName().compare(QString(mpLibraryTreeItem->getOMSBusConnector()->connectors[j])) == 0) {
492 pConnectorItem->setChecked(true);
493 break;
494 }
495 }
496 } else if (pConnectorItem && components.contains(pConnectorItem->getComponent())) {
497 pConnectorItem->setChecked(true);
498 components.removeOne(pConnectorItem->getComponent());
499 }
500 }
501}
502
503/*!
504 * \brief AddBusDialog::addBus
505 * Adds the bus by calling AddBusCommand
506 */
507void AddBusDialog::addBus()
508{
509 if (mpNameTextBox->text().isEmpty()) {
510 QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
511 GUIMessages::getMessage(GUIMessages::ENTER_NAME).arg(tr("Bus")), Helper::ok);
512 return;
513 }
514
515 QStringList connectors;
516 ConnectorItem *pInputConnectorsItem = mpInputConnectorsTreeModel->getRootConnectorItem()->childAt(0);
517 if (pInputConnectorsItem) {
518 for (int i = 0 ; i < pInputConnectorsItem->childrenSize() ; i++) {
519 ConnectorItem *pConnectorItem = pInputConnectorsItem->childAt(i);
520 if (pConnectorItem && pConnectorItem->isChecked()) {
521 connectors.append(pConnectorItem->getComponent()->getLibraryTreeItem()->getNameStructure());
522 }
523 }
524 }
525
526 ConnectorItem *pOutputConnectorsItem = mpOutputConnectorsTreeModel->getRootConnectorItem()->childAt(0);
527 if (pOutputConnectorsItem) {
528 for (int i = 0 ; i < pOutputConnectorsItem->childrenSize() ; i++) {
529 ConnectorItem *pConnectorItem = pOutputConnectorsItem->childAt(i);
530 if (pConnectorItem && pConnectorItem->isChecked()) {
531 connectors.append(pConnectorItem->getComponent()->getLibraryTreeItem()->getNameStructure());
532 }
533 }
534 }
535
536 LibraryTreeItem *pParentLibraryTreeItem = mpGraphicsView->getModelWidget()->getLibraryTreeItem();
537 QString bus = QString("%1.%2").arg(pParentLibraryTreeItem->getNameStructure()).arg(mpNameTextBox->text());
538
539 if (mpLibraryTreeItem) { // edit case
540 mpGraphicsView->getModelWidget()->beginMacro("Edit bus");
541
542 /*! @todo Rename the bus here.
543 */
544
545 QStringList existingConnectors;
546 if (mpLibraryTreeItem->getOMSBusConnector() && mpLibraryTreeItem->getOMSBusConnector()->connectors) {
547 for (int i = 0 ; mpLibraryTreeItem->getOMSBusConnector()->connectors[i] ; i++) {
548 existingConnectors.append(QString("%1.%2").arg(mpGraphicsView->getModelWidget()->getLibraryTreeItem()->getNameStructure())
549 .arg(QString(mpLibraryTreeItem->getOMSBusConnector()->connectors[i])));
550 }
551 }
552
553 // add connectors to the bus
554 QSet<QString> addConnectors = connectors.toSet().subtract(existingConnectors.toSet());
555 foreach (QString connector, addConnectors)for (auto _container_ = QtPrivate::qMakeForeachContainer(addConnectors
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (QString connector
= *_container_.i; _container_.control; _container_.control =
0)
{
556 AddConnectorToBusCommand *pAddConnectorToBusCommand = new AddConnectorToBusCommand(bus, connector, mpGraphicsView);
557 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddConnectorToBusCommand);
558 }
559 // delete connectors from the bus
560 QSet<QString> deleteConnectors = existingConnectors.toSet().subtract(connectors.toSet());
561 foreach (QString connector, deleteConnectors)for (auto _container_ = QtPrivate::qMakeForeachContainer(deleteConnectors
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (QString connector
= *_container_.i; _container_.control; _container_.control =
0)
{
562 DeleteConnectorFromBusCommand *pDeleteConnectorFromBusCommand = new DeleteConnectorFromBusCommand(bus, connector, mpGraphicsView);
563 mpGraphicsView->getModelWidget()->getUndoStack()->push(pDeleteConnectorFromBusCommand);
564 }
565 mpGraphicsView->getModelWidget()->updateModelText();
566 mpGraphicsView->getModelWidget()->getLibraryTreeItem()->handleIconUpdated();
567 accept();
568 mpGraphicsView->getModelWidget()->endMacro();
569 } else { // add case
570 mpGraphicsView->getModelWidget()->beginMacro("Add bus");
571 QString annotation = QString("Placement(true,%1,%2,-10.0,-10.0,10.0,10.0,0,%1,%2,-10.0,-10.0,10.0,10.0,)")
572 .arg(Utilities::mapToCoOrdinateSystem(0.5, 0, 1, -100, 100))
573 .arg(Utilities::mapToCoOrdinateSystem(0.5, 0, 1, -100, 100));
574 AddBusCommand *pAddBusCommand = new AddBusCommand(mpNameTextBox->text(), 0, annotation, mpGraphicsView, false);
575 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddBusCommand);
576 if (!pAddBusCommand->isFailed()) {
577 // add connectors to the bus
578 foreach (QString connector, connectors)for (auto _container_ = QtPrivate::qMakeForeachContainer(connectors
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (QString connector
= *_container_.i; _container_.control; _container_.control =
0)
{
579 AddConnectorToBusCommand *pAddConnectorToBusCommand = new AddConnectorToBusCommand(bus, connector, mpGraphicsView);
580 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddConnectorToBusCommand);
581 }
582 mpGraphicsView->getModelWidget()->getLibraryTreeItem()->emitComponentAddedForComponent();
583 mpGraphicsView->getModelWidget()->updateModelText();
584 mpGraphicsView->getModelWidget()->getLibraryTreeItem()->handleIconUpdated();
585 accept();
586 }
587 mpGraphicsView->getModelWidget()->endMacro();
588 }
589}
590
591/*!
592 * \class AddTLMBusDialog
593 * \brief A dialog for creating a tlm bus.
594 */
595/*!
596 * \brief AddTLMBusDialog::AddTLMBusDialog
597 * \param components
598 * \param pLibraryTreeItem
599 * \param pGraphicsView
600 */
601AddTLMBusDialog::AddTLMBusDialog(QList<Component *> components, LibraryTreeItem *pLibraryTreeItem, GraphicsView *pGraphicsView)
602 : QDialog(pGraphicsView)
603{
604 setAttribute(Qt::WA_DeleteOnClose);
605 setWindowTitle(QString("%1 - %2").arg(Helper::applicationName).arg(pLibraryTreeItem ? Helper::editTLMBus : Helper::addTLMBus));
606 setMinimumWidth(400);
607 mpLibraryTreeItem = pLibraryTreeItem;
608 mpGraphicsView = pGraphicsView;
609 // set heading
610 mpHeading = mpLibraryTreeItem ? Utilities::getHeadingLabel(Helper::editTLMBus) : Utilities::getHeadingLabel(Helper::addTLMBus);
611 // set separator line
612 mpHorizontalLine = Utilities::getHeadingLine();
613 // name
614 mpNameLabel = new Label(Helper::name);
615 mpNameTextBox = new QLineEdit(mpLibraryTreeItem ? mpLibraryTreeItem->getName() : "");
616 // domain
617 mpDomainLabel = new Label(tr("Domain:"));
618 mpDomainComboBox = new QComboBox;
619 mpDomainComboBox->addItem("Input", oms_tlm_domain_input);
620 mpDomainComboBox->addItem("Output", oms_tlm_domain_output);
621 mpDomainComboBox->addItem("Mechanical", oms_tlm_domain_mechanical);
622 mpDomainComboBox->addItem("Rotational", oms_tlm_domain_rotational);
623 mpDomainComboBox->addItem("Hydraulic", oms_tlm_domain_hydraulic);
624 mpDomainComboBox->addItem("Electric", oms_tlm_domain_electric);
625 if (mpLibraryTreeItem) {
626 int currentIndex = mpDomainComboBox->findData(mpLibraryTreeItem->getOMSTLMBusConnector()->domain);
627 if (currentIndex > -1) {
628 mpDomainComboBox->setCurrentIndex(currentIndex);
629 }
630 }
631 connect(mpDomainComboBox, SIGNAL(currentIndexChanged(int))"2""currentIndexChanged(int)", SLOT(fetchTLMTypes())"1""fetchTLMTypes()");
632 // dimension
633 mpDimensionLabel = new Label(tr("Dimension:"));
634 mpDimensionSpinBox = new QSpinBox;
635 mpDimensionSpinBox->setRange(1, 3);
636 if (mpLibraryTreeItem) {
637 mpDimensionSpinBox->setValue(mpLibraryTreeItem->getOMSTLMBusConnector()->dimensions);
638 }
639 connect(mpDimensionSpinBox, SIGNAL(valueChanged(int))"2""valueChanged(int)", SLOT(fetchTLMTypes())"1""fetchTLMTypes()");
640 // interpolation
641 mpInterpolationLabel = new Label(tr("Interpolation:"));
642 mpInterpolationComboBox = new QComboBox;
643 mpInterpolationComboBox->addItem("No interpolation", oms_tlm_no_interpolation);
644 mpInterpolationComboBox->addItem("Coarse grained", oms_tlm_coarse_grained);
645 mpInterpolationComboBox->addItem("Fine grained", oms_tlm_fine_grained);
646 if (mpLibraryTreeItem) {
647 int currentIndex = mpInterpolationComboBox->findData(mpLibraryTreeItem->getOMSTLMBusConnector()->interpolation);
648 if (currentIndex > -1) {
649 mpInterpolationComboBox->setCurrentIndex(currentIndex);
650 }
651 }
652 connect(mpInterpolationComboBox, SIGNAL(currentIndexChanged(int))"2""currentIndexChanged(int)", SLOT(fetchTLMTypes())"1""fetchTLMTypes()");
653 // input connectors
654 mpInputConnectorsTreeModel = new ConnectorsModel(this);
655 mpInputConnectorsTreeModel->setColumnCount(2);
656 mpInputConnectorsTreeView = new ConnectorsTreeView;
657 mpInputConnectorsTreeView->setModel(mpInputConnectorsTreeModel);
658 // inputs item
659 ConnectorItem *pInputsConnectorItem = mpInputConnectorsTreeModel->createConnectorItem(0, mpInputConnectorsTreeModel->getRootConnectorItem());
660 pInputsConnectorItem->setText("Input Connectors");
661 // output connectors
662 mpOutputConnectorsTreeModel = new ConnectorsModel(this);
663 mpOutputConnectorsTreeModel->setColumnCount(2);
664 mpOutputConnectorsTreeView = new ConnectorsTreeView;
665 mpOutputConnectorsTreeView->setModel(mpOutputConnectorsTreeModel);
666 // outputs item
667 ConnectorItem *pOutputsConnectorItem = mpInputConnectorsTreeModel->createConnectorItem(0, mpOutputConnectorsTreeModel->getRootConnectorItem());
668 pOutputsConnectorItem->setText("Output Connectors");
669 // add the connectors to input and output connectors tree views
670 foreach (Component* pComponent, mpGraphicsView->getComponentsList())for (auto _container_ = QtPrivate::qMakeForeachContainer(mpGraphicsView
->getComponentsList()); _container_.control && _container_
.i != _container_.e; ++_container_.i, _container_.control ^= 1
) for (Component* pComponent = *_container_.i; _container_.control
; _container_.control = 0)
{
671 if (pComponent->getLibraryTreeItem() && pComponent->getLibraryTreeItem()->getOMSConnector()
672 && (!pComponent->isInBus() || pComponent->getBusComponent()->getLibraryTreeItem() == mpLibraryTreeItem)) {
673 ConnectorItem *pConnectorItem = 0;
674 if (pComponent->getLibraryTreeItem()->getOMSConnector()->causality == oms_causality_input) {
675 pConnectorItem = mpInputConnectorsTreeModel->createConnectorItem(pComponent, pInputsConnectorItem);
Value stored to 'pConnectorItem' is never read
676 } else if (pComponent->getLibraryTreeItem()->getOMSConnector()->causality == oms_causality_output) {
677 pConnectorItem = mpOutputConnectorsTreeModel->createConnectorItem(pComponent, pOutputsConnectorItem);
678 } else {
679 qDebugQMessageLogger(nullptr, 0, nullptr).debug() << "AddTLMBusDialog::AddTLMBusDialog() unknown causality" << pComponent->getLibraryTreeItem()->getOMSConnector()->causality;
680 Q_UNUSED(pConnectorItem)(void)pConnectorItem;;
681 }
682 }
683 }
684 fetchTLMTypes();
685 // check which input connectors are already part of the tlm bus
686 markExistingTLMBusConnectors(pInputsConnectorItem, components);
687 // check which input connectors are already part of the tlm bus
688 markExistingTLMBusConnectors(pOutputsConnectorItem, components);
689 mpInputConnectorsTreeView->expandAll();
690 mpInputConnectorsTreeView->resizeColumnToContents(0);
691 mpOutputConnectorsTreeView->expandAll();
692 mpOutputConnectorsTreeView->resizeColumnToContents(0);
693 // buttons
694 mpOkButton = new QPushButton(Helper::ok);
695 mpOkButton->setAutoDefault(true);
696 connect(mpOkButton, SIGNAL(clicked())"2""clicked()", SLOT(addTLMBus())"1""addTLMBus()");
697 mpCancelButton = new QPushButton(Helper::cancel);
698 mpCancelButton->setAutoDefault(false);
699 connect(mpCancelButton, SIGNAL(clicked())"2""clicked()", SLOT(reject())"1""reject()");
700 // add buttons to the button box
701 mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
702 mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
703 mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
704 // set the layout
705 QGridLayout *pMainLayout = new QGridLayout;
706 pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
707 pMainLayout->addWidget(mpHeading, 0, 0, 1, 2);
708 pMainLayout->addWidget(mpHorizontalLine, 1, 0, 1, 2);
709 pMainLayout->addWidget(mpNameLabel, 2, 0);
710 pMainLayout->addWidget(mpNameTextBox, 2, 1);
711 pMainLayout->addWidget(mpDomainLabel, 3, 0);
712 pMainLayout->addWidget(mpDomainComboBox, 3, 1);
713 pMainLayout->addWidget(mpDimensionLabel, 4, 0);
714 pMainLayout->addWidget(mpDimensionSpinBox, 4, 1);
715 pMainLayout->addWidget(mpInterpolationLabel, 5, 0);
716 pMainLayout->addWidget(mpInterpolationComboBox, 5, 1);
717 pMainLayout->addWidget(mpInputConnectorsTreeView, 6, 0, 1, 2);
718 pMainLayout->addWidget(mpOutputConnectorsTreeView, 7, 0, 1, 2);
719 pMainLayout->addWidget(mpButtonBox, 8, 0, 1, 2, Qt::AlignRight);
720 setLayout(pMainLayout);
721}
722
723/*!
724 * \brief AddTLMBusDialog::markExistingTLMBusConnectors
725 * Mark the existing tlm bus connectors checked.
726 * \param pParentConnectorItem
727 * \param components
728 */
729void AddTLMBusDialog::markExistingTLMBusConnectors(ConnectorItem *pParentConnectorItem, QList<Component *> components)
730{
731 for (int i = 0 ; i < pParentConnectorItem->childrenSize() ; i++) {
732 ConnectorItem *pConnectorItem = pParentConnectorItem->childAt(i);
733 if (mpLibraryTreeItem && mpLibraryTreeItem->getOMSTLMBusConnector() && mpLibraryTreeItem->getOMSTLMBusConnector()->connectornames) {
734 for (int j = 0 ; mpLibraryTreeItem->getOMSTLMBusConnector()->connectornames[j] ; j++) {
735 if (pConnectorItem->getComponent()->getName().compare(QString(mpLibraryTreeItem->getOMSTLMBusConnector()->connectornames[j])) == 0) {
736 pConnectorItem->setChecked(true);
737 pConnectorItem->setTLMType(mpLibraryTreeItem->getOMSTLMBusConnector()->connectortypes[j]);
738 break;
739 }
740 }
741 } else if (pConnectorItem && components.contains(pConnectorItem->getComponent())) {
742 pConnectorItem->setChecked(true);
743 components.removeOne(pConnectorItem->getComponent());
744 }
745 }
746}
747
748/*!
749 * \brief AddTLMBusDialog::fetchTLMTypes
750 * Fetches the TLM Types based on the domain, dimension and interpolation.
751 */
752void AddTLMBusDialog::fetchTLMTypes()
753{
754 char **types = NULL__null;
755 char **descriptions = NULL__null;
756 if (OMSProxy::instance()->getTLMVariableTypes((oms_tlm_domain_t)mpDomainComboBox->itemData(mpDomainComboBox->currentIndex()).toInt(),
757 mpDimensionSpinBox->value(),
758 (oms_tlm_interpolation_t)mpInterpolationComboBox->itemData(mpInterpolationComboBox->currentIndex()).toInt(),
759 &types, &descriptions)) {
760 // convert types and descriptions list to QStringList
761 QStringList tlmTypes, tlmTypesDescriptions;
762 for (int i = 0 ; types[i] ; i++) {
763 tlmTypes.append(QString(types[i]));
764 tlmTypesDescriptions.append(QString(descriptions[i]));
765 }
766 // Insert TLM types in Input Connectors treeview
767 mpInputConnectorsTreeModel->setTLMTypes(tlmTypes);
768 mpInputConnectorsTreeModel->setTLMTypesDescriptions(tlmTypesDescriptions);
769 ConnectorItem *pInputConnectorsItem = mpInputConnectorsTreeModel->getRootConnectorItem()->childAt(0);
770 if (pInputConnectorsItem) {
771 for (int i = 0 ; i < pInputConnectorsItem->childrenSize() ; i++) {
772 ConnectorItem *pConnectorItem = pInputConnectorsItem->childAt(i);
773 if (pConnectorItem && !tlmTypes.isEmpty()) {
774 QModelIndex index = mpInputConnectorsTreeModel->connectorItemIndex(pConnectorItem, 1);
775 if (index.isValid()) {
776 pConnectorItem->setTLMTypeDescription(tlmTypesDescriptions.first());
777 mpInputConnectorsTreeModel->setData(index, tlmTypes.first());
778 }
779 }
780 }
781 }
782 // Insert TLM types in Output Connectors treeview
783 mpOutputConnectorsTreeModel->setTLMTypes(tlmTypes);
784 mpOutputConnectorsTreeModel->setTLMTypesDescriptions(tlmTypesDescriptions);
785 ConnectorItem *pOutputConnectorsItem = mpOutputConnectorsTreeModel->getRootConnectorItem()->childAt(0);
786 if (pOutputConnectorsItem) {
787 for (int i = 0 ; i < pOutputConnectorsItem->childrenSize() ; i++) {
788 ConnectorItem *pConnectorItem = pOutputConnectorsItem->childAt(i);
789 if (pConnectorItem && !tlmTypes.isEmpty()) {
790 QModelIndex index = mpOutputConnectorsTreeModel->connectorItemIndex(pConnectorItem, 1);
791 if (index.isValid()) {
792 pConnectorItem->setTLMTypeDescription(tlmTypesDescriptions.first());
793 mpOutputConnectorsTreeModel->setData(index, tlmTypes.first());
794 }
795 }
796 }
797 }
798 }
799}
800
801/*!
802 * \brief AddTLMBusDialog::addTLMBus
803 * Adds the tlm bus by calling AddTLMBusCommand
804 */
805void AddTLMBusDialog::addTLMBus()
806{
807 if (mpNameTextBox->text().isEmpty()) {
808 QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
809 GUIMessages::getMessage(GUIMessages::ENTER_NAME).arg(tr("TLM Bus")), Helper::ok);
810 return;
811 }
812
813 QSet<QPair<QString, QString> > connectors;
814 ConnectorItem *pInputConnectorsItem = mpInputConnectorsTreeModel->getRootConnectorItem()->childAt(0);
815 if (pInputConnectorsItem) {
816 for (int i = 0 ; i < pInputConnectorsItem->childrenSize() ; i++) {
817 ConnectorItem *pConnectorItem = pInputConnectorsItem->childAt(i);
818 if (pConnectorItem && pConnectorItem->isChecked()) {
819 connectors.insert(qMakePair(pConnectorItem->getComponent()->getLibraryTreeItem()->getNameStructure(), pConnectorItem->getTLMType()));
820 }
821 }
822 }
823
824 ConnectorItem *pOutputConnectorsItem = mpOutputConnectorsTreeModel->getRootConnectorItem()->childAt(0);
825 if (pOutputConnectorsItem) {
826 for (int i = 0 ; i < pOutputConnectorsItem->childrenSize() ; i++) {
827 ConnectorItem *pConnectorItem = pOutputConnectorsItem->childAt(i);
828 if (pConnectorItem && pConnectorItem->isChecked()) {
829 connectors.insert(qMakePair(pConnectorItem->getComponent()->getLibraryTreeItem()->getNameStructure(), pConnectorItem->getTLMType()));
830 }
831 }
832 }
833
834 LibraryTreeItem *pParentLibraryTreeItem = mpGraphicsView->getModelWidget()->getLibraryTreeItem();
835 QString tlmBus = QString("%1.%2").arg(pParentLibraryTreeItem->getNameStructure()).arg(mpNameTextBox->text());
836
837 if (mpLibraryTreeItem) { // edit case
838 mpGraphicsView->getModelWidget()->beginMacro("Edit TLM bus");
839
840 /*! @todo Rename the tlm bus here.
841 */
842
843 QSet<QPair<QString, QString> > existingConnectors;
844 if (mpLibraryTreeItem->getOMSTLMBusConnector() && mpLibraryTreeItem->getOMSTLMBusConnector()->connectornames) {
845 for (int i = 0 ; mpLibraryTreeItem->getOMSTLMBusConnector()->connectornames[i] ; i++) {
846 existingConnectors.insert(qMakePair(QString("%1.%2").arg(mpGraphicsView->getModelWidget()->getLibraryTreeItem()->getNameStructure())
847 .arg(QString(mpLibraryTreeItem->getOMSTLMBusConnector()->connectornames[i])),
848 QString(mpLibraryTreeItem->getOMSTLMBusConnector()->connectortypes[i])));
849 }
850 }
851 // delete connectors from the bus
852 QSet<QPair<QString, QString> > deleteConnectors = existingConnectors;
853 deleteConnectors.subtract(connectors);
854 QPair<QString, QString> connector;
855 foreach (connector, deleteConnectors)for (auto _container_ = QtPrivate::qMakeForeachContainer(deleteConnectors
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (connector
= *_container_.i; _container_.control; _container_.control =
0)
{
856 DeleteConnectorFromTLMBusCommand *pDeleteConnectorFromTLMBusCommand;
857 pDeleteConnectorFromTLMBusCommand = new DeleteConnectorFromTLMBusCommand(tlmBus, connector.first, connector.second, mpGraphicsView);
858 mpGraphicsView->getModelWidget()->getUndoStack()->push(pDeleteConnectorFromTLMBusCommand);
859 }
860 // add connectors to the bus
861 QSet<QPair<QString, QString> > addConnectors = connectors;
862 addConnectors.subtract(existingConnectors);
863 foreach (connector, addConnectors)for (auto _container_ = QtPrivate::qMakeForeachContainer(addConnectors
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (connector
= *_container_.i; _container_.control; _container_.control =
0)
{
864 AddConnectorToTLMBusCommand *pAddConnectorToBusCommand = new AddConnectorToTLMBusCommand(tlmBus, connector.first,
865 connector.second, mpGraphicsView);
866 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddConnectorToBusCommand);
867 }
868
869 mpGraphicsView->getModelWidget()->updateModelText();
870 mpGraphicsView->getModelWidget()->getLibraryTreeItem()->handleIconUpdated();
871 accept();
872 mpGraphicsView->getModelWidget()->endMacro();
873 } else { // add case
874 mpGraphicsView->getModelWidget()->beginMacro("Add TLM bus");
875 QString annotation = QString("Placement(true,%1,%2,-10.0,-10.0,10.0,10.0,0,%1,%2,-10.0,-10.0,10.0,10.0,)")
876 .arg(Utilities::mapToCoOrdinateSystem(0.5, 0, 1, -100, 100))
877 .arg(Utilities::mapToCoOrdinateSystem(0.5, 0, 1, -100, 100));
878 AddTLMBusCommand *pAddTLMBusCommand = new AddTLMBusCommand(mpNameTextBox->text(), 0, annotation, mpGraphicsView, false,
879 (oms_tlm_domain_t)mpDomainComboBox->itemData(mpDomainComboBox->currentIndex()).toInt(),
880 mpDimensionSpinBox->value(),
881 (oms_tlm_interpolation_t)mpInterpolationComboBox->itemData(mpInterpolationComboBox->currentIndex()).toInt());
882 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddTLMBusCommand);
883 if (!pAddTLMBusCommand->isFailed()) {
884 // add connectors to the bus
885 QPair<QString, QString> connector;
886 foreach (connector, connectors)for (auto _container_ = QtPrivate::qMakeForeachContainer(connectors
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (connector
= *_container_.i; _container_.control; _container_.control =
0)
{
887 AddConnectorToTLMBusCommand *pAddConnectorToTLMBusCommand = new AddConnectorToTLMBusCommand(tlmBus, connector.first,
888 connector.second, mpGraphicsView);
889 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddConnectorToTLMBusCommand);
890 }
891 mpGraphicsView->getModelWidget()->getLibraryTreeItem()->emitComponentAddedForComponent();
892 mpGraphicsView->getModelWidget()->updateModelText();
893 mpGraphicsView->getModelWidget()->getLibraryTreeItem()->handleIconUpdated();
894 accept();
895 }
896 mpGraphicsView->getModelWidget()->endMacro();
897 }
898}
899
900/*!
901 * \class ConnectionItem
902 * \brief Contains the information about the connection.
903 */
904/*!
905 * \brief ConnectionItem::ConnectionItem
906 * \param start
907 * \param end
908 * \param checked
909 * \param pParent
910 */
911ConnectionItem::ConnectionItem(QString start, QString end, bool checked, ConnectionItem *pParent)
912{
913 mStart = start;
914 mInitialStart = start;
915 mEnd = end;
916 mInitialEnd = end;
917 mpParentConnectionItem = pParent;
918 mChecked = checked;
919 mExisting = false;
920}
921
922/*!
923 * \brief ConnectionItem::row
924 * Returns the row number corresponding to ConnectorItem.
925 * \return
926 */
927int ConnectionItem::row() const
928{
929 if (mpParentConnectionItem) {
930 return mpParentConnectionItem->mChildren.indexOf(const_cast<ConnectionItem*>(this));
931 }
932
933 return 0;
934}
935
936/*!
937 * \class ConnectionsModel
938 * \brief A model for connections.
939 */
940/*!
941 * \brief ConnectionsModel::ConnectionsModel
942 * \param pConnectionLineAnnotation
943 * \param parent
944 */
945ConnectionsModel::ConnectionsModel(LineAnnotation *pConnectionLineAnnotation, QObject *parent)
946 : QAbstractItemModel(parent)
947{
948 mpConnectionLineAnnotation = pConnectionLineAnnotation;
949 mpRootConnectionItem = new ConnectionItem("", "", false, 0);
950}
951
952/*!
953 * \brief ConnectionsModel::columnCount
954 * Returns the number of columns for the children of the given parent.
955 * \param parent
956 * \return
957 */
958int ConnectionsModel::columnCount(const QModelIndex &parent) const
959{
960 Q_UNUSED(parent)(void)parent;;
961 return 3;
962}
963
964/*!
965 * \brief ConnectionsModel::rowCount
966 * Returns the number of rows under the given parent.
967 * When the parent is valid it means that rowCount is returning the number of children of parent.
968 * \param parent
969 * \return
970 */
971int ConnectionsModel::rowCount(const QModelIndex &parent) const
972{
973 ConnectionItem *pParentConnectionItem;
974 if (parent.column() > 0) {
975 return 0;
976 }
977
978 if (!parent.isValid()) {
979 pParentConnectionItem = mpRootConnectionItem;
980 } else {
981 pParentConnectionItem = static_cast<ConnectionItem*>(parent.internalPointer());
982 }
983 return pParentConnectionItem->childrenSize();
984}
985
986QVariant ConnectionsModel::headerData(int section, Qt::Orientation orientation, int role) const
987{
988 Q_UNUSED(orientation)(void)orientation;;
989 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
990 switch (section) {
991 case 0:
992 return mHeaderLabels.size() > 0 ? mHeaderLabels.at(0) : "";
993 case 1:
994 return mHeaderLabels.size() > 1 ? mHeaderLabels.at(1) : "";
995 case 2:
996 return "ssd:Connection";
997 default:
998 break;
999 }
1000 }
1001 return QAbstractItemModel::headerData(section, orientation, role);
1002}
1003
1004/*!
1005 * \brief ConnectionsModel::index
1006 * Returns the index of the item in the model specified by the given row, column and parent index.
1007 * \param row
1008 * \param column
1009 * \param parent
1010 * \return
1011 */
1012QModelIndex ConnectionsModel::index(int row, int column, const QModelIndex &parent) const
1013{
1014 if (!hasIndex(row, column, parent)) {
1015 return QModelIndex();
1016 }
1017
1018 ConnectionItem *pParentConnectionItem;
1019 if (!parent.isValid()) {
1020 pParentConnectionItem = mpRootConnectionItem;
1021 } else {
1022 pParentConnectionItem = static_cast<ConnectionItem*>(parent.internalPointer());
1023 }
1024
1025 ConnectionItem *pChildConnectionItem = pParentConnectionItem->child(row);
1026 if (pChildConnectionItem) {
1027 return createIndex(row, column, pChildConnectionItem);
1028 } else {
1029 return QModelIndex();
1030 }
1031}
1032
1033/*!
1034 * \brief ConnectionsModel::parent
1035 * Finds the parent for QModelIndex
1036 * \param index
1037 * \return
1038 */
1039QModelIndex ConnectionsModel::parent(const QModelIndex &index) const
1040{
1041 if (!index.isValid()) {
1042 return QModelIndex();
1043 }
1044
1045 ConnectionItem *pChildConnectionItem = static_cast<ConnectionItem*>(index.internalPointer());
1046 ConnectionItem *pParentConnectionItem = pChildConnectionItem->parent();
1047 if (pParentConnectionItem == mpRootConnectionItem)
1048 return QModelIndex();
1049
1050 return createIndex(pParentConnectionItem->row(), 0, pParentConnectionItem);
1051}
1052
1053/*!
1054 * \brief ConnectionsModel::setData
1055 * Updates the model data.
1056 * \param index
1057 * \param value
1058 * \param role
1059 * \return
1060 */
1061bool ConnectionsModel::setData(const QModelIndex &index, const QVariant &value, int role)
1062{
1063 ConnectionItem *pConnectionItem = static_cast<ConnectionItem*>(index.internalPointer());
1064 if (!pConnectionItem) {
1065 return false;
1066 }
1067
1068 if (index.column() == 0 && role == Qt::CheckStateRole) {
1069 if (value.toInt() == Qt::Checked) {
1070 pConnectionItem->setChecked(true);
1071 } else if (value.toInt() == Qt::Unchecked) {
1072 pConnectionItem->setChecked(false);
1073 }
1074 emit dataChanged(index, index);
1075 return true;
1076 }
1077
1078 return QAbstractItemModel::setData(index, value, role);
1079}
1080
1081/*!
1082 * \brief ConnectionsModel::data
1083 * Returns the ConnectionItem data.
1084 * \param index
1085 * \param role
1086 * \return
1087 */
1088QVariant ConnectionsModel::data(const QModelIndex &index, int role) const
1089{
1090 if (!index.isValid()) {
1091 return QVariant();
1092 }
1093
1094 ConnectionItem *pConnectionItem = static_cast<ConnectionItem*>(index.internalPointer());
1095
1096 switch (index.column()) {
1097 case 0:
1098 switch (role) {
1099 case Qt::CheckStateRole:
1100 if (pConnectionItem->getStart().isEmpty() || pConnectionItem->getEnd().isEmpty()) {
1101 return Qt::Unchecked;
1102 }
1103 return pConnectionItem->isChecked() ? Qt::Checked : Qt::Unchecked;;
1104 case Qt::DisplayRole:
1105 case Qt::ToolTipRole:
1106 return pConnectionItem->getStart();
1107 case Qt::BackgroundRole:
1108 return pConnectionItem->isExisting() ? QColor(Qt::yellow) : QVariant();
1109 default:
1110 return QVariant();
1111 }
1112 case 1:
1113 switch (role) {
1114 case Qt::DisplayRole:
1115 case Qt::ToolTipRole:
1116 return pConnectionItem->getEnd();
1117 case Qt::BackgroundRole:
1118 return pConnectionItem->isExisting() ? QColor(Qt::yellow) : QVariant();
1119 default:
1120 return QVariant();
1121 }
1122 case 2:
1123 switch (role) {
1124 case Qt::DisplayRole:
1125 case Qt::ToolTipRole:
1126 {
1127 LibraryTreeItem *pStartElement = mpConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem()->parent();
1128 LibraryTreeItem *pEndElement = mpConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem()->parent();
1129 if (pStartElement && pEndElement && !pConnectionItem->getStart().isEmpty() && !pConnectionItem->getEnd().isEmpty()) {
1130 return QString("<ssd:Connection startElement=\"%1\" startConnector=\"%2\" endElement=\"%3\" endConnector=\"%4\" />")
1131 .arg(pStartElement->getName(), pConnectionItem->getStart(), pEndElement->getName(), pConnectionItem->getEnd());
1132 } else {
1133 return QVariant();
1134 }
1135 }
1136 case Qt::BackgroundRole:
1137 return pConnectionItem->isExisting() ? QColor(Qt::yellow) : QVariant();
1138 default:
1139 return QVariant();
1140 }
1141 default:
1142 return QVariant();
1143 }
1144}
1145
1146/*!
1147 * \brief ConnectionsModel::mimeTypes
1148 * \return
1149 */
1150QStringList ConnectionsModel::mimeTypes() const
1151{
1152 QStringList types;
1153 types << Helper::busConnectorFormat;
1154 return types;
1155}
1156
1157/*!
1158 * \brief ConnectionsModel::mimeData
1159 * \param indexes
1160 * \return
1161 */
1162QMimeData* ConnectionsModel::mimeData(const QModelIndexList &indexes) const
1163{
1164 QMimeData *mimeData = new QMimeData();
1165 QByteArray encodedData;
1166 QDataStream stream(&encodedData, QIODevice::WriteOnly);
1167
1168 if (!indexes.isEmpty()) {
1169 const QModelIndex &index = indexes.at(0); // single selection model
1170 if (index.isValid()) {
1171 stream << index.row();
1172 stream << index.column();
1173 stream << data(index, Qt::DisplayRole).toString();
1174 }
1175 }
1176
1177 mimeData->setData(Helper::busConnectorFormat, encodedData);
1178 return mimeData;
1179}
1180
1181#if (QT_VERSION((5<<16)|(9<<8)|(5)) >= QT_VERSION_CHECK(5, 6, 0)((5<<16)|(6<<8)|(0)))
1182bool ConnectionsModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
1183{
1184 Q_UNUSED(action)(void)action;;
1185 Q_UNUSED(column)(void)column;;
1186 Q_UNUSED(row)(void)row;;
1187 Q_UNUSED(parent)(void)parent;;
1188
1189 if (!data->hasFormat(Helper::busConnectorFormat)) {
1190 return false;
1191 }
1192
1193 int sourceRow, sourceColumn;
1194 QByteArray encodedData = data->data(Helper::busConnectorFormat);
1195 QDataStream stream(&encodedData, QIODevice::ReadOnly);
1196
1197 stream >> sourceRow;
1198 stream >> sourceColumn;
1199
1200 // move is only allowed in the same column
1201 if (sourceColumn != parent.column()) {
1202 return false;
1203 }
1204
1205 return true;
1206}
1207#endif
1208
1209bool ConnectionsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
1210{
1211 Q_UNUSED(action)(void)action;;
1212 Q_UNUSED(row)(void)row;;
1213 Q_UNUSED(column)(void)column;;
1214 int sourceRow, sourceColumn;
1215 QByteArray encodedData = data->data(Helper::busConnectorFormat);
1216 QDataStream stream(&encodedData, QIODevice::ReadOnly);
1217
1218 stream >> sourceRow;
1219 stream >> sourceColumn;
1220
1221 QModelIndex sourceIndex = index(sourceRow, sourceColumn);
1222 ConnectionItem *pSourceConnectionItem = static_cast<ConnectionItem*>(sourceIndex.internalPointer());
1223
1224 ConnectionItem *pDestinationConnectionItem = static_cast<ConnectionItem*>(parent.internalPointer());
1225
1226 QString sourceValue = "";
1227 QString destinationValue = "";
1228 if (sourceColumn == 0) {
1229 sourceValue = pSourceConnectionItem->getStart();
1230 destinationValue = pDestinationConnectionItem->getStart();
1231 pSourceConnectionItem->setStart(destinationValue);
1232 pDestinationConnectionItem->setStart(sourceValue);
1233 } else if (sourceColumn == 1) {
1234 sourceValue = pSourceConnectionItem->getEnd();
1235 destinationValue = pDestinationConnectionItem->getEnd();
1236 pSourceConnectionItem->setEnd(destinationValue);
1237 pDestinationConnectionItem->setEnd(sourceValue);
1238 }
1239
1240 pSourceConnectionItem->setChecked(!pSourceConnectionItem->getStart().isEmpty() && !pSourceConnectionItem->getEnd().isEmpty());
1241 pDestinationConnectionItem->setChecked(!pDestinationConnectionItem->getStart().isEmpty() && !pDestinationConnectionItem->getEnd().isEmpty());
1242
1243 return true;
1244}
1245
1246/*!
1247 * \brief ConnectionsModel::supportedDropActions
1248 * \return
1249 */
1250Qt::DropActions ConnectionsModel::supportedDropActions() const
1251{
1252 return Qt::MoveAction;
1253}
1254
1255/*!
1256 * \brief ConnectionsModel::flags
1257 * Returns the ConnectionItem flags.
1258 * \param index
1259 * \return
1260 */
1261Qt::ItemFlags ConnectionsModel::flags(const QModelIndex &index) const
1262{
1263 if (!index.isValid()) {
1264 return Qt::ItemIsEnabled;
1265 } else {
1266 switch (index.column()) {
1267 case 0:
1268 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
1269 case 1:
1270 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
1271 case 2:
1272 default:
1273 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
1274
1275 }
1276 }
1277}
1278
1279/*!
1280 * \brief ConnectionsModel::connectionItemIndex
1281 * Finds the QModelIndex attached to ConnectionItem.
1282 * \param pConnectionItem
1283 * \return
1284 */
1285QModelIndex ConnectionsModel::connectionItemIndex(const ConnectionItem *pConnectionItem) const
1286{
1287 return connectionItemIndexHelper(pConnectionItem, mpRootConnectionItem, QModelIndex());
1288}
1289
1290/*!
1291 * \brief ConnectionsModel::createConnectionItem
1292 * Creates the ConnectionItem and returns it.
1293 * \param start
1294 * \param end
1295 * \param checked
1296 * \param pParent
1297 * \return
1298 */
1299ConnectionItem* ConnectionsModel::createConnectionItem(QString start, QString end, bool checked, ConnectionItem *pParent)
1300{
1301 int row = pParent->childrenSize();
1302 beginInsertRows(connectionItemIndex(pParent), row, row);
1303 ConnectionItem *pConnectionItem = new ConnectionItem(start, end, checked, pParent);
1304 pParent->insertChild(row, pConnectionItem);
1305 endInsertRows();
1306 return pConnectionItem;
1307}
1308
1309/*!
1310 * \brief ConnectionsModel::connectionItemIndexHelper
1311 * Helper function for ConnectionsModel::connectionItemIndex()
1312 * \param pConnectionItem
1313 * \param pParentConnectionItem
1314 * \param parentIndex
1315 * \return
1316 */
1317QModelIndex ConnectionsModel::connectionItemIndexHelper(const ConnectionItem *pConnectionItem,
1318 const ConnectionItem *pParentConnectionItem, const QModelIndex &parentIndex) const
1319{
1320 if (pConnectionItem == pParentConnectionItem) {
1321 return parentIndex;
1322 }
1323 for (int i = pParentConnectionItem->childrenSize(); --i >= 0; ) {
1324 const ConnectionItem *childItem = pParentConnectionItem->childAt(i);
1325 QModelIndex childIndex = index(i, 0, parentIndex);
1326 QModelIndex index = connectionItemIndexHelper(pConnectionItem, childItem, childIndex);
1327 if (index.isValid()) {
1328 return index;
1329 }
1330 }
1331 return QModelIndex();
1332}
1333
1334/*!
1335 * \class BusConnectionDialog
1336 * \brief A dialog for creating a bus connection.
1337 */
1338/*!
1339 * \brief BusConnectionDialog::BusConnectionDialog
1340 * \param pGraphicsView
1341 * \param pConnectionLineAnnotation
1342 */
1343BusConnectionDialog::BusConnectionDialog(GraphicsView *pGraphicsView, LineAnnotation *pConnectionLineAnnotation, bool addCase)
1344 : QDialog(pGraphicsView)
1345{
1346 setAttribute(Qt::WA_DeleteOnClose);
1347 setWindowTitle(QString("%1 - %2").arg(Helper::applicationName).arg(addCase ? Helper::addBusConnection : Helper::editBusConnection));
1348 resize(800, 600);
1349 mpGraphicsView = pGraphicsView;
1350 mpConnectionLineAnnotation = pConnectionLineAnnotation;
1351 mAddCase = addCase;
1352 // set heading
1353 mpHeading = Utilities::getHeadingLabel(addCase ? Helper::addBusConnection : Helper::editBusConnection);
1354 // set separator line
1355 mpHorizontalLine = Utilities::getHeadingLine();
1356 // input output label
1357 LibraryTreeItem *pStartLibraryTreeItem = mpConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem();
1358 LibraryTreeItem *pEndLibraryTreeItem = mpConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem();
1359 Label *pInputOutputLabel = new Label(QString("Connect <b>%1</b> input connectors to <b>%2</b> output connectors")
1360 .arg(pStartLibraryTreeItem->getName())
1361 .arg(pEndLibraryTreeItem->getName()));
1362 // input output connections
1363 mpInputOutputConnectionsModel = new ConnectionsModel(mpConnectionLineAnnotation, this);
1364 QStringList headerLabels;
1365 headerLabels << QString("%1 inputs").arg(pStartLibraryTreeItem->getName())
1366 << QString("%1 outputs").arg(pEndLibraryTreeItem->getName());
1367 mpInputOutputConnectionsModel->setHeaderLabels(headerLabels);
1368 mpInputOutputConnectionsTableView = new QTableView;
1369 mpInputOutputConnectionsTableView->setModel(mpInputOutputConnectionsModel);
1370 mpInputOutputConnectionsTableView->setTextElideMode(Qt::ElideMiddle);
1371 mpInputOutputConnectionsTableView->setDragDropMode(QAbstractItemView::InternalMove);
1372 mpInputOutputConnectionsTableView->setDropIndicatorShown(true);
1373 mpInputOutputConnectionsTableView->setSelectionBehavior(QAbstractItemView::SelectItems);
1374 mpInputOutputConnectionsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
1375 // output input label
1376 Label *pOutputInputLabel = new Label(QString("Connect <b>%1</b> output connectors to <b>%2</b> input connectors")
1377 .arg(pStartLibraryTreeItem->getName())
1378 .arg(pEndLibraryTreeItem->getName()));
1379 // output input connections
1380 mpOutputInputConnectionsModel = new ConnectionsModel(mpConnectionLineAnnotation, this);
1381 headerLabels.clear();
1382 headerLabels << QString("%1 outputs").arg(pStartLibraryTreeItem->getName())
1383 << QString("%1 inputs").arg(pEndLibraryTreeItem->getName());
1384 mpOutputInputConnectionsModel->setHeaderLabels(headerLabels);
1385 mpOutputInputConnectionsTableView = new QTableView;
1386 mpOutputInputConnectionsTableView->setModel(mpOutputInputConnectionsModel);
1387 mpOutputInputConnectionsTableView->setTextElideMode(Qt::ElideMiddle);
1388 mpOutputInputConnectionsTableView->setDragDropMode(QAbstractItemView::InternalMove);
1389 mpOutputInputConnectionsTableView->setDropIndicatorShown(true);
1390 mpOutputInputConnectionsTableView->setSelectionBehavior(QAbstractItemView::SelectItems);
1391 mpOutputInputConnectionsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
1392 // start bus input output connectors
1393 oms_busconnector_t *pStartBus = pStartLibraryTreeItem->getOMSBusConnector();
1394 QStringList startBusInputConnectors, startBusOutputConnectors;
1395 if (pStartBus && pStartBus->connectors) {
1396 LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
1397 LibraryTreeItem *pParentLibraryTreeItem = pStartLibraryTreeItem->parent();
1398 for (int i = 0; pStartBus->connectors[i] ; ++i) {
1399 LibraryTreeItem *pLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(QString("%1.%2")
1400 .arg(pParentLibraryTreeItem->getNameStructure())
1401 .arg(pStartBus->connectors[i]), pParentLibraryTreeItem);
1402 if (pLibraryTreeItem && pLibraryTreeItem->getOMSConnector()) {
1403 if (pLibraryTreeItem->getOMSConnector()->causality == oms_causality_input) {
1404 startBusInputConnectors.append(QString(pStartBus->connectors[i]));
1405 } else if (pLibraryTreeItem->getOMSConnector()->causality == oms_causality_output) {
1406 startBusOutputConnectors.append(QString(pStartBus->connectors[i]));
1407 }
1408 }
1409 }
1410 } else if (pStartLibraryTreeItem->getOMSConnector()) {
1411 if (pStartLibraryTreeItem->getOMSConnector()->causality == oms_causality_input) {
1412 startBusInputConnectors.append(pStartLibraryTreeItem->getName());
1413 } else if (pStartLibraryTreeItem->getOMSConnector()->causality == oms_causality_output) {
1414 startBusOutputConnectors.append(pStartLibraryTreeItem->getName());
1415 }
1416 }
1417 startBusInputConnectors.sort();
1418 startBusOutputConnectors.sort();
1419 // end bus input output connectors
1420 oms_busconnector_t *pEndBus = pEndLibraryTreeItem->getOMSBusConnector();
1421 QStringList endBusInputConnectors, endBusOutputConnectors;
1422 if (pEndBus && pEndBus->connectors) {
1423 LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
1424 LibraryTreeItem *pParentLibraryTreeItem = pEndLibraryTreeItem->parent();
1425 for (int i = 0; pEndBus->connectors[i] ; ++i) {
1426 LibraryTreeItem *pLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(QString("%1.%2")
1427 .arg(pParentLibraryTreeItem->getNameStructure())
1428 .arg(pEndBus->connectors[i]), pParentLibraryTreeItem);
1429 if (pLibraryTreeItem && pLibraryTreeItem->getOMSConnector()) {
1430 if (pLibraryTreeItem->getOMSConnector()->causality == oms_causality_input) {
1431 endBusInputConnectors.append(QString(pEndBus->connectors[i]));
1432 } else if (pLibraryTreeItem->getOMSConnector()->causality == oms_causality_output) {
1433 endBusOutputConnectors.append(QString(pEndBus->connectors[i]));
1434 }
1435 }
1436 }
1437 } else if (pEndLibraryTreeItem->getOMSConnector()) {
1438 if (pEndLibraryTreeItem->getOMSConnector()->causality == oms_causality_input) {
1439 startBusInputConnectors.append(pEndLibraryTreeItem->getName());
1440 } else if (pEndLibraryTreeItem->getOMSConnector()->causality == oms_causality_output) {
1441 startBusOutputConnectors.append(pEndLibraryTreeItem->getName());
1442 }
1443 }
1444 endBusInputConnectors.sort();
1445 endBusOutputConnectors.sort();
1446
1447 if (!addCase) {
1448 for (int i = 0 ; i < mpGraphicsView->getConnectionsList().size() ; ++i) {
1449 LineAnnotation *pAtomicConnectionLineAnnotation = mpGraphicsView->getConnectionsList().at(i);
1450 if (pAtomicConnectionLineAnnotation && pAtomicConnectionLineAnnotation->getOMSConnectionType() == oms_connection_single) {
1451 QString startConnectorName = StringHandler::getLastWordAfterDot(pAtomicConnectionLineAnnotation->getStartComponentName());
1452 QString endConnectorName = StringHandler::getLastWordAfterDot(pAtomicConnectionLineAnnotation->getEndComponentName());
1453 if (startBusInputConnectors.contains(startConnectorName) && endBusOutputConnectors.contains(endConnectorName)) {
1454 ConnectionItem *pConnectionItem;
1455 pConnectionItem = mpInputOutputConnectionsModel->createConnectionItem(startConnectorName, endConnectorName, true,
1456 mpInputOutputConnectionsModel->getRootConnectionItem());
1457 pConnectionItem->setExisting(true);
1458 startBusInputConnectors.removeOne(startConnectorName);
1459 endBusOutputConnectors.removeOne(endConnectorName);
1460 } else if (startBusInputConnectors.contains(endConnectorName) && endBusOutputConnectors.contains(startConnectorName)) {
1461 ConnectionItem *pConnectionItem;
1462 pConnectionItem = mpInputOutputConnectionsModel->createConnectionItem(endConnectorName, startConnectorName, true,
1463 mpInputOutputConnectionsModel->getRootConnectionItem());
1464 pConnectionItem->setExisting(true);
1465 startBusInputConnectors.removeOne(endConnectorName);
1466 endBusOutputConnectors.removeOne(startConnectorName);
1467 } else if (startBusOutputConnectors.contains(startConnectorName) && endBusInputConnectors.contains(endConnectorName)) {
1468 ConnectionItem *pConnectionItem;
1469 pConnectionItem = mpOutputInputConnectionsModel->createConnectionItem(startConnectorName, endConnectorName, true,
1470 mpOutputInputConnectionsModel->getRootConnectionItem());
1471 pConnectionItem->setExisting(true);
1472 startBusOutputConnectors.removeOne(startConnectorName);
1473 endBusInputConnectors.removeOne(endConnectorName);
1474 } else if (startBusOutputConnectors.contains(endConnectorName) && endBusInputConnectors.contains(startConnectorName)) {
1475 ConnectionItem *pConnectionItem;
1476 pConnectionItem = mpOutputInputConnectionsModel->createConnectionItem(endConnectorName, startConnectorName, true,
1477 mpOutputInputConnectionsModel->getRootConnectionItem());
1478 pConnectionItem->setExisting(true);
1479 startBusOutputConnectors.removeOne(endConnectorName);
1480 endBusInputConnectors.removeOne(startConnectorName);
1481 }
1482 }
1483 }
1484 }
1485
1486 int size = qMax(startBusInputConnectors.size(), endBusOutputConnectors.size());
1487
1488 for (int i = 0; i < size ; ++i) {
1489 QString start = i < startBusInputConnectors.size() ? startBusInputConnectors.at(i) : "";
1490 QString end = i < endBusOutputConnectors.size() ? endBusOutputConnectors.at(i) : "";
1491 mpInputOutputConnectionsModel->createConnectionItem(start, end, addCase, mpInputOutputConnectionsModel->getRootConnectionItem());
1492 }
1493 mpInputOutputConnectionsTableView->resizeColumnToContents(2);
1494
1495 size = qMax(startBusOutputConnectors.size(), endBusInputConnectors.size());
1496
1497 for (int i = 0; i < size ; ++i) {
1498 QString start = i < startBusOutputConnectors.size() ? startBusOutputConnectors.at(i) : "";
1499 QString end = i < endBusInputConnectors.size() ? endBusInputConnectors.at(i) : "";
1500 mpOutputInputConnectionsModel->createConnectionItem(start, end, addCase, mpOutputInputConnectionsModel->getRootConnectionItem());
1501 }
1502 mpOutputInputConnectionsTableView->resizeColumnToContents(2);
1503
1504 // buttons
1505 mpOkButton = new QPushButton(Helper::ok);
1506 mpOkButton->setAutoDefault(true);
1507 connect(mpOkButton, SIGNAL(clicked())"2""clicked()", SLOT(addBusConnection())"1""addBusConnection()");
1508 mpCancelButton = new QPushButton(Helper::cancel);
1509 mpCancelButton->setAutoDefault(false);
1510 connect(mpCancelButton, SIGNAL(clicked())"2""clicked()", SLOT(reject())"1""reject()");
1511 // add buttons to the button box
1512 mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
1513 mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
1514 mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
1515 // set the layout
1516 QGridLayout *pMainLayout = new QGridLayout;
1517 pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
1518 pMainLayout->addWidget(mpHeading, 0, 0);
1519 pMainLayout->addWidget(mpHorizontalLine, 1, 0);
1520 pMainLayout->addWidget(pInputOutputLabel, 2, 0);
1521 pMainLayout->addWidget(mpInputOutputConnectionsTableView, 3, 0);
1522 pMainLayout->addWidget(pOutputInputLabel, 4, 0);
1523 pMainLayout->addWidget(mpOutputInputConnectionsTableView, 5, 0);
1524 int i = 6;
1525 if (!addCase) {
1526 pMainLayout->addWidget(new Label(tr("Note: Yellow marked rows are existing connections.")), i++, 0);
1527 }
1528 pMainLayout->addWidget(mpButtonBox, i, 0, Qt::AlignRight);
1529 setLayout(pMainLayout);
1530}
1531
1532/*!
1533 * \brief BusConnectionDialog::addOrDeleteAtomicConnections
1534 * Adds or Deletes the atomic connections based on the connections set on each ConnectionsModel.
1535 * \param pConnectionsModel
1536 */
1537void BusConnectionDialog::addOrDeleteAtomicConnections(ConnectionsModel *pConnectionsModel)
1538{
1539 for (int i = 0; i < pConnectionsModel->getRootConnectionItem()->childrenSize() ; ++i) {
1540 ConnectionItem *pConnectionItem = pConnectionsModel->getRootConnectionItem()->childAt(i);
1541 if (pConnectionItem->isExisting()) { // delete atomic connection
1542 /* 1. Unchecked and start and end are unchanged.
1543 * 2. Unchecked and start or end are empty.
1544 * 3. Checked/Unchecked and start or end are changed.
1545 */
1546 if ((!pConnectionItem->isChecked() && (pConnectionItem->getStart().compare(pConnectionItem->getInitialStart()) == 0)
1547 && (pConnectionItem->getEnd().compare(pConnectionItem->getInitialEnd()) == 0))
1548 || (!pConnectionItem->isChecked() && (pConnectionItem->getStart().isEmpty() || pConnectionItem->getEnd().isEmpty()))
1549 || ((pConnectionItem->getStart().compare(pConnectionItem->getInitialStart()) != 0)
1550 || (pConnectionItem->getEnd().compare(pConnectionItem->getInitialEnd()) != 0))) {
1551 deleteAtomicConnection(pConnectionItem->getInitialStart(), pConnectionItem->getInitialEnd());
1552 /* When we have case 3. Checked/Unchecked and start or end are changed.
1553 * Then we also need to add a new atomic connection if checked.
1554 */
1555 if (pConnectionItem->isChecked() && ((pConnectionItem->getStart().compare(pConnectionItem->getInitialStart()) != 0)
1556 || (pConnectionItem->getEnd().compare(pConnectionItem->getInitialEnd()) != 0))) {
1557 addAtomicConnection(pConnectionItem->getStart(), pConnectionItem->getEnd());
1558 }
1559 }
1560 } else if (pConnectionItem->isChecked()) { // add atomic connection
1561 addAtomicConnection(pConnectionItem->getStart(), pConnectionItem->getEnd());
1562 }
1563 }
1564}
1565
1566/*!
1567 * \brief BusConnectionDialog::deleteAtomicConnection
1568 * Deletes an atomic connection
1569 * \param startConnectorName
1570 * \param endConnectorName
1571 */
1572void BusConnectionDialog::deleteAtomicConnection(QString startConnectorName, QString endConnectorName)
1573{
1574 LibraryTreeItem *pStartLibraryTreeItem = mpConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem();
1575 LibraryTreeItem *pStartParentLibraryTreeItem = pStartLibraryTreeItem->parent();
1576 LibraryTreeItem *pEndLibraryTreeItem = mpConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem();
1577 LibraryTreeItem *pEndParentLibraryTreeItem = pEndLibraryTreeItem->parent();
1578
1579 Component *pStartComponent = pStartParentLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->getComponentObject(startConnectorName);
1580 Component *pEndComponent = pEndParentLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->getComponentObject(endConnectorName);
1581
1582 if (pStartComponent && pEndComponent) {
1583 foreach (LineAnnotation *pConnectionLineAnnotation, mpGraphicsView->getConnectionsList())for (auto _container_ = QtPrivate::qMakeForeachContainer(mpGraphicsView
->getConnectionsList()); _container_.control && _container_
.i != _container_.e; ++_container_.i, _container_.control ^= 1
) for (LineAnnotation *pConnectionLineAnnotation = *_container_
.i; _container_.control; _container_.control = 0)
{
1584 if ((pConnectionLineAnnotation->getStartComponentName().compare(pStartComponent->getLibraryTreeItem()->getNameStructure()) == 0)
1585 && (pConnectionLineAnnotation->getEndComponentName().compare(pEndComponent->getLibraryTreeItem()->getNameStructure()) == 0)) {
1586 DeleteConnectionCommand *pDeleteConnectionCommand = new DeleteConnectionCommand(pConnectionLineAnnotation);
1587 mpGraphicsView->getModelWidget()->getUndoStack()->push(pDeleteConnectionCommand);
1588 }
1589 }
1590 }
1591}
1592
1593/*!
1594 * \brief BusConnectionDialog::addAtomicConnection
1595 * Adds an atomic connection.
1596 * \param startConnectorName
1597 * \param endConnectorName
1598 */
1599void BusConnectionDialog::addAtomicConnection(QString startConnectorName, QString endConnectorName)
1600{
1601 LibraryTreeItem *pStartLibraryTreeItem = mpConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem();
1602 LibraryTreeItem *pStartParentLibraryTreeItem = pStartLibraryTreeItem->parent();
1603 LibraryTreeItem *pEndLibraryTreeItem = mpConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem();
1604 LibraryTreeItem *pEndParentLibraryTreeItem = pEndLibraryTreeItem->parent();
1605
1606 Component *pStartComponent = pStartParentLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->getComponentObject(startConnectorName);
1607 Component *pEndComponent = pEndParentLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->getComponentObject(endConnectorName);
1608
1609 if (pStartComponent && pEndComponent) {
1610 LineAnnotation *pNewConnectionLineAnnotation = new LineAnnotation("", 0, 0, mpGraphicsView);
1611 pNewConnectionLineAnnotation->updateShape(mpConnectionLineAnnotation);
1612 pNewConnectionLineAnnotation->setOMSConnectionType(oms_connection_single);
1613 pNewConnectionLineAnnotation->setStartComponentName(pStartComponent->getLibraryTreeItem()->getNameStructure());
1614 pNewConnectionLineAnnotation->setEndComponentName(pEndComponent->getLibraryTreeItem()->getNameStructure());
1615 AddConnectionCommand *pAddConnectionCommand = new AddConnectionCommand(pNewConnectionLineAnnotation, true);
1616 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddConnectionCommand);
1617 if (pNewConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem()->getOMSBusConnector()
1618 && pNewConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem()->getOMSBusConnector()) {
1619 pNewConnectionLineAnnotation->setVisible(false);
1620 }
1621 }
1622}
1623
1624/*!
1625 * \brief BusConnectionDialog::addBusConnection
1626 * Adds the bus connection and its corresponding atomic connections.
1627 */
1628void BusConnectionDialog::addBusConnection()
1629{
1630 if (mAddCase) {
1631 mpGraphicsView->getModelWidget()->beginMacro(Helper::addBusConnection);
1632 } else {
1633 mpGraphicsView->getModelWidget()->beginMacro(Helper::editBusConnection);
1634 }
1635
1636 addOrDeleteAtomicConnections(mpInputOutputConnectionsModel);
1637 addOrDeleteAtomicConnections(mpOutputInputConnectionsModel);
1638
1639 if (mAddCase
1640 && mpConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem()->getOMSBusConnector()
1641 && mpConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem()->getOMSBusConnector()) {
1642 LibraryTreeItem *pStartLibraryTreeItem = mpConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem();
1643 mpConnectionLineAnnotation->setStartComponentName(pStartLibraryTreeItem->getNameStructure());
1644
1645 LibraryTreeItem *pEndLibraryTreeItem = mpConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem();
1646 mpConnectionLineAnnotation->setEndComponentName(pEndLibraryTreeItem->getNameStructure());
1647
1648 mpConnectionLineAnnotation->setOMSConnectionType(oms_connection_bus);
1649 mpConnectionLineAnnotation->setLineThickness(0.5);
1650
1651 AddConnectionCommand *pAddConnectionCommand = new AddConnectionCommand(mpConnectionLineAnnotation, true);
1652 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddConnectionCommand);
1653 } else if (mAddCase) {
1654 /* When connecting a connector to a bus connector
1655 * We don't want a bus connection in that case so we have to delete it since the atomic connection
1656 * already created above.
1657 */
1658 mpGraphicsView->removeCurrentConnection();
1659 }
1660
1661 mpGraphicsView->getModelWidget()->updateModelText();
1662 mpGraphicsView->getModelWidget()->endMacro();
1663 accept();
1664}
1665
1666/*!
1667 * \class TLMBusConnectionDialog
1668 * \brief A dialog for creating a tlm bus connection.
1669 */
1670/*!
1671 * \brief TLMBusConnectionDialog::TLMBusConnectionDialog
1672 * \param pGraphicsView
1673 * \param pConnectionLineAnnotation
1674 */
1675TLMConnectionDialog::TLMConnectionDialog(GraphicsView *pGraphicsView, LineAnnotation *pConnectionLineAnnotation, bool addCase)
1676 : QDialog(pGraphicsView)
1677{
1678 setAttribute(Qt::WA_DeleteOnClose);
1679 setWindowTitle(QString("%1 - %2").arg(Helper::applicationName).arg(addCase ? Helper::addTLMConnection : Helper::editTLMConnection));
1680 mpGraphicsView = pGraphicsView;
1681 mpConnectionLineAnnotation = pConnectionLineAnnotation;
1682 mAddCase = addCase;
1683 // set heading
1684 mpHeading = Utilities::getHeadingLabel(addCase ? Helper::addTLMConnection : Helper::editTLMConnection);
1685 // set separator line
1686 mpHorizontalLine = Utilities::getHeadingLine();
1687 // delay
1688 mpDelayLabel = new Label(tr("Delay:"));
1689 mpDelayTextBox = new QLineEdit(addCase ? "" : pConnectionLineAnnotation->getDelay());
1690 // alpha
1691 mpAlphaLabel = new Label(tr("Alpha:"));
1692 mpAlphaTextBox = new QLineEdit(addCase ? "" : pConnectionLineAnnotation->getAlpha());
1693 // Linear Impedance
1694 mpLinearImpedanceLabel = new Label(tr("Linear Impedance:"));
1695 mpLinearImpedanceTextBox = new QLineEdit(addCase ? "" : pConnectionLineAnnotation->getZf());
1696 // Angular Impedance
1697 mpAngularImpedanceLabel = new Label(tr("Angular Impedance:"));
1698 mpAngularImpedanceTextBox = new QLineEdit(addCase ? "" : pConnectionLineAnnotation->getZfr());
1699 // Add the validator
1700 QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
1701 mpDelayTextBox->setValidator(pDoubleValidator);
1702 mpAlphaTextBox->setValidator(pDoubleValidator);
1703 mpLinearImpedanceTextBox->setValidator(pDoubleValidator);
1704 mpAngularImpedanceTextBox->setValidator(pDoubleValidator);
1705 // buttons
1706 mpOkButton = new QPushButton(Helper::ok);
1707 mpOkButton->setAutoDefault(true);
1708 connect(mpOkButton, SIGNAL(clicked())"2""clicked()", SLOT(addTLMConnection())"1""addTLMConnection()");
1709 mpCancelButton = new QPushButton(Helper::cancel);
1710 mpCancelButton->setAutoDefault(false);
1711 connect(mpCancelButton, SIGNAL(clicked())"2""clicked()", SLOT(reject())"1""reject()");
1712 // add buttons to the button box
1713 mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
1714 mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
1715 mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
1716 // set the layout
1717 QGridLayout *pMainLayout = new QGridLayout;
1718 pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
1719 pMainLayout->addWidget(mpHeading, 0, 0, 1, 2);
1720 pMainLayout->addWidget(mpHorizontalLine, 1, 0, 1, 2);
1721 pMainLayout->addWidget(mpDelayLabel, 2, 0);
1722 pMainLayout->addWidget(mpDelayTextBox, 2, 1);
1723 pMainLayout->addWidget(mpAlphaLabel, 3, 0);
1724 pMainLayout->addWidget(mpAlphaTextBox, 3, 1);
1725 pMainLayout->addWidget(mpLinearImpedanceLabel, 4, 0);
1726 pMainLayout->addWidget(mpLinearImpedanceTextBox, 4, 1);
1727 pMainLayout->addWidget(mpAngularImpedanceLabel, 5, 0);
1728 pMainLayout->addWidget(mpAngularImpedanceTextBox, 5, 1);
1729 pMainLayout->addWidget(mpButtonBox, 6, 0, 1, 2, Qt::AlignRight);
1730 setLayout(pMainLayout);
1731}
1732
1733void TLMConnectionDialog::addTLMConnection()
1734{
1735 if (mpDelayTextBox->text().isEmpty()) {
1736 QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
1737 GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg(tr("Delay")), Helper::ok);
1738 return;
1739 }
1740
1741 if (mpAlphaTextBox->text().isEmpty()) {
1742 QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
1743 GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg(tr("Alpha")), Helper::ok);
1744 return;
1745 }
1746
1747 if (mpLinearImpedanceTextBox->text().isEmpty()) {
1748 QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
1749 GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg(tr("Linear Impedance")), Helper::ok);
1750 return;
1751 }
1752
1753 if (mpAngularImpedanceTextBox->text().isEmpty()) {
1754 QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
1755 GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg(tr("Angular Impedance")), Helper::ok);
1756 return;
1757 }
1758
1759 if (mAddCase) {
1760 mpConnectionLineAnnotation->setStartComponentName(mpConnectionLineAnnotation->getStartComponent()->getLibraryTreeItem()->getNameStructure());
1761 mpConnectionLineAnnotation->setEndComponentName(mpConnectionLineAnnotation->getEndComponent()->getLibraryTreeItem()->getNameStructure());
1762 mpConnectionLineAnnotation->setDelay(mpDelayTextBox->text());
1763 mpConnectionLineAnnotation->setAlpha(mpAlphaTextBox->text());
1764 mpConnectionLineAnnotation->setZf(mpLinearImpedanceTextBox->text());
1765 mpConnectionLineAnnotation->setZfr(mpAngularImpedanceTextBox->text());
1766 mpConnectionLineAnnotation->setOMSConnectionType(oms_connection_tlm);
1767
1768 AddConnectionCommand *pAddConnectionCommand = new AddConnectionCommand(mpConnectionLineAnnotation, true);
1769 mpGraphicsView->getModelWidget()->getUndoStack()->push(pAddConnectionCommand);
1770 } else {
1771 oms_tlm_connection_parameters_t oldTLMParameters;
1772 oldTLMParameters.delay = mpConnectionLineAnnotation->getDelay().toDouble();
1773 oldTLMParameters.alpha = mpConnectionLineAnnotation->getAlpha().toDouble();
1774 oldTLMParameters.linearimpedance = mpConnectionLineAnnotation->getZf().toDouble();
1775 oldTLMParameters.angularimpedance = mpConnectionLineAnnotation->getZfr().toDouble();
1776
1777 oms_tlm_connection_parameters_t newTLMParameters;
1778 newTLMParameters.delay = mpDelayTextBox->text().toDouble();
1779 newTLMParameters.alpha = mpAlphaTextBox->text().toDouble();
1780 newTLMParameters.linearimpedance = mpLinearImpedanceTextBox->text().toDouble();
1781 newTLMParameters.angularimpedance = mpAngularImpedanceTextBox->text().toDouble();
1782
1783 UpdateTLMParametersCommand *pUpdateTLMParametersCommand = new UpdateTLMParametersCommand(mpConnectionLineAnnotation, oldTLMParameters,
1784 newTLMParameters);
1785 mpGraphicsView->getModelWidget()->getUndoStack()->push(pUpdateTLMParametersCommand);
1786 }
1787 mpGraphicsView->getModelWidget()->updateModelText();
1788 accept();
1789}