3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
9 #include "graphicsscenerectmove.h"
11 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize) {
17 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
18 QPointF p = e->scenePos();
20 resizeMode = NoResize;
21 m_selectedItem = NULL;
22 QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
25 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
26 t->setTextInteractionFlags(Qt::TextEditorInteraction);
29 QGraphicsScene::mouseDoubleClickEvent(e);
32 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
33 QPointF p = e->scenePos();
35 resizeMode = NoResize;
36 QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
37 QGraphicsItem *item = NULL;
38 bool hasSelected = false;
39 foreach(QGraphicsItem* g, list) {
40 // check is there is a selected item in list
41 if (g->zValue() > -1000 && g->isSelected()) {
48 if (m_selectedItem && m_selectedItem->type() == 8) {
49 // disable text editing
50 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
51 t->setTextInteractionFlags(Qt::NoTextInteraction);
53 m_selectedItem = NULL;
54 foreach(QGraphicsItem* g, list) {
55 if (g->zValue() > -1000) {
62 m_clickPoint = e->scenePos();
63 m_selectedItem = item;
64 if (item->type() == 8) {
65 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
66 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
67 QGraphicsScene::mousePressEvent(e);
70 t->setTextInteractionFlags(Qt::NoTextInteraction);
71 } else if (item->type() == 3) {
72 QGraphicsRectItem *gi = (QGraphicsRectItem*)item;
73 QRectF r = gi->rect();
74 r.translate(gi->scenePos());
75 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
77 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
78 resizeMode = BottomLeft;
79 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
80 resizeMode = TopRight;
81 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
82 resizeMode = BottomRight;
83 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
85 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
87 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
89 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
94 QGraphicsScene::mousePressEvent(e);
96 kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << resizeMode;
101 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent* e) {
102 //m_selectedItem = NULL;
107 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
109 if (m_selectedItem && e->buttons() & Qt::LeftButton) {
110 if (m_selectedItem->type() == 3) {
111 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
112 QRectF newrect = gi->rect();
113 QPointF newpoint = e->scenePos();
114 //newpoint -= m_selectedItem->scenePos();
115 switch (resizeMode) {
117 newrect.setBottomRight(newrect.bottomRight() + gi->pos() - newpoint);
118 gi->setPos(newpoint);
121 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + gi->pos().x() - newpoint.x(), newpoint.y() - gi->pos().y()));
122 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
125 newrect.setBottomRight(QPointF(newpoint.x() - gi->pos().x(), newrect.bottom() + gi->pos().y() - newpoint.y()));
126 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
129 newrect.setBottomRight(newpoint - gi->pos());
132 newrect.setRight(gi->pos().x() + newrect.width() - newpoint.x());
133 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
136 newrect.setRight(newpoint.x() - gi->pos().x());
139 newrect.setBottom(gi->pos().y() + newrect.bottom() - newpoint.y());
140 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
143 newrect.setBottom(newpoint.y() - gi->pos().y());
146 QPointF diff = e->scenePos() - m_clickPoint;
147 m_clickPoint = e->scenePos();
148 gi->moveBy(diff.x(), diff.y());
151 gi->setRect(newrect);
152 //gi->setPos(m_selectedItem->scenePos());
153 /*if (resizeMode == NoResize) {
154 QGraphicsScene::mouseMoveEvent(e);
157 } else if (m_selectedItem->type() == 8) {
158 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
159 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
160 QGraphicsScene::mouseMoveEvent(e);
163 QPointF diff = e->scenePos() - m_clickPoint;
164 m_clickPoint = e->scenePos();
165 m_selectedItem->moveBy(diff.x(), diff.y());
170 QPointF p = e->scenePos();
172 resizeMode = NoResize;
173 bool itemFound = false;
174 foreach(QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
175 if (g->type() == 3) {
176 QGraphicsRectItem *gi = (QGraphicsRectItem*)g;
177 QRectF r = gi->rect();
178 r.translate(gi->scenePos());
180 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
181 setCursor(QCursor(Qt::SizeFDiagCursor));
182 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
183 setCursor(QCursor(Qt::SizeBDiagCursor));
184 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
185 setCursor(QCursor(Qt::SizeBDiagCursor));
186 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
187 setCursor(QCursor(Qt::SizeFDiagCursor));
188 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
189 setCursor(Qt::SizeHorCursor);
190 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
191 setCursor(Qt::SizeHorCursor);
192 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
193 setCursor(Qt::SizeVerCursor);
194 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
195 setCursor(Qt::SizeVerCursor);
196 } else setCursor(QCursor(Qt::ArrowCursor));
199 if (!itemFound) setCursor(QCursor(Qt::ArrowCursor));
201 QGraphicsScene::mouseMoveEvent(e);
205 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
206 QList<QGraphicsView*> viewlist = views();
207 //kDebug() << wheelEvent->delta() << " " << zoom;
209 if (viewlist.size() > 0) {
210 if (wheelEvent->delta() < 0) emit sceneZoom(true);
211 else emit sceneZoom(false);
215 void GraphicsSceneRectMove::setScale(double s) {
216 if (zoom < 1.0 / 7.0 && s < 1.0) return;
217 else if (zoom > 10.0 / 7.9 && s > 1.0) return;
218 QList<QGraphicsView*> viewlist = views();
219 if (viewlist.size() > 0) {
220 viewlist[0]->scale(s, s);
223 //kDebug()<<"////////// ZOOM: "<<zoom;
226 void GraphicsSceneRectMove::setZoom(double s) {
227 QList<QGraphicsView*> viewlist = views();
228 if (viewlist.size() > 0) {
229 viewlist[0]->resetTransform();
230 viewlist[0]->scale(s, s);
234 //kDebug()<<"////////// ZOOM: "<<zoom;
237 void GraphicsSceneRectMove::setCursor(QCursor c) {
238 QList<QGraphicsView*> l = views();
239 foreach(QGraphicsView* v, l) {