/
ModelWidgetContainer.cpp
7116 lines (6877 loc) · 320 KB
/
ModelWidgetContainer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/
#include "Modeling/ModelWidgetContainer.h"
#include "MainWindow.h"
#include "LibraryTreeWidget.h"
#include "ItemDelegate.h"
#include "Options/OptionsDialog.h"
#include "MessagesWidget.h"
#include "DocumentationWidget.h"
#include "Annotations/ShapePropertiesDialog.h"
#include "Component/ComponentProperties.h"
#include "Commands.h"
#include "TLM/FetchInterfaceDataDialog.h"
#include "Plotting/VariablesWidget.h"
#include "Options/NotificationsDialog.h"
#include "ModelicaClassDialog.h"
#include "TLM/TLMCoSimulationDialog.h"
#include "Git/GitCommands.h"
#if !defined(WITHOUT_OSG)
#include "Animation/ThreeDViewer.h"
#endif
#include "OMS/OMSProxy.h"
#include "Modeling/BusDialog.h"
#include <QNetworkReply>
//! @class GraphicsScene
//! @brief The GraphicsScene class is a container for graphicsl components in a simulationmodel.
//! Constructor.
//! @param parent defines a parent to the new instanced object.
GraphicsScene::GraphicsScene(StringHandler::ViewType viewType, ModelWidget *pModelWidget)
: QGraphicsScene(pModelWidget), mViewType(viewType)
{
mpModelWidget = pModelWidget;
}
//! @class GraphicsView
//! @brief The GraphicsView class is a class which display the content of a scene of components.
//! Constructor.
//! @param parent defines a parent to the new instanced object.
GraphicsView::GraphicsView(StringHandler::ViewType viewType, ModelWidget *parent)
: QGraphicsView(parent), mViewType(viewType), mSkipBackground(false)
{
/* Ticket #3275
* Set the scroll bars policy to always on to avoid unnecessary resize events.
*/
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setFrameShape(QFrame::StyledPanel);
setDragMode(QGraphicsView::RubberBandDrag);
setAcceptDrops(true);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
setMouseTracking(true);
mpModelWidget = parent;
// set the coOrdinate System
mCoOrdinateSystem = CoOrdinateSystem();
GraphicalViewsPage *pGraphicalViewsPage;
pGraphicalViewsPage = OptionsDialog::instance()->getGraphicalViewsPage();
QList<QPointF> extent;
qreal left = (mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewExtentLeft() : pGraphicalViewsPage->getDiagramViewExtentLeft();
qreal bottom = (mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewExtentBottom() : pGraphicalViewsPage->getDiagramViewExtentBottom();
qreal right = (mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewExtentRight() : pGraphicalViewsPage->getDiagramViewExtentRight();
qreal top = (mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewExtentTop() : pGraphicalViewsPage->getDiagramViewExtentTop();
extent << QPointF(left, bottom) << QPointF(right, top);
mCoOrdinateSystem.setExtent(extent);
mCoOrdinateSystem.setPreserveAspectRatio((mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewPreserveAspectRation() : pGraphicalViewsPage->getDiagramViewPreserveAspectRation());
mCoOrdinateSystem.setInitialScale((mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewScaleFactor() : pGraphicalViewsPage->getDiagramViewScaleFactor());
qreal horizontal = (mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewGridHorizontal() : pGraphicalViewsPage->getDiagramViewGridHorizontal();
qreal vertical = (mViewType == StringHandler::Icon) ? pGraphicalViewsPage->getIconViewGridVertical() : pGraphicalViewsPage->getDiagramViewGridVertical();
mCoOrdinateSystem.setGrid(QPointF(horizontal, vertical));
setExtentRectangle(left, bottom, right, top);
scale(1.0, -1.0); // invert the drawing area.
setIsCustomScale(false);
setAddClassAnnotationNeeded(false);
setIsCreatingConnection(false);
setIsCreatingTransition(false);
mIsCreatingLineShape = false;
mIsCreatingPolygonShape = false;
mIsCreatingRectangleShape = false;
mIsCreatingEllipseShape = false;
mIsCreatingTextShape = false;
mIsCreatingBitmapShape = false;
mIsPanning = false;
mLastMouseEventPos = QPoint(0, 0);
mpClickedComponent = 0;
mpClickedState = 0;
setIsMovingComponentsAndShapes(false);
setRenderingLibraryPixmap(false);
mpConnectionLineAnnotation = 0;
mpTransitionLineAnnotation = 0;
mpLineShapeAnnotation = 0;
mpPolygonShapeAnnotation = 0;
mpRectangleShapeAnnotation = 0;
mpEllipseShapeAnnotation = 0;
mpTextShapeAnnotation = 0;
mpBitmapShapeAnnotation = 0;
createActions();
}
void GraphicsView::setExtentRectangle(qreal left, qreal bottom, qreal right, qreal top)
{
mExtentRectangle = QRectF(left, bottom, fabs(left - right), fabs(bottom - top));
/* Ticket:4340 Extend vertical space
* Make the drawing area 25% bigger than the actual size. So we can better use the panning feature.
*/
QRectF sceneRectangle(left * 1.5, bottom * 1.5, fabs(left - right) * 1.5, fabs(bottom - top) * 1.5);
setSceneRect(sceneRectangle);
centerOn(sceneRectangle.center());
}
void GraphicsView::setIsCreatingConnection(bool enable)
{
mIsCreatingConnection = enable;
setDragModeInternal(enable);
setItemsFlags(!enable);
}
void GraphicsView::setIsCreatingTransition(bool enable)
{
mIsCreatingTransition = enable;
if (enable) {
setDragMode(QGraphicsView::NoDrag);
} else {
setDragMode(QGraphicsView::RubberBandDrag);
}
setItemsFlags(!enable);
}
void GraphicsView::setIsCreatingLineShape(bool enable)
{
mIsCreatingLineShape = enable;
setDragModeInternal(enable);
setItemsFlags(!enable);
updateUndoRedoActions(enable);
}
void GraphicsView::setIsCreatingPolygonShape(bool enable)
{
mIsCreatingPolygonShape = enable;
setDragModeInternal(enable);
setItemsFlags(!enable);
updateUndoRedoActions(enable);
}
void GraphicsView::setIsCreatingRectangleShape(bool enable)
{
mIsCreatingRectangleShape = enable;
setDragModeInternal(enable);
setItemsFlags(!enable);
updateUndoRedoActions(enable);
}
void GraphicsView::setIsCreatingEllipseShape(bool enable)
{
mIsCreatingEllipseShape = enable;
setDragModeInternal(enable);
setItemsFlags(!enable);
updateUndoRedoActions(enable);
}
void GraphicsView::setIsCreatingTextShape(bool enable)
{
mIsCreatingTextShape = enable;
setDragModeInternal(enable);
setItemsFlags(!enable);
updateUndoRedoActions(enable);
}
void GraphicsView::setIsCreatingBitmapShape(bool enable)
{
mIsCreatingBitmapShape = enable;
setDragModeInternal(enable);
setItemsFlags(!enable);
updateUndoRedoActions(enable);
}
void GraphicsView::setIsPanning(bool enable)
{
mIsPanning = enable;
setDragModeInternal(enable, true);
setItemsFlags(!enable);
}
void GraphicsView::setDragModeInternal(bool enable, bool updateCursor)
{
if (enable) {
setDragMode(QGraphicsView::NoDrag);
if (updateCursor) {
viewport()->setCursor(Qt::ClosedHandCursor);
}
} else {
setDragMode(QGraphicsView::RubberBandDrag);
if (updateCursor) {
viewport()->unsetCursor();
}
}
}
void GraphicsView::setItemsFlags(bool enable)
{
// set components, shapes and connection flags accordingly
foreach(Component *pComponent, mComponentsList) {
pComponent->setComponentFlags(enable);
}
foreach(ShapeAnnotation *pShapeAnnotation, mShapesList){
pShapeAnnotation->setShapeFlags(enable);
}
foreach(LineAnnotation *pLineAnnotation, mConnectionsList) {
pLineAnnotation->setShapeFlags(enable);
}
}
/*!
* \brief GraphicsView::updateUndoRedoActions
* Updates the Undo Redo actions depending shape(s) creation state.
* \param enable
*/
void GraphicsView::updateUndoRedoActions(bool enable)
{
if (enable) {
MainWindow::instance()->getUndoAction()->setEnabled(!enable);
MainWindow::instance()->getRedoAction()->setEnabled(!enable);
} else {
mpModelWidget->updateUndoRedoActions();
}
}
bool GraphicsView::addComponent(QString className, QPointF position)
{
MainWindow *pMainWindow = MainWindow::instance();
LibraryTreeItem *pLibraryTreeItem = pMainWindow->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItem(className);
if (!pLibraryTreeItem) {
return false;
}
mpModelWidget->removeDynamicResults(); // show static values during editing
// if we are dropping something on meta-model editor then we can skip Modelica stuff.
if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::CompositeModel) {
if (!pLibraryTreeItem->isSaved()) {
QMessageBox::information(pMainWindow, QString(Helper::applicationName).append(" - ").append(Helper::information),
tr("The class <b>%1</b> is not saved. You can only drag & drop saved classes.")
.arg(pLibraryTreeItem->getNameStructure()), Helper::ok);
return false;
} else {
// item not to be dropped on itself; if dropping an item on itself
if (isClassDroppedOnItself(pLibraryTreeItem)) {
return false;
}
QString name = getUniqueComponentName(StringHandler::toCamelCase(pLibraryTreeItem->getName()));
ComponentInfo *pComponentInfo = new ComponentInfo;
QFileInfo fileInfo(pLibraryTreeItem->getFileName());
// create StartCommand depending on the external model file extension.
if (fileInfo.suffix().compare("mo") == 0) {
pComponentInfo->setStartCommand("StartTLMOpenModelica");
} else if (fileInfo.suffix().compare("in") == 0) {
pComponentInfo->setStartCommand("StartTLMBeast");
} else if (fileInfo.suffix().compare("hmf") == 0) {
pComponentInfo->setStartCommand("StartTLMHopsan");
} else if (fileInfo.suffix().compare("fmu") == 0) {
pComponentInfo->setStartCommand("StartTLMFmiWrapper");
} else if (fileInfo.suffix().compare("slx") == 0) {
pComponentInfo->setStartCommand("StartTLMSimulink");
} else {
pComponentInfo->setStartCommand("");
}
pComponentInfo->setModelFile(fileInfo.fileName());
addComponentToView(name, pLibraryTreeItem, "", position, pComponentInfo, true, false);
return true;
}
} else {
// Only allow drag & drop of Modelica LibraryTreeItem on a Modelica LibraryTreeItem
if (mpModelWidget->getLibraryTreeItem()->getLibraryType() != pLibraryTreeItem->getLibraryType()) {
QMessageBox::information(pMainWindow, QString("%1 - %2").arg(Helper::applicationName, Helper::information),
tr("You can only drag & drop Modelica models."), Helper::ok);
return false;
}
StringHandler::ModelicaClasses type = pLibraryTreeItem->getRestriction();
OptionsDialog *pOptionsDialog = OptionsDialog::instance();
// item not to be dropped on itself; if dropping an item on itself
if (isClassDroppedOnItself(pLibraryTreeItem)) {
return false;
} else { // check if the model is partial
QString name;
if (pLibraryTreeItem->isPartial()) {
if (pOptionsDialog->getNotificationsPage()->getReplaceableIfPartialCheckBox()->isChecked()) {
NotificationsDialog *pNotificationsDialog = new NotificationsDialog(NotificationsDialog::ReplaceableIfPartial,
NotificationsDialog::InformationIcon,
MainWindow::instance());
pNotificationsDialog->setNotificationLabelString(GUIMessages::getMessage(GUIMessages::MAKE_REPLACEABLE_IF_PARTIAL)
.arg(StringHandler::getModelicaClassType(type).toLower()).arg(name));
if (!pNotificationsDialog->exec()) {
return false;
}
}
}
// get the model defaultComponentPrefixes
QString defaultPrefix = pMainWindow->getOMCProxy()->getDefaultComponentPrefixes(pLibraryTreeItem->getNameStructure());
// get the model defaultComponentName
QString defaultName = pMainWindow->getOMCProxy()->getDefaultComponentName(pLibraryTreeItem->getNameStructure());
if (defaultName.isEmpty()) {
name = getUniqueComponentName(StringHandler::toCamelCase(pLibraryTreeItem->getName()));
} else {
if (checkComponentName(defaultName)) {
name = defaultName;
} else {
name = getUniqueComponentName(defaultName);
}
}
// Allow user to change the component name if always ask for component name settings is true.
if (pOptionsDialog->getNotificationsPage()->getAlwaysAskForDraggedComponentName()->isChecked()) {
ComponentNameDialog *pComponentNameDialog = new ComponentNameDialog(name, this, pMainWindow);
if (pComponentNameDialog->exec()) {
name = pComponentNameDialog->getComponentName();
pComponentNameDialog->deleteLater();
} else {
pComponentNameDialog->deleteLater();
return false;
}
}
// if we or user has changed the default name
if (!defaultName.isEmpty() && name.compare(defaultName) != 0) {
// show the information to the user if we have changed the name of some inner component.
if (defaultPrefix.contains("inner")) {
if (pOptionsDialog->getNotificationsPage()->getInnerModelNameChangedCheckBox()->isChecked()) {
NotificationsDialog *pNotificationsDialog = new NotificationsDialog(NotificationsDialog::InnerModelNameChanged,
NotificationsDialog::InformationIcon,
MainWindow::instance());
pNotificationsDialog->setNotificationLabelString(GUIMessages::getMessage(GUIMessages::INNER_MODEL_NAME_CHANGED)
.arg(defaultName).arg(name));
if (!pNotificationsDialog->exec()) {
return false;
}
}
}
}
ComponentInfo *pComponentInfo = new ComponentInfo;
pComponentInfo->applyDefaultPrefixes(defaultPrefix);
// if dropping an item on the diagram layer
if (mViewType == StringHandler::Diagram) {
// if item is a class, model, block, connector or record. then we can drop it to the graphicsview
if ((type == StringHandler::Class) || (type == StringHandler::Model) || (type == StringHandler::Block) ||
(type == StringHandler::ExpandableConnector) || (type == StringHandler::Connector) || (type == StringHandler::Record)) {
addComponentToView(name, pLibraryTreeItem, "", position, pComponentInfo);
return true;
} else {
QMessageBox::information(pMainWindow, QString(Helper::applicationName).append(" - ").append(Helper::information),
GUIMessages::getMessage(GUIMessages::DIAGRAM_VIEW_DROP_MSG).arg(pLibraryTreeItem->getNameStructure())
.arg(StringHandler::getModelicaClassType(type)), Helper::ok);
return false;
}
} else if (mViewType == StringHandler::Icon) { // if dropping an item on the icon layer
// if item is a connector. then we can drop it to the graphicsview
if (type == StringHandler::Connector || type == StringHandler::ExpandableConnector) {
addComponentToView(name, pLibraryTreeItem, "", position, pComponentInfo);
return true;
} else {
QMessageBox::information(pMainWindow, QString(Helper::applicationName).append(" - ").append(Helper::information),
GUIMessages::getMessage(GUIMessages::ICON_VIEW_DROP_MSG).arg(pLibraryTreeItem->getNameStructure())
.arg(StringHandler::getModelicaClassType(type)), Helper::ok);
return false;
}
}
}
}
return false;
}
/*!
* \brief GraphicsView::addComponentToView
* Adds the Component to the Graphical Views.
* \param name
* \param pLibraryTreeItem
* \param annotation
* \param position
* \param pComponentInfo
* \param addObject
* \param openingClass
*/
void GraphicsView::addComponentToView(QString name, LibraryTreeItem *pLibraryTreeItem, QString annotation, QPointF position,
ComponentInfo *pComponentInfo, bool addObject, bool openingClass)
{
AddComponentCommand *pAddComponentCommand;
pAddComponentCommand = new AddComponentCommand(name, pLibraryTreeItem, annotation, position, pComponentInfo, addObject, openingClass, this);
mpModelWidget->getUndoStack()->push(pAddComponentCommand);
if (!openingClass) {
mpModelWidget->getLibraryTreeItem()->emitComponentAdded(pAddComponentCommand->getComponent());
mpModelWidget->updateModelText();
}
}
void GraphicsView::addComponentToClass(Component *pComponent)
{
if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::Modelica) {
MainWindow *pMainWindow = MainWindow::instance();
// Add the component to model in OMC.
/* Ticket:4132
* Always send the full path so that addComponent API doesn't fail when it makes a call to getDefaultPrefixes.
* I updated the addComponent API to make path relative.
*/
pMainWindow->getOMCProxy()->addComponent(pComponent->getName(), pComponent->getComponentInfo()->getClassName(),
mpModelWidget->getLibraryTreeItem()->getNameStructure(), pComponent->getPlacementAnnotation());
LibraryTreeModel *pLibraryTreeModel = pMainWindow->getLibraryWidget()->getLibraryTreeModel();
// get the toplevel class of dragged component
QString packageName = StringHandler::getFirstWordBeforeDot(pComponent->getLibraryTreeItem()->getNameStructure());
LibraryTreeItem *pPackageLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(packageName);
// get the top level class of current class
QString topLevelClassName = StringHandler::getFirstWordBeforeDot(mpModelWidget->getLibraryTreeItem()->getNameStructure());
LibraryTreeItem *pTopLevelLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(topLevelClassName);
if (pPackageLibraryTreeItem && pTopLevelLibraryTreeItem) {
// get uses annotation of the toplevel class
QList<QList<QString > > usesAnnotation = pMainWindow->getOMCProxy()->getUses(pTopLevelLibraryTreeItem->getNameStructure());
QStringList newUsesAnnotation;
for (int i = 0 ; i < usesAnnotation.size() ; i++) {
if (usesAnnotation.at(i).at(0).compare(packageName) == 0) {
return; // if the package is already in uses annotation of class then simply return without doing anything.
} else {
newUsesAnnotation.append(QString("%1(version=\"%2\")").arg(usesAnnotation.at(i).at(0)).arg(usesAnnotation.at(i).at(1)));
}
}
// if the package has version only then add the uses annotation
if (!pPackageLibraryTreeItem->mClassInformation.version.isEmpty() &&
// Do not add a uses-annotation to itself
pTopLevelLibraryTreeItem->getNameStructure() != packageName) {
newUsesAnnotation.append(QString("%1(version=\"%2\")").arg(packageName).arg(pPackageLibraryTreeItem->mClassInformation.version));
QString usesAnnotationString = QString("annotate=$annotation(uses(%1))").arg(newUsesAnnotation.join(","));
pMainWindow->getOMCProxy()->addClassAnnotation(pTopLevelLibraryTreeItem->getNameStructure(), usesAnnotationString);
pLibraryTreeModel->updateLibraryTreeItemClassText(pTopLevelLibraryTreeItem);
}
}
} else if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::CompositeModel) {
// add SubModel Element
CompositeModelEditor *pCompositeModelEditor = dynamic_cast<CompositeModelEditor*>(mpModelWidget->getEditor());
pCompositeModelEditor->addSubModel(pComponent);
/* We need to iterate over Component childrens
* because if user deletes a submodel for which interfaces are already fetched
* then undoing the delete operation reaches here and we should add the interfaces back.
*/
foreach (Component *pInterfaceComponent, pComponent->getComponentsList()) {
pCompositeModelEditor->addInterface(pInterfaceComponent, pComponent->getName());
}
}
}
/*!
* \brief GraphicsView::deleteComponent
* Delete the component and its corresponding connectors from the components list and OMC.
* \param component is the object to be deleted.
*/
void GraphicsView::deleteComponent(Component *pComponent)
{
// First Remove the Connector associated to this component
int i = 0;
while(i != mConnectionsList.size()) {
QString startComponentName, endComponentName = "";
if (mConnectionsList[i]->getStartComponent()) {
startComponentName = mConnectionsList[i]->getStartComponent()->getRootParentComponent()->getName();
}
if (mConnectionsList[i]->getEndComponent()) {
endComponentName = mConnectionsList[i]->getEndComponent()->getRootParentComponent()->getName();
}
if (startComponentName == pComponent->getName() || endComponentName == pComponent->getName()) {
deleteConnection(mConnectionsList[i]);
i = 0; //Restart iteration if map has changed
} else {
++i;
}
}
pComponent->setSelected(false);
if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::OMS) {
mpModelWidget->getUndoStack()->push(new DeleteSubModelCommand(pComponent, this));
} else {
mpModelWidget->getUndoStack()->push(new DeleteComponentCommand(pComponent, this));
}
}
/*!
* \brief GraphicsView::deleteComponentObject
* Deletes the Component.
* \param pComponent
*/
void GraphicsView::deleteComponentFromClass(Component *pComponent)
{
if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::Modelica) {
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
// delete the component from OMC
pOMCProxy->deleteComponent(pComponent->getName(), mpModelWidget->getLibraryTreeItem()->getNameStructure());
} else if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::CompositeModel) {
CompositeModelEditor *pCompositeModelEditor = dynamic_cast<CompositeModelEditor*>(mpModelWidget->getEditor());
pCompositeModelEditor->deleteSubModel(pComponent->getName());
}
}
/*!
* \brief GraphicsView::getComponentObject
* Finds the Component
* \param componentName
* \return
*/
Component* GraphicsView::getComponentObject(QString componentName)
{
// look in inherited components
foreach (Component *pInheritedComponent, mInheritedComponentsList) {
if (pInheritedComponent->getName().compare(componentName) == 0) {
return pInheritedComponent;
}
}
// look in components
foreach (Component *pComponent, mComponentsList) {
if (pComponent->getName().compare(componentName) == 0) {
return pComponent;
}
}
return 0;
}
/*!
* \brief GraphicsView::getUniqueComponentName
* Creates a unique component name.
* \param componentName
* \param number
* \return
*/
QString GraphicsView::getUniqueComponentName(QString componentName, int number)
{
QString name;
name = QString(componentName).append(QString::number(number));
foreach (Component *pComponent, mComponentsList) {
if (pComponent->getName().compare(name, Qt::CaseSensitive) == 0) {
name = getUniqueComponentName(componentName, ++number);
break;
}
}
return name;
}
/*!
* \brief GraphicsView::checkComponentName
* Checks if the component with the same name already exists or not.
* \param componentName
* \return
*/
bool GraphicsView::checkComponentName(QString componentName)
{
foreach (Component *pComponent, mComponentsList) {
if (pComponent->getName().compare(componentName, Qt::CaseSensitive) == 0) {
return false;
}
}
return true;
}
/*!
* \brief GraphicsView::addConnectionToOMC
* Adds the connection to class.
* \param pConnectionLineAnnotation - the connection to add.
*/
void GraphicsView::addConnectionToClass(LineAnnotation *pConnectionLineAnnotation)
{
if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::CompositeModel) {
CompositeModelEditor *pCompositeModelEditor = dynamic_cast<CompositeModelEditor*>(mpModelWidget->getEditor());
if (pCompositeModelEditor) {
pCompositeModelEditor->createConnection(pConnectionLineAnnotation);
}
} else if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::OMS) {
if (OMSProxy::instance()->addConnection(mpModelWidget->getLibraryTreeItem()->getNameStructure(),
pConnectionLineAnnotation->getStartComponentName(),
pConnectionLineAnnotation->getEndComponentName())) {
pConnectionLineAnnotation->updateOMSConnection();
}
} else {
MainWindow *pMainWindow = MainWindow::instance();
if (pMainWindow->getOMCProxy()->addConnection(pConnectionLineAnnotation->getStartComponentName(),
pConnectionLineAnnotation->getEndComponentName(),
mpModelWidget->getLibraryTreeItem()->getNameStructure(),
QString("annotate=").append(pConnectionLineAnnotation->getShapeAnnotation()))) {
/* Ticket #2450
* Do not check for the ports compatibility via instantiatemodel. Just let the user create the connection.
*/
//pMainWindow->getOMCProxy()->instantiateModelSucceeds(mpModelWidget->getNameStructure());
}
}
}
/*!
* \brief GraphicsView::deleteConnectionFromClass
* Deletes the connection from class.
* \param pConnectionLineAnnotation - the connection to delete.
*/
void GraphicsView::deleteConnectionFromClass(LineAnnotation *pConnectionLineAnnotation)
{
MainWindow *pMainWindow = MainWindow::instance();
if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::CompositeModel) {
CompositeModelEditor *pCompositeModelEditor = dynamic_cast<CompositeModelEditor*>(mpModelWidget->getEditor());
pCompositeModelEditor->deleteConnection(pConnectionLineAnnotation->getStartComponentName(), pConnectionLineAnnotation->getEndComponentName());
} else if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::OMS) {
OMSProxy::instance()->deleteConnection(mpModelWidget->getLibraryTreeItem()->getNameStructure(),
pConnectionLineAnnotation->getStartComponentName(),
pConnectionLineAnnotation->getEndComponentName());
} else {
pMainWindow->getOMCProxy()->deleteConnection(pConnectionLineAnnotation->getStartComponentName(),
pConnectionLineAnnotation->getEndComponentName(),
mpModelWidget->getLibraryTreeItem()->getNameStructure());
}
}
/*!
* \brief GraphicsView::updateConnectionInClass
* Updates a connection in a class.
* \param pConnectonLineAnnotation
*/
void GraphicsView::updateConnectionInClass(LineAnnotation *pConnectionLineAnnotation)
{
if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::CompositeModel) {
CompositeModelEditor *pCompositeModelEditor = dynamic_cast<CompositeModelEditor*>(mpModelWidget->getEditor());
if (pCompositeModelEditor) {
pCompositeModelEditor->updateConnection(pConnectionLineAnnotation);
}
}
}
/*!
* \brief GraphicsView::removeConnectionsFromView
* Removes the connections from the view.
*/
void GraphicsView::removeConnectionsFromView()
{
foreach (LineAnnotation *pConnectionLineAnnotation, mConnectionsList) {
deleteConnectionFromList(pConnectionLineAnnotation);
removeItem(pConnectionLineAnnotation);
}
}
/*!
* \brief GraphicsView::addTransitionToClass
* Adds the transition to class.
* \param pTransitionLineAnnotation - the transition to add.
*/
void GraphicsView::addTransitionToClass(LineAnnotation *pTransitionLineAnnotation)
{
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
if (pOMCProxy->addTransition(mpModelWidget->getLibraryTreeItem()->getNameStructure(), pTransitionLineAnnotation->getStartComponentName(),
pTransitionLineAnnotation->getEndComponentName(), pTransitionLineAnnotation->getCondition(),
pTransitionLineAnnotation->getImmediate(), pTransitionLineAnnotation->getReset(),
pTransitionLineAnnotation->getSynchronize(), pTransitionLineAnnotation->getPriority(),
QString("annotate=$annotation(%1,%2)").arg(pTransitionLineAnnotation->getShapeAnnotation())
.arg(pTransitionLineAnnotation->getTextAnnotation()->getShapeAnnotation()))) {
}
}
/*!
* \brief GraphicsView::deleteTransitionFromClass
* Deletes the transition from class.
* \param pTransitionLineAnnotation - the transition to delete.
*/
void GraphicsView::deleteTransitionFromClass(LineAnnotation *pTransitionLineAnnotation)
{
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
pOMCProxy->deleteTransition(mpModelWidget->getLibraryTreeItem()->getNameStructure(), pTransitionLineAnnotation->getStartComponentName(),
pTransitionLineAnnotation->getEndComponentName(), pTransitionLineAnnotation->getCondition(),
pTransitionLineAnnotation->getImmediate(), pTransitionLineAnnotation->getReset(),
pTransitionLineAnnotation->getSynchronize(), pTransitionLineAnnotation->getPriority());
}
/*!
* \brief GraphicsView::addInitialStateToClass
* Adds the initial state to class.
* \param pInitialStateLineAnnotation - the initial state to add.
*/
void GraphicsView::addInitialStateToClass(LineAnnotation *pInitialStateLineAnnotation)
{
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
if (pOMCProxy->addInitialState(mpModelWidget->getLibraryTreeItem()->getNameStructure(),
pInitialStateLineAnnotation->getStartComponentName(),
QString("annotate=").append(pInitialStateLineAnnotation->getShapeAnnotation()))) {
}
}
/*!
* \brief GraphicsView::deleteInitialStateFromClass
* Deletes the initial state from class.
* \param pInitialStateLineAnnotation - the initial state to delete.
*/
void GraphicsView::deleteInitialStateFromClass(LineAnnotation *pInitialStateLineAnnotation)
{
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
pOMCProxy->deleteInitialState(mpModelWidget->getLibraryTreeItem()->getNameStructure(), pInitialStateLineAnnotation->getStartComponentName());
}
/*!
* \brief GraphicsView::addShapeToList
* \param pShape
* \param index
*/
void GraphicsView::addShapeToList(ShapeAnnotation *pShape, int index)
{
if (index <= -1) {
mShapesList.append(pShape);
} else {
mShapesList.insert(index, pShape);
}
}
/*!
* \brief GraphicsView::deleteShape
* Deletes the shape from the icon/diagram layer.
* \param pShapeAnnotation
*/
void GraphicsView::deleteShape(ShapeAnnotation *pShapeAnnotation)
{
pShapeAnnotation->setSelected(false);
mpModelWidget->getUndoStack()->push(new DeleteShapeCommand(pShapeAnnotation));
}
/*!
* \brief GraphicsView::deleteShapeFromList
* \param pShape
* \return
*/
int GraphicsView::deleteShapeFromList(ShapeAnnotation *pShape)
{
int index = mShapesList.indexOf(pShape);
mShapesList.removeOne(pShape);
return index;
}
/*!
* \brief GraphicsView::reOrderShapes
* Reorders the shapes.
*/
void GraphicsView::reOrderShapes()
{
int zValue = 0;
// set stacking order for inherited shapes
foreach (ShapeAnnotation *pShapeAnnotation, mInheritedShapesList) {
zValue++;
pShapeAnnotation->setZValue(zValue);
}
// set stacking order for shapes.
foreach (ShapeAnnotation *pShapeAnnotation, mShapesList) {
zValue++;
pShapeAnnotation->setZValue(zValue);
}
}
/*!
* \brief GraphicsView::bringToFront
* \param pShape
* Brings the shape to front of all other shapes.
*/
void GraphicsView::bringToFront(ShapeAnnotation *pShape)
{
deleteShapeFromList(pShape);
int i = 0;
// update the shapes z index
for (; i < mShapesList.size() ; i++) {
mShapesList.at(i)->setZValue(i + 1);
}
pShape->setZValue(i + 1);
mShapesList.append(pShape);
// update class annotation.
addClassAnnotation();
}
/*!
* \brief GraphicsView::bringForward
* \param pShape
* Brings the shape one level forward.
*/
void GraphicsView::bringForward(ShapeAnnotation *pShape)
{
int shapeIndex = mShapesList.indexOf(pShape);
if (shapeIndex == -1 || shapeIndex == mShapesList.size() - 1) { // if the shape is already at top.
return;
}
// swap the shapes in the list
mShapesList.swap(shapeIndex, shapeIndex + 1);
// update the shapes z index
for (int i = 0 ; i < mShapesList.size() ; i++) {
mShapesList.at(i)->setZValue(i + 1);
}
// update class annotation.
addClassAnnotation();
}
/*!
* \brief GraphicsView::sendToBack
* \param pShape
* Sends the shape to back of all other shapes.
*/
void GraphicsView::sendToBack(ShapeAnnotation *pShape)
{
deleteShapeFromList(pShape);
int i = 0;
pShape->setZValue(i + 1);
mShapesList.prepend(pShape);
// update the shapes z index
for (i = 1 ; i < mShapesList.size() ; i++) {
mShapesList.at(i)->setZValue(i + 1);
}
// update class annotation.
addClassAnnotation();
}
/*!
* \brief GraphicsView::sendBackward
* \param pShape
* Sends the shape one level backward.
*/
void GraphicsView::sendBackward(ShapeAnnotation *pShape)
{
int shapeIndex = mShapesList.indexOf(pShape);
if (shapeIndex <= 0) { // if the shape is already at bottom.
return;
}
// swap the shapes in the list
mShapesList.swap(shapeIndex - 1, shapeIndex);
// update the shapes z index
for (int i = 0 ; i < mShapesList.size() ; i++) {
mShapesList.at(i)->setZValue(i + 1);
}
// update class annotation.
addClassAnnotation();
}
void GraphicsView::createLineShape(QPointF point)
{
if (mpModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
return;
}
if (!isCreatingLineShape()) {
mpLineShapeAnnotation = new LineAnnotation("", this);
mpModelWidget->getUndoStack()->push(new AddShapeCommand(mpLineShapeAnnotation));
setIsCreatingLineShape(true);
mpLineShapeAnnotation->addPoint(point);
mpLineShapeAnnotation->addPoint(point);
} else { // if we are already creating a line then only add one point.
mpLineShapeAnnotation->addPoint(point);
}
}
void GraphicsView::createPolygonShape(QPointF point)
{
if (mpModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
return;
}
if (!isCreatingPolygonShape()) {
mpPolygonShapeAnnotation = new PolygonAnnotation("", this);
mpModelWidget->getUndoStack()->push(new AddShapeCommand(mpPolygonShapeAnnotation));
setIsCreatingPolygonShape(true);
mpPolygonShapeAnnotation->addPoint(point);
mpPolygonShapeAnnotation->addPoint(point);
mpPolygonShapeAnnotation->addPoint(point);
} else { // if we are already creating a polygon then only add one point.
mpPolygonShapeAnnotation->addPoint(point);
}
}
void GraphicsView::createRectangleShape(QPointF point)
{
if (mpModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
return;
}
if (!isCreatingRectangleShape()) {
mpRectangleShapeAnnotation = new RectangleAnnotation("", this);
mpModelWidget->getUndoStack()->push(new AddShapeCommand(mpRectangleShapeAnnotation));
setIsCreatingRectangleShape(true);
mpRectangleShapeAnnotation->replaceExtent(0, point);
mpRectangleShapeAnnotation->replaceExtent(1, point);
} else { // if we are already creating a rectangle then finish creating it.
// finish creating the rectangle
setIsCreatingRectangleShape(false);
MainWindow *pMainWindow = MainWindow::instance();
// set the transformation matrix
mpRectangleShapeAnnotation->setOrigin(mpRectangleShapeAnnotation->sceneBoundingRect().center());
mpRectangleShapeAnnotation->adjustExtentsWithOrigin();
mpRectangleShapeAnnotation->initializeTransformation();
// draw corner items for the rectangle shape
mpRectangleShapeAnnotation->drawCornerItems();
mpRectangleShapeAnnotation->setSelected(true);
// make the toolbar button of rectangle unchecked
pMainWindow->getRectangleShapeAction()->setChecked(false);
pMainWindow->getConnectModeAction()->setChecked(true);
mpModelWidget->getLibraryTreeItem()->emitShapeAdded(mpRectangleShapeAnnotation, this);
mpModelWidget->updateClassAnnotationIfNeeded();
mpModelWidget->updateModelText();
}
}
void GraphicsView::createEllipseShape(QPointF point)
{
if (mpModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
return;
}
if (!isCreatingEllipseShape()) {
mpEllipseShapeAnnotation = new EllipseAnnotation("", this);
mpModelWidget->getUndoStack()->push(new AddShapeCommand(mpEllipseShapeAnnotation));
setIsCreatingEllipseShape(true);
mpEllipseShapeAnnotation->replaceExtent(0, point);
mpEllipseShapeAnnotation->replaceExtent(1, point);
} else { // if we are already creating an ellipse then finish creating it.
// finish creating the ellipse
setIsCreatingEllipseShape(false);
MainWindow *pMainWindow = MainWindow::instance();
// set the transformation matrix
mpEllipseShapeAnnotation->setOrigin(mpEllipseShapeAnnotation->sceneBoundingRect().center());
mpEllipseShapeAnnotation->adjustExtentsWithOrigin();
mpEllipseShapeAnnotation->initializeTransformation();
// draw corner items for the ellipse shape
mpEllipseShapeAnnotation->drawCornerItems();
mpEllipseShapeAnnotation->setSelected(true);
// make the toolbar button of ellipse unchecked
pMainWindow->getEllipseShapeAction()->setChecked(false);
pMainWindow->getConnectModeAction()->setChecked(true);
mpModelWidget->getLibraryTreeItem()->emitShapeAdded(mpEllipseShapeAnnotation, this);
mpModelWidget->updateClassAnnotationIfNeeded();
mpModelWidget->updateModelText();
}
}
void GraphicsView::createTextShape(QPointF point)
{
if (mpModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
return;
}
if (!isCreatingTextShape()) {
mpTextShapeAnnotation = new TextAnnotation("", this);
mpModelWidget->getUndoStack()->push(new AddShapeCommand(mpTextShapeAnnotation));
setIsCreatingTextShape(true);
mpTextShapeAnnotation->setTextString("text");
mpTextShapeAnnotation->replaceExtent(0, point);
mpTextShapeAnnotation->replaceExtent(1, point);
} else { // if we are already creating a text then finish creating it.
// finish creating the text
setIsCreatingTextShape(false);
MainWindow *pMainWindow = MainWindow::instance();
// set the transformation matrix
mpTextShapeAnnotation->setOrigin(mpTextShapeAnnotation->sceneBoundingRect().center());
mpTextShapeAnnotation->adjustExtentsWithOrigin();
mpTextShapeAnnotation->initializeTransformation();
// draw corner items for the text shape
mpTextShapeAnnotation->drawCornerItems();
// make the toolbar button of text unchecked
pMainWindow->getTextShapeAction()->setChecked(false);
pMainWindow->getConnectModeAction()->setChecked(true);
mpModelWidget->getLibraryTreeItem()->emitShapeAdded(mpTextShapeAnnotation, this);
mpTextShapeAnnotation->showShapeProperties();
mpModelWidget->updateClassAnnotationIfNeeded();
mpModelWidget->updateModelText();
mpTextShapeAnnotation->setSelected(true);
}
}
void GraphicsView::createBitmapShape(QPointF point)
{
if (mpModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
return;
}
if (!isCreatingBitmapShape()) {
mpBitmapShapeAnnotation = new BitmapAnnotation(mpModelWidget->getLibraryTreeItem()->getFileName(), "", this);