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