All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PluginsModel.cpp
Go to the documentation of this file.
1 #include "PluginsModel.h"
2 
3 
4 // TerraLib
5 #include "../../../../plugin/PluginInfo.h"
6 #include "../../../../plugin/PluginManager.h"
7 
8 // Qt
9 #include <QFont>
10 
11 std::string GetData(const int& column, const te::plugin::PluginInfo& info)
12 {
13  std::string result;
14 
15  switch(column)
16  {
17  case 1:
18  result = info.m_displayName;
19  break;
20 
21  case 2:
22  result = info.m_version;
23  break;
24 
25  case 3:
26  result = info.m_licenseDescription;
27  break;
28 
29  case 4:
30  result = info.m_category;
31  break;
32 
33  case 5:
34  result = info.m_site;
35  break;
36 
37  case 6:
38  result = info.m_provider.m_name;
39  break;
40 
41  case 7:
42  result = info.m_provider.m_site;
43  break;
44 
45  case 8:
46  result = info.m_provider.m_email;
47  break;
48 
49  case 9:
50  result = info.m_name;
51  break;
52 
53  default:
54  break;
55  };
56 
57  return result;
58 }
59 
60 QString GetHeader(const int& section)
61 {
62  QString header;
63 
64  switch(section)
65  {
66  case 0:
67  header = QObject::tr("Enabling");
68  break;
69 
70  case 1:
71  header = QObject::tr("Plugin");
72  break;
73 
74  case 2:
75  header = QObject::tr("Version");
76  break;
77 
78  case 3:
79  header = QObject::tr("License");
80  break;
81 
82  case 4:
83  header = QObject::tr("Category");
84  break;
85 
86  case 5:
87  header = QObject::tr("Site");
88  break;
89 
90  case 6:
91  header = QObject::tr("Provider");
92  break;
93 
94  case 7:
95  header = QObject::tr("Provider site");
96  break;
97 
98  case 8:
99  header = QObject::tr("Provider email");
100  break;
101 
102  case 9:
103  header = QObject::tr("Name");
104  break;
105 
106  };
107 
108  return header;
109 }
110 
112  QAbstractTableModel(parent)
113 {
114 }
115 
117 {
118 }
119 
120 int te::qt::widgets::PluginsModel::rowCount(const QModelIndex & parent) const
121 {
122  return (int) m_plugins.size();
123 }
124 
125 int te::qt::widgets::PluginsModel::columnCount(const QModelIndex & parent) const
126 {
127  return 10;
128 }
129 
130 QVariant te::qt::widgets::PluginsModel::data(const QModelIndex & index, int role) const
131 {
132  if(!index.isValid())
133  return QVariant();
134 
135  switch(role)
136  {
137  case Qt::TextAlignmentRole:
138  return int(Qt::AlignVCenter | Qt::AlignLeft);
139  break;
140 
141  case Qt::DisplayRole:
142  return (index.column() > 0) ?
143  QString(GetData(index.column(), *(m_plugins[index.row()])).c_str()) :
144  QVariant();
145  break;
146 
147  case Qt::FontRole:
148  {
149  QFont f;
150  PluginsStatus st = m_pluginsStatus[index.row()];
151 
152  if(st.testFlag(To_add) || st.testFlag(To_enable) || st.testFlag(To_disable))
153  f.setBold(true);
154  else if(st.testFlag(To_remove))
155  f.setItalic(true);
156 
157  return f;
158  }
159  break;
160 
161  case Qt::CheckStateRole:
162  {
163  PluginsStatus st = m_pluginsStatus[index.row()];
164 
165  if(!st.testFlag(Broked) && index.column() == 0)
166  return ((st.testFlag(Loaded) && !(st.testFlag(To_disable))) || st.testFlag(To_enable)) ? Qt::Checked : Qt::Unchecked;
167 
168  return QVariant();
169  }
170  break;
171 
172  default:
173  return QVariant();
174  break;
175  }
176 }
177 
178 QVariant te::qt::widgets::PluginsModel::headerData(int section, Qt::Orientation orientation, int role) const
179 {
180  switch (role)
181  {
182  case Qt::DisplayRole:
183  return (orientation == Qt::Horizontal) ?
184  GetHeader(section) :
185  QVariant::fromValue<int>(section+1);
186  break;
187 
188  default:
189  return QAbstractTableModel::headerData(section, orientation, role);
190  };
191 }
192 
193 Qt::ItemFlags te::qt::widgets::PluginsModel::flags(const QModelIndex & index) const
194 {
195  if (!index.isValid())
196  return 0;
197 
198  Qt::ItemFlags flags(0);
199 
200  if(!m_pluginsStatus[index.row()].testFlag(To_remove))
201  {
202  flags = flags | Qt::ItemIsEnabled;
203 
204  if(index.column() == 0)
205  flags = flags | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
206 
207  flags = flags | Qt::ItemIsSelectable;
208  }
209 
210  return flags;
211 }
212 
213 bool te::qt::widgets::PluginsModel::setData(const QModelIndex & index, const QVariant & value, int role)
214 {
215  if(!index.isValid())
216  return false;
217 
218  if(role == Qt::CheckStateRole && !m_pluginsStatus[index.row()].testFlag(Broked))
219  {
220  if(value.toInt() == Qt::Checked)
221  {
222  m_pluginsStatus[index.row()] &= ~To_disable;
223  m_pluginsStatus[index.row()] |= To_enable;
224  }
225  else
226  {
227  m_pluginsStatus[index.row()] &= ~To_enable;
228  m_pluginsStatus[index.row()] |= To_disable;
229  }
230 
231  QModelIndex idx;
232 
233  emit dataChanged (idx, idx);
234  }
235 
236  return true;
237 }
238 
239 void te::qt::widgets::PluginsModel::addPlugin(const te::plugin::PluginInfo* info, const PluginsStatus& status)
240 {
241  m_plugins.push_back(new te::plugin::PluginInfo(*info));
242  m_pluginsStatus.push_back(status);
243 
244 #if (QT_VERSION < 0x050000)
245  reset();
246 #else
247  beginResetModel();
248  endResetModel();
249 #endif
250 }
251 
252 void te::qt::widgets::PluginsModel::removePlugins(const QModelIndexList& plgs)
253 {
254  QModelIndexList::ConstIterator it;
255 
256  for(it=plgs.constBegin(); it!=plgs.constEnd(); ++it)
257  m_pluginsStatus[(*it).row()] = To_remove;
258 
259 #if (QT_VERSION < 0x050000)
260  reset();
261 #else
262  beginResetModel();
263  endResetModel();
264 #endif
265 
266 }
267 
268 void te::qt::widgets::PluginsModel::getPluginsInfo(std::vector<te::plugin::PluginInfo*>& plgs, std::vector<PluginsStatus>& status)
269 {
270  plgs = m_plugins;
271  status = m_pluginsStatus;
272 }
273 
275 {
276  std::vector<te::plugin::PluginInfo*>::iterator it;
277 
278  for(it = m_plugins.begin(); it != m_plugins.end(); ++it)
279  delete *it;
280 
281  m_plugins.clear();
282  m_pluginsStatus.clear();
283 }
void removePlugins(const QModelIndexList &plgs)
Remove the selected plugins.
Qt::ItemFlags flags(const QModelIndex &index) const
Returns the flags associated with a given item.
std::string m_name
The plugin name: an internal value used to identify the plugin in the system. Must be a unique value...
Definition: PluginInfo.h:66
int rowCount(const QModelIndex &parent) const
Returns the number of plugins regitered in plugins manager.
void addPlugin(const te::plugin::PluginInfo *info, const PluginsStatus &status)
Adds information about plugin.
void clear()
Clear the list of plugins and status.
std::string m_site
The provider home page.
Definition: Provider.h:48
std::string m_licenseDescription
A brief description about the plugin license.
Definition: PluginInfo.h:73
std::string m_displayName
The plugin name to be displayed in a graphical interface.
Definition: PluginInfo.h:67
void getPluginsInfo(std::vector< te::plugin::PluginInfo * > &plgs, std::vector< PluginsStatus > &status)
Returns plugins informations.
Provider m_provider
Information about the plugin provider.
Definition: PluginInfo.h:77
std::string m_version
The plugin version.
Definition: PluginInfo.h:69
std::string m_site
An URL pointing to the plugin site.
Definition: PluginInfo.h:76
std::string GetData(const int &column, const te::plugin::PluginInfo &info)
QString GetHeader(const int &section)
std::string m_email
The provider contact e-mail.
Definition: Provider.h:49
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Returns the data to be presented as header.
QVariant data(const QModelIndex &index, int role) const
Returns data identified by index.
int columnCount(const QModelIndex &parent) const
Returns the number of columns.
std::string m_name
Provider name: may be a person or a company.
Definition: Provider.h:47
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
The basic information about a plugin.
Definition: PluginInfo.h:61
std::string m_category
The plugin category.
Definition: PluginInfo.h:75
virtual ~PluginsModel()
Destructor.
PluginsModel(QObject *parent=0)
Constructor.