6b1665cf012e8dc7851f4c8f580a052a1bcf142f
[quassel.git] / src / qmlui / qmlchatline.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2011 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 #include <QPainter>
21
22 #include "qmlchatline.h"
23
24 void QmlChatLine::registerTypes() {
25   qRegisterMetaType<RenderData>("QmlChatLine::RenderData");
26   qRegisterMetaTypeStreamOperators<RenderData>("QmlChatLine::RenderData");
27   qmlRegisterType<QmlChatLine>("eu.quassel.qml", 1, 0, "ChatLine");
28 }
29
30 QDataStream &operator<<(QDataStream &out, const QmlChatLine::RenderData &data) {
31   out << data.isValid << data.messageLabel;
32   for(int i = 0; i < (int)QmlChatLine::NumColumns; ++i) {
33     const QmlChatLine::RenderData::Column &col = data[static_cast<QmlChatLine::ColumnType>(i)];
34     out << col.text << col.formats << col.background << col.selectedBackground;
35   }
36   return out;
37 }
38
39 QDataStream &operator>>(QDataStream &in, QmlChatLine::RenderData &data) {
40   in >> data.isValid >> data.messageLabel;
41   for(int i = 0; i < (int)QmlChatLine::NumColumns; ++i) {
42     QmlChatLine::RenderData::Column &col = data[static_cast<QmlChatLine::ColumnType>(i)];
43     in >> col.text >> col.formats >> col.background >> col.selectedBackground;
44   }
45   return in;
46 }
47
48 QmlChatLine::QmlChatLine(QDeclarativeItem *parent)
49   : QDeclarativeItem(parent),
50     _timestampWidth(0),
51     _senderWidth(0),
52     _contentsWidth(0),
53     _layout(0)
54 {
55   setFlag(ItemHasNoContents, false);
56   setImplicitHeight(20);
57   setImplicitWidth(1000);
58   connect(this, SIGNAL(columnWidthChanged(ColumnType)), SLOT(onColumnWidthChanged(ColumnType)));
59 }
60
61 QmlChatLine::~QmlChatLine() {
62
63 }
64
65 void QmlChatLine::setTimestampWidth(qreal w) {
66   if(w != _timestampWidth) {
67     _timestampWidth = w;
68     emit timestampWidthChanged(w);
69     emit columnWidthChanged(TimestampColumn);
70   }
71 }
72
73 void QmlChatLine::setSenderWidth(qreal w) {
74   if(w != _senderWidth) {
75     _senderWidth = w;
76     emit senderWidthChanged(w);
77     emit columnWidthChanged(SenderColumn);
78   }
79 }
80
81 void QmlChatLine::setContentsWidth(qreal w) {
82   if(w != _contentsWidth) {
83     _contentsWidth = w;
84     emit contentsWidthChanged(w);
85     emit columnWidthChanged(ContentsColumn);
86   }
87 }
88
89 void QmlChatLine::setColumnSpacing(qreal s) {
90   if(s != _columnSpacing) {
91     _columnSpacing = s;
92     emit columnSpacingChanged(s);
93   }
94 }
95
96 QPointF QmlChatLine::columnPos(ColumnType colType) const {
97   switch(colType) {
98   case TimestampColumn:
99     return QPointF(0, 0);
100   case SenderColumn:
101     return QPointF(timestampWidth(), 0);
102   case ContentsColumn:
103     return QPointF(timestampWidth() + senderWidth(), 0);
104   default:
105     return QPointF();
106   }
107 }
108
109 qreal QmlChatLine::columnWidth(ColumnType colType) const {
110   switch(colType) {
111   case TimestampColumn:
112     return timestampWidth();
113   case SenderColumn:
114     return senderWidth();
115   case ContentsColumn:
116     return contentsWidth();
117   default:
118     return 0;
119   }
120 }
121
122 QRectF QmlChatLine::columnBoundingRect(ColumnType colType) const {
123   QRectF rect;
124   switch(colType) {
125   case TimestampColumn:
126     return QRectF(columnPos(TimestampColumn), QSizeF(timestampWidth() - columnSpacing(), implicitHeight()));
127   case SenderColumn:
128     return QRectF(columnPos(SenderColumn), QSizeF(senderWidth() - columnSpacing(), implicitHeight()));
129   case ContentsColumn:
130     return QRectF(columnPos(ContentsColumn), QSizeF(contentsWidth(), implicitHeight()));
131   default:
132     return QRectF();
133   }
134 }
135
136 void QmlChatLine::setRenderData(const RenderData &data) {
137   _data = data;
138   if(_layout) {
139     delete _layout;
140     _layout = 0;
141   }
142
143   //update();
144 }
145
146 QmlChatLine::ColumnLayout *QmlChatLine::layout() const {
147   if(!_layout) {
148     _layout = new ColumnLayout(this);
149   }
150   return _layout;
151 }
152
153 void QmlChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
154   Q_UNUSED(option)
155   Q_UNUSED(widget)
156   //painter->drawText(0, 0, renderData()[TimestampColumn].text);
157   //painter->drawText(timestampWidth() + columnSpacing(), 0, renderData()[SenderColumn].text);
158   //painter->drawText(timestampWidth() + senderWidth() + 2*columnSpacing(), 0, renderData()[ContentsColumn].text);
159   layout()->draw(painter);
160 }
161
162 void QmlChatLine::onColumnWidthChanged(ColumnType colType) {
163
164   //qDebug() << "changed width" << _timestampWidth << _senderWidth << _contentsWidth;
165   //setImplicitHeight(implicitHeight() + 5);
166
167   if(colType == ContentsColumn) {
168     layout()->prepare();
169     setImplicitHeight(layout()->height());
170   }
171
172   update();
173 }
174
175 /**************************************************************************************/
176
177 QmlChatLine::ColumnLayout::ColumnLayout(const QmlChatLine *parent)
178   : _parent(parent)
179 {
180
181 }
182
183 qreal QmlChatLine::ColumnLayout::height() const {
184   return chatLine()->contentsWidth()/20;
185 }
186
187 void QmlChatLine::ColumnLayout::prepare() {
188
189 }
190
191 void QmlChatLine::ColumnLayout::draw(QPainter *p) {
192   p->drawText(chatLine()->boundingRect(), chatLine()->renderData()[ContentsColumn].text);
193   //p->drawText(chatLine()->timestampWidth() + chatLine()->columnSpacing(), 0, chatLine()->renderData()[SenderColumn].text);
194   //p->drawText(chatLine()->timestampWidth() + chatLine()->senderWidth() + 2*chatLine()->columnSpacing(), 0, chatLine()->renderData()[ContentsColumn].text);
195
196 }