8ab7331b7ed05e99cc2bb96a40b6da6943ef3b25
[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 {
36   setAcceptsHoverEvents(true);
37   setZValue(10);
38   setCursor(QCursor(Qt::OpenHandCursor));
39   setFlag(ItemIsMovable);
40
41   connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(hoverChanged(qreal)));
42 }
43
44 void ColumnHandleItem::setXPos(qreal xpos) {
45   setPos(xpos - width()/2, (qreal)0);
46 }
47
48 void ColumnHandleItem::sceneRectChanged(const QRectF &rect) {
49   if(rect.height() != boundingRect().height())
50     prepareGeometryChange();
51 }
52
53 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
54   QGraphicsItem::mouseMoveEvent(event);
55 }
56
57 void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { qDebug() << "pressed!";
58   setCursor(QCursor(Qt::ClosedHandCursor));
59   QGraphicsItem::mousePressEvent(event);
60 }
61
62 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
63   setCursor(QCursor(Qt::OpenHandCursor));
64   QGraphicsItem::mouseReleaseEvent(event);
65 }
66
67 void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
68   Q_UNUSED(option);
69   Q_UNUSED(widget);
70
71   QLinearGradient gradient(0, 0, width(), 0);
72   gradient.setColorAt(0.25, Qt::transparent);
73   gradient.setColorAt(0.5, QColor(0, 0, 0, _hover * 200));
74   gradient.setColorAt(0.75, Qt::transparent);
75   painter->fillRect(boundingRect(), gradient);
76 }
77
78 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
79   Q_UNUSED(event);
80
81   _timeLine.setDirection(QTimeLine::Forward);
82   if(_timeLine.state() != QTimeLine::Running)
83     _timeLine.start();
84 }
85
86 void ColumnHandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
87   Q_UNUSED(event);
88
89   _timeLine.setDirection(QTimeLine::Backward);
90   if(_timeLine.state() != QTimeLine::Running)
91     _timeLine.start();
92 }
93
94 void ColumnHandleItem::hoverChanged(qreal value) {
95   _hover = value;
96   update();
97 }
98