/
LibraryTreeWidget.cpp
3287 lines (3147 loc) · 128 KB
/
LibraryTreeWidget.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-2014, 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>
*
* RCS: $Id$
*
*/
#include "LibraryTreeWidget.h"
#include "VariablesWidget.h"
#include "SimulationOutputWidget.h"
ItemDelegate::ItemDelegate(QObject *pParent, bool drawRichText, bool drawGrid)
: QItemDelegate(pParent)
{
mDrawRichText = drawRichText;
mLastTextPos = QPoint(0, 0);
mDrawGrid = drawGrid;
mpParent = pParent;
}
QString ItemDelegate::formatDisplayText(QVariant variant) const
{
QString text;
if (variant.type() == QVariant::Double) {
text = QLocale().toString(variant.toDouble());
} else {
text = variant.toString();
/* if rich text flag is set */
if (mDrawRichText) {
text.replace("\n", "<br />");
text.replace("\t", "	");
}
}
return text;
}
void ItemDelegate::initTextDocument(QTextDocument *pTextDocument, QFont font, int width) const
{
QTextOption textOption = pTextDocument->defaultTextOption();
textOption.setWrapMode(QTextOption::WordWrap);
pTextDocument->setDefaultTextOption(textOption);
pTextDocument->setDefaultFont(font);
pTextDocument->setTextWidth(width);
}
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV2 opt = setOptions(index, option);
const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option);
opt.features = v2 ? v2->features : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
// prepare
painter->save();
// get the data and the rectangles
QVariant value;
QIcon icon;
QIcon::Mode iconMode = QIcon::Normal;
QIcon::State iconState = QIcon::On;
QPixmap pixmap;
QRect decorationRect;
value = index.data(Qt::DecorationRole);
if (value.isValid()) {
if (value.type() == QVariant::Icon) {
icon = qvariant_cast<QIcon>(value);
decorationRect = QRect(QPoint(0, 0), icon.actualSize(option.decorationSize, iconMode, iconState));
} else {
pixmap = decoration(opt, value);
decorationRect = QRect(QPoint(0, 0), option.decorationSize).intersected(pixmap.rect());
}
}
QString text;
QRect displayRect;
value = index.data(Qt::DisplayRole);
if (value.isValid()) {
text = formatDisplayText(value);
displayRect = textRectangle(painter, option.rect, opt.font, text);
}
QRect checkRect;
Qt::CheckState checkState = Qt::Unchecked;
value = index.data(Qt::CheckStateRole);
if (value.isValid()) {
checkState = static_cast<Qt::CheckState>(value.toInt());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
checkRect = doCheck(opt, opt.rect, value);
#else /* Qt4 */
checkRect = check(opt, opt.rect, value);
#endif
}
// do the layout
doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
/* We check if item belongs to QTreeView and QTreeView model is LibraryTreeProxyModel.
* If LibraryTreeItem is unsaved then draw its background as Qt::darkRed.
*/
if (parent() && qobject_cast<QTreeView*>(parent())) {
QTreeView *pTreeView = qobject_cast<QTreeView*>(parent());
LibraryTreeProxyModel *pLibraryTreeProxyModel = qobject_cast<LibraryTreeProxyModel*>(pTreeView->model());
if (pLibraryTreeProxyModel) {
QModelIndex sourceIndex = pLibraryTreeProxyModel->mapToSource(index);
LibraryTreeItem *pLibraryTreeItem = static_cast<LibraryTreeItem*>(sourceIndex.internalPointer());
if (!pLibraryTreeItem->isSaved()) {
opt.palette.setBrush(QPalette::Highlight, Qt::darkRed);
}
}
}
// draw background
drawBackground(painter, opt, index);
// hover
/* Ticket #2245. Do not draw hover effect for items. Doesn't seem to work on few versions of Linux. */
/*drawHover(painter, opt, index);*/
drawCheck(painter, opt, checkRect, checkState);
/* if draw grid flag is set */
if (mDrawGrid) {
QPen pen;
if (!mGridColor.isValid()) {
int gridHint = qApp->style()->styleHint(QStyle::SH_Table_GridLineColor, &option);
const QColor gridColor = static_cast<QRgb>(gridHint);
pen.setColor(gridColor);
} else {
pen.setColor(mGridColor);
}
painter->save();
painter->setPen(pen);
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->restore();
}
/* if rich text flag is set */
if (mDrawRichText) {
QAbstractTextDocumentLayout::PaintContext ctx;
QTextDocument textDocument;
initTextDocument(&textDocument, opt.font, option.rect.width());
QVariant variant = index.data(Qt::ForegroundRole);
if (variant.isValid()) {
if (option.state & ~QStyle::State_Selected) {
ctx.palette.setColor(QPalette::Text, variant.value<QColor>());
}
}
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) {
cg = QPalette::Inactive;
}
if (option.state & QStyle::State_Selected) {
ctx.palette.setColor(QPalette::Text, option.palette.color(cg, QPalette::HighlightedText));
}
textDocument.setHtml(text);
painter->save();
painter->translate(displayRect.left(), displayRect.top());
QRect clip(0, 0, displayRect.width(), displayRect.height());
painter->setClipRect(clip);
textDocument.documentLayout()->draw(painter, ctx);
painter->restore();
} else {
drawDisplay(painter, opt, displayRect, text);
}
if (!icon.isNull()) {
// adjust the decorationRect for multiline items. So that icon remains at top.
if (mDrawRichText) {
decorationRect = QRect(decorationRect.left(), displayRect.top() + 4, decorationRect.width(), decorationRect.height());
}
icon.paint(painter, decorationRect, option.decorationAlignment, QIcon::Normal, QIcon::Off);
} else {
drawDecoration(painter, opt, decorationRect, pixmap);
}
if (parent() && qobject_cast<VariablesTreeView*>(parent())) {
if ((index.column() == 1) && (index.flags() & Qt::ItemIsEditable)) {
/* The display rect is slightly larger than the area because it include the outer line. */
painter->drawRect(displayRect.adjusted(0, 0, -1, -1));
}
}
painter->restore();
}
void ItemDelegate::drawHover(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (option.state & QStyle::State_MouseOver)
{
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
cg = QPalette::Inactive;
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
}
}
//! Reimplementation of sizeHint function. Defines the minimum height.
QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// if user has set the SizeHintRole then just use it.
QVariant value = index.data(Qt::SizeHintRole);
if (value.isValid())
return qvariant_cast<QSize>(value);
QSize size = QItemDelegate::sizeHint(option, index);
/* Only calculate the height of the item based on the text for SimulationOutputTree items. Fix for multi line messages. */
if (mDrawRichText && parent() && qobject_cast<SimulationOutputTree*>(parent())) {
SimulationOutputTree *pSimulationOutputTree = qobject_cast<SimulationOutputTree*>(parent());
int width = 0;
if (pSimulationOutputTree) {
width = pSimulationOutputTree->columnWidth(index.column()) - (pSimulationOutputTree->indentation() * pSimulationOutputTree->getDepth(index));
}
QVariant value = index.data(Qt::DisplayRole);
QString text;
if (value.isValid()) {
text = formatDisplayText(value);
}
QTextDocument textDocument;
initTextDocument(&textDocument, option.font, width); /* we can't use option.rect.width() here since it will be empty. */
textDocument.setHtml(text);
size.rheight() = qMax(textDocument.size().height(), (qreal)24.0);
}
return size;
}
/*!
Shows a Qt::PointingHandCursor for simulation output links.\n
If the link is clicked then calls the SimulationOutputWidget::openTransformationBrowser(QUrl).
*/
bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if (mDrawRichText && parent() && qobject_cast<SimulationOutputTree*>(parent()) &&
(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) && (option.state & QStyle::State_Enabled)) {
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(event);
QPointF position = pMouseEvent->pos() - option.rect.topLeft();
QVariant variant = index.data(Qt::DisplayRole);
QString text;
if (variant.isValid()) {
text = formatDisplayText(variant);
}
QTextDocument textDocument;
initTextDocument(&textDocument, option.font, option.rect.width());
textDocument.setHtml(text);
QString anchor = textDocument.documentLayout()->anchorAt(position);
SimulationOutputTree *pSimulationOutputTree = qobject_cast<SimulationOutputTree*>(parent());
if (anchor.isEmpty()) {
pSimulationOutputTree->unsetCursor();
return true;
} else {
pSimulationOutputTree->setCursor(Qt::PointingHandCursor);
if (event->type() == QEvent::MouseButtonRelease) {
pSimulationOutputTree->getSimulationOutputWidget()->openTransformationBrowser(QUrl(anchor));
}
return true;
}
} else {
return QItemDelegate::editorEvent(event, model, option, index);
}
}
/*!
* \class LibraryTreeItem
* \brief Contains the information about the Modelica class.
*/
/*!
* \brief LibraryTreeItem::LibraryTreeItem
* Used for creating the root item.
*/
LibraryTreeItem::LibraryTreeItem()
{
mIsRootItem = true;
mpParentLibraryTreeItem = 0;
setLibraryType(LibraryTreeItem::Modelica);
setSystemLibrary(false);
setModelWidget(0);
setName("");
setNameStructure("");
OMCInterface::getClassInformation_res classInformation;
setClassInformation(classInformation);
setFileName("");
setReadOnly(false);
setIsSaved(false);
setIsProtected(false);
setIsDocumentationClass(false);
setSaveContentsType(LibraryTreeItem::SaveInOneFile);
setToolTip("");
setIcon(QIcon());
setPixmap(QPixmap());
setDragPixmap(QPixmap());
setClassText("");
setExpanded(false);
setNonExisting(true);
}
/*!
* \brief LibraryTreeItem::LibraryTreeItem
* \param type
* \param text
* \param nameStructure
* \param classInformation
* \param fileName
* \param isSaved
* \param pParent
*/
LibraryTreeItem::LibraryTreeItem(LibraryType type, QString text, QString nameStructure, OMCInterface::getClassInformation_res classInformation,
QString fileName, bool isSaved, LibraryTreeItem *pParent)
: mLibraryType(type), mSystemLibrary(false), mpModelWidget(0)
{
mIsRootItem = false;
mpParentLibraryTreeItem = pParent;
setPixmap(QPixmap());
setDragPixmap(QPixmap());
setName(text);
setNameStructure(nameStructure);
if (type == LibraryTreeItem::Modelica) {
setClassInformation(classInformation);
} else {
setFileName(fileName);
setReadOnly(!StringHandler::isFileWritAble(fileName));
}
setIsSaved(isSaved);
setIsProtected(false);
setIsDocumentationClass(false);
if (isFilePathValid()) {
QFileInfo fileInfo(getFileName());
// if item has file name as package.mo then its save folder structure
if (fileInfo.fileName().compare("package.mo") == 0) {
setSaveContentsType(LibraryTreeItem::SaveFolderStructure);
} else {
setSaveContentsType(LibraryTreeItem::SaveInOneFile);
}
}
setClassText("");
setExpanded(false);
setNonExisting(false);
updateAttributes();
}
/*!
* \brief LibraryTreeItem::~LibraryTreeItem
* Destructor for LibraryTreeItem
*/
LibraryTreeItem::~LibraryTreeItem()
{
qDeleteAll(mChildren);
mChildren.clear();
}
/*!
* \brief LibraryTreeItem::setClassInformation
* Sets the OMCInterface::getClassInformation_res
* \param classInformation
*/
void LibraryTreeItem::setClassInformation(OMCInterface::getClassInformation_res classInformation)
{
if (mLibraryType == LibraryTreeItem::Modelica) {
mClassInformation = classInformation;
if (!isFilePathValid()) {
setFileName(classInformation.fileName);
}
setReadOnly(classInformation.fileReadOnly);
}
}
/*!
* \brief LibraryTreeItem::isFilePathValid
* Returns true if file path is valid file location and not modelica class name.
* \return
*/
bool LibraryTreeItem::isFilePathValid() {
// Since now we set the fileName via loadString() & parseString() so might get filename as className/<interactive>.
return QFile::exists(mFileName);
}
/*!
* \brief LibraryTreeItem::updateAttributes
* Updates the LibraryTreeItem icon, text and tooltip.
*/
void LibraryTreeItem::updateAttributes() {
setIcon(getLibraryTreeItemIcon());
QString tooltip;
if (mLibraryType == LibraryTreeItem::Modelica) {
tooltip = QString("%1: %2<br />%3: %4<br />%5: %6<br />%7: %8<br />%9: %10")
.arg(Helper::type).arg(mClassInformation.restriction)
.arg(Helper::name).arg(mName)
.arg(Helper::description).arg(mClassInformation.comment)
.arg(Helper::fileLocation).arg(mFileName)
.arg(QObject::tr("Path")).arg(mNameStructure);
} else {
tooltip = QString("%1: %2<br />%3: %4")
.arg(Helper::name).arg(mName)
.arg(Helper::fileLocation).arg(mFileName);
}
setToolTip(tooltip);
}
/*!
* \brief LibraryTreeItem::getLibraryTreeItemIcon
* \return QIcon - the LibraryTreeItem icon
*/
QIcon LibraryTreeItem::getLibraryTreeItemIcon()
{
if (mLibraryType == LibraryTreeItem::Text) {
return QIcon(":/Resources/icons/txt.svg");
} else if (mLibraryType == LibraryTreeItem::TLM) {
return QIcon(":/Resources/icons/tlm-icon.svg");
} else {
switch (getRestriction()) {
case StringHandler::Model:
return QIcon(":/Resources/icons/model-icon.svg");
case StringHandler::Class:
return QIcon(":/Resources/icons/class-icon.svg");
case StringHandler::Connector:
case StringHandler::ExpandableConnector:
return QIcon(":/Resources/icons/connect-mode.svg");
case StringHandler::Record:
return QIcon(":/Resources/icons/record-icon.svg");
case StringHandler::Block:
return QIcon(":/Resources/icons/block-icon.svg");
case StringHandler::Function:
return QIcon(":/Resources/icons/function-icon.svg");
case StringHandler::Package:
return QIcon(":/Resources/icons/package-icon.svg");
case StringHandler::Type:
case StringHandler::Operator:
case StringHandler::OperatorRecord:
case StringHandler::OperatorFunction:
return QIcon(":/Resources/icons/type-icon.svg");
case StringHandler::Optimization:
return QIcon(":/Resources/icons/optimization-icon.svg");
default:
return QIcon(":/Resources/icons/type-icon.svg");
}
}
}
/*!
* \brief LibraryTreeItem::isInPackageOneFile
* Returns true if the LibraryTreeItem is nested and is set to be saved in parent's file.
* \return
*/
bool LibraryTreeItem::isInPackageOneFile()
{
if (!isTopLevel() && mpParentLibraryTreeItem && mpParentLibraryTreeItem->getFileName().compare(getFileName()) == 0) {
return true;
} else {
return false;
}
}
/*!
* \brief LibraryTreeItem::insertChild
* Inserts a child LibraryTreeItem at the given position.
* \param position
* \param pLibraryTreeItem
*/
void LibraryTreeItem::insertChild(int position, LibraryTreeItem *pLibraryTreeItem)
{
mChildren.insert(position, pLibraryTreeItem);
}
/*!
* \brief LibraryTreeItem::child
* Returns the child LibraryTreeItem stored at given row.
* \param row
* \return
*/
LibraryTreeItem* LibraryTreeItem::child(int row)
{
return mChildren.value(row);
}
/*!
* \brief LibraryTreeItem::moveChild
* Moves the item from to to index in the list.
* \param from
* \param to
*/
void LibraryTreeItem::moveChild(int from, int to)
{
mChildren.move(from, to);
}
/*!
* \brief LibraryTreeItem::addInheritedClass
* Adds the inherited class and connects to its signals for notifications.
* \param pLibraryTreeItem
*/
void LibraryTreeItem::addInheritedClass(LibraryTreeItem *pLibraryTreeItem)
{
mInheritedClasses.append(pLibraryTreeItem);
connect(pLibraryTreeItem, SIGNAL(loaded(LibraryTreeItem*)), this, SLOT(handleLoaded(LibraryTreeItem*)), Qt::UniqueConnection);
connect(pLibraryTreeItem, SIGNAL(unLoaded(LibraryTreeItem*)), this, SLOT(handleUnloaded(LibraryTreeItem*)), Qt::UniqueConnection);
connect(pLibraryTreeItem, SIGNAL(shapeAdded(LibraryTreeItem*,ShapeAnnotation*,GraphicsView*)),
this, SLOT(handleShapeAdded(LibraryTreeItem*,ShapeAnnotation*,GraphicsView*)), Qt::UniqueConnection);
connect(pLibraryTreeItem, SIGNAL(componentAdded(LibraryTreeItem*,Component*)),
this, SLOT(handleComponentAdded(LibraryTreeItem*,Component*)), Qt::UniqueConnection);
connect(pLibraryTreeItem, SIGNAL(connectionAdded(LibraryTreeItem*,LineAnnotation*)),
this, SLOT(handleConnectionAdded(LibraryTreeItem*,LineAnnotation*)), Qt::UniqueConnection);
connect(pLibraryTreeItem, SIGNAL(iconUpdated()), this, SLOT(handleIconUpdated()), Qt::UniqueConnection);
}
/*!
* \brief LibraryTreeItem::removeAllInheritedClasses
* Removes the inherited classes and its signals.
*/
void LibraryTreeItem::removeInheritedClasses()
{
foreach (LibraryTreeItem *pLibraryTreeItem, mInheritedClasses) {
disconnect(pLibraryTreeItem, SIGNAL(loaded(LibraryTreeItem*)), this, SLOT(handleLoaded(LibraryTreeItem*)));
disconnect(pLibraryTreeItem, SIGNAL(unLoaded(LibraryTreeItem*)), this, SLOT(handleUnloaded(LibraryTreeItem*)));
disconnect(pLibraryTreeItem, SIGNAL(shapeAdded(LibraryTreeItem*,ShapeAnnotation*,GraphicsView*)),
this, SLOT(handleShapeAdded(LibraryTreeItem*,ShapeAnnotation*,GraphicsView*)));
disconnect(pLibraryTreeItem, SIGNAL(componentAdded(LibraryTreeItem*,Component*)),
this, SLOT(handleComponentAdded(LibraryTreeItem*,Component*)));
disconnect(pLibraryTreeItem, SIGNAL(connectionAdded(LibraryTreeItem*,LineAnnotation*)),
this, SLOT(handleConnectionAdded(LibraryTreeItem*,LineAnnotation*)));
disconnect(pLibraryTreeItem, SIGNAL(iconUpdated()), this, SLOT(handleIconUpdated()));
}
mInheritedClasses.clear();
}
/*!
* \brief LibraryTreeItem::removeChild
* Removes the child LibraryTreeItem.
* \param pLibraryTreeItem
*/
void LibraryTreeItem::removeChild(LibraryTreeItem *pLibraryTreeItem)
{
mChildren.removeOne(pLibraryTreeItem);
}
/*!
* \brief LibraryTreeItem::data
* Returns the data stored under the given role for the item referred to by the column.
* \param column
* \param role
* \return
*/
QVariant LibraryTreeItem::data(int column, int role) const
{
switch (column) {
case 0:
switch (role) {
case Qt::DisplayRole:
return mName;
case Qt::DecorationRole:
return mPixmap.isNull() ? mIcon : mPixmap;
case Qt::ToolTipRole:
return mToolTip;
case Qt::ForegroundRole:
return mIsSaved ? QVariant() : Qt::darkRed;
default:
return QVariant();
}
default:
return QVariant();
}
}
/*!
* \brief LibraryTreeItem::row
* Returns the row number corresponding to LibraryTreeItem.
* \return
*/
int LibraryTreeItem::row() const
{
if (mpParentLibraryTreeItem) {
return mpParentLibraryTreeItem->mChildren.indexOf(const_cast<LibraryTreeItem*>(this));
}
return 0;
}
/*!
* \brief LibraryTreeItem::isTopLevel
* Checks whether the LibraryTreeItem is top level or not.
* \return
*/
bool LibraryTreeItem::isTopLevel()
{
if (parent()->isRootItem()) {
return true;
} else {
return false;
}
}
/*!
* \brief LibraryTreeItem::isSimulationAllowed
* Checks whether simulation is allowed for this item or not.
* \return
*/
bool LibraryTreeItem::isSimulationAllowed()
{
// if the class is partial then return false.
if (isPartial()) {
return false;
}
switch (getRestriction()) {
case StringHandler::Model:
case StringHandler::Class:
case StringHandler::Block:
return true;
default:
return false;
}
}
/*!
* \brief LibraryTreeItem::handleLoaded
* Handles the case when an undefined inherited class is loaded.
* \param pLibraryTreeItem
*/
void LibraryTreeItem::handleLoaded(LibraryTreeItem *pLibraryTreeItem)
{
if (mpModelWidget) {
MainWindow *pMainWindow = mpModelWidget->getModelWidgetContainer()->getMainWindow();
// if the base class need to be loaded then load it first.
if (!pLibraryTreeItem->getModelWidget()) {
pMainWindow->getLibraryWidget()->getLibraryTreeModel()->showModelWidget(pLibraryTreeItem, "", false);
}
ModelWidget::InheritedClass *pInheritedClass = mpModelWidget->findInheritedClass(pLibraryTreeItem);
if (pInheritedClass) {
mpModelWidget->modelInheritedClassLoaded(pInheritedClass);
// load new icon for the class.
pMainWindow->getLibraryWidget()->getLibraryTreeModel()->loadLibraryTreeItemPixmap(this);
// update the icon in the libraries browser view.
pMainWindow->getLibraryWidget()->getLibraryTreeModel()->updateLibraryTreeItem(this);
}
}
}
/*!
* \brief LibraryTreeItem::handleUnLoaded
* Handles the case when a inherited class is unloaded.
* \param pLibraryTreeItem
*/
void LibraryTreeItem::handleUnloaded(LibraryTreeItem *pLibraryTreeItem)
{
if (mpModelWidget) {
ModelWidget::InheritedClass *pInheritedClass = mpModelWidget->findInheritedClass(pLibraryTreeItem);
if (pInheritedClass) {
mpModelWidget->modelInheritedClassUnLoaded(pInheritedClass);
MainWindow *pMainWindow = mpModelWidget->getModelWidgetContainer()->getMainWindow();
// load new icon for the class.
pMainWindow->getLibraryWidget()->getLibraryTreeModel()->loadLibraryTreeItemPixmap(this);
// update the icon in the libraries browser view.
pMainWindow->getLibraryWidget()->getLibraryTreeModel()->updateLibraryTreeItem(this);
}
}
}
/*!
* \brief LibraryTreeItem::handleShapeAdded
* Handles a case when inherited class has created a new shape.
* \param pLibraryTreeItem
* \param pShapeAnnotation
* \param pGraphicsView
*/
void LibraryTreeItem::handleShapeAdded(LibraryTreeItem *pLibraryTreeItem, ShapeAnnotation *pShapeAnnotation, GraphicsView *pGraphicsView)
{
if (mpModelWidget) {
ModelWidget::InheritedClass *pInheritedClass = mpModelWidget->findInheritedClass(pLibraryTreeItem);
if (pInheritedClass) {
if (pGraphicsView->getViewType() == StringHandler::Icon) {
pInheritedClass->mIconShapesList.append(mpModelWidget->createInheritedShape(pShapeAnnotation, mpModelWidget->getIconGraphicsView()));
mpModelWidget->getIconGraphicsView()->reOrderItems();
} else {
pInheritedClass->mDiagramShapesList.append(mpModelWidget->createInheritedShape(pShapeAnnotation, mpModelWidget->getDiagramGraphicsView()));
mpModelWidget->getDiagramGraphicsView()->reOrderItems();
}
}
}
}
void LibraryTreeItem::handleComponentAdded(LibraryTreeItem *pLibraryTreeItem, Component *pComponent)
{
if (mpModelWidget) {
ModelWidget::InheritedClass *pInheritedClass = mpModelWidget->findInheritedClass(pLibraryTreeItem);
if (pInheritedClass) {
if (pComponent->getLibraryTreeItem() && pComponent->getLibraryTreeItem()->isConnector()) {
pInheritedClass->mIconComponentsList.append(mpModelWidget->createInheritedComponent(pComponent, mpModelWidget->getIconGraphicsView()));
}
pInheritedClass->mDiagramComponentsList.append(mpModelWidget->createInheritedComponent(pComponent, mpModelWidget->getDiagramGraphicsView()));
}
}
}
void LibraryTreeItem::handleConnectionAdded(LibraryTreeItem *pLibraryTreeItem, LineAnnotation *pConnectionLineAnnotation)
{
if (mpModelWidget) {
ModelWidget::InheritedClass *pInheritedClass = mpModelWidget->findInheritedClass(pLibraryTreeItem);
if (pInheritedClass) {
pInheritedClass->mConnectionsList.append(mpModelWidget->createInheritedConnection(pConnectionLineAnnotation, pInheritedClass->mpLibraryTreeItem));
}
}
}
/*!
* \brief LibraryTreeItem::handleIconUpdated
* Handles a case when class icon update is required.
*/
void LibraryTreeItem::handleIconUpdated()
{
if (mpModelWidget) {
MainWindow *pMainWindow = mpModelWidget->getModelWidgetContainer()->getMainWindow();
// load new icon for the class.
pMainWindow->getLibraryWidget()->getLibraryTreeModel()->loadLibraryTreeItemPixmap(this);
// update the icon in the libraries browser view.
pMainWindow->getLibraryWidget()->getLibraryTreeModel()->updateLibraryTreeItem(this);
emit iconUpdated();
}
}
/*!
* \class LibraryTreeProxyModel
* \brief A sort filter proxy model for Libraries Browser.
*/
/*!
* \brief LibraryTreeProxyModel::LibraryTreeProxyModel
* \param pLibraryWidget
*/
LibraryTreeProxyModel::LibraryTreeProxyModel(LibraryWidget *pLibraryWidget)
: QSortFilterProxyModel(pLibraryWidget)
{
mpLibraryWidget = pLibraryWidget;
}
/*!
* \brief LibraryTreeProxyModel::filterAcceptsRow
* Filters the LibraryTreeItems based on the filter reguler expression.
* Also checks if LibraryTreeItem is protected and show/hide it based on Show Protected Classes settings value.
* \param sourceRow
* \param sourceParent
* \return
*/
bool LibraryTreeProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
if (!filterRegExp().isEmpty()) {
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
if (index.isValid()) {
// if any of children matches the filter, then current index matches the filter as well
int rows = sourceModel()->rowCount(index);
for (int i = 0 ; i < rows ; ++i) {
if (filterAcceptsRow(i, index)) {
return true;
}
}
// check current index itself
LibraryTreeItem *pLibraryTreeItem = static_cast<LibraryTreeItem*>(index.internalPointer());
if (pLibraryTreeItem) {
if (pLibraryTreeItem->isProtected() && !mpLibraryWidget->getMainWindow()->getOptionsDialog()->getGeneralSettingsPage()->getShowProtectedClasses()) {
return false;
} else {
return pLibraryTreeItem->getNameStructure().contains(filterRegExp());
}
} else {
return sourceModel()->data(index).toString().contains(filterRegExp());
}
QString key = sourceModel()->data(index, filterRole()).toString();
return key.contains(filterRegExp());
} else {
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
} else {
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
if (index.isValid()) {
LibraryTreeItem *pLibraryTreeItem = static_cast<LibraryTreeItem*>(index.internalPointer());
if (pLibraryTreeItem) {
if (pLibraryTreeItem->isProtected() && !mpLibraryWidget->getMainWindow()->getOptionsDialog()->getGeneralSettingsPage()->getShowProtectedClasses()) {
return false;
} else {
return pLibraryTreeItem->getNameStructure().contains(filterRegExp());
}
} else {
return sourceModel()->data(index).toString().contains(filterRegExp());
}
} else {
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
}
}
/*!
* \class LibraryTreeModel
* \brief A model for Libraries Browser.
*/
/*!
* \brief LibraryTreeModel::LibraryTreeModel
* \param pLibraryWidget
*/
LibraryTreeModel::LibraryTreeModel(LibraryWidget *pLibraryWidget)
: QAbstractItemModel(pLibraryWidget)
{
mpLibraryWidget = pLibraryWidget;
mpRootLibraryTreeItem = new LibraryTreeItem;
}
/*!
* \brief LibraryTreeModel::columnCount
* Returns the number of columns for the children of the given parent.
* \param parent
* \return
*/
int LibraryTreeModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
/*!
* \brief LibraryTreeModel::rowCount
* Returns the number of rows under the given parent.
* When the parent is valid it means that rowCount is returning the number of children of parent.
* \param parent
* \return
*/
int LibraryTreeModel::rowCount(const QModelIndex &parent) const
{
LibraryTreeItem *pParentLibraryTreeItem;
if (parent.column() > 0) {
return 0;
}
if (!parent.isValid()) {
pParentLibraryTreeItem = mpRootLibraryTreeItem;
} else {
pParentLibraryTreeItem = static_cast<LibraryTreeItem*>(parent.internalPointer());
}
return pParentLibraryTreeItem->getChildren().size();
}
/*!
* \brief LibraryTreeModel::headerData
* Returns the data for the given role and section in the header with the specified orientation.
* \param section
* \param orientation
* \param role
* \return
*/
QVariant LibraryTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(section);
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return Helper::libraries;
}
return QVariant();
}
/*!
* \brief LibraryTreeModel::index
* Returns the index of the item in the model specified by the given row, column and parent index.
* \param row
* \param column
* \param parent
* \return
*/
QModelIndex LibraryTreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
LibraryTreeItem *pParentLibraryTreeItem;
if (!parent.isValid()) {
pParentLibraryTreeItem = mpRootLibraryTreeItem;
} else {
pParentLibraryTreeItem = static_cast<LibraryTreeItem*>(parent.internalPointer());
}
LibraryTreeItem *pChildLibraryTreeItem = pParentLibraryTreeItem->child(row);
if (pChildLibraryTreeItem) {
return createIndex(row, column, pChildLibraryTreeItem);
} else {
return QModelIndex();
}
}
/*!
* \brief LibraryTreeModel::parent
* Finds the parent for QModelIndex
* \param index
* \return
*/
QModelIndex LibraryTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
LibraryTreeItem *pChildLibraryTreeItem = static_cast<LibraryTreeItem*>(index.internalPointer());
LibraryTreeItem *pParentLibraryTreeItem = pChildLibraryTreeItem->parent();
if (pParentLibraryTreeItem == mpRootLibraryTreeItem)
return QModelIndex();
return createIndex(pParentLibraryTreeItem->row(), 0, pParentLibraryTreeItem);
}
/*!
* \brief LibraryTreeModel::data
* Returns the LibraryTreeItem data.
* \param index
* \param role
* \return
*/
QVariant LibraryTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
LibraryTreeItem *pLibraryTreeItem = static_cast<LibraryTreeItem*>(index.internalPointer());
return pLibraryTreeItem->data(index.column(), role);
}
/*!
* \brief LibraryTreeModel::flags
* Returns the LibraryTreeItem flags.
* \param index
* \return
*/
Qt::ItemFlags LibraryTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) {
return Qt::ItemIsEnabled;
} else {
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
}
}
/*!
* \brief LibraryTreeModel::findLibraryTreeItem
* Finds the LibraryTreeItem based on the name and case sensitivity.
* \param name
* \param root
* \return
*/
LibraryTreeItem* LibraryTreeModel::findLibraryTreeItem(const QString &name, LibraryTreeItem *root, Qt::CaseSensitivity caseSensitivity) const
{
if (!root) {
root = mpRootLibraryTreeItem;
}
if (root->getNameStructure().compare(name, caseSensitivity) == 0) {
return root;
}
for (int i = root->getChildren().size(); --i >= 0; ) {
if (LibraryTreeItem *item = findLibraryTreeItem(name, root->getChildren().at(i))) {
return item;
}
}
return 0;
}
/*!
* \brief LibraryTreeModel::findLibraryTreeItem
* Finds the LibraryTreeItem based on the Regular Expression.
* \param regExp
* \param root
* \return
*/
LibraryTreeItem* LibraryTreeModel::findLibraryTreeItem(const QRegExp ®Exp, LibraryTreeItem *root) const
{
if (!root) {
root = mpRootLibraryTreeItem;
}
if (root->getNameStructure().contains(regExp)) {
return root;
}
for (int i = root->getChildren().size(); --i >= 0; ) {
if (LibraryTreeItem *item = findLibraryTreeItem(regExp, root->getChildren().at(i))) {
return item;
}
}
return 0;
}
/*!
* \brief LibraryTreeModel::findNonExistingLibraryTreeItem
* Finds the non existing LibraryTreeItem based on the name and case sensitivity.
* \param name