Tweak ChatItem/ChatLine layouting
[quassel.git] / src / qtui / chatline.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QDateTime>
22 #include <QString>
23 #include <QtGui>
24
25 #include "bufferinfo.h"
26 #include "buffersyncer.h"
27 #include "client.h"
28 #include "chatitem.h"
29 #include "chatline.h"
30 #include "columnhandleitem.h"
31 #include "messagemodel.h"
32 #include "networkmodel.h"
33 #include "qtui.h"
34 #include "qtuisettings.h"
35 #include "qtuistyle.h"
36
37 ChatLine::ChatLine(int row, QAbstractItemModel *model,
38                    const qreal &width,
39                    const qreal &timestampWidth, const qreal &senderWidth, const qreal &contentsWidth,
40                    const QPointF &senderPos, const QPointF &contentsPos,
41                    QGraphicsItem *parent)
42   : QGraphicsItem(parent),
43     _row(row), // needs to be set before the items
44     _model(model),
45     _contentsItem(contentsWidth, contentsPos, this),
46     _senderItem(senderWidth, _contentsItem.height(), senderPos, this),
47     _timestampItem(timestampWidth, _contentsItem.height(), this),
48     _width(width),
49     _height(_contentsItem.height()),
50     _selection(0)
51 {
52   Q_ASSERT(model);
53   QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
54   setZValue(0);
55   setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
56 }
57
58 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
59   switch(column) {
60     case ChatLineModel::TimestampColumn:
61       return _timestampItem;
62     case ChatLineModel::SenderColumn:
63       return _senderItem;
64     case ChatLineModel::ContentsColumn:
65       return _contentsItem;
66   default:
67     return *(ChatItem *)0; // provoke an error
68   }
69 }
70
71 // NOTE: senderPos is in ChatLines coordinate system!
72 void ChatLine::setFirstColumn(const qreal &timestampWidth, const qreal &senderWidth, const QPointF &senderPos) {
73   _timestampItem.prepareGeometryChange();
74   _timestampItem.setGeometry(timestampWidth, _height);
75   // senderItem doesn't need a geom change as it's Pos is changed (ensured by void ChatScene::firstHandlePositionChanged(qreal xpos))
76   _senderItem.setGeometry(senderWidth, _height);
77   _senderItem.setPos(senderPos);
78 }
79
80 // NOTE: contentsPos is in ChatLines coordinate system!
81 void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth,
82                                const QPointF &contentsPos, qreal &linePos) {
83   // contentsItem doesn't need a geom change as it's Pos is changed (ensured by void ChatScene::firstHandlePositionChanged(qreal xpos))
84   qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
85   linePos -= height;
86   bool needGeometryChange = linePos == pos().y();
87
88   if(needGeometryChange) {
89     _timestampItem.prepareGeometryChange();
90     _senderItem.prepareGeometryChange();
91   }
92   _timestampItem.setHeight(height);
93   _senderItem.setGeometry(senderWidth, height);
94
95   _contentsItem.setPos(contentsPos);
96
97   if(needGeometryChange)
98     prepareGeometryChange();
99
100   _height = height;
101
102   setPos(0, linePos);
103 }
104
105 void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos) {
106   qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
107   linePos -= height;
108   bool needGeometryChange = (height != _height || width != _width);
109
110   if(height != _height) {
111     _timestampItem.prepareGeometryChange();
112     _timestampItem.setHeight(height);
113     _senderItem.prepareGeometryChange();
114     _senderItem.setHeight(height);
115   }
116
117   if(needGeometryChange) {
118     prepareGeometryChange();
119     _height = height;
120     _width = width;
121   }
122
123   setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
124 }
125
126 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
127   if(selected) {
128     quint8 sel = (_selection & Highlighted) | Selected | minColumn;
129     if(sel != _selection) {
130       _selection = sel;
131       for(int i = 0; i < minColumn; i++)
132         item((ChatLineModel::ColumnType)i).clearSelection();
133       for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
134         item((ChatLineModel::ColumnType)i).setFullSelection();
135       update();
136     }
137   } else {
138     quint8 sel = _selection & Highlighted;
139     if(sel != _selection) {
140       _selection = sel;
141       for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
142         item((ChatLineModel::ColumnType)i).clearSelection();
143       update();
144     }
145   }
146 }
147
148 void ChatLine::setHighlighted(bool highlighted) {
149   if(highlighted) _selection |= Highlighted;
150   else _selection &= ~Highlighted;
151   update();
152 }
153
154 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
155   Q_UNUSED(option);
156   Q_UNUSED(widget);
157
158   const QAbstractItemModel *model_ = model();
159   QModelIndex myIdx = model_->index(row(), 0);
160   Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt();
161   UiStyle::MessageLabel label = (UiStyle::MessageLabel)myIdx.data(ChatLineModel::MsgLabelRole).toInt();
162
163   QTextCharFormat msgFmt = QtUi::style()->format(UiStyle::formatType(type), label);
164   if(msgFmt.hasProperty(QTextFormat::BackgroundBrush)) {
165     painter->fillRect(boundingRect(), msgFmt.background());
166   }
167
168   if(_selection & Selected) {
169     QTextCharFormat selFmt = QtUi::style()->format(UiStyle::formatType(type), label | UiStyle::Selected);
170     if(selFmt.hasProperty(QTextFormat::BackgroundBrush)) {
171       qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask)).x();
172       QRectF selectRect(left, 0, width() - left, height());
173       painter->fillRect(selectRect, selFmt.background());
174     }
175   }
176
177   // new line marker
178   if(model_ && row() > 0  && chatScene()->isSingleBufferScene()) {
179     QModelIndex prevRowIdx = model_->index(row() - 1, 0);
180     MsgId prevMsgId = prevRowIdx.data(MessageModel::MsgIdRole).value<MsgId>();
181     MsgId myMsgId = myIdx.data(MessageModel::MsgIdRole).value<MsgId>();
182     Message::Flags flags = (Message::Flags)myIdx.data(MessageModel::FlagsRole).toInt();
183
184     // don't show the marker if we wrote that new line
185     if(!(flags & Message::Self)) {
186       BufferId bufferId = BufferId(chatScene()->idString().toInt());
187       MsgId lastSeenMsgId = Client::networkModel()->lastSeenMarkerMsgId(bufferId);
188       if(lastSeenMsgId < myMsgId && lastSeenMsgId >= prevMsgId) {
189         QLinearGradient gradient(0, 0, 0, contentsItem().fontMetrics()->lineSpacing());
190         gradient.setColorAt(0, QtUi::style()->markerLineBrush().color()); // FIXME: Use full (gradient?) brush instead of just the color
191         gradient.setColorAt(0.1, Qt::transparent);
192         painter->fillRect(boundingRect(), gradient);
193       }
194     }
195   }
196 }