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