tempmodel.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "tempmodel.h"
  22. #include "hwmon.h"
  23. #include "temp.h"
  24. namespace Fancontrol
  25. {
  26. TempModel::TempModel(QObject *parent) : QAbstractListModel(parent),
  27. m_unit(QStringLiteral("°C"))
  28. {
  29. }
  30. QHash<int, QByteArray> TempModel::roleNames() const
  31. {
  32. QHash<int, QByteArray> roleNames;
  33. roleNames.insert(DisplayRole, "display");
  34. roleNames.insert(ObjectRole, "object");
  35. return roleNames;
  36. }
  37. QVariant TempModel::data(const QModelIndex& index, int role) const
  38. {
  39. if (!index.isValid())
  40. return QVariant();
  41. const int row = index.row();
  42. if (row >= rowCount())
  43. return QVariant();
  44. const auto temp = m_temps.at(row);
  45. if (!temp)
  46. return QVariant();
  47. switch (role)
  48. {
  49. case DisplayRole:
  50. return temp->name() + ": " + QString::number(temp->value()) + m_unit + " (" + temp->path() + ")";
  51. case ObjectRole:
  52. return QVariant::fromValue(temp);
  53. default:
  54. return QVariant();
  55. }
  56. }
  57. void TempModel::setTemps(QList<Temp *> temps)
  58. {
  59. std::sort(temps.begin(), temps.end(), [] (Temp *a, Temp *b) { if (a->parent() == b->parent()) return a->index() < b->index(); return a->parent()->name() < b->parent()->name(); });
  60. if (m_temps == temps)
  61. return;
  62. beginResetModel();
  63. m_temps = temps;
  64. emit tempsChanged();
  65. for (const auto &temp : temps)
  66. {
  67. connect(temp, &Temp::nameChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
  68. connect(temp, &Temp::valueChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
  69. }
  70. endResetModel();
  71. }
  72. void TempModel::addTemp(Temp *newTemp)
  73. {
  74. for (const auto &oldTemp : qAsConst(m_temps))
  75. {
  76. if (*oldTemp == *newTemp)
  77. return;
  78. }
  79. auto newTemps = m_temps << newTemp;
  80. std::sort(newTemps.begin(), newTemps.end(), [] (Temp *a, Temp *b) { if (a->parent() == b->parent()) return a->index() < b->index(); return a->parent()->name() < b->parent()->name(); });
  81. int index = newTemps.indexOf(newTemp);
  82. connect(newTemp, &Temp::nameChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
  83. beginInsertRows(QModelIndex(), index, index);
  84. m_temps = newTemps;
  85. emit tempsChanged();
  86. endInsertRows();
  87. }
  88. void TempModel::addTemps(const QList<Temp *> &newTemps)
  89. {
  90. for (const auto &newTemp : newTemps)
  91. {
  92. addTemp(newTemp);
  93. }
  94. }
  95. void TempModel::updateTemp(Temp *temp)
  96. {
  97. if (!temp)
  98. return;
  99. const auto i = m_temps.indexOf(temp);
  100. if (i == -1)
  101. return;
  102. emit dataChanged(index(i, 0), index(i, 0), QVector<int>{ DisplayRole });
  103. }
  104. void TempModel::updateTemp()
  105. {
  106. const auto temp = qobject_cast<Temp *>(sender());
  107. updateTemp(temp);
  108. }
  109. void TempModel::updateAll()
  110. {
  111. emit dataChanged(index(0, 0), index(m_temps.size(), 0), QVector<int>{ DisplayRole });
  112. }
  113. QObject * TempModel::temp(int index) const
  114. {
  115. return m_temps.value(index);
  116. }
  117. int TempModel::indexOf(QObject* temp) const
  118. {
  119. return m_temps.indexOf(qobject_cast<Temp*>(temp));
  120. }
  121. }