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