]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplineeditor.h
Bezier spline editor: Fix inaccurate placement of handles
[kdenlive] / src / beziercurve / beziersplineeditor.h
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *   This file is part of Kdenlive (www.kdenlive.org).                     *
4  *                                                                         *
5  *   Kdenlive is free software: you can redistribute it and/or modify      *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation, either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   Kdenlive is distributed in the hope that it will be useful,           *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with Kdenlive.  If not, see <http://www.gnu.org/licenses/>.     *
17  ***************************************************************************/
18
19 #ifndef BEZIERSPLINEEDITOR_H
20 #define BEZIERSPLINEEDITOR_H
21
22 #include "cubicbezierspline.h"
23
24 #include <QtCore>
25 #include <QWidget>
26
27 class BezierSplineEditor : public QWidget
28 {
29     Q_OBJECT
30
31 public:
32     BezierSplineEditor(QWidget* parent = 0);
33     virtual ~BezierSplineEditor();
34
35     CubicBezierSpline spline();
36     void setSpline(const CubicBezierSpline &spline);
37
38     /** @brief Returns the selected point or else BPoint. */
39     BPoint getCurrentPoint();
40
41     /** @brief Replaces current point with @param p (index stays the same). */
42     void updateCurrentPoint(const BPoint &p);
43
44     /** @brief Number of lines used in grid. */
45     int gridLines();
46
47     /** @brief Sets the number of grid lines to draw (in one direction) to @param lines. */
48     void setGridLines(int lines);
49
50     /** @brief Sets the background pixmap to @param pixmap. */
51     void setPixmap(const QPixmap &pixmap);
52
53     /** @brief Sets the property showAllHandles to @param show.
54      *
55      * showAllHandles: Whether to show only handles for the selected point for all points. */
56     void setShowAllHandles(bool show);
57
58 public slots:
59     void slotZoomIn();
60     void slotZoomOut();
61
62 protected:
63     void paintEvent(QPaintEvent *event);
64     void mousePressEvent(QMouseEvent *event);
65     void mouseReleaseEvent(QMouseEvent * event);
66     void mouseMoveEvent(QMouseEvent * event);
67     void leaveEvent(QEvent *event);
68     void resizeEvent(QResizeEvent *event);
69
70 private:
71     CubicBezierSpline m_spline;
72     enum modes { ModeDrag, ModeNormal };
73     enum point_types { PTypeH1, PTypeP, PTypeH2 };
74     modes m_mode;
75     int m_zoomLevel;
76     int m_gridLines;
77     /** Whether to show handles for all points or only for the selected one. */
78     bool m_showAllHandles;
79     /** Background */
80     QPixmap m_pixmap;
81     /** A copy of m_pixmap but scaled to fit the size of the edit region */
82     QPixmap *m_pixmapCache;
83     /** Whether we have to regenerate the pixmap cache because the pixmap or the size of the edit region changed. */
84     bool m_pixmapIsDirty;
85
86     int m_currentPointIndex;
87     point_types m_currentPointType;
88     double m_grabOffsetX;
89     double m_grabOffsetY;
90     /** selected point before it was modified by dragging (at the time of the mouse press) */
91     BPoint m_grabPOriginal;
92     /** point with the index currentPointIndex + 1 at the time of the mouse press */
93     BPoint m_grabPNext;
94     /** point with the index currentPointIndex - 1 at the time of the mouse press */
95     BPoint m_grabPPrevious;
96
97     /** @brief Finds the point nearest to @param p and returns it's index.
98      * @param sel Is filled with the type of the closest point (h1, p, h2)
99      *
100      * If no point is near enough -1 is returned. */
101     int nearestPointInRange(QPointF p, int wWidth, int wHeight, point_types *sel);
102
103 signals:
104     void modified();
105     void currentPoint(const BPoint &p);
106 };
107
108 #endif