]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/onmonitorcornersitem.cpp
Monitorscene control widget:
[kdenlive] / src / onmonitoritems / onmonitorcornersitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "onmonitorcornersitem.h"
21 #include "kdenlivesettings.h"
22
23 #include <QGraphicsSceneMouseEvent>
24 #include <QPainter>
25 #include <QStyleOptionGraphicsItem>
26 #include <QCursor>
27
28 OnMonitorCornersItem::OnMonitorCornersItem(MonitorScene* scene, QGraphicsItem* parent) :
29         AbstractOnMonitorItem(scene),
30         QGraphicsPolygonItem(parent)
31 {
32     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
33
34     QPen framepen(Qt::SolidLine);
35     framepen.setColor(Qt::yellow);
36     setPen(framepen);
37     setBrush(Qt::NoBrush);
38 }
39
40 cornersActions OnMonitorCornersItem::getMode(QPoint pos)
41 {
42     QPainterPath mouseArea;
43     pos = mapFromScene(pos).toPoint();
44     mouseArea.addRect(pos.x() - 6, pos.y() - 6, 12, 12);
45     if (mouseArea.contains(polygon().at(0)))
46         return Corner1;
47     else if (mouseArea.contains(polygon().at(1)))
48         return Corner2;
49     else if (mouseArea.contains(polygon().at(2)))
50         return Corner3;
51     else if (mouseArea.contains(polygon().at(3)))
52         return Corner4;
53     else
54         return NoAction;
55 }
56
57 void OnMonitorCornersItem::slotMousePressed(QGraphicsSceneMouseEvent* event)
58 {
59     event->accept();
60
61     if (!isEnabled())
62         return;
63
64     m_mode = getMode(event->scenePos().toPoint());
65 }
66
67 void OnMonitorCornersItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
68 {
69     event->accept();
70
71     if (!isEnabled()) {
72         emit requestCursor(QCursor(Qt::ArrowCursor));
73         return;
74     }
75
76     /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
77      *   event->accept();
78      *   return;
79     }*/
80
81     if (event->buttons() & Qt::LeftButton) {
82         QPoint mousePos = mapFromScene(event->scenePos()).toPoint();
83         QPolygon p = polygon().toPolygon();
84         switch (m_mode) {
85         case Corner1:
86             p.replace(0, mousePos);
87             m_modified = true;
88             break;
89         case Corner2:
90             p.replace(1, mousePos);
91             m_modified = true;
92             break;
93         case Corner3:
94             p.replace(2, mousePos);
95             m_modified = true;
96             break;
97         case Corner4:
98             p.replace(3, mousePos);
99             m_modified = true;
100             break;
101         default:
102             break;
103         }
104         setPolygon(p);
105     } else {
106         switch (getMode(event->scenePos().toPoint())) {
107         case NoAction:
108             emit requestCursor(QCursor(Qt::ArrowCursor));
109             break;
110         default:
111             emit requestCursor(QCursor(Qt::OpenHandCursor));
112             break;
113         }
114     }
115     if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
116         emit actionFinished();
117         m_modified = false;
118     }
119 }
120
121 void OnMonitorCornersItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
122 {
123     if (KdenliveSettings::onmonitoreffects_cornersshowlines())
124         QGraphicsPolygonItem::paint(painter, option, widget);
125
126     painter->setRenderHint(QPainter::Antialiasing);
127     painter->setBrush(QBrush(Qt::yellow));
128     double handleSize = 4 / painter->matrix().m11();
129     painter->drawEllipse(polygon().at(0), handleSize, handleSize);
130     painter->drawEllipse(polygon().at(1), handleSize, handleSize);
131     painter->drawEllipse(polygon().at(2), handleSize, handleSize);
132     painter->drawEllipse(polygon().at(3), handleSize, handleSize);
133 }
134
135 #include "onmonitorcornersitem.moc"