OTHER: Make text in textbar dragable.

This commit is contained in:
Thomas Frauendorfer 2008-10-27 07:24:42 +01:00
parent d4cb52ecd9
commit 49e4efad11
3 changed files with 131 additions and 82 deletions

View file

@ -59,8 +59,8 @@ MainDisplay::MainDisplay (MainWindow *parent) : SkinDisplay(parent)
SetupPushButtons (); SetupPushButtons ();
SetupToggleButtons (); SetupToggleButtons ();
m_text = new TextScroller (this, 154, 10, "main"); m_text = new TextScroller (this, 154, 12, "main");
m_text->move (112, 25); m_text->move (111, 24);
m_time = new TimeDisplay(this); m_time = new TimeDisplay(this);
m_time->move (36, 26); m_time->move (36, 26);

View file

@ -19,15 +19,17 @@
#include <QBrush> #include <QBrush>
#include <QFont> #include <QFont>
#include <QMouseEvent>
#include <QPainter> #include <QPainter>
#include <QPalette> #include <QPalette>
#include <QPixmap> #include <QPixmap>
#include <QSettings> #include <QSettings>
#include <QTimer> #include <QTimer>
#include <QtDebug>
TextScroller::TextScroller (QWidget *parent, uint w, TextScroller::TextScroller (QWidget *parent, uint w,
uint h, const QString &name) : uint h, const QString &name) : QWidget (parent)
QWidget (parent)
{ {
Skin *skin = Skin::getInstance (); Skin *skin = Skin::getInstance ();
@ -48,21 +50,20 @@ TextScroller::TextScroller (QWidget *parent, uint w,
s.setValue("ttf", true); s.setValue("ttf", true);
m_name = name; m_name = name;
m_h = h;
m_w = w;
m_x_off = 0; m_x_off = 0;
m_x2_off = 0; m_drag_off = 0;
m_fontsize = s.value ("fontsize").toInt (); m_fontsize = s.value ("fontsize").toInt ();
m_ttf = s.value ("ttf").toBool (); m_ttf = s.value ("ttf").toBool ();
m_text = "Promoe " PROMOE_VERSION; m_text = "Promoe " PROMOE_VERSION;
m_scroll = s.value ("scroll").toBool (); m_scroll = s.value ("scroll").toBool ();
m_dragtext = false;
s.endGroup (); s.endGroup ();
setMinimumSize(m_w + 2, m_h); setFixedSize(w, h);
setMaximumSize(m_w + 2, m_h);
m_timer = new QTimer (this); m_timer = new QTimer (this);
m_timer->setInterval (40);
connect (m_timer, SIGNAL (timeout()), this, SLOT (addOffset ())); connect (m_timer, SIGNAL (timeout()), this, SLOT (addOffset ()));
//connect (xmmsh, SIGNAL (settingsSaved ()), this, SLOT (settingsSaved ())); //connect (xmmsh, SIGNAL (settingsSaved ()), this, SLOT (settingsSaved ()));
} }
@ -77,13 +78,12 @@ TextScroller::settingsSaved (void)
if (m_scroll != s.value ("scroll").toBool ()) { if (m_scroll != s.value ("scroll").toBool ()) {
m_x_off = 0; m_x_off = 0;
m_x2_off = 0;
} }
m_scroll = s.value ("scroll").toBool (); m_scroll = s.value ("scroll").toBool ();
s.endGroup (); s.endGroup ();
setText (m_text); drawText ();
update (); update ();
} }
@ -95,38 +95,44 @@ TextScroller::setPixmaps (Skin *skin)
pal.setBrush (QPalette::Window, b); pal.setBrush (QPalette::Window, b);
setPalette (pal); setPalette (pal);
setText (m_text); drawText ();
update(); 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 = 0;
m_x2_off = 0;
}
update ();
m_timer->start (40);
}
void void
TextScroller::setText (QString text) TextScroller::setText (QString text)
{ {
m_text = text; m_text = text;
if (m_ttf) { drawText ();
drawQtFont (text); }
void
TextScroller::addOffset ()
{
if (m_x_off < m_pixmap.width()) {
m_x_off ++;
} else { } else {
drawBitmapFont (text); m_x_off = 0;
} }
update ();
}
void
TextScroller::drawText ()
{
if (m_ttf) {
drawQtFont (m_text);
} else {
drawBitmapFont (m_text);
}
// take care that the text doesn't jump after resetting it's offset
// if we were still dragging it on a song change
if (m_dragtext)
m_drag_off -= m_x_off;
m_x_off = 0; m_x_off = 0;
m_x2_off = 0;
update (); update ();
} }
@ -135,32 +141,23 @@ TextScroller::drawBitmapFont (QString text)
{ {
Skin *skin = Skin::getInstance (); Skin *skin = Skin::getInstance ();
int width = text.length() * 6; int w = text.length() * 5;
QString temp = text.toLower (); QString temp = text.toLower ();
if (width > m_w) { if (w > width ()) {
temp += QString::fromAscii (" -- "); temp += QString (" *** ");
m_pixmap = QPixmap (width + 6*6, m_h); m_pixmap = QPixmap (w + 7*5, 6);
if (m_scroll) {
m_timer->start (40);
} else {
m_timer->stop ();
}
} else { } else {
m_pixmap = QPixmap (m_w, m_h); m_pixmap = QPixmap (width (), 6);
m_timer->stop ();
} }
updateScrolling ();
QByteArray temp2 = temp.toLatin1(); QByteArray temp2 = temp.toLatin1();
const char *t = temp2.data(); const char *t = temp2.data();
QPainter (paint); QPainter (paint);
paint.begin (&m_pixmap); paint.begin (&m_pixmap);
paint.drawPixmap (m_pixmap.rect (), paint.drawPixmap (m_pixmap.rect (), skin->getItem (Skin::TEXTBG));
skin->getItem (Skin::TEXTBG),
skin->getItem (Skin::TEXTBG).rect ());
for (uint i = 0; i < strlen (t); i++) { for (uint i = 0; i < strlen (t); i++) {
QPixmap p = skin->getLetter (t[i]); QPixmap p = skin->getLetter (t[i]);
@ -168,12 +165,11 @@ TextScroller::drawBitmapFont (QString text)
p = skin->getLetter(' '); p = skin->getLetter(' ');
} }
paint.drawPixmap (QRect ((i * 6), 0, 4, 6), paint.drawPixmap (QRect ((i * 5), 0, 5, 6),
p, p.rect()); p, p.rect());
} }
paint.end(); paint.end();
} }
void void
@ -189,22 +185,15 @@ TextScroller::drawQtFont (QString text)
QString (temp) = text; QString (temp) = text;
if (rect.width() > m_w) { if (rect.width() > width ()) {
temp += QString::fromAscii (" -- "); temp += QString (" *** ");
QRect rect = fM.boundingRect (temp); QRect rect = fM.boundingRect (temp);
m_pixmap = QPixmap (rect.width(), m_h); m_pixmap = QPixmap (rect.width(), height ());
if (m_scroll) {
m_timer->start (40);
} else {
m_timer->stop ();
}
} else { } else {
m_pixmap = QPixmap (m_w, m_h); m_pixmap = QPixmap (size ());
m_timer->stop ();
} }
updateScrolling ();
QPainter paint; QPainter paint;
paint.begin (&m_pixmap); paint.begin (&m_pixmap);
@ -220,37 +209,92 @@ TextScroller::drawQtFont (QString text)
Qt::AlignLeft | Qt::AlignVCenter, Qt::AlignLeft | Qt::AlignVCenter,
temp); temp);
paint.end (); paint.end ();
} }
void void
TextScroller::paintEvent (QPaintEvent *event) TextScroller::paintEvent (QPaintEvent *event)
{ {
int pad = 0;
if (m_pixmap.isNull ()) { if (m_pixmap.isNull ()) {
return; return;
} }
int w2 = m_pixmap.size().width() - m_x_off; // A pixmap font is only 6 pixels high and should be centered vertically
if (w2 < m_w) { // for a QFont h_offset is 0
pad = m_w - w2; int h_offset = (height () - m_pixmap.height ()) /2;
}
int left_width = qMin (m_pixmap.width() - m_x_off, width ());
QPainter (paint); QPainter (paint);
paint.begin (this); paint.begin (this);
paint.drawPixmap (QRect (m_x2_off, 0, m_w - pad, m_h), paint.drawPixmap (QPoint (0, h_offset),
m_pixmap, m_pixmap,
QRect (m_x_off, 0, m_w - pad, m_h)); QRect (m_x_off, 0, left_width, m_pixmap.height ()));
if (pad) { if (left_width < width ()) {
paint.drawPixmap (QRect (m_w - pad, 0, pad, m_h), paint.drawPixmap (left_width, h_offset, m_pixmap);
m_pixmap,
QRect (0, 0, pad, m_h));
} }
paint.end (); paint.end ();
} }
TextScroller::~TextScroller () inline void
TextScroller::updateScrolling ()
{ {
if (m_scroll && !m_dragtext && (m_pixmap.width () > width ())) {
m_timer->start ();
} else {
m_timer->stop ();
}
} }
void
TextScroller::mousePressEvent (QMouseEvent *event)
{
if (event->button () != Qt::LeftButton) {
event->ignore ();
return;
}
if (m_pixmap.width () <= width ()) {
// don't use event->ignore here!
return;
}
// calculate the offset relative to m_pixmap
// if the offset would be saved relative to the widget another
// helpervariable would become necessary to save m_x_off
// m_drag_off can be bigger than the width of m_pixmap but that is no
// problem as we use the remainder operator in the calculation results
m_drag_off = m_x_off + event->x();
m_dragtext = true;
updateScrolling ();
}
void
TextScroller::mouseReleaseEvent (QMouseEvent *event)
{
if (event->button () != Qt::LeftButton) {
event->ignore ();
return;
}
m_drag_off = 0;
m_dragtext = false;
updateScrolling ();
}
void
TextScroller::mouseMoveEvent (QMouseEvent *event)
{
if (!m_dragtext) {
event->ignore ();
return;
}
m_x_off = ( m_drag_off - event->x()) % m_pixmap.width ();
// make sure we have a positive value
if (m_x_off < 0)
m_x_off += m_pixmap.width ();
update ();
}

View file

@ -17,6 +17,7 @@
#define __TEXTBOX_H__ #define __TEXTBOX_H__
#include <QWidget> #include <QWidget>
class QMouseEvent;
class QPixmap; class QPixmap;
class QTimer; class QTimer;
@ -28,7 +29,7 @@ class TextScroller : public QWidget
public: public:
TextScroller (QWidget *parent, uint, uint, const QString &); TextScroller (QWidget *parent, uint, uint, const QString &);
~TextScroller (); ~TextScroller () {}
void setText(QString text); void setText(QString text);
void setFontSize (int i) { m_fontsize = i; } void setFontSize (int i) { m_fontsize = i; }
@ -40,24 +41,28 @@ class TextScroller : public QWidget
void settingsSaved (void); void settingsSaved (void);
protected: protected:
QPixmap m_pixmap;
void paintEvent (QPaintEvent *event); void paintEvent (QPaintEvent *event);
void mousePressEvent (QMouseEvent * event);
void mouseReleaseEvent (QMouseEvent * event);
void mouseMoveEvent (QMouseEvent * event);
private: private:
int m_w;
int m_h;
int m_x_off; int m_x_off;
int m_x2_off;
int m_fontsize; int m_fontsize;
int m_drag_off;
bool m_ttf; bool m_ttf;
bool m_scroll; bool m_scroll;
bool m_dragtext;
QTimer *m_timer; QTimer *m_timer;
QString m_text; QString m_text;
QString m_name; QString m_name;
QPixmap m_pixmap;
void drawText ();
void drawBitmapFont (QString text); void drawBitmapFont (QString text);
void drawQtFont (QString text); void drawQtFont (QString text);
void updateScrolling ();
}; };
#endif #endif