properly handling disconnects - this might even fix an antique bug with duplicate...
[quassel.git] / src / qtui / chatline.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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   _timestampItem.clearLayout();
80   _senderItem.clearLayout();
81 }
82
83 // NOTE: contentsPos is in ChatLines coordinate system!
84 void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth,
85                                const QPointF &contentsPos, qreal &linePos) {
86   // contentsItem doesn't need a geom change as it's Pos is changed (ensured by void ChatScene::firstHandlePositionChanged(qreal xpos))
87   qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
88   linePos -= height;
89   bool needGeometryChange = linePos == pos().y() && height != _height;
90
91   if(needGeometryChange) {
92     _timestampItem.prepareGeometryChange();
93     _senderItem.prepareGeometryChange();
94   }
95   _timestampItem.setHeight(height);
96   _senderItem.setGeometry(senderWidth, height);
97
98   _contentsItem.setPos(contentsPos);
99
100   _timestampItem.clearLayout();
101   _senderItem.clearLayout();
102
103   if(needGeometryChange)
104     prepareGeometryChange();
105
106   _height = height;
107
108   setPos(0, linePos);
109 }
110
111 void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos) {
112   qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
113   linePos -= height;
114   bool needGeometryChange = linePos == pos().y();
115
116   if(needGeometryChange) {
117     _timestampItem.prepareGeometryChange();
118     _senderItem.prepareGeometryChange();
119   }
120   _timestampItem.setHeight(height);
121   _senderItem.setHeight(height);
122   _contentsItem.clearLayout();
123
124   if(needGeometryChange)
125     prepareGeometryChange();
126
127   _height = height;
128   _width = width;
129
130   setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
131 }
132
133 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
134   if(selected) {
135     quint8 sel = (_selection & Highlighted) | Selected | minColumn;
136     if(sel != _selection) {
137       _selection = sel;
138       for(int i = 0; i < minColumn; i++)
139         item((ChatLineModel::ColumnType)i).clearSelection();
140       for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
141         item((ChatLineModel::ColumnType)i).setFullSelection();
142       update();
143     }
144   } else {
145     quint8 sel = _selection & Highlighted;
146     if(sel != _selection) {
147       _selection = sel;
148       for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
149         item((ChatLineModel::ColumnType)i).clearSelection();
150       update();
151     }
152   }
153 }
154
155 void ChatLine::setHighlighted(bool highlighted) {
156   if(highlighted) _selection |= Highlighted;
157   else _selection &= ~Highlighted;
158   update();
159 }
160
161 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
162   Q_UNUSED(option);
163   Q_UNUSED(widget);
164   if(_selection & Highlighted) {
165     painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
166   }
167   if(_selection & Selected) {
168     qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask)).x();
169     QRectF selectRect(left, 0, width() - left, height());
170     painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
171   }
172
173   // new line marker
174   const QAbstractItemModel *model_ = model();
175   if(model_ && row() > 0) {
176     QModelIndex prevRowIdx = model_->index(row() - 1, 0);
177     MsgId msgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
178     Message::Flags flags = (Message::Flags)model_->data(model_->index(row(), 0), MessageModel::FlagsRole).toInt();
179     // don't show the marker if we wrote that new line
180     if(!(flags & Message::Self)) {
181       BufferId bufferId = model_->data(prevRowIdx, MessageModel::BufferIdRole).value<BufferId>();
182       if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) {
183         QtUiStyleSettings s("Colors");
184         QLinearGradient gradient(0, 0, 0, contentsItem().fontMetrics()->lineSpacing());
185         gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
186         gradient.setColorAt(0.1, Qt::transparent);
187         painter->fillRect(boundingRect(), gradient);
188       }
189     }
190   }
191 }