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