cleanup: Clean up BufferViewConfig
[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 "columnhandleitem.h"
22
23 #include <QApplication>
24 #include <QCursor>
25 #include <QGraphicsSceneHoverEvent>
26 #include <QPainter>
27 #include <QPalette>
28
29 #include "chatview.h"
30
31 ColumnHandleItem::ColumnHandleItem(qreal w, QGraphicsItem* parent)
32     : QGraphicsObject(parent)
33     , _width(w)
34     , _boundingRect(-_width / 2, 0, _width, 0)
35     , _moving(false)
36     , _offset(0)
37     , _minXPos(0)
38     , _maxXPos(0)
39     , _opacity(0)
40     , _animation(new QPropertyAnimation(this, "opacity", this))
41 {
42     setAcceptHoverEvents(true);
43     setZValue(10);
44     setCursor(QCursor(Qt::OpenHandCursor));
45
46     _animation->setStartValue(0);
47     _animation->setEndValue(1);
48     _animation->setDirection(QPropertyAnimation::Forward);
49     _animation->setDuration(350);
50     _animation->setEasingCurve(QEasingCurve::InOutSine);
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 void ColumnHandleItem::setXLimits(qreal min, qreal max)
63 {
64     _minXPos = min;
65     _maxXPos = max;
66     // if(x() < min) setPos(min, 0);
67     // else if(x() > max) setPos(max - width(), 0);
68 }
69
70 void ColumnHandleItem::sceneRectChanged(const QRectF& rect)
71 {
72     prepareGeometryChange();
73     _boundingRect = QRectF(-_width / 2, rect.y(), _width, rect.height());
74 }
75
76 void ColumnHandleItem::setOpacity(qreal opacity)
77 {
78     _opacity = opacity;
79     update();
80 }
81
82 void ColumnHandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
83 {
84     if (event->buttons() & Qt::LeftButton && _moving) {
85         qreal newx = event->scenePos().x() - _offset;
86         if (newx < _minXPos)
87             newx = _minXPos;
88         else if (newx + width() > _maxXPos)
89             newx = _maxXPos - width();
90         setPos(newx, 0);
91         event->accept();
92     }
93     else {
94         event->ignore();
95     }
96 }
97
98 void ColumnHandleItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
99 {
100     if (event->buttons() & Qt::LeftButton) {
101         QApplication::setOverrideCursor(Qt::ClosedHandCursor);
102         _moving = true;
103         _offset = event->pos().x();
104         event->accept();
105     }
106     else {
107         event->ignore();
108     }
109 }
110
111 void ColumnHandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
112 {
113     if (_moving) {
114         _moving = false;
115         QRectF sceneBRect = _boundingRect.translated(x(), 0);
116         _sceneLeft = sceneBRect.left();
117         _sceneRight = sceneBRect.right();
118         emit positionChanged(x());
119         QApplication::restoreOverrideCursor();
120         event->accept();
121     }
122     else {
123         event->ignore();
124     }
125 }
126
127 void ColumnHandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
128 {
129     Q_UNUSED(event);
130
131     _animation->setDirection(QPropertyAnimation::Forward);
132     _animation->start();
133 }
134
135 void ColumnHandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
136 {
137     Q_UNUSED(event);
138
139     _animation->setDirection(QPropertyAnimation::Backward);
140     _animation->start();
141 }
142
143 void ColumnHandleItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
144 {
145     Q_UNUSED(option);
146     Q_UNUSED(widget);
147
148     QLinearGradient gradient(boundingRect().topLeft(), boundingRect().topRight());
149     QColor color = QApplication::palette().windowText().color();
150     color.setAlphaF(_opacity);
151     gradient.setColorAt(0, Qt::transparent);
152     gradient.setColorAt(0.45, color);
153     gradient.setColorAt(0.55, color);
154     gradient.setColorAt(1, Qt::transparent);
155     painter->fillRect(boundingRect(), gradient);
156 }