fixed the target buffers for queries
[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(QGraphicsItem *parent) : QGraphicsItem(parent) {
31   _width = 0;
32   //if(_wrapMode == WordWrap) {
33   //  setFlags(QGraphicsItem::ItemClipsToShape, true);
34   //}
35 }
36
37 ChatItem::~ChatItem() {
38
39 }
40
41 void ChatItem::setWidth(int w) {
42   _width = w;
43   layout();
44 }
45
46 void ChatItem::setTextOption(const QTextOption &option) {
47   _textOption = option;
48   layout();
49 }
50
51 QTextOption ChatItem::textOption() const {
52   return _textOption;
53 }
54
55 QString ChatItem::text() const {
56   return _layout.text();
57 }
58
59 void ChatItem::setText(const UiStyle::StyledText &text) {
60   _layout.setText(text.text);
61   _layout.setAdditionalFormats(text.formats);
62   layout();
63 }
64
65 void ChatItem::layout() {
66   if(!_layout.additionalFormats().count()) return; // no text set
67   if(_width <= 0) return;
68   prepareGeometryChange();
69   QFontMetrics metrics(_layout.additionalFormats()[0].format.font());
70   int leading = metrics.leading();
71   int height = 0;
72   _layout.setTextOption(textOption());
73   _layout.beginLayout();
74   while(1) {
75     QTextLine line = _layout.createLine();
76     if(!line.isValid()) break;
77     line.setLineWidth(_width);
78     if(textOption().wrapMode() != QTextOption::NoWrap && line.naturalTextWidth() > _width) {
79       // word did not fit, we need to wrap it in the middle
80       // this is a workaround for Qt failing to handle WrapAtWordBoundaryOrAnywhere correctly
81       QTextOption::WrapMode mode = textOption().wrapMode();
82       textOption().setWrapMode(QTextOption::WrapAnywhere);
83       _layout.setTextOption(textOption());
84       line.setLineWidth(_width);
85       textOption().setWrapMode(mode);
86       _layout.setTextOption(textOption());
87     }
88     height += leading;
89     line.setPosition(QPoint(0, height));
90     height += line.height();
91   }
92   _layout.endLayout();
93   update();
94 }
95
96 QRectF ChatItem::boundingRect() const {
97   return _layout.boundingRect();
98 }
99
100 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
101   Q_UNUSED(option); Q_UNUSED(widget);
102   _layout.draw(painter, QPointF(0, 0));
103
104 }
105
106 /*
107 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
108   qDebug() << (void*)this << "moving" << event->pos();
109   if(event->pos().y() < 0) {
110     QTextCursor cursor(document());
111     //cursor.insertText("foo");
112     //cursor.select(QTextCursor::Document);
113     event->ignore();
114   } else QGraphicsTextItem::mouseMoveEvent(event);
115 }
116 */
117
118
119