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 _boundingRect.setWidth(w);
51 int h = heightForWidth(w);
52 _boundingRect.setHeight(h);
53 if(haveLayout()) updateLayout();
57 int ChatItem::heightForWidth(int width) {
58 if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
59 return fontMetrics()->lineSpacing(); // only contents can be multi-line
62 WrapColumnFinder finder(this);
63 while(finder.nextWrapColumn() > 0) _lines++;
64 return _lines * fontMetrics()->lineSpacing();
67 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
68 QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
71 option.setWrapMode(wrapMode);
72 option.setAlignment(alignment);
73 layout->setTextOption(option);
75 QList<QTextLayout::FormatRange> formatRanges
76 = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
77 layout->setAdditionalFormats(formatRanges);
81 void ChatItem::updateLayout() {
82 switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
83 case ChatLineModel::TimestampColumn:
84 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
86 case ChatLineModel::SenderColumn:
87 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
88 _layout->beginLayout();
90 QTextLine line = _layout->createLine();
92 line.setLineWidth(width());
93 line.setPosition(QPointF(0, fontMetrics()->leading()));
98 case ChatLineModel::ContentsColumn: {
99 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
102 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
103 if(!wrapList.count()) return; // empty chatitem
105 ChatLineModel::Word word = wrapList.at(0);
108 WrapColumnFinder finder(this);
109 _layout->beginLayout();
111 QTextLine line = _layout->createLine();
115 int col = finder.nextWrapColumn();
116 line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
118 h += fontMetrics()->leading();
119 line.setPosition(QPointF(0, h));
122 _layout->endLayout();
128 void ChatItem::clearLayout() {
133 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
134 Q_UNUSED(option); Q_UNUSED(widget);
135 if(!haveLayout()) updateLayout();
136 _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
137 //painter->drawRect(boundingRect());
142 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
143 qDebug() << (void*)this << "moving" << event->pos();
144 if(event->pos().y() < 0) {
145 QTextCursor cursor(document());
146 //cursor.insertText("foo");
147 //cursor.select(QTextCursor::Document);
149 } else QGraphicsTextItem::mouseMoveEvent(event);
153 /*************************************************************************************************/
155 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
156 wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
164 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
168 int ChatItem::WrapColumnFinder::nextWrapColumn() {
169 while(wordidx < wrapList.count()) {
170 w += wrapList.at(wordidx).width;
171 if(w >= item->width()) {
172 if(lastwrapcol >= wrapList.at(wordidx).start) {
173 // first word, and it doesn't fit
174 if(!line.isValid()) {
175 layout = item->createLayout(QTextOption::NoWrap);
176 layout->beginLayout();
177 line = layout->createLine();
178 line.setLineWidth(item->width());
181 int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
182 qreal x = line.cursorToX(idx, QTextLine::Trailing);
183 w = w - wrapList.at(wordidx).width - (x - lastwrappos);
188 // not the first word, so just wrap before this
189 lastwrapcol = wrapList.at(wordidx).start;
190 lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
194 w += wrapList.at(wordidx).trailing;