Added Help attribute to optionally add additiional information to an interface item label (#206)
Dieser Commit ist enthalten in:
Ursprung
3898d69a3d
Commit
457b32a249
@ -50,6 +50,24 @@ QString InterfaceItem::displayName() const
|
||||
return Locale::tr(id);
|
||||
}
|
||||
|
||||
QString InterfaceItem::helpText() const
|
||||
{
|
||||
QString id;
|
||||
|
||||
if (QVariant attr = getAttribute(AttributeName::Help, QVariant()); attr.isValid())
|
||||
id = attr.toString();
|
||||
else if (QVariant displayAttr = getAttribute(AttributeName::DisplayName, QVariant()); displayAttr.isValid())
|
||||
id = displayAttr.toString() + "/help";
|
||||
else
|
||||
id = QString(object().classId()) + ":" + name() + "/help";
|
||||
|
||||
QString translated = Locale::tr(id);
|
||||
if (translated == id)
|
||||
return QString();
|
||||
|
||||
return translated;
|
||||
}
|
||||
|
||||
bool InterfaceItem::hasAttribute(AttributeName name) const
|
||||
{
|
||||
return m_attributes.contains(name);
|
||||
|
||||
@ -47,6 +47,7 @@ class InterfaceItem : public QObject
|
||||
Object& object();
|
||||
const QString& name() const { return m_name; }
|
||||
QString displayName() const;
|
||||
QString helpText() const;
|
||||
|
||||
bool hasAttribute(AttributeName name) const;
|
||||
QVariant getAttribute(AttributeName name, const QVariant& default_) const;
|
||||
|
||||
@ -22,25 +22,81 @@
|
||||
|
||||
#include "interfaceitemnamelabel.hpp"
|
||||
#include "../network/interfaceitem.hpp"
|
||||
#include "../theme/theme.hpp"
|
||||
|
||||
InterfaceItemNameLabel::InterfaceItemNameLabel(InterfaceItem& item, QWidget* parent) :
|
||||
QLabel(parent),
|
||||
m_item{item}
|
||||
InterfaceItemNameLabel::InterfaceItemNameLabel(InterfaceItem& item, QWidget* parent)
|
||||
: QWidget(parent), m_item(item)
|
||||
{
|
||||
setVisible(m_item.getAttributeBool(AttributeName::Visible, true));
|
||||
setText(m_item.displayName());
|
||||
m_label = new QLabel(m_item.displayName(), this);
|
||||
m_label->setVisible(m_item.getAttributeBool(AttributeName::Visible, true));
|
||||
|
||||
QHBoxLayout* layout = new QHBoxLayout(this);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
layout->setSpacing(2);
|
||||
layout->addWidget(m_label);
|
||||
|
||||
// Create help button if needed
|
||||
m_helpBtn = nullptr;
|
||||
const QString help = m_item.helpText();
|
||||
if (!help.isEmpty())
|
||||
{
|
||||
m_helpBtn = new QToolButton(this);
|
||||
m_helpBtn->setIcon(Theme::getIcon("help")); // portable icon
|
||||
m_helpBtn->setToolTip(help);
|
||||
m_helpBtn->setAutoRaise(true);
|
||||
m_helpBtn->setCursor(Qt::PointingHandCursor);
|
||||
m_helpBtn->setIconSize(QSize(12, 12));
|
||||
layout->addWidget(m_helpBtn, 0, Qt::AlignTop);
|
||||
}
|
||||
|
||||
layout->addStretch();
|
||||
|
||||
// Capture layout too
|
||||
connect(&m_item, &InterfaceItem::attributeChanged, this,
|
||||
[this](AttributeName name, const QVariant& value)
|
||||
[this, layout](AttributeName name, const QVariant&)
|
||||
{
|
||||
switch(name)
|
||||
switch (name)
|
||||
{
|
||||
case AttributeName::Visible:
|
||||
setVisible(value.toBool());
|
||||
m_label->setVisible(m_item.getAttributeBool(AttributeName::Visible, true));
|
||||
break;
|
||||
|
||||
case AttributeName::DisplayName:
|
||||
setText(m_item.displayName());
|
||||
case AttributeName::Help:
|
||||
{
|
||||
// Update label text
|
||||
m_label->setText(m_item.displayName());
|
||||
|
||||
// Update help button tooltip
|
||||
if (m_helpBtn)
|
||||
{
|
||||
const QString newHelp = m_item.helpText();
|
||||
if (newHelp.isEmpty())
|
||||
{
|
||||
delete m_helpBtn;
|
||||
m_helpBtn = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_helpBtn->setToolTip(newHelp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const QString newHelp = m_item.helpText();
|
||||
if (!newHelp.isEmpty())
|
||||
{
|
||||
m_helpBtn = new QToolButton(this);
|
||||
m_helpBtn->setIcon(Theme::getIcon("help"));
|
||||
m_helpBtn->setToolTip(newHelp);
|
||||
m_helpBtn->setAutoRaise(true);
|
||||
m_helpBtn->setCursor(Qt::PointingHandCursor);
|
||||
m_helpBtn->setIconSize(QSize(12, 12));
|
||||
layout->insertWidget(1, m_helpBtn, 0, Qt::AlignTop); // insert next to label
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
|
||||
@ -23,16 +23,23 @@
|
||||
#ifndef TRAINTASTIC_CLIENT_WIDGET_INTERFACEITEMNAMELABEL_HPP
|
||||
#define TRAINTASTIC_CLIENT_WIDGET_INTERFACEITEMNAMELABEL_HPP
|
||||
|
||||
#include <QWidget>
|
||||
#include <QLabel>
|
||||
#include <QToolButton>
|
||||
#include <QHBoxLayout>
|
||||
|
||||
class InterfaceItem;
|
||||
|
||||
class InterfaceItemNameLabel : public QLabel
|
||||
class InterfaceItemNameLabel : public QWidget
|
||||
{
|
||||
protected:
|
||||
InterfaceItem& m_item;
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
protected:
|
||||
InterfaceItem& m_item;
|
||||
QLabel* m_label;
|
||||
QToolButton* m_helpBtn;
|
||||
|
||||
public:
|
||||
InterfaceItemNameLabel(InterfaceItem& item, QWidget* parent = nullptr);
|
||||
};
|
||||
|
||||
|
||||
@ -98,6 +98,11 @@ struct Attributes
|
||||
item.addAttribute(AttributeName::DisplayName, value);
|
||||
}
|
||||
|
||||
static inline void addHelp(InterfaceItem& item, std::string_view value)
|
||||
{
|
||||
item.addAttribute(AttributeName::Help, value);
|
||||
}
|
||||
|
||||
static inline void setDisplayName(InterfaceItem& item, std::string_view value)
|
||||
{
|
||||
item.setAttribute(AttributeName::DisplayName, value);
|
||||
|
||||
@ -42,6 +42,7 @@ enum class AttributeName : uint16_t
|
||||
AliasValues = 12,
|
||||
Unit = 14,
|
||||
Step = 15,
|
||||
Help = 16,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren