Merging r780:797 from trunk to branches/0.3.
[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 <QTextCursor>
24 #include <QTextDocument>
25
26 #include <QtGui>
27
28 #include "chatitem.h"
29
30 ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
31   //if(_wrapMode == WordWrap) {
32   //  setFlags(QGraphicsItem::ItemClipsToShape, true);
33   //}
34 }
35
36 ChatItem::~ChatItem() {
37
38 }
39
40 QVariant ChatItem::data(int role) const {
41   if(!_index.isValid()) {
42     qWarning() << "ChatItem::data(): Model index is invalid!";
43     return QVariant();
44   }
45   return _index.data(role);
46 }
47
48 QRectF ChatItem::boundingRect() const {
49   return QRectF(0, 0, 500,20);
50 }
51
52 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
53   Q_UNUSED(option); Q_UNUSED(widget);
54
55   painter->drawRect(boundingRect());
56   painter->drawText(boundingRect(), data(MessageModel::DisplayRole).toString());
57 }
58
59
60 /*
61 void ChatItem::setWidth(int w) {
62   _width = w;
63   layout();
64 }
65
66 void ChatItem::setTextOption(const QTextOption &option) {
67   _textOption = option;
68   layout();
69 }
70
71 QTextOption ChatItem::textOption() const {
72   return _textOption;
73 }
74
75 QString ChatItem::text() const {
76   return _layout.text();
77 }
78
79 void ChatItem::setText(const UiStyle::StyledText &text) {
80   _layout.setText(text.text);
81   _layout.setAdditionalFormats(text.formats);
82   layout();
83 }
84
85 void ChatItem::layout() {
86   if(!_layout.additionalFormats().count()) return; // no text set
87   if(_width <= 0) return;
88   prepareGeometryChange();
89   QFontMetrics metrics(_layout.additionalFormats()[0].format.font());
90   int leading = metrics.leading();
91   int height = 0;
92   _layout.setTextOption(textOption());
93   _layout.beginLayout();
94   while(1) {
95     QTextLine line = _layout.createLine();
96     if(!line.isValid()) break;
97     line.setLineWidth(_width);
98     if(textOption().wrapMode() != QTextOption::NoWrap && line.naturalTextWidth() > _width) {
99       // word did not fit, we need to wrap it in the middle
100       // this is a workaround for Qt failing to handle WrapAtWordBoundaryOrAnywhere correctly
101       QTextOption::WrapMode mode = textOption().wrapMode();
102       textOption().setWrapMode(QTextOption::WrapAnywhere);
103       _layout.setTextOption(textOption());
104       line.setLineWidth(_width);
105       textOption().setWrapMode(mode);
106       _layout.setTextOption(textOption());
107     }
108     height += leading;
109     line.setPosition(QPoint(0, height));
110     height += line.height();
111   }
112   _layout.endLayout();
113   update();
114 }    QDateTime _timestamp;
115     MsgId _msgId;
116
117
118 QRectF ChatItem::boundingRect() const {
119   return _layout.boundingRect();
120 }
121
122 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
123   Q_UNUSED(option); Q_UNUSED(widget);
124   _layout.draw(painter, QPointF(0, 0));
125
126 }
127 */
128
129 /*
130 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
131   qDebug() << (void*)this << "moving" << event->pos();
132   if(event->pos().y() < 0) {
133     QTextCursor cursor(document());
134     //cursor.insertText("foo");
135     //cursor.select(QTextCursor::Document);
136     event->ignore();
137   } else QGraphicsTextItem::mouseMoveEvent(event);
138 }
139 */
140
141
142