ctkPluginTableModel.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginTableModel.h"
  16. #include <ctkPlugin.h>
  17. #include <ctkPluginContext.h>
  18. ctkPluginTableModel::ctkPluginTableModel(ctkPluginContext* pc, QObject* parent)
  19. : QAbstractTableModel(parent)
  20. {
  21. plugins = pc->getPlugins();
  22. pc->connectPluginListener(this, SLOT(pluginChanged(ctkPluginEvent)));
  23. }
  24. QVariant ctkPluginTableModel::data(const QModelIndex& index, int role) const
  25. {
  26. if (!index.isValid()) return QVariant();
  27. QSharedPointer<ctkPlugin> plugin = plugins.at(index.row());
  28. if (role == Qt::DisplayRole)
  29. {
  30. int col = index.column();
  31. if (col == 0)
  32. {
  33. return QVariant(plugin->getSymbolicName());
  34. }
  35. else if (col == 1)
  36. {
  37. return QVariant(plugin->getVersion().toString());
  38. }
  39. else if (col == 2)
  40. {
  41. return QVariant(getStringForState(plugin->getState()));
  42. }
  43. }
  44. else if (role == Qt::UserRole)
  45. {
  46. return QVariant::fromValue<qlonglong>(plugin->getPluginId());
  47. }
  48. return QVariant();
  49. }
  50. QVariant ctkPluginTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  51. {
  52. if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
  53. {
  54. if (section == 0)
  55. {
  56. return QVariant("Plugin");
  57. }
  58. else if (section == 1)
  59. {
  60. return QVariant("Version");
  61. }
  62. else if (section == 2)
  63. {
  64. return QVariant("State");
  65. }
  66. }
  67. return QVariant();
  68. }
  69. int ctkPluginTableModel::columnCount(const QModelIndex& parent) const
  70. {
  71. Q_UNUSED(parent)
  72. return 3;
  73. }
  74. int ctkPluginTableModel::rowCount(const QModelIndex& parent) const
  75. {
  76. Q_UNUSED(parent)
  77. return plugins.size();
  78. }
  79. QString ctkPluginTableModel::getStringForState(const ctkPlugin::State state) const
  80. {
  81. static const QString uninstalled("UNINSTALLED");
  82. static const QString installed("INSTALLED");
  83. static const QString resolved("RESOLVED");
  84. static const QString starting("STARTING");
  85. static const QString stopping("STOPPING");
  86. static const QString active("ACTIVE");
  87. switch(state)
  88. {
  89. case ctkPlugin::UNINSTALLED: return uninstalled;
  90. case ctkPlugin::INSTALLED: return installed;
  91. case ctkPlugin::RESOLVED: return resolved;
  92. case ctkPlugin::STARTING: return starting;
  93. case ctkPlugin::STOPPING: return stopping;
  94. case ctkPlugin::ACTIVE: return active;
  95. default: return QString("unknown");
  96. }
  97. }
  98. void ctkPluginTableModel::pluginChanged(const ctkPluginEvent& event)
  99. {
  100. QModelIndex topLeftIndex = createIndex(plugins.indexOf(event.getPlugin()), 0);
  101. QModelIndex bottomRightIndex = createIndex(topLeftIndex.row(), columnCount()-1);
  102. emit dataChanged(topLeftIndex, bottomRightIndex);
  103. }