Add shortcut that jumps to the markerline
[quassel.git] / src / qtui / chatitem.h
index 36fb1c1..4e51e93 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
+ *   Copyright (C) 2005-2010 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -22,7 +22,6 @@
 #define CHATITEM_H_
 
 #include <QAction>
-#include <QGraphicsItem>
 #include <QObject>
 
 #include "chatlinemodel.h"
 
 #include <QTextLayout>
 
-class ChatItem : public QGraphicsItem {
+class ChatLine;
+class ChatView;
+
+/* All external positions are relative to the parent ChatLine */
+/* Yes, that's also true for the boundingRect() and related things */
+
+class ChatItem {
 protected:
-  ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent);
+  // boundingRect is relative to the parent ChatLine
+  ChatItem(const QRectF &boundingRect, ChatLine *parent);
+  virtual ~ChatItem();
 
 public:
   const QAbstractItemModel *model() const;
+  ChatLine *chatLine() const;
+  ChatScene *chatScene() const;
+  ChatView *chatView() const;
   int row() const;
   virtual ChatLineModel::ColumnType column() const = 0;
-  inline ChatScene *chatScene() const { return qobject_cast<ChatScene *>(scene()); }
 
-  inline QRectF boundingRect() const { return _boundingRect; }
-  inline qreal width() const { return _boundingRect.width(); }
-  inline qreal height() const { return _boundingRect.height(); }
+  // 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;
 
-  void initLayoutHelper(QTextLayout *layout, QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
-  virtual inline void initLayout(QTextLayout *layout) const {
-    initLayoutHelper(layout, QTextOption::NoWrap);
-    doLayout(layout);
-  }
-  virtual void doLayout(QTextLayout *) const;
-  virtual UiStyle::FormatList formatList() 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 = 0);
-  enum { Type = ChatScene::ChatItemType };
-  virtual inline int type() const { return Type; }
+  virtual inline int type() const { return ChatScene::ChatItemType; }
 
   QVariant data(int role) const;
 
@@ -74,6 +83,13 @@ public:
   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:
   enum SelectionMode {
     NoSelection,
@@ -84,6 +100,15 @@ protected:
   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 *);
   QVector<QTextLayout::FormatRange> selectionFormats() const;
@@ -100,29 +125,23 @@ protected:
 
   qint16 posToCursor(const QPointF &pos) const;
 
-  // 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) {
-    _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 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;
 
   SelectionMode _selectionMode;
   qint16 _selectionStart, _selectionEnd;
 
+  mutable QTextLayout *_cachedLayout;
+
+  // internal selection stuff
+  void setSelection(int start, int length);
+
   friend class ChatLine;
 };
 
@@ -133,9 +152,8 @@ private:
 //! 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), parent) {}
-  enum { Type = ChatScene::TimestampChatItemType };
-  virtual inline int type() const { return Type; }
+  TimestampChatItem(const QRectF &boundingRect, ChatLine *parent) : ChatItem(boundingRect, parent) {}
+  virtual inline int type() const { return ChatScene::TimestampChatItemType; }
   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
 };
 
@@ -145,17 +163,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, parent) {}
+  SenderChatItem(const QRectF &boundingRect, ChatLine *parent) : ChatItem(boundingRect, parent) {}
   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
+  virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
 
 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 void initLayout(QTextLayout *layout) const {
-    initLayoutHelper(layout, QTextOption::ManualWrap, Qt::AlignRight);
-    doLayout(layout);
-  }
+  virtual inline int type() const { return ChatScene::SenderChatItemType; }
+  virtual void initLayout(QTextLayout *layout) const;
 };
 
 // ************************************************************
@@ -168,15 +183,16 @@ 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();
 
-  enum { Type = ChatScene::ContentsChatItemType };
-  virtual inline int type() const { return Type; }
+  virtual inline int type() const { return ChatScene::ContentsChatItemType; }
 
   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
   QFontMetricsF *fontMetrics() const;
 
+  virtual void clearCache();
+
 protected:
   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
   virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
@@ -188,10 +204,7 @@ protected:
 
   virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
 
-  virtual inline void initLayout(QTextLayout *layout) const {
-    initLayoutHelper(layout, QTextOption::WrapAtWordBoundaryOrAnywhere);
-    doLayout(layout);
-  }
+  virtual void initLayout(QTextLayout *layout) const;
   virtual void doLayout(QTextLayout *layout) const;
   virtual UiStyle::FormatList formatList() const;
 
@@ -199,7 +212,7 @@ private:
   class ActionProxy;
   class WrapColumnFinder;
 
-  ContentsChatItemPrivate *_data;
+  mutable ContentsChatItemPrivate *_data;
   ContentsChatItemPrivate *privateData() const;
 
   Clickable clickableAt(const QPointF &pos) const;
@@ -209,13 +222,14 @@ private:
   void clearWebPreview();
 
   qreal setGeometryByWidth(qreal w);
-  friend class ChatLine;
-  friend struct ContentsChatItemPrivate;
 
   QFontMetricsF *_fontMetrics;
 
   // we need a receiver for Action signals
   static ActionProxy _actionProxy;
+
+  friend class ChatLine;
+  friend struct ContentsChatItemPrivate;
 };
 
 struct ContentsChatItemPrivate {
@@ -268,4 +282,13 @@ private:
 
 /*************************************************************************************************/
 
+// 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