Indicate whether a key is set for the buffer.
[quassel.git] / src / qtui / columnhandleitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "chatview.h"
22 #include "columnhandleitem.h"
23
24 #include <QApplication>
25 #include <QCursor>
26 #include <QGraphicsSceneHoverEvent>
27 #include <QPainter>
28 #include <QPalette>
29
30 ColumnHandleItem::ColumnHandleItem(qreal w, QGraphicsItem *parent)
31     : QGraphicsObject(parent),
32     _width(w),
33     _boundingRect(-_width/2, 0, _width, 0),
34     _moving(false),
35     _offset(0),
36     _minXPos(0),
37     _maxXPos(0),
38     _opacity(0),
39     _animation(new QPropertyAnimation(this, "opacity", this))
40 {
41     setAcceptsHoverEvents(true);
42     setZValue(10);
43     setCursor(QCursor(Qt::OpenHandCursor));
44
45     _animation->setStartValue(0);
46     _animation->setEndValue(1);
47     _animation->setDirection(QPropertyAnimation::Forward);
48     _animation->setDuration(350);
49     _animation->setEasingCurve(QEasingCurve::InOutSine);
50
51     //connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(hoverChanged(qreal)));
52 }
53
54
55 void ColumnHandleItem::setXPos(qreal xpos)
56 {
57     setPos(xpos, 0);
58     QRectF sceneBRect = _boundingRect.translated(x(), 0);
59     _sceneLeft = sceneBRect.left();
60     _sceneRight = sceneBRect.right();
61 }
62
63
64 void ColumnHandleItem::setXLimits(qreal min, qreal max)
65 {
66     _minXPos = min;
67     _maxXPos = max;
68     //if(x() < min) setPos(min, 0);
69     //else if(x() > max) setPos(max - width(), 0);
70 }
71
72
73 void ColumnHandleItem::sceneRectChanged(const QRectF &rect)
74 {
75     prepareGeometryChange();
76     _boundingRect = QRectF(-_width/2, rect.y(), _width, rect.height());
77 }
78
79
80 void ColumnHandleItem::setOpacity(qreal opacity)
81 {
82     _opacity = opacity;
83     update();
84 }
85
86
87 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
88 {
89     if (event->buttons() & Qt::LeftButton && _moving) {
90         qreal newx = event->scenePos().x() - _offset;
91         if (newx < _minXPos)
92             newx = _minXPos;
93         else if (newx + width() > _maxXPos)
94             newx = _maxXPos - width();
95         setPos(newx, 0);
96         event->accept();
97     }
98     else {
99         event->ignore();
100     }
101 }
102
103
104 void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
105 {
106     if (event->buttons() & Qt::LeftButton) {
107         QApplication::setOverrideCursor(Qt::ClosedHandCursor);
108         _moving = true;
109         _offset = event->pos().x();
110         event->accept();
111     }
112     else {
113         event->ignore();
114     }
115 }
116
117
118 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
119 {
120     if (_moving) {
121         _moving = false;
122         QRectF sceneBRect = _boundingRect.translated(x(), 0);
123         _sceneLeft = sceneBRect.left();
124         _sceneRight = sceneBRect.right();
125         emit positionChanged(x());
126         QApplication::restoreOverrideCursor();
127         event->accept();
128     }
129     else {
130         event->ignore();
131     }
132 }
133
134
135 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
136 {
137     Q_UNUSED(event);
138
139     _animation->setDirection(QPropertyAnimation::Forward);
140     _animation->start();
141 }
142
143
144 void ColumnHandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
145 {
146     Q_UNUSED(event);
147
148     _animation->setDirection(QPropertyAnimation::Backward);
149     _animation->start();
150 }
151
152
153 void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
154 {
155     Q_UNUSED(option);
156     Q_UNUSED(widget);
157
158     QLinearGradient gradient(boundingRect().topLeft(), boundingRect().topRight());
159     QColor color = QApplication::palette().windowText().color();
160     color.setAlphaF(_opacity);
161     gradient.setColorAt(0, Qt::transparent);
162     gradient.setColorAt(0.45, color);
163     gradient.setColorAt(0.55, color);
164     gradient.setColorAt(1, Qt::transparent);
165     painter->fillRect(boundingRect(), gradient);
166 }