mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2025-02-25 18:55:24 -06:00
get text for AssignableItem in a separate function
This commit is contained in:
parent
ebef4fefdf
commit
03f765c040
@ -146,41 +146,7 @@ QStandardItem *DeviceModel::createAssignable(
|
|||||||
ifaceItem->setData(v, AssignableRole);
|
ifaceItem->setData(v, AssignableRole);
|
||||||
|
|
||||||
// Set initial text to current value (one-time at startup)
|
// Set initial text to current value (one-time at startup)
|
||||||
QString text("No value set");
|
ifaceItem->setText(displayText(proxy, itemData));
|
||||||
auto unit = itemData.unit();
|
|
||||||
auto currentValue = proxy->currentValue();
|
|
||||||
|
|
||||||
if (currentValue.has_value()) {
|
|
||||||
p::match(itemData.assignableInfo())(
|
|
||||||
pattern(as<EnumerationVec>(arg)) =
|
|
||||||
[&](auto e_vec) {
|
|
||||||
/* Find index from EnumVec (O(n) doesn't matter since we only search
|
|
||||||
once here) This should never be anything other than an uint but
|
|
||||||
in theory it could be whatever at this point */
|
|
||||||
try {
|
|
||||||
auto index = std::get<uint>(currentValue.value());
|
|
||||||
for (auto &e : e_vec) {
|
|
||||||
if (index == e.key) {
|
|
||||||
text = QString::fromStdString(e.name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (std::bad_variant_access &e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
// text =
|
|
||||||
// QString::fromStdString(e_vec[std::get<uint>(currentValue.value())].name);
|
|
||||||
},
|
|
||||||
pattern(as<RangeInfo>(_)) =
|
|
||||||
[&]() {
|
|
||||||
auto base = fromAssignmentArgument(currentValue.value());
|
|
||||||
if (unit.has_value())
|
|
||||||
text = QString("%1 %2").arg(base, unit.value());
|
|
||||||
else
|
|
||||||
text = base;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
ifaceItem->setText(text);
|
|
||||||
|
|
||||||
connect(ifaceItem, &AssignableItem::assignableDataChanged, [=](QVariant v) {
|
connect(ifaceItem, &AssignableItem::assignableDataChanged, [=](QVariant v) {
|
||||||
// Only show checkbox when value has been changed
|
// Only show checkbox when value has been changed
|
||||||
@ -232,6 +198,40 @@ QStandardItem *DeviceModel::createAssignable(
|
|||||||
return ifaceItem;
|
return ifaceItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DeviceModel::displayText(AssignableProxy *proxy, AssignableItemData data) {
|
||||||
|
auto currentValue = proxy->currentValue();
|
||||||
|
auto defVal = "No value set";
|
||||||
|
if (!currentValue.has_value())
|
||||||
|
return defVal;
|
||||||
|
|
||||||
|
auto a_info = data.assignableInfo();
|
||||||
|
// Try to get text representation of current enum
|
||||||
|
if (std::holds_alternative<EnumerationVec>(a_info) &&
|
||||||
|
std::holds_alternative<uint>(currentValue.value())) {
|
||||||
|
auto enumVec = std::get<EnumerationVec>(a_info);
|
||||||
|
auto index = std::get<uint>(currentValue.value());
|
||||||
|
// Find the key
|
||||||
|
for (auto &e : enumVec) {
|
||||||
|
if (index == e.key) {
|
||||||
|
return QString::fromStdString(e.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: not that helpful message without any name or DBus path
|
||||||
|
qWarning("Tried to get name for invalid Enumeration index %u", index);
|
||||||
|
return defVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 defVal;
|
||||||
|
}
|
||||||
|
|
||||||
QVariant DeviceModel::data(const QModelIndex &index, int role) const {
|
QVariant DeviceModel::data(const QModelIndex &index, int role) const {
|
||||||
if (index.row() != DeviceModel::NameColumn && role == DeviceModel::NodeNameRole) {
|
if (index.row() != DeviceModel::NameColumn && role == DeviceModel::NodeNameRole) {
|
||||||
// Get name from adjacent column
|
// Get name from adjacent column
|
||||||
|
@ -74,6 +74,7 @@ private:
|
|||||||
TC::TreeNode<TCDBus::DeviceNode> node, QDBusConnection conn);
|
TC::TreeNode<TCDBus::DeviceNode> node, QDBusConnection conn);
|
||||||
std::optional<QStandardItem *> setupStaticReadable(
|
std::optional<QStandardItem *> setupStaticReadable(
|
||||||
TC::TreeNode<TCDBus::DeviceNode> node, QDBusConnection conn);
|
TC::TreeNode<TCDBus::DeviceNode> node, QDBusConnection conn);
|
||||||
|
QString displayText(AssignableProxy *proxy, AssignableItemData data);
|
||||||
constexpr int fadeOutTime() { return 5000; } // milliseconds
|
constexpr int fadeOutTime() { return 5000; } // milliseconds
|
||||||
constexpr int transparency() { return 120; } // 0-255
|
constexpr int transparency() { return 120; } // 0-255
|
||||||
// Colors for items
|
// Colors for items
|
||||||
|
Loading…
Reference in New Issue
Block a user