3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
10 #include "graphicsscenerectmove.h"
12 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize) {
17 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) {
18 m_selectedItem = item;
19 item->setSelected(true);
23 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent) {
24 if (m_selectedItem == NULL) {
25 QGraphicsScene::keyPressEvent(keyEvent);
29 if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
30 switch (keyEvent->key()) {
32 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
35 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
38 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
41 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
44 QGraphicsScene::keyPressEvent(keyEvent);
50 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
51 QPointF p = e->scenePos();
53 resizeMode = NoResize;
54 m_selectedItem = NULL;
55 QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
58 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
59 t->setTextInteractionFlags(Qt::TextEditorInteraction);
62 QGraphicsScene::mouseDoubleClickEvent(e);
65 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
66 QPointF p = e->scenePos();
68 resizeMode = NoResize;
69 QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
70 QGraphicsItem *item = NULL;
71 bool hasSelected = false;
72 foreach(QGraphicsItem* g, list) {
73 // check is there is a selected item in list
74 if (g->zValue() > -1000 && g->isSelected()) {
81 if (m_selectedItem && m_selectedItem->type() == 8) {
82 // disable text editing
83 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
84 t->setTextInteractionFlags(Qt::NoTextInteraction);
86 m_selectedItem = NULL;
87 foreach(QGraphicsItem* g, list) {
88 if (g->zValue() > -1000) {
95 m_clickPoint = e->scenePos();
96 m_selectedItem = item;
97 if (item->type() == 8) {
98 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
99 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
100 QGraphicsScene::mousePressEvent(e);
103 t->setTextInteractionFlags(Qt::NoTextInteraction);
104 } else if (item->type() == 3) {
105 QGraphicsRectItem *gi = (QGraphicsRectItem*)item;
106 QRectF r = gi->rect();
107 r.translate(gi->scenePos());
108 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
109 resizeMode = TopLeft;
110 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
111 resizeMode = BottomLeft;
112 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
113 resizeMode = TopRight;
114 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
115 resizeMode = BottomRight;
116 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
118 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
120 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
122 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
127 QGraphicsScene::mousePressEvent(e);
129 kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << resizeMode;
134 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent* e) {
135 //m_selectedItem = NULL;
140 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
142 if (m_selectedItem && e->buttons() & Qt::LeftButton) {
143 if (m_selectedItem->type() == 3) {
144 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
145 QRectF newrect = gi->rect();
146 QPointF newpoint = e->scenePos();
147 //newpoint -= m_selectedItem->scenePos();
148 switch (resizeMode) {
150 newrect.setBottomRight(newrect.bottomRight() + gi->pos() - newpoint);
151 gi->setPos(newpoint);
154 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + gi->pos().x() - newpoint.x(), newpoint.y() - gi->pos().y()));
155 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
158 newrect.setBottomRight(QPointF(newpoint.x() - gi->pos().x(), newrect.bottom() + gi->pos().y() - newpoint.y()));
159 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
162 newrect.setBottomRight(newpoint - gi->pos());
165 newrect.setRight(gi->pos().x() + newrect.width() - newpoint.x());
166 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
169 newrect.setRight(newpoint.x() - gi->pos().x());
172 newrect.setBottom(gi->pos().y() + newrect.bottom() - newpoint.y());
173 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
176 newrect.setBottom(newpoint.y() - gi->pos().y());
179 QPointF diff = e->scenePos() - m_clickPoint;
180 m_clickPoint = e->scenePos();
181 gi->moveBy(diff.x(), diff.y());
184 gi->setRect(newrect);
185 //gi->setPos(m_selectedItem->scenePos());
186 /*if (resizeMode == NoResize) {
187 QGraphicsScene::mouseMoveEvent(e);
190 } else if (m_selectedItem->type() == 8) {
191 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
192 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
193 QGraphicsScene::mouseMoveEvent(e);
196 QPointF diff = e->scenePos() - m_clickPoint;
197 m_clickPoint = e->scenePos();
198 m_selectedItem->moveBy(diff.x(), diff.y());
203 QPointF p = e->scenePos();
205 resizeMode = NoResize;
206 bool itemFound = false;
207 foreach(QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
208 if (g->type() == 3) {
209 QGraphicsRectItem *gi = (QGraphicsRectItem*)g;
210 QRectF r = gi->rect();
211 r.translate(gi->scenePos());
213 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
214 setCursor(QCursor(Qt::SizeFDiagCursor));
215 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
216 setCursor(QCursor(Qt::SizeBDiagCursor));
217 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
218 setCursor(QCursor(Qt::SizeBDiagCursor));
219 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
220 setCursor(QCursor(Qt::SizeFDiagCursor));
221 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
222 setCursor(Qt::SizeHorCursor);
223 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
224 setCursor(Qt::SizeHorCursor);
225 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
226 setCursor(Qt::SizeVerCursor);
227 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
228 setCursor(Qt::SizeVerCursor);
229 } else setCursor(QCursor(Qt::ArrowCursor));
232 if (!itemFound) setCursor(QCursor(Qt::ArrowCursor));
234 QGraphicsScene::mouseMoveEvent(e);
238 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
239 QList<QGraphicsView*> viewlist = views();
240 //kDebug() << wheelEvent->delta() << " " << zoom;
242 if (viewlist.size() > 0) {
243 if (wheelEvent->delta() < 0) emit sceneZoom(true);
244 else emit sceneZoom(false);
248 void GraphicsSceneRectMove::setScale(double s) {
249 if (zoom < 1.0 / 7.0 && s < 1.0) return;
250 else if (zoom > 10.0 / 7.9 && s > 1.0) return;
251 QList<QGraphicsView*> viewlist = views();
252 if (viewlist.size() > 0) {
253 viewlist[0]->scale(s, s);
256 //kDebug()<<"////////// ZOOM: "<<zoom;
259 void GraphicsSceneRectMove::setZoom(double s) {
260 QList<QGraphicsView*> viewlist = views();
261 if (viewlist.size() > 0) {
262 viewlist[0]->resetTransform();
263 viewlist[0]->scale(s, s);
267 //kDebug()<<"////////// ZOOM: "<<zoom;
270 void GraphicsSceneRectMove::setCursor(QCursor c) {
271 QList<QGraphicsView*> l = views();
272 foreach(QGraphicsView* v, l) {