1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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);
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()));
46 TopicBar::~TopicBar() {
51 void TopicBar::resizeEvent(QResizeEvent *event) {
52 QPushButton::resizeEvent(event);
56 void TopicBar::calcTextMetrics() {
57 int w = width() - 2*frameWidth;
58 QRect boundingRect = QFontMetrics(topicFont).boundingRect(text);
59 textWidth = boundingRect.width();
61 offset = 0; fillTextStart = -1; secondTextStart = -1;
65 fillTextStart = textWidth;
66 boundingRect = QFontMetrics(topicFont).boundingRect(fillText);
67 secondTextStart = fillTextStart + boundingRect.width();
68 displayText = QString("%1%2%1").arg(text).arg(fillText);
70 //timer->start(); // uncomment this to get autoscroll rather than on-demand
74 // TODO catch resizeEvent for scroll settings
75 void TopicBar::setContents(QString t, bool _oneshot) {
76 text = t; oneshot = _oneshot;
80 void TopicBar::paintEvent(QPaintEvent *event) {
81 QPushButton::paintEvent(event);
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);
90 void TopicBar::updateOffset() {
92 if(offset >= secondTextStart) {
94 if(oneshot) timer->stop(); // only scroll once!
99 void TopicBar::startScrolling() {
100 if(displayText.length() > text.length()) {
106 void TopicBar::stopScrolling() {