Merge branch 'seezer'
[quassel.git] / src / qtui / topicbutton.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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 "topicbutton.h"
22
23
24 #include <QDebug>
25
26 #include <QApplication>
27 #include <QPainter>
28 #include <QHBoxLayout>
29 #include <QFont>
30 #include <QFontMetrics>
31
32 #include "qtui.h"
33 #include "message.h"
34
35 TopicButton::TopicButton(QWidget *parent)
36   : QAbstractButton(parent)
37 {
38   setFixedHeight(QFontMetrics(qApp->font()).height());
39   setToolTip(tr("Click to edit!"));
40 }
41
42 void TopicButton::paintEvent(QPaintEvent *event) {
43   Q_UNUSED(event);
44
45   QPainter painter(this);
46   painter.setBackgroundMode(Qt::OpaqueMode);
47
48   // FIXME re-enable topic painting
49 #ifndef SPUTDEV
50   QRect drawRect = rect();
51   QRect brect;
52   QString textPart;
53   foreach(QTextLayout::FormatRange fr, styledContents.formatList) {
54     textPart = styledContents.plainText.mid(fr.start, fr.length);
55     painter.setFont(fr.format.font());
56     painter.setPen(QPen(fr.format.foreground(), 0));
57     painter.setBackground(fr.format.background());
58     painter.drawText(drawRect, Qt::AlignLeft|Qt::TextSingleLine, textPart, &brect);
59     drawRect.setLeft(brect.right());
60   }
61 #endif
62 }
63
64 void TopicButton::setAndStyleText(const QString &text) {
65   if(QAbstractButton::text() == text)
66     return;
67
68   setText(text); // this triggers a repaint event
69
70 #ifndef SPUTDEV
71   styledContents = QtUi::style()->styleString(Message::mircToInternal(text));
72   int height = 1;
73   foreach(QTextLayout::FormatRange fr, styledContents.formatList) {
74     height = qMax(height, QFontMetrics(fr.format.font()).height());
75   }
76
77   // ensure the button is editable (height != 1) if there is no text to show
78   if(text.isEmpty())
79     height = QFontMetrics(qApp->font()).height();
80   
81   setFixedHeight(height);
82 #endif  
83   // show topic in tooltip
84   setToolTip(tr("%1\n\nClick to edit!").arg(QAbstractButton::text()));
85 }
86