UiStyle::StyledMessage derived now from Message
[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 <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);
45   ~ChatLineModelItemPrivate();
46
47   inline bool needsStyling();
48   QVariant data(MessageModel::ColumnType column, int role);
49
50 private:
51   void style();
52   void computeWrapList();
53
54   ChatLineModel::WrapList _wrapList;
55   Message *_msgBuffer;
56   UiStyle::StyledMessage *_styledMsg;
57
58   static unsigned char *TextBoundaryFinderBuffer;
59   static int TextBoundaryFinderBufferSize;
60 };
61
62 ChatLineModelItemPrivate::ChatLineModelItemPrivate(const Message &msg)
63 : _msgBuffer(new Message(msg)),
64   _styledMsg(0) {
65
66 }
67
68 ChatLineModelItemPrivate::~ChatLineModelItemPrivate() {
69   if(_msgBuffer) {
70     delete _msgBuffer;
71   } else {
72     delete _styledMsg;
73   }
74 }
75
76 bool ChatLineModelItemPrivate::needsStyling() {
77   return (bool)_msgBuffer;
78 }
79
80 QVariant ChatLineModelItemPrivate::data(MessageModel::ColumnType column, int role) {
81   if(needsStyling())
82     style();
83   switch(column) {
84     case ChatLineModel::TimestampColumn:
85       switch(role) {
86         case ChatLineModel::DisplayRole:
87           return _styledMsg->decoratedTimestamp();
88         case ChatLineModel::EditRole:
89           return _styledMsg->timestamp();
90         case ChatLineModel::FormatRole:
91           return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
92                                     << qMakePair((quint16)0, (quint32)_styledMsg->timestampFormat()));
93       }
94       break;
95     case ChatLineModel::SenderColumn:
96       switch(role) {
97         case ChatLineModel::DisplayRole:
98           return _styledMsg->decoratedSender();
99         case ChatLineModel::EditRole:
100           return _styledMsg->plainSender();
101         case ChatLineModel::FormatRole:
102           return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
103                                     << qMakePair((quint16)0, (quint32)_styledMsg->senderFormat()));
104       }
105       break;
106     case ChatLineModel::ContentsColumn:
107       switch(role) {
108         case ChatLineModel::DisplayRole:
109         case ChatLineModel::EditRole:
110           return _styledMsg->plainContents();
111         case ChatLineModel::FormatRole:
112           return QVariant::fromValue<UiStyle::FormatList>(_styledMsg->contentsFormatList());
113         case ChatLineModel::WrapListRole:
114           if(_wrapList.isEmpty())
115             computeWrapList();
116           return QVariant::fromValue<ChatLineModel::WrapList>(_wrapList);
117       }
118       break;
119     default:
120       Q_ASSERT(false);
121       return 0;
122   }
123   return QVariant();
124 }
125
126 void ChatLineModelItemPrivate::style() {
127   _styledMsg = new QtUiStyle::StyledMessage(*_msgBuffer);
128   _styledMsg->style(QtUi::style());
129   delete _msgBuffer;
130   _msgBuffer = 0;
131 }
132
133 void ChatLineModelItemPrivate::computeWrapList() {
134   int length = _styledMsg->contents().length();
135   if(!length)
136     return;
137
138   enum Mode { SearchStart, SearchEnd };
139
140   QList<ChatLineModel::Word> wplist;  // use a temp list which we'll later copy into a QVector for efficiency
141   QTextBoundaryFinder finder(QTextBoundaryFinder::Word, _styledMsg->contents().unicode(), length,
142                               TextBoundaryFinderBuffer, TextBoundaryFinderBufferSize);
143
144   int idx;
145   int oldidx = 0;
146   bool wordStart = false;
147   bool wordEnd = false;
148   Mode mode = SearchEnd;
149   ChatLineModel::Word word;
150   word.start = 0;
151   qreal wordstartx = 0;
152
153   QTextLayout layout(_styledMsg->contents());
154   QTextOption option;
155   option.setWrapMode(QTextOption::NoWrap);
156   layout.setTextOption(option);
157
158   layout.setAdditionalFormats(QtUi::style()->toTextLayoutList(_styledMsg->contentsFormatList(), length));
159   layout.beginLayout();
160   QTextLine line = layout.createLine();
161   line.setNumColumns(length);
162   layout.endLayout();
163
164   do {
165     idx = finder.toNextBoundary();
166     if(idx < 0) {
167       idx = length;
168       wordStart = false;
169       wordEnd = false;
170       mode = SearchStart;
171     } else {
172       wordStart = finder.boundaryReasons().testFlag(QTextBoundaryFinder::StartWord);
173       wordEnd = finder.boundaryReasons().testFlag(QTextBoundaryFinder::EndWord);
174     }
175
176     //if(flg) qDebug() << idx << mode << wordStart << wordEnd << contents->plainText.left(idx) << contents->plainText.mid(idx);
177
178     if(mode == SearchEnd || (!wordStart && wordEnd)) {
179       if(wordStart || !wordEnd) continue;
180       oldidx = idx;
181       mode = SearchStart;
182       continue;
183     }
184     qreal wordendx = line.cursorToX(oldidx);
185     qreal trailingendx = line.cursorToX(idx);
186     word.endX = wordendx;
187     word.width = wordendx - wordstartx;
188     word.trailing = trailingendx - wordendx;
189     wordstartx = trailingendx;
190     wplist.append(word);
191
192     if(wordStart) {
193       word.start = idx;
194       mode = SearchEnd;
195     }
196     // the part " || (finder.position() == contents->plainText.length())" shouldn't be necessary
197     // but in rare and indeterministic cases Qt states that the end of the text is not a boundary o_O
198   } while(finder.isAtBoundary() || (finder.position() == length));
199
200   // A QVector needs less space than a QList
201   _wrapList.resize(wplist.count());
202   for(int i = 0; i < wplist.count(); i++) {
203     _wrapList[i] = wplist.at(i);
204   }
205 }
206
207 unsigned char *ChatLineModelItemPrivate::TextBoundaryFinderBuffer = (unsigned char *)malloc(512 * sizeof(HB_CharAttributes_Dummy));
208 int ChatLineModelItemPrivate::TextBoundaryFinderBufferSize = 512 * (sizeof(HB_CharAttributes_Dummy) / sizeof(unsigned char));
209
210 // ****************************************
211 // the actual ChatLineModelItem
212 // ****************************************
213 ChatLineModelItem::ChatLineModelItem(const Message &msg)
214   : MessageModelItem(msg),
215     _data(new ChatLineModelItemPrivate(msg))
216 {
217 }
218
219 ChatLineModelItem::ChatLineModelItem(const ChatLineModelItem &other)
220   : MessageModelItem(other)
221 {
222   _data = new ChatLineModelItemPrivate(message());
223 }
224
225 ChatLineModelItem::~ChatLineModelItem() {
226   delete _data;
227 }
228
229 QVariant ChatLineModelItem::data(int column, int role) const {
230   QVariant d = _data->data((MessageModel::ColumnType)column, role);
231   if(!d.isValid()) return MessageModelItem::data(column, role);
232   return d;
233 }