1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QFontMetrics>
22 #include <QTextBoundaryFinder>
24 #include "chatlinemodelitem.h"
25 #include "chatlinemodel.h"
27 #include "qtuistyle.h"
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
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;
38 } HB_CharAttributes_Dummy;
40 // PRIVATE DATA FOR CHATLINE MODEL ITEM
41 class ChatLineModelItemPrivate {
44 inline ChatLineModelItemPrivate(const Message &msg);
45 ~ChatLineModelItemPrivate();
47 inline bool needsStyling();
48 QVariant data(MessageModel::ColumnType column, int role);
52 void computeWrapList();
54 ChatLineModel::WrapList _wrapList;
56 UiStyle::StyledMessage *_styledMsg;
58 static unsigned char *TextBoundaryFinderBuffer;
59 static int TextBoundaryFinderBufferSize;
62 ChatLineModelItemPrivate::ChatLineModelItemPrivate(const Message &msg)
63 : _msgBuffer(new Message(msg)),
68 ChatLineModelItemPrivate::~ChatLineModelItemPrivate() {
76 bool ChatLineModelItemPrivate::needsStyling() {
77 return (bool)_msgBuffer;
80 QVariant ChatLineModelItemPrivate::data(MessageModel::ColumnType column, int role) {
84 case ChatLineModel::TimestampColumn:
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()));
95 case ChatLineModel::SenderColumn:
97 case ChatLineModel::DisplayRole:
98 return _styledMsg->decoratedSender();
99 case ChatLineModel::EditRole:
100 return _styledMsg->sender();
101 case ChatLineModel::FormatRole:
102 return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
103 << qMakePair((quint16)0, (quint32)_styledMsg->senderFormat()));
106 case ChatLineModel::ContentsColumn:
108 case ChatLineModel::DisplayRole:
109 case ChatLineModel::EditRole:
110 return _styledMsg->contents();
111 case ChatLineModel::FormatRole:
112 return QVariant::fromValue<UiStyle::FormatList>(_styledMsg->contentsFormatList());
113 case ChatLineModel::WrapListRole:
114 if(_wrapList.isEmpty())
116 return QVariant::fromValue<ChatLineModel::WrapList>(_wrapList);
126 void ChatLineModelItemPrivate::style() {
127 _styledMsg = new QtUiStyle::StyledMessage(QtUi::style()->styleMessage(*_msgBuffer));
132 void ChatLineModelItemPrivate::computeWrapList() {
133 int length = _styledMsg->contents().length();
137 enum Mode { SearchStart, SearchEnd };
139 QList<ChatLineModel::Word> wplist; // use a temp list which we'll later copy into a QVector for efficiency
140 QTextBoundaryFinder finder(QTextBoundaryFinder::Word, _styledMsg->contents().unicode(), length,
141 TextBoundaryFinderBuffer, TextBoundaryFinderBufferSize);
145 bool wordStart = false;
146 bool wordEnd = false;
147 Mode mode = SearchEnd;
148 ChatLineModel::Word word;
150 qreal wordstartx = 0;
152 QTextLayout layout(_styledMsg->contents());
154 option.setWrapMode(QTextOption::NoWrap);
155 layout.setTextOption(option);
157 layout.setAdditionalFormats(QtUi::style()->toTextLayoutList(_styledMsg->contentsFormatList(), length));
158 layout.beginLayout();
159 QTextLine line = layout.createLine();
160 line.setNumColumns(length);
164 idx = finder.toNextBoundary();
171 wordStart = finder.boundaryReasons().testFlag(QTextBoundaryFinder::StartWord);
172 wordEnd = finder.boundaryReasons().testFlag(QTextBoundaryFinder::EndWord);
175 //if(flg) qDebug() << idx << mode << wordStart << wordEnd << contents->plainText.left(idx) << contents->plainText.mid(idx);
177 if(mode == SearchEnd || (!wordStart && wordEnd)) {
178 if(wordStart || !wordEnd) continue;
183 qreal wordendx = line.cursorToX(oldidx);
184 qreal trailingendx = line.cursorToX(idx);
185 word.endX = wordendx;
186 word.width = wordendx - wordstartx;
187 word.trailing = trailingendx - wordendx;
188 wordstartx = trailingendx;
195 // the part " || (finder.position() == contents->plainText.length())" shouldn't be necessary
196 // but in rare and indeterministic cases Qt states that the end of the text is not a boundary o_O
197 } while(finder.isAtBoundary() || (finder.position() == length));
199 // A QVector needs less space than a QList
200 _wrapList.resize(wplist.count());
201 for(int i = 0; i < wplist.count(); i++) {
202 _wrapList[i] = wplist.at(i);
206 unsigned char *ChatLineModelItemPrivate::TextBoundaryFinderBuffer = (unsigned char *)malloc(512 * sizeof(HB_CharAttributes_Dummy));
207 int ChatLineModelItemPrivate::TextBoundaryFinderBufferSize = 512 * (sizeof(HB_CharAttributes_Dummy) / sizeof(unsigned char));
209 // ****************************************
210 // the actual ChatLineModelItem
211 // ****************************************
212 ChatLineModelItem::ChatLineModelItem(const Message &msg)
213 : MessageModelItem(msg),
214 _data(new ChatLineModelItemPrivate(msg))
218 ChatLineModelItem::~ChatLineModelItem() {
222 QVariant ChatLineModelItem::data(int column, int role) const {
223 QVariant d = _data->data((MessageModel::ColumnType)column, role);
224 if(!d.isValid()) return MessageModelItem::data(column, role);