Use handleClick() rather than mouseReleaseEvent() for handling URL clicks
[quassel.git] / src / qtui / chatitem.h
index 587cc60..0710b6a 100644 (file)
@@ -34,7 +34,7 @@ struct ChatItemPrivate;
 
 class ChatItem : public QGraphicsItem {
 protected:
-  ChatItem(const qreal &width, const qreal &height, const QPointF &pos, ChatLineModel::ColumnType column, QGraphicsItem *parent);
+  ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent);
   virtual ~ChatItem();
 
 public:
@@ -43,28 +43,32 @@ public:
   virtual ChatLineModel::ColumnType column() const = 0;
   inline ChatScene *chatScene() const { return qobject_cast<ChatScene *>(scene()); }
 
-  inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
   inline QRectF boundingRect() const { return _boundingRect; }
   inline qreal width() const { return _boundingRect.width(); }
   inline qreal height() const { return _boundingRect.height(); }
 
-  inline bool hasLayout() const { return (bool)_data; }
-  QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft);
-  virtual inline QTextLayout *createLayout() { return createLayout(QTextOption::WrapAnywhere); }
+  QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
+  virtual void doLayout();
   void clearLayout();
 
-  virtual void updateLayout();
   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
+  enum { Type = ChatScene::ChatItemType };
+  virtual inline int type() const { return Type; }
 
   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<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
 
+  virtual void handleClick(const QPointF &pos, ChatScene::ClickMode);
+
 protected:
   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
@@ -72,11 +76,14 @@ protected:
 
   inline QTextLayout *layout() const;
 
+  virtual QTextLayout::FormatRange selectionFormat() const;
   virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
-  qint16 posToCursor(const QPointF &pos);
 
-  inline void ChatItem::setPrivateData(ChatItemPrivate *data) { Q_ASSERT(!_data); _data = data; }
-  inline ChatItemPrivate *ChatItem::privateData() const;
+  qint16 posToCursor(const QPointF &pos) const;
+
+  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()
@@ -85,8 +92,12 @@ protected:
     _boundingRect.setWidth(width);
     _boundingRect.setHeight(height);
   }
-  inline void setHeight(const qreal &height) { _boundingRect.setHeight(height); }
-  inline void setWidth(const qreal &width) { _boundingRect.setWidth(width); }
+  inline void setHeight(const qreal &height) {
+    _boundingRect.setHeight(height);
+  }
+  inline void setWidth(const qreal &width) {
+    _boundingRect.setWidth(width);
+  }
 
 private:
   // internal selection stuff
@@ -94,7 +105,6 @@ private:
 
   ChatItemPrivate *_data;
   QRectF _boundingRect;
-  QFontMetricsF *_fontMetrics;
 
   enum SelectionMode { NoSelection, PartialSelection, FullSelection };
   SelectionMode _selectionMode;
@@ -113,7 +123,7 @@ struct ChatItemPrivate {
 
 // inlines of ChatItem
 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
-ChatItemPrivate *ChatItem::privateData() const { return _data; }
+ChatItemPrivate *ChatItem::newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere)); }
 
 // ************************************************************
 // TimestampChatItem
@@ -122,7 +132,9 @@ ChatItemPrivate *ChatItem::privateData() const { return _data; }
 //! A ChatItem for the timestamp column
 class TimestampChatItem : public ChatItem {
 public:
-  TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), column(), parent) {}
+  TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
+  enum { Type = ChatScene::TimestampChatItemType };
+  virtual inline int type() const { return Type; }
   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
 };
 
@@ -132,9 +144,14 @@ public:
 //! A ChatItem for the sender column
 class SenderChatItem : public ChatItem {
 public:
-  SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, column(), parent) {}
+  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; }
-  virtual inline QTextLayout *createLayout() { return ChatItem::createLayout(QTextOption::WrapAnywhere, Qt::AlignRight); }
+
+protected:
+  virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
+  enum { Type = ChatScene::SenderChatItemType };
+  virtual inline int type() const { return Type; }
+  virtual inline ChatItemPrivate *newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::ManualWrap, Qt::AlignRight)); }
 };
 
 // ************************************************************
@@ -147,18 +164,24 @@ class ContentsChatItem : public ChatItem {
 public:
   ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
 
+  enum { Type = ChatScene::ContentsChatItemType };
+  virtual inline int type() const { return Type; }
+
   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
+  inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
 
 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);
+  virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
+
+  virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
 
   virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
 
-  virtual void updateLayout();
+  virtual void doLayout();
+  virtual inline ChatItemPrivate *newPrivateData();
 
 private:
   struct Clickable;
@@ -166,15 +189,16 @@ private:
 
   inline ContentsChatItemPrivate *privateData() const;
 
-  QList<Clickable> findClickables();
+  QList<Clickable> findClickables() const;
   void endHoverMode();
+  void showWebPreview(const Clickable &click);
+  void clearWebPreview();
 
-  // 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;
 };
 
 struct ContentsChatItem::Clickable {
@@ -196,13 +220,18 @@ struct ContentsChatItem::Clickable {
 };
 
 struct ContentsChatItemPrivate : ChatItemPrivate {
+  ContentsChatItem *contentsItem;
   QList<ContentsChatItem::Clickable> clickables;
   ContentsChatItem::Clickable currentClickable;
-  bool hasDragged;
 
-  ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c) : ChatItemPrivate(l), clickables(c), hasDragged(false) {}
+  ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c, ContentsChatItem *parent)
+  : ChatItemPrivate(l), contentsItem(parent), clickables(c) {}
 };
 
+//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);
@@ -218,9 +247,6 @@ private:
   qint16 wordidx;
   qint16 lineCount;
   qreal choppedTrailing;
-  qint16 lastwrapcol;
-  qreal lastwrappos;
-  qreal width;
 };
 
 /*************************************************************************************************/
@@ -230,6 +256,4 @@ private:
 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
 
-ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)privateData(); }
-
 #endif