Fraxinus  22.09
An IGT application
cxPluginTableModel.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 // Based on a class from CTK:
13 
14 /*=============================================================================
15 
16 
17  Library: CTK
18 
19  Copyright (c) German Cancer Research Center,
20  Division of Medical and Biological Informatics
21 
22  Licensed under the Apache License, Version 2.0 (the "License");
23  you may not use this file except in compliance with the License.
24  You may obtain a copy of the License at
25 
26  http://www.apache.org/licenses/LICENSE-2.0
27 
28  Unless required by applicable law or agreed to in writing, software
29  distributed under the License is distributed on an "AS IS" BASIS,
30  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  See the License for the specific language governing permissions and
32  limitations under the License.
33 
34 =============================================================================*/
35 
36 #include "cxPluginTableModel.h"
37 
38 #include <QtDebug>
39 #include <ctkPlugin.h>
40 #include <ctkPluginContext.h>
41 #include <iostream>
42 
44 
45 namespace cx
46 {
47 
49  : QAbstractTableModel(parent)
50 {
51  mFramework = framework;
52  connect(mFramework.get(), SIGNAL(pluginPoolChanged()), this, SLOT(resetAll()));
53  mFramework->getPluginContext()->connectPluginListener(this, SLOT(pluginChanged(ctkPluginEvent)));
54  this->resetAll();
55 }
56 
58 {
59  this->beginResetModel();
60  QStringList names = mFramework->getPluginSymbolicNames();
61  std::map<QString, ctkPluginPtr> plugins;
62  for (int i=0; i<names.size(); ++i)
63  plugins[names[i]].clear();
64 
65  QList<ctkPluginPtr> installed = mFramework->getPluginContext()->getPlugins();
66  for (int i=0; i<installed.size(); ++i)
67  plugins[installed[i]->getSymbolicName()] = installed[i];
68 
69  mPlugins = plugins;
70 
71  this->endResetModel();
72 }
73 
74 QVariant ctkPluginTableModel::data(const QModelIndex& index, int role) const
75 {
76  if (!index.isValid()) return QVariant();
77 
78  std::map<QString, ctkPluginPtr>::const_iterator iter = mPlugins.begin();
79  std::advance(iter, index.row());
80  QString name = iter->first;
81  QSharedPointer<ctkPlugin> plugin = iter->second;
82 
83  if (role == Qt::DisplayRole)
84  {
85  int col = index.column();
86  if (col == 0)
87  {
88  return QVariant(name);
89  }
90  else if (col == 1)
91  {
92  if (plugin)
93  return QVariant(plugin->getVersion().toString());
94  }
95  else if (col == 2)
96  {
97  if (plugin)
98  return QVariant(getStringForctkPluginState(plugin->getState()));
99  else
100  return getStringForctkPluginState(ctkPlugin::UNINSTALLED);
101  }
102  }
103  else if (role == Qt::UserRole)
104  {
105  return QVariant(name);
106  }
107 
108  return QVariant();
109 }
110 
111 QVariant ctkPluginTableModel::headerData(int section, Qt::Orientation orientation, int role) const
112 {
113  if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
114  {
115  if (section == 0)
116  {
117  return QVariant("Plugin");
118  }
119  else if (section == 1)
120  {
121  return QVariant("Version");
122  }
123  else if (section == 2)
124  {
125  return QVariant("State");
126  }
127  }
128 
129  return QVariant();
130 }
131 
132 int ctkPluginTableModel::columnCount(const QModelIndex& parent) const
133 {
134  Q_UNUSED(parent)
135 
136  return 3;
137 }
138 
139 int ctkPluginTableModel::rowCount(const QModelIndex& parent) const
140 {
141  Q_UNUSED(parent)
142 
143  return mPlugins.size();
144 }
145 
146 void ctkPluginTableModel::pluginChanged(const ctkPluginEvent& event)
147 {
148  if (!event.getPlugin())
149  {
150  qDebug() << "!! invalid plugin event";
151  return;
152  }
153 
154  int i=0;
155  for(std::map<QString, ctkPluginPtr>::iterator iter=mPlugins.begin(); iter!=mPlugins.end(); ++iter, ++i)
156  {
157  if (event.getPlugin()->getSymbolicName() != iter->first)
158  continue;
159 
160  iter->second = event.getPlugin();
161 
162  QModelIndex topLeftIndex = createIndex(i, 0);
163  QModelIndex bottomRightIndex = createIndex(topLeftIndex.row(), columnCount()-1);
164  emit dataChanged(topLeftIndex, bottomRightIndex);
165  }
166 }
167 
168 }
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
int rowCount(const QModelIndex &parent=QModelIndex()) const
QString getStringForctkPluginState(const ctkPlugin::State state)
ctkPluginTableModel(PluginFrameworkManagerPtr framework, QObject *parent=0)
int columnCount(const QModelIndex &parent=QModelIndex()) const
boost::shared_ptr< class PluginFrameworkManager > PluginFrameworkManagerPtr
void pluginChanged(const ctkPluginEvent &event)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Namespace for all CustusX production code.