pluginsmodel.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. The code is adpted from QtNote project
  3. Copyright (C) 2010-2016 Sergey Ili'nykh
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Contacts:
  15. E-Mail: rion4ik@gmail.com XMPP: rion@jabber.ru
  16. */
  17. #include <QPalette>
  18. #include <QFont>
  19. #include "pluginsmodel.h"
  20. #include "dictcore.h"
  21. #include "application.h"
  22. #include "pluginmanager.h"
  23. #include "util.h"
  24. namespace QStarDict {
  25. PluginsModel::PluginsModel(LoadType lt, PluginManager *pmanager) :
  26. QAbstractTableModel(pmanager),
  27. pmanager(pmanager)
  28. {
  29. pluginIds = pmanager->availablePlugins();
  30. QMutableStringListIterator it(pluginIds);
  31. while (it.hasNext()) {
  32. auto &md = pmanager->pluginDesc(it.next())->metadata;
  33. if (lt == LoadType::JustDict && !md.features.contains("dict")) {
  34. it.remove();
  35. }
  36. if (lt == LoadType::ExceptDict && md.features.contains("dict")) {
  37. it.remove();
  38. }
  39. }
  40. QPixmap pix(":pics/configure.png");
  41. settingIcon = QIcon(pix);
  42. QTransform transform;
  43. transform.rotate(45);
  44. pix.transformed(transform, Qt::SmoothTransformation);
  45. settingIcon.addPixmap(pix.transformed(transform),
  46. QIcon::Active);
  47. }
  48. int PluginsModel::rowCount(const QModelIndex &parent) const
  49. {
  50. if (parent.isValid()) {
  51. return 0;
  52. }
  53. return pluginIds.count();
  54. }
  55. int PluginsModel::columnCount(const QModelIndex &parent) const
  56. {
  57. if (parent.isValid()) {
  58. return 0;
  59. }
  60. return 4;
  61. }
  62. QVariant PluginsModel::data(const QModelIndex &index, int role) const
  63. {
  64. QString pluginId = pluginIds[index.row()];
  65. if (role == IdRole) {
  66. return pluginId;
  67. }
  68. if (index.column() == 0) {
  69. switch (role) {
  70. case Qt::CheckStateRole:
  71. return pmanager->pluginDesc(pluginId)->isEnabled()?
  72. Qt::Checked : Qt::Unchecked;
  73. case Qt::DisplayRole:
  74. return pmanager->pluginDesc(pluginId)->metadata.name;
  75. case Qt::DecorationRole:
  76. {
  77. auto &md = pmanager->pluginDesc(pluginId)->metadata;
  78. QIcon icon = md.icon;
  79. if (icon.isNull()) {
  80. if (md.features.contains("dict")) {
  81. icon = QIcon(":pics/qstardict.png");
  82. } else if (md.features.contains("de")) {
  83. icon = QIcon(":pics/computer.png");
  84. } else {
  85. icon = QIcon(":pics/plugin.png");
  86. }
  87. }
  88. return icon;
  89. }
  90. case Qt::ToolTipRole:
  91. {
  92. auto pd = pmanager->pluginDesc(pluginId);
  93. QString ret = pd->metadata.description + QLatin1String("<br/><br/>") +
  94. tr("<b>Filename:</b> %1").arg(pd->loader->fileName()) + "<br/><br/>" +
  95. tr("<b>Status:</b> %1").arg(
  96. pd->state & PluginManager::Plugin::Valid?
  97. pd->isLoaded()?
  98. tr("Loaded") : tr("Not Loaded") :
  99. tr("Invalid")
  100. );
  101. return ret;
  102. }
  103. case Qt::FontRole:
  104. {
  105. QFont f; // application default font. may by not what we expect
  106. auto pd = pmanager->pluginDesc(pluginId);
  107. if (pd->isLoaded()) {
  108. f.setBold(true);
  109. }
  110. return f;
  111. }
  112. case Qt::ForegroundRole:
  113. {
  114. QColor color = qApp->palette().color(QPalette::Foreground); // mey be not what we expect
  115. auto pd = pmanager->pluginDesc(pluginId);
  116. if (!(pd->state & PluginManager::Plugin::Valid)) {
  117. color.setAlpha(128);
  118. }
  119. return color;
  120. }
  121. }
  122. } else if (index.column() == 1) { // version
  123. if (role == Qt::DisplayRole) {
  124. return pmanager->pluginDesc(pluginId)->metadata.version;
  125. }
  126. } else if (index.column() == 2) { // settings button
  127. // options button
  128. if (role == Qt::DecorationRole && pmanager->plugin<ConfigurablePlugin>(pluginId)) {
  129. return settingIcon;
  130. }
  131. } else if (index.column() == 3) { // settings button
  132. // options button
  133. if (role == Qt::DecorationRole) {
  134. return QIcon(":pics/dialog-information.png");
  135. }
  136. }
  137. return QVariant();
  138. }
  139. bool PluginsModel::setData(const QModelIndex &index, const QVariant &value, int role)
  140. {
  141. if (index.column() == 0 && role == Qt::CheckStateRole) {
  142. Qt::CheckState cs = (Qt::CheckState)value.value<int>();
  143. pmanager->setEnabled(pluginIds[index.row()], cs == Qt::Checked);
  144. emit dataChanged(index, index);
  145. emit loadedListChanged();
  146. return true;
  147. }
  148. return false;
  149. }
  150. Qt::ItemFlags PluginsModel::flags(const QModelIndex &index) const
  151. {
  152. if (index.column() == 0) {
  153. return QAbstractTableModel::flags(index) | Qt::ItemIsUserCheckable;
  154. }
  155. return QAbstractTableModel::flags(index);
  156. }
  157. QString PluginsModel::pluginId(int row) const
  158. {
  159. return pluginIds.at(row);
  160. }
  161. QStringList PluginsModel::loadedPlugins()
  162. {
  163. QStringList ret;
  164. foreach (const QString &id, pluginIds) {
  165. if (pmanager->isLoaded(id)) {
  166. ret << id;
  167. }
  168. }
  169. return ret;
  170. }
  171. } // namespace QStarDict