which are each coupled to a QPersistentModelIndex. Thus, we won't need to store much more data
in the items themselves than that index. The actual data is contained in ChatlineModelItems.
}
QVariant MessageModelItem::data(int column, int role) const {
- if(column < MessageModel::TimestampColumn || column > MessageModel::TextColumn) return QVariant();
+ if(column < MessageModel::TimestampColumn || column > MessageModel::ContentsColumn) return QVariant();
switch(role) {
case MessageModel::MsgIdRole: return QVariant::fromValue<MsgId>(_msgId);
case MessageModel::BufferIdRole: return QVariant::fromValue<BufferId>(_bufferId);
};
enum ColumnType {
- TimestampColumn, SenderColumn, TextColumn, UserColumnType
+ TimestampColumn, SenderColumn, ContentsColumn, UserColumnType
};
MessageModel(QObject *parent);
//if(_wrapMode == WordWrap) {
// setFlags(QGraphicsItem::ItemClipsToShape, true);
//}
+
}
ChatItem::~ChatItem() {
#define _CHATITEM_H_
#include <QGraphicsItem>
-#include <QPersistentModelIndex>
#include <QTextLayout>
#include <QTextOption>
-#include "messagemodel.h"
+#include "chatline.h"
+#include "chatlinemodel.h"
#include "uistyle.h"
-class QGraphicsSceneMouseEvent;
-
class ChatItem : public QGraphicsItem {
public:
- ChatItem(const QPersistentModelIndex &index, QGraphicsItem *parent = 0);
+ ChatItem(const QPersistentModelIndex &index, QGraphicsItem *parent);
virtual ~ChatItem();
+ inline QPersistentModelIndex index() const { return _index; }
+ inline const MessageModel *model() const { return _index.isValid() ? qobject_cast<const MessageModel *>(_index.model()) : 0; }
+ inline int row() const { return _index.isValid() ? _index.row() : 0; }
+
virtual QRectF boundingRect() const;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
- inline QPersistentModelIndex persistentIndex() const { return _index; }
- inline const MessageModel *model() const { return _index.isValid() ? qobject_cast<const MessageModel *>(_index.model()) : 0; }
- inline int row() const { return _index.isValid() ? _index.row() : 0; }
virtual QVariant data(int role) const;
//QString text() const;
//void setText(const UiStyle::StyledText &text);
#include "chatline.h"
#include "qtui.h"
-ChatLine::ChatLine(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
+Chatline::Chatline(const QModelIndex &index, QGraphicsItem *parent) : QGraphicsItem(parent) {
+ _timestampItem = new ChatItem(QPersistentModelIndex(index.sibling(index.row(), ChatlineModel::TimestampColumn)), this);
+ _senderItem = new ChatItem(QPersistentModelIndex(index.sibling(index.row(), ChatlineModel::SenderColumn)), this);
+ _contentsItem = new ChatItem(QPersistentModelIndex(index.sibling(index.row(), ChatlineModel::ContentsColumn)), this);
+ _senderItem->setPos(80, 0);
+ _contentsItem->setPos(160, 0);
}
-ChatLine::~ChatLine() {
+Chatline::~Chatline() {
}
-QRectF ChatLine::boundingRect () const {
+QRectF Chatline::boundingRect () const {
return childrenBoundingRect();
}
-void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+void Chatline::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
}
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
-#ifndef _CHATLINE_H_
-#define _CHATLINE_H_
+#ifndef CHATLINE_H_
+#define CHATLINE_H_
#include <QGraphicsItem>
-#include "message.h"
+#include "messagemodel.h"
-class ChatLine : public QGraphicsItem {
+class ChatItem;
+
+class Chatline : public QGraphicsItem {
public:
- ChatLine(const QPersistentModelIndex &, QGraphicsItem *parent = 0);
- virtual ~ChatLine();
+ Chatline(const QModelIndex &tempIndex, QGraphicsItem *parent = 0);
+ virtual ~Chatline();
virtual QRectF boundingRect () const;
virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
//bool sceneEvent ( QEvent * event );
private:
- QPersistentModelIndex _index;
+ ChatItem *_timestampItem, *_senderItem, *_contentsItem;
};
#endif
switch(column) {
case ChatlineModel::TimestampColumn: part = &_timestamp; break;
case ChatlineModel::SenderColumn: part = &_sender; break;
- case ChatlineModel::TextColumn: part = &_contents; break;
+ case ChatlineModel::ContentsColumn: part = &_contents; break;
default: return MessageModelItem::data(column, role);
}
ChatScene::ChatScene(MessageModel *model, QObject *parent) : QGraphicsScene(parent), _model(model) {
connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
for(int i = 0; i < model->rowCount(); i++) {
- ChatItem *item = new ChatItem(QPersistentModelIndex(model->index(i, 2)));
- addItem(item);
- item->setPos(30, i*item->boundingRect().height());
+ Chatline *line = new Chatline(model->index(i, 0));
+ addItem(line);
+ line->setPos(30, i*line->boundingRect().height());
}