1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel IRC Development Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
26 TopicBar::TopicBar(QWidget *parent) : QPushButton(parent) {
27 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
29 // Define the font and calculate the metrics for it
31 topicFont.setPointSize(5);
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);
39 timer = new QTimer(this);
40 timer->setInterval(20);
41 connect(timer, SIGNAL(timeout()), this, SLOT(updateOffset()));
44 TopicBar::~TopicBar() {
49 void TopicBar::setContents(QString t, bool _oneshot) {
50 text = t; oneshot = _oneshot;
51 int w = width() - 2*frameWidth;
52 QRect boundingRect = QFontMetrics(topicFont).boundingRect(text);
53 if(boundingRect.width() <= w) {
54 offset = 0; fillTextStart = -1; secondTextStart = -1;
57 fillTextStart = boundingRect.width();
58 boundingRect = QFontMetrics(topicFont).boundingRect(fillText);
59 secondTextStart = fillTextStart + boundingRect.width();
60 text = QString("%1%2%1").arg(text).arg(fillText);
66 void TopicBar::paintEvent(QPaintEvent *event) {
67 QPushButton::paintEvent(event);
69 QPainter painter(this);
70 painter.setFont(topicFont);
71 painter.setClipRect(frameWidth, frameWidth, rect().width() - 2*frameWidth, rect().height() - 2*frameWidth);
72 painter.drawText(QPoint(-offset + frameWidth, QFontMetrics(topicFont).ascent() + frameWidth), text);
76 void TopicBar::updateOffset() {
78 if(offset >= secondTextStart) {
80 if(oneshot) timer->stop(); // only scroll once!