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