1 /***************************************************************************
2 * Copyright (C) 2005-2012 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 ***************************************************************************/
22 #include <QGraphicsSceneMouseEvent>
26 #include "bufferinfo.h"
27 #include "buffersyncer.h"
32 #include "columnhandleitem.h"
33 #include "messagemodel.h"
34 #include "networkmodel.h"
36 #include "qtuisettings.h"
37 #include "qtuistyle.h"
39 ChatLine::ChatLine(int row, QAbstractItemModel *model,
41 const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
42 const QPointF &senderPos, const QPointF &contentsPos,
43 QGraphicsItem *parent)
44 : QGraphicsItem(parent),
45 _row(row), // needs to be set before the items
47 _contentsItem(contentsPos, contentsWidth, this),
48 _senderItem(QRectF(senderPos, QSizeF(senderWidth, _contentsItem.height())), this),
49 _timestampItem(QRectF(0, 0, timestampWidth, _contentsItem.height()), this),
51 _height(_contentsItem.height()),
57 QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
59 setAcceptHoverEvents(true);
60 setHighlighted(index.data(MessageModel::FlagsRole).toInt() & Message::Highlight);
63 ChatLine::~ChatLine() {
65 chatView()->setHasCache(this, false);
68 ChatItem *ChatLine::item(ChatLineModel::ColumnType column) {
70 case ChatLineModel::TimestampColumn:
71 return &_timestampItem;
72 case ChatLineModel::SenderColumn:
74 case ChatLineModel::ContentsColumn:
75 return &_contentsItem;
81 ChatItem *ChatLine::itemAt(const QPointF &pos) {
82 if(_contentsItem.boundingRect().contains(pos))
83 return &_contentsItem;
84 if(_senderItem.boundingRect().contains(pos))
86 if(_timestampItem.boundingRect().contains(pos))
87 return &_timestampItem;
91 void ChatLine::clearCache() {
92 _timestampItem.clearCache();
93 _senderItem.clearCache();
94 _contentsItem.clearCache();
97 void ChatLine::setMouseGrabberItem(ChatItem *item) {
98 _mouseGrabberItem = item;
101 bool ChatLine::sceneEvent(QEvent *event) {
102 if(event->type() == QEvent::GrabMouse) {
103 // get mouse cursor pos relative to us
104 ChatView *view = chatScene()->chatView();
105 QPointF linePos = mapFromScene(view->mapToScene(view->mapFromGlobal(QCursor::pos())));
106 setMouseGrabberItem(itemAt(linePos));
107 } else if(event->type() == QEvent::UngrabMouse) {
108 setMouseGrabberItem(0);
110 return QGraphicsItem::sceneEvent(event);
113 void ChatLine::setFirstColumn(const qreal ×tampWidth, const qreal &senderWidth, const QPointF &senderPos) {
114 _timestampItem.setGeometry(timestampWidth, _height);
115 _senderItem.setGeometry(senderWidth, _height);
116 _senderItem.setPos(senderPos);
119 void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth, const QPointF &contentsPos, qreal &linePos) {
120 // linepos is the *bottom* position for the line
121 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
123 bool needGeometryChange = (height != _height);
125 _timestampItem.setHeight(height);
126 _senderItem.setGeometry(senderWidth, height);
127 _contentsItem.setPos(contentsPos);
129 if(needGeometryChange)
130 prepareGeometryChange();
137 void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos) {
138 // linepos is the *bottom* position for the line
139 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
141 bool needGeometryChange = (height != _height || width != _width);
143 if(height != _height) {
144 _timestampItem.setHeight(height);
145 _senderItem.setHeight(height);
148 if(needGeometryChange) {
149 prepareGeometryChange();
154 setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
157 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
159 quint8 sel = (_selection & Highlighted) | Selected | minColumn;
160 if(sel != _selection) {
162 for(int i = 0; i < minColumn; i++)
163 item((ChatLineModel::ColumnType)i)->clearSelection();
164 for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
165 item((ChatLineModel::ColumnType)i)->setFullSelection();
169 quint8 sel = _selection & Highlighted;
170 if(sel != _selection) {
172 for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
173 item((ChatLineModel::ColumnType)i)->clearSelection();
179 void ChatLine::setHighlighted(bool highlighted) {
180 if(highlighted) _selection |= Highlighted;
181 else _selection &= ~Highlighted;
185 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
189 const QAbstractItemModel *model_ = model();
190 QModelIndex myIdx = model_->index(row(), 0);
191 Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt();
192 UiStyle::MessageLabel label = (UiStyle::MessageLabel)myIdx.data(ChatLineModel::MsgLabelRole).toInt();
194 QTextCharFormat msgFmt = QtUi::style()->format(UiStyle::formatType(type), label);
195 if(msgFmt.hasProperty(QTextFormat::BackgroundBrush)) {
196 painter->fillRect(boundingRect(), msgFmt.background());
199 if(_selection & Selected) {
200 QTextCharFormat selFmt = QtUi::style()->format(UiStyle::formatType(type), label | UiStyle::Selected);
201 if(selFmt.hasProperty(QTextFormat::BackgroundBrush)) {
202 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask))->pos().x();
203 QRectF selectRect(left, 0, width() - left, height());
204 painter->fillRect(selectRect, selFmt.background());
209 // the items draw themselves at the correct position
210 timestampItem()->paint(painter, option, widget);
211 senderItem()->paint(painter, option, widget);
212 contentsItem()->paint(painter, option, widget);
215 // We need to dispatch all mouse-related events to the appropriate (mouse grabbing) ChatItem
217 ChatItem *ChatLine::mouseEventTargetItem(const QPointF &pos) {
218 if(mouseGrabberItem())
219 return mouseGrabberItem();
223 void ChatLine::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
224 ChatItem *item = mouseEventTargetItem(event->pos());
226 item->mouseMoveEvent(event);
229 void ChatLine::mousePressEvent(QGraphicsSceneMouseEvent *event) {
230 ChatItem *item = mouseEventTargetItem(event->pos());
232 item->mousePressEvent(event);
235 void ChatLine::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
236 ChatItem *item = mouseEventTargetItem(event->pos());
238 item->mouseReleaseEvent(event);
241 void ChatLine::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
242 ChatItem *item = mouseEventTargetItem(event->pos());
243 if(item && !_hoverItem) {
245 item->hoverEnterEvent(event);
249 void ChatLine::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
251 _hoverItem->hoverLeaveEvent(event);
256 void ChatLine::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
257 ChatItem *item = mouseEventTargetItem(event->pos());
259 item->hoverMoveEvent(event);