File indexing completed on 2024-06-16 04:35:04

0001 /*
0002     SPDX-FileCopyrightText: 2005 Casper Boemann <cbr@boemann.dk>
0003     SPDX-FileCopyrightText: 2009 Dmitry Kazakov <dimula73@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #pragma once
0009 
0010 // Qt includes.
0011 
0012 #include <QWidget>
0013 
0014 #include "../abstractcurvewidget.h"
0015 #include "kis_cubic_curve.h"
0016 
0017 class QMouseEvent;
0018 class QPaintEvent;
0019 
0020 
0021 /**
0022  * KisCurveWidget is a widget that shows a single curve that can be edited
0023  * by the user. The user can grab the curve and move it; this creates
0024  * a new control point. Control points can be deleted by selecting a point
0025  * and pressing the delete key.
0026  *
0027  * (From: http://techbase.kde.org/Projects/Widgets_and_Classes#KisCurveWidget)
0028  * KisCurveWidget allows editing of spline based y=f(x) curves. Handy for cases
0029  * where you want the user to control such things as tablet pressure
0030  * response, color transformations, acceleration by time, aeroplane lift
0031  *by angle of attack.
0032  */
0033 class KisCurveWidget : public AbstractCurveWidget<KisCubicCurve>
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     using Point_t = QPointF;
0039 
0040     /**
0041      * Create a new curve widget with a default curve, that is a straight
0042      * line from bottom-left to top-right.
0043      */
0044     explicit KisCurveWidget(QWidget *parent = nullptr);
0045 
0046     ~KisCurveWidget() override;
0047 
0048     QSize sizeHint() const override;
0049 
0050 protected:
0051     void paintEvent(QPaintEvent *) override;
0052     void mousePressEvent(QMouseEvent *e) override;
0053     void mouseMoveEvent(QMouseEvent *e) override;
0054 
0055 public:
0056     /**
0057      * Handy function that creates new point in the middle
0058      * of the curve and sets focus on the m_intIn field,
0059      * so the user can move this point anywhere in a moment
0060      */
0061     void addPointInTheMiddle();
0062 
0063     void setCurve(KisCubicCurve &&curve);
0064 
0065     QList<QPointF> getPoints() const override;
0066 
0067 private:
0068     double io2sp(int x) const;
0069     int sp2io(double x) const;
0070     bool jumpOverExistingPoints(QPointF &pt, int skipIndex);
0071     int nearestPointInRange(QPointF pt, int wWidth, int wHeight) const;
0072 
0073     /* Dragging variables */
0074     double m_grabOffsetX;
0075     double m_grabOffsetY;
0076     double m_grabOriginalX;
0077     double m_grabOriginalY;
0078     QPointF m_draggedAwayPoint;
0079     int m_draggedAwayPointIndex;
0080 
0081     bool m_guideVisible;
0082     QColor m_colorGuide;
0083 };