promoe/TextBar.cpp

186 lines
3.4 KiB
C++

#include "MainWindow.h"
#include "Display.h"
#include "TextBar.h"
TextScroller::TextScroller (QWidget *parent, uint w, uint h) : QWidget (parent)
{
Skin *skin = Skin::getInstance ();
connect (skin, SIGNAL (skinChanged (Skin *)),
this, SLOT (setPixmaps(Skin *)));
m_h = h;
m_w = w;
m_x_off = 0;
m_x2_off = 0;
#ifdef Q_OS_MACX
m_fontsize = 9;
#else
m_fontsize = 8; /* default */
#endif
m_ttf = true;
setMinimumSize(m_w + 2, m_h);
setMaximumSize(m_w + 2, m_h);
if (m_h > 10) {
m_y = 4;
} else {
m_y = 0;
}
m_timer = new QTimer (this);
connect (m_timer, SIGNAL (timeout()), this, SLOT (addOffset ()));
}
void
TextScroller::setPixmaps (Skin *skin)
{
QPalette pal = palette ();
QBrush b = QBrush (Qt::TexturePattern);
b.setTexture (skin->getItem (Skin::TEXTBG));
pal.setBrush (QPalette::Window, b);
setPalette (pal);
setText (QString::fromUtf8 ("Promoe 0.1"));
update();
}
void
TextScroller::addOffset ()
{
if (m_x2_off > 0) {
m_x2_off --;
} else if (m_x_off < m_pixmap.size().width()) {
m_x_off ++;
} else {
m_x_off = 1;
m_x2_off = 0;
}
repaint ();
m_timer->start (40);
}
void
TextScroller::setText (const QString &text)
{
if (m_ttf) {
drawQtFont (text);
} else {
drawBitmapFont (text);
}
m_x_off = 1;
m_x2_off = 0;
update ();
}
void
TextScroller::drawBitmapFont (const QString &text)
{
Skin *skin = Skin::getInstance ();
int width = text.length() * 6;
QString (temp) = text.toLower ();
if (width > m_w) {
temp += QString::fromAscii (" -- ");
m_x2_off = m_w / 2;
m_pixmap = QPixmap (width + 6*6, m_h);
m_timer->start (40);
} else {
m_pixmap = QPixmap (m_w, m_h);
}
const char *t = temp.toLatin1();
QPainter (paint);
paint.begin (&m_pixmap);
paint.fillRect (m_pixmap.rect(), Qt::white);
for (uint i = 0; i < strlen (t); i++) {
QPixmap p = skin->getLetter (t[i]);
if (!p) {
p = skin->getLetter(' ');
}
paint.drawPixmap (QRect (i * 6, m_y, 4, 6),
p, p.rect());
}
paint.end();
m_pixmap.setMask (m_pixmap.createHeuristicMask ());
}
void
TextScroller::drawQtFont (const QString &text)
{
Skin *skin = Skin::getInstance ();
QFont font(skin->getPLeditValue ("font"));
font.setPixelSize (m_fontsize);
QFontMetrics fM(font);
QRect rect = fM.boundingRect (text);
QString (temp) = text;
if (rect.width() > m_w) {
temp += QString::fromAscii (" -- ");
QRect rect = fM.boundingRect (temp);
m_timer->start (40);
m_x2_off = m_w / 2;
m_pixmap = QPixmap (rect.width(), m_h);
} else {
m_pixmap = QPixmap (m_w, m_h);
}
QPainter paint;
paint.begin (&m_pixmap);
paint.drawPixmap (m_pixmap.rect (),
skin->getItem (Skin::TEXTBG),
skin->getItem (Skin::TEXTBG).rect ());
paint.setFont (font);
QColor c;
c.setNamedColor (skin->getPLeditValue ("normal"));
paint.setPen (c);
paint.drawText (m_pixmap.rect (),
Qt::AlignLeft | Qt::AlignVCenter,
temp);
paint.end ();
}
void
TextScroller::paintEvent (QPaintEvent *event)
{
int pad = 0;
if (m_pixmap.isNull ()) {
return;
}
int w2 = m_pixmap.size().width() - m_x_off;
if (w2 < m_w) {
pad = m_w - w2;
}
QPainter (paint);
paint.begin (this);
paint.drawPixmap (QRect (m_x2_off, 0, m_w - pad, m_h),
m_pixmap,
QRect (m_x_off, 0, m_w, m_h));
if (pad) {
paint.drawPixmap (QRect (m_w - pad, 0, pad, m_h),
m_pixmap,
QRect (0, 0, pad, m_h));
}
paint.end ();
}
TextScroller::~TextScroller ()
{
}