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);
38 _selectionMode = NoSelection;
41 ChatItem::~ChatItem() {
45 QVariant ChatItem::data(int role) const {
46 if(!_index.isValid()) {
47 qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
50 return _index.data(role);
53 qreal ChatItem::setWidth(qreal w) {
54 if(w == _boundingRect.width()) return _boundingRect.height();
55 prepareGeometryChange();
56 _boundingRect.setWidth(w);
57 qreal h = computeHeight();
58 _boundingRect.setHeight(h);
59 if(haveLayout()) updateLayout();
63 qreal ChatItem::computeHeight() {
64 if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
65 return fontMetrics()->lineSpacing(); // only contents can be multi-line
68 WrapColumnFinder finder(this);
69 while(finder.nextWrapColumn() > 0) _lines++;
70 return _lines * fontMetrics()->lineSpacing();
73 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
74 QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
77 option.setWrapMode(wrapMode);
78 option.setAlignment(alignment);
79 layout->setTextOption(option);
81 QList<QTextLayout::FormatRange> formatRanges
82 = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
83 layout->setAdditionalFormats(formatRanges);
87 void ChatItem::updateLayout() {
88 switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
89 case ChatLineModel::TimestampColumn:
90 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
92 case ChatLineModel::SenderColumn:
93 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
94 _layout->beginLayout();
96 QTextLine line = _layout->createLine();
98 line.setLineWidth(width());
99 line.setPosition(QPointF(0,0));
101 _layout->endLayout();
104 case ChatLineModel::ContentsColumn: {
105 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
108 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
109 if(!wrapList.count()) return; // empty chatitem
112 WrapColumnFinder finder(this);
113 _layout->beginLayout();
115 QTextLine line = _layout->createLine();
119 int col = finder.nextWrapColumn();
120 line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
121 line.setPosition(QPointF(0, h));
122 h += line.height() + fontMetrics()->leading();
124 _layout->endLayout();
130 void ChatItem::clearLayout() {
135 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
136 // This is a deliberate trade-off. (-> selectFmt creation, data() call)
137 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
138 Q_UNUSED(option); Q_UNUSED(widget);
139 if(!haveLayout()) updateLayout();
140 painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
141 if(_selectionMode == FullSelection) {
143 painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight));
145 } // TODO: add selection format here
146 QVector<QTextLayout::FormatRange> formats;
147 if(_selectionMode != NoSelection) {
148 QTextLayout::FormatRange selectFmt;
149 selectFmt.format.setForeground(QApplication::palette().brush(QPalette::HighlightedText));
150 selectFmt.format.setBackground(QApplication::palette().brush(QPalette::Highlight));
151 if(_selectionMode == PartialSelection) {
152 selectFmt.start = qMin(_selectionStart, _selectionEnd);
153 selectFmt.length = qAbs(_selectionStart - _selectionEnd);
154 } else { // FullSelection
156 selectFmt.length = data(MessageModel::DisplayRole).toString().length();
158 formats.append(selectFmt);
160 _layout->draw(painter, QPointF(0,0), formats, boundingRect());
163 qint16 ChatItem::posToCursor(const QPointF &pos) {
164 if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
165 if(pos.y() < 0) return 0;
166 if(!haveLayout()) updateLayout();
167 for(int l = _layout->lineCount() - 1; l >= 0; l--) {
168 QTextLine line = _layout->lineAt(l);
169 if(pos.y() >= line.y()) {
170 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
176 void ChatItem::setFullSelection() {
177 _selectionMode = FullSelection;
181 void ChatItem::clearSelection() {
182 _selectionMode = NoSelection;
186 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
187 if(event->buttons() & Qt::LeftButton) {
188 chatScene()->setSelectingItem(this); // removes earlier selection if exists
189 _selectionStart = _selectionEnd = posToCursor(event->pos());
190 _selectionMode = PartialSelection;
197 void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
198 if(contains(event->pos())) {
199 _selectionEnd = posToCursor(event->pos());
204 chatScene()->startGlobalSelection(this);
208 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
209 if(_selectionMode != NoSelection) {
210 _selectionEnd = posToCursor(event->pos());
212 = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
213 QApplication::clipboard()->setText(selection, QClipboard::Clipboard); // TODO configure where selections should go
220 /*************************************************************************************************/
222 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
223 wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
231 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
235 qint16 ChatItem::WrapColumnFinder::nextWrapColumn() {
236 while(wordidx < wrapList.count()) {
237 w += wrapList.at(wordidx).width;
238 if(w >= item->width()) {
239 if(lastwrapcol >= wrapList.at(wordidx).start) {
240 // first word, and it doesn't fit
241 if(!line.isValid()) {
242 layout = item->createLayout(QTextOption::NoWrap);
243 layout->beginLayout();
244 line = layout->createLine();
245 line.setLineWidth(item->width());
248 int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
249 qreal x = line.cursorToX(idx, QTextLine::Trailing);
250 w = w - wrapList.at(wordidx).width - (x - lastwrappos);
255 // not the first word, so just wrap before this
256 lastwrapcol = wrapList.at(wordidx).start;
257 lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
261 w += wrapList.at(wordidx).trailing;