Introduce column separator items; change most of ChatView to use qreal
[quassel.git] / src / qtui / chatitem.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 <QGraphicsSceneMouseEvent>
23 #include <QPainter>
24 #include <QTextLayout>
25
26 #include "chatitem.h"
27 #include "chatlinemodel.h"
28 #include "qtui.h"
29
30 ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
31   _fontMetrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
32   _layout = 0;
33   _lines = 0;
34 }
35
36 ChatItem::~ChatItem() {
37   delete _layout;
38 }
39
40 QVariant ChatItem::data(int role) const {
41   if(!_index.isValid()) {
42     qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
43     return QVariant();
44   }
45   return _index.data(role);
46 }
47
48 int ChatItem::setWidth(int w) {
49   if(w == _boundingRect.width()) return _boundingRect.height();
50   prepareGeometryChange();
51   _boundingRect.setWidth(w);
52   int h = heightForWidth(w);
53   _boundingRect.setHeight(h);
54   if(haveLayout()) updateLayout();
55   return h;
56 }
57
58 int ChatItem::heightForWidth(int width) {
59   if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
60     return fontMetrics()->lineSpacing(); // only contents can be multi-line
61
62   _lines = 1;
63   WrapColumnFinder finder(this);
64   while(finder.nextWrapColumn() > 0) _lines++;
65   return _lines * fontMetrics()->lineSpacing();
66 }
67
68 QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) {
69   QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
70
71   QTextOption option;
72   option.setWrapMode(wrapMode);
73   option.setAlignment(alignment);
74   layout->setTextOption(option);
75
76   QList<QTextLayout::FormatRange> formatRanges
77          = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
78   layout->setAdditionalFormats(formatRanges);
79   return layout;
80 }
81
82 void ChatItem::updateLayout() {
83   switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
84     case ChatLineModel::TimestampColumn:
85       if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
86       // fallthrough
87     case ChatLineModel::SenderColumn:
88       if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
89       _layout->beginLayout();
90       {
91         QTextLine line = _layout->createLine();
92         if(line.isValid()) {
93           line.setLineWidth(width());
94           line.setPosition(QPointF(0, fontMetrics()->leading()));
95         }
96         _layout->endLayout();
97       }
98       break;
99     case ChatLineModel::ContentsColumn: {
100       if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
101
102       // Now layout
103       ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
104       if(!wrapList.count()) return; // empty chatitem
105       int wordidx = 0;
106       ChatLineModel::Word word = wrapList.at(0);
107
108       qreal h = 0;
109       WrapColumnFinder finder(this);
110       _layout->beginLayout();
111       forever {
112         QTextLine line = _layout->createLine();
113         if (!line.isValid())
114           break;
115
116         int col = finder.nextWrapColumn();
117         line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
118
119         h += fontMetrics()->leading();
120         line.setPosition(QPointF(0, h));
121         h += line.height();
122       }
123       _layout->endLayout();
124     }
125     break;
126   }
127 }
128
129 void ChatItem::clearLayout() {
130   delete _layout;
131   _layout = 0;
132 }
133
134 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
135   Q_UNUSED(option); Q_UNUSED(widget);
136   if(!haveLayout()) updateLayout();
137   _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
138   //painter->drawRect(boundingRect());
139
140 }
141
142 /*
143 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
144   qDebug() << (void*)this << "moving" << event->pos();
145   if(event->pos().y() < 0) {
146     QTextCursor cursor(document());
147     //cursor.insertText("foo");
148     //cursor.select(QTextCursor::Document);
149     event->ignore();
150   } else QGraphicsTextItem::mouseMoveEvent(event);
151 }
152 */
153
154 /*************************************************************************************************/
155
156 ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
157   wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
158   wordidx = 0;
159   layout = 0;
160   lastwrapcol = 0;
161   lastwrappos = 0;
162   w = 0;
163 }
164
165 ChatItem::WrapColumnFinder::~WrapColumnFinder() {
166   delete layout;
167 }
168
169 int ChatItem::WrapColumnFinder::nextWrapColumn() {
170   while(wordidx < wrapList.count()) {
171     w += wrapList.at(wordidx).width;
172     if(w >= item->width()) {
173       if(lastwrapcol >= wrapList.at(wordidx).start) {
174         // first word, and it doesn't fit
175         if(!line.isValid()) {
176           layout = item->createLayout(QTextOption::NoWrap);
177           layout->beginLayout();
178           line = layout->createLine();
179           line.setLineWidth(item->width());
180           layout->endLayout();
181         }
182         int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
183         qreal x = line.cursorToX(idx, QTextLine::Trailing);
184         w = w - wrapList.at(wordidx).width - (x - lastwrappos);
185         lastwrappos = x;
186         lastwrapcol = idx;
187         return idx;
188       }
189       // not the first word, so just wrap before this
190       lastwrapcol = wrapList.at(wordidx).start;
191       lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
192       w = 0;
193       return lastwrapcol;
194     }
195     w += wrapList.at(wordidx).trailing;
196     wordidx++;
197   }
198   return -1;
199 }