0dd090d5ac79d43b05cd6abdddda9c174afc532a
[quassel.git] / src / qmlui / qmlchatline.h
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
21 #ifndef QMLCHATLINE_H_
22 #define QMLCHATLINE_H_
23
24 #include <QDeclarativeItem>
25
26 #include "uistyle.h"
27
28 class QAbstractItemModel;
29
30 #include <QAbstractItemModel>
31
32 class QmlChatLine : public QDeclarativeItem {
33   Q_OBJECT
34
35   Q_PROPERTY(QObject *model READ modelPointer WRITE setModelPointer)
36   Q_PROPERTY(QmlChatLine::RenderData renderData READ renderData WRITE setRenderData)
37   Q_PROPERTY(qreal timestampWidth READ timestampWidth WRITE setTimestampWidth NOTIFY timestampWidthChanged)
38   Q_PROPERTY(qreal senderWidth READ senderWidth WRITE setSenderWidth NOTIFY senderWidthChanged)
39   Q_PROPERTY(qreal contentsWidth READ contentsWidth WRITE setContentsWidth NOTIFY contentsWidthChanged)
40   Q_PROPERTY(qreal columnSpacing READ columnSpacing WRITE setColumnSpacing NOTIFY columnSpacingChanged)
41   Q_PROPERTY(QVariant test READ test WRITE setTest)
42
43 public:
44   enum ColumnType {
45     TimestampColumn,
46     SenderColumn,
47     ContentsColumn,
48     NumColumns
49   };
50
51   //! Contains all model data needed to render a QmlChatLine
52   struct RenderData {
53     struct Column {
54       QString text;
55       UiStyle::FormatList formats;
56       QBrush background;
57       QBrush selectedBackground;
58     };
59
60     qint32 messageLabel;
61     bool isValid;
62
63     Column &operator[](ColumnType col) {
64       return _data[col];
65     }
66
67     Column const &operator[](ColumnType col) const {
68       return _data[col];
69     }
70
71     RenderData() { messageLabel = 0; isValid = false; }
72
73   private:
74     Column _data[NumColumns];
75   };
76
77   class ColumnLayout;
78
79   QmlChatLine(QDeclarativeItem *parent = 0);
80   virtual ~QmlChatLine();
81
82   inline QAbstractItemModel *model() const { return _model; }
83   inline QObject *modelPointer() const { return _model; }
84   void setModelPointer(QObject *model) { _model = qobject_cast<QAbstractItemModel *>(model); }
85
86   inline RenderData renderData() const { return _data; }
87   void setRenderData(const RenderData &data);
88
89   ColumnLayout *layout() const;
90
91   inline qreal timestampWidth() const { return _timestampWidth; }
92   void setTimestampWidth(qreal w);
93   inline qreal senderWidth() const { return _senderWidth; }
94   void setSenderWidth(qreal w);
95   inline qreal contentsWidth() const { return _contentsWidth; }
96   void setContentsWidth(qreal w);
97   inline qreal columnSpacing() const { return _columnSpacing; }
98   void setColumnSpacing(qreal s);
99
100   inline QString text() const { return renderData()[ContentsColumn].text; }
101
102   void setTest(const QVariant &test) { _test = test; qDebug() << "set test" << test; }
103   QVariant test() const { return _test; }
104
105   QPointF columnPos(ColumnType colType) const;
106   qreal columnWidth(ColumnType colType) const;
107   QRectF columnBoundingRect(ColumnType colType) const;
108
109   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
110
111   static void registerTypes();
112
113 signals:
114   void timestampWidthChanged(qreal);
115   void senderWidthChanged(qreal);
116   void contentsWidthChanged(qreal);
117   void columnWidthChanged(ColumnType column);
118   void columnSpacingChanged(qreal);
119
120 public slots:
121   void onClicked(qreal mouseX, qreal mouseY) { qDebug() << "clicked" << mouseX << mouseY; }
122   void onPressed(qreal mouseX, qreal mouseY) { qDebug() << "pressed" << mouseX << mouseY; }
123   void onMousePositionChanged(qreal mouseX, qreal mouseY) { qDebug() << "moved" << mouseX << mouseY; }
124
125 protected:
126
127 protected slots:
128   void onColumnWidthChanged(ColumnType column);
129
130 private:
131   QAbstractItemModel *_model;
132   RenderData _data;
133
134   qreal _timestampWidth, _senderWidth, _contentsWidth;
135   qreal _columnSpacing;
136
137   QVariant _test;
138
139   mutable ColumnLayout *_layout;
140 };
141
142 QDataStream &operator<<(QDataStream &out, const QmlChatLine::RenderData &data);
143 QDataStream &operator>>(QDataStream &in, QmlChatLine::RenderData &data);
144
145 Q_DECLARE_METATYPE(QmlChatLine::RenderData)
146
147 class QmlChatLine::ColumnLayout {
148 public:
149   explicit ColumnLayout(const QmlChatLine *parent);
150   virtual ~ColumnLayout() {}
151
152   inline const QmlChatLine *chatLine() const { return _parent; }
153
154   qreal height() const;
155   virtual void prepare();
156   virtual void draw(QPainter *p);
157
158 private:
159   const QmlChatLine *_parent;
160 };
161
162 #endif