Sfoglia il codice sorgente

Better internal sorting of hwmons, fans and temps. Should solve a number of issues resulting from bad sorting.

Malte Veerman 6 anni fa
parent
commit
32c208c7d6

+ 6 - 1
import/src/loader.cpp

@@ -625,8 +625,11 @@ QString Loader::createConfig() const
             usedHwmons << hwmon;
 
         const auto pwmFans = hwmon->pwmFans();
-        for (const auto &pwmFan : pwmFans)
+        auto keys = pwmFans.keys();
+        std::sort(keys.begin(), keys.end());
+        for (const auto &key : keys)
         {
+            const auto pwmFan = pwmFans.value(key);
             if (pwmFan->hasTemp() && pwmFan->temp() && !pwmFan->testing())
             {
                 usedFans << pwmFan;
@@ -636,6 +639,8 @@ QString Loader::createConfig() const
         }
     }
 
+    std::sort(usedHwmons.begin(), usedHwmons.end(), [] (Hwmon *a, Hwmon *b) { return a->index() < b->index(); });
+
     auto configFile = QStringLiteral("# This file was created by Fancontrol-GUI") + QChar(QChar::LineFeed);
 
     if (m_interval != 0)

+ 26 - 22
import/src/pwmfanmodel.cpp

@@ -21,6 +21,7 @@
 
 
 #include "pwmfanmodel.h"
+#include "hwmon.h"
 #include "pwmfan.h"
 
 
@@ -69,46 +70,49 @@ QVariant PwmFanModel::data(const QModelIndex& index, int role) const
     }
 }
 
-void PwmFanModel::setPwmFans(const QList<PwmFan *> &fans)
+void PwmFanModel::setPwmFans(QList<PwmFan *> fans)
 {
+    std::sort(fans.begin(), fans.end(), [] (PwmFan *a, PwmFan *b) { if (a->parent() == b->parent()) return a->index() < b->index(); return a->parent()->name() < b->parent()->name(); });
+
     if (m_fans == fans)
         return;
 
+    beginResetModel();
+
     m_fans = fans;
     emit fansChanged();
 
     for (const auto &fan : fans)
         connect(fan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
+
+    endResetModel();
 }
 
-void PwmFanModel::addPwmFans(QList<PwmFan *> newFans)
+void PwmFanModel::addPwmFan(PwmFan* newFan)
 {
-    for (const auto &newFan : newFans)
+    for (const auto &oldFan : qAsConst(m_fans))
     {
-        for (const auto &fan : qAsConst(m_fans))
-        {
-            if (*fan == *newFan)
-            {
-                newFans.removeAll(newFan);
-                break;
-            }
-        }
+        if (*oldFan == *newFan)
+            return;
     }
 
-    if (!newFans.isEmpty())
-    {
-        m_fans += newFans;
-        emit fansChanged();
+    auto newFans = m_fans << newFan;
+    std::sort(newFans.begin(), newFans.end(), [] (PwmFan *a, PwmFan *b) { if (a->parent() == b->parent()) return a->index() < b->index(); return a->parent()->name() < b->parent()->name(); });
+    int index = newFans.indexOf(newFan);
 
-        const auto oldSize = rowCount();
+    connect(newFan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
 
-        insertRows(oldSize, newFans.size());
+    beginInsertRows(QModelIndex(), index, index);
+    m_fans = newFans;
+    emit fansChanged();
+    endInsertRows();
+}
 
-        for (const auto &fan : qAsConst(newFans))
-        {
-            connect(fan, &PwmFan::nameChanged, this, static_cast<void(PwmFanModel::*)()>(&PwmFanModel::updateFan));
-            updateFan(fan);
-        }
+void PwmFanModel::addPwmFans(const QList<PwmFan *> &newFans)
+{
+    for (const auto &newFan : newFans)
+    {
+        addPwmFan(newFan);
     }
 }
 

+ 3 - 2
import/src/pwmfanmodel.h

@@ -47,8 +47,9 @@ public:
     Q_ENUM(Roles)
 
     PwmFanModel(QObject *parent = Q_NULLPTR);
-    void setPwmFans(const QList<PwmFan *> &fans);
-    void addPwmFans(QList<PwmFan *> newFans);
+    void setPwmFans(QList<PwmFan *> fans);
+    void addPwmFan(PwmFan *newFan);
+    void addPwmFans(const QList<PwmFan *> &newFans);
     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override { Q_UNUSED(parent) return m_fans.size(); }
     virtual QVariant data(const QModelIndex &index, int role = DisplayRole) const override;
     virtual QHash<int, QByteArray> roleNames() const override;

+ 25 - 24
import/src/tempmodel.cpp

@@ -21,6 +21,7 @@
 
 
 #include "tempmodel.h"
+#include "hwmon.h"
 #include "temp.h"
 
 
@@ -70,8 +71,13 @@ QVariant TempModel::data(const QModelIndex& index, int role) const
     }
 }
 
-void TempModel::setTemps(const QList<Temp *> &temps)
+void TempModel::setTemps(QList<Temp *> temps)
 {
+    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(); });
+
+    if (m_temps == temps)
+        return;
+
     beginResetModel();
 
     m_temps = temps;
@@ -86,36 +92,31 @@ void TempModel::setTemps(const QList<Temp *> &temps)
     endResetModel();
 }
 
-void TempModel::addTemps(QList<Temp *> newTemps)
+void TempModel::addTemp(Temp *newTemp)
 {
-
-    for (const auto &newTemp : newTemps)
+    for (const auto &oldTemp : qAsConst(m_temps))
     {
-        for (const auto &temp : qAsConst(m_temps))
-        {
-            if (*temp == *newTemp)
-            {
-                newTemps.removeAll(newTemp);
-                break;
-            }
-        }
+        if (*oldTemp == *newTemp)
+            return;
     }
 
-    if (!newTemps.isEmpty())
-    {
-        beginResetModel();
+    auto newTemps = m_temps << newTemp;
+    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(); });
+    int index = newTemps.indexOf(newTemp);
 
-        m_temps += newTemps;
-        emit tempsChanged();
+    connect(newTemp, &Temp::nameChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
 
-        for (const auto &temp : qAsConst(newTemps))
-        {
-            connect(temp, &Temp::nameChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
-            connect(temp, &Temp::valueChanged, this, static_cast<void(TempModel::*)()>(&TempModel::updateTemp));
-            updateTemp(temp);
-        }
+    beginInsertRows(QModelIndex(), index, index);
+    m_temps = newTemps;
+    emit tempsChanged();
+    endInsertRows();
+}
 
-        endResetModel();
+void TempModel::addTemps(const QList<Temp *> &newTemps)
+{
+    for (const auto &newTemp : newTemps)
+    {
+        addTemp(newTemp);
     }
 }
 

+ 3 - 2
import/src/tempmodel.h

@@ -48,8 +48,9 @@ public:
     Q_ENUM(Roles)
 
     TempModel(QObject *parent = Q_NULLPTR);
-    void setTemps(const QList<Temp *> &temps);
-    void addTemps(QList<Temp *> newTemps);
+    void setTemps(QList<Temp *> temps);
+    void addTemp(Temp *newTemp);
+    void addTemps(const QList<Temp *> &newTemps);
     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override { Q_UNUSED(parent) return m_temps.size(); }
     virtual QVariant data(const QModelIndex &index, int role = DisplayRole) const override;
     virtual QHash<int, QByteArray> roleNames() const override;