3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
9 #include <QApplication>
11 #include "graphicsscenerectmove.h"
13 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize), m_tool(TITLE_RECTANGLE) {
16 setBackgroundBrush(QBrush(QColor(0, 0, 0, 0)));
19 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) {
21 m_selectedItem = item;
22 item->setSelected(true);
26 TITLETOOL GraphicsSceneRectMove::tool() {
30 void GraphicsSceneRectMove::setTool(TITLETOOL tool) {
34 setCursor(Qt::CrossCursor);
37 setCursor(Qt::IBeamCursor);
40 setCursor(Qt::ArrowCursor);
45 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent) {
46 if (m_selectedItem == NULL) {
47 QGraphicsScene::keyPressEvent(keyEvent);
51 if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
52 switch (keyEvent->key()) {
54 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
58 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
62 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
66 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
70 case Qt::Key_Backspace:
71 delete m_selectedItem;
72 m_selectedItem = NULL;
73 emit selectionChanged();
76 QGraphicsScene::keyPressEvent(keyEvent);
81 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
82 QPointF p = e->scenePos();
84 resizeMode = NoResize;
85 m_selectedItem = NULL;
86 QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
89 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
90 t->setTextInteractionFlags(Qt::TextEditorInteraction);
93 QGraphicsScene::mouseDoubleClickEvent(e);
96 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
97 if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
98 emit actionFinished();
99 QGraphicsScene::mouseReleaseEvent(e);
102 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
103 QPointF p = e->scenePos();
105 resizeMode = NoResize;
106 QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
107 QGraphicsItem *item = NULL;
108 bool hasSelected = false;
110 if (m_tool == TITLE_SELECT) {
111 foreach(QGraphicsItem* g, list) {
112 kDebug() << " - - CHECKING ITEM Z:" << g->zValue() << ", TYPE: " << g->type();
113 // check is there is a selected item in list
114 if (g->zValue() > -1000 && g->isSelected()) {
121 if (m_selectedItem && m_selectedItem->type() == 8) {
122 // disable text editing
123 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
124 t->setTextInteractionFlags(Qt::NoTextInteraction);
126 m_selectedItem = NULL;
127 foreach(QGraphicsItem* g, list) {
128 if (g->zValue() > -1000) {
135 m_clickPoint = e->scenePos();
136 m_selectedItem = item;
137 kDebug() << "///////// ITEM TYPE: " << item->type();
138 if (item->type() == 8) {
139 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
140 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
141 QGraphicsScene::mousePressEvent(e);
144 t->setTextInteractionFlags(Qt::NoTextInteraction);
145 setCursor(Qt::ClosedHandCursor);
146 } else if (item->type() == 3) {
147 QGraphicsRectItem *gi = (QGraphicsRectItem*)item;
148 QRectF r = gi->rect();
149 r.translate(gi->scenePos());
150 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
151 resizeMode = TopLeft;
152 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
153 resizeMode = BottomLeft;
154 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
155 resizeMode = TopRight;
156 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
157 resizeMode = BottomRight;
158 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
160 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
162 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
164 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
166 } else setCursor(Qt::ClosedHandCursor);
169 QGraphicsScene::mousePressEvent(e);
170 } else if (m_tool == TITLE_RECTANGLE) {
171 m_clickPoint = e->scenePos();
172 m_selectedItem = NULL;
173 } else if (m_tool == TITLE_TEXT) {
174 m_selectedItem = addText(QString());
175 emit newText((QGraphicsTextItem *) m_selectedItem);
176 m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
177 ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
178 m_selectedItem->setPos(e->scenePos());
179 QGraphicsScene::mousePressEvent(e);
182 kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << resizeMode;
188 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
190 if (m_selectedItem && e->buttons() & Qt::LeftButton) {
191 if (m_selectedItem->type() == 3) {
192 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
193 QRectF newrect = gi->rect();
194 QPointF newpoint = e->scenePos();
195 //newpoint -= m_selectedItem->scenePos();
196 switch (resizeMode) {
198 newrect.setBottomRight(newrect.bottomRight() + gi->pos() - newpoint);
199 gi->setPos(newpoint);
202 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + gi->pos().x() - newpoint.x(), newpoint.y() - gi->pos().y()));
203 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
206 newrect.setBottomRight(QPointF(newpoint.x() - gi->pos().x(), newrect.bottom() + gi->pos().y() - newpoint.y()));
207 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
210 newrect.setBottomRight(newpoint - gi->pos());
213 newrect.setRight(gi->pos().x() + newrect.width() - newpoint.x());
214 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
217 newrect.setRight(newpoint.x() - gi->pos().x());
220 newrect.setBottom(gi->pos().y() + newrect.bottom() - newpoint.y());
221 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
224 newrect.setBottom(newpoint.y() - gi->pos().y());
227 QPointF diff = e->scenePos() - m_clickPoint;
228 m_clickPoint = e->scenePos();
229 gi->moveBy(diff.x(), diff.y());
232 gi->setRect(newrect);
233 //gi->setPos(m_selectedItem->scenePos());
234 /*if (resizeMode == NoResize) {
235 QGraphicsScene::mouseMoveEvent(e);
238 } else if (m_selectedItem->type() == 8) {
239 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
240 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
241 QGraphicsScene::mouseMoveEvent(e);
244 QPointF diff = e->scenePos() - m_clickPoint;
245 m_clickPoint = e->scenePos();
246 m_selectedItem->moveBy(diff.x(), diff.y());
249 } else if (m_tool == TITLE_SELECT) {
251 QPointF p = e->scenePos();
253 resizeMode = NoResize;
254 bool itemFound = false;
255 foreach(QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
256 if (g->type() == 3 && g->zValue() > -1000) {
257 QGraphicsRectItem *gi = (QGraphicsRectItem*)g;
258 QRectF r = gi->rect();
259 r.translate(gi->scenePos());
261 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
262 setCursor(QCursor(Qt::SizeFDiagCursor));
263 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
264 setCursor(QCursor(Qt::SizeBDiagCursor));
265 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
266 setCursor(QCursor(Qt::SizeBDiagCursor));
267 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
268 setCursor(QCursor(Qt::SizeFDiagCursor));
269 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
270 setCursor(Qt::SizeHorCursor);
271 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
272 setCursor(Qt::SizeHorCursor);
273 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
274 setCursor(Qt::SizeVerCursor);
275 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
276 setCursor(Qt::SizeVerCursor);
277 } else setCursor(Qt::OpenHandCursor);
280 if (!itemFound) setCursor(Qt::ArrowCursor);
282 QGraphicsScene::mouseMoveEvent(e);
283 } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
284 if (m_selectedItem == NULL && (m_clickPoint.toPoint() - e->scenePos().toPoint()).manhattanLength() >= QApplication::startDragDistance()) {
285 // create new rect item
286 m_selectedItem = addRect(0, 0, e->scenePos().x() - m_clickPoint.x(), e->scenePos().y() - m_clickPoint.y());
287 emit newRect((QGraphicsRectItem *) m_selectedItem);
288 m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
289 m_selectedItem->setPos(m_clickPoint);
290 resizeMode = BottomRight;
291 QGraphicsScene::mouseMoveEvent(e);
296 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
297 QList<QGraphicsView*> viewlist = views();
298 //kDebug() << wheelEvent->delta() << " " << zoom;
300 if (viewlist.size() > 0) {
301 if (wheelEvent->delta() < 0) emit sceneZoom(true);
302 else emit sceneZoom(false);
306 void GraphicsSceneRectMove::setScale(double s) {
307 if (zoom < 1.0 / 7.0 && s < 1.0) return;
308 else if (zoom > 10.0 / 7.9 && s > 1.0) return;
309 QList<QGraphicsView*> viewlist = views();
310 if (viewlist.size() > 0) {
311 viewlist[0]->scale(s, s);
314 //kDebug()<<"////////// ZOOM: "<<zoom;
317 void GraphicsSceneRectMove::setZoom(double s) {
318 QList<QGraphicsView*> viewlist = views();
319 if (viewlist.size() > 0) {
320 viewlist[0]->resetTransform();
321 viewlist[0]->scale(s, s);
325 //kDebug()<<"////////// ZOOM: "<<zoom;
328 void GraphicsSceneRectMove::setCursor(QCursor c) {
329 QList<QGraphicsView*> l = views();
330 foreach(QGraphicsView* v, l) {