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