From: Manuel Nickschas Date: Fri, 25 Jul 2008 22:07:13 +0000 (+0200) Subject: Manual implement column handle movement to be able to restrict it to horizontal movin... X-Git-Tag: 0.3.0~156 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=0072d69c3387cad4e393968647e0f2894aed6877;hp=9806418efa4c59dae71fd628ea9c57453ca81434 Manual implement column handle movement to be able to restrict it to horizontal moving between hard limits --- diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index bcf3c935..6eb07a97 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -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); } diff --git a/src/qtui/columnhandleitem.cpp b/src/qtui/columnhandleitem.cpp index 8ab7331b..e8dd97e2 100644 --- a/src/qtui/columnhandleitem.cpp +++ b/src/qtui/columnhandleitem.cpp @@ -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); +} + diff --git a/src/qtui/columnhandleitem.h b/src/qtui/columnhandleitem.h index dc125120..f9d1f7ad 100644 --- a/src/qtui/columnhandleitem.h +++ b/src/qtui/columnhandleitem.h @@ -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; };