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 <QDesktopServices>
24 #include <QFontMetrics>
25 #include <QGraphicsSceneMouseEvent>
28 #include <QTextLayout>
31 #include "chatlinemodel.h"
34 ChatItem::ChatItem(int col, QAbstractItemModel *model, QGraphicsItem *parent)
35 : QGraphicsItem(parent),
40 _selectionMode(NoSelection),
44 QModelIndex index = model->index(row(), col);
45 _fontMetrics = QtUi::style()->fontMetrics(model->data(index, ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
46 setAcceptHoverEvents(true);
50 ChatItem::~ChatItem() {
54 QVariant ChatItem::data(int role) const {
55 QModelIndex index = model()->index(row(), column());
56 if(!index.isValid()) {
57 qWarning() << "ChatItem::data(): model index is invalid!" << index;
60 return model()->data(index, role);
63 qreal ChatItem::setWidth(qreal w) {
64 if(w == _boundingRect.width()) return _boundingRect.height();
65 prepareGeometryChange();
66 _boundingRect.setWidth(w);
67 qreal h = computeHeight();
68 _boundingRect.setHeight(h);
69 if(haveLayout()) updateLayout();
73 qreal ChatItem::computeHeight() {
74 if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
75 return fontMetrics()->lineSpacing(); // only contents can be multi-line
78 WrapColumnFinder finder(this);
79 while(finder.nextWrapColumn() > 0) _lines++;
80 return _lines * fontMetrics()->lineSpacing();
83 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
84 QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
87 option.setWrapMode(wrapMode);
88 option.setAlignment(alignment);
89 layout->setTextOption(option);
91 QList<QTextLayout::FormatRange> formatRanges
92 = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
93 layout->setAdditionalFormats(formatRanges);
97 void ChatItem::updateLayout() {
98 switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
99 case ChatLineModel::TimestampColumn:
100 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
102 case ChatLineModel::SenderColumn:
103 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
104 _layout->beginLayout();
106 QTextLine line = _layout->createLine();
108 line.setLineWidth(width());
109 line.setPosition(QPointF(0,0));
111 _layout->endLayout();
114 case ChatLineModel::ContentsColumn: {
115 if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
118 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
119 if(!wrapList.count()) return; // empty chatitem
122 WrapColumnFinder finder(this);
123 _layout->beginLayout();
125 QTextLine line = _layout->createLine();
129 int col = finder.nextWrapColumn();
130 line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
131 line.setPosition(QPointF(0, h));
132 h += line.height() + fontMetrics()->leading();
134 _layout->endLayout();
140 void ChatItem::clearLayout() {
145 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
146 // This is a deliberate trade-off. (-> selectFmt creation, data() call)
147 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
148 Q_UNUSED(option); Q_UNUSED(widget);
149 if(!haveLayout()) updateLayout();
150 painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
151 //if(_selectionMode == FullSelection) {
153 //painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight));
154 //painter->restore();
156 QVector<QTextLayout::FormatRange> formats;
157 if(_selectionMode != NoSelection) {
158 QTextLayout::FormatRange selectFmt;
159 selectFmt.format.setForeground(QApplication::palette().brush(QPalette::HighlightedText));
160 selectFmt.format.setBackground(QApplication::palette().brush(QPalette::Highlight));
161 if(_selectionMode == PartialSelection) {
162 selectFmt.start = qMin(_selectionStart, _selectionEnd);
163 selectFmt.length = qAbs(_selectionStart - _selectionEnd);
164 } else { // FullSelection
166 selectFmt.length = data(MessageModel::DisplayRole).toString().length();
168 formats.append(selectFmt);
170 _layout->draw(painter, QPointF(0,0), formats, boundingRect());
173 qint16 ChatItem::posToCursor(const QPointF &pos) {
174 if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
175 if(pos.y() < 0) return 0;
176 if(!haveLayout()) updateLayout();
177 for(int l = _layout->lineCount() - 1; l >= 0; l--) {
178 QTextLine line = _layout->lineAt(l);
179 if(pos.y() >= line.y()) {
180 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
186 void ChatItem::setFullSelection() {
187 if(_selectionMode != FullSelection) {
188 _selectionMode = FullSelection;
193 void ChatItem::clearSelection() {
194 if(_selectionMode != NoSelection) {
195 _selectionMode = NoSelection;
200 void ChatItem::continueSelecting(const QPointF &pos) {
201 _selectionMode = PartialSelection;
202 _selectionEnd = posToCursor(pos);
206 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
207 if(event->buttons() == Qt::LeftButton) {
208 if(_selectionMode == NoSelection) {
209 chatScene()->setSelectingItem(this); // removes earlier selection if exists
210 _selectionStart = _selectionEnd = posToCursor(event->pos());
211 _selectionMode = PartialSelection;
213 chatScene()->setSelectingItem(0);
214 _selectionMode = NoSelection;
223 void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
224 if(event->buttons() == Qt::LeftButton) {
225 if(contains(event->pos())) {
226 qint16 end = posToCursor(event->pos());
227 if(end != _selectionEnd) {
233 chatScene()->startGlobalSelection(this, event->pos());
241 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
242 if(_selectionMode != NoSelection && !event->buttons() & Qt::LeftButton) {
243 _selectionEnd = posToCursor(event->pos());
245 = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
246 QApplication::clipboard()->setText(selection, QClipboard::Clipboard); // TODO configure where selections should go
253 void ChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
254 // FIXME dirty and fast hack to make http:// urls klickable
256 QRegExp regex("\\b([hf]t{1,2}ps?://[^\\s]+)\\b");
257 QString str = data(ChatLineModel::DisplayRole).toString();
258 int idx = posToCursor(event->pos());
261 mi = regex.indexIn(str, mi);
263 if(idx >= mi && idx < mi + regex.matchedLength()) {
264 QDesktopServices::openUrl(QUrl(regex.capturedTexts()[1]));
267 mi += regex.matchedLength();
272 void ChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
273 //qDebug() << (void*)this << "entering";
277 void ChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
278 //qDebug() << (void*)this << "leaving";
282 void ChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
283 //qDebug() << (void*)this << event->pos();
288 /*************************************************************************************************/
290 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
291 wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
299 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
303 qint16 ChatItem::WrapColumnFinder::nextWrapColumn() {
304 while(wordidx < wrapList.count()) {
305 w += wrapList.at(wordidx).width;
306 if(w >= item->width()) {
307 if(lastwrapcol >= wrapList.at(wordidx).start) {
308 // first word, and it doesn't fit
309 if(!line.isValid()) {
310 layout = item->createLayout(QTextOption::NoWrap);
311 layout->beginLayout();
312 line = layout->createLine();
313 line.setLineWidth(item->width());
316 int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
317 qreal x = line.cursorToX(idx, QTextLine::Trailing);
318 w = w - wrapList.at(wordidx).width - (x - lastwrappos);
323 // not the first word, so just wrap before this
324 lastwrapcol = wrapList.at(wordidx).start;
325 lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
329 w += wrapList.at(wordidx).trailing;