/
cellmlannotationviewcellmllistwidget.cpp
1082 lines (762 loc) · 43.8 KB
/
cellmlannotationviewcellmllistwidget.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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// CellML Annotation view CellML list widget
//==============================================================================
#include "cellmlannotationviewcellmllistwidget.h"
#include "cellmlannotationvieweditingwidget.h"
#include "corecliutils.h"
#include "coreguiutils.h"
#include "filemanager.h"
#include "i18ninterface.h"
#include "treeviewwidget.h"
//==============================================================================
#include <QAbstractItemView>
#include <QApplication>
#include <QDesktopServices>
#include <QFont>
#include <QLayout>
#include <QMenu>
#include <QPoint>
#include <QStandardItemModel>
#include <QStyle>
//==============================================================================
namespace OpenCOR {
namespace CellMLAnnotationView {
//==============================================================================
CellmlAnnotationViewCellmlElementItemDelegate::CellmlAnnotationViewCellmlElementItemDelegate(QObject *pParent) :
Core::StyledItemDelegate(pParent)
{
}
//==============================================================================
void CellmlAnnotationViewCellmlElementItemDelegate::paint(QPainter *pPainter,
const QStyleOptionViewItem &pOption,
const QModelIndex &pIndex) const
{
// Paint the item as normal, except for error/warning/category items
CellmlAnnotationViewCellmlElementItem *cellmlElementItem = static_cast<CellmlAnnotationViewCellmlElementItem *>(qobject_cast<const QStandardItemModel *>(pIndex.model())->itemFromIndex(pIndex));
QStyleOptionViewItem option(pOption);
initStyleOption(&option, pIndex);
if ( (cellmlElementItem->type() == CellmlAnnotationViewCellmlElementItem::Error)
|| (cellmlElementItem->type() == CellmlAnnotationViewCellmlElementItem::Warning)) {
// This is an error/warning item, so prevent it from being hoverable and
// make it look enabled since it's actually disabled (so that it can't
// be selected) and yet we want to see it as if it wasn't
option.state &= ~QStyle::State_MouseOver;
option.state |= QStyle::State_Enabled;
} else if (cellmlElementItem->isCategory()) {
// We are dealing with a category so show it bold
option.font.setBold(cellmlElementItem->isCategory());
}
QStyledItemDelegate::paint(pPainter, option, pIndex);
}
//==============================================================================
CellmlAnnotationViewCellmlElementItem::CellmlAnnotationViewCellmlElementItem(bool pCategory,
Type pType,
const QString &pText,
iface::cellml_api::CellMLElement *pElement,
int pNumber) :
QStandardItem(pText),
mCategory(pCategory),
mType(pType),
mElement(pElement),
mNumber(pNumber)
{
}
//==============================================================================
CellmlAnnotationViewCellmlElementItem::CellmlAnnotationViewCellmlElementItem(bool pError,
const QString &pText) :
CellmlAnnotationViewCellmlElementItem(false, pError?Error:Warning, pText, nullptr, -1)
{
// Disable the item and use its text as a tooltip (in case it's too long and
// doesn't fit within the allocated space we have)
// Note: the item will get 're-enabled' by our item delegate...
setEnabled(false);
setToolTip(pText);
// Set the icon for the item
setIcon(mType);
}
//==============================================================================
CellmlAnnotationViewCellmlElementItem::CellmlAnnotationViewCellmlElementItem(Type pType,
const QString &pText) :
CellmlAnnotationViewCellmlElementItem(true, pType, pText, nullptr, -1)
{
// Use its text as a tooltip (in case it's too long and doesn't fit within
// the allocated space we have)
setToolTip(pText);
// Set the icon for the item
setIcon(pType);
}
//==============================================================================
CellmlAnnotationViewCellmlElementItem::CellmlAnnotationViewCellmlElementItem(Type pType,
iface::cellml_api::CellMLElement *pElement,
int pNumber) :
CellmlAnnotationViewCellmlElementItem(false, pType, QString(), pElement, pNumber)
{
// Set the text for some types
enum {
RightArrow = 0x2192
};
switch (pType) {
case Import: {
ObjRef<iface::cellml_api::URI> xlinkHref = dynamic_cast<iface::cellml_api::CellMLImport *>(pElement)->xlinkHref();
setText(QString::fromStdWString(xlinkHref->asText()));
break;
}
case UnitElement:
setText(QString::fromStdWString(dynamic_cast<iface::cellml_api::Unit *>(pElement)->units()));
break;
case Group:
setText(tr("Group #%1").arg(pNumber));
break;
case RelationshipReference: {
QString name = QString::fromStdWString(dynamic_cast<iface::cellml_api::RelationshipRef *>(pElement)->name());
setText( QString::fromStdWString(dynamic_cast<iface::cellml_api::RelationshipRef *>(pElement)->relationship())
+(name.isEmpty()?QString():" "+name));
break;
}
case ComponentReference:
setText(QString::fromStdWString(dynamic_cast<iface::cellml_api::ComponentRef *>(pElement)->componentName()));
break;
case Connection:
setText(tr("Connection #%1").arg(pNumber));
break;
case ComponentMapping: {
ObjRef<iface::cellml_api::MapComponents> mapComponents = dynamic_cast<iface::cellml_api::MapComponents *>(pElement);
setText(QString("%1 %2 %3").arg(QString::fromStdWString(mapComponents->firstComponentName()))
.arg(QChar(RightArrow))
.arg(QString::fromStdWString(mapComponents->secondComponentName())));
break;
}
case VariableMapping: {
ObjRef<iface::cellml_api::MapVariables> mapVariables = dynamic_cast<iface::cellml_api::MapVariables *>(pElement);
setText(QString("%1 %2 %3").arg(QString::fromStdWString(mapVariables->firstVariableName()))
.arg(QChar(RightArrow))
.arg(QString::fromStdWString(mapVariables->secondVariableName())));
break;
}
default:
// Another type of element that has a name
setText(QString::fromStdWString(dynamic_cast<iface::cellml_api::NamedCellMLElement *>(pElement)->name()));
}
// Use its text as a tooltip (in case it's too long and doesn't fit within
// the allocated space we have)
setToolTip(text());
// Set the icon for the item
setIcon(pType);
}
//==============================================================================
void CellmlAnnotationViewCellmlElementItem::setIcon(Type pType)
{
// Set our icon based on our type
static const QIcon ErrorIcon = QIcon(":/oxygen/emblems/emblem-important.png");
static const QIcon WarningIcon = QIcon(":/oxygen/status/task-attention.png");
static const QIcon ModelIcon = QIcon(":/CellMLSupport/modelNode.png");
static const QIcon ImportIcon = QIcon(":/CellMLSupport/importNode.png");
static const QIcon UnitIcon = QIcon(":/CellMLSupport/unitNode.png");
static const QIcon UnitElementIcon = QIcon(":/CellMLSupport/unitElementNode.png");
static const QIcon ComponentIcon = QIcon(":/CellMLSupport/componentNode.png");
static const QIcon VariableIcon = QIcon(":/CellMLSupport/variableNode.png");
static const QIcon GroupIcon = QIcon(":/CellMLSupport/groupNode.png");
static const QIcon RelationshipIcon = QIcon(":/CellMLSupport/relationshipNode.png");
static const QIcon ConnectionIcon = QIcon(":/CellMLSupport/connectionNode.png");
switch (pType) {
case Error:
QStandardItem::setIcon(ErrorIcon);
break;
case Warning:
QStandardItem::setIcon(WarningIcon);
break;
case Model:
QStandardItem::setIcon(ModelIcon);
break;
case Import:
QStandardItem::setIcon(ImportIcon);
break;
case ImportUnit:
case Unit:
QStandardItem::setIcon(UnitIcon);
break;
case UnitElement:
QStandardItem::setIcon(UnitElementIcon);
break;
case ImportComponent:
case Component:
case ComponentMapping:
QStandardItem::setIcon(ComponentIcon);
break;
case Variable:
case VariableMapping:
QStandardItem::setIcon(VariableIcon);
break;
case Group:
QStandardItem::setIcon(GroupIcon);
break;
case RelationshipReference:
QStandardItem::setIcon(RelationshipIcon);
break;
case ComponentReference:
QStandardItem::setIcon(ComponentIcon);
break;
case Connection:
QStandardItem::setIcon(ConnectionIcon);
break;
}
}
//==============================================================================
bool CellmlAnnotationViewCellmlElementItem::isCategory() const
{
// Return whether we are a category
return mCategory;
}
//==============================================================================
int CellmlAnnotationViewCellmlElementItem::type() const
{
// Return our type
return mType;
}
//==============================================================================
int CellmlAnnotationViewCellmlElementItem::number() const
{
// Return our number
return mNumber;
}
//==============================================================================
iface::cellml_api::CellMLElement * CellmlAnnotationViewCellmlElementItem::element() const
{
// Return our element
return mElement;
}
//==============================================================================
CellmlAnnotationViewCellmlListWidget::CellmlAnnotationViewCellmlListWidget(CellmlAnnotationViewEditingWidget *pParent) :
Widget(pParent),
mCellmlFile(pParent->cellmlFile())
{
// Create and customise our tree view widget which will contain all of the
// imports, units, components, groups and connections from a CellML file
mTreeViewModel = new QStandardItemModel(this);
mTreeViewWidget = new Core::TreeViewWidget(this);
mTreeViewWidget->setModel(mTreeViewModel);
mTreeViewWidget->setItemDelegate(new CellmlAnnotationViewCellmlElementItemDelegate(this));
mTreeViewWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
mTreeViewWidget->setHeaderHidden(true);
mTreeViewWidget->setRootIsDecorated(false);
// Populate ourselves
createLayout()->addWidget(mTreeViewWidget);
// We want a context menu for our tree view widget
mTreeViewWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(mTreeViewWidget, &Core::TreeViewWidget::customContextMenuRequested,
this, &CellmlAnnotationViewCellmlListWidget::showCustomContextMenu);
// Create our actions
mExpandAllAction = Core::newAction(QIcon(":/oxygen/actions/list-add.png"),
this);
mCollapseAllAction = Core::newAction(QIcon(":/oxygen/actions/list-remove.png"),
this);
mRemoveCurrentMetadataAction = Core::newAction(this);
mRemoveAllMetadataAction = Core::newAction(this);
mOpenImportAction = Core::newAction(this);
connect(mExpandAllAction, &QAction::triggered,
this, &CellmlAnnotationViewCellmlListWidget::expandAll);
connect(mCollapseAllAction, &QAction::triggered,
this, &CellmlAnnotationViewCellmlListWidget::collapseAll);
connect(mRemoveCurrentMetadataAction, &QAction::triggered,
this, &CellmlAnnotationViewCellmlListWidget::removeCurrentMetadata);
connect(mRemoveAllMetadataAction, &QAction::triggered,
this, &CellmlAnnotationViewCellmlListWidget::removeAllMetadata);
connect(mOpenImportAction, &QAction::triggered,
this, &CellmlAnnotationViewCellmlListWidget::openImport);
// Some connections to handle the expansion/collapse of a CellML element
connect(mTreeViewWidget, &Core::TreeViewWidget::expanded,
this, &CellmlAnnotationViewCellmlListWidget::resizeTreeViewToContents);
connect(mTreeViewWidget, &Core::TreeViewWidget::collapsed,
this, &CellmlAnnotationViewCellmlListWidget::resizeTreeViewToContents);
// Some connections to handle the change of CellML element
connect(mTreeViewWidget->selectionModel(), &QItemSelectionModel::currentChanged,
this, &CellmlAnnotationViewCellmlListWidget::updateMetadataDetails);
// Initialise our tree view widget
// Note: we don't want to select first item of our tree view widget just yet
// since we need a connection between ourselves and the metadata
// details widget to be set first (see the constructor for
// CellmlAnnotationViewEditingWidget)...
initializeTreeViewWidget(false);
// Make our tree view widget our focus proxy
setFocusProxy(mTreeViewWidget);
// Some further initialisations that are done as part of retranslating the
// GUI (so that they can be updated when changing languages)
retranslateUi();
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::retranslateUi()
{
// Retranslate our actions
I18nInterface::retranslateAction(mExpandAllAction, tr("Expand All"),
tr("Expand all the children nodes"));
I18nInterface::retranslateAction(mCollapseAllAction, tr("Collapse All"),
tr("Collapse all the children nodes"));
I18nInterface::retranslateAction(mRemoveCurrentMetadataAction, tr("Remove Current Metadata"),
tr("Remove the metadata associated with the current node"));
I18nInterface::retranslateAction(mRemoveAllMetadataAction, tr("Remove All Metadata"),
tr("Remove all the metadata associated with the different nodes"));
I18nInterface::retranslateAction(mOpenImportAction, tr("Open Import"),
tr("Open the imported file"));
// Retranslate some of the CellML elements in our tree view widget
retranslateDataItem(static_cast<CellmlAnnotationViewCellmlElementItem *>(mTreeViewModel->invisibleRootItem()));
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::retranslateDataItem(CellmlAnnotationViewCellmlElementItem *pCellmlElementItem)
{
// Retranslate our CellML element's children
for (int i = 0, iMax = pCellmlElementItem->rowCount(); i < iMax; ++i)
retranslateDataItem(static_cast<CellmlAnnotationViewCellmlElementItem *>(pCellmlElementItem->child(i)));
// Retranslate our CellML element item, if it's not our model's invisible
// root item
if (pCellmlElementItem == mTreeViewModel->invisibleRootItem())
return;
else if (pCellmlElementItem->isCategory()) {
// We are dealing with a category, so retranslate its type
switch (pCellmlElementItem->type()) {
case CellmlAnnotationViewCellmlElementItem::Import:
pCellmlElementItem->setText(tr("Imports"));
break;
case CellmlAnnotationViewCellmlElementItem::Unit:
pCellmlElementItem->setText(tr("Units"));
break;
case CellmlAnnotationViewCellmlElementItem::Component:
pCellmlElementItem->setText(tr("Components"));
break;
case CellmlAnnotationViewCellmlElementItem::Variable:
pCellmlElementItem->setText(tr("Variables"));
break;
case CellmlAnnotationViewCellmlElementItem::Group:
pCellmlElementItem->setText(tr("Groups"));
break;
case CellmlAnnotationViewCellmlElementItem::RelationshipReference:
pCellmlElementItem->setText(tr("Relationship References"));
break;
case CellmlAnnotationViewCellmlElementItem::ComponentReference:
pCellmlElementItem->setText(tr("Component References"));
break;
case CellmlAnnotationViewCellmlElementItem::Connection:
pCellmlElementItem->setText(tr("Connections"));
break;
default:
// Not a type that we can retranslate, so do nothing...
;
}
} else {
// We are not dealing with a category, so check the type and see whether
// a CellML element needs retranslating
switch (pCellmlElementItem->type()) {
case CellmlAnnotationViewCellmlElementItem::Group:
pCellmlElementItem->setText(tr("Group #%1").arg(pCellmlElementItem->number()));
break;
case CellmlAnnotationViewCellmlElementItem::Connection:
pCellmlElementItem->setText(tr("Connection #%1").arg(pCellmlElementItem->number()));
break;
default:
// Not a sub-type that we can retranslate, so do nothing...
;
}
}
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::initializeTreeViewWidget(bool pSelectFirstItem)
{
// Populate our tree view widget
populateModel();
// Expand our tree view widget enough so that we can see the meaningful
// parts of the CellML file
mTreeViewWidget->expandToDepth(1);
// Resize our tree view widget, just to be on the safe side
resizeTreeViewToContents();
// Select the first item of our tree view widget
if (pSelectFirstItem)
mTreeViewWidget->selectFirstItem();
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::populateModel()
{
// Retrieve the model's root
iface::cellml_api::Model *cellmlModel = mCellmlFile->model();
CellmlAnnotationViewCellmlElementItem *modelItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Model,
cellmlModel);
mTreeViewModel->invisibleRootItem()->appendRow(modelItem);
// Retrieve the model's imports
ObjRef<iface::cellml_api::CellMLImportSet> imports = cellmlModel->imports();
if (imports->length()) {
// Imports category
CellmlAnnotationViewCellmlElementItem *importsItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Import,
tr("Imports"));
modelItem->appendRow(importsItem);
// Retrieve the model's imports themselves
ObjRef<iface::cellml_api::CellMLImportIterator> importsIter = imports->iterateImports();
for (ObjRef<iface::cellml_api::CellMLImport> import = importsIter->nextImport();
import; import = importsIter->nextImport()) {
CellmlAnnotationViewCellmlElementItem *importItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Import,
import);
importsItem->appendRow(importItem);
// Retrieve the model's import's units
ObjRef<iface::cellml_api::ImportUnitsSet> importUnitsSet = import->units();
if (importUnitsSet->length()) {
// Units category
CellmlAnnotationViewCellmlElementItem *unitsItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::ImportUnit,
tr("Units"));
importItem->appendRow(unitsItem);
// Retrieve the model's import's units themselves
ObjRef<iface::cellml_api::ImportUnitsIterator> importUnitsIter = importUnitsSet->iterateImportUnits();
for (ObjRef<iface::cellml_api::ImportUnits> importUnit = importUnitsIter->nextImportUnits();
importUnit; importUnit = importUnitsIter->nextImportUnits()) {
unitsItem->appendRow(new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::ImportUnit,
importUnit));
}
}
// Retrieve the model's import's components
ObjRef<iface::cellml_api::ImportComponentSet> importComponents = import->components();
if (importComponents->length()) {
// Components category
CellmlAnnotationViewCellmlElementItem *componentsItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::ImportComponent,
tr("Components"));
importItem->appendRow(componentsItem);
// Retrieve the model's import's components themselves
ObjRef<iface::cellml_api::ImportComponentIterator> importComponentsIter = importComponents->iterateImportComponents();
for (ObjRef<iface::cellml_api::ImportComponent> importComponent = importComponentsIter->nextImportComponent();
importComponent; importComponent = importComponentsIter->nextImportComponent()) {
componentsItem->appendRow(new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::ImportComponent,
importComponent));
}
}
}
}
// Retrieve the model's units
ObjRef<iface::cellml_api::UnitsSet> unitsSet = cellmlModel->localUnits();
populateUnitsModel(modelItem, unitsSet);
// Retrieve the model's components
ObjRef<iface::cellml_api::CellMLComponentSet> components = cellmlModel->localComponents();
if (components->length()) {
// Components category
CellmlAnnotationViewCellmlElementItem *componentsItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Component,
tr("Components"));
modelItem->appendRow(componentsItem);
// Retrieve the model's components themselves
ObjRef<iface::cellml_api::CellMLComponentIterator> componentsIter = components->iterateComponents();
for (ObjRef<iface::cellml_api::CellMLComponent> component = componentsIter->nextComponent();
component; component = componentsIter->nextComponent()) {
CellmlAnnotationViewCellmlElementItem *componentItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Component,
component);
componentsItem->appendRow(componentItem);
// Retrieve the model's component's units
ObjRef<iface::cellml_api::UnitsSet> componentUnitsSet = component->units();
populateUnitsModel(componentItem, componentUnitsSet);
// Retrieve the model's component's variables
ObjRef<iface::cellml_api::CellMLVariableSet> componentVariables = component->variables();
if (componentVariables->length()) {
// Variables category
CellmlAnnotationViewCellmlElementItem *variablesItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Variable,
tr("Variables"));
componentItem->appendRow(variablesItem);
// Retrieve the model's component's variables themselves
ObjRef<iface::cellml_api::CellMLVariableIterator> componentVariablesIter = componentVariables->iterateVariables();
for (ObjRef<iface::cellml_api::CellMLVariable> componentVariable = componentVariablesIter->nextVariable();
componentVariable; componentVariable = componentVariablesIter->nextVariable()) {
variablesItem->appendRow(new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Variable,
componentVariable));
}
}
}
}
// Retrieve the model's groups
ObjRef<iface::cellml_api::GroupSet> groups = cellmlModel->groups();
if (groups->length()) {
// Groups category
CellmlAnnotationViewCellmlElementItem *groupsItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Group,
tr("Groups"));
modelItem->appendRow(groupsItem);
// Retrieve the model's groups themselves
int counter = 0;
ObjRef<iface::cellml_api::GroupIterator> groupsIter = groups->iterateGroups();
for (ObjRef<iface::cellml_api::Group> group = groupsIter->nextGroup();
group; group = groupsIter->nextGroup()) {
CellmlAnnotationViewCellmlElementItem *groupItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Group,
group, ++counter);
groupsItem->appendRow(groupItem);
// Retrieve the model's group's relationship references
ObjRef<iface::cellml_api::RelationshipRefSet> groupRelationshipReferences = group->relationshipRefs();
if (groupRelationshipReferences->length()) {
// Relationship references category
CellmlAnnotationViewCellmlElementItem *groupRelationshipReferencesItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::RelationshipReference,
tr("Relationship References"));
groupItem->appendRow(groupRelationshipReferencesItem);
// Retrieve the model's group's relationship references
// themselves
ObjRef<iface::cellml_api::RelationshipRefIterator> groupRelationshipReferencesIter = groupRelationshipReferences->iterateRelationshipRefs();
for (ObjRef<iface::cellml_api::RelationshipRef> groupRelationshipReference = groupRelationshipReferencesIter->nextRelationshipRef();
groupRelationshipReference; groupRelationshipReference = groupRelationshipReferencesIter->nextRelationshipRef()) {
groupRelationshipReferencesItem->appendRow(new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::RelationshipReference,
groupRelationshipReference));
}
}
// Retrieve the model's group's component references
ObjRef<iface::cellml_api::ComponentRefSet> groupComponentReferences = group->componentRefs();
if (groupComponentReferences->length()) {
// Component references category
CellmlAnnotationViewCellmlElementItem *groupComponentReferencesItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::ComponentReference,
tr("Component References"));
groupItem->appendRow(groupComponentReferencesItem);
// Retrieve the model's group's relationship references
// themselves
ObjRef<iface::cellml_api::ComponentRefIterator> groupComponentReferencesIter = groupComponentReferences->iterateComponentRefs();
for (ObjRef<iface::cellml_api::ComponentRef> groupComponentReference = groupComponentReferencesIter->nextComponentRef();
groupComponentReference; groupComponentReference = groupComponentReferencesIter->nextComponentRef()) {
populateGroupComponentReferenceModel(groupComponentReferencesItem, groupComponentReference);
}
}
}
}
// Retrieve the model's connections
ObjRef<iface::cellml_api::ConnectionSet> connections = cellmlModel->connections();
if (connections->length()) {
// Connections category
CellmlAnnotationViewCellmlElementItem *connectionsItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Connection,
tr("Connections"));
modelItem->appendRow(connectionsItem);
// Retrieve the model's connections themselves
int counter = 0;
ObjRef<iface::cellml_api::ConnectionIterator> connectionsIter = connections->iterateConnections();
for (ObjRef<iface::cellml_api::Connection> connection = connectionsIter->nextConnection();
connection; connection = connectionsIter->nextConnection()) {
CellmlAnnotationViewCellmlElementItem *connectionItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Connection,
connection, ++counter);
connectionsItem->appendRow(connectionItem);
// Component mapping
ObjRef<iface::cellml_api::MapComponents> connectionComponentMapping = connection->componentMapping();
QString connectionComponentMappingFirstComponentName = QString::fromStdWString(connectionComponentMapping->firstComponentName());
QString connectionComponentMappingSecondComponentName = QString::fromStdWString(connectionComponentMapping->secondComponentName());
if ( !connectionComponentMappingFirstComponentName.isEmpty()
&& !connectionComponentMappingSecondComponentName.isEmpty()) {
connectionItem->appendRow(new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::ComponentMapping,
connectionComponentMapping));
}
// Variable mappings
ObjRef<iface::cellml_api::MapVariablesSet> connectionVariableMappings = connection->variableMappings();
ObjRef<iface::cellml_api::MapVariablesIterator> connectionVariableMappingsIter = connectionVariableMappings->iterateMapVariables();
for (ObjRef<iface::cellml_api::MapVariables> connectionVariableMapping = connectionVariableMappingsIter->nextMapVariables();
connectionVariableMapping; connectionVariableMapping = connectionVariableMappingsIter->nextMapVariables()) {
connectionItem->appendRow(new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::VariableMapping,
connectionVariableMapping));
}
}
}
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::populateUnitsModel(CellmlAnnotationViewCellmlElementItem *pCellmlElementItem,
iface::cellml_api::UnitsSet *pUnitsSet)
{
// Retrieve the units
if (pUnitsSet->length()) {
// Units category
CellmlAnnotationViewCellmlElementItem *unitsItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Unit,
tr("Units"));
pCellmlElementItem->appendRow(unitsItem);
// Retrieve the units themselves
ObjRef<iface::cellml_api::UnitsIterator> unitsIter = pUnitsSet->iterateUnits();
for (ObjRef<iface::cellml_api::Units> units = unitsIter->nextUnits();
units; units = unitsIter->nextUnits()) {
CellmlAnnotationViewCellmlElementItem *unitItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::Unit,
units);
unitsItem->appendRow(unitItem);
// Retrieve the units' unit references
ObjRef<iface::cellml_api::UnitSet> unitSet = units->unitCollection();
ObjRef<iface::cellml_api::UnitIterator> unitIter = unitSet->iterateUnits();
for (ObjRef<iface::cellml_api::Unit> unit = unitIter->nextUnit();
unit; unit = unitIter->nextUnit()) {
unitItem->appendRow(new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::UnitElement,
unit));
}
}
}
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::populateGroupComponentReferenceModel(CellmlAnnotationViewCellmlElementItem *pCellmlElementItem,
iface::cellml_api::ComponentRef *pGroupComponentReference)
{
CellmlAnnotationViewCellmlElementItem *groupComponentReferencesItem = new CellmlAnnotationViewCellmlElementItem(CellmlAnnotationViewCellmlElementItem::ComponentReference,
pGroupComponentReference);
pCellmlElementItem->appendRow(groupComponentReferencesItem);
// Retrieve the component reference's children
ObjRef<iface::cellml_api::ComponentRefSet> groupComponentReferences = pGroupComponentReference->componentRefs();
ObjRef<iface::cellml_api::ComponentRefIterator> groupComponentReferencesIter = groupComponentReferences->iterateComponentRefs();
for (ObjRef<iface::cellml_api::ComponentRef> groupComponentReference = groupComponentReferencesIter->nextComponentRef();
groupComponentReference; groupComponentReference = groupComponentReferencesIter->nextComponentRef()) {
populateGroupComponentReferenceModel(groupComponentReferencesItem, groupComponentReference);
}
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::resizeTreeViewToContents()
{
// Resize our tree view widget so that all of its contents is visible
mTreeViewWidget->resizeColumnToContents(0);
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::updateMetadataDetails(const QModelIndex &pNewIndex,
const QModelIndex &pOldIndex)
{
Q_UNUSED(pOldIndex);
// Make sure that we have a valid new index
if (!pNewIndex.isValid())
return;
// Let people know that we want to see the metadata associated with the
// CellML element
emit metadataDetailsRequested(static_cast<CellmlAnnotationViewCellmlElementItem *>(mTreeViewModel->itemFromIndex(pNewIndex))->element());
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::showCustomContextMenu(const QPoint &pPosition) const
{
// Make sure that we are dealing with a CellML files that doesn't have any
// issues
if (mCellmlFile->issues().count())
return;
// Determine whether to show the context menu based on whether we are over
// an item
CellmlAnnotationViewCellmlElementItem *item = static_cast<CellmlAnnotationViewCellmlElementItem *>(mTreeViewModel->itemFromIndex(mTreeViewWidget->indexAt(pPosition)));
if (item) {
// We are over an item, so create a custom context menu for our current
// item
// Update the enabled status of our actions
bool fileReadableAndWritable = Core::FileManager::instance()->isReadableAndWritable(mCellmlFile->fileName());
mExpandAllAction->setEnabled(item->hasChildren() && !indexIsAllExpanded(mTreeViewWidget->currentIndex()));
mCollapseAllAction->setEnabled(item->hasChildren() && mTreeViewWidget->isExpanded(mTreeViewWidget->currentIndex()));
mRemoveCurrentMetadataAction->setEnabled(fileReadableAndWritable && !item->isCategory() && mCellmlFile->rdfTriples(item->element()).count());
mRemoveAllMetadataAction->setEnabled(fileReadableAndWritable && !item->isCategory() && mCellmlFile->rdfTriples().count());
mOpenImportAction->setEnabled(item->type() == CellmlAnnotationViewCellmlElementItem::Import);
// Create and show the context menu, if it isn't empty
QMenu menu;
if (item->hasChildren()) {
menu.addAction(mExpandAllAction);
menu.addAction(mCollapseAllAction);
menu.addSeparator();
}
if (!item->isCategory()) {
menu.addAction(mRemoveCurrentMetadataAction);
menu.addAction(mRemoveAllMetadataAction);
}
if (item->type() == CellmlAnnotationViewCellmlElementItem::Import) {
menu.addSeparator();
menu.addAction(mOpenImportAction);
}
if (!menu.isEmpty())
menu.exec(QCursor::pos());
}
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::expandAll()
{
// Expand all the CellML elements below the current one
indexExpandAll(mTreeViewWidget->currentIndex());
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::collapseAll()
{
// Collapse all the CellML elements below the current one
indexCollapseAll(mTreeViewWidget->currentIndex());
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::removeCurrentMetadata()
{
// Remove all the metadata associated with the current node
mCellmlFile->rdfTriples().remove(currentCellmlElementItem()->element());
// Re-update the metadata details view now that the current node doesn't
// have any metadata associated with it
updateMetadataDetails(mTreeViewWidget->currentIndex(), QModelIndex());
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::removeAllMetadata()
{
// Remove all the metadata associated with the CellML file
mCellmlFile->rdfTriples().removeAll();
// Re-update the metadata details view now that the CellML file doesn't have
// any metadata associated with it
updateMetadataDetails(mTreeViewWidget->currentIndex(), QModelIndex());
}
//==============================================================================
void CellmlAnnotationViewCellmlListWidget::indexExpandAll(const QModelIndex &pIndex) const
{
// Recursively expand all the CellML elements below the current one
// Note: the test with against pIndex.child(0, 0) is to ensure that we are
// not trying to expand an index which item has no children. Indeed,
// a call to expand() is quite expensive, so the fewer of those we
// make the better...
if (pIndex.model()->index(0, 0, pIndex).isValid()) {
mTreeViewWidget->expand(pIndex);
QStandardItem *item = mTreeViewModel->itemFromIndex(pIndex);
for (int i = 0, iMax = item->rowCount(); i < iMax; ++i)
indexExpandAll(item->child(i)->index());
}