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