1 /***************************************************************************
2 * Copyright (C) 2005/06 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 ***************************************************************************/
21 #include "topiclabel.h"
26 #include <QApplication>
27 #include <QDesktopServices>
29 // #include <QHBoxLayout>
31 #include <QFontMetrics>
36 TopicLabel::TopicLabel(QWidget *parent)
43 setToolTip(tr("Drag to scroll the topic!"));
44 setCursor(Qt::OpenHandCursor);
47 void TopicLabel::paintEvent(QPaintEvent *event) {
50 textPartOffset.clear();
52 QPainter painter(this);
53 painter.setBackgroundMode(Qt::OpaqueMode);
55 // FIXME use QTextLayout instead
57 QRect drawRect = rect().adjusted(offset, 0, 0, 0);
60 foreach(QTextLayout::FormatRange fr, formatList) {
61 textPart = plainText.mid(fr.start, fr.length);
62 textPartOffset << drawRect.left();
63 painter.setFont(fr.format.font());
64 painter.setPen(QPen(fr.format.foreground(), 0));
65 painter.setBackground(fr.format.background());
66 painter.drawText(drawRect, Qt::AlignLeft|Qt::AlignVCenter, textPart, &brect);
67 drawRect.setLeft(brect.right());
69 textWidth = brect.right();
72 void TopicLabel::setText(const QString &text) {
80 UiStyle::StyledString styledContents = QtUi::style()->styleString(QtUi::style()->mircToInternal(text));
81 plainText = styledContents.plainText;
82 formatList = QtUi::style()->toTextLayoutList(styledContents.formatList, plainText.length());
84 foreach(QTextLayout::FormatRange fr, formatList) {
85 height = qMax(height, QFontMetrics(fr.format.font()).height());
88 // ensure the label is editable (height != 1) if there is no text to show
90 height = QFontMetrics(qApp->font()).height();
92 // setFixedHeight(height);
93 // show topic in tooltip
97 void TopicLabel::mouseMoveEvent(QMouseEvent *event) {
102 int newOffset = event->pos().x() - dragStartX;
105 else if(width() + 1 < textWidth || offset < newOffset)
111 void TopicLabel::mousePressEvent(QMouseEvent *event) {
114 dragStartX = event->pos().x() - offset;
115 setCursor(Qt::ClosedHandCursor);
118 void TopicLabel::mouseReleaseEvent(QMouseEvent *event) {
121 if(qAbs(offset) < 30) {
125 setCursor(Qt::OpenHandCursor);
128 void TopicLabel::mouseDoubleClickEvent(QMouseEvent *event) {
130 if(textPartOffset.isEmpty())
133 // find the text part that contains the url. We don't expect color codes in urls so we expect only full parts (yet?)
135 int x = event->pos().x() + offset;
136 while(textPart + 1 < textPartOffset.count()) {
137 if(textPartOffset[textPart + 1] < x)
142 int textOffset = textPartOffset[textPart];
144 // we've Identified the needed text part \o/
145 QString text = plainText.mid(formatList[textPart].start, formatList[textPart].length);
147 // now we have to find the the left and right word delimiters of the clicked word
148 QFontMetrics fontMetric(formatList[textPart].format.font());
151 int spacePos = text.indexOf(" ");
152 x -= offset; // offset needs to go here as it's already in the textOffset
153 while(spacePos != -1) {
154 if(fontMetric.width(text.left(spacePos + 1)) + textOffset < x) {
155 start = spacePos + 1;
156 spacePos = text.indexOf(" ", start + 1);
162 int end = text.indexOf(" ", start);
167 QString word = text.mid(start, len);
168 QRegExp regex("^(h|f)t{1,2}ps?:\\/\\/");
169 if(regex.indexIn(word) != -1) {
170 QDesktopServices::openUrl(QUrl(word));