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