Bump inxi script to 1.0.8
[quassel.git] / dev-notes / obsolete / qtopia / topicbar.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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) version 3.                                           *
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 <QtGui>
22
23 #include "topicbar.h"
24 #include "client.h"
25
26
27 TopicBar::TopicBar(QWidget *parent) : QPushButton(parent) {
28   setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
29
30   // Define the font and calculate the metrics for it
31   topicFont = font();
32   topicFont.setPointSize(5);
33
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);
37
38   textWidth = 0;
39   fillText = " *** ";
40   oneshot = true;
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()));
45
46   _model = Client::bufferModel();
47   connect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
48           this, SLOT(dataChanged(QModelIndex, QModelIndex)));
49
50   _selectionModel = Client::bufferModel()->standardSelectionModel();
51   connect(_selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
52           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
53 }
54
55 TopicBar::~TopicBar() {
56
57
58 }
59
60 void TopicBar::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
61   Q_UNUSED(previous);
62   setContents(current.sibling(current.row(), 1).data().toString());
63 }
64
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());
70 };
71
72 void TopicBar::resizeEvent(QResizeEvent *event) {
73   QPushButton::resizeEvent(event);
74   calcTextMetrics();
75 }
76
77 void TopicBar::calcTextMetrics() {
78   int w = width() - 2*frameWidth;
79   QRect boundingRect = QFontMetrics(topicFont).boundingRect(text);
80   textWidth = boundingRect.width();
81   if(textWidth <= w) {
82     offset = 0; fillTextStart = -1; secondTextStart = -1;
83     displayText = text;
84     timer->stop();
85   } else {
86     fillTextStart = textWidth;
87     boundingRect = QFontMetrics(topicFont).boundingRect(fillText);
88     secondTextStart = fillTextStart + boundingRect.width();
89     displayText = QString("%1%2%1").arg(text).arg(fillText);
90     offset = 0;
91     //timer->start();  // uncomment this to get autoscroll rather than on-demand
92   }
93 }
94
95 // TODO catch resizeEvent for scroll settings
96 void TopicBar::setContents(QString t, bool _oneshot) {
97   text = t; oneshot = _oneshot;
98   calcTextMetrics();
99 }
100
101 void TopicBar::paintEvent(QPaintEvent *event) {
102   QPushButton::paintEvent(event);
103
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);
108
109 }
110
111 void TopicBar::updateOffset() {
112   offset+=1;
113   if(offset >= secondTextStart) {
114     offset = 0;
115     if(oneshot) timer->stop(); // only scroll once!
116   }
117   update();
118 }
119
120 void TopicBar::startScrolling() {
121   if(displayText.length() > text.length()) {
122     //oneshot = false;
123     timer->start();
124   }
125 }
126
127 void TopicBar::stopScrolling() {
128   oneshot = true;
129 }