Fix some warnings
[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 "columnhandleitem.h"
27
28 ColumnHandleItem::ColumnHandleItem(qreal w, QGraphicsItem *parent)
29   : QGraphicsItem(parent),
30     _width(w),
31     _hover(0),
32     _moving(false),
33     _timeLine(150),
34     _minXPos(0),
35     _maxXPos(0)
36 {
37   setAcceptsHoverEvents(true);
38   setZValue(10);
39   setCursor(QCursor(Qt::OpenHandCursor));
40
41   connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(hoverChanged(qreal)));
42 }
43
44 void ColumnHandleItem::setXPos(qreal xpos) {
45   setPos(xpos - width()/2, 0);
46 }
47
48 void ColumnHandleItem::setXLimits(qreal min, qreal max) {
49   _minXPos = min;
50   _maxXPos = max;
51   //if(x() < min) setPos(min, 0);
52   //else if(x() > max) setPos(max - width(), 0);
53 }
54
55 void ColumnHandleItem::sceneRectChanged(const QRectF &rect) {
56   if(rect.height() != boundingRect().height())
57     prepareGeometryChange();
58 }
59
60 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
61   if(event->buttons() & Qt::LeftButton && _moving) {
62     if(contains(event->lastPos())) {
63       qreal newx = x() + (event->scenePos() - event->lastScenePos()).x();
64       if(newx < _minXPos) newx = _minXPos;
65       else if(newx + width() > _maxXPos) newx = _maxXPos - width();
66       setPos(newx, 0);
67     }
68     event->accept();
69   } else {
70     event->ignore();
71   }
72 }
73
74 void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
75   if(event->buttons() & Qt::LeftButton) {
76     setCursor(QCursor(Qt::ClosedHandCursor));
77     _moving = true;
78     event->accept();
79   } else {
80     event->ignore();
81   }
82 }
83
84 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
85   if(_moving) {
86     _moving = false;
87     setCursor(QCursor(Qt::OpenHandCursor));
88     emit positionChanged(x() + width()/2);
89     event->accept();
90   } else {
91     event->ignore();
92   }
93 }
94
95 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
96   Q_UNUSED(event);
97
98   _timeLine.setDirection(QTimeLine::Forward);
99   if(_timeLine.state() != QTimeLine::Running)
100     _timeLine.start();
101 }
102
103 void ColumnHandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
104   Q_UNUSED(event);
105
106   _timeLine.setDirection(QTimeLine::Backward);
107   if(_timeLine.state() != QTimeLine::Running)
108     _timeLine.start();
109 }
110
111 void ColumnHandleItem::hoverChanged(qreal value) {
112   _hover = value;
113   update();
114 }
115
116 void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
117   Q_UNUSED(option);
118   Q_UNUSED(widget);
119
120   QLinearGradient gradient(0, 0, width(), 0);
121   gradient.setColorAt(0.25, Qt::transparent);
122   gradient.setColorAt(0.5, QColor(0, 0, 0, _hover * 200));
123   gradient.setColorAt(0.75, Qt::transparent);
124   painter->fillRect(boundingRect(), gradient);
125 }
126