1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef SPLINEEDITOR_H
5#define SPLINEEDITOR_H
6
7#include <QWidget>
8#include <QMenu>
9#include <QAction>
10#include <QScrollArea>
11
12#include <QEasingCurve>
13#include <QHash>
14
15class SegmentProperties;
16
17class SplineEditor : public QWidget
18{
19 Q_OBJECT
20
21 Q_PROPERTY(QEasingCurve easingCurve READ easingCurve WRITE setEasingCurve NOTIFY easingCurveChanged);
22
23public:
24 explicit SplineEditor(QWidget *parent = nullptr);
25 QString generateCode();
26 QStringList presetNames() const;
27 QWidget *pointListWidget();
28
29 void setControlPoint(int index, const QPointF &point)
30 {
31 m_controlPoints[index] = point;
32 update();
33 }
34
35 void setSmooth(int index, bool smooth)
36 {
37 m_smoothAction->setChecked(smooth);
38 smoothPoint(index: index * 3 + 2);
39 //update();
40 }
41
42Q_SIGNALS:
43 void easingCurveChanged();
44 void easingCurveCodeChanged(const QString &code);
45
46
47public Q_SLOTS:
48 void setEasingCurve(const QEasingCurve &easingCurve);
49 void setPreset(const QString &name);
50 void setEasingCurve(const QString &code);
51
52protected:
53 void paintEvent(QPaintEvent *) override;
54 void mousePressEvent(QMouseEvent *) override;
55 void mouseMoveEvent(QMouseEvent *) override;
56 void mouseReleaseEvent(QMouseEvent *) override;
57#if QT_CONFIG(contextmenu)
58 void contextMenuEvent(QContextMenuEvent *) override;
59#endif // contextmenu
60
61 void invalidate();
62 void invalidateSmoothList();
63 void invalidateSegmentProperties();
64
65 QEasingCurve easingCurve() const
66 { return m_easingCurve; }
67
68 QHash<QString, QEasingCurve> presets() const;
69
70private:
71 int findControlPoint(const QPoint &point);
72 bool isSmooth(int i) const;
73
74 void smoothPoint( int index);
75 void cornerPoint( int index);
76 void deletePoint(int index);
77 void addPoint(const QPointF point);
78
79 void initPresets();
80
81 void setupPointListWidget();
82
83 bool isControlPointSmooth(int i) const;
84
85 QEasingCurve m_easingCurve;
86 QVector<QPointF> m_controlPoints;
87 QVector<bool> m_smoothList;
88 int m_numberOfSegments;
89 int m_activeControlPoint;
90 bool m_mouseDrag;
91 QPoint m_mousePress;
92 QHash<QString, QEasingCurve> m_presets;
93
94 QMenu *m_pointContextMenu;
95 QMenu *m_curveContextMenu;
96 QAction *m_deleteAction;
97 QAction *m_smoothAction;
98 QAction *m_cornerAction;
99 QAction *m_addPoint;
100
101 QScrollArea *m_pointListWidget;
102
103 QList<SegmentProperties*> m_segmentProperties;
104 bool m_block;
105};
106
107#endif // SPLINEEDITOR_H
108

source code of qtdeclarative/tools/qmleasing/splineeditor.h