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