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 _fontMetrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
36 ChatItem::~ChatItem() {
40 QVariant ChatItem::data(int role) const {
41 if(!_index.isValid()) {
42 qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
45 return _index.data(role);
48 int ChatItem::setWidth(int w) {
49 if(w == _boundingRect.width()) return _boundingRect.height();
50 prepareGeometryChange();
51 _boundingRect.setWidth(w);
52 int h = heightForWidth(w);
53 _boundingRect.setHeight(h);
54 if(haveLayout()) updateLayout();
58 int ChatItem::heightForWidth(int width) {
59 if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
60 return fontMetrics()->lineSpacing(); // only contents can be multi-line
63 WrapColumnFinder finder(this);
64 while(finder.nextWrapColumn() > 0) _lines++;
65 return _lines * fontMetrics()->lineSpacing();
68 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
69 QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
72 option.setWrapMode(wrapMode);
73 option.setAlignment(alignment);
74 layout->setTextOption(option);
76 QList<QTextLayout::FormatRange> formatRanges
77 = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
78 layout->setAdditionalFormats(formatRanges);
82 void ChatItem::updateLayout() {
83 switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
84 case ChatLineModel::TimestampColumn:
85 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
87 case ChatLineModel::SenderColumn:
88 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
89 _layout->beginLayout();
91 QTextLine line = _layout->createLine();
93 line.setLineWidth(width());
94 line.setPosition(QPointF(0, fontMetrics()->leading()));
99 case ChatLineModel::ContentsColumn: {
100 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
103 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
104 if(!wrapList.count()) return; // empty chatitem
106 ChatLineModel::Word word = wrapList.at(0);
109 WrapColumnFinder finder(this);
110 _layout->beginLayout();
112 QTextLine line = _layout->createLine();
116 int col = finder.nextWrapColumn();
117 line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
119 h += fontMetrics()->leading();
120 line.setPosition(QPointF(0, h));
123 _layout->endLayout();
129 void ChatItem::clearLayout() {
134 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
135 Q_UNUSED(option); Q_UNUSED(widget);
136 if(!haveLayout()) updateLayout();
137 _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
138 //painter->drawRect(boundingRect());
143 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
144 qDebug() << (void*)this << "moving" << event->pos();
145 if(event->pos().y() < 0) {
146 QTextCursor cursor(document());
147 //cursor.insertText("foo");
148 //cursor.select(QTextCursor::Document);
150 } else QGraphicsTextItem::mouseMoveEvent(event);
154 /*************************************************************************************************/
156 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
157 wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
165 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
169 int ChatItem::WrapColumnFinder::nextWrapColumn() {
170 while(wordidx < wrapList.count()) {
171 w += wrapList.at(wordidx).width;
172 if(w >= item->width()) {
173 if(lastwrapcol >= wrapList.at(wordidx).start) {
174 // first word, and it doesn't fit
175 if(!line.isValid()) {
176 layout = item->createLayout(QTextOption::NoWrap);
177 layout->beginLayout();
178 line = layout->createLine();
179 line.setLineWidth(item->width());
182 int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
183 qreal x = line.cursorToX(idx, QTextLine::Trailing);
184 w = w - wrapList.at(wordidx).width - (x - lastwrappos);
189 // not the first word, so just wrap before this
190 lastwrapcol = wrapList.at(wordidx).start;
191 lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
195 w += wrapList.at(wordidx).trailing;