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(ChatLineModel::ColumnType col, QAbstractItemModel *model, QGraphicsItem *parent)
35 : QGraphicsItem(parent),
37 _selectionMode(NoSelection),
42 QModelIndex index = model->index(row(), col);
43 _fontMetrics = QtUi::style()->fontMetrics(model->data(index, ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
44 setAcceptHoverEvents(true);
48 ChatItem::~ChatItem() {
52 QVariant ChatItem::data(int role) const {
53 QModelIndex index = model()->index(row(), column());
54 if(!index.isValid()) {
55 qWarning() << "ChatItem::data(): model index is invalid!" << index;
58 return model()->data(index, role);
61 qreal ChatItem::setGeometry(qreal w, qreal h) {
62 if(w == _boundingRect.width()) return _boundingRect.height();
63 prepareGeometryChange();
64 _boundingRect.setWidth(w);
65 if(h < 0) h = computeHeight();
66 _boundingRect.setHeight(h);
67 if(haveLayout()) updateLayout();
71 qreal ChatItem::computeHeight() {
72 return fontMetrics()->lineSpacing(); // only contents can be multi-line
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() {
91 setLayout(createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft));
93 layout()->beginLayout();
94 QTextLine line = layout()->createLine();
96 line.setLineWidth(width());
97 line.setPosition(QPointF(0,0));
99 layout()->endLayout();
102 void ChatItem::clearLayout() {
107 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
108 // This is a deliberate trade-off. (-> selectFmt creation, data() call)
109 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
110 Q_UNUSED(option); Q_UNUSED(widget);
111 if(!haveLayout()) updateLayout();
112 painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
113 //if(_selectionMode == FullSelection) {
115 //painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight));
116 //painter->restore();
118 QVector<QTextLayout::FormatRange> formats;
119 if(_selectionMode != NoSelection) {
120 QTextLayout::FormatRange selectFmt;
121 selectFmt.format.setForeground(QApplication::palette().brush(QPalette::HighlightedText));
122 selectFmt.format.setBackground(QApplication::palette().brush(QPalette::Highlight));
123 if(_selectionMode == PartialSelection) {
124 selectFmt.start = qMin(_selectionStart, _selectionEnd);
125 selectFmt.length = qAbs(_selectionStart - _selectionEnd);
126 } else { // FullSelection
128 selectFmt.length = data(MessageModel::DisplayRole).toString().length();
130 formats.append(selectFmt);
132 layout()->draw(painter, QPointF(0,0), formats, boundingRect());
135 qint16 ChatItem::posToCursor(const QPointF &pos) {
136 if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
137 if(pos.y() < 0) return 0;
138 if(!haveLayout()) updateLayout();
139 for(int l = layout()->lineCount() - 1; l >= 0; l--) {
140 QTextLine line = layout()->lineAt(l);
141 if(pos.y() >= line.y()) {
142 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
148 void ChatItem::setFullSelection() {
149 if(_selectionMode != FullSelection) {
150 _selectionMode = FullSelection;
155 void ChatItem::clearSelection() {
156 _selectionMode = NoSelection;
160 void ChatItem::continueSelecting(const QPointF &pos) {
161 _selectionMode = PartialSelection;
162 _selectionEnd = posToCursor(pos);
166 QList<QRectF> ChatItem::findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive) {
167 QList<QRectF> resultList;
168 const QAbstractItemModel *model_ = model();
172 QString plainText = model_->data(model_->index(row(), column()), MessageModel::DisplayRole).toString();
173 QList<int> indexList;
174 int searchIdx = plainText.indexOf(searchWord, 0, caseSensitive);
175 while(searchIdx != -1) {
176 indexList << searchIdx;
177 searchIdx = plainText.indexOf(searchWord, searchIdx + 1, caseSensitive);
183 foreach(int idx, indexList) {
184 QTextLine line = layout()->lineForTextPosition(idx);
185 qreal x = line.cursorToX(idx);
186 qreal width = line.cursorToX(idx + searchWord.count()) - x;
187 qreal height = fontMetrics()->lineSpacing();
188 qreal y = height * line.lineNumber();
189 resultList << QRectF(x, y, width, height);
194 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
195 if(event->buttons() == Qt::LeftButton) {
196 chatScene()->setSelectingItem(this);
197 _selectionStart = _selectionEnd = posToCursor(event->pos());
198 _selectionMode = NoSelection; // will be set to PartialSelection by mouseMoveEvent
206 void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
207 if(event->buttons() == Qt::LeftButton) {
208 if(contains(event->pos())) {
209 qint16 end = posToCursor(event->pos());
210 if(end != _selectionEnd) {
212 _selectionMode = (_selectionStart != _selectionEnd ? PartialSelection : NoSelection);
217 chatScene()->startGlobalSelection(this, event->pos());
225 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
226 if(_selectionMode != NoSelection && !event->buttons() & Qt::LeftButton) {
227 _selectionEnd = posToCursor(event->pos());
229 = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
230 chatScene()->putToClipboard(selection);
237 /*************************************************************************************************/
239 /*************************************************************************************************/
241 void SenderChatItem::updateLayout() {
242 if(!haveLayout()) setLayout(createLayout(QTextOption::WrapAnywhere, Qt::AlignRight));
243 ChatItem::updateLayout();
246 /*************************************************************************************************/
248 ContentsChatItem::ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent),
254 ContentsChatItem::~ContentsChatItem() {
258 qreal ContentsChatItem::computeHeight() {
260 WrapColumnFinder finder(this);
261 while(finder.nextWrapColumn() > 0) lines++;
262 return lines * fontMetrics()->lineSpacing();
265 void ContentsChatItem::setLayout(QTextLayout *layout) {
267 _layoutData = new LayoutData;
268 _layoutData->layout = layout;
271 void ContentsChatItem::clearLayout() {
276 void ContentsChatItem::updateLayout() {
277 if(!haveLayout()) setLayout(createLayout(QTextOption::WrapAnywhere));
280 ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
281 if(!wrapList.count()) return; // empty chatitem
284 WrapColumnFinder finder(this);
285 layout()->beginLayout();
287 QTextLine line = layout()->createLine();
291 int col = finder.nextWrapColumn();
292 line.setNumColumns(col >= 0 ? col - line.textStart() : layout()->text().length());
293 line.setPosition(QPointF(0, h));
294 h += line.height() + fontMetrics()->leading();
296 layout()->endLayout();
299 void ContentsChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
300 // FIXME dirty and fast hack to make http:// urls klickable
302 QRegExp regex("\\b([hf]t{1,2}ps?://[^\\s]+)\\b");
303 QString str = data(ChatLineModel::DisplayRole).toString();
304 int idx = posToCursor(event->pos());
307 mi = regex.indexIn(str, mi);
309 if(idx >= mi && idx < mi + regex.matchedLength()) {
310 QDesktopServices::openUrl(QUrl(regex.capturedTexts()[1]));
313 mi += regex.matchedLength();
318 void ContentsChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
319 //qDebug() << (void*)this << "entering";
323 void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
324 //qDebug() << (void*)this << "leaving";
328 void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
329 //qDebug() << (void*)this << event->pos();
333 /*************************************************************************************************/
335 ContentsChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
336 wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
344 ContentsChatItem::WrapColumnFinder::~WrapColumnFinder() {
348 qint16 ContentsChatItem::WrapColumnFinder::nextWrapColumn() {
349 while(wordidx < wrapList.count()) {
350 w += wrapList.at(wordidx).width;
351 if(w >= item->width()) {
352 if(lastwrapcol >= wrapList.at(wordidx).start) {
353 // first word, and it doesn't fit
354 if(!line.isValid()) {
355 layout = item->createLayout(QTextOption::NoWrap);
356 layout->beginLayout();
357 line = layout->createLine();
358 line.setLineWidth(item->width());
361 int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
362 qreal x = line.cursorToX(idx, QTextLine::Trailing);
363 w = w - wrapList.at(wordidx).width - (x - lastwrappos);
368 // not the first word, so just wrap before this
369 lastwrapcol = wrapList.at(wordidx).start;
370 lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
374 w += wrapList.at(wordidx).trailing;