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