Fixed crash in iTween network
[yamf.git] / yamf / drawing / private / guide.cpp
blobb24c7e1bcf299ce5b57423ef1191d954e81b715a
1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
3 * kruadrosxx@gmail.com *
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) any later version. *
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 ***************************************************************************/
21 #include "guide.h"
23 #include <QGraphicsScene>
24 #include <QPainter>
25 #include <QCursor>
26 #include <QGraphicsSceneMouseEvent>
27 #include <QGraphicsView>
28 #include <QApplication>
30 #include <dcore/debug.h>
32 namespace YAMF {
33 namespace Drawing {
34 namespace Private {
36 struct Guide::Private
38 Private(): view(0) {};
39 Qt::Orientation orientation;
40 bool enabled;
41 QGraphicsView *view;
44 Guide::Guide(Qt::Orientation o,QGraphicsScene *scene): QGraphicsItem(0, scene), d(new Private)
46 d->orientation = o;
47 d->enabled = true;
48 setZValue(1000);
49 // setAcceptsHoverEvents(true);
50 // setAcceptedMouseButtons(0);
51 // setFlag(QGraphicsItem::ItemIsFocusable, false);
52 setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
57 Guide::~Guide()
59 delete d;
62 QRectF Guide::boundingRect() const
64 if(!d->view)
66 return QRectF(0,0,1,1);
69 QPointF init = d->view->mapToScene(QPoint(0,0));
71 if(d->orientation == Qt::Vertical)
73 return QRectF( QPointF(0,init.y()) , QSizeF(5, d->view->height()*2));
75 else
77 return QRectF(QPointF(init.x(), 0), QSizeF(d->view->width()*2, 5));
81 void Guide::paint( QPainter * painter, const QStyleOptionGraphicsItem * , QWidget *w )
83 // if(!d->view)
84 // {
85 foreach(QGraphicsView *view, scene()->views()) // get current view
87 if(view->viewport() == w)
89 d->view = view;
92 // }
94 painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
95 if(d->orientation == Qt::Vertical)
97 painter->drawLine((int)boundingRect().center().x(), (int)boundingRect().y(), (int)boundingRect().center().x(), (int)boundingRect().height());
99 else
101 painter->drawLine( (int)boundingRect().x(), (int)boundingRect().center().y(), (int)boundingRect().width(), (int)boundingRect().center().y());
105 void Guide::setEnabledSyncCursor(bool enabled)
107 d->enabled = enabled;
110 QVariant Guide::itemChange( GraphicsItemChange change, const QVariant & value )
112 if(change == ItemPositionChange)
114 if(d->orientation == Qt::Vertical)
116 return QPointF(value.toPointF().x(), 0);
118 else
120 return QPointF(0, value.toPointF().y());
123 return QGraphicsItem::itemChange(change, value );
126 // void Guide::hoverEnterEvent(QGraphicsSceneHoverEvent * e) OLD
127 // {
128 // QGraphicsSceneMouseEvent *event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMousePress);
129 // event->setButtons(Qt::LeftButton);
130 // event->setButton(Qt::LeftButton);
132 // mousePressEvent(event);
134 // delete event;
136 // setAcceptsHoverEvents(false);
138 // syncCursor();
139 // }
141 void Guide::mouseMoveEvent(QGraphicsSceneMouseEvent * e)
143 if(d->enabled)
145 syncCursor();
147 else
149 setPos(e->scenePos());
153 bool Guide::sceneEvent(QEvent *e)
155 // switch(e->type())
156 // {OLD
157 // case QEvent::GraphicsSceneMouseMove:
158 // case QEvent::GraphicsSceneHoverEnter:
159 // case QEvent::GraphicsSceneHoverLeave:
160 // case QEvent::GraphicsSceneHoverMove:
161 // {
162 // QGraphicsSceneMouseEvent *event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMousePress);
163 // event->setButtons(Qt::LeftButton);
164 // event->setButton(Qt::LeftButton);
166 // mousePressEvent(event);
168 // delete event;
169 // }
170 // break;
171 // default: break;
172 // }
174 return QGraphicsItem::sceneEvent(e);
177 void Guide::syncCursor()
179 QPointF globalPos;
180 if(scene())
182 if(d->view)
184 globalPos = d->view->viewport()->mapToGlobal(scenePos().toPoint() + d->view->mapFromScene(QPointF(0,0)));
188 double distance;
189 if(d->orientation == Qt::Vertical)
191 distance = globalPos.x()+ 2 - QCursor::pos().x();
193 else
195 distance = globalPos.y() + 2 - QCursor::pos().y();
198 if( -QApplication::startDragDistance() < distance && distance < QApplication::startDragDistance() )
200 if(d->orientation == Qt::Vertical)
202 QCursor::setPos((int)globalPos.x()+2, (int)QCursor::pos().y()) ;
204 else
206 QCursor::setPos((int)QCursor::pos().x(), (int)globalPos.y()+2);