e8dd97e2e91dc6acab465b771075441228f1d8eb
[quassel.git] / src / qtui / columnhandleitem.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
3 *   devel@quassel-irc.org                                                 *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) version 3.                                           *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include <QCursor>
22 #include <QGraphicsScene>
23 #include <QGraphicsSceneHoverEvent>
24 #include <QPainter>
25
26 #include <QDebug>
27
28 #include "columnhandleitem.h"
29
30 ColumnHandleItem::ColumnHandleItem(qreal w, QGraphicsItem *parent)
31   : QGraphicsItem(parent),
32     _width(w),
33     _hover(0),
34     _timeLine(150),
35     _moving(false),
36     _minXPos(0),
37     _maxXPos(0)
38 {
39   setAcceptsHoverEvents(true);
40   setZValue(10);
41   setCursor(QCursor(Qt::OpenHandCursor));
42
43   connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(hoverChanged(qreal)));
44 }
45
46 void ColumnHandleItem::setXPos(qreal xpos) {
47   setPos(xpos - width()/2, 0);
48 }
49
50 void ColumnHandleItem::setXLimits(qreal min, qreal max) {
51   _minXPos = min;
52   _maxXPos = max;
53   //if(x() < min) setPos(min, 0);
54   //else if(x() > max) setPos(max - width(), 0);
55 }
56
57 void ColumnHandleItem::sceneRectChanged(const QRectF &rect) {
58   if(rect.height() != boundingRect().height())
59     prepareGeometryChange();
60 }
61
62 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
63   if(event->buttons() & Qt::LeftButton && _moving) {
64     if(contains(event->lastPos())) {
65       qreal newx = x() + (event->scenePos() - event->lastScenePos()).x();
66       if(newx < _minXPos) newx = _minXPos;
67       else if(newx + width() > _maxXPos) newx = _maxXPos - width();
68       setPos(newx, 0);
69     }
70     event->accept();
71   } else {
72     event->ignore();
73   }
74 }
75
76 void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
77   if(event->buttons() & Qt::LeftButton) {
78     setCursor(QCursor(Qt::ClosedHandCursor));
79     _moving = true;
80     event->accept();
81   } else {
82     event->ignore();
83   }
84 }
85
86 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
87   if(_moving) {
88     _moving = false;
89     setCursor(QCursor(Qt::OpenHandCursor));
90     event->accept();
91   } else {
92     event->ignore();
93   }
94 }
95
96 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
97   Q_UNUSED(event);
98
99   _timeLine.setDirection(QTimeLine::Forward);
100   if(_timeLine.state() != QTimeLine::Running)
101     _timeLine.start();
102 }
103
104 void ColumnHandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
105   Q_UNUSED(event);
106
107   _timeLine.setDirection(QTimeLine::Backward);
108   if(_timeLine.state() != QTimeLine::Running)
109     _timeLine.start();
110 }
111
112 void ColumnHandleItem::hoverChanged(qreal value) {
113   _hover = value;
114   update();
115 }
116
117 void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
118   Q_UNUSED(option);
119   Q_UNUSED(widget);
120   
121   QLinearGradient gradient(0, 0, width(), 0);
122   gradient.setColorAt(0.25, Qt::transparent);
123   gradient.setColorAt(0.5, QColor(0, 0, 0, _hover * 200));
124   gradient.setColorAt(0.75, Qt::transparent);
125   painter->fillRect(boundingRect(), gradient);
126 }
127