Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Common/LtsWidgets/lts_bitfield_widget.cpp
T
torstenandCopilot d6b6ffce7c Apply clang-format to Lts/Common and DTC/enum headers
- Formatted all CodeGen module files (9 files)
- Formatted LtsAppDescriptor module files
- Formatted LtsAppManager/AppChooser files
- Applied clang-format to all DTC and enum headers across modules
- Removed coordTransTests from CommonUnitTestsPlaceholder (moved to tests/)

Cosmetic changes only, no functional modifications.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-19 14:46:29 +02:00

126 lines
2.5 KiB
C++

#include "lts_bitfield_widget.h"
#include <QPainter>
namespace LtsWidgets
{
LtsBitfieldWidget::LtsBitfieldWidget(QWidget* parent, quint8 value) : QWidget(parent), m_value(value)
{
setObjectName(QString::fromUtf8("LtsBitfieldWidget"));
}
void LtsBitfieldWidget::paintEvent(QPaintEvent* pe)
{
QWidget::paintEvent(pe);
QPainter painter;
painter.begin(this);
painter.setPen(Qt::SolidLine);
if (m_hasCaption)
{
QFont font = painter.font();
font.setPointSizeF((height() / 2.0 > width() / 8.0) ? width() / 8.0 : height() / 2.0);
painter.setFont(font);
}
for (int i = 0; i < 8; ++i)
{
QRectF bitGraphic;
bitGraphic.setLeft(width() / 8.0 * i);
bitGraphic.setWidth((width() / 8.0) - 1);
bitGraphic.setHeight(height() - 1);
bool bitVal = (m_value & (1 << (7 - i))) > 0;
painter.fillRect(bitGraphic, QBrush(bitVal ? m_onColor : m_offColor, Qt::SolidPattern));
if (m_hasCaption)
{
painter.setPen(bitVal ? m_offColor : m_onColor);
painter.drawText(bitGraphic, Qt::AlignCenter, bitVal ? QStringLiteral("1") : QStringLiteral("0"));
}
painter.setPen(Qt::gray);
painter.drawRect(bitGraphic);
}
painter.end();
}
void LtsBitfieldWidget::setHasCaption(bool hasCaption)
{
if (m_hasCaption == hasCaption)
return;
m_hasCaption = hasCaption;
repaint();
emit hasCaptionChanged(hasCaption);
}
void LtsBitfieldWidget::setOnColor(const QColor& onColor)
{
if (m_onColor == onColor)
return;
m_onColor = onColor;
repaint();
}
void LtsBitfieldWidget::setOffColor(const QColor& offColor)
{
if (m_offColor == offColor)
return;
m_offColor = offColor;
repaint();
}
void LtsBitfieldWidget::mousePressEvent(QMouseEvent* event)
{
if (!m_isEditable)
{
QWidget::mousePressEvent(event);
return;
}
for (int i = 0; i < 8; ++i)
{
if (event->pos().x() > width() / 8 * i && event->pos().x() < width() / 8 * (i + 1))
{
m_value ^= (1 << (7 - i));
emit valueChanged(m_value);
break;
}
}
repaint();
}
void LtsBitfieldWidget::setEditable(bool editable)
{
if (m_isEditable == editable)
return;
m_isEditable = editable;
emit isEditableChanged(m_isEditable);
}
void LtsBitfieldWidget::setValue(quint8 value)
{
if (m_value == value)
return;
m_value = value;
repaint();
emit valueChanged(m_value);
}
} // namespace LtsWidgets