1 /***************************************************************************
2 * Copyright (C) 2005-2011 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include "qmlchatline.h"
24 void QmlChatLine::registerTypes() {
25 qRegisterMetaType<RenderData>("QmlChatLine::RenderData");
26 qRegisterMetaTypeStreamOperators<RenderData>("QmlChatLine::RenderData");
27 qmlRegisterType<QmlChatLine>("eu.quassel.qml", 1, 0, "ChatLine");
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;
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;
46 QmlChatLine::QmlChatLine(QDeclarativeItem *parent)
47 : QDeclarativeItem(parent),
53 setFlag(ItemHasNoContents, false);
54 setImplicitHeight(20);
55 setImplicitWidth(1000);
56 connect(this, SIGNAL(columnWidthChanged(ColumnType)), SLOT(onColumnWidthChanged(ColumnType)));
59 QmlChatLine::~QmlChatLine() {
63 void QmlChatLine::setTimestampWidth(qreal w) {
64 if(w != _timestampWidth) {
66 emit timestampWidthChanged(w);
67 emit columnWidthChanged(TimestampColumn);
71 void QmlChatLine::setSenderWidth(qreal w) {
72 if(w != _senderWidth) {
74 emit senderWidthChanged(w);
75 emit columnWidthChanged(SenderColumn);
79 void QmlChatLine::setContentsWidth(qreal w) {
80 if(w != _contentsWidth) {
82 emit contentsWidthChanged(w);
83 emit columnWidthChanged(ContentsColumn);
87 void QmlChatLine::setColumnSpacing(qreal s) {
88 if(s != _columnSpacing) {
90 emit columnSpacingChanged(s);
94 QPointF QmlChatLine::columnPos(ColumnType colType) const {
99 return QPointF(timestampWidth(), 0);
101 return QPointF(timestampWidth() + senderWidth(), 0);
107 qreal QmlChatLine::columnWidth(ColumnType colType) const {
109 case TimestampColumn:
110 return timestampWidth();
112 return senderWidth();
114 return contentsWidth();
120 QRectF QmlChatLine::columnBoundingRect(ColumnType colType) const {
123 case TimestampColumn:
124 return QRectF(columnPos(TimestampColumn), QSizeF(timestampWidth() - columnSpacing(), implicitHeight()));
126 return QRectF(columnPos(SenderColumn), QSizeF(senderWidth() - columnSpacing(), implicitHeight()));
128 return QRectF(columnPos(ContentsColumn), QSizeF(contentsWidth(), implicitHeight()));
134 void QmlChatLine::setRenderData(const RenderData &data) {
144 QmlChatLine::ColumnLayout *QmlChatLine::layout() const {
146 _layout = new ColumnLayout(this);
151 void QmlChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *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);
160 void QmlChatLine::onColumnWidthChanged(ColumnType colType) {
162 //qDebug() << "changed width" << _timestampWidth << _senderWidth << _contentsWidth;
163 //setImplicitHeight(implicitHeight() + 5);
165 if(colType == ContentsColumn) {
167 setImplicitHeight(layout()->height());
173 /**************************************************************************************/
175 QmlChatLine::ColumnLayout::ColumnLayout(const QmlChatLine *parent)
181 qreal QmlChatLine::ColumnLayout::height() const {
182 return chatLine()->contentsWidth()/20;
185 void QmlChatLine::ColumnLayout::prepare() {
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);