Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
berryPerspectiveListModel.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 
15 
18 
19 #include <QIcon>
20 
21 namespace berry {
22 
23 struct PerspectiveListModel::Impl
24 {
25  IPerspectiveRegistry& m_PerspReg;
26  QList<IPerspectiveDescriptor::Pointer> m_Perspectives;
27  const bool m_MarkDefault;
28 
29  Impl(IPerspectiveRegistry& perspReg, bool markDefault)
30  : m_PerspReg(perspReg)
31  // TODO use activity filter for correct perspective list
32  , m_Perspectives(perspReg.GetPerspectives())
33  , m_MarkDefault(markDefault)
34  {
35  }
36 };
37 
38 PerspectiveListModel::PerspectiveListModel(IPerspectiveRegistry& perspReg, bool markDefault, QObject* parent)
39  : QAbstractListModel(parent)
40  , d(new Impl(perspReg, markDefault))
41 {
42 }
43 
45 {
46 }
47 
48 int PerspectiveListModel::rowCount(const QModelIndex& /*parent*/) const
49 {
50  return d->m_Perspectives.size();
51 }
52 
53 QVariant PerspectiveListModel::data(const QModelIndex& index, int role) const
54 {
55  if (index.row() < 0 || index.row() >= d->m_Perspectives.size() ||
56  index.column() > 0)
57  {
58  return QVariant();
59  }
60 
61  if (role == Qt::DisplayRole)
62  {
63  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
64  QString label = desc->GetLabel();
65  if (d->m_MarkDefault)
66  {
67  QString def = d->m_PerspReg.GetDefaultPerspective();
68  if (desc->GetId() == def)
69  {
70  label += " (default)";
71  }
72  }
73  return label;
74  }
75  else if (role == Qt::DecorationRole)
76  {
77  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
78  return desc->GetImageDescriptor();
79  }
80  else if (role == Id)
81  {
82  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
83  return desc->GetId();
84  }
85  else if (role == Description)
86  {
87  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
88  return desc->GetDescription();
89  }
90  return QVariant();
91 }
92 
93 QVariant PerspectiveListModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
94 {
95  if (role == Qt::DisplayRole)
96  {
97  if (section == 0)
98  {
99  return QString("Perspective");
100  }
101  }
102  return QVariant();
103 }
104 
105 QString PerspectiveListModel::perspectiveName(const QModelIndex& index) const
106 {
107  if (!index.isValid()) return QString::null;
108 
109  return d->m_Perspectives.at(index.row())->GetLabel();
110 }
111 
113 {
114  return d->m_Perspectives.at(index.row());
115 }
116 
117 QModelIndex PerspectiveListModel::index(const QString& perspId) const
118 {
119  int index = -1;
120  for(int i = 0; i < d->m_Perspectives.size(); ++i)
121  {
122  if (d->m_Perspectives.at(i)->GetId() == perspId)
123  {
124  index = i;
125  break;
126  }
127  }
128 
129  if (index > -1)
130  {
131  return this->createIndex(index, 0);
132  }
133  return QModelIndex();
134 }
135 
136 
137 
138 }
QString perspectiveName(const QModelIndex &index) const
SmartPointer< IPerspectiveDescriptor > perspectiveDescriptor(const QModelIndex &index) const
QModelIndex index(const QString &perspId) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
PerspectiveListModel(IPerspectiveRegistry &perspReg, bool markDefault=true, QObject *parent=nullptr)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override