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) {
77 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6) {
78 resizeMode = BottomLeft;
79 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
80 resizeMode = TopRight;
81 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
82 resizeMode = BottomRight;
83 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3) {
85 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3) {
87 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3) {
89 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3) {
94 if (!hasSelected) 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.setTopLeft(newpoint);
120 newrect.setBottomLeft(newpoint);
123 newrect.setTopRight(newpoint);
126 newrect.setBottomRight(newpoint);
129 newrect.setLeft(newpoint.x());
132 newrect.setRight(newpoint.x());
135 newrect.setTop(newpoint.y());
138 newrect.setBottom(newpoint.y());
141 QPointF diff = e->scenePos() - m_clickPoint;
142 m_clickPoint = e->scenePos();
143 gi->moveBy(diff.x(), diff.y());
146 gi->setRect(newrect);
147 //gi->setPos(m_selectedItem->scenePos());
148 /*if (resizeMode == NoResize) {
149 QGraphicsScene::mouseMoveEvent(e);
152 } else if (m_selectedItem->type() == 8) {
153 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
154 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
155 QGraphicsScene::mouseMoveEvent(e);
158 QPointF diff = e->scenePos() - m_clickPoint;
159 m_clickPoint = e->scenePos();
160 m_selectedItem->moveBy(diff.x(), diff.y());
164 QPointF p = e->scenePos();
166 resizeMode = NoResize;
168 foreach(QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
170 if (g->type() == 3) {
172 QGraphicsRectItem *gi = (QGraphicsRectItem*)g;
173 QRectF r = gi->rect();
174 r.translate(gi->scenePos());
176 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6) {
177 setCursor(QCursor(Qt::SizeFDiagCursor));
178 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6) {
179 setCursor(QCursor(Qt::SizeBDiagCursor));
180 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
181 setCursor(QCursor(Qt::SizeBDiagCursor));
182 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
183 setCursor(QCursor(Qt::SizeFDiagCursor));
184 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3) {
185 setCursor(Qt::SizeHorCursor);
186 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3) {
187 setCursor(Qt::SizeHorCursor);
188 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3) {
189 setCursor(Qt::SizeVerCursor);
190 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3) {
191 setCursor(Qt::SizeVerCursor);
192 } else setCursor(QCursor(Qt::ArrowCursor));
196 QGraphicsScene::mouseMoveEvent(e);
200 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
201 QList<QGraphicsView*> viewlist = views();
202 kDebug() << wheelEvent->delta() << " " << zoom;
204 if (viewlist.size() > 0) {
205 if (wheelEvent->delta() < 0 && zoom < 20.0) {
208 } else if (wheelEvent->delta() > 0 && zoom > .05) {
212 //viewlist[0]->resetTransform();
213 //viewlist[0]->scale(zoom, zoom);
217 void GraphicsSceneRectMove::setScale(double s) {
218 QList<QGraphicsView*> viewlist = views();
219 if (viewlist.size() > 0) {
220 viewlist[0]->scale(s, s);
225 void GraphicsSceneRectMove::setCursor(QCursor c) {
226 QList<QGraphicsView*> l = views();
227 foreach(QGraphicsView* v, l) {