Allow configuration of shortcuts for platforms other than KDE
[quassel.git] / src / qtui / columnhandleitem.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 by the Quassel Project                          *
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(350),
41     _rulerColor(QApplication::palette().windowText().color())
42 {
43   _timeLine.setUpdateInterval(20);
44
45   setAcceptsHoverEvents(true);
46   setZValue(10);
47   setCursor(QCursor(Qt::OpenHandCursor));
48
49   connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(hoverChanged(qreal)));
50 }
51
52 void ColumnHandleItem::setXPos(qreal xpos) {
53   setPos(xpos, 0);
54   QRectF sceneBRect = _boundingRect.translated(x(), 0);
55   _sceneLeft = sceneBRect.left();
56   _sceneRight = sceneBRect.right();
57 }
58
59 void ColumnHandleItem::setXLimits(qreal min, qreal max) {
60   _minXPos = min;
61   _maxXPos = max;
62   //if(x() < min) setPos(min, 0);
63   //else if(x() > max) setPos(max - width(), 0);
64 }
65
66 void ColumnHandleItem::sceneRectChanged(const QRectF &rect) {
67   prepareGeometryChange();
68   _boundingRect = QRectF(-_width/2, rect.y(), _width, rect.height());
69 }
70
71 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
72   if(event->buttons() & Qt::LeftButton && _moving) {
73     if(contains(event->lastPos())) {
74       qreal newx = x() + (event->scenePos() - event->lastScenePos()).x();
75       if(newx < _minXPos) newx = _minXPos;
76       else if(newx + width() > _maxXPos) newx = _maxXPos - width();
77       setPos(newx, 0);
78     }
79     event->accept();
80   } else {
81     event->ignore();
82   }
83 }
84
85 void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
86   if(event->buttons() & Qt::LeftButton) {
87     setCursor(QCursor(Qt::ClosedHandCursor));
88     _moving = true;
89     event->accept();
90   } else {
91     event->ignore();
92   }
93 }
94
95 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
96   if(_moving) {
97     _moving = false;
98     setCursor(QCursor(Qt::OpenHandCursor));
99     QRectF sceneBRect = _boundingRect.translated(x(), 0);
100     _sceneLeft = sceneBRect.left();
101     _sceneRight = sceneBRect.right();
102     emit positionChanged(x());
103     event->accept();
104   } else {
105     event->ignore();
106   }
107 }
108
109 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
110   Q_UNUSED(event);
111
112   _timeLine.setDirection(QTimeLine::Forward);
113   if(_timeLine.state() != QTimeLine::Running)
114     _timeLine.start();
115 }
116
117 void ColumnHandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
118   Q_UNUSED(event);
119
120   _timeLine.setDirection(QTimeLine::Backward);
121   if(_timeLine.state() != QTimeLine::Running)
122     _timeLine.start();
123 }
124
125 void ColumnHandleItem::hoverChanged(qreal value) {
126   _hover = value;
127   update();
128 }
129
130 void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
131   Q_UNUSED(option);
132   Q_UNUSED(widget);
133
134   QLinearGradient gradient(boundingRect().topLeft(), boundingRect().topRight());
135   QColor _rulerColor = QApplication::palette().windowText().color();
136   _rulerColor.setAlphaF(_hover);
137   gradient.setColorAt(0, Qt::transparent);
138   gradient.setColorAt(0.4, _rulerColor);
139   gradient.setColorAt(0.6, _rulerColor);
140   gradient.setColorAt(1, Qt::transparent);
141   painter->fillRect(boundingRect(), gradient);
142 }
143