From 49e4efad1114d6e81a5108a5203f0a0490d2f92a Mon Sep 17 00:00:00 2001 From: Thomas Frauendorfer Date: Mon, 27 Oct 2008 07:24:42 +0100 Subject: [PATCH] OTHER: Make text in textbar dragable. --- src/mainwindow/maindisplay.cpp | 4 +- src/mainwindow/textbar.cpp | 194 ++++++++++++++++++++------------- src/mainwindow/textbar.h | 15 ++- 3 files changed, 131 insertions(+), 82 deletions(-) diff --git a/src/mainwindow/maindisplay.cpp b/src/mainwindow/maindisplay.cpp index 8fa53fe..ff0b8ed 100644 --- a/src/mainwindow/maindisplay.cpp +++ b/src/mainwindow/maindisplay.cpp @@ -59,8 +59,8 @@ MainDisplay::MainDisplay (MainWindow *parent) : SkinDisplay(parent) SetupPushButtons (); SetupToggleButtons (); - m_text = new TextScroller (this, 154, 10, "main"); - m_text->move (112, 25); + m_text = new TextScroller (this, 154, 12, "main"); + m_text->move (111, 24); m_time = new TimeDisplay(this); m_time->move (36, 26); diff --git a/src/mainwindow/textbar.cpp b/src/mainwindow/textbar.cpp index 3726f7b..d2a4198 100644 --- a/src/mainwindow/textbar.cpp +++ b/src/mainwindow/textbar.cpp @@ -19,15 +19,17 @@ #include #include +#include #include #include #include #include #include +#include + TextScroller::TextScroller (QWidget *parent, uint w, - uint h, const QString &name) : - QWidget (parent) + uint h, const QString &name) : QWidget (parent) { Skin *skin = Skin::getInstance (); @@ -48,21 +50,20 @@ TextScroller::TextScroller (QWidget *parent, uint w, s.setValue("ttf", true); m_name = name; - m_h = h; - m_w = w; m_x_off = 0; - m_x2_off = 0; + m_drag_off = 0; m_fontsize = s.value ("fontsize").toInt (); m_ttf = s.value ("ttf").toBool (); m_text = "Promoe " PROMOE_VERSION; m_scroll = s.value ("scroll").toBool (); + m_dragtext = false; s.endGroup (); - - setMinimumSize(m_w + 2, m_h); - setMaximumSize(m_w + 2, m_h); + + setFixedSize(w, h); m_timer = new QTimer (this); + m_timer->setInterval (40); connect (m_timer, SIGNAL (timeout()), this, SLOT (addOffset ())); //connect (xmmsh, SIGNAL (settingsSaved ()), this, SLOT (settingsSaved ())); } @@ -77,13 +78,12 @@ TextScroller::settingsSaved (void) if (m_scroll != s.value ("scroll").toBool ()) { m_x_off = 0; - m_x2_off = 0; } m_scroll = s.value ("scroll").toBool (); s.endGroup (); - setText (m_text); + drawText (); update (); } @@ -95,38 +95,44 @@ TextScroller::setPixmaps (Skin *skin) pal.setBrush (QPalette::Window, b); setPalette (pal); - setText (m_text); + drawText (); 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 TextScroller::setText (QString text) { m_text = text; - if (m_ttf) { - drawQtFont (text); + drawText (); +} + +void +TextScroller::addOffset () +{ + if (m_x_off < m_pixmap.width()) { + m_x_off ++; } 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_x2_off = 0; update (); } @@ -135,32 +141,23 @@ TextScroller::drawBitmapFont (QString text) { Skin *skin = Skin::getInstance (); - int width = text.length() * 6; + int w = text.length() * 5; QString temp = text.toLower (); - if (width > m_w) { - temp += QString::fromAscii (" -- "); - m_pixmap = QPixmap (width + 6*6, m_h); - - if (m_scroll) { - m_timer->start (40); - } else { - m_timer->stop (); - } + if (w > width ()) { + temp += QString (" *** "); + m_pixmap = QPixmap (w + 7*5, 6); } else { - m_pixmap = QPixmap (m_w, m_h); - m_timer->stop (); + m_pixmap = QPixmap (width (), 6); } + updateScrolling (); QByteArray temp2 = temp.toLatin1(); const char *t = temp2.data(); QPainter (paint); - paint.begin (&m_pixmap); - paint.drawPixmap (m_pixmap.rect (), - skin->getItem (Skin::TEXTBG), - skin->getItem (Skin::TEXTBG).rect ()); + paint.drawPixmap (m_pixmap.rect (), skin->getItem (Skin::TEXTBG)); for (uint i = 0; i < strlen (t); i++) { QPixmap p = skin->getLetter (t[i]); @@ -168,12 +165,11 @@ TextScroller::drawBitmapFont (QString text) p = skin->getLetter(' '); } - paint.drawPixmap (QRect ((i * 6), 0, 4, 6), + paint.drawPixmap (QRect ((i * 5), 0, 5, 6), p, p.rect()); } paint.end(); - } void @@ -189,22 +185,15 @@ TextScroller::drawQtFont (QString text) QString (temp) = text; - if (rect.width() > m_w) { - temp += QString::fromAscii (" -- "); + if (rect.width() > width ()) { + temp += QString (" *** "); QRect rect = fM.boundingRect (temp); - m_pixmap = QPixmap (rect.width(), m_h); - - if (m_scroll) { - m_timer->start (40); - } else { - m_timer->stop (); - } - + m_pixmap = QPixmap (rect.width(), height ()); } else { - m_pixmap = QPixmap (m_w, m_h); - m_timer->stop (); + m_pixmap = QPixmap (size ()); } + updateScrolling (); QPainter paint; paint.begin (&m_pixmap); @@ -220,37 +209,92 @@ TextScroller::drawQtFont (QString text) 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; - } + // A pixmap font is only 6 pixels high and should be centered vertically + // for a QFont h_offset is 0 + int h_offset = (height () - m_pixmap.height ()) /2; + + int left_width = qMin (m_pixmap.width() - m_x_off, width ()); QPainter (paint); paint.begin (this); - paint.drawPixmap (QRect (m_x2_off, 0, m_w - pad, m_h), + paint.drawPixmap (QPoint (0, h_offset), m_pixmap, - QRect (m_x_off, 0, m_w - pad, m_h)); - if (pad) { - paint.drawPixmap (QRect (m_w - pad, 0, pad, m_h), - m_pixmap, - QRect (0, 0, pad, m_h)); + QRect (m_x_off, 0, left_width, m_pixmap.height ())); + if (left_width < width ()) { + paint.drawPixmap (left_width, h_offset, m_pixmap); } 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 (); +} diff --git a/src/mainwindow/textbar.h b/src/mainwindow/textbar.h index 4b7faf0..1d14f6b 100644 --- a/src/mainwindow/textbar.h +++ b/src/mainwindow/textbar.h @@ -17,6 +17,7 @@ #define __TEXTBOX_H__ #include +class QMouseEvent; class QPixmap; class QTimer; @@ -28,7 +29,7 @@ class TextScroller : public QWidget public: TextScroller (QWidget *parent, uint, uint, const QString &); - ~TextScroller (); + ~TextScroller () {} void setText(QString text); void setFontSize (int i) { m_fontsize = i; } @@ -40,24 +41,28 @@ class TextScroller : public QWidget void settingsSaved (void); protected: - QPixmap m_pixmap; void paintEvent (QPaintEvent *event); + void mousePressEvent (QMouseEvent * event); + void mouseReleaseEvent (QMouseEvent * event); + void mouseMoveEvent (QMouseEvent * event); private: - int m_w; - int m_h; int m_x_off; - int m_x2_off; int m_fontsize; + int m_drag_off; bool m_ttf; bool m_scroll; + bool m_dragtext; QTimer *m_timer; QString m_text; QString m_name; + QPixmap m_pixmap; + void drawText (); void drawBitmapFont (QString text); void drawQtFont (QString text); + void updateScrolling (); }; #endif