Compute correct height of ChatItems.
[quassel.git] / src / qtui / chatitem.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 <QFontMetrics>
22 #include <QGraphicsSceneMouseEvent>
23 #include <QTextCursor>
24 #include <QTextDocument>
25
26 #include <QtGui>
27
28 #include "chatitem.h"
29 #include "chatlinemodel.h"
30 #include "qtui.h"
31
32 ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
33   QFontMetricsF *metrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
34   _lineHeight = metrics->lineSpacing();
35 }
36
37 ChatItem::~ChatItem() {
38
39 }
40
41 QVariant ChatItem::data(int role) const {
42   if(!_index.isValid()) {
43     qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
44     return QVariant();
45   }
46   return _index.data(role);
47 }
48
49 /*
50 QRectF ChatItem::boundingRect() const {
51   return QRectF(0, 0, _width, _height);
52 }
53 */
54
55 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
56   Q_UNUSED(option); Q_UNUSED(widget);
57
58   painter->drawText(boundingRect(), data(MessageModel::DisplayRole).toString());
59   painter->setPen(Qt::DotLine);
60   painter->drawRect(boundingRect());
61 }
62
63 int ChatItem::setWidth(int w) {
64   if(w == _boundingRect.width()) return _boundingRect.height();
65   int h = heightForWidth(w);
66   _boundingRect.setWidth(w);
67   _boundingRect.setHeight(h);
68   return h;
69 }
70
71 int ChatItem::heightForWidth(int width) {
72   if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
73     return _lineHeight; // only contents can be multi-line
74
75   QVariantList wrapList = data(ChatLineModel::WrapListRole).toList();
76   int lines = 1;
77   int offset = 0;
78   for(int i = 0; i < wrapList.count(); i+=2) {
79     if(wrapList.at(i+1).toUInt() - offset < width) continue;
80     lines++;
81     if(i > 0) {
82       if(offset != wrapList.at(i-1).toUInt()) offset = wrapList.at(i-1).toUInt();
83       else offset += width;
84     } else {
85       offset += width;
86     }
87     i-=2;
88   }
89   return lines * _lineHeight;
90 }
91
92 /*
93
94 void ChatItem::setTextOption(const QTextOption &option) {
95   _textOption = option;
96   layout();
97 }
98
99 QTextOption ChatItem::textOption() const {
100   return _textOption;
101 }
102
103 QString ChatItem::text() const {
104   return _layout.text();
105 }
106
107 void ChatItem::setText(const UiStyle::StyledText &text) {
108   _layout.setText(text.text);
109   _layout.setAdditionalFormats(text.formatList);
110   layout();
111 }
112
113 void ChatItem::layout() {
114   if(!_layout.additionalFormats().count()) return; // no text set
115   if(_width <= 0) return;
116   prepareGeometryChange();
117   QFontMetrics metrics(_layout.additionalFormats()[0].format.font());
118   int leading = metrics.leading();
119   int height = 0;
120   _layout.setTextOption(textOption());
121   _layout.beginLayout();
122   while(1) {
123     QTextLine line = _layout.createLine();
124     if(!line.isValid()) break;
125     line.setLineWidth(_width);
126     if(textOption().wrapMode() != QTextOption::NoWrap && line.naturalTextWidth() > _width) {
127       // word did not fit, we need to wrap it in the middle
128       // this is a workaround for Qt failing to handle WrapAtWordBoundaryOrAnywhere correctly
129       QTextOption::WrapMode mode = textOption().wrapMode();
130       textOption().setWrapMode(QTextOption::WrapAnywhere);
131       _layout.setTextOption(textOption());
132       line.setLineWidth(_width);
133       textOption().setWrapMode(mode);
134       _layout.setTextOption(textOption());
135     }
136     height += leading;
137     line.setPosition(QPoint(0, height));
138     height += line.height();
139   }
140   _layout.endLayout();
141   update();
142 }    QDateTime _timestamp;
143     MsgId _msgId;
144
145
146 QRectF ChatItem::boundingRect() const {
147   return _layout.boundingRect();
148 }
149
150 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
151   Q_UNUSED(option); Q_UNUSED(widget);
152   _layout.draw(painter, QPointF(0, 0));
153
154 }
155 */
156
157 /*
158 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
159   qDebug() << (void*)this << "moving" << event->pos();
160   if(event->pos().y() < 0) {
161     QTextCursor cursor(document());
162     //cursor.insertText("foo");
163     //cursor.select(QTextCursor::Document);
164     event->ignore();
165   } else QGraphicsTextItem::mouseMoveEvent(event);
166 }
167 */
168
169
170