0e93ddbfd1535dadffd021d5a3ab7a176c22ce4d
[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 "uistyle.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 unsigned char *ChatLineModelItem::TextBoundaryFinderBuffer = (unsigned char *)malloc(512 * sizeof(HB_CharAttributes_Dummy));
41 int ChatLineModelItem::TextBoundaryFinderBufferSize = 512 * (sizeof(HB_CharAttributes_Dummy) / sizeof(unsigned char));
42
43 struct ChatLineModelItemPrivate {
44   ChatLineModel::WrapList wrapList;
45 };
46
47 ChatLineModelItem::ChatLineModelItem(const Message &msg)
48   : MessageModelItem(msg),
49     _data(new ChatLineModelItemPrivate)
50 {
51   QtUiStyle::StyledMessage m = QtUi::style()->styleMessage(msg);
52
53   _timestamp.plainText = m.timestamp.plainText;
54   _sender.plainText = m.sender.plainText;
55   _contents.plainText = m.contents.plainText;
56
57   _timestamp.formatList = m.timestamp.formatList;
58   _sender.formatList = m.sender.formatList;
59   _contents.formatList = m.contents.formatList;
60 }
61
62 ChatLineModelItem::~ChatLineModelItem() {
63   delete _data;
64 }
65
66 QVariant ChatLineModelItem::data(int column, int role) const {
67   const ChatLinePart *part = 0;
68
69   switch(column) {
70   case ChatLineModel::TimestampColumn:
71     part = &_timestamp;
72     break;
73   case ChatLineModel::SenderColumn:
74     part = &_sender;
75     break;
76   case ChatLineModel::ContentsColumn:
77     part = &_contents;
78     break;
79   default:
80     return MessageModelItem::data(column, role);
81   }
82
83   switch(role) {
84   case ChatLineModel::DisplayRole:
85     return part->plainText;
86   case ChatLineModel::FormatRole:
87     return QVariant::fromValue<UiStyle::FormatList>(part->formatList);
88   case ChatLineModel::WrapListRole:
89     if(column != ChatLineModel::ContentsColumn)
90       return QVariant();
91     if(_data->wrapList.isEmpty())
92       computeWrapList();
93     return QVariant::fromValue<ChatLineModel::WrapList>(_data->wrapList);
94   }
95   return MessageModelItem::data(column, role);
96 }
97
98 void ChatLineModelItem::computeWrapList() const {
99   if(_contents.plainText.isEmpty())
100     return;
101
102   enum Mode { SearchStart, SearchEnd };
103
104   QList<ChatLineModel::Word> wplist;  // use a temp list which we'll later copy into a QVector for efficiency
105   QTextBoundaryFinder finder(QTextBoundaryFinder::Word, _contents.plainText.unicode(), _contents.plainText.length(), TextBoundaryFinderBuffer, TextBoundaryFinderBufferSize);
106
107   int idx;
108   int oldidx = 0;
109   bool wordStart = false;
110   bool wordEnd = false;
111   Mode mode = SearchEnd;
112   ChatLineModel::Word word;
113   word.start = 0;
114   int wordstartx = 0;
115
116   QTextLayout layout(_contents.plainText);
117   QTextOption option;
118   option.setWrapMode(QTextOption::NoWrap);
119   layout.setTextOption(option);
120
121   layout.setAdditionalFormats(QtUi::style()->toTextLayoutList(_contents.formatList, _contents.plainText.length()));
122   layout.beginLayout();
123   QTextLine line = layout.createLine();
124   line.setNumColumns(_contents.plainText.length());
125   layout.endLayout();
126
127   do {
128     idx = finder.toNextBoundary();
129     if(idx < 0) {
130       idx = _contents.plainText.length();
131       wordStart = false;
132       wordEnd = false;
133       mode = SearchStart;
134     } else {
135       wordStart = finder.boundaryReasons().testFlag(QTextBoundaryFinder::StartWord);
136       wordEnd = finder.boundaryReasons().testFlag(QTextBoundaryFinder::EndWord);
137     }
138
139     //if(flg) qDebug() << idx << mode << wordStart << wordEnd << _contents.plainText.left(idx) << _contents.plainText.mid(idx);
140
141     if(mode == SearchEnd || (!wordStart && wordEnd)) {
142       if(wordStart || !wordEnd) continue;
143       oldidx = idx;
144       mode = SearchStart;
145       continue;
146     }
147     int wordendx = line.cursorToX(oldidx);
148     int trailingendx = line.cursorToX(idx);
149     word.width = wordendx - wordstartx;
150     word.trailing = trailingendx - wordendx;
151     wordstartx = trailingendx;
152     wplist.append(word);
153
154     if(wordStart) {
155       word.start = idx;
156       mode = SearchEnd;
157     }
158     // the part " || (finder.position() == _contents.plainText.length())" shouldn't be necessary
159     // but in rare and indeterministic cases Qt states that the end of the text is not a boundary o_O
160   } while(finder.isAtBoundary() || (finder.position() == _contents.plainText.length()));
161
162   // A QVector needs less space than a QList
163   _data->wrapList.resize(wplist.count());
164   for(int i = 0; i < wplist.count(); i++) {
165     _data->wrapList[i] = wplist.at(i);
166   }
167 }
168