Some cleanups in ChatScene in preparation to mouse handling revamp
authorManuel Nickschas <sputnick@quassel-irc.org>
Fri, 7 Nov 2008 21:21:57 +0000 (22:21 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 20 Nov 2008 14:14:53 +0000 (15:14 +0100)
* We should always use qreal for scene coordinates
* Provide a few convenience methods

src/qtui/chatitem.h
src/qtui/chatmonitorview.cpp
src/qtui/chatscene.cpp
src/qtui/chatscene.h
src/qtui/chatview.cpp

index cf608cd..79f8720 100644 (file)
@@ -69,6 +69,8 @@ protected:
   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
 
+  //virtual bool handleClick(ClickMode mode);
+
   inline QTextLayout *layout() const;
 
   virtual QTextLayout::FormatRange selectionFormat() const;
index 5ab9df7..d5c3731 100644 (file)
@@ -48,7 +48,7 @@ void ChatMonitorView::contextMenuEvent(QContextMenuEvent *event) {
   showOwnNicksAction->setCheckable(true);
   showOwnNicksAction->setChecked(_filter->showOwnMessages());
     
-  if(scene()->sectionByScenePos(event->pos()) == ChatLineModel::SenderColumn) {
+  if(scene()->columnByScenePos(event->pos()) == ChatLineModel::SenderColumn) {
     contextMenu.addSeparator();
 
     QAction *showNetworkAction = contextMenu.addAction(tr("Show network name"), this, SLOT(showFieldsChanged(bool)));
@@ -66,7 +66,7 @@ void ChatMonitorView::contextMenuEvent(QContextMenuEvent *event) {
 }
 
 void ChatMonitorView::mouseDoubleClickEvent(QMouseEvent *event) {
-  if(scene()->sectionByScenePos(event->pos()) != ChatLineModel::SenderColumn) {
+  if(scene()->columnByScenePos(event->pos()) != ChatLineModel::SenderColumn) {
     ChatView::mouseDoubleClickEvent(event);
     return;
   }
index 1ce69f6..ba31ce8 100644 (file)
@@ -104,6 +104,14 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal w
 ChatScene::~ChatScene() {
 }
 
+bool ChatScene::containsBuffer(const BufferId &id) const {
+  MessageFilter *filter = qobject_cast<MessageFilter*>(model());
+  if(filter)
+    return filter->containsBuffer(id);
+  else
+    return false;
+}
+
 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
   Q_UNUSED(index);
 
@@ -464,17 +472,9 @@ void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
 }
 
 void ChatScene::updateSelection(const QPointF &pos) {
-  // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
-  // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
-  ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(_secondColHandle->sceneRight() + 1, pos.y())));
-  if(!contentItem) return;
-
-  int curRow = contentItem->row();
-  int curColumn;
-  if(pos.x() > _secondColHandle->sceneRight()) curColumn = ChatLineModel::ContentsColumn;
-  else if(pos.x() > _firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
-  else curColumn = ChatLineModel::TimestampColumn;
-
+  int curRow = rowByScenePos(pos);
+  if(curRow < 0) return;
+  int curColumn = (int)columnByScenePos(pos);
   ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
   if(minColumn != _selectionMinCol) {
     _selectionMinCol = minColumn;
@@ -595,7 +595,7 @@ void ChatScene::requestBacklog() {
   return;
 }
 
-int ChatScene::sectionByScenePos(int x) {
+ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) {
   if(x < _firstColHandle->x())
     return ChatLineModel::TimestampColumn;
   if(x < _secondColHandle->x())
@@ -604,6 +604,15 @@ int ChatScene::sectionByScenePos(int x) {
   return ChatLineModel::ContentsColumn;
 }
 
+int ChatScene::rowByScenePos(qreal y) {
+  // This is somewhat hacky... we look at the contents item that is at the given y position, since
+  // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
+  // ChatItems cover their ChatLine, so we won't get to the latter directly.
+  ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(_secondColHandle->sceneRight() + 1, y)));
+  if(!contentItem) return -1;
+  return contentItem->row();
+}
+
 void ChatScene::updateSceneRect(qreal width) {
   if(_lines.isEmpty()) {
     updateSceneRect(QRectF(0, 0, width, 0));
index 85328df..b0c80f4 100644 (file)
@@ -24,7 +24,9 @@
 #include <QAbstractItemModel>
 #include <QGraphicsScene>
 #include <QSet>
+#include <QTimer>
 
+#include "chatlinemodel.h"
 #include "columnhandleitem.h"
 #include "messagefilter.h"
 
@@ -54,16 +56,25 @@ public:
     WebPreviewType
   };
 
+  enum ClickMode {
+    NoClick,
+    SingleClick,
+    DoubleClick,
+    TripleClick
+  };
+
   ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, QObject *parent);
   virtual ~ChatScene();
 
   inline QAbstractItemModel *model() const { return _model; }
   inline QString idString() const { return _idString; }
 
-  int sectionByScenePos(int x);
-  inline int sectionByScenePos(const QPoint &pos) { return sectionByScenePos(pos.x()); }
+  int rowByScenePos(qreal y);
+  inline int rowByScenePos(const QPointF &pos) { return rowByScenePos(pos.y()); }
+  ChatLineModel::ColumnType columnByScenePos(qreal x);
+  inline ChatLineModel::ColumnType columnByScenePos(const QPointF &pos) { return columnByScenePos(pos.x()); }
   inline bool isSingleBufferScene() const { return _singleBufferScene; }
-  inline bool containsBuffer(const BufferId &id) const;
+  bool containsBuffer(const BufferId &id) const;
   inline ChatLine *chatLine(int row) { return (row < _lines.count()) ? _lines[row] : 0; }
 
   inline ColumnHandleItem *firstColumnHandle() const { return _firstColHandle; }
@@ -100,6 +111,7 @@ protected:
   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
   virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
+  //virtual bool handleLeftClick(ClickMode mode);
 
 protected slots:
   void rowsInserted(const QModelIndex &, int, int);
@@ -144,6 +156,8 @@ private:
 
   bool _showWebPreview;
 
+  QTimer _clickTimer;
+
   struct WebPreview {
     ChatItem *parentItem;
     QGraphicsItem *previewItem;
@@ -156,12 +170,4 @@ private:
   WebPreview webPreview;
 };
 
-bool ChatScene::containsBuffer(const BufferId &id) const {
-  MessageFilter *filter = qobject_cast<MessageFilter*>(model());
-  if(filter)
-    return filter->containsBuffer(id);
-  else
-    return false;
-}
-
 #endif
index 9c5a6dc..53fa024 100644 (file)
@@ -61,7 +61,7 @@ void ChatView::init(MessageFilter *filter) {
   _scrollTimer.setSingleShot(true);
   connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
 
-  _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 2, this); // see below: resizeEvent()
+  _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 4, this); // see below: resizeEvent()
   connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
   connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
   connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));