Manual implement column handle movement to be able to restrict it to horizontal movin...
authorManuel Nickschas <sputnick@quassel-irc.org>
Fri, 25 Jul 2008 22:07:13 +0000 (00:07 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sat, 2 Aug 2008 13:17:10 +0000 (15:17 +0200)
src/qtui/chatscene.cpp
src/qtui/columnhandleitem.cpp
src/qtui/columnhandleitem.h

index bcf3c93..6eb07a9 100644 (file)
@@ -28,6 +28,8 @@
 #include "columnhandleitem.h"
 #include "qtui.h"
 
+const qreal minContentsWidth = 200;
+
 ChatScene::ChatScene(QAbstractItemModel *model, QObject *parent) : QGraphicsScene(parent), _model(model) {
   _width = 0;
   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
@@ -46,7 +48,9 @@ ChatScene::ChatScene(QAbstractItemModel *model, QObject *parent) : QGraphicsScen
   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
 
   firstColHandle->setXPos(firstColHandlePos);
+  firstColHandle->setXLimits(0, secondColHandlePos);
   secondColHandle->setXPos(secondColHandlePos);
+  secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
 
   emit heightChanged(height());
 }
@@ -91,6 +95,7 @@ void ChatScene::setWidth(qreal w) {
     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
   }
   setSceneRect(QRectF(0, 0, w, _height));
+  secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
   emit heightChanged(_height);
 }
 
index 8ab7331..e8dd97e 100644 (file)
@@ -31,18 +31,27 @@ ColumnHandleItem::ColumnHandleItem(qreal w, QGraphicsItem *parent)
   : QGraphicsItem(parent),
     _width(w),
     _hover(0),
-    _timeLine(150)
+    _timeLine(150),
+    _moving(false),
+    _minXPos(0),
+    _maxXPos(0)
 {
   setAcceptsHoverEvents(true);
   setZValue(10);
   setCursor(QCursor(Qt::OpenHandCursor));
-  setFlag(ItemIsMovable);
 
   connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(hoverChanged(qreal)));
 }
 
 void ColumnHandleItem::setXPos(qreal xpos) {
-  setPos(xpos - width()/2, (qreal)0);
+  setPos(xpos - width()/2, 0);
+}
+
+void ColumnHandleItem::setXLimits(qreal min, qreal max) {
+  _minXPos = min;
+  _maxXPos = max;
+  //if(x() < min) setPos(min, 0);
+  //else if(x() > max) setPos(max - width(), 0);
 }
 
 void ColumnHandleItem::sceneRectChanged(const QRectF &rect) {
@@ -51,28 +60,37 @@ void ColumnHandleItem::sceneRectChanged(const QRectF &rect) {
 }
 
 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
-  QGraphicsItem::mouseMoveEvent(event);
+  if(event->buttons() & Qt::LeftButton && _moving) {
+    if(contains(event->lastPos())) {
+      qreal newx = x() + (event->scenePos() - event->lastScenePos()).x();
+      if(newx < _minXPos) newx = _minXPos;
+      else if(newx + width() > _maxXPos) newx = _maxXPos - width();
+      setPos(newx, 0);
+    }
+    event->accept();
+  } else {
+    event->ignore();
+  }
 }
 
-void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { qDebug() << "pressed!";
-  setCursor(QCursor(Qt::ClosedHandCursor));
-  QGraphicsItem::mousePressEvent(event);
+void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+  if(event->buttons() & Qt::LeftButton) {
+    setCursor(QCursor(Qt::ClosedHandCursor));
+    _moving = true;
+    event->accept();
+  } else {
+    event->ignore();
+  }
 }
 
 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  setCursor(QCursor(Qt::OpenHandCursor));
-  QGraphicsItem::mouseReleaseEvent(event);
-}
-
-void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
-  Q_UNUSED(option);
-  Q_UNUSED(widget);
-
-  QLinearGradient gradient(0, 0, width(), 0);
-  gradient.setColorAt(0.25, Qt::transparent);
-  gradient.setColorAt(0.5, QColor(0, 0, 0, _hover * 200));
-  gradient.setColorAt(0.75, Qt::transparent);
-  painter->fillRect(boundingRect(), gradient);
+  if(_moving) {
+    _moving = false;
+    setCursor(QCursor(Qt::OpenHandCursor));
+    event->accept();
+  } else {
+    event->ignore();
+  }
 }
 
 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
@@ -96,3 +114,14 @@ void ColumnHandleItem::hoverChanged(qreal value) {
   update();
 }
 
+void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+  Q_UNUSED(option);
+  Q_UNUSED(widget);
+  
+  QLinearGradient gradient(0, 0, width(), 0);
+  gradient.setColorAt(0.25, Qt::transparent);
+  gradient.setColorAt(0.5, QColor(0, 0, 0, _hover * 200));
+  gradient.setColorAt(0.75, Qt::transparent);
+  painter->fillRect(boundingRect(), gradient);
+}
+
index dc12512..f9d1f7a 100644 (file)
@@ -31,21 +31,22 @@ class ColumnHandleItem : public QObject, public QGraphicsItem {
 
 public:
   ColumnHandleItem(qreal width, QGraphicsItem *parent = 0);
-  
+
   inline qreal width() const { return _width; }
   inline QRectF boundingRect() const { return QRectF(0, 0, _width, scene()->height()); }
   void setXPos(qreal xpos);
-  
+
   void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
-  void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
-  void mousePressEvent(QGraphicsSceneMouseEvent *event);
-  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
-  
+
   void sceneRectChanged(const QRectF &);
+  void setXLimits(qreal min, qreal max);
 
 protected:
   void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
   void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
+  void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
+  void mousePressEvent(QGraphicsSceneMouseEvent *event);
+  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
 
 private slots:
   void hoverChanged(qreal value);
@@ -53,6 +54,8 @@ private slots:
 private:
   qreal _width;
   qreal _hover;
+  bool _moving;
+  qreal _minXPos, _maxXPos;
   QTimeLine _timeLine;
 };