]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
title improvements (bold, italic, move items with arrow keys)
[kdenlive] / src / graphicsscenerectmove.cpp
1
2 #include <KDebug>
3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
6 #include <QCursor>
7 #include <QList>
8 #include <QKeyEvent>
9
10 #include "graphicsscenerectmove.h"
11
12 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize) {
13     //grabMouse();
14     zoom = 1.0;
15 }
16
17 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) {
18     m_selectedItem = item;
19     item->setSelected(true);
20 }
21
22 //virtual
23 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent) {
24     if (m_selectedItem == NULL) {
25         QGraphicsScene::keyPressEvent(keyEvent);
26         return;
27     }
28     int diff = 1;
29     if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
30     switch (keyEvent->key()) {
31     case Qt::Key_Left:
32         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
33         break;
34     case Qt::Key_Right:
35         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
36         break;
37     case Qt::Key_Up:
38         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
39         break;
40     case Qt::Key_Down:
41         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
42         break;
43     default:
44         QGraphicsScene::keyPressEvent(keyEvent);
45     }
46     emit itemMoved();
47 }
48
49 //virtual
50 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
51     QPointF p = e->scenePos();
52     p += QPoint(-2, -2);
53     resizeMode = NoResize;
54     m_selectedItem = NULL;
55     QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
56     if (g) {
57         if (g->type() == 8) {
58             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
59             t->setTextInteractionFlags(Qt::TextEditorInteraction);
60         }
61     }
62     QGraphicsScene::mouseDoubleClickEvent(e);
63 }
64
65 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
66     QPointF p = e->scenePos();
67     p += QPoint(-2, -2);
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()) {
75             hasSelected = true;
76             item = g;
77             break;
78         }
79     }
80     if (item == NULL) {
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);
85         }
86         m_selectedItem = NULL;
87         foreach(QGraphicsItem* g, list) {
88             if (g->zValue() > -1000) {
89                 item = g;
90                 break;
91             }
92         }
93     }
94     if (item != NULL) {
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);
101                 return;
102             }
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) {
117                 resizeMode = Left;
118             } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
119                 resizeMode = Right;
120             } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
121                 resizeMode = Up;
122             } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
123                 resizeMode = Down;
124             }
125         }
126     }
127     QGraphicsScene::mousePressEvent(e);
128
129     kDebug() << "//////  MOUSE CLICK, RESIZE MODE: " << resizeMode;
130
131 }
132
133 //virtual
134 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent* e) {
135     //m_selectedItem = NULL;
136 }
137
138
139 //virtual
140 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
141
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) {
149             case TopLeft:
150                 newrect.setBottomRight(newrect.bottomRight() + gi->pos() - newpoint);
151                 gi->setPos(newpoint);
152                 break;
153             case BottomLeft:
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()));
156                 break;
157             case TopRight:
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()));
160                 break;
161             case BottomRight:
162                 newrect.setBottomRight(newpoint - gi->pos());
163                 break;
164             case Left:
165                 newrect.setRight(gi->pos().x() + newrect.width() - newpoint.x());
166                 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
167                 break;
168             case Right:
169                 newrect.setRight(newpoint.x() - gi->pos().x());
170                 break;
171             case Up:
172                 newrect.setBottom(gi->pos().y() + newrect.bottom() - newpoint.y());
173                 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
174                 break;
175             case Down:
176                 newrect.setBottom(newpoint.y() - gi->pos().y());
177                 break;
178             default:
179                 QPointF diff = e->scenePos() - m_clickPoint;
180                 m_clickPoint = e->scenePos();
181                 gi->moveBy(diff.x(), diff.y());
182                 break;
183             }
184             gi->setRect(newrect);
185             //gi->setPos(m_selectedItem->scenePos());
186             /*if (resizeMode == NoResize) {
187                 QGraphicsScene::mouseMoveEvent(e);
188                 return;
189             }*/
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);
194                 return;
195             }
196             QPointF diff = e->scenePos() - m_clickPoint;
197             m_clickPoint = e->scenePos();
198             m_selectedItem->moveBy(diff.x(), diff.y());
199         }
200         emit itemMoved();
201     } else {
202
203         QPointF p = e->scenePos();
204         p += QPoint(-2, -2);
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());
212                 itemFound = true;
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));
230                 break;
231             }
232             if (!itemFound) setCursor(QCursor(Qt::ArrowCursor));
233         }
234         QGraphicsScene::mouseMoveEvent(e);
235     }
236 }
237
238 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
239     QList<QGraphicsView*> viewlist = views();
240     //kDebug() << wheelEvent->delta() << " " << zoom;
241     double scale = 1.0;
242     if (viewlist.size() > 0) {
243         if (wheelEvent->delta() < 0) emit sceneZoom(true);
244         else emit sceneZoom(false);
245     }
246 }
247
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);
254         zoom = zoom * s;
255     }
256     //kDebug()<<"//////////  ZOOM: "<<zoom;
257 }
258
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);
264         zoom = s;
265     }
266
267     //kDebug()<<"//////////  ZOOM: "<<zoom;
268 }
269
270 void GraphicsSceneRectMove::setCursor(QCursor c) {
271     QList<QGraphicsView*> l = views();
272     foreach(QGraphicsView* v, l) {
273         v->setCursor(c);
274     }
275 }