pwmfanmodel.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2015 Malte Veerman <malte.veerman@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License or (at your option) version 3 or any later version
  8. * accepted by the membership of KDE e.V. (or its successor approved
  9. * by the membership of KDE e.V.), which shall act as a proxy
  10. * defined in Section 14 of version 3 of the license.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "pwmfanmodel.h"
  22. #include "hwmon.h"
  23. #include "pwmfan.h"
  24. namespace Fancontrol
  25. {
  26. PwmFanModel::PwmFanModel(QObject *parent) : QAbstractListModel(parent)
  27. {
  28. }
  29. QHash<int, QByteArray> PwmFanModel::roleNames() const
  30. {
  31. QHash<int, QByteArray> roleNames;
  32. roleNames.insert(DisplayRole, "display");
  33. roleNames.insert(ObjectRole, "object");
  34. return roleNames;
  35. }
  36. QVariant PwmFanModel::data(const QModelIndex& index, int role) const
  37. {
  38. if (!index.isValid())
  39. return QVariant();
  40. const int row = index.row();
  41. if (row >= rowCount())
  42. return QVariant();
  43. const auto fan = m_fans.at(row);
  44. if (!fan)
  45. return QVariant();
  46. switch (role)
  47. {
  48. case DisplayRole:
  49. return fan->name() + " (" + fan->id() + ")";
  50. case ObjectRole:
  51. return QVariant::fromValue(fan);
  52. default:
  53. return QVariant();
  54. }
  55. }
  56. void PwmFanModel::setPwmFans(QList<PwmFan *> fans)
  57. {
  58. std::sort(fans.begin(), fans.end(), [] (PwmFan *a, PwmFan *b) { if (a->parent() == b->parent()) return a->index() < b->index(); return a->parent()->name() < b->parent()->name(); });
  59. if (m_fans == fans)
  60. return;
  61. beginResetModel();
  62. m_fans = fans;
  63. emit fansChanged();
  64. for (const auto &fan : fans)
  65. connect(fan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
  66. endResetModel();
  67. }
  68. void PwmFanModel::addPwmFan(PwmFan* newFan)
  69. {
  70. for (const auto &oldFan : qAsConst(m_fans))
  71. {
  72. if (*oldFan == *newFan)
  73. return;
  74. }
  75. auto newFans = m_fans << newFan;
  76. std::sort(newFans.begin(), newFans.end(), [] (PwmFan *a, PwmFan *b) { if (a->parent() == b->parent()) return a->index() < b->index(); return a->parent()->name() < b->parent()->name(); });
  77. int index = newFans.indexOf(newFan);
  78. connect(newFan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
  79. beginInsertRows(QModelIndex(), index, index);
  80. m_fans = newFans;
  81. emit fansChanged();
  82. endInsertRows();
  83. }
  84. void PwmFanModel::addPwmFans(const QList<PwmFan *> &newFans)
  85. {
  86. for (const auto &newFan : newFans)
  87. {
  88. addPwmFan(newFan);
  89. }
  90. }
  91. void PwmFanModel::updateFan(PwmFan *fan)
  92. {
  93. if (!fan)
  94. return;
  95. const auto i = m_fans.indexOf(fan);
  96. if (i == -1)
  97. return;
  98. emit dataChanged(index(i, 0), index(i, 0), QVector<int>{ DisplayRole });
  99. }
  100. void PwmFanModel::updateFan()
  101. {
  102. const auto fan = qobject_cast<PwmFan*>(sender());
  103. updateFan(fan);
  104. }
  105. QObject * PwmFanModel::fan(int index) const
  106. {
  107. return m_fans.value(index);
  108. }
  109. }