I CAN HAZ WORDWRAPZcd /usr/local/layman/portage-kdecd devel/quassel
[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   _fontMetrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
32   _layout = 0;
33   _lines = 0;
34 }
35
36 ChatItem::~ChatItem() {
37
38 }
39
40 QVariant ChatItem::data(int role) const {
41   if(!_index.isValid()) {
42     qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
43     return QVariant();
44   }
45   return _index.data(role);
46 }
47
48 int ChatItem::setWidth(int w) {
49   w -= 10;
50   if(w == _boundingRect.width()) return _boundingRect.height();
51   _boundingRect.setWidth(w);
52   int h = heightForWidth(w);
53   _boundingRect.setHeight(h);
54   if(haveLayout()) updateLayout();
55   return h;
56 }
57
58 int ChatItem::heightForWidth(int width) {
59   if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
60     return fontMetrics()->lineSpacing(); // only contents can be multi-line
61
62   _lines = 1;
63   WrapColumnFinder finder(this);
64   while(finder.nextWrapColumn() > 0) _lines++;
65   return _lines * fontMetrics()->lineSpacing();
66 }
67
68 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
69   wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
70   wordidx = 0;
71   layout = 0;
72   lastwrapcol = 0;
73   lastwrappos = 0;
74   w = 0;
75 }
76
77 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
78   delete layout;
79 }
80
81 int ChatItem::WrapColumnFinder::nextWrapColumn() {
82   while(wordidx < wrapList.count()) {
83     w += wrapList.at(wordidx).width;
84     if(w >= item->width()) {
85       if(lastwrapcol >= wrapList.at(wordidx).start) {
86         // first word, and it doesn't fit
87         if(!line.isValid()) {
88           layout = item->createLayout(QTextOption::NoWrap);
89           layout->beginLayout();
90           line = layout->createLine();
91           line.setLineWidth(item->width());
92           layout->endLayout();
93         }
94         int idx = line.xToCursor(lastwrappos + item->width());
95         qreal x = line.cursorToX(idx);
96         w = w - wrapList.at(wordidx).width - (x - lastwrappos);
97         lastwrappos = x;
98         lastwrapcol = idx;
99         return idx;
100       }
101       // not the first word, so just wrap before this
102       lastwrapcol = wrapList.at(wordidx).start;
103       lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
104       w = 0;
105       return lastwrapcol;
106     }
107     w += wrapList.at(wordidx).trailing;
108     wordidx++;
109   }
110   return -1;
111 }
112
113 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode) {
114   QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
115
116   QTextOption option;
117   option.setWrapMode(wrapMode);
118   layout->setTextOption(option);
119
120   QList<QTextLayout::FormatRange> formatRanges
121          = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
122   layout->setAdditionalFormats(formatRanges);
123   return layout;
124 }
125
126 void ChatItem::updateLayout() {
127   if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
128
129   // Now layout
130   ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
131   if(!wrapList.count()) return; // empty chatitem
132   int wordidx = 0;
133   ChatLineModel::Word word = wrapList.at(0);
134
135   qreal h = 0;
136   WrapColumnFinder finder(this);
137   _layout->beginLayout();
138   forever {
139     QTextLine line = _layout->createLine();
140     if (!line.isValid())
141       break;
142
143     int col = finder.nextWrapColumn();
144     line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
145
146     h += fontMetrics()->leading();
147     line.setPosition(QPointF(0, h));
148     h += line.height();
149   }
150   _layout->endLayout();
151 }
152
153 void ChatItem::clearLayout() {
154   delete _layout;
155   _layout = 0;
156 }
157
158 //int ChatItem::findNextWrapColumn(
159
160 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
161   Q_UNUSED(option); Q_UNUSED(widget);
162   if(!haveLayout()) updateLayout();
163   _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
164   painter->drawRect(boundingRect());
165   int width = 0;
166   QVariantList wrapList = data(ChatLineModel::WrapListRole).toList();
167   for(int i = 2; i < wrapList.count(); i+=2) {
168     QRect r(wrapList[i-1].toUInt(), 0, wrapList[i+1].toUInt() - wrapList[i-1].toUInt(), fontMetrics()->lineSpacing());
169     painter->drawRect(r);
170   }
171 }
172
173
174 /*
175 void ChatItem::layout() {
176   if(!_layout.additionalFormats().count()) return; // no text set
177   if(_width <= 0) return;
178   prepareGeometryChange();
179   QFontMetrics metrics(_layout.additionalFormats()[0].format.font());
180   int leading = metrics.leading();
181   int height = 0;
182   _layout.setTextOption(textOption());
183   _layout.beginLayout();
184   while(1) {
185     QTextLine line = _layout.createLine();
186     if(!line.isValid()) break;
187     line.setLineWidth(_width);
188     if(textOption().wrapMode() != QTextOption::NoWrap && line.naturalTextWidth() > _width) {
189       // word did not fit, we need to wrap it in the middle
190       // this is a workaround for Qt failing to handle WrapAtWordBoundaryOrAnywhere correctly
191       QTextOption::WrapMode mode = textOption().wrapMode();
192       textOption().setWrapMode(QTextOption::WrapAnywhere);
193       _layout.setTextOption(textOption());
194       line.setLineWidth(_width);
195       textOption().setWrapMode(mode);
196       _layout.setTextOption(textOption());
197     }
198     height += leading;
199     line.setPosition(QPoint(0, height));
200     height += line.height();
201   }
202   _layout.endLayout();
203   update();
204 }    QDateTime _timestamp;
205     MsgId _msgId;
206
207
208 QRectF ChatItem::boundingRect() const {
209   return _layout.boundingRect();
210 }
211
212 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
213   Q_UNUSED(option); Q_UNUSED(widget);
214   _layout.draw(painter, QPointF(0, 0));
215
216 }
217 */
218
219 /*
220 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
221   qDebug() << (void*)this << "moving" << event->pos();
222   if(event->pos().y() < 0) {
223     QTextCursor cursor(document());
224     //cursor.insertText("foo");
225     //cursor.select(QTextCursor::Document);
226     event->ignore();
227   } else QGraphicsTextItem::mouseMoveEvent(event);
228 }
229 */