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 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;
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;
48 QmlChatLine::QmlChatLine(QDeclarativeItem *parent)
49 : QDeclarativeItem(parent),
55 setFlag(ItemHasNoContents, false);
56 setImplicitHeight(20);
57 setImplicitWidth(1000);
58 connect(this, SIGNAL(columnWidthChanged(ColumnType)), SLOT(onColumnWidthChanged(ColumnType)));
61 QmlChatLine::~QmlChatLine() {
65 void QmlChatLine::setTimestampWidth(qreal w) {
66 if(w != _timestampWidth) {
68 emit timestampWidthChanged(w);
69 emit columnWidthChanged(TimestampColumn);
73 void QmlChatLine::setSenderWidth(qreal w) {
74 if(w != _senderWidth) {
76 emit senderWidthChanged(w);
77 emit columnWidthChanged(SenderColumn);
81 void QmlChatLine::setContentsWidth(qreal w) {
82 if(w != _contentsWidth) {
84 emit contentsWidthChanged(w);
85 emit columnWidthChanged(ContentsColumn);
89 void QmlChatLine::setColumnSpacing(qreal s) {
90 if(s != _columnSpacing) {
92 emit columnSpacingChanged(s);
96 QPointF QmlChatLine::columnPos(ColumnType colType) const {
101 return QPointF(timestampWidth(), 0);
103 return QPointF(timestampWidth() + senderWidth(), 0);
109 qreal QmlChatLine::columnWidth(ColumnType colType) const {
111 case TimestampColumn:
112 return timestampWidth();
114 return senderWidth();
116 return contentsWidth();
122 QRectF QmlChatLine::columnBoundingRect(ColumnType colType) const {
125 case TimestampColumn:
126 return QRectF(columnPos(TimestampColumn), QSizeF(timestampWidth() - columnSpacing(), implicitHeight()));
128 return QRectF(columnPos(SenderColumn), QSizeF(senderWidth() - columnSpacing(), implicitHeight()));
130 return QRectF(columnPos(ContentsColumn), QSizeF(contentsWidth(), implicitHeight()));
136 void QmlChatLine::setRenderData(const RenderData &data) {
146 QmlChatLine::ColumnLayout *QmlChatLine::layout() const {
148 _layout = new ColumnLayout(this);
153 void QmlChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *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);
162 void QmlChatLine::onColumnWidthChanged(ColumnType colType) {
164 //qDebug() << "changed width" << _timestampWidth << _senderWidth << _contentsWidth;
165 //setImplicitHeight(implicitHeight() + 5);
167 if(colType == ContentsColumn) {
169 setImplicitHeight(layout()->height());
175 /**************************************************************************************/
177 QmlChatLine::ColumnLayout::ColumnLayout(const QmlChatLine *parent)
183 qreal QmlChatLine::ColumnLayout::height() const {
184 return chatLine()->contentsWidth()/20;
187 void QmlChatLine::ColumnLayout::prepare() {
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);