#include "chatlinemodel.h"
#include "qtui.h"
-ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
- _fontMetrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
- _layout = 0;
- _lines = 0;
- _selectionStart = -1;
- _selectionMode = NoSelection;
+ChatItem::ChatItem(int col, QAbstractItemModel *model, QGraphicsItem *parent)
+ : QGraphicsItem(parent),
+ _fontMetrics(0),
+ _col(col),
+ _lines(0),
+ _layout(0),
+ _selectionMode(NoSelection),
+ _selectionStart(-1)
+{
+ Q_ASSERT(model);
+ QModelIndex index = model->index(row(), col);
+ _fontMetrics = QtUi::style()->fontMetrics(model->data(index, ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
setAcceptHoverEvents(true);
setZValue(20);
}
}
QVariant ChatItem::data(int role) const {
- if(!_index.isValid()) {
- qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
+ QModelIndex index = model()->index(row(), column());
+ if(!index.isValid()) {
+ qWarning() << "ChatItem::data(): model index is invalid!" << index;
return QVariant();
}
- return _index.data(role);
+ return model()->data(index, role);
}
qreal ChatItem::setWidth(qreal w) {
#include <QGraphicsItem>
#include <QObject>
-#include "chatline.h"
#include "chatlinemodel.h"
#include "chatscene.h"
#include "uistyle.h"
+#include "qtui.h"
class QTextLayout;
class ChatItem : public QGraphicsItem {
public:
- ChatItem(const QPersistentModelIndex &index, QGraphicsItem *parent);
+ ChatItem(int col, QAbstractItemModel *, 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; }
- inline ChatScene *chatScene() const { return qobject_cast<ChatScene *>(scene()); }
+ inline const QAbstractItemModel *model() const { return chatScene() ? chatScene()->model() : 0; }
+ int row() const;
+ inline int column() const { return _col; }
+ inline ChatScene *chatScene() const { return qobject_cast<ChatScene *>(scene()); }
inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
inline virtual QRectF boundingRect() const { return _boundingRect; }
QRectF _boundingRect;
QFontMetricsF *_fontMetrics;
+ int _col;
quint8 _lines;
- QPersistentModelIndex _index;
QTextLayout * _layout;
QList<quint16> _wrapPositions;
qreal w;
};
+#include "chatline.h"
+inline int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
+
+
#endif
#include "chatline.h"
#include "qtui.h"
-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);
-
- _timestampItem->setPos(0,0);
- _width = _height = 0;
- _selection = 0;
-
- if(_contentsItem->data(MessageModel::FlagsRole).toInt() & Message::Highlight) setHighlighted(true);
-}
-
-ChatLine::~ChatLine() {
- delete _timestampItem;
- delete _senderItem;
- delete _contentsItem;
+ChatLine::ChatLine(int row, QAbstractItemModel *model, QGraphicsItem *parent)
+ : QGraphicsItem(parent),
+ _row(row), // needs to be set before the items
+ _timestampItem(ChatLineModel::TimestampColumn, model, this),
+ _senderItem(ChatLineModel::SenderColumn, model, this),
+ _contentsItem(ChatLineModel::ContentsColumn, model, this),
+ _width(0),
+ _height(0),
+ _selection(0)
+{
+ Q_ASSERT(model);
+ QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
+ setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
}
QRectF ChatLine::boundingRect () const {
return QRectF(0, 0, _width, _height);
}
-ChatItem *ChatLine::item(ChatLineModel::ColumnType column) const {
+ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
switch(column) {
- case ChatLineModel::TimestampColumn: return _timestampItem;
- case ChatLineModel::SenderColumn: return _senderItem;
- case ChatLineModel::ContentsColumn: return _contentsItem;
- default: return 0;
+ case ChatLineModel::TimestampColumn:
+ return _timestampItem;
+ case ChatLineModel::SenderColumn:
+ return _senderItem;
+ case ChatLineModel::ContentsColumn:
+ return _contentsItem;
+ default:
+ return *(ChatItem *)0; // provoke an error
}
}
qreal ChatLine::setGeometry(qreal width, qreal firstHandlePos, qreal secondHandlePos) {
- if(width != _width) prepareGeometryChange();
+ if(width != _width)
+ prepareGeometryChange();
qreal firstsep = QtUi::style()->firstColumnSeparator()/2;
qreal secondsep = QtUi::style()->secondColumnSeparator()/2;
- _timestampItem->setWidth(firstHandlePos - firstsep);
- _senderItem->setWidth(secondHandlePos - firstHandlePos - (firstsep+secondsep));
- _height = _contentsItem->setWidth(width - secondHandlePos - secondsep);
+ _timestampItem.setWidth(firstHandlePos - firstsep);
+ _senderItem.setWidth(secondHandlePos - firstHandlePos - (firstsep+secondsep));
+ _height = _contentsItem.setWidth(width - secondHandlePos - secondsep);
- _senderItem->setPos(firstHandlePos + firstsep, 0);
- _contentsItem->setPos(secondHandlePos + secondsep, 0);
+ _senderItem.setPos(firstHandlePos + firstsep, 0);
+ _contentsItem.setPos(secondHandlePos + secondsep, 0);
_width = width;
return _height;
quint8 sel = (_selection & 0x80) | 0x40 | minColumn;
if(sel != _selection) {
_selection = sel;
- for(int i = 0; i < minColumn; i++) item((ChatLineModel::ColumnType)i)->clearSelection();
- for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++) item((ChatLineModel::ColumnType)i)->setFullSelection();
+ for(int i = 0; i < minColumn; i++)
+ item((ChatLineModel::ColumnType)i).clearSelection();
+ for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
+ item((ChatLineModel::ColumnType)i).setFullSelection();
update();
}
} else {
quint8 sel = _selection & 0x80;
if(sel != _selection) {
_selection = sel;
- for(int i = 0; i <= ChatLineModel::ContentsColumn; i++) item((ChatLineModel::ColumnType)i)->clearSelection();
+ for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
+ item((ChatLineModel::ColumnType)i).clearSelection();
update();
}
}
painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
}
if(_selection & Selected) {
- qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f))->x();
+ qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f)).x();
QRectF selectRect(left, 0, width() - left, height());
painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
}
#include <QGraphicsItem>
#include "chatlinemodel.h"
-
-class ChatItem;
+#include "chatitem.h"
class ChatLine : public QGraphicsItem {
public:
- ChatLine(const QModelIndex &tempIndex, QGraphicsItem *parent = 0);
- virtual ~ChatLine();
+ ChatLine(int row, QAbstractItemModel *model, QGraphicsItem *parent = 0);
virtual QRectF boundingRect () const;
+
+ inline int row() { return _row; }
+ inline void setRow(int row) { _row = row; }
inline qreal width() const { return _width; }
inline qreal height() const { return _height; }
- ChatItem *item(ChatLineModel::ColumnType) const;
+ ChatItem &item(ChatLineModel::ColumnType);
virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
protected:
private:
- ChatItem *_timestampItem, *_senderItem, *_contentsItem;
+ int _row;
+ ChatItem _timestampItem, _senderItem, _contentsItem;
qreal _width, _height;
enum { Selected = 0x40, Highlighted = 0x80 };
#include "buffer.h"
#include "chatitem.h"
+#include "chatline.h"
#include "chatlinemodelitem.h"
#include "chatscene.h"
#include "columnhandleitem.h"
connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
for(int i = 0; i < model->rowCount(); i++) {
- ChatLine *line = new ChatLine(model->index(i, 0));
+ ChatLine *line = new ChatLine(i, model);
_lines.append(line);
addItem(line);
}
qreal y = 0;
if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
for(int i = start; i <= end; i++) {
- ChatLine *line = new ChatLine(model()->index(i, 0));
+ ChatLine *line = new ChatLine(i, model());
_lines.insert(i, line);
addItem(line);
if(_width > 0) {
h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
}
}
+ // update existing items
+ for(int i = end+1; i < _lines.count(); i++) {
+ _lines[i]->setRow(i);
+ }
+
if(h > 0) {
_height += h;
for(int i = end+1; i < _lines.count(); i++) {
}
void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
- _selectionStart = _selectionEnd = item->index().row();
- _selectionStartCol = _selectionMinCol = item->index().column();
+ _selectionStart = _selectionEnd = item->row();
+ _selectionStartCol = _selectionMinCol = item->column();
_isSelecting = true;
_lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
updateSelection(item->mapToScene(itemPos));
ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
if(!contentItem) return;
- int curRow = contentItem->index().row();
+ int curRow = contentItem->row();
int curColumn;
if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
QString result;
for(int l = start; l <= end; l++) {
if(_selectionMinCol == ChatLineModel::TimestampColumn)
- result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
+ result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
if(_selectionMinCol <= ChatLineModel::SenderColumn)
- result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
- result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";
+ result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
+ result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
}
return result;
}