Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkModuleTableModel.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 #include "QmitkModuleTableModel.h"
14 
15 #include <usGetModuleContext.h>
16 #include <usModule.h>
17 #include <usModuleContext.h>
18 
19 #include <QBrush>
20 
21 class QmitkModuleTableModelPrivate
22 {
23 public:
24  QmitkModuleTableModelPrivate(QmitkModuleTableModel *q, us::ModuleContext *mc) : q(q), context(mc)
25  {
26  std::vector<us::Module *> m = context->GetModules();
27  for (std::vector<us::Module *>::const_iterator it = m.begin(); it != m.end(); ++it)
28  {
29  modules[(*it)->GetModuleId()] = *it;
30  }
31 
32  context->AddModuleListener(this, &QmitkModuleTableModelPrivate::ModuleChanged);
33  }
34 
35  ~QmitkModuleTableModelPrivate() { context->RemoveModuleListener(this, &QmitkModuleTableModelPrivate::ModuleChanged); }
36  void ModuleChanged(us::ModuleEvent event) { q->insertModule(event.GetModule()); }
38  us::ModuleContext *context;
39  QMap<long, us::Module *> modules;
40 };
41 
42 QmitkModuleTableModel::QmitkModuleTableModel(QObject *parent, us::ModuleContext *mc)
43  : QAbstractTableModel(parent), d(new QmitkModuleTableModelPrivate(this, mc ? mc : us::GetModuleContext()))
44 {
45 }
46 
48 {
49  delete d;
50 }
51 
52 int QmitkModuleTableModel::rowCount(const QModelIndex &parent) const
53 {
54  if (parent.isValid())
55  return 0;
56  return d->modules.size();
57 }
58 
59 int QmitkModuleTableModel::columnCount(const QModelIndex &parent) const
60 {
61  if (parent.isValid())
62  return 0;
63  return 4;
64 }
65 
66 QVariant QmitkModuleTableModel::data(const QModelIndex &index, int role) const
67 {
68  if (!index.isValid())
69  return QVariant();
70 
71  if (role == Qt::DisplayRole)
72  {
73  us::Module *module = d->modules[index.row() + 1];
74  switch (index.column())
75  {
76  case 0:
77  return QVariant::fromValue(static_cast<int>(module->GetModuleId()));
78  case 1:
79  return QString::fromStdString(module->GetName());
80  case 2:
81  return QString::fromStdString(module->GetVersion().ToString());
82  case 3:
83  return QString::fromStdString(module->GetLocation());
84  }
85  }
86  else if (role == Qt::ForegroundRole)
87  {
88  us::Module *module = d->modules[index.row() + 1];
89  if (!module->IsLoaded())
90  {
91  return QBrush(Qt::gray);
92  }
93  }
94  else if (role == Qt::ToolTipRole)
95  {
96  us::Module *module = d->modules[index.row() + 1];
97  QString id = QString::number(module->GetModuleId());
98  QString name = QString::fromStdString(module->GetName());
99  QString version = QString::fromStdString(module->GetVersion().ToString());
100  QString location = QString::fromStdString(module->GetLocation());
101  QString state = module->IsLoaded() ? "Loaded" : "Unloaded";
102 
103  QString tooltip = "Id: %1\nName: %2\nVersion: %3\nLocation: %7\nState: %9";
104 
105  return tooltip.arg(id, name, version, location, state);
106  }
107  return QVariant();
108 }
109 
110 QVariant QmitkModuleTableModel::headerData(int section, Qt::Orientation orientation, int role) const
111 {
112  if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
113  return QVariant();
114 
115  switch (section)
116  {
117  case 0:
118  return "Id";
119  case 1:
120  return "Name";
121  case 2:
122  return "Version";
123  case 3:
124  return "Location";
125  }
126  return QVariant();
127 }
128 
129 void QmitkModuleTableModel::insertModule(us::Module *module)
130 {
131  long id = module->GetModuleId();
132  if (d->modules.contains(id))
133  {
134  d->modules[id] = module;
135  emit dataChanged(createIndex(id - 1, 0), createIndex(id - 1, columnCount()));
136  return;
137  }
138  else
139  {
140  beginInsertRows(QModelIndex(), id - 1, id - 1);
141  d->modules[id] = module;
142  endInsertRows();
143  }
144 }
QmitkModuleTableModel(QObject *parent=nullptr, us::ModuleContext *mc=nullptr)
Module * GetModule() const
bool IsLoaded() const
Definition: usModule.cpp:114
long GetModuleId() const
Definition: usModule.cpp:211
ModuleVersion GetVersion() const
Definition: usModule.cpp:226
std::string ToString() const
std::string GetLocation() const
Definition: usModule.cpp:216
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
std::string GetName() const
Definition: usModule.cpp:221
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.