pwmfanmodel.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman <malte.veerman@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License or (at your option) version 3 or any later version
  9. * accepted by the membership of KDE e.V. (or its successor approved
  10. * by the membership of KDE e.V.), which shall act as a proxy
  11. * defined in Section 14 of version 3 of the license.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "pwmfanmodel.h"
  23. #include "pwmfan.h"
  24. namespace Fancontrol
  25. {
  26. PwmFanModel::PwmFanModel(QObject *parent) : QStringListModel(parent)
  27. {
  28. }
  29. void PwmFanModel::setPwmFans(const QList<PwmFan *> &fans)
  30. {
  31. if (m_fans == fans)
  32. return;
  33. m_fans = fans;
  34. emit fansChanged();
  35. QStringList list;
  36. for (const auto &fan : fans)
  37. {
  38. connect(fan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
  39. list << fan->name() + " (" + fan->path() + ")";
  40. }
  41. setStringList(list);
  42. }
  43. void PwmFanModel::addPwmFans(QList<PwmFan *> newFans)
  44. {
  45. for (const auto &newFan : newFans)
  46. {
  47. for (const auto &fan : m_fans)
  48. {
  49. if (*fan == *newFan)
  50. {
  51. newFans.removeAll(newFan);
  52. break;
  53. }
  54. }
  55. }
  56. if (!newFans.isEmpty())
  57. {
  58. m_fans += newFans;
  59. emit fansChanged();
  60. const auto oldSize = rowCount();
  61. insertRows(oldSize, newFans.size());
  62. for (const auto &fan : newFans)
  63. {
  64. connect(fan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
  65. updateFan(fan);
  66. }
  67. }
  68. }
  69. void PwmFanModel::updateFan(PwmFan *fan)
  70. {
  71. if (!fan)
  72. return;
  73. const auto i = m_fans.indexOf(fan);
  74. if (i == -1)
  75. return;
  76. const auto string = fan->name() + " (" + fan->path() + ")";
  77. setData(index(i, 0), string);
  78. emit dataChanged(index(i, 0), index(i, 0));
  79. }
  80. void PwmFanModel::updateFan()
  81. {
  82. const auto fan = qobject_cast<PwmFan *>(sender());
  83. if (!fan)
  84. return;
  85. const auto i = m_fans.indexOf(fan);
  86. if (i == -1)
  87. return;
  88. const auto string = fan->name() + " (" + fan->path() + ")";
  89. setData(index(i, 0), string);
  90. emit dataChanged(index(i, 0), index(i, 0));
  91. }
  92. QList<QObject *> PwmFanModel::fans() const
  93. {
  94. auto list = QList<QObject *>();
  95. for (const auto &fan : m_fans)
  96. list << qobject_cast<QObject *>(fan);
  97. return list;
  98. }
  99. }