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