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 <QApplication>
23 #include <QFontMetrics>
24 #include <QGraphicsSceneMouseEvent>
27 #include <QTextLayout>
30 #include "chatlinemodel.h"
33 ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
34 _fontMetrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
40 ChatItem::~ChatItem() {
44 QVariant ChatItem::data(int role) const {
45 if(!_index.isValid()) {
46 qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
49 return _index.data(role);
52 int ChatItem::setWidth(int w) {
53 if(w == _boundingRect.width()) return _boundingRect.height();
54 prepareGeometryChange();
55 _boundingRect.setWidth(w);
56 int h = heightForWidth(w);
57 _boundingRect.setHeight(h);
58 if(haveLayout()) updateLayout();
62 int ChatItem::heightForWidth(int width) {
63 if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
64 return fontMetrics()->lineSpacing(); // only contents can be multi-line
67 WrapColumnFinder finder(this);
68 while(finder.nextWrapColumn() > 0) _lines++;
69 return _lines * fontMetrics()->lineSpacing();
72 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
73 QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
76 option.setWrapMode(wrapMode);
77 option.setAlignment(alignment);
78 layout->setTextOption(option);
80 QList<QTextLayout::FormatRange> formatRanges
81 = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
82 layout->setAdditionalFormats(formatRanges);
86 void ChatItem::updateLayout() {
87 switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
88 case ChatLineModel::TimestampColumn:
89 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
91 case ChatLineModel::SenderColumn:
92 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
93 _layout->beginLayout();
95 QTextLine line = _layout->createLine();
97 line.setLineWidth(width());
98 line.setPosition(QPointF(0, fontMetrics()->leading()));
100 _layout->endLayout();
103 case ChatLineModel::ContentsColumn: {
104 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
107 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
108 if(!wrapList.count()) return; // empty chatitem
110 ChatLineModel::Word word = wrapList.at(0);
113 WrapColumnFinder finder(this);
114 _layout->beginLayout();
116 QTextLine line = _layout->createLine();
120 int col = finder.nextWrapColumn();
121 line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
123 h += fontMetrics()->leading();
124 line.setPosition(QPointF(0, h));
127 _layout->endLayout();
133 void ChatItem::clearLayout() {
138 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
139 Q_UNUSED(option); Q_UNUSED(widget);
140 if(!haveLayout()) updateLayout();
141 _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
144 int ChatItem::posToCursor(const QPointF &pos) {
145 if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
146 if(pos.y() < 0) return 0;
147 if(!haveLayout()) updateLayout();
148 for(int l = _layout->lineCount() - 1; l >= 0; l--) {
149 QTextLine line = _layout->lineAt(l);
150 if(pos.y() >= line.y()) {
151 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
157 void ChatItem::clearSelection() {
158 if(_selectionStart >= 0) {
159 QList<QTextLayout::FormatRange> formats = _layout->additionalFormats();
160 formats.removeLast();
161 _layout->setAdditionalFormats(formats);
162 _selectionStart = -1;
168 void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
169 int selectionEnd = posToCursor(event->pos());
170 QList<QTextLayout::FormatRange> formats = _layout->additionalFormats();
171 formats.last().start = qMin(_selectionStart, selectionEnd);
172 formats.last().length = qMax(_selectionStart, selectionEnd) - formats.last().start;
173 _layout->setAdditionalFormats(formats);
178 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
179 if(event->buttons() & Qt::LeftButton) {
180 if(!haveLayout()) updateLayout();
181 chatScene()->setSelectingItem(this);
182 _selectionStart = posToCursor(event->pos());
183 QList<QTextLayout::FormatRange> formats = _layout->additionalFormats();
184 QTextLayout::FormatRange selectFmt;
185 QPalette pal = QApplication::palette();
186 selectFmt.format.setForeground(pal.brush(QPalette::HighlightedText));
187 selectFmt.format.setBackground(pal.brush(QPalette::Highlight));
188 selectFmt.length = 0;
189 formats.append(selectFmt);
190 _layout->setAdditionalFormats(formats);
199 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
200 if(_selectionStart >= 0) {
201 int selectionEnd = posToCursor(event->pos());
203 = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, selectionEnd), qAbs(_selectionStart - selectionEnd));
204 QApplication::clipboard()->setText(selection, QClipboard::Clipboard); // TODO configure where selections should go
211 /*************************************************************************************************/
213 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
214 wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
222 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
226 int ChatItem::WrapColumnFinder::nextWrapColumn() {
227 while(wordidx < wrapList.count()) {
228 w += wrapList.at(wordidx).width;
229 if(w >= item->width()) {
230 if(lastwrapcol >= wrapList.at(wordidx).start) {
231 // first word, and it doesn't fit
232 if(!line.isValid()) {
233 layout = item->createLayout(QTextOption::NoWrap);
234 layout->beginLayout();
235 line = layout->createLine();
236 line.setLineWidth(item->width());
239 int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
240 qreal x = line.cursorToX(idx, QTextLine::Trailing);
241 w = w - wrapList.at(wordidx).width - (x - lastwrappos);
246 // not the first word, so just wrap before this
247 lastwrapcol = wrapList.at(wordidx).start;
248 lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
252 w += wrapList.at(wordidx).trailing;