tempmodel.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "tempmodel.h"
  23. #include "temp.h"
  24. namespace Fancontrol
  25. {
  26. TempModel::TempModel(QObject *parent) : QStringListModel(parent),
  27. m_unit(QStringLiteral("°C"))
  28. {
  29. }
  30. QString TempModel::composeText(Temp *temp)
  31. {
  32. return temp->name() + ": " + QString::number(temp->value()) + m_unit + " (" + temp->path() + ")";
  33. }
  34. void TempModel::setTemps(const QList<Temp *> &temps)
  35. {
  36. m_temps = temps;
  37. emit tempsChanged();
  38. QStringList list;
  39. for (const auto &temp : temps)
  40. {
  41. connect(temp, &Temp::nameChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
  42. connect(temp, &Temp::valueChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
  43. list << composeText(temp);
  44. }
  45. setStringList(list);
  46. }
  47. void TempModel::addTemps(QList<Temp *> newTemps)
  48. {
  49. for (const auto &newTemp : newTemps)
  50. {
  51. for (const auto &temp : m_temps)
  52. {
  53. if (*temp == *newTemp)
  54. {
  55. newTemps.removeAll(newTemp);
  56. break;
  57. }
  58. }
  59. }
  60. if (!newTemps.isEmpty())
  61. {
  62. m_temps += newTemps;
  63. emit tempsChanged();
  64. const auto oldSize = rowCount();
  65. insertRows(oldSize, newTemps.size());
  66. for (const auto &temp : newTemps)
  67. {
  68. connect(temp, &Temp::nameChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
  69. connect(temp, &Temp::valueChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
  70. updateTemp(temp);
  71. }
  72. }
  73. }
  74. void TempModel::updateTemp(Temp *temp)
  75. {
  76. if (!temp)
  77. return;
  78. const auto i = m_temps.indexOf(temp);
  79. if (i == -1)
  80. return;
  81. setData(index(i, 0), composeText(temp));
  82. emit dataChanged(index(i, 0), index(i, 0));
  83. }
  84. void TempModel::updateTemp()
  85. {
  86. const auto temp = qobject_cast<Temp *>(sender());
  87. updateTemp(temp);
  88. }
  89. void TempModel::updateAll()
  90. {
  91. for (int i=0; i<m_temps.size(); i++)
  92. setData(index(i, 0), composeText(m_temps.at(i)));
  93. emit dataChanged(index(0, 0), index(m_temps.size(), 0));
  94. }
  95. QList<QObject *> TempModel::temps() const
  96. {
  97. QList<QObject *> list;
  98. for (const auto &temp : m_temps)
  99. list << qobject_cast<QObject *>(temp);
  100. return list;
  101. }
  102. }