mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2025-02-25 18:55:24 -06:00
qt: show DynamicReadable units
This commit is contained in:
parent
dc29d5640c
commit
c938f05228
@ -140,23 +140,35 @@ std::optional<QStandardItem*> DeviceModel::setupAssignable(
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void updateReadItemText(QStandardItem *item, T value,
|
||||
std::optional<QString> unit) {
|
||||
// TODO: this can be made a lot (around 3x) faster by using direct copying
|
||||
// Form a string of the form "1000 MHz" if has unit
|
||||
auto text = (unit.has_value()) ?
|
||||
QString("%1 %2").arg(value).arg(unit.value()) :
|
||||
QString("%1").arg(value);
|
||||
item->setText(text);
|
||||
}
|
||||
|
||||
std::optional<QStandardItem*> DeviceModel::setupDynReadable(
|
||||
TC::TreeNode<TCDBus::DeviceNode> node, QDBusConnection conn) {
|
||||
auto item = new QStandardItem;
|
||||
auto proxy = new DynamicReadableProxy(node.value().path, conn, this);
|
||||
auto unit = proxy->unit();
|
||||
|
||||
connect(proxy, &DynamicReadableProxy::valueChanged, [=](ReadResult res) {
|
||||
p::match(res)(
|
||||
pattern(as<ReadableValue>(arg)) = [=](auto rv) {
|
||||
p::match(rv)(
|
||||
pattern(as<double>(arg)) = [=](auto d) {
|
||||
item->setText(QString::number(d));
|
||||
updateReadItemText(item, d, unit);
|
||||
},
|
||||
pattern(as<int>(arg)) = [=](auto i) {
|
||||
item->setText(QString::number(i));
|
||||
updateReadItemText(item, i, unit);
|
||||
},
|
||||
pattern(as<uint>(arg)) = [=](auto u) {
|
||||
item->setText(QString::number(u));
|
||||
updateReadItemText(item, u, unit);
|
||||
},
|
||||
pattern(_) = []{}
|
||||
);
|
||||
|
@ -9,9 +9,7 @@ namespace TCD = TuxClocker::DBus;
|
||||
using namespace TuxClocker::Device;
|
||||
|
||||
Q_DECLARE_METATYPE(TCD::Result<QDBusVariant>)
|
||||
Q_DECLARE_METATYPE(ReadableValue)
|
||||
Q_DECLARE_METATYPE(ReadError)
|
||||
Q_DECLARE_METATYPE(ReadResult)
|
||||
Q_DECLARE_METATYPE(TCD::Result<QString>)
|
||||
|
||||
ReadResult toTCResult(TCD::Result<QDBusVariant> res) {
|
||||
if (res.error)
|
||||
@ -37,6 +35,7 @@ DynamicReadableProxy::DynamicReadableProxy(QString path, QDBusConnection conn,
|
||||
QObject *parent) : QObject(parent),
|
||||
m_iface("org.tuxclocker", path, "org.tuxclocker.DynamicReadable", conn) {
|
||||
qDBusRegisterMetaType<TCD::Result<QDBusVariant>>();
|
||||
qDBusRegisterMetaType<TCD::Result<QString>>();
|
||||
|
||||
m_timer.start(1000);
|
||||
|
||||
@ -50,3 +49,19 @@ DynamicReadableProxy::DynamicReadableProxy(QString path, QDBusConnection conn,
|
||||
emit valueChanged(toTCResult(reply.value()));
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<QString> DynamicReadableProxy::unit() {
|
||||
/* Workaround for QVariant, or whatever errors out braindeath by calling
|
||||
the method instead */
|
||||
QDBusInterface propIface("org.tuxclocker", m_iface.path(),
|
||||
"org.freedesktop.DBus.Properties", m_iface.connection());
|
||||
QDBusReply<QDBusVariant> reply =
|
||||
propIface.call("Get", "org.tuxclocker.DynamicReadable", "unit");
|
||||
if (!reply.isValid()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto arg = reply.value().variant().value<QDBusArgument>();
|
||||
TCD::Result<QString> value;
|
||||
arg >> value;
|
||||
return (value.error) ? std::nullopt : std::optional(value.value);
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ class DynamicReadableProxy : public QObject {
|
||||
public:
|
||||
DynamicReadableProxy(QString path, QDBusConnection conn,
|
||||
QObject *parent = nullptr);
|
||||
std::optional<QString> unit();
|
||||
signals:
|
||||
void valueChanged(TuxClocker::Device::ReadResult val);
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user