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