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