X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fchatitem.h;h=a945795ec2d3fabffac90627c0667be6bf4c163e;hp=5877040cf33b2b914ba1545d6645f65f58d066d5;hb=3a3e844f9fcfd12235a0086af75ecd503b621ef4;hpb=e88a5b5f44c84f3892c120b98e6a376032a32644 diff --git a/src/qtui/chatitem.h b/src/qtui/chatitem.h index 5877040c..a945795e 100644 --- a/src/qtui/chatitem.h +++ b/src/qtui/chatitem.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,234 +15,297 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #ifndef CHATITEM_H_ #define CHATITEM_H_ -#include +#include #include #include "chatlinemodel.h" #include "chatscene.h" +#include "clickable.h" #include "uistyle.h" #include "qtui.h" -class QTextLayout; -struct ChatItemPrivate; +#include +#include -class ChatItem : public QGraphicsItem { -protected: - ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent); - virtual ~ChatItem(); - -public: - inline const QAbstractItemModel *model() const; - inline int row() const; - virtual ChatLineModel::ColumnType column() const = 0; - inline ChatScene *chatScene() const { return qobject_cast(scene()); } - - inline QRectF boundingRect() const { return _boundingRect; } - inline qreal width() const { return _boundingRect.width(); } - inline qreal height() const { return _boundingRect.height(); } +class ChatLine; +class ChatView; - QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const; - virtual void doLayout(); - void clearLayout(); +/* All external positions are relative to the parent ChatLine */ +/* Yes, that's also true for the boundingRect() and related things */ - virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); - - QVariant data(int role) const; - - // selection stuff, to be called by the scene - void clearSelection(); - void setFullSelection(); - void continueSelecting(const QPointF &pos); +class ChatItem +{ +protected: + // boundingRect is relative to the parent ChatLine + ChatItem(const QRectF &boundingRect, ChatLine *parent); + virtual ~ChatItem(); - QList findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive); +public: + const QAbstractItemModel *model() const; + ChatLine *chatLine() const; + ChatScene *chatScene() const; + ChatView *chatView() const; + int row() const; + virtual ChatLineModel::ColumnType column() const = 0; + + // The boundingRect() is relative to the parent ChatLine + inline QRectF boundingRect() const; + inline qreal width() const; + inline qreal height() const; + inline QPointF pos() const; + inline qreal x() const; + inline qreal y() const; + + QPointF mapToLine(const QPointF &) const; + QPointF mapFromLine(const QPointF &) const; + QPointF mapToScene(const QPointF &) const; + QPointF mapFromScene(const QPointF &) const; + + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr); + virtual inline int type() const { return ChatScene::ChatItemType; } + + QVariant data(int role) const; + + // selection stuff, to be called by the scene + QString selection() const; + void clearSelection(); + void setFullSelection(); + void continueSelecting(const QPointF &pos); + bool hasSelection() const; + bool isPosOverSelection(const QPointF &pos) const; + + QList findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive); + + virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos); + virtual void handleClick(const QPointF &pos, ChatScene::ClickMode); + + void initLayoutHelper(QTextLayout *layout, QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const; + + //! Remove internally cached data + /** This removes e.g. the cached QTextLayout to avoid wasting space for nonvisible ChatLines + */ + virtual void clearCache(); protected: - virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); - virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - - inline QTextLayout *layout() const; - - virtual inline QVector additionalFormats() const { return QVector(); } - qint16 posToCursor(const QPointF &pos); - - inline bool hasPrivateData() const { return (bool)_data; } - ChatItemPrivate *privateData() const; - virtual inline ChatItemPrivate *newPrivateData(); - - // WARNING: setGeometry and setHeight should not be used without either: - // a) calling prepareGeometryChange() immediately before setColumns() - // b) calling Chatline::setPos() immediately afterwards - inline void setGeometry(qreal width, qreal height) { - prepareGeometryChange(); - _boundingRect.setWidth(width); - _boundingRect.setHeight(height); - } - inline void setHeight(const qreal &height) { - prepareGeometryChange(); - _boundingRect.setHeight(height); - } - inline void setWidth(const qreal &width) { - prepareGeometryChange(); - _boundingRect.setWidth(width); - } + enum SelectionMode { + NoSelection, + PartialSelection, + FullSelection + }; + + virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *) {} + virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *) {} + virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *) {} + + QTextLayout *layout() const; + + virtual void initLayout(QTextLayout *layout) const; + virtual void doLayout(QTextLayout *) const; + virtual UiStyle::FormatList formatList() const; + + void paintBackground(QPainter *); + virtual QVector additionalFormats() const; + void overlayFormat(UiStyle::FormatList &fmtList, quint16 start, quint16 end, UiStyle::FormatType overlayFmt) const; + + inline qint16 selectionStart() const { return _selectionStart; } + inline void setSelectionStart(qint16 start) { _selectionStart = start; } + inline qint16 selectionEnd() const { return _selectionEnd; } + inline void setSelectionEnd(qint16 end) { _selectionEnd = end; } + inline SelectionMode selectionMode() const { return _selectionMode; } + inline void setSelectionMode(SelectionMode mode) { _selectionMode = mode; } + void setSelection(SelectionMode mode, qint16 selectionStart, qint16 selectionEnd); + + virtual bool hasActiveClickable() const; + virtual std::pair activeClickableRange() const; + + qint16 posToCursor(const QPointF &pos) const; + + inline void setGeometry(qreal width, qreal height) { clearCache(); _boundingRect.setSize(QSizeF(width, height)); } + inline void setHeight(const qreal &height) { clearCache(); _boundingRect.setHeight(height); } + inline void setWidth(const qreal &width) { clearCache(); _boundingRect.setWidth(width); } + inline void setPos(const QPointF &pos) { _boundingRect.moveTopLeft(pos); } private: - // internal selection stuff - void setSelection(int start, int length); + ChatLine *_parent; + QRectF _boundingRect; - ChatItemPrivate *_data; - QRectF _boundingRect; + SelectionMode _selectionMode; + qint16 _selectionStart, _selectionEnd; - enum SelectionMode { NoSelection, PartialSelection, FullSelection }; - SelectionMode _selectionMode; - qint16 _selectionStart, _selectionEnd; + mutable QTextLayout *_cachedLayout; - friend class ChatLine; -}; + // internal selection stuff + void setSelection(int start, int length); -struct ChatItemPrivate { - QTextLayout *layout; - ChatItemPrivate(QTextLayout *l) : layout(l) {} - ~ChatItemPrivate() { - delete layout; - } + friend class ChatLine; }; -// inlines of ChatItem -QTextLayout *ChatItem::layout() const { return privateData()->layout; } -ChatItemPrivate *ChatItem::newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere)); } // ************************************************************ // TimestampChatItem // ************************************************************ //! A ChatItem for the timestamp column -class TimestampChatItem : public ChatItem { +class TimestampChatItem : public ChatItem +{ public: - TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {} - virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; } + TimestampChatItem(const QRectF &boundingRect, ChatLine *parent) : ChatItem(boundingRect, parent) {} + inline int type() const override { return ChatScene::TimestampChatItemType; } + inline ChatLineModel::ColumnType column() const override { return ChatLineModel::TimestampColumn; } }; + // ************************************************************ // SenderChatItem // ************************************************************ //! A ChatItem for the sender column -class SenderChatItem : public ChatItem { +class SenderChatItem : public ChatItem +{ public: - SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {} - virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; } + SenderChatItem(const QRectF &boundingRect, ChatLine *parent) : ChatItem(boundingRect, parent) {} + inline ChatLineModel::ColumnType column() const override { return ChatLineModel::SenderColumn; } + void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) override; protected: - virtual inline ChatItemPrivate *newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere, Qt::AlignRight)); } + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override; + inline int type() const override { return ChatScene::SenderChatItemType; } + void initLayout(QTextLayout *layout) const override; }; + // ************************************************************ // ContentsChatItem // ************************************************************ struct ContentsChatItemPrivate; //! A ChatItem for the contents column -class ContentsChatItem : public ChatItem { +class ContentsChatItem : public ChatItem +{ + Q_DECLARE_TR_FUNCTIONS(ContentsChatItem) + public: - ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent); + ContentsChatItem(const QPointF &pos, const qreal &width, ChatLine *parent); + ~ContentsChatItem() override; + + inline int type() const override { return ChatScene::ContentsChatItemType; } - inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; } - inline QFontMetricsF *fontMetrics() const { return _fontMetrics; } + inline ChatLineModel::ColumnType column() const override { return ChatLineModel::ContentsColumn; } + QFontMetricsF *fontMetrics() const; + + void clearCache() override; protected: - virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); - virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); - virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; + void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override; + void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override; + void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) override; + + bool hasActiveClickable() const override; + std::pair activeClickableRange() const override; - virtual QVector additionalFormats() const; + void addActionsToMenu(QMenu *menu, const QPointF &itemPos) override; + virtual void copyLinkToClipboard(); - virtual void doLayout(); - virtual inline ChatItemPrivate *newPrivateData(); + void initLayout(QTextLayout *layout) const override; + void doLayout(QTextLayout *layout) const override; + UiStyle::FormatList formatList() const override; private: - struct Clickable; - class WrapColumnFinder; + class ActionProxy; + class WrapColumnFinder; + + mutable ContentsChatItemPrivate *_data; + ContentsChatItemPrivate *privateData() const; - inline ContentsChatItemPrivate *privateData() const; + Clickable clickableAt(const QPointF &pos) const; - QList findClickables() const; - void endHoverMode(); - void showWebPreview(const Clickable &click); - void clearWebPreview(); + void endHoverMode(); + void showWebPreview(const Clickable &click); + void clearWebPreview(); + qreal setGeometryByWidth(qreal w); - // WARNING: setGeometry and setHeight should not be used without either: - // a) calling prepareGeometryChange() immediately before setColumns() - // b) calling Chatline::setPos() immediately afterwards - qreal setGeometryByWidth(qreal w); - friend class ChatLine; - friend struct ContentsChatItemPrivate; + QFontMetricsF *_fontMetrics; - QFontMetricsF *_fontMetrics; + // we need a receiver for Action signals + static ActionProxy _actionProxy; + + friend class ChatLine; + friend struct ContentsChatItemPrivate; }; -struct ContentsChatItem::Clickable { - // Don't change these enums without also changing the regexps in analyze()! - enum Type { - Invalid = -1, - Url = 0, - Channel = 1, - Nick = 2 - }; - - Type type; - quint16 start; - quint16 length; - - inline Clickable() : type(Invalid) {}; - inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {}; - inline bool isValid() const { return type != Invalid; } + +struct ContentsChatItemPrivate { + ContentsChatItem *contentsItem; + ClickableList clickables; + Clickable currentClickable; + Clickable activeClickable; + + ContentsChatItemPrivate(ClickableList c, ContentsChatItem *parent) : contentsItem(parent), clickables(std::move(c)) {} }; -struct ContentsChatItemPrivate : ChatItemPrivate { - ContentsChatItem *contentsItem; - QList clickables; - ContentsChatItem::Clickable currentClickable; - bool hasDragged; +class ContentsChatItem::WrapColumnFinder +{ +public: + WrapColumnFinder(const ChatItem *parent); + ~WrapColumnFinder(); + + qint16 nextWrapColumn(qreal width); - ContentsChatItemPrivate(QTextLayout *l, const QList &c, ContentsChatItem *parent) : ChatItemPrivate(l), contentsItem(parent), clickables(c), hasDragged(false) {} +private: + const ChatItem *item; + QTextLayout layout; + QTextLine line; + ChatLineModel::WrapList wrapList; + qint16 wordidx; + qint16 lineCount; + qreal choppedTrailing; }; -//inlines regarding ContentsChatItemPrivate -ChatItemPrivate *ContentsChatItem::newPrivateData() { return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this); } -ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); } -class ContentsChatItem::WrapColumnFinder { -public: - WrapColumnFinder(ChatItem *parent); - ~WrapColumnFinder(); +//! Acts as a proxy for Action signals targetted at a ContentsChatItem +/** Since a ChatItem is not a QObject, hence cannot receive signals, we use a static ActionProxy + * as a receiver instead. This avoids having to handle ChatItem actions (e.g. context menu entries) + * outside the ChatItem. + */ +class ContentsChatItem::ActionProxy : public QObject +{ + Q_OBJECT - qint16 nextWrapColumn(); +public slots: + inline void copyLinkToClipboard() { item()->copyLinkToClipboard(); } private: - ChatItem *item; - QTextLayout *layout; - QTextLine line; - ChatLineModel::WrapList wrapList; - qint16 wordidx; - qint16 lineCount; - qreal choppedTrailing; + /// Returns the ContentsChatItem that should receive the action event. + /** For efficiency reasons, values are not checked for validity. You gotta make sure that you set the data() member + * in the Action correctly. + * @return The ChatItem from which the sending Action originated + */ + inline ContentsChatItem *item() const + { + return static_cast(qobject_cast(sender())->data().value()); + } }; + /*************************************************************************************************/ -// Avoid circular include deps -#include "chatline.h" -const QAbstractItemModel *ChatItem::model() const { return static_cast(parentItem())->model(); } -int ChatItem::row() const { return static_cast(parentItem())->row(); } +// Inlines + +QRectF ChatItem::boundingRect() const { return _boundingRect; } +qreal ChatItem::width() const { return _boundingRect.width(); } +qreal ChatItem::height() const { return _boundingRect.height(); } +QPointF ChatItem::pos() const { return _boundingRect.topLeft(); } +qreal ChatItem::x() const { return pos().x(); } +qreal ChatItem::y() const { return pos().y(); } #endif