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;
39 setAcceptHoverEvents(true);
43 ChatItem::~ChatItem() {
47 QVariant ChatItem::data(int role) const {
48 if(!_index.isValid()) {
49 qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
52 return _index.data(role);
55 qreal ChatItem::setWidth(qreal w) {
56 if(w == _boundingRect.width()) return _boundingRect.height();
57 prepareGeometryChange();
58 _boundingRect.setWidth(w);
59 qreal h = computeHeight();
60 _boundingRect.setHeight(h);
61 if(haveLayout()) updateLayout();
65 qreal ChatItem::computeHeight() {
66 if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
67 return fontMetrics()->lineSpacing(); // only contents can be multi-line
70 WrapColumnFinder finder(this);
71 while(finder.nextWrapColumn() > 0) _lines++;
72 return _lines * fontMetrics()->lineSpacing();
75 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
76 QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
79 option.setWrapMode(wrapMode);
80 option.setAlignment(alignment);
81 layout->setTextOption(option);
83 QList<QTextLayout::FormatRange> formatRanges
84 = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
85 layout->setAdditionalFormats(formatRanges);
89 void ChatItem::updateLayout() {
90 switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
91 case ChatLineModel::TimestampColumn:
92 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
94 case ChatLineModel::SenderColumn:
95 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
96 _layout->beginLayout();
98 QTextLine line = _layout->createLine();
100 line.setLineWidth(width());
101 line.setPosition(QPointF(0,0));
103 _layout->endLayout();
106 case ChatLineModel::ContentsColumn: {
107 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
110 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
111 if(!wrapList.count()) return; // empty chatitem
114 WrapColumnFinder finder(this);
115 _layout->beginLayout();
117 QTextLine line = _layout->createLine();
121 int col = finder.nextWrapColumn();
122 line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
123 line.setPosition(QPointF(0, h));
124 h += line.height() + fontMetrics()->leading();
126 _layout->endLayout();
132 void ChatItem::clearLayout() {
137 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
138 // This is a deliberate trade-off. (-> selectFmt creation, data() call)
139 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
140 Q_UNUSED(option); Q_UNUSED(widget);
141 if(!haveLayout()) updateLayout();
142 painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
143 //if(_selectionMode == FullSelection) {
145 //painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight));
146 //painter->restore();
148 QVector<QTextLayout::FormatRange> formats;
149 if(_selectionMode != NoSelection) {
150 QTextLayout::FormatRange selectFmt;
151 selectFmt.format.setForeground(QApplication::palette().brush(QPalette::HighlightedText));
152 selectFmt.format.setBackground(QApplication::palette().brush(QPalette::Highlight));
153 if(_selectionMode == PartialSelection) {
154 selectFmt.start = qMin(_selectionStart, _selectionEnd);
155 selectFmt.length = qAbs(_selectionStart - _selectionEnd);
156 } else { // FullSelection
158 selectFmt.length = data(MessageModel::DisplayRole).toString().length();
160 formats.append(selectFmt);
162 _layout->draw(painter, QPointF(0,0), formats, boundingRect());
165 qint16 ChatItem::posToCursor(const QPointF &pos) {
166 if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
167 if(pos.y() < 0) return 0;
168 if(!haveLayout()) updateLayout();
169 for(int l = _layout->lineCount() - 1; l >= 0; l--) {
170 QTextLine line = _layout->lineAt(l);
171 if(pos.y() >= line.y()) {
172 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
178 void ChatItem::setFullSelection() {
179 if(_selectionMode != FullSelection) {
180 _selectionMode = FullSelection;
185 void ChatItem::clearSelection() {
186 if(_selectionMode != NoSelection) {
187 _selectionMode = NoSelection;
192 void ChatItem::continueSelecting(const QPointF &pos) {
193 _selectionMode = PartialSelection;
194 _selectionEnd = posToCursor(pos);
198 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
199 if(event->buttons() & Qt::LeftButton) {
200 if(_selectionMode == NoSelection) {
201 chatScene()->setSelectingItem(this); // removes earlier selection if exists
202 _selectionStart = _selectionEnd = posToCursor(event->pos());
203 _selectionMode = PartialSelection;
205 chatScene()->setSelectingItem(0);
206 _selectionMode = NoSelection;
215 void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
216 if(contains(event->pos())) {
217 qint16 end = posToCursor(event->pos());
218 if(end != _selectionEnd) {
224 chatScene()->startGlobalSelection(this, event->pos());
228 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
229 if(_selectionMode != NoSelection) {
230 _selectionEnd = posToCursor(event->pos());
232 = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
233 QApplication::clipboard()->setText(selection, QClipboard::Clipboard); // TODO configure where selections should go
240 void ChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
241 //qDebug() << (void*)this << "entering";
245 void ChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
246 //qDebug() << (void*)this << "leaving";
250 void ChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
251 //qDebug() << (void*)this << event->pos();
256 /*************************************************************************************************/
258 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
259 wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
267 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
271 qint16 ChatItem::WrapColumnFinder::nextWrapColumn() {
272 while(wordidx < wrapList.count()) {
273 w += wrapList.at(wordidx).width;
274 if(w >= item->width()) {
275 if(lastwrapcol >= wrapList.at(wordidx).start) {
276 // first word, and it doesn't fit
277 if(!line.isValid()) {
278 layout = item->createLayout(QTextOption::NoWrap);
279 layout->beginLayout();
280 line = layout->createLine();
281 line.setLineWidth(item->width());
284 int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
285 qreal x = line.cursorToX(idx, QTextLine::Trailing);
286 w = w - wrapList.at(wordidx).width - (x - lastwrappos);
291 // not the first word, so just wrap before this
292 lastwrapcol = wrapList.at(wordidx).start;
293 lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
297 w += wrapList.at(wordidx).trailing;