130 lines
2.3 KiB
C++
Executable File
130 lines
2.3 KiB
C++
Executable File
|
|
|
|
|
|
#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);
|
|
}
|
|
|
|
}
|