Continuing my personal crusade against Buffer.
[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 "qtui.h"
32
33 ChatLine::ChatLine(int row, QAbstractItemModel *model, QGraphicsItem *parent)
34   : QGraphicsItem(parent),
35     _row(row), // needs to be set before the items
36     _timestampItem(ChatLineModel::TimestampColumn, model, this),
37     _senderItem(ChatLineModel::SenderColumn, model, this),
38     _contentsItem(ChatLineModel::ContentsColumn, model, this),
39     _width(0),
40     _height(0),
41     _selection(0)
42 {
43   Q_ASSERT(model);
44   QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
45   setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
46 }
47
48 QRectF ChatLine::boundingRect () const {
49   //return childrenBoundingRect();
50   return QRectF(0, 0, _width, _height);
51 }
52
53 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
54   switch(column) {
55     case ChatLineModel::TimestampColumn:
56       return _timestampItem;
57     case ChatLineModel::SenderColumn:
58       return _senderItem;
59     case ChatLineModel::ContentsColumn:
60       return _contentsItem;
61   default:
62     return *(ChatItem *)0; // provoke an error
63   }
64 }
65
66 qreal ChatLine::setGeometry(qreal width, qreal firstHandlePos, qreal secondHandlePos) {
67   if(width != _width)
68     prepareGeometryChange();
69   qreal firstsep = QtUi::style()->firstColumnSeparator()/2;
70   qreal secondsep = QtUi::style()->secondColumnSeparator()/2;
71
72   _timestampItem.setWidth(firstHandlePos - firstsep);
73   _senderItem.setWidth(secondHandlePos - firstHandlePos - (firstsep+secondsep));
74   _height = _contentsItem.setWidth(width - secondHandlePos - secondsep);
75
76   _senderItem.setPos(firstHandlePos + firstsep, 0);
77   _contentsItem.setPos(secondHandlePos + secondsep, 0);
78
79   _width = width;
80   return _height;
81 }
82
83 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
84   if(selected) {
85     quint8 sel = (_selection & 0x80) | 0x40 | minColumn;
86     if(sel != _selection) {
87       _selection = sel;
88       for(int i = 0; i < minColumn; i++)
89         item((ChatLineModel::ColumnType)i).clearSelection();
90       for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
91         item((ChatLineModel::ColumnType)i).setFullSelection();
92       update();
93     }
94   } else {
95     quint8 sel = _selection & 0x80;
96     if(sel != _selection) {
97       _selection = sel;
98       for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
99         item((ChatLineModel::ColumnType)i).clearSelection();
100       update();
101     }
102   }
103 }
104
105 void ChatLine::setHighlighted(bool highlighted) {
106   if(highlighted) _selection |= 0x80;
107   else _selection &= 0x7f;
108   update();
109 }
110
111 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
112 //   const QAbstractItemModel *model_ = model();
113 //   if(model_ && row() > 0) {
114 //     MsgId msgId = model_->data(model_->index(row() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
115 //     BufferId bufferId = model_->data(model_->index(row() - 1, 0), MessageModel::BufferIdRole).value<BufferId>();
116 //     qDebug() << msgId;
117 //   }
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 }