Files
qt5-legatus-legacy/Lts/Tools/QLedLine/qledline.cpp
T
2026-08-13 18:33:10 +02:00

224 lines
4.4 KiB
C++
Executable File

/**
* @RDE_HEADER@
* Implements n QLedChars to display a line of Text/Image
* $Id$
* $HeadURL$
* $Author$
* Created by Florian Schult, SLSE, 2016-03-18
*/
#include "qledline.h"
#include <QString>
#include <QLayout>
#include <QTimer>
#include <QImage>
#include "qledchar.h"
QLedLine::QLedLine(QWidget *parent)
: QWidget(parent)
, m_charCount(0)
, m_height(0)
, m_visible(true)
, m_text()
, m_layout(new QHBoxLayout(this))
, m_chars()
, m_flashTimer(new QTimer(this))
, m_color(Qt::red) // Standard color
, m_image()
{
m_layout->setSpacing(0);
m_layout->setContentsMargins(0,0,0,0);
m_layout->setMargin(0);
QSpacerItem *sp1 = new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding);
QSpacerItem *sp2 = new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding);
m_layout->addSpacerItem(sp1);
m_layout->addSpacerItem(sp2);
m_layout->setAlignment(m_layout, Qt::AlignHCenter);
}
QLedLine::~QLedLine()
{
qDeleteAll(m_chars);
// optional. They have "this" as parent and are QT.
delete m_layout;
delete m_flashTimer;
}
QString QLedLine::shownText() const
{
return m_text.leftJustified(m_chars.size(), QLatin1Char(' '), true);
}
bool QLedLine::isTextVisible() const
{
return m_visible;
}
int QLedLine::flashTime() const
{
return m_flashTimer->interval();
}
QColor QLedLine::color() const
{
return m_color;
}
void QLedLine::setText(const QString &text)
{
m_text = text;
// Todo: Arabic: RightJustified? Automatic?
QString shown = shownText();
for (int i=0; i<m_chars.size(); ++i)
m_chars[i]->setLetter(shown[i]);
}
// Expects a properly sized QImage, corresponding to the count and Pixelsizes of QLedLine and QLedChar
void QLedLine::setImage(const QImage &image)
{
m_image = image;
QImage charImage;
QSize charSize = m_chars[0]->charSize();
for (int i=0; i<m_chars.size(); ++i)
{
charImage = m_image.copy((charSize.width() * i), 0, charSize.width(), charSize.height());
m_chars[i]->setImage(charImage);
}
}
QSize QLedLine::charSize() const
{
return m_chars[0]->charSize();
}
QString QLedLine::text() const
{
return m_text;
}
void QLedLine::addChar()
{
QLedChar *ledChar = new QLedChar(m_height, this, m_text[m_chars.size()]);
ledChar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
if (!m_chars.isEmpty())
ledChar->setLetterVisible(m_chars[0]->letterVisible());
connect(m_flashTimer, &QTimer::timeout, ledChar, &QLedChar::onFlashTimerTimeout);
ledChar->setColor(m_color);
// Takes the last stretch-item and puts it behind the new added ledChar
QLayoutItem *spacer = m_layout->takeAt(m_layout->count()-1);
m_layout->addWidget(ledChar);
m_layout->addItem(spacer);
m_chars.append(ledChar);
ledChar->setLetter(shownText().at(m_chars.size()-1));
}
void QLedLine::deleteChar()
{
if (m_chars.isEmpty())
return;
delete m_chars.last();
m_chars.remove(m_chars.size()-1);
}
void QLedLine::setCharCount(int charCount)
{
m_charCount = charCount;
while(m_chars.size() != m_charCount)
{
if(m_chars.size() > m_charCount)
deleteChar();
else if(m_chars.size() < m_charCount)
addChar();
}
}
int QLedLine::charCount() const
{
return m_charCount;
}
void QLedLine::setHeight(int height)
{
m_height = height;
for(int i=0; i<m_chars.size(); ++i)
{
m_chars[i]->setCharHeight(m_height);
}
setText(m_text);
}
int QLedLine::height() const
{
return m_height;
}
void QLedLine::setCharCountAndHeight(int charCount, int height)
{
setHeight(height);
setCharCount(charCount);
}
void QLedLine::setColor(const QColor &color)
{
m_color = color;
for (int i=0; i<m_charCount; ++i)
m_chars[i]->setColor(color);
}
void QLedLine::clear()
{
for (int i=0; i<m_charCount; ++i)
{
m_chars[i]->setLetterVisible(false);
m_chars[i]->setLetterVisible(true);
}
}
void QLedLine::setFlashing(int msecs)
{
m_flashTimer->setInterval(msecs);
if (msecs == 0)
{
if (m_visible)
{
for (int i = 0; i < m_chars.size(); ++i)
m_chars[i]->setLetterVisible();
}
m_flashTimer->stop();
}
else
{
if(m_visible)
m_flashTimer->start();
else
m_flashTimer->stop();
}
}
void QLedLine::setTextVisible(bool visible)
{
m_visible = visible;
for (int i=0; i<m_chars.size(); ++i)
m_chars[i]->setLetterVisible(m_visible);
if(m_visible)
{
if(m_flashTimer->interval() > 0)
m_flashTimer->start();
}
else
{
m_flashTimer->stop();
}
}