usability++ (drag mousecursor, tooltip, limited scrolling)
[quassel.git] / src / qtui / topiclabel.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 "topiclabel.h"
22
23
24 #include <QDebug>
25
26 #include <QApplication>
27 #include <QDesktopServices>
28 #include <QPainter>
29 // #include <QHBoxLayout>
30 #include <QFont>
31 #include <QFontMetrics>
32
33 #include "qtui.h"
34 #include "message.h"
35
36 TopicLabel::TopicLabel(QWidget *parent)
37   : QFrame(parent),
38     offset(0),
39     dragStartX(0),
40     textWidth(0),
41     dragMode(false)
42 {
43   setToolTip(tr("Drag to scroll the topic!"));
44   setCursor(Qt::OpenHandCursor);
45 }
46
47 void TopicLabel::paintEvent(QPaintEvent *event) {
48   Q_UNUSED(event);
49
50   textPartOffset.clear();
51   
52   QPainter painter(this);
53   painter.setBackgroundMode(Qt::OpaqueMode);
54
55   // FIXME re-enable topic painting
56 #ifndef SPUTDEV
57   QRect drawRect = rect().adjusted(offset, 0, 0, 0);
58   QRect brect;
59   QString textPart;
60   foreach(QTextLayout::FormatRange fr, styledContents.formatList) {
61     textPart = styledContents.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());
68   }
69   textWidth = brect.right();
70 #endif
71 }
72
73 void TopicLabel::setText(const QString &text) {
74   if(_text == text)
75     return;
76
77   _text = text;
78   offset = 0;
79   update();
80
81 #ifndef SPUTDEV
82   styledContents = QtUi::style()->styleString(Message::mircToInternal(text));
83   int height = 1;
84   foreach(QTextLayout::FormatRange fr, styledContents.formatList) {
85     height = qMax(height, QFontMetrics(fr.format.font()).height());
86   }
87
88   // ensure the label is editable (height != 1) if there is no text to show
89   if(text.isEmpty())
90     height = QFontMetrics(qApp->font()).height();
91   
92   // setFixedHeight(height);
93 #endif  
94   // show topic in tooltip
95 }
96
97
98 void TopicLabel::mouseMoveEvent(QMouseEvent *event) {
99   if(!dragMode)
100     return;
101
102   event->accept();
103   int newOffset = event->pos().x() - dragStartX;
104   if(newOffset > 0)
105     offset = 0;
106   else if(width() < textWidth || offset < newOffset)
107     offset = newOffset;
108 //     qDebug() << offset << (width() - textWidth) << width() << textWidth;
109 //     if(offset < width() - textWidth)
110 //       offset = width() - textWidth;
111   update();
112 }
113
114 void TopicLabel::mousePressEvent(QMouseEvent *event) {
115   event->accept();
116   dragMode = true;
117   dragStartX = event->pos().x() - offset;
118   setCursor(Qt::ClosedHandCursor);
119 }
120
121 void TopicLabel::mouseReleaseEvent(QMouseEvent *event) {
122   event->accept();
123   dragMode = false;
124   if(qAbs(offset) < 30) {
125     offset = 0;
126     update();
127   }
128   setCursor(Qt::OpenHandCursor);
129 }
130
131 void TopicLabel::mouseDoubleClickEvent(QMouseEvent *event) {
132 #ifndef SPUTDEV
133   event->accept();
134   int textPart = 0;
135   int textOffset = 0;
136
137   if(textPartOffset.isEmpty())
138     return;
139
140   // find the text part that contains the url. We don't expect color codes in urls so we expect only full parts (yet?)
141   int x = event->pos().x();
142   while(textPart + 1 < textPartOffset.count()) {
143     if(textPartOffset[textPart + 1] < x) {
144       textPart++;
145       textOffset = textPartOffset[textPart];
146     } else {
147       break;
148     }
149   }
150
151   // we've Identified the needed text part \o/
152   QString text = styledContents.plainText.mid(styledContents.formatList[textPart].start, styledContents.formatList[textPart].length);
153
154   // now we have to find the the left and right word delimiters of the clicked word
155   QFontMetrics fontMetric(styledContents.formatList[textPart].format.font());
156   
157   int start = 0;
158   int spacePos = text.indexOf(" ");
159   while(spacePos != -1) {
160     if(fontMetric.width(text.left(spacePos + 1)) + textOffset < x) {
161       start = spacePos + 1;
162       spacePos = text.indexOf(" ", start + 1);
163     } else {
164       break;
165     }
166   }
167
168   int end = text.indexOf(" ", start);
169   int len = -1;
170   if(end != -1) {
171     len = end - start;
172   }
173   QString word = text.mid(start, len);
174   qDebug() << word;
175   QRegExp regex("^(h|f)t{1,2}ps?:\\/\\/");
176   if(regex.indexIn(word) != -1) {
177     QDesktopServices::openUrl(QUrl(word));
178   }
179 #endif
180   
181 }