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 ***************************************************************************/
27 TopicBar::TopicBar(QWidget *parent) : QPushButton(parent) {
28 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
30 // Define the font and calculate the metrics for it
32 topicFont.setPointSize(5);
34 // frameWidth = style()->pixelMetric(QStyle::PM_ButtonMargin); // Nice idea, but Qtopia's buttons are just too large...
35 frameWidth = 3; // so we hardcode a more reasonable framewidth than 7
36 setFixedHeight(QFontMetrics(topicFont).height() + 2*frameWidth);
41 timer = new QTimer(this);
42 timer->setInterval(25);
43 connect(timer, SIGNAL(timeout()), this, SLOT(updateOffset()));
44 connect(this, SIGNAL(clicked()), this, SLOT(startScrolling()));
46 _model = Client::bufferModel();
47 connect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
48 this, SLOT(dataChanged(QModelIndex, QModelIndex)));
50 _selectionModel = Client::bufferModel()->standardSelectionModel();
51 connect(_selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
52 this, SLOT(currentChanged(QModelIndex, QModelIndex)));
55 TopicBar::~TopicBar() {
60 void TopicBar::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
62 setContents(current.sibling(current.row(), 1).data().toString());
65 void TopicBar::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
66 QItemSelectionRange changedArea(topLeft, bottomRight);
67 QModelIndex currentTopicIndex = _selectionModel->currentIndex().sibling(_selectionModel->currentIndex().row(), 1);
68 if(changedArea.contains(currentTopicIndex))
69 setContents(currentTopicIndex.data().toString());
72 void TopicBar::resizeEvent(QResizeEvent *event) {
73 QPushButton::resizeEvent(event);
77 void TopicBar::calcTextMetrics() {
78 int w = width() - 2*frameWidth;
79 QRect boundingRect = QFontMetrics(topicFont).boundingRect(text);
80 textWidth = boundingRect.width();
82 offset = 0; fillTextStart = -1; secondTextStart = -1;
86 fillTextStart = textWidth;
87 boundingRect = QFontMetrics(topicFont).boundingRect(fillText);
88 secondTextStart = fillTextStart + boundingRect.width();
89 displayText = QString("%1%2%1").arg(text).arg(fillText);
91 //timer->start(); // uncomment this to get autoscroll rather than on-demand
95 // TODO catch resizeEvent for scroll settings
96 void TopicBar::setContents(QString t, bool _oneshot) {
97 text = t; oneshot = _oneshot;
101 void TopicBar::paintEvent(QPaintEvent *event) {
102 QPushButton::paintEvent(event);
104 QPainter painter(this);
105 painter.setFont(topicFont);
106 painter.setClipRect(frameWidth, frameWidth, rect().width() - 2*frameWidth, rect().height() - 2*frameWidth);
107 painter.drawText(QPoint(-offset + frameWidth, QFontMetrics(topicFont).ascent() + frameWidth), displayText);
111 void TopicBar::updateOffset() {
113 if(offset >= secondTextStart) {
115 if(oneshot) timer->stop(); // only scroll once!
120 void TopicBar::startScrolling() {
121 if(displayText.length() > text.length()) {
127 void TopicBar::stopScrolling() {