From 0789d47e81940cbbdf925466442380063a50ac05 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Tue, 19 Aug 2008 01:16:06 +0200 Subject: [PATCH] Cleanups, tweaks and fixes --- src/qtui/chatitem.cpp | 107 +++++++++++++++----------- src/qtui/chatitem.h | 66 ++++++++++------ src/qtui/chatline.cpp | 2 + src/qtui/chatviewsearchcontroller.cpp | 11 ++- 4 files changed, 110 insertions(+), 76 deletions(-) diff --git a/src/qtui/chatitem.cpp b/src/qtui/chatitem.cpp index 6a9b1436..602a2c9e 100644 --- a/src/qtui/chatitem.cpp +++ b/src/qtui/chatitem.cpp @@ -34,9 +34,9 @@ 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); @@ -46,7 +46,7 @@ ChatItem::ChatItem(ChatLineModel::ColumnType col, QAbstractItemModel *model, QGr } ChatItem::~ChatItem() { - delete _layoutData; + delete _layout; } QVariant ChatItem::data(int role) const { @@ -86,12 +86,6 @@ QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignmen 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)); @@ -105,9 +99,9 @@ void ChatItem::updateLayout() { 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 @@ -199,7 +193,6 @@ QList ChatItem::findWords(const QString &searchWord, Qt::CaseSensitivity return resultList; } - void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if(event->buttons() == Qt::LeftButton) { if(_selectionMode == NoSelection) { @@ -249,40 +242,6 @@ void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { } } -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(); -} - /*************************************************************************************************/ /*************************************************************************************************/ @@ -294,6 +253,16 @@ void SenderChatItem::updateLayout() { /*************************************************************************************************/ +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); @@ -301,6 +270,17 @@ qreal ContentsChatItem::computeHeight() { 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)); @@ -324,6 +304,39 @@ void ContentsChatItem::updateLayout() { 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(); +} /*************************************************************************************************/ diff --git a/src/qtui/chatitem.h b/src/qtui/chatitem.h index 745f2d32..8e15c9f9 100644 --- a/src/qtui/chatitem.h +++ b/src/qtui/chatitem.h @@ -48,8 +48,8 @@ public: 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); @@ -70,15 +70,9 @@ protected: 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(); @@ -95,16 +89,12 @@ private: 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: @@ -113,30 +103,57 @@ 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: @@ -156,8 +173,11 @@ private: qreal w; }; +/*************************************************************************************************/ + +// Avoid circular include deps #include "chatline.h" -inline int ChatItem::row() const { return static_cast(parentItem())->row(); } -inline QTextLayout *ChatItem::layout() const { return _layoutData->layout; } +int ChatItem::row() const { return static_cast(parentItem())->row(); } +QTextLayout *ContentsChatItem::layout() const { return _layoutData->layout; } #endif diff --git a/src/qtui/chatline.cpp b/src/qtui/chatline.cpp index 94820d63..a28f4e84 100644 --- a/src/qtui/chatline.cpp +++ b/src/qtui/chatline.cpp @@ -111,6 +111,8 @@ void ChatLine::setHighlighted(bool highlighted) { } 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())); } diff --git a/src/qtui/chatviewsearchcontroller.cpp b/src/qtui/chatviewsearchcontroller.cpp index 44ddc694..591c20e3 100644 --- a/src/qtui/chatviewsearchcontroller.cpp +++ b/src/qtui/chatviewsearchcontroller.cpp @@ -76,7 +76,7 @@ void ChatViewSearchController::setSearchString(const QString &searchString) { void ChatViewSearchController::updateHighlights(bool reuse) { if(!_scene) return; - + QAbstractItemModel *model = _scene->model(); Q_ASSERT(model); @@ -130,10 +130,10 @@ void ChatViewSearchController::highlightLine(ChatLine *line) { QList 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); @@ -192,8 +192,6 @@ void ChatViewSearchController::setSearchOnlyRegularMsgs(bool searchOnlyRegularMs updateHighlights(searchOnlyRegularMsgs); } - - SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent) : QGraphicsItem(parent), _boundingRect(QRectF(-wordRect.width() / 2, -wordRect.height() / 2, wordRect.width(), wordRect.height())) @@ -203,8 +201,9 @@ SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent) } 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)); -- 2.20.1