1 #pragma once
2 
3 #ifndef COLORFIELD_H
4 #define COLORFIELD_H
5 
6 #ifdef _MSC_VER
7 #pragma warning(disable : 4251)
8 #endif
9 
10 #include "tcommon.h"
11 #include "traster.h"
12 #include "toonzqt/intfield.h"
13 #include "tspectrum.h"
14 #include "tcolorstyles.h"
15 
16 #include <QWidget>
17 
18 #undef DVAPI
19 #undef DVVAR
20 #ifdef TOONZQT_EXPORTS
21 #define DVAPI DV_EXPORT_API
22 #define DVVAR DV_EXPORT_VAR
23 #else
24 #define DVAPI DV_IMPORT_API
25 #define DVVAR DV_IMPORT_VAR
26 #endif
27 
28 // forward declaration
29 class QSlider;
30 class QImage;
31 class QPixmap;
32 class TCleanupStyle;
33 class TPaletteHandle;
34 
35 //=============================================================================
36 
37 namespace DVGui {
38 
39 //=============================================================================
40 // StyleSample
41 //-----------------------------------------------------------------------------
42 
43 class DVAPI StyleSample final : public QWidget {
44   Q_OBJECT
45   QImage m_samplePixmap;
46   TRaster32P m_bgRas;
47   TColorStyle *m_style;  // owner
48   bool m_clickEnabled;
49   bool m_drawEnable;
50   TPixel m_chessColor1;
51   TPixel m_chessColor2;
52 
53   bool m_isEditing;
54 
55 public:
56   StyleSample(QWidget *parent, int sizeX, int sizeY);
57   ~StyleSample();
58 
enableClick(bool on)59   void enableClick(bool on) { m_clickEnabled = on; }
60 
61   void setStyle(TColorStyle &style);
62   TColorStyle *getStyle() const;
63 
64   void setColor(const TPixel32 &color);
65   void setChessboardColors(const TPixel32 &col1, const TPixel32 &col2);
66 
setIsEditing(bool isEditing)67   void setIsEditing(bool isEditing) {
68     m_isEditing = isEditing;
69     update();
70   }
isEditing()71   bool isEditing() const { return m_isEditing; }
72 
setEnable(bool drawEnable)73   void setEnable(bool drawEnable) { m_drawEnable = drawEnable; }
isEnable()74   bool isEnable() const { return m_drawEnable; }
75 
76 protected:
77   void paintEvent(QPaintEvent *event) override;
78   void mousePressEvent(QMouseEvent *) override;
79   void mouseDoubleClickEvent(QMouseEvent *event) override;
80 
81 signals:
82   void clicked(const TColorStyle &style);
83 };
84 
85 //=============================================================================
86 // ChannelField
87 //-----------------------------------------------------------------------------
88 
89 class DVAPI ChannelField final : public QWidget {
90   Q_OBJECT
91 
92   DVGui::IntLineEdit *m_channelEdit;
93   QSlider *m_channelSlider;
94   int m_maxValue;
95 
96 public:
97   ChannelField(QWidget *parent = 0, const QString &string = "", int value = 0,
98                int maxValue = 255, bool horizontal = false, int labelWidth = 13,
99                int sliderWidth = -1);
100 
~ChannelField()101   ~ChannelField() {}
102 
103   void setChannel(int value);
104   int getChannel();
105 
106 signals:
107   void valueChanged(int value, bool isDragging);
108 
109 protected slots:
110   void onSliderChanged(int value);
111   void onSliderReleased();
112   void onEditChanged(const QString &str);
113   void onEditFinished();
114 };
115 
116 //=============================================================================
117 // ColorField
118 //-----------------------------------------------------------------------------
119 
120 class DVAPI ColorField final : public QWidget {
121   Q_OBJECT
122 
123   StyleSample *m_colorSample;
124   ChannelField *m_redChannel;
125   ChannelField *m_greenChannel;
126   ChannelField *m_blueChannel;
127   ChannelField *m_alphaChannel;
128 
129   TPixel32 m_color;
130 
131   //! If it is true editing changed are notified, setIsEditing emit
132   //! editingChanged signal.
133   bool m_notifyEditingChange;
134   bool m_useStyleEditor;
135 
136 public:
137   class ColorFieldEditorController {
138   public:
ColorFieldEditorController()139     ColorFieldEditorController() {}
~ColorFieldEditorController()140     virtual ~ColorFieldEditorController() {}
edit(DVGui::ColorField * colorField)141     virtual void edit(DVGui::ColorField *colorField){};
hide()142     virtual void hide(){};
143   };
144 
145   static ColorFieldEditorController *m_editorController;
146 
147   ColorField(QWidget *parent = 0, bool isAlphaActive = true,
148              TPixel32 color = TPixel32(0, 0, 0, 255), int squareSize = 40,
149              bool useStyleEditor = true, int sliderWidth = -1);
150 
~ColorField()151   ~ColorField() {}
152 
153   void setColor(const TPixel32 &color);
getColor()154   TPixel32 getColor() const { return m_color; }
155   void setChessboardColors(const TPixel32 &col1, const TPixel32 &col2);
156 
157   static void setEditorController(ColorFieldEditorController *editorController);
158   static ColorFieldEditorController *getEditorController();
159 
notifyColorChanged(const TPixel32 & color,bool isDragging)160   void notifyColorChanged(const TPixel32 &color, bool isDragging) {
161     emit colorChanged(color, isDragging);
162   }
163 
setEditingChangeNotified(bool notify)164   void setEditingChangeNotified(bool notify) { m_notifyEditingChange = notify; }
165 
setIsEditing(bool isEditing)166   void setIsEditing(bool isEditing) {
167     assert(m_colorSample);
168     m_colorSample->setIsEditing(isEditing);
169     if (m_notifyEditingChange) emit editingChanged(getColor(), isEditing);
170   }
isEditing()171   bool isEditing() const {
172     assert(m_colorSample);
173     return m_colorSample->isEditing();
174   }
175 
176   void hideChannelsFields(bool hide);
177   void setAlphaActive(bool active);
178 
179 protected:
180   void updateChannels();
181   void mousePressEvent(QMouseEvent *event) override;
182   void mouseDoubleClickEvent(QMouseEvent *event) override;
183   void hideEvent(QHideEvent *) override;
184 
185 protected slots:
186   void onRedChannelChanged(int value, bool isDragging);
187   void onGreenChannelChanged(int value, bool isDragging);
188   void onBlueChannelChanged(int value, bool isDragging);
189   void onAlphaChannelChanged(int value, bool isDragging);
190 
191 signals:
192   void editingChanged(const TPixel32 &, bool isEditing);
193   void colorChanged(const TPixel32 &, bool isDragging);
194 };
195 
196 //=============================================================================
197 // CleanupColorField
198 //-----------------------------------------------------------------------------
199 
200 class DVAPI CleanupColorField final : public QWidget {
201   Q_OBJECT
202 
203   TPaletteHandle *m_ph;
204   StyleSample *m_colorSample;
205   ChannelField *m_brightnessChannel;
206   ChannelField *m_contrastChannel;
207   ChannelField *m_hRangeChannel;
208   ChannelField *m_lineWidthChannel;
209   ChannelField *m_cThresholdChannel;
210   ChannelField *m_wThresholdChannel;
211 
212   TColorStyleP m_style;
213   TCleanupStyle *m_cleanupStyle;
214 
215   bool m_greyMode;
216 
217   //! If it is true editing changed are notified, setIsEditing emit
218   //! editingChanged signal.
219   bool m_notifyEditingChange;
220 
221 public:
222   class CleanupColorFieldEditorController {
223   public:
CleanupColorFieldEditorController()224     CleanupColorFieldEditorController() {}
~CleanupColorFieldEditorController()225     virtual ~CleanupColorFieldEditorController() {}
226 
edit(DVGui::CleanupColorField * colorField)227     virtual void edit(DVGui::CleanupColorField *colorField){};
hide()228     virtual void hide(){};
229   };
230 
231   static CleanupColorFieldEditorController *m_editorController;
232 
233 public:
234   CleanupColorField(QWidget *parent, TCleanupStyle *cleanupStyle,
235                     TPaletteHandle *ph, bool greyMode);
~CleanupColorField()236   ~CleanupColorField() { getEditorController()->edit(0); }
237 
238   static void setEditorController(
239       CleanupColorFieldEditorController *editorController);
240   static CleanupColorFieldEditorController *getEditorController();
241 
setEditingChangeNotified(bool notify)242   void setEditingChangeNotified(bool notify) { m_notifyEditingChange = notify; }
243 
isEditing()244   bool isEditing() const {
245     assert(m_colorSample);
246     return m_colorSample->isEditing();
247   }
setIsEditing(bool isEditing)248   void setIsEditing(bool isEditing) {
249     assert(m_colorSample);
250     m_colorSample->setIsEditing(isEditing);
251     if (m_notifyEditingChange) emit editingChanged(getColor(), isEditing);
252   }
253 
254   void setColor(const TPixel32 &color);
255   TPixel32 getColor() const;
256   void updateColor();
257 
258   void setOutputColor(const TPixel32 &color);
259   TPixel32 getOutputColor() const;
260 
getStyle()261   TColorStyle *getStyle() { return (TColorStyle *)m_cleanupStyle; }
262   void setStyle(TColorStyle *style);
263 
264   void setContrastEnabled(bool enable);
265 
266 protected:
267   void mousePressEvent(QMouseEvent *event) override;
268   void mouseDoubleClickEvent(QMouseEvent *event) override;
269   void hideEvent(QHideEvent *) override;
270 
271 protected slots:
272 
273   void onBrightnessChannelChanged(int value, bool dragging);
274   void onContrastChannelChanged(int value, bool dragging);
275   void onCThresholdChannelChanged(int value, bool dragging);
276   void onWThresholdChannelChanged(int value, bool dragging);
277   void onHRangeChannelChanged(int value, bool dragging);
278   void onLineWidthChannelChanged(int value, bool dragging);
279 
280 signals:
281 
282   void editingChanged(const TPixel32 &, bool isEditing);
283   void StyleSelected(TCleanupStyle *);
284 };
285 
286 //-----------------------------------------------------------------------------
287 }  // namespace DVGui
288 //-----------------------------------------------------------------------------
289 
290 #endif  // COLORFIELD_H
291