show change of state in items

This commit is contained in:
Jussi Kuokkanen 2023-09-06 19:22:36 +03:00
parent b149159d27
commit 1722d6226e
3 changed files with 90 additions and 21 deletions

View File

@ -18,23 +18,67 @@ void AssignableItem::setData(const QVariant &v, int role) {
}
if (role == Qt::CheckStateRole) {
bool state = v.toBool();
m_committed = state;
emit committalChanged(state);
}
QStandardItem::setData(v, role);
}
void AssignableItem::updateText(AssignableItemData data) {
if (data.value().canConvert<DynamicReadableConnectionData>()) {
setText("(Parametrized)");
QString AssignableItem::appendUnit(QString text) {
if (m_unit.has_value())
return QString{"%1 %2"}.arg(text, m_unit.value());
return text;
}
void AssignableItem::setCurrentValueText(QString text) {
m_currentValueText = text;
QString targetText;
// Don't show unit when target is parametrization
if (m_currentTargetData.parametrizationText.has_value()) {
targetText = m_currentTargetData.parametrizationText.value();
} else if (m_currentTargetData.valueText.has_value()) {
targetText = appendUnit(m_currentTargetData.valueText.value());
} else {
setText(appendUnit(text));
return;
}
setText(QString{"%1 -> %2"}.arg(appendUnit(text), targetText));
}
void AssignableItem::applyTargetText() {
if (m_currentTargetData.valueText.has_value()) {
auto newCurrent = m_currentTargetData.valueText.value();
setText(appendUnit(newCurrent));
m_currentValueText = newCurrent;
}
m_currentTargetData.valueText = std::nullopt;
m_currentTargetData.parametrizationText = std::nullopt;
}
void AssignableItem::clearTargetText() {
updateText(m_currentValueText);
m_currentTargetData.valueText = std::nullopt;
m_currentTargetData.parametrizationText = std::nullopt;
}
void AssignableItem::updateText(QString &text) { setText(appendUnit(text)); }
void AssignableItem::updateText(AssignableItemData data) {
QString targetText;
if (data.value().canConvert<DynamicReadableConnectionData>()) {
// Include possible unit
auto target = "(Parametrized)";
m_currentTargetData.parametrizationText = target;
targetText = target;
goto ret;
}
if (std::holds_alternative<RangeInfo>(data.assignableInfo())) {
if (m_unit.has_value())
setText(QString{"%1 %2"}.arg(data.value().toString(), m_unit.value()));
else
setText(data.value().toString());
return;
targetText = appendUnit(data.value().toString());
m_currentTargetData.valueText = data.value().toString();
goto ret;
}
if (std::holds_alternative<EnumerationVec>(data.assignableInfo())) {
@ -42,11 +86,15 @@ void AssignableItem::updateText(AssignableItemData data) {
auto enumVec = std::get<EnumerationVec>(data.assignableInfo());
if (enumVec.size() - 1 >= index) {
setText(QString::fromStdString(enumVec[index].name));
targetText = QString::fromStdString(enumVec[index].name);
m_currentTargetData.valueText = QString::fromStdString(enumVec[index].name);
} else {
// This could arise from the settings being edited manually
qWarning("Trying to set Enumeration with invalid index %u!", index);
setText(QString::number(index));
m_currentTargetData.valueText = QString::number(index);
targetText = QString::number(index);
}
}
ret:
setText(QString{"%1 -> %2"}.arg(appendUnit(m_currentValueText), targetText));
}

View File

@ -8,6 +8,12 @@
#include <QObject>
#include <QStandardItem>
// We don't want do show unit after parametrization indicator, even when it exists
struct TargetData {
std::optional<QString> valueText;
std::optional<QString> parametrizationText;
};
// Class for forwarding changes in DeviceModel's assignables
// Forgive me for the sin of multiple inheritance
@ -21,6 +27,12 @@ public:
// Whether or not the set value shall be applied. Doesn't reset or change it.
void setCommittal(bool on) { m_committed = on; }
void setData(const QVariant &v, int role = Qt::UserRole + 1);
// Should be called when value is changed successfully
void setCurrentValueText(QString);
void applyTargetText();
void clearTargetText();
// Update with a value, this will add unit
void updateText(QString &);
signals:
void assignableDataChanged(QVariant value);
void committalChanged(bool on);
@ -28,7 +40,11 @@ private:
Q_OBJECT
void updateText(AssignableItemData);
QString appendUnit(QString);
std::optional<QString> m_unit;
bool m_committed = false;
TargetData m_currentTargetData;
QString m_currentValueText;
};

View File

@ -131,9 +131,16 @@ QStandardItem *DeviceModel::createAssignable(
[=](auto v) {
QVariant data;
data.setValue(connectionColor());
ifaceItem->setData(data, Qt::BackgroundRole);
ifaceItem->setText(text);
// qDebug() << text;
if (!ifaceItem->committal()) {
// Don't set color when committed
ifaceItem->clearTargetText();
ifaceItem->setData(data, Qt::BackgroundRole);
}
// TODO: small annoyance: causes 10 W -> 11 W
// instead of desired 10 -> 11 W
// when committing
ifaceItem->setCurrentValueText(text);
},
pattern(_) = [] {});
});
@ -146,7 +153,8 @@ QStandardItem *DeviceModel::createAssignable(
ifaceItem->setData(v, AssignableRole);
// Set initial text to current value (one-time at startup)
ifaceItem->setText(displayText(proxy, itemData));
// Related to TODO above
ifaceItem->setCurrentValueText(displayText(proxy, itemData));
connect(ifaceItem, &AssignableItem::assignableDataChanged, [=](QVariant v) {
// Only show checkbox when value has been changed
@ -162,6 +170,8 @@ QStandardItem *DeviceModel::createAssignable(
});
connect(proxy, &AssignableProxy::applied, [=](auto err) {
ifaceItem->applyTargetText();
// Fade out result color
auto startColor = (err.has_value()) ? errorColor() : successColor();
auto anim = new QVariantAnimation;
@ -188,8 +198,7 @@ QStandardItem *DeviceModel::createAssignable(
if (ifaceItem->checkState() == Qt::Checked) {
proxy->apply();
} else {
// Note: gets current value, which something else might have changed
ifaceItem->setText(displayText(proxy, itemData));
ifaceItem->clearTargetText();
}
// Make unchecked item uncheckable too
// TODO: do this as soon as item is unchecked, haven't found a good way so far
@ -226,11 +235,7 @@ QString DeviceModel::displayText(AssignableProxy *proxy, AssignableItemData data
auto unit = data.unit();
if (std::holds_alternative<RangeInfo>(a_info)) {
auto valueText = fromAssignmentArgument(currentValue.value());
if (unit.has_value())
return QString("%1 %2").arg(valueText, unit.value());
else
return valueText;
return fromAssignmentArgument(currentValue.value());
}
return defVal;
}