pwmfanmodel.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "pwmfan.h"
  23. namespace Fancontrol
  24. {
  25. PwmFanModel::PwmFanModel(QObject *parent) : QStringListModel(parent)
  26. {
  27. }
  28. void PwmFanModel::setPwmFans(const QList<PwmFan *> &fans)
  29. {
  30. if (m_fans == fans)
  31. return;
  32. m_fans = fans;
  33. emit fansChanged();
  34. QStringList list;
  35. for (const auto &fan : fans)
  36. {
  37. connect(fan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
  38. list << fan->name() + " (" + fan->path() + ")";
  39. }
  40. setStringList(list);
  41. }
  42. void PwmFanModel::addPwmFans(QList<PwmFan *> newFans)
  43. {
  44. for (const auto &newFan : newFans)
  45. {
  46. for (const auto &fan : qAsConst(m_fans))
  47. {
  48. if (*fan == *newFan)
  49. {
  50. newFans.removeAll(newFan);
  51. break;
  52. }
  53. }
  54. }
  55. if (!newFans.isEmpty())
  56. {
  57. m_fans += newFans;
  58. emit fansChanged();
  59. const auto oldSize = rowCount();
  60. insertRows(oldSize, newFans.size());
  61. for (const auto &fan : qAsConst(newFans))
  62. {
  63. connect(fan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
  64. updateFan(fan);
  65. }
  66. }
  67. }
  68. void PwmFanModel::updateFan(PwmFan *fan)
  69. {
  70. if (!fan)
  71. return;
  72. const auto i = m_fans.indexOf(fan);
  73. if (i == -1)
  74. return;
  75. const auto string = fan->name() + " (" + fan->path() + ")";
  76. setData(index(i, 0), string);
  77. emit dataChanged(index(i, 0), index(i, 0));
  78. }
  79. void PwmFanModel::updateFan()
  80. {
  81. const auto fan = qobject_cast<PwmFan*>(sender());
  82. if (!fan)
  83. return;
  84. const auto i = m_fans.indexOf(fan);
  85. if (i == -1)
  86. return;
  87. const auto string = fan->name() + " (" + fan->path() + ")";
  88. setData(index(i, 0), string);
  89. emit dataChanged(index(i, 0), index(i, 0));
  90. }
  91. QList<QObject *> PwmFanModel::fans() const
  92. {
  93. auto list = QList<QObject *>();
  94. for (const auto &fan : m_fans)
  95. list << fan;
  96. return list;
  97. }
  98. }