ChatItem::ChatItem(ChatLineModel::ColumnType col, QAbstractItemModel *model, QGraphicsItem *parent)
: QGraphicsItem(parent),
_fontMetrics(0),
- _layoutData(0),
_selectionMode(NoSelection),
- _selectionStart(-1)
+ _selectionStart(-1),
+ _layout(0)
{
Q_ASSERT(model);
QModelIndex index = model->index(row(), col);
}
ChatItem::~ChatItem() {
- delete _layoutData;
+ delete _layout;
}
QVariant ChatItem::data(int role) const {
return layout;
}
-void ChatItem::setLayout(QTextLayout *layout) {
- if(!_layoutData)
- _layoutData = new LayoutData;
- _layoutData->layout = layout;
-}
-
void ChatItem::updateLayout() {
if(!haveLayout())
setLayout(createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft));
layout()->endLayout();
}
-void ChatItem::clearLayoutData() {
- delete _layoutData;
- _layoutData = 0;
+void ChatItem::clearLayout() {
+ delete _layout;
+ _layout = 0;
}
// NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
return resultList;
}
-
void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if(event->buttons() == Qt::LeftButton) {
if(_selectionMode == NoSelection) {
}
}
-void ChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
- // FIXME dirty and fast hack to make http:// urls klickable
-
- QRegExp regex("\\b([hf]t{1,2}ps?://[^\\s]+)\\b");
- QString str = data(ChatLineModel::DisplayRole).toString();
- int idx = posToCursor(event->pos());
- int mi = 0;
- do {
- mi = regex.indexIn(str, mi);
- if(mi < 0) break;
- if(idx >= mi && idx < mi + regex.matchedLength()) {
- QDesktopServices::openUrl(QUrl(regex.capturedTexts()[1]));
- break;
- }
- mi += regex.matchedLength();
- } while(mi >= 0);
- event->accept();
-}
-
-void ChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
- //qDebug() << (void*)this << "entering";
- event->ignore();
-}
-
-void ChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
- //qDebug() << (void*)this << "leaving";
- event->ignore();
-}
-
-void ChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
- //qDebug() << (void*)this << event->pos();
- event->ignore();
-}
-
/*************************************************************************************************/
/*************************************************************************************************/
/*************************************************************************************************/
+ContentsChatItem::ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent),
+ _layoutData(0)
+{
+
+}
+
+ContentsChatItem::~ContentsChatItem() {
+ delete _layoutData;
+}
+
qreal ContentsChatItem::computeHeight() {
int lines = 1;
WrapColumnFinder finder(this);
return lines * fontMetrics()->lineSpacing();
}
+void ContentsChatItem::setLayout(QTextLayout *layout) {
+ if(!_layoutData)
+ _layoutData = new LayoutData;
+ _layoutData->layout = layout;
+}
+
+void ContentsChatItem::clearLayout() {
+ delete _layoutData;
+ _layoutData = 0;
+}
+
void ContentsChatItem::updateLayout() {
if(!haveLayout()) setLayout(createLayout(QTextOption::WrapAnywhere));
layout()->endLayout();
}
+void ContentsChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
+ // FIXME dirty and fast hack to make http:// urls klickable
+
+ QRegExp regex("\\b([hf]t{1,2}ps?://[^\\s]+)\\b");
+ QString str = data(ChatLineModel::DisplayRole).toString();
+ int idx = posToCursor(event->pos());
+ int mi = 0;
+ do {
+ mi = regex.indexIn(str, mi);
+ if(mi < 0) break;
+ if(idx >= mi && idx < mi + regex.matchedLength()) {
+ QDesktopServices::openUrl(QUrl(regex.capturedTexts()[1]));
+ break;
+ }
+ mi += regex.matchedLength();
+ } while(mi >= 0);
+ event->accept();
+}
+
+void ContentsChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
+ //qDebug() << (void*)this << "entering";
+ event->ignore();
+}
+
+void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
+ //qDebug() << (void*)this << "leaving";
+ event->ignore();
+}
+
+void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
+ //qDebug() << (void*)this << event->pos();
+ event->ignore();
+}
/*************************************************************************************************/
inline qreal width() const { return _boundingRect.width(); }
inline qreal height() const { return _boundingRect.height(); }
- inline bool haveLayout() const { return _layoutData != 0 && layout() != 0; }
- void clearLayoutData();
+ virtual inline bool haveLayout() const { return layout() != 0; }
+ virtual void clearLayout();
virtual QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft);
virtual void updateLayout();
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
- virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
-
- virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
- virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
- virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
- struct LayoutData;
- inline QTextLayout *layout() const;
- void setLayout(QTextLayout *);
+ virtual inline QTextLayout *layout() const { return _layout; }
+ virtual inline void setLayout(QTextLayout *l) { _layout = l; }
qint16 posToCursor(const QPointF &pos);
virtual qreal computeHeight();
SelectionMode _selectionMode;
qint16 _selectionStart, _selectionEnd;
- LayoutData *_layoutData;
+ QTextLayout *_layout;
};
-struct ChatItem::LayoutData {
- QTextLayout *layout;
-
- LayoutData() { layout = 0; }
- ~LayoutData() { delete layout; }
-};
+/*************************************************************************************************/
+//! A ChatItem for the timestamp column
class TimestampChatItem : public ChatItem {
public:
};
+/*************************************************************************************************/
+
+//! A ChatItem for the sender column
class SenderChatItem : public ChatItem {
public:
SenderChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
- void updateLayout();
+ virtual void updateLayout();
};
+/*************************************************************************************************/
+
+//! A ChatItem for the contents column
class ContentsChatItem : public ChatItem {
public:
- ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
+ ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent);
+ virtual ~ContentsChatItem();
+
inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
- void updateLayout();
+ virtual void clearLayout();
+ virtual void updateLayout();
+ virtual inline bool haveLayout() const { return _layoutData != 0 && layout() != 0; }
+
+protected:
+ virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
+ virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
+ virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
+ virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
+
+ virtual inline QTextLayout *layout() const;
+ virtual void setLayout(QTextLayout *l);
private:
+ struct LayoutData;
+ class WrapColumnFinder;
+
qreal computeHeight();
- class WrapColumnFinder;
+ LayoutData *_layoutData;
};
+struct ContentsChatItem::LayoutData {
+ QTextLayout *layout;
+ LayoutData() { layout = 0; }
+ ~LayoutData() { delete layout; }
+};
class ContentsChatItem::WrapColumnFinder {
public:
qreal w;
};
+/*************************************************************************************************/
+
+// Avoid circular include deps
#include "chatline.h"
-inline int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
-inline QTextLayout *ChatItem::layout() const { return _layoutData->layout; }
+int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
+QTextLayout *ContentsChatItem::layout() const { return _layoutData->layout; }
#endif
}
void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+ Q_UNUSED(option);
+ Q_UNUSED(widget);
if(_selection & Highlighted) {
painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
}
void ChatViewSearchController::updateHighlights(bool reuse) {
if(!_scene)
return;
-
+
QAbstractItemModel *model = _scene->model();
Q_ASSERT(model);
QList<ChatItem *> checkItems;
if(_searchSenders)
checkItems << &(line->item(MessageModel::SenderColumn));
-
+
if(_searchMsgs)
checkItems << &(line->item(MessageModel::ContentsColumn));
-
+
foreach(ChatItem *item, checkItems) {
foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
_highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
updateHighlights(searchOnlyRegularMsgs);
}
-
-
SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
: QGraphicsItem(parent),
_boundingRect(QRectF(-wordRect.width() / 2, -wordRect.height() / 2, wordRect.width(), wordRect.height()))
}
void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+ Q_UNUSED(option);
Q_UNUSED(widget);
-
+
painter->setPen(QPen(Qt::black, 1.5));
//painter->setBrush(QColor(127, 133, 250));
painter->setBrush(QColor(254, 237, 45));