1 #pragma once
2 
3 #ifndef TONECURVEFIELD_H
4 #define TONECURVEFIELD_H
5 
6 #include "tcommon.h"
7 #include "tstroke.h"
8 #include "toonzqt/histogram.h"
9 #include <QWidget>
10 
11 #undef DVAPI
12 #undef DVVAR
13 #ifdef TOONZQT_EXPORTS
14 #define DVAPI DV_EXPORT_API
15 #define DVVAR DV_EXPORT_VAR
16 #else
17 #define DVAPI DV_IMPORT_API
18 #define DVVAR DV_IMPORT_VAR
19 #endif
20 
21 // forward declaration
22 class TStroke;
23 class QPainterPath;
24 class QStackedWidget;
25 class FxHistogramRender;
26 
27 //=============================================================================
28 namespace DVGui {
29 
30 // forward declaration
31 class DoublePairField;
32 class CheckBox;
33 class DoubleLineEdit;
34 
35 //=============================================================================
36 // ChennelCurveEditor
37 //-----------------------------------------------------------------------------
38 
39 class DVAPI ChennelCurveEditor final : public QWidget {
40   Q_OBJECT
41 
42   HistogramView *m_histogramView;
43   ChannelBar *m_verticalChannelBar;
44 
45   QList<QPointF> m_points;
46   int m_currentControlPointIndex;
47 
48   Qt::MouseButton m_mouseButton;
49 
50   int m_curveHeight;
51 
52   int m_LeftRightMargin;
53   int m_TopMargin;
54   int m_BottomMargin;
55 
56   bool m_isLinear;
57 
58   QPointF m_preMousePos;
59 
60   bool m_isEnlarged;
61 
62 public:
63   ChennelCurveEditor(QWidget *parent = 0, HistogramView *histogramView = 0);
64 
65   void setPoints(QList<TPointD> points);
66   QList<TPointD> getPoints();
67 
getCurrentControlPointIndex()68   int getCurrentControlPointIndex() { return m_currentControlPointIndex; };
setCurrentControlPointIndex(int index)69   void setCurrentControlPointIndex(int index) {
70     m_currentControlPointIndex = index;
71   };
72 
73   bool eventFilter(QObject *object, QEvent *event) override;
74 
75   void setFirstLastXPosition(std::pair<double, double> values, bool isDragging);
76 
77   void setLinear(bool isLinear);
78   void moveCurrentControlPoint(QPointF delta);
79 
80   void setEnlarged(bool isEnlarged);
81   void setLabelRange(ChannelBar::Range range);
82 
83 protected:
84   QPointF viewToStrokePoint(const QPointF &p);
85   int getClosestPointIndex(const QPointF &pos, double &minDistance2) const;
86 
isCentralControlPoint(const int index)87   bool isCentralControlPoint(const int index) const { return index % 3 == 0; }
isLeftControlPoint(const int index)88   bool isLeftControlPoint(const int index) const { return index % 3 == 2; }
isRightControlPoint(const int index)89   bool isRightControlPoint(const int index) const { return index % 3 == 1; }
90 
91   void setPoint(int index, const QPointF point);
92   void movePoint(int index, const QPointF delta);
93   QPointF checkPoint(const QPointF p);
94 
95   QPointF getVisibleHandlePos(int index) const;
96 
97   void moveCentralControlPoint(int index, QPointF delta);
98   //	bool eraseControlPointWhileMove(int index, const QPointF delta);
99 
100   void addControlPoint(double percent);
101 
102   void removeControlPoint(int index);
103   void removeCurrentControlPoint();
104 
105   void selectNextControlPoint();
106   void selectPreviousControlPoint();
107 
108   QPainterPath getPainterPath();
109 
110   void paintEvent(QPaintEvent *) override;
111   void mouseMoveEvent(QMouseEvent *) override;
112   void mousePressEvent(QMouseEvent *) override;
113   void mouseReleaseEvent(QMouseEvent *) override;
114   void keyPressEvent(QKeyEvent *e) override;
115 
116   void focusInEvent(QFocusEvent *fe) override;
117   void focusOutEvent(QFocusEvent *fe) override;
118 
119 signals:
120   void focusOut();
121   void controlPointChanged(bool isDragging);
122   void controlPointAdded(int index);
123   void controlPointRemoved(int index);
124 
125   void firstLastXPostionChanged(double, double);
126   void updateCurrentPosition(int, QPointF);
127 };
128 
129 //=============================================================================
130 // ToneCurveField
131 //-----------------------------------------------------------------------------
132 
133 class DVAPI ToneCurveField final : public QWidget {
134   Q_OBJECT
135 
136   QStackedWidget *m_toneCurveStackedWidget;
137   QStackedWidget *m_sliderStackedWidget;
138   QComboBox *m_channelListChooser;
139   CheckBox *m_isLinearCheckBox, *m_isEnlargedCheckBox;
140 
141   DoubleLineEdit *m_currentInput, *m_currentOutput;
142   int m_currentPointIndex;
143   QComboBox *m_rangeMode;
144 
145 public:
146   ToneCurveField(QWidget *parent = 0, FxHistogramRender *fxHistogramRender = 0);
147 
148   void setCurrentChannel(int currentChannel);
149   ChennelCurveEditor *getChannelEditor(int channel) const;
150   ChennelCurveEditor *getCurrentChannelEditor() const;
151   DoublePairField *getCurrentSlider() const;
152 
getChannelCount()153   int getChannelCount() { return m_toneCurveStackedWidget->count(); }
154 
155   void setIsLinearCheckBox(bool isChecked);
156   bool isEnlarged();
157 
158 protected slots:
159   void sliderValueChanged(bool);
160   void onFirstLastXPostionChanged(double, double);
161   void onUpdateCurrentPosition(int, QPointF);
162   void onCurrentPointEditted();
163   void onCurrentChannelSwitched(int);
164   void onRangeModeSwitched(int);
165 
166 public slots:
167   void setLinear(bool);
168   void setEnlarged(bool);
169   void setLinearManually(bool);
170 
171 signals:
172   void currentChannelIndexChanged(int);
173   void isLinearChanged(bool);
174   void sizeChanged();
175 };
176 }  // namespace DVGui
177 
178 #endif  // TONECURVEFIELD_H
179