1 
2 
3 #include "toonz/tpalettehandle.h"
4 
5 #include "tundo.h"
6 #include "historytypes.h"
7 
8 //=============================================================================
9 // AutopaintToggleUndo
10 //-----------------------------------------------------------------------------
11 
12 namespace {
13 class AutopaintToggleUndo final : public TUndo {
14   TPaletteHandle *m_paletteHandle;
15   TPaletteP m_palette;
16   int m_styleId;
17   bool m_flag;
18 
19 public:
AutopaintToggleUndo(TPaletteHandle * paletteHandle,int styleId)20   AutopaintToggleUndo(TPaletteHandle *paletteHandle, int styleId)
21       : m_paletteHandle(paletteHandle)
22       , m_palette(paletteHandle->getPalette())
23       , m_styleId(styleId) {}
24 
toggleAutopaint() const25   void toggleAutopaint() const {
26     TColorStyle *s = m_palette->getStyle(m_styleId);
27     s->setFlags(s->getFlags() == 0 ? 1 : 0);
28     m_paletteHandle->notifyColorStyleChanged();
29   }
30 
undo() const31   void undo() const override { toggleAutopaint(); }
32 
redo() const33   void redo() const override { toggleAutopaint(); }
34 
onAdd()35   void onAdd() override { redo(); }
36 
getSize() const37   int getSize() const override { return sizeof(*this); }
38 
getHistoryString()39   QString getHistoryString() override {
40     return QObject::tr("Toggle Autopaint Option  Palette : %1  Style#%2")
41         .arg(QString::fromStdWString(m_palette->getPaletteName()))
42         .arg(QString::number(m_styleId));
43   }
44 
getHistoryType()45   int getHistoryType() override { return HistoryType::Palette; }
46 };
47 
48 }  // namespace
49 
50 //=============================================================================
51 // TPaletteHandle
52 //-----------------------------------------------------------------------------
53 
TPaletteHandle()54 TPaletteHandle::TPaletteHandle()
55     : m_palette(0), m_styleIndex(-1), m_styleParamIndex(-1) {
56   connectBroadcasts(this);
57 }
58 
59 //-----------------------------------------------------------------------------
60 
~TPaletteHandle()61 TPaletteHandle::~TPaletteHandle() {}
62 
63 //-----------------------------------------------------------------------------
64 
getPalette() const65 TPalette *TPaletteHandle::getPalette() const { return m_palette; }
66 
67 //-----------------------------------------------------------------------------
68 
getStyleIndex() const69 int TPaletteHandle::getStyleIndex() const { return m_styleIndex; }
70 
71 //-----------------------------------------------------------------------------
72 
getStyleParamIndex() const73 int TPaletteHandle::getStyleParamIndex() const { return m_styleParamIndex; }
74 
75 //-----------------------------------------------------------------------------
76 
getStyle() const77 TColorStyle *TPaletteHandle::getStyle() const {
78   if (!m_palette || m_styleIndex == -1) return 0;
79   return m_palette->getStyle(m_styleIndex);
80 }
81 
82 //-----------------------------------------------------------------------------
83 
connectBroadcasts(const QObject * receiver)84 bool TPaletteHandle::connectBroadcasts(const QObject *receiver) {
85   bool ret = true;
86 
87   ret = connect(this, SIGNAL(broadcastPaletteChanged()), receiver,
88                 SIGNAL(paletteChanged())) &&
89         ret;
90   ret = connect(this, SIGNAL(broadcastPaletteTitleChanged()), receiver,
91                 SIGNAL(paletteTitleChanged())) &&
92         ret;
93   ret = connect(this, SIGNAL(broadcastColorStyleSwitched()), receiver,
94                 SIGNAL(colorStyleSwitched())) &&
95         ret;
96   ret = connect(this, SIGNAL(broadcastColorStyleChanged(bool)), receiver,
97                 SIGNAL(colorStyleChanged(bool))) &&
98         ret;
99   ret = connect(this, SIGNAL(broadcastColorStyleChangedOnMouseRelease()),
100                 receiver, SIGNAL(colorStyleChangedOnMouseRelease())) &&
101         ret;
102 
103   return ret;
104 }
105 
106 //-----------------------------------------------------------------------------
107 
disconnectBroadcasts(const QObject * receiver)108 bool TPaletteHandle::disconnectBroadcasts(const QObject *receiver) {
109   bool ret = true;
110 
111   ret = disconnect(this, SIGNAL(broadcastPaletteChanged()), receiver,
112                    SIGNAL(paletteChanged())) &&
113         ret;
114   ret = disconnect(this, SIGNAL(broadcastPaletteTitleChanged()), receiver,
115                    SIGNAL(paletteTitleChanged())) &&
116         ret;
117   ret = disconnect(this, SIGNAL(broadcastColorStyleSwitched()), receiver,
118                    SIGNAL(colorStyleSwitched())) &&
119         ret;
120   ret = disconnect(this, SIGNAL(broadcastColorStyleChanged(bool)), receiver,
121                    SIGNAL(colorStyleChanged(bool))) &&
122         ret;
123   ret = disconnect(this, SIGNAL(broadcastColorStyleChangedOnMouseRelease()),
124                    receiver, SIGNAL(colorStyleChangedOnMouseRelease())) &&
125         ret;
126 
127   return ret;
128 }
129 
130 //-----------------------------------------------------------------------------
131 
setPalette(TPalette * palette,int styleIndex)132 void TPaletteHandle::setPalette(TPalette *palette, int styleIndex) {
133   if (palette) {
134     if (styleIndex < 0) {
135       styleIndex = palette->getCurrentStyleId();
136       if (!palette->getStylePage(styleIndex)) {  // in case the style is deleted
137         styleIndex = 1;
138         palette->setCurrentStyleId(styleIndex);
139       }
140     } else
141       palette->setCurrentStyleId(styleIndex);
142   }
143 
144   if (m_palette == palette)
145     setStyleIndex(styleIndex);
146   else {
147     m_palette         = palette;
148     m_styleIndex      = styleIndex;
149     m_styleParamIndex = 0;
150 
151     emit paletteSwitched();
152     // to let ToonzCheck to update the current index
153     emit broadcastColorStyleSwitched();
154   }
155 }
156 
157 //-----------------------------------------------------------------------------
158 // forceEmit flag is used in PageViewer and StylePicker tool.
159 // See the function PageViewer::setCurrentStyleIndex() in paletteviewergui.cpp
160 // Also see the function StylePickerTool::pick() in stylepickertool.cpp
161 
setStyleIndex(int index,bool forceEmit)162 void TPaletteHandle::setStyleIndex(int index, bool forceEmit) {
163   if (m_styleIndex != index || m_styleParamIndex != 0 || forceEmit) {
164     if (m_palette) m_palette->setCurrentStyleId(index);
165     m_styleIndex      = index;
166     m_styleParamIndex = 0;
167     emit broadcastColorStyleSwitched();
168   }
169 }
170 
171 //-----------------------------------------------------------------------------
172 
setStyleParamIndex(int index)173 void TPaletteHandle::setStyleParamIndex(int index) {
174   if (m_styleParamIndex != index) {
175     m_styleParamIndex = index;
176     emit broadcastColorStyleSwitched();
177   }
178 }
179 
180 //-----------------------------------------------------------------------------
181 
notifyPaletteChanged()182 void TPaletteHandle::notifyPaletteChanged() { emit broadcastPaletteChanged(); }
183 
184 //-----------------------------------------------------------------------------
185 
notifyPaletteTitleChanged()186 void TPaletteHandle::notifyPaletteTitleChanged() {
187   emit broadcastPaletteTitleChanged();
188 }
189 
190 //-----------------------------------------------------------------------------
191 
notifyColorStyleSwitched()192 void TPaletteHandle::notifyColorStyleSwitched() {
193   emit broadcastColorStyleSwitched();
194 }
195 
196 //-----------------------------------------------------------------------------
197 
notifyColorStyleChanged(bool onDragging,bool setDirtyFlag)198 void TPaletteHandle::notifyColorStyleChanged(bool onDragging,
199                                              bool setDirtyFlag) {
200   if (setDirtyFlag && getPalette() && !getPalette()->getDirtyFlag())
201     getPalette()->setDirtyFlag(true);
202 
203   emit broadcastColorStyleChanged(onDragging);
204 
205   if (!onDragging) emit broadcastColorStyleChangedOnMouseRelease();
206 }
207 
208 //-----------------------------------------------------------------------------
209 
toggleAutopaint()210 void TPaletteHandle::toggleAutopaint() {
211   int index = getStyleIndex();
212   if (index > 0) {
213     TUndoManager::manager()->add(new AutopaintToggleUndo(this, index));
214   }
215 }
216