ctkPluginTableModel.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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. namespace ctk {
  19. PluginTableModel::PluginTableModel(PluginContext* pc, QObject* parent)
  20. : QAbstractTableModel(parent)
  21. {
  22. plugins = pc->getPlugins();
  23. }
  24. QVariant PluginTableModel::data(const QModelIndex& index, int role) const
  25. {
  26. if (!index.isValid()) return QVariant();
  27. Plugin* 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 PluginTableModel::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 PluginTableModel::columnCount(const QModelIndex& parent) const
  70. {
  71. return 3;
  72. }
  73. int PluginTableModel::rowCount(const QModelIndex& parent) const
  74. {
  75. return plugins.size();
  76. }
  77. QString PluginTableModel::getStringForState(const Plugin::State state) const
  78. {
  79. static const QString uninstalled("UNINSTALLED");
  80. static const QString installed("INSTALLED");
  81. static const QString resolved("RESOLVED");
  82. static const QString starting("STARTING");
  83. static const QString stopping("STOPPING");
  84. static const QString active("ACTIVE");
  85. switch(state)
  86. {
  87. case Plugin::UNINSTALLED: return uninstalled;
  88. case Plugin::INSTALLED: return installed;
  89. case Plugin::RESOLVED: return resolved;
  90. case Plugin::STARTING: return starting;
  91. case Plugin::STOPPING: return stopping;
  92. case Plugin::ACTIVE: return active;
  93. default: return QString("unknown");
  94. }
  95. }
  96. }