187ba33a2c4e5febc6583f416d85b837c3ca02ed
[quassel.git] / src / qtui / chatlinemodelitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "chatlinemodelitem.h"
22 #include "chatlinemodel.h"
23 #include "qtui.h"
24 #include "qtuistyle.h"
25
26 // ****************************************
27 // the actual ChatLineModelItem
28 // ****************************************
29 ChatLineModelItem::ChatLineModelItem(const Message &msg)
30   : MessageModelItem(),
31     _styledMsg(msg)
32 {
33   if(!msg.sender().contains('!'))
34     _styledMsg.setFlags(msg.flags() |= Message::ServerMsg);
35 }
36
37 QVariant ChatLineModelItem::data(int column, int role) const {
38   if(role == ChatLineModel::MsgLabelRole)
39     return messageLabel();
40
41   QVariant variant;
42   MessageModel::ColumnType col = (MessageModel::ColumnType)column;
43   switch(col) {
44   case ChatLineModel::TimestampColumn:
45     variant = timestampData(role);
46     break;
47   case ChatLineModel::SenderColumn:
48     variant = senderData(role);
49     break;
50   case ChatLineModel::ContentsColumn:
51     variant = contentsData(role);
52     break;
53   default:
54     break;
55   }
56   if(!variant.isValid())
57     return MessageModelItem::data(column, role);
58   return variant;
59 }
60
61 QVariant ChatLineModelItem::timestampData(int role) const {
62   switch(role) {
63   case ChatLineModel::DisplayRole:
64     return _styledMsg.decoratedTimestamp();
65   case ChatLineModel::EditRole:
66     return _styledMsg.timestamp();
67   case ChatLineModel::BackgroundRole:
68     return backgroundBrush(UiStyle::Timestamp);
69   case ChatLineModel::SelectedBackgroundRole:
70     return backgroundBrush(UiStyle::Timestamp, true);
71   case ChatLineModel::FormatRole:
72     return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
73                       << qMakePair((quint16)0, (quint32)UiStyle::formatType(_styledMsg.type()) | UiStyle::Timestamp));
74   }
75   return QVariant();
76 }
77
78 QVariant ChatLineModelItem::senderData(int role) const {
79   switch(role) {
80   case ChatLineModel::DisplayRole:
81     return _styledMsg.decoratedSender();
82   case ChatLineModel::EditRole:
83     return _styledMsg.plainSender();
84   case ChatLineModel::BackgroundRole:
85     return backgroundBrush(UiStyle::Sender);
86   case ChatLineModel::SelectedBackgroundRole:
87     return backgroundBrush(UiStyle::Sender, true);
88   case ChatLineModel::FormatRole:
89     return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
90                       << qMakePair((quint16)0, (quint32)UiStyle::formatType(_styledMsg.type()) | UiStyle::Sender));
91   }
92   return QVariant();
93 }
94
95 QVariant ChatLineModelItem::contentsData(int role) const {
96   switch(role) {
97   case ChatLineModel::DisplayRole:
98   case ChatLineModel::EditRole:
99     return _styledMsg.plainContents();
100   case ChatLineModel::BackgroundRole:
101     return backgroundBrush(UiStyle::Contents);
102   case ChatLineModel::SelectedBackgroundRole:
103     return backgroundBrush(UiStyle::Contents, true);
104   case ChatLineModel::FormatRole:
105     return QVariant::fromValue<UiStyle::FormatList>(_styledMsg.contentsFormatList());
106   }
107   return QVariant();
108 }
109
110 quint32 ChatLineModelItem::messageLabel() const {
111   quint32 label = _styledMsg.senderHash() << 16;
112   if(_styledMsg.flags() & Message::Self)
113     label |= UiStyle::OwnMsg;
114   if(_styledMsg.flags() & Message::Highlight)
115     label |= UiStyle::Highlight;
116   return label;
117 }
118
119 QVariant ChatLineModelItem::backgroundBrush(UiStyle::FormatType subelement, bool selected) const {
120   QTextCharFormat fmt = QtUi::style()->format(UiStyle::formatType(_styledMsg.type()) | subelement, messageLabel() | (selected ? UiStyle::Selected : 0));
121   if(fmt.hasProperty(QTextFormat::BackgroundBrush))
122     return QVariant::fromValue<QBrush>(fmt.background());
123   return QVariant();
124 }