pwmfanmodel.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman maldela@halloarsch.de
  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. m_fans = fans;
  32. emit fansChanged();
  33. QStringList list;
  34. foreach (PwmFan *const fan, fans)
  35. {
  36. connect(fan, SIGNAL(nameChanged()), this, SLOT(updateFan()));
  37. list << fan->name() + " (" + fan->path() + ")";
  38. }
  39. setStringList(list);
  40. }
  41. void PwmFanModel::addPwmFans(const QList<PwmFan *> &fans)
  42. {
  43. if (!fans.isEmpty())
  44. {
  45. m_fans += fans;
  46. emit fansChanged();
  47. int oldSize = rowCount();
  48. insertRows(oldSize, fans.size());
  49. foreach (PwmFan *const fan, fans)
  50. {
  51. connect(fan, SIGNAL(nameChanged()), this, SLOT(updateFan()));
  52. updateFan(fan);
  53. }
  54. }
  55. }
  56. void PwmFanModel::updateFan(PwmFan *fan)
  57. {
  58. if (!fan)
  59. return;
  60. int i = m_fans.indexOf(fan);
  61. if (i == -1)
  62. return;
  63. QString string = fan->name() + " (" + fan->path() + ")";
  64. setData(index(i, 0), string);
  65. emit dataChanged(index(i, 0), index(i, 0));
  66. }
  67. void PwmFanModel::updateFan()
  68. {
  69. PwmFan *fan = qobject_cast<PwmFan *>(sender());
  70. if (!fan)
  71. return;
  72. int i = m_fans.indexOf(fan);
  73. if (i == -1)
  74. return;
  75. QString string = fan->name() + " (" + fan->path() + ")";
  76. setData(index(i, 0), string);
  77. emit dataChanged(index(i, 0), index(i, 0));
  78. }
  79. QList<QObject *> PwmFanModel::fans() const
  80. {
  81. QList<QObject *> list;
  82. foreach(PwmFan *const fan, m_fans)
  83. list << qobject_cast<QObject *>(fan);
  84. return list;
  85. }
  86. }