Make StyledMessage more (space) efficient, clean up ChatlineModelItem
[quassel.git] / src / qtui / chatlinemodelitem.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 <QFontMetrics>
22 #include <QTextBoundaryFinder>
23
24 #include "chatlinemodelitem.h"
25 #include "chatlinemodel.h"
26 #include "qtui.h"
27 #include "qtuistyle.h"
28
29 // This Struct is taken from Harfbuzz. We use it only to calc it's size.
30 // we use a shared memory region so we do not have to malloc a buffer area for every line
31 typedef struct {
32     /*HB_LineBreakType*/ unsigned lineBreakType  :2;
33     /*HB_Bool*/ unsigned whiteSpace              :1;     /* A unicode whitespace character, except NBSP, ZWNBSP */
34     /*HB_Bool*/ unsigned charStop                :1;     /* Valid cursor position (for left/right arrow) */
35     /*HB_Bool*/ unsigned wordBoundary            :1;
36     /*HB_Bool*/ unsigned sentenceBoundary        :1;
37     unsigned unused                  :2;
38 } HB_CharAttributes_Dummy;
39
40 // PRIVATE DATA FOR CHATLINE MODEL ITEM
41 class ChatLineModelItemPrivate {
42
43 public:
44   inline ChatLineModelItemPrivate(const Message &msg) : _msgBuffer(new Message(msg)), _styledMsg(0) {}
45   ~ChatLineModelItemPrivate() {
46     if(_msgBuffer) {
47       delete _msgBuffer;
48     } else {
49       delete _styledMsg;
50     }
51   }
52
53   inline bool needsStyling() { return (bool)_msgBuffer; }
54
55   QVariant data(MessageModel::ColumnType column, int role) {
56     if(needsStyling())
57       style();
58     switch(column) {
59       case ChatLineModel::TimestampColumn:
60         switch(role) {
61           case ChatLineModel::DisplayRole:
62             return _styledMsg->decoratedTimestamp();
63           case ChatLineModel::EditRole:
64             return _styledMsg->timestamp();
65           case ChatLineModel::FormatRole:
66             return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
67                                       << qMakePair((quint16)0, (quint32)_styledMsg->timestampFormat()));
68         }
69         break;
70       case ChatLineModel::SenderColumn:
71         switch(role) {
72           case ChatLineModel::DisplayRole:
73             return _styledMsg->decoratedSender();
74           case ChatLineModel::EditRole:
75             return _styledMsg->sender();
76           case ChatLineModel::FormatRole:
77             return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
78                                       << qMakePair((quint16)0, (quint32)_styledMsg->senderFormat()));
79         }
80         break;
81       case ChatLineModel::ContentsColumn:
82         switch(role) {
83           case ChatLineModel::DisplayRole:
84           case ChatLineModel::EditRole:
85             return _styledMsg->contents();
86           case ChatLineModel::FormatRole:
87             return QVariant::fromValue<UiStyle::FormatList>(_styledMsg->contentsFormatList());
88           case ChatLineModel::WrapListRole:
89             if(_wrapList.isEmpty())
90               computeWrapList();
91             return QVariant::fromValue<ChatLineModel::WrapList>(_wrapList);
92         }
93         break;
94       default:
95         Q_ASSERT(false);
96         return 0;
97     }
98     return QVariant();
99   }
100
101 private:
102   void style() {
103     _styledMsg = new QtUiStyle::StyledMessage(QtUi::style()->styleMessage(*_msgBuffer));
104
105     delete _msgBuffer;
106     _msgBuffer = 0;
107   }
108
109   void computeWrapList() {
110     int length = _styledMsg->contents().length();
111     if(!length)
112       return;
113
114     enum Mode { SearchStart, SearchEnd };
115
116     QList<ChatLineModel::Word> wplist;  // use a temp list which we'll later copy into a QVector for efficiency
117     QTextBoundaryFinder finder(QTextBoundaryFinder::Word, _styledMsg->contents().unicode(), length,
118                                TextBoundaryFinderBuffer, TextBoundaryFinderBufferSize);
119
120     int idx;
121     int oldidx = 0;
122     bool wordStart = false;
123     bool wordEnd = false;
124     Mode mode = SearchEnd;
125     ChatLineModel::Word word;
126     word.start = 0;
127     qreal wordstartx = 0;
128
129     QTextLayout layout(_styledMsg->contents());
130     QTextOption option;
131     option.setWrapMode(QTextOption::NoWrap);
132     layout.setTextOption(option);
133
134     layout.setAdditionalFormats(QtUi::style()->toTextLayoutList(_styledMsg->contentsFormatList(), length));
135     layout.beginLayout();
136     QTextLine line = layout.createLine();
137     line.setNumColumns(length);
138     layout.endLayout();
139
140     do {
141       idx = finder.toNextBoundary();
142       if(idx < 0) {
143         idx = length;
144         wordStart = false;
145         wordEnd = false;
146         mode = SearchStart;
147       } else {
148         wordStart = finder.boundaryReasons().testFlag(QTextBoundaryFinder::StartWord);
149         wordEnd = finder.boundaryReasons().testFlag(QTextBoundaryFinder::EndWord);
150       }
151
152       //if(flg) qDebug() << idx << mode << wordStart << wordEnd << contents->plainText.left(idx) << contents->plainText.mid(idx);
153
154       if(mode == SearchEnd || (!wordStart && wordEnd)) {
155         if(wordStart || !wordEnd) continue;
156         oldidx = idx;
157         mode = SearchStart;
158         continue;
159       }
160       qreal wordendx = line.cursorToX(oldidx);
161       qreal trailingendx = line.cursorToX(idx);
162       word.endX = wordendx;
163       word.width = wordendx - wordstartx;
164       word.trailing = trailingendx - wordendx;
165       wordstartx = trailingendx;
166       wplist.append(word);
167
168       if(wordStart) {
169         word.start = idx;
170         mode = SearchEnd;
171       }
172       // the part " || (finder.position() == contents->plainText.length())" shouldn't be necessary
173       // but in rare and indeterministic cases Qt states that the end of the text is not a boundary o_O
174     } while(finder.isAtBoundary() || (finder.position() == length));
175
176     // A QVector needs less space than a QList
177     _wrapList.resize(wplist.count());
178     for(int i = 0; i < wplist.count(); i++) {
179       _wrapList[i] = wplist.at(i);
180     }
181   }
182
183   ChatLineModel::WrapList _wrapList;
184   Message *_msgBuffer;
185   UiStyle::StyledMessage *_styledMsg;
186
187   static unsigned char *TextBoundaryFinderBuffer;
188   static int TextBoundaryFinderBufferSize;
189 };
190
191 unsigned char *ChatLineModelItemPrivate::TextBoundaryFinderBuffer = (unsigned char *)malloc(512 * sizeof(HB_CharAttributes_Dummy));
192 int ChatLineModelItemPrivate::TextBoundaryFinderBufferSize = 512 * (sizeof(HB_CharAttributes_Dummy) / sizeof(unsigned char));
193
194
195 // ****************************************
196 // the actual ChatLineModelItem
197 // ****************************************
198 ChatLineModelItem::ChatLineModelItem(const Message &msg)
199   : MessageModelItem(msg),
200     _data(new ChatLineModelItemPrivate(msg))
201 {
202 }
203
204 ChatLineModelItem::~ChatLineModelItem() {
205   delete _data;
206 }
207
208 QVariant ChatLineModelItem::data(int column, int role) const {
209   QVariant d = _data->data((MessageModel::ColumnType)column, role);
210   if(!d.isValid()) return MessageModelItem::data(column, role);
211   return d;
212 }