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