Test our newly acquired shortcut capabilities by finally allowing Ctrl+F to trigger...
[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, QGraphicsItem *parent)
38   : QGraphicsItem(parent),
39     _row(row), // needs to be set before the items
40     _timestampItem(model, this),
41     _senderItem(model, this),
42     _contentsItem(model, this),
43     _width(0),
44     _height(0),
45     _selection(0)
46 {
47   Q_ASSERT(model);
48   QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
49   setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
50 }
51
52 QRectF ChatLine::boundingRect () const {
53   //return childrenBoundingRect();
54   return QRectF(0, 0, _width, _height);
55 }
56
57 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
58   switch(column) {
59     case ChatLineModel::TimestampColumn:
60       return _timestampItem;
61     case ChatLineModel::SenderColumn:
62       return _senderItem;
63     case ChatLineModel::ContentsColumn:
64       return _contentsItem;
65   default:
66     return *(ChatItem *)0; // provoke an error
67   }
68 }
69
70 qreal ChatLine::setGeometry(qreal width) {
71   if(width != _width)
72     prepareGeometryChange();
73
74   ColumnHandleItem *firstColumnHandle = chatScene()->firstColumnHandle();
75   ColumnHandleItem *secondColumnHandle = chatScene()->secondColumnHandle();
76   _height = _contentsItem.setGeometry(width - secondColumnHandle->sceneRight());
77   _timestampItem.setGeometry(firstColumnHandle->sceneLeft(), _height);
78   _senderItem.setGeometry(secondColumnHandle->sceneLeft() - firstColumnHandle->sceneRight(), _height);
79
80   _senderItem.setPos(firstColumnHandle->sceneRight(), 0);
81   _contentsItem.setPos(secondColumnHandle->sceneRight(), 0);
82
83   _width = width;
84   return _height;
85 }
86
87 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
88   if(selected) {
89     quint8 sel = (_selection & 0x80) | 0x40 | minColumn;
90     if(sel != _selection) {
91       _selection = sel;
92       for(int i = 0; i < minColumn; i++)
93         item((ChatLineModel::ColumnType)i).clearSelection();
94       for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
95         item((ChatLineModel::ColumnType)i).setFullSelection();
96       update();
97     }
98   } else {
99     quint8 sel = _selection & 0x80;
100     if(sel != _selection) {
101       _selection = sel;
102       for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
103         item((ChatLineModel::ColumnType)i).clearSelection();
104       update();
105     }
106   }
107 }
108
109 void ChatLine::setHighlighted(bool highlighted) {
110   if(highlighted) _selection |= 0x80;
111   else _selection &= 0x7f;
112   update();
113 }
114
115 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
116   Q_UNUSED(option);
117   Q_UNUSED(widget);
118   if(_selection & Highlighted) {
119     painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
120   }
121   if(_selection & Selected) {
122     qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f)).x();
123     QRectF selectRect(left, 0, width() - left, height());
124     painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
125   }
126
127   // new line marker
128   const QAbstractItemModel *model_ = model();
129   if(model_ && row() > 0) {
130     QModelIndex prevRowIdx = model_->index(row() - 1, 0);
131     MsgId msgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
132     Message::Flags flags = (Message::Flags)model_->data(model_->index(row(), 0), MessageModel::FlagsRole).toInt();
133     // don't show the marker if we wrote that new line
134     if(!(flags & Message::Self)) {
135       BufferId bufferId = model_->data(prevRowIdx, MessageModel::BufferIdRole).value<BufferId>();
136       if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) {
137         QtUiStyleSettings s("Colors");
138         QLinearGradient gradient(0, 0, 0, height());
139         gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
140         gradient.setColorAt(0.1, Qt::transparent);
141         painter->fillRect(boundingRect(), gradient);
142       }
143     }
144   }
145 }