211 lines
3.7 KiB
C++
Executable File
211 lines
3.7 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Implements a QLedMatrix to display a single character
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Florian Schult, SLSE, 2016-03-18
|
|
*/
|
|
|
|
#include "qledchar.h"
|
|
|
|
#include <QFontMetrics>
|
|
#include <QPainter>
|
|
|
|
#define s_fontName QStringLiteral("monospace")
|
|
|
|
QLedChar::QLedChar(int height, QWidget *parent, const QChar &letter)
|
|
: QLedMatrix(parent)
|
|
, m_ptHeight(findPtHeight(height))
|
|
, m_font()
|
|
, m_color()
|
|
, m_bgColor(QColor(34,34,34))
|
|
, m_image()
|
|
, m_size()
|
|
, m_letter(letter)
|
|
, m_isTransparent(false)
|
|
, m_isLetterVisible(true)
|
|
{
|
|
init();
|
|
}
|
|
|
|
void QLedChar::init()
|
|
{
|
|
m_font = QFont(s_fontName, m_ptHeight);
|
|
m_font.setStyleStrategy(QFont::NoAntialias);
|
|
QFontMetrics metrics(m_font);
|
|
|
|
m_size.setWidth(metrics.maxWidth());
|
|
m_size.setHeight(metrics.height());
|
|
setSize(m_size);
|
|
}
|
|
|
|
QChar QLedChar::letter() const
|
|
{
|
|
return m_letter;
|
|
}
|
|
|
|
QLedChar::~QLedChar()
|
|
{
|
|
}
|
|
|
|
void QLedChar::setLetter(const QChar &letter)
|
|
{
|
|
m_letter = letter;
|
|
|
|
if (!m_isLetterVisible)
|
|
{
|
|
for(int row = 0; row < m_size.height(); ++row)
|
|
{
|
|
for(int col = 0; col < m_size.width(); ++col)
|
|
{
|
|
setColorAt(row, col, NoColor);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
QImage image(m_size,QImage::Format_RGB32);
|
|
QPainter painter;
|
|
painter.begin(&image);
|
|
painter.setFont(m_font);
|
|
painter.fillRect(image.rect(), m_bgColor);
|
|
painter.setPen(m_color);
|
|
painter.drawText(QPoint(0,m_ptHeight), letter);
|
|
painter.end();
|
|
|
|
setImage(image);
|
|
drawImage();
|
|
}
|
|
|
|
void QLedChar::drawImage()
|
|
{
|
|
for(int row = 0; row < m_size.height(); ++row)
|
|
{
|
|
for(int col = 0; col < m_size.width(); ++col)
|
|
{
|
|
setColorAt(row, col, m_image.pixel(col, row));
|
|
}
|
|
}
|
|
}
|
|
|
|
QImage QLedChar::image() const
|
|
{
|
|
return m_image;
|
|
}
|
|
|
|
void QLedChar::setImage(const QImage &image)
|
|
{
|
|
m_image = image;
|
|
drawImage();
|
|
}
|
|
|
|
int QLedChar::findPtHeight(int maxPxHeight)
|
|
{
|
|
int bestPtHeight = 0;
|
|
// Starting difference, should be at least 0.35
|
|
float difference = 0.4f;
|
|
float differenceStep = difference / 2;
|
|
bool heightFound = false;
|
|
|
|
while(!heightFound)
|
|
{
|
|
differenceStep /= 2;
|
|
float ptHeight = maxPxHeight - (maxPxHeight * difference);
|
|
|
|
QFont font(s_fontName,ptHeight);
|
|
QFontMetrics metrics(font);
|
|
|
|
int pxHeight = metrics.height();
|
|
|
|
if(pxHeight < maxPxHeight)
|
|
difference -= differenceStep;
|
|
else if(pxHeight > maxPxHeight)
|
|
difference += differenceStep;
|
|
else
|
|
{
|
|
bestPtHeight = ptHeight;
|
|
heightFound = true;
|
|
}
|
|
|
|
if(differenceStep == 0)
|
|
{
|
|
// Catches invalid pixelsizes, which lack proper pointsizes
|
|
bestPtHeight = ptHeight-1;
|
|
heightFound = true;
|
|
}
|
|
}
|
|
return bestPtHeight;
|
|
}
|
|
|
|
QSize QLedChar::charSize() const
|
|
{
|
|
return m_size;
|
|
}
|
|
|
|
void QLedChar::setLetterVisible(bool visible)
|
|
{
|
|
m_isLetterVisible = visible;
|
|
setLetter(m_letter);
|
|
}
|
|
|
|
bool QLedChar::isTransparent() const
|
|
{
|
|
return m_isTransparent;
|
|
}
|
|
|
|
void QLedChar::setTransparent(bool isTransparent)
|
|
{
|
|
m_isTransparent = isTransparent;
|
|
if (isTransparent)
|
|
setBackgroundMode(Qt::TransparentMode);
|
|
else
|
|
setBackgroundMode(Qt::OpaqueMode);
|
|
}
|
|
|
|
bool QLedChar::letterVisible() const
|
|
{
|
|
return m_isLetterVisible;
|
|
}
|
|
|
|
void QLedChar::setCharHeight(int height)
|
|
{
|
|
m_ptHeight = findPtHeight(height);
|
|
init();
|
|
}
|
|
|
|
int QLedChar::charHeight() const
|
|
{
|
|
return m_size.height();
|
|
}
|
|
|
|
QColor QLedChar::color() const
|
|
{
|
|
return m_color;
|
|
}
|
|
|
|
void QLedChar::setColor(const QColor &color)
|
|
{
|
|
m_color = color;
|
|
setLetter(m_letter);
|
|
}
|
|
|
|
QColor QLedChar::bgColor() const
|
|
{
|
|
return m_bgColor;
|
|
}
|
|
|
|
void QLedChar::setBgColor(const QColor &bgColor)
|
|
{
|
|
m_bgColor = bgColor;
|
|
setLetter(m_letter);
|
|
}
|
|
|
|
void QLedChar::onFlashTimerTimeout()
|
|
{
|
|
m_isLetterVisible = !m_isLetterVisible;
|
|
setLetter(m_letter);
|
|
}
|
|
|
|
|