Reworking the wordwrap stuff.
[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 <QPainter>
24 #include <QTextLayout>
25
26 #include "chatitem.h"
27 #include "chatlinemodel.h"
28 #include "qtui.h"
29
30 ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
31   QFontMetricsF *metrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
32   _lineHeight = metrics->lineSpacing();
33   _lineLeading = metrics->leading();
34   _layout = 0;
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 int ChatItem::setWidth(int w) {
50   w -= 10;
51   if(w == _boundingRect.width()) return _boundingRect.height();
52   int h = heightForWidth(w);
53   _boundingRect.setWidth(w);
54   _boundingRect.setHeight(h);
55   if(haveLayout()) updateLayout();
56   return h;
57 }
58
59 int ChatItem::heightForWidth(int width) {
60   if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
61     return _lineHeight; // only contents can be multi-line
62
63   ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
64   int lines = 1;
65   qreal w = 0;
66   for(int i = 0; i < wrapList.count(); i++) {
67     w += wrapList.at(i).width;
68     if(w <= width) {
69       w += wrapList.at(i).trailing;
70       continue;
71     }
72     lines++;
73     w = wrapList.at(i).width;
74     while(w >= width) {
75       lines++;
76       w -= width;
77     }
78   }
79   return lines * _lineHeight;
80 }
81
82 void ChatItem::layout() {
83   if(haveLayout()) return;
84   _layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
85
86   // Convert format information into a FormatRange
87   QList<QTextLayout::FormatRange> formatRanges;
88   UiStyle::FormatList formatList = data(MessageModel::FormatRole).value<UiStyle::FormatList>();
89   QTextLayout::FormatRange range;
90   int i = 0;
91   for(i = 0; i < formatList.count(); i++) {
92     range.format = QtUi::style()->mergedFormat(formatList.at(i).second);
93     range.start = formatList.at(i).first;
94     if(i > 0) formatRanges.last().length = range.start - formatRanges.last().start;
95     formatRanges.append(range);
96   }
97   if(i > 0) formatRanges.last().length = _layout->text().length() - formatRanges.last().start;
98   _layout->setAdditionalFormats(formatRanges);
99   updateLayout();
100 }
101
102 void ChatItem::updateLayout() {
103   if(!haveLayout()) layout();
104
105   // Now layout
106   qreal h = 0;
107   _layout->beginLayout();
108   forever {
109     QTextLine line = _layout->createLine();
110     if (!line.isValid())
111       break;
112
113     line.setLineWidth(width());
114     h += _lineLeading;
115     line.setPosition(QPointF(0, h));
116     h += line.height();
117   }
118   _layout->endLayout();
119 }
120
121 void ChatItem::clearLayout() {
122   delete _layout;
123   _layout = 0;
124 }
125
126 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
127   Q_UNUSED(option); Q_UNUSED(widget);
128   layout();
129   _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
130   painter->drawRect(boundingRect());
131   int width = 0;
132   QVariantList wrapList = data(ChatLineModel::WrapListRole).toList();
133   for(int i = 2; i < wrapList.count(); i+=2) {
134     QRect r(wrapList[i-1].toUInt(), 0, wrapList[i+1].toUInt() - wrapList[i-1].toUInt(), _lineHeight);
135     painter->drawRect(r);
136   }
137 }
138
139 /*
140 void ChatItem::layout() {
141   if(!_layout.additionalFormats().count()) return; // no text set
142   if(_width <= 0) return;
143   prepareGeometryChange();
144   QFontMetrics metrics(_layout.additionalFormats()[0].format.font());
145   int leading = metrics.leading();
146   int height = 0;
147   _layout.setTextOption(textOption());
148   _layout.beginLayout();
149   while(1) {
150     QTextLine line = _layout.createLine();
151     if(!line.isValid()) break;
152     line.setLineWidth(_width);
153     if(textOption().wrapMode() != QTextOption::NoWrap && line.naturalTextWidth() > _width) {
154       // word did not fit, we need to wrap it in the middle
155       // this is a workaround for Qt failing to handle WrapAtWordBoundaryOrAnywhere correctly
156       QTextOption::WrapMode mode = textOption().wrapMode();
157       textOption().setWrapMode(QTextOption::WrapAnywhere);
158       _layout.setTextOption(textOption());
159       line.setLineWidth(_width);
160       textOption().setWrapMode(mode);
161       _layout.setTextOption(textOption());
162     }
163     height += leading;
164     line.setPosition(QPoint(0, height));
165     height += line.height();
166   }
167   _layout.endLayout();
168   update();
169 }    QDateTime _timestamp;
170     MsgId _msgId;
171
172
173 QRectF ChatItem::boundingRect() const {
174   return _layout.boundingRect();
175 }
176
177 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
178   Q_UNUSED(option); Q_UNUSED(widget);
179   _layout.draw(painter, QPointF(0, 0));
180
181 }
182 */
183
184 /*
185 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
186   qDebug() << (void*)this << "moving" << event->pos();
187   if(event->pos().y() < 0) {
188     QTextCursor cursor(document());
189     //cursor.insertText("foo");
190     //cursor.select(QTextCursor::Document);
191     event->ignore();
192   } else QGraphicsTextItem::mouseMoveEvent(event);
193 }
194 */