Introduce QtUiStyleSettings and make highlight color configurable again
[quassel.git] / src / client / messagemodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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 "messagemodel.h"
22
23 #include "message.h"
24
25 MessageModel::MessageModel(QObject *parent) : QAbstractItemModel(parent) {
26
27
28
29 }
30
31 MessageModel::~MessageModel() {
32
33
34 }
35
36 QVariant MessageModel::data(const QModelIndex &index, int role) const {
37   int row = index.row(); int column = index.column();
38   if(row < 0 || row >= _messageList.count() || column < 0) return QVariant();
39   if(role == ColumnTypeRole) return column;
40   return _messageList[row]->data(index.column(), role);
41 }
42
43 bool MessageModel::setData(const QModelIndex &index, const QVariant &value, int role) {
44   int row = index.row();
45   if(row < 0 || row >= _messageList.count()) return false;
46   if(_messageList[row]->setData(index.column(), value, role)) {
47     emit dataChanged(index, index);
48     return true;
49   }
50   return false;
51 }
52
53 void MessageModel::insertMessage(const Message &msg) {
54   MsgId id = msg.msgId();
55   int idx = indexForId(id);
56   MessageModelItem *item = createMessageModelItem(msg);
57   beginInsertRows(QModelIndex(), idx, idx);
58   _messageList.insert(idx, item);
59   endInsertRows();
60 }
61
62 void MessageModel::insertMessages(const QList<Message> &msglist) {
63   if(msglist.isEmpty()) return;
64   // FIXME make this more efficient by grouping msgs
65   foreach(Message msg, msglist) insertMessage(msg);
66
67 }
68
69 void MessageModel::clear() {
70   reset();
71   qDeleteAll(_messageList);
72   _messageList.clear();
73 }
74
75 // returns index of msg with given Id or of the next message after that (i.e., the index where we'd insert this msg)
76 int MessageModel::indexForId(MsgId id) {
77   if(_messageList.isEmpty() || id <= _messageList[0]->data(0, MsgIdRole).value<MsgId>()) return 0;
78   if(id > _messageList.last()->data(0, MsgIdRole).value<MsgId>()) return _messageList.count();
79   // binary search
80   int start = 0; int end = _messageList.count()-1;
81   while(1) {
82     if(end - start == 1) return end;
83     int pivot = (end + start) / 2;
84     if(id <= _messageList[pivot]->data(0, MsgIdRole).value<MsgId>()) end = pivot;
85     else start = pivot;
86   }
87 }
88
89 /**********************************************************************************/
90
91 MessageModelItem::MessageModelItem(const Message &msg) {
92   _timestamp = msg.timestamp();
93   _msgId = msg.msgId();
94   _bufferId = msg.bufferInfo().bufferId();
95   _type = msg.type();
96   _flags = msg.flags();
97
98 }
99
100 MessageModelItem::~MessageModelItem() {
101
102 }
103
104 QVariant MessageModelItem::data(int column, int role) const {
105   if(column < MessageModel::TimestampColumn || column > MessageModel::ContentsColumn) return QVariant();
106   switch(role) {
107     case MessageModel::MsgIdRole: return QVariant::fromValue<MsgId>(_msgId);
108     case MessageModel::BufferIdRole: return QVariant::fromValue<BufferId>(_bufferId);
109     case MessageModel::TypeRole: return _type;
110     case MessageModel::FlagsRole: return (int)_flags;
111     case MessageModel::TimestampRole: return _timestamp;
112     default: return QVariant();
113   }
114 }
115