modernize: Replace most remaining old-style connects by PMF ones
[quassel.git] / src / qtui / columnhandleitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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     setAcceptHoverEvents(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
52
53 void ColumnHandleItem::setXPos(qreal xpos)
54 {
55     setPos(xpos, 0);
56     QRectF sceneBRect = _boundingRect.translated(x(), 0);
57     _sceneLeft = sceneBRect.left();
58     _sceneRight = sceneBRect.right();
59     emit positionChanged(xpos);
60 }
61
62
63 void ColumnHandleItem::setXLimits(qreal min, qreal max)
64 {
65     _minXPos = min;
66     _maxXPos = max;
67     //if(x() < min) setPos(min, 0);
68     //else if(x() > max) setPos(max - width(), 0);
69 }
70
71
72 void ColumnHandleItem::sceneRectChanged(const QRectF &rect)
73 {
74     prepareGeometryChange();
75     _boundingRect = QRectF(-_width/2, rect.y(), _width, rect.height());
76 }
77
78
79 void ColumnHandleItem::setOpacity(qreal opacity)
80 {
81     _opacity = opacity;
82     update();
83 }
84
85
86 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
87 {
88     if (event->buttons() & Qt::LeftButton && _moving) {
89         qreal newx = event->scenePos().x() - _offset;
90         if (newx < _minXPos)
91             newx = _minXPos;
92         else if (newx + width() > _maxXPos)
93             newx = _maxXPos - width();
94         setPos(newx, 0);
95         event->accept();
96     }
97     else {
98         event->ignore();
99     }
100 }
101
102
103 void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
104 {
105     if (event->buttons() & Qt::LeftButton) {
106         QApplication::setOverrideCursor(Qt::ClosedHandCursor);
107         _moving = true;
108         _offset = event->pos().x();
109         event->accept();
110     }
111     else {
112         event->ignore();
113     }
114 }
115
116
117 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
118 {
119     if (_moving) {
120         _moving = false;
121         QRectF sceneBRect = _boundingRect.translated(x(), 0);
122         _sceneLeft = sceneBRect.left();
123         _sceneRight = sceneBRect.right();
124         emit positionChanged(x());
125         QApplication::restoreOverrideCursor();
126         event->accept();
127     }
128     else {
129         event->ignore();
130     }
131 }
132
133
134 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
135 {
136     Q_UNUSED(event);
137
138     _animation->setDirection(QPropertyAnimation::Forward);
139     _animation->start();
140 }
141
142
143 void ColumnHandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
144 {
145     Q_UNUSED(event);
146
147     _animation->setDirection(QPropertyAnimation::Backward);
148     _animation->start();
149 }
150
151
152 void ColumnHandleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
153 {
154     Q_UNUSED(option);
155     Q_UNUSED(widget);
156
157     QLinearGradient gradient(boundingRect().topLeft(), boundingRect().topRight());
158     QColor color = QApplication::palette().windowText().color();
159     color.setAlphaF(_opacity);
160     gradient.setColorAt(0, Qt::transparent);
161     gradient.setColorAt(0.45, color);
162     gradient.setColorAt(0.55, color);
163     gradient.setColorAt(1, Qt::transparent);
164     painter->fillRect(boundingRect(), gradient);
165 }