1 /*******************************************************************
2
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 Fachhochschule Potsdam - http://fh-potsdam.de
5
6 Fritzing is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Fritzing is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
18
19 ********************************************************************
20
21 $Revision: 6904 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-02-26 16:26:03 +0100 (Di, 26. Feb 2013) $
24
25 ********************************************************************/
26
27 #include "resizehandle.h"
28 #include "../sketch/zoomablegraphicsview.h"
29 #include "../debugdialog.h"
30 #include "../sketch/fgraphicsscene.h"
31
32 #include <QCursor>
33
ResizeHandle(const QPixmap & pixmap,const QCursor & cursor,bool ignoresTransforms,QGraphicsItem * parent)34 ResizeHandle::ResizeHandle(const QPixmap &pixmap, const QCursor & cursor, bool ignoresTransforms, QGraphicsItem *parent)
35 : QGraphicsPixmapItem(pixmap, parent)
36 {
37 setCursor(cursor);
38 setVisible(true);
39 setFlag(QGraphicsItem::ItemIgnoresTransformations, ignoresTransforms);
40 }
41
~ResizeHandle()42 ResizeHandle::~ResizeHandle() {
43 }
44
mousePressEvent(QGraphicsSceneMouseEvent * event)45 void ResizeHandle::mousePressEvent(QGraphicsSceneMouseEvent * event) {
46 event->accept();
47 emit mousePressSignal(event, this);
48 }
49
mouseMoveEvent(QGraphicsSceneMouseEvent * event)50 void ResizeHandle::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
51 event->accept();
52 emit mouseMoveSignal(event, this);
53 }
54
mouseReleaseEvent(QGraphicsSceneMouseEvent * event)55 void ResizeHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
56 event->accept();
57 emit mouseReleaseSignal(event, this);
58 }
59
setResizeOffset(QPointF p)60 void ResizeHandle::setResizeOffset(QPointF p) {
61 m_resizeOffset = p;
62 }
63
resizeOffset()64 QPointF ResizeHandle::resizeOffset()
65 {
66 return m_resizeOffset;
67 }
68
itemChange(GraphicsItemChange change,const QVariant & value)69 QVariant ResizeHandle::itemChange(GraphicsItemChange change, const QVariant &value)
70 {
71 switch (change) {
72 case QGraphicsItem::ItemSceneHasChanged:
73 if (scaling()) {
74 ZoomableGraphicsView *sw = dynamic_cast<ZoomableGraphicsView*>(scene()->parent());
75 if (sw) {
76 connect(sw, SIGNAL(zoomChanged(double)), this, SLOT(zoomChangedSlot(double)));
77 }
78
79 }
80 break;
81 default:
82 break;
83 }
84
85 return QGraphicsPixmapItem::itemChange(change, value);
86 }
87
zoomChangedSlot(double scale)88 void ResizeHandle::zoomChangedSlot(double scale) {
89 emit zoomChangedSignal(scale);
90 }
91
currentScale()92 double ResizeHandle::currentScale() {
93 if (scaling()) {
94 ZoomableGraphicsView *sw = dynamic_cast<ZoomableGraphicsView*>(scene()->parent());
95 if(sw) {
96 return sw->currentZoom()/100;
97 }
98 }
99 return 1;
100 }
101
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)102 void ResizeHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 {
104 if(scene()) {
105 FGraphicsScene * fscene = qobject_cast<FGraphicsScene *>(scene());
106 if (fscene != NULL && fscene->displayHandles()) {
107 QGraphicsPixmapItem::paint(painter, option, widget);
108 }
109 }
110 }
111
scaling()112 bool ResizeHandle::scaling() {
113 return (this->flags() & QGraphicsItem::ItemIgnoresTransformations) && (scene() != NULL);
114 }
115
116