Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryPerspectiveListModel.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 
19 
22 
23 #include <QIcon>
24 
25 namespace berry {
26 
27 struct PerspectiveListModel::Impl
28 {
29  IPerspectiveRegistry& m_PerspReg;
30  QList<IPerspectiveDescriptor::Pointer> m_Perspectives;
31  const bool m_MarkDefault;
32 
33  Impl(IPerspectiveRegistry& perspReg, bool markDefault)
34  : m_PerspReg(perspReg)
35  // TODO use activity filter for correct perspective list
36  , m_Perspectives(perspReg.GetPerspectives())
37  , m_MarkDefault(markDefault)
38  {
39  }
40 };
41 
42 PerspectiveListModel::PerspectiveListModel(IPerspectiveRegistry& perspReg, bool markDefault, QObject* parent)
43  : QAbstractListModel(parent)
44  , d(new Impl(perspReg, markDefault))
45 {
46 }
47 
49 {
50 }
51 
52 int PerspectiveListModel::rowCount(const QModelIndex& /*parent*/) const
53 {
54  return d->m_Perspectives.size();
55 }
56 
57 QVariant PerspectiveListModel::data(const QModelIndex& index, int role) const
58 {
59  if (index.row() < 0 || index.row() >= d->m_Perspectives.size() ||
60  index.column() > 0)
61  {
62  return QVariant();
63  }
64 
65  if (role == Qt::DisplayRole)
66  {
67  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
68  QString label = desc->GetLabel();
69  if (d->m_MarkDefault)
70  {
71  QString def = d->m_PerspReg.GetDefaultPerspective();
72  if (desc->GetId() == def)
73  {
74  label += " (default)";
75  }
76  }
77  return label;
78  }
79  else if (role == Qt::DecorationRole)
80  {
81  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
82  return desc->GetImageDescriptor();
83  }
84  else if (role == Id)
85  {
86  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
87  return desc->GetId();
88  }
89  else if (role == Description)
90  {
91  const IPerspectiveDescriptor::Pointer& desc = d->m_Perspectives.at(index.row());
92  return desc->GetDescription();
93  }
94  return QVariant();
95 }
96 
97 QVariant PerspectiveListModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
98 {
99  if (role == Qt::DisplayRole)
100  {
101  if (section == 0)
102  {
103  return QString("Perspective");
104  }
105  }
106  return QVariant();
107 }
108 
109 QString PerspectiveListModel::perspectiveName(const QModelIndex& index) const
110 {
111  if (!index.isValid()) return QString::null;
112 
113  return d->m_Perspectives.at(index.row())->GetLabel();
114 }
115 
117 {
118  return d->m_Perspectives.at(index.row());
119 }
120 
121 QModelIndex PerspectiveListModel::index(const QString& perspId) const
122 {
123  int index = -1;
124  for(int i = 0; i < d->m_Perspectives.size(); ++i)
125  {
126  if (d->m_Perspectives.at(i)->GetId() == perspId)
127  {
128  index = i;
129  break;
130  }
131  }
132 
133  if (index > -1)
134  {
135  return this->createIndex(index, 0);
136  }
137  return QModelIndex();
138 }
139 
140 
141 
142 }
QModelIndex index(const QString &perspId) const
SmartPointer< IPerspectiveDescriptor > perspectiveDescriptor(const QModelIndex &index) const
QString perspectiveName(const QModelIndex &index) 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