Some more tweaking for QuasselTopia.
[quassel.git] / src / qtopia / topicbar.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel IRC Development Team             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "topicbar.h"
22
23 #include <QtGui>
24
25
26 TopicBar::TopicBar(QWidget *parent) : QPushButton(parent) {
27   setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
28
29   // Define the font and calculate the metrics for it
30   topicFont = font();
31   topicFont.setPointSize(5);
32
33   // frameWidth = style()->pixelMetric(QStyle::PM_ButtonMargin); // Nice idea, but Qtopia's buttons are just too large...
34   frameWidth = 3; // so we hardcode a more reasonable framewidth than 7
35   setFixedHeight(QFontMetrics(topicFont).height() + 2*frameWidth);
36
37   textWidth = 0;
38   fillText = " *** ";
39   oneshot = true;
40   timer = new QTimer(this);
41   timer->setInterval(25);
42   connect(timer, SIGNAL(timeout()), this, SLOT(updateOffset()));
43   connect(this, SIGNAL(clicked()), this, SLOT(startScrolling()));
44 }
45
46 TopicBar::~TopicBar() {
47
48
49 }
50
51 void TopicBar::resizeEvent(QResizeEvent *event) {
52   QPushButton::resizeEvent(event);
53   calcTextMetrics();
54 }
55
56 void TopicBar::calcTextMetrics() {
57   int w = width() - 2*frameWidth;
58   QRect boundingRect = QFontMetrics(topicFont).boundingRect(text);
59   textWidth = boundingRect.width();
60   if(textWidth <= w) {
61     offset = 0; fillTextStart = -1; secondTextStart = -1;
62     displayText = text;
63     timer->stop();
64   } else {
65     fillTextStart = textWidth;
66     boundingRect = QFontMetrics(topicFont).boundingRect(fillText);
67     secondTextStart = fillTextStart + boundingRect.width();
68     displayText = QString("%1%2%1").arg(text).arg(fillText);
69     offset = 0;
70     timer->start();
71   }
72 }
73
74 // TODO catch resizeEvent for scroll settings
75 void TopicBar::setContents(QString t, bool _oneshot) {
76   text = t; oneshot = _oneshot;
77   calcTextMetrics();
78 }
79
80 void TopicBar::paintEvent(QPaintEvent *event) {
81   QPushButton::paintEvent(event);
82
83   QPainter painter(this);
84   painter.setFont(topicFont);
85   painter.setClipRect(frameWidth, frameWidth, rect().width() - 2*frameWidth, rect().height() - 2*frameWidth);
86   painter.drawText(QPoint(-offset + frameWidth, QFontMetrics(topicFont).ascent() + frameWidth), displayText);
87
88 }
89
90 void TopicBar::updateOffset() {
91   offset+=1;
92   if(offset >= secondTextStart) {
93     offset = 0;
94     if(oneshot) timer->stop(); // only scroll once!
95   }
96   update();
97 }
98
99 void TopicBar::startScrolling() {
100   if(displayText.length() > text.length()) {
101     //oneshot = false;
102     timer->start();
103   }
104 }
105
106 void TopicBar::stopScrolling() {
107   oneshot = true;
108 }