Commit 39aa0184 authored by David Redondo's avatar David Redondo 🏎

Allow using ExtendedLegend without setting a chart on it

This will be useful for SensorFaces that are not based on a QuickCharts chart.
We already implicitely require the type of sourceModel to be SensorDataModel
to format the sensor values. Make this explicit by stating the type. Use the data
provided by the model for the legend. The colors can be injected into the model
by the SensorFace to provide them to Legend. If "chart" is set, we continue
using it.
parent 3d0c758f
......@@ -25,6 +25,7 @@ import QtQuick.Layouts 1.1
import org.kde.kirigami 2.8 as Kirigami
import org.kde.ksysguard.faces.private 1.0
import org.kde.ksysguard.formatter 1.0
import org.kde.ksysguard.sensors 1.0
......@@ -41,7 +42,6 @@ ChartsControls.Legend {
* Has no effect
*/
property var colorSource
property var sourceModel
flow: GridLayout.TopToBottom
......@@ -60,6 +60,29 @@ ChartsControls.Legend {
return Formatter.formatValueShowNull(input, sourceModel.data(sourceModel.index(0, index), SensorDataModel.Unit))
}
Binding on model {
when: !chart
value: QTransposeProxyModel {
sourceModel: legend.sourceModel
}
}
Binding on valueRole {
when: !chart
value: "Value"
}
Binding on nameRole {
when: !chart
value: "Name"
}
Binding on shortNameRole {
when: !chart
value: "ShortName"
}
Binding on colorRole {
when: !chart
value: "Color"
}
Repeater {
id: sensorsRepeater
delegate: ChartsControls.LegendDelegate {
......
......@@ -29,6 +29,7 @@
#include <KDeclarative/ConfigPropertyMap>
#include <QQmlEngine>
#include <QTransposeProxyModel>
using namespace KSysGuard;
......@@ -39,4 +40,5 @@ void FacesPlugin::registerTypes(const char *uri)
qmlRegisterType<KSysGuard::SensorFace>(uri, 1, 0, "AbstractSensorFace");
qmlRegisterUncreatableType<KSysGuard::SensorFaceController>(uri, 1, 0, "SensorFaceController", QStringLiteral("It's not possible to create objects of type SensorFaceController"));
qmlRegisterAnonymousType<KDeclarative::ConfigPropertyMap>(uri, 1);
qmlRegisterType<QTransposeProxyModel>("org.kde.ksysguard.faces.private", 1, 0, "QTransposeProxyModel");
}
......@@ -47,6 +47,7 @@ public:
QHash<QString, SensorInfo> sensorInfos;
QHash<QString, QVariant> sensorData;
QVariantMap sensorColors;
bool usedByQml = false;
bool componentComplete = false;
......@@ -125,6 +126,10 @@ QVariant SensorDataModel::data(const QModelIndex &index, int role) const
return info.variantType;
case SensorId:
return sensor;
case Color:
if (!d->sensorColors.empty()) {
return d->sensorColors.value(sensor);
}
default:
break;
}
......@@ -252,6 +257,19 @@ void SensorDataModel::setEnabled(bool newEnabled)
Q_EMIT enabledChanged();
}
QVariantMap SensorDataModel::sensorColors() const
{
return d->sensorColors;
}
void SensorDataModel::setSensorColors(const QVariantMap &sensorColors)
{
if (sensorColors == d->sensorColors) {
return;
}
d->sensorColors = sensorColors;
Q_EMIT sensorColorsChanged();
}
void SensorDataModel::addSensor(const QString &sensorId)
{
......
......@@ -59,6 +59,11 @@ class SENSORS_EXPORT SensorDataModel : public QAbstractTableModel, public QQmlPa
*/
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
/**
* Used by the model to provide data for the Color role if set.
*/
Q_PROPERTY(QVariantMap sensorColors READ sensorColors WRITE setSensorColors NOTIFY sensorColorsChanged)
public:
enum AdditionalRoles {
SensorId = Qt::UserRole + 1, //< The backend path to the sensor.
......@@ -71,6 +76,7 @@ public:
Type, //< The QVariant::Type of the sensor.
Value, //< The value of the sensor.
FormattedValue, //< A formatted string of the value of the sensor.
Color, //< A color of the sensor, if sensorColors is set
};
Q_ENUM(AdditionalRoles)
......@@ -97,6 +103,10 @@ public:
qreal minimum() const;
qreal maximum() const;
QVariantMap sensorColors() const;
void setSensorColors(const QVariantMap &sensorColors);
Q_SIGNAL void sensorColorsChanged();
Q_INVOKABLE void addSensor(const QString &sensorId);
Q_INVOKABLE void removeSensor(const QString &sensorId);
Q_INVOKABLE int column(const QString &sensorId) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment