1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QFontMetrics>
22 #include <QGraphicsSceneMouseEvent>
24 #include <QTextLayout>
27 #include "chatlinemodel.h"
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();
37 ChatItem::~ChatItem() {
41 QVariant ChatItem::data(int role) const {
42 if(!_index.isValid()) {
43 qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
46 return _index.data(role);
49 int ChatItem::setWidth(int w) {
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();
59 int ChatItem::heightForWidth(int width) {
60 if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
61 return _lineHeight; // only contents can be multi-line
63 QVariantList wrapList = data(ChatLineModel::WrapListRole).toList();
66 for(int i = 0; i < wrapList.count(); i+=2) {
67 if(wrapList.at(i+1).toUInt() - offset < width) continue;
70 if(offset < wrapList.at(i-1).toUInt()) offset = wrapList.at(i-1).toUInt();
77 return lines * _lineHeight;
80 void ChatItem::layout() {
81 if(haveLayout()) return;
82 _layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
84 // Convert format information into a FormatRange
85 QList<QTextLayout::FormatRange> formatRanges;
86 UiStyle::FormatList formatList = data(MessageModel::FormatRole).value<UiStyle::FormatList>();
87 QTextLayout::FormatRange range;
89 for(i = 0; i < formatList.count(); i++) {
90 range.format = QtUi::style()->mergedFormat(formatList.at(i).second);
91 range.start = formatList.at(i).first;
92 if(i > 0) formatRanges.last().length = range.start - formatRanges.last().start;
93 formatRanges.append(range);
95 if(i > 0) formatRanges.last().length = _layout->text().length() - formatRanges.last().start;
96 _layout->setAdditionalFormats(formatRanges);
100 void ChatItem::updateLayout() {
101 if(!haveLayout()) layout();
105 _layout->beginLayout();
107 QTextLine line = _layout->createLine();
111 line.setLineWidth(width());
113 line.setPosition(QPointF(0, h));
116 _layout->endLayout();
119 void ChatItem::clearLayout() {
124 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
125 Q_UNUSED(option); Q_UNUSED(widget);
127 _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
128 painter->drawRect(boundingRect());
132 void ChatItem::layout() {
133 if(!_layout.additionalFormats().count()) return; // no text set
134 if(_width <= 0) return;
135 prepareGeometryChange();
136 QFontMetrics metrics(_layout.additionalFormats()[0].format.font());
137 int leading = metrics.leading();
139 _layout.setTextOption(textOption());
140 _layout.beginLayout();
142 QTextLine line = _layout.createLine();
143 if(!line.isValid()) break;
144 line.setLineWidth(_width);
145 if(textOption().wrapMode() != QTextOption::NoWrap && line.naturalTextWidth() > _width) {
146 // word did not fit, we need to wrap it in the middle
147 // this is a workaround for Qt failing to handle WrapAtWordBoundaryOrAnywhere correctly
148 QTextOption::WrapMode mode = textOption().wrapMode();
149 textOption().setWrapMode(QTextOption::WrapAnywhere);
150 _layout.setTextOption(textOption());
151 line.setLineWidth(_width);
152 textOption().setWrapMode(mode);
153 _layout.setTextOption(textOption());
156 line.setPosition(QPoint(0, height));
157 height += line.height();
161 } QDateTime _timestamp;
165 QRectF ChatItem::boundingRect() const {
166 return _layout.boundingRect();
169 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
170 Q_UNUSED(option); Q_UNUSED(widget);
171 _layout.draw(painter, QPointF(0, 0));
177 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
178 qDebug() << (void*)this << "moving" << event->pos();
179 if(event->pos().y() < 0) {
180 QTextCursor cursor(document());
181 //cursor.insertText("foo");
182 //cursor.select(QTextCursor::Document);
184 } else QGraphicsTextItem::mouseMoveEvent(event);