Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 // Based on a class from CTK:
34 
35 /*=============================================================================
36 
37 
38  Library: CTK
39 
40  Copyright (c) German Cancer Research Center,
41  Division of Medical and Biological Informatics
42 
43  Licensed under the Apache License, Version 2.0 (the "License");
44  you may not use this file except in compliance with the License.
45  You may obtain a copy of the License at
46 
47  http://www.apache.org/licenses/LICENSE-2.0
48 
49  Unless required by applicable law or agreed to in writing, software
50  distributed under the License is distributed on an "AS IS" BASIS,
51  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
52  See the License for the specific language governing permissions and
53  limitations under the License.
54 
55 =============================================================================*/
56 
57 #include "cxPluginTableModel.h"
58 
59 #include <QtDebug>
60 #include <ctkPlugin.h>
61 #include <ctkPluginContext.h>
62 #include <iostream>
63 
65 
66 namespace cx
67 {
68 
70  : QAbstractTableModel(parent)
71 {
72  mFramework = framework;
73  connect(mFramework.get(), SIGNAL(pluginPoolChanged()), this, SLOT(resetAll()));
74  mFramework->getPluginContext()->connectPluginListener(this, SLOT(pluginChanged(ctkPluginEvent)));
75  this->resetAll();
76 }
77 
79 {
80  this->beginResetModel();
81  QStringList names = mFramework->getPluginSymbolicNames();
82  std::map<QString, ctkPluginPtr> plugins;
83  for (int i=0; i<names.size(); ++i)
84  plugins[names[i]].clear();
85 
86  QList<ctkPluginPtr> installed = mFramework->getPluginContext()->getPlugins();
87  for (int i=0; i<installed.size(); ++i)
88  plugins[installed[i]->getSymbolicName()] = installed[i];
89 
90  mPlugins = plugins;
91 
92  this->endResetModel();
93 }
94 
95 QVariant ctkPluginTableModel::data(const QModelIndex& index, int role) const
96 {
97  if (!index.isValid()) return QVariant();
98 
99  std::map<QString, ctkPluginPtr>::const_iterator iter = mPlugins.begin();
100  std::advance(iter, index.row());
101  QString name = iter->first;
102  QSharedPointer<ctkPlugin> plugin = iter->second;
103 
104  if (role == Qt::DisplayRole)
105  {
106  int col = index.column();
107  if (col == 0)
108  {
109  return QVariant(name);
110  }
111  else if (col == 1)
112  {
113  if (plugin)
114  return QVariant(plugin->getVersion().toString());
115  }
116  else if (col == 2)
117  {
118  if (plugin)
119  return QVariant(getStringForctkPluginState(plugin->getState()));
120  else
121  return getStringForctkPluginState(ctkPlugin::UNINSTALLED);
122  }
123  }
124  else if (role == Qt::UserRole)
125  {
126  return QVariant(name);
127  }
128 
129  return QVariant();
130 }
131 
132 QVariant ctkPluginTableModel::headerData(int section, Qt::Orientation orientation, int role) const
133 {
134  if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
135  {
136  if (section == 0)
137  {
138  return QVariant("Plugin");
139  }
140  else if (section == 1)
141  {
142  return QVariant("Version");
143  }
144  else if (section == 2)
145  {
146  return QVariant("State");
147  }
148  }
149 
150  return QVariant();
151 }
152 
153 int ctkPluginTableModel::columnCount(const QModelIndex& parent) const
154 {
155  Q_UNUSED(parent)
156 
157  return 3;
158 }
159 
160 int ctkPluginTableModel::rowCount(const QModelIndex& parent) const
161 {
162  Q_UNUSED(parent)
163 
164  return mPlugins.size();
165 }
166 
167 void ctkPluginTableModel::pluginChanged(const ctkPluginEvent& event)
168 {
169  if (!event.getPlugin())
170  {
171  qDebug() << "!! invalid plugin event";
172  return;
173  }
174 
175  int i=0;
176  for(std::map<QString, ctkPluginPtr>::iterator iter=mPlugins.begin(); iter!=mPlugins.end(); ++iter, ++i)
177  {
178  if (event.getPlugin()->getSymbolicName() != iter->first)
179  continue;
180 
181  iter->second = event.getPlugin();
182 
183  QModelIndex topLeftIndex = createIndex(i, 0);
184  QModelIndex bottomRightIndex = createIndex(topLeftIndex.row(), columnCount()-1);
185  emit dataChanged(topLeftIndex, bottomRightIndex);
186  }
187 }
188 
189 }
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