1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 unsigned char *ChatLineModelItem::TextBoundaryFinderBuffer = (unsigned char *)malloc(512 * sizeof(HB_CharAttributes_Dummy));
41 int ChatLineModelItem::TextBoundaryFinderBufferSize = 512 * (sizeof(HB_CharAttributes_Dummy) / sizeof(unsigned char));
43 // ****************************************
44 // the actual ChatLineModelItem
45 // ****************************************
46 ChatLineModelItem::ChatLineModelItem(const Message &msg)
50 if (!msg.sender().contains('!'))
51 _styledMsg.setFlags(msg.flags() |= Message::ServerMsg);
53 if (_styledMsg.type() == Message::Nick) {
54 // HACK: Work around nick changes on Quassel core not properly being set as Self
55 // While this is fixed in the core, old cores and past history will still show incorrectly.
56 if (nickFromMask(_styledMsg.sender()) == stripFormatCodes(_styledMsg.contents()).toLower()) {
57 _styledMsg.setFlags(msg.flags() |= Message::Self);
60 // Unfortunately, the missing Self flag for other message types can't easily be worked around.
64 bool ChatLineModelItem::setData(int column, const QVariant &value, int role)
67 case MessageModel::FlagsRole:
68 _styledMsg.setFlags((Message::Flags)value.toUInt());
71 return MessageModelItem::setData(column, value, role);
76 QVariant ChatLineModelItem::data(int column, int role) const
78 if (role == ChatLineModel::MsgLabelRole)
79 return messageLabel();
82 MessageModel::ColumnType col = (MessageModel::ColumnType)column;
84 case ChatLineModel::TimestampColumn:
85 variant = timestampData(role);
87 case ChatLineModel::SenderColumn:
88 variant = senderData(role);
90 case ChatLineModel::ContentsColumn:
91 variant = contentsData(role);
96 if (!variant.isValid())
97 return MessageModelItem::data(column, role);
102 QVariant ChatLineModelItem::timestampData(int role) const
105 case ChatLineModel::DisplayRole:
106 return _styledMsg.decoratedTimestamp();
107 case ChatLineModel::EditRole:
108 return _styledMsg.timestamp();
109 case ChatLineModel::BackgroundRole:
110 return backgroundBrush(UiStyle::Timestamp);
111 case ChatLineModel::SelectedBackgroundRole:
112 return backgroundBrush(UiStyle::Timestamp, true);
113 case ChatLineModel::FormatRole:
114 return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
115 << qMakePair((quint16)0, (quint32) UiStyle::formatType(_styledMsg.type()) | UiStyle::Timestamp));
121 QVariant ChatLineModelItem::senderData(int role) const
124 case ChatLineModel::DisplayRole:
125 return _styledMsg.decoratedSender();
126 case ChatLineModel::EditRole:
127 return _styledMsg.plainSender();
128 case ChatLineModel::BackgroundRole:
129 return backgroundBrush(UiStyle::Sender);
130 case ChatLineModel::SelectedBackgroundRole:
131 return backgroundBrush(UiStyle::Sender, true);
132 case ChatLineModel::FormatRole:
133 return QVariant::fromValue<UiStyle::FormatList>(UiStyle::FormatList()
134 << qMakePair((quint16)0, (quint32) UiStyle::formatType(_styledMsg.type()) | UiStyle::Sender));
140 QVariant ChatLineModelItem::contentsData(int role) const
143 case ChatLineModel::DisplayRole:
144 case ChatLineModel::EditRole:
145 return _styledMsg.plainContents();
146 case ChatLineModel::BackgroundRole:
147 return backgroundBrush(UiStyle::Contents);
148 case ChatLineModel::SelectedBackgroundRole:
149 return backgroundBrush(UiStyle::Contents, true);
150 case ChatLineModel::FormatRole:
151 return QVariant::fromValue<UiStyle::FormatList>(_styledMsg.contentsFormatList());
152 case ChatLineModel::WrapListRole:
153 if (_wrapList.isEmpty())
155 return QVariant::fromValue<ChatLineModel::WrapList>(_wrapList);
161 quint32 ChatLineModelItem::messageLabel() const
163 quint32 label = _styledMsg.senderHash() << 16;
164 if (_styledMsg.flags() & Message::Self)
165 label |= UiStyle::OwnMsg;
166 if (_styledMsg.flags() & Message::Highlight)
167 label |= UiStyle::Highlight;
172 QVariant ChatLineModelItem::backgroundBrush(UiStyle::FormatType subelement, bool selected) const
174 QTextCharFormat fmt = QtUi::style()->format(UiStyle::formatType(_styledMsg.type()) | subelement, messageLabel() | (selected ? UiStyle::Selected : 0));
175 if (fmt.hasProperty(QTextFormat::BackgroundBrush))
176 return QVariant::fromValue<QBrush>(fmt.background());
181 void ChatLineModelItem::computeWrapList() const
183 QString text = _styledMsg.plainContents();
184 int length = text.length();
188 QList<ChatLineModel::Word> wplist; // use a temp list which we'll later copy into a QVector for efficiency
189 QTextBoundaryFinder finder(QTextBoundaryFinder::Line, _styledMsg.plainContents().unicode(), length,
190 TextBoundaryFinderBuffer, TextBoundaryFinderBufferSize);
194 ChatLineModel::Word word;
196 qreal wordstartx = 0;
198 QTextLayout layout(_styledMsg.plainContents());
200 option.setWrapMode(QTextOption::NoWrap);
201 layout.setTextOption(option);
203 layout.setAdditionalFormats(QtUi::style()->toTextLayoutList(_styledMsg.contentsFormatList(), length, messageLabel()));
204 layout.beginLayout();
205 QTextLine line = layout.createLine();
206 line.setNumColumns(length);
209 while ((idx = finder.toNextBoundary()) >= 0 && idx <= length) {
210 // QTextBoundaryFinder has inconsistent behavior in Qt version up to and including 4.6.3 (at least).
211 // It doesn't point to the position we should break, but to the character before that.
212 // Unfortunately Qt decided to fix this by changing the behavior of QTBF, so now we have to add a version
213 // check. At the time of this writing, I'm still trying to get this reverted upstream...
215 // cf. https://bugs.webkit.org/show_bug.cgi?id=31076 and Qt commit e6ac173
216 static int needWorkaround = -1;
217 if (needWorkaround < 0) {
219 QStringList versions = QString(qVersion()).split('.');
220 if (versions.count() == 3 && versions.at(0).toInt() == 4) {
221 if (versions.at(1).toInt() <= 6 && versions.at(2).toInt() <= 3)
225 if (needWorkaround == 1) {
235 for (; wordend > word.start; wordend--) {
236 if (!text.at(wordend-1).isSpace())
240 qreal wordendx = line.cursorToX(wordend);
241 qreal trailingendx = line.cursorToX(idx);
242 word.endX = wordendx;
243 word.width = wordendx - wordstartx;
244 word.trailing = trailingendx - wordendx;
245 wordstartx = trailingendx;
251 // A QVector needs less space than a QList
252 _wrapList.resize(wplist.count());
253 for (int i = 0; i < wplist.count(); i++) {
254 _wrapList[i] = wplist.at(i);