1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
3 * kruadrosxx@gmail.com *
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. *
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. *
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 ***************************************************************************/
23 #include <QGraphicsScene>
26 #include <QGraphicsSceneMouseEvent>
27 #include <QGraphicsView>
28 #include <QApplication>
30 #include <dcore/debug.h>
38 Private(): view(0) {};
39 Qt::Orientation orientation
;
44 Guide::Guide(Qt::Orientation o
,QGraphicsScene
*scene
): QGraphicsItem(0, scene
), d(new Private
)
49 // setAcceptsHoverEvents(true);
50 // setAcceptedMouseButtons(0);
51 // setFlag(QGraphicsItem::ItemIsFocusable, false);
52 setFlag(QGraphicsItem::ItemIgnoresTransformations
, true);
62 QRectF
Guide::boundingRect() const
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));
77 return QRectF(QPointF(init
.x(), 0), QSizeF(d
->view
->width()*2, 5));
81 void Guide::paint( QPainter
* painter
, const QStyleOptionGraphicsItem
* , QWidget
*w
)
85 foreach(QGraphicsView
*view
, scene()->views()) // get current view
87 if(view
->viewport() == w
)
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());
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);
120 return QPointF(0, value
.toPointF().y());
123 return QGraphicsItem::itemChange(change
, value
);
126 // void Guide::hoverEnterEvent(QGraphicsSceneHoverEvent * e) OLD
128 // QGraphicsSceneMouseEvent *event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMousePress);
129 // event->setButtons(Qt::LeftButton);
130 // event->setButton(Qt::LeftButton);
132 // mousePressEvent(event);
136 // setAcceptsHoverEvents(false);
141 void Guide::mouseMoveEvent(QGraphicsSceneMouseEvent
* e
)
149 setPos(e
->scenePos());
153 bool Guide::sceneEvent(QEvent
*e
)
157 // case QEvent::GraphicsSceneMouseMove:
158 // case QEvent::GraphicsSceneHoverEnter:
159 // case QEvent::GraphicsSceneHoverLeave:
160 // case QEvent::GraphicsSceneHoverMove:
162 // QGraphicsSceneMouseEvent *event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMousePress);
163 // event->setButtons(Qt::LeftButton);
164 // event->setButton(Qt::LeftButton);
166 // mousePressEvent(event);
174 return QGraphicsItem::sceneEvent(e
);
177 void Guide::syncCursor()
184 globalPos
= d
->view
->viewport()->mapToGlobal(scenePos().toPoint() + d
->view
->mapFromScene(QPointF(0,0)));
189 if(d
->orientation
== Qt::Vertical
)
191 distance
= globalPos
.x()+ 2 - QCursor::pos().x();
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()) ;
206 QCursor::setPos((int)QCursor::pos().x(), (int)globalPos
.y()+2);