comparison layer/SingleColourLayer.cpp @ 768:8b614632568c

SingleColourLayer: fix colour reference counting * Always unref/ref colour before/after changing. * Ref colour in constructor and unref in destructor.
author Jakob Leben <jakob.leben@gmail.com>
date Sat, 12 Apr 2014 01:07:05 -0700
parents e93c6ae12526
children 1d526ba11a24
comparison
equal deleted inserted replaced
767:e93c6ae12526 768:8b614632568c
30 SingleColourLayer::SingleColourLayer() : 30 SingleColourLayer::SingleColourLayer() :
31 m_colour(0), 31 m_colour(0),
32 m_colourExplicitlySet(false), 32 m_colourExplicitlySet(false),
33 m_defaultColourSet(false) 33 m_defaultColourSet(false)
34 { 34 {
35 // Reference current colour because setDefaulColourFor
36 // will unreference it before (possibly) changing it.
37 refColor();
35 setDefaultColourFor(0); 38 setDefaultColourFor(0);
39 }
40
41 SingleColourLayer::~SingleColourLayer()
42 {
43 unrefColor();
36 } 44 }
37 45
38 QPixmap 46 QPixmap
39 SingleColourLayer::getLayerPresentationPixmap(QSize size) const 47 SingleColourLayer::getLayerPresentationPixmap(QSize size) const
40 { 48 {
150 ColourDatabase *cdb = ColourDatabase::getInstance(); 158 ColourDatabase *cdb = ColourDatabase::getInstance();
151 159
152 int hint = -1; 160 int hint = -1;
153 bool impose = false; 161 bool impose = false;
154 if (v) { 162 if (v) {
155 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
156 m_colourRefCount[m_colour] > 0) {
157 m_colourRefCount[m_colour]--;
158 }
159 // We don't want to call this if !v because that probably 163 // We don't want to call this if !v because that probably
160 // means we're being called from the constructor, and this is 164 // means we're being called from the constructor, and this is
161 // a virtual function 165 // a virtual function
162 hint = getDefaultColourHint(dark, impose); 166 hint = getDefaultColourHint(dark, impose);
163 #ifdef DEBUG_COLOUR_SELECTION 167 #ifdef DEBUG_COLOUR_SELECTION
172 if (hint >= 0 && impose) { 176 if (hint >= 0 && impose) {
173 setBaseColour(hint); 177 setBaseColour(hint);
174 return; 178 return;
175 } 179 }
176 180
181 unrefColor();
182
177 int bestCount = 0, bestColour = -1; 183 int bestCount = 0, bestColour = -1;
178 184
179 for (int i = 0; i < cdb->getColourCount(); ++i) { 185 for (int i = 0; i < cdb->getColourCount(); ++i) {
180 186
181 int index = i; 187 int index = i;
201 207
202 #ifdef DEBUG_COLOUR_SELECTION 208 #ifdef DEBUG_COLOUR_SELECTION
203 cerr << endl; 209 cerr << endl;
204 #endif 210 #endif
205 } 211 }
206 212
207 if (bestColour < 0) m_colour = 0; 213 if (bestColour < 0) m_colour = 0;
208 else m_colour = bestColour; 214 else m_colour = bestColour;
209 215
210 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) { 216 refColor();
211 m_colourRefCount[m_colour] = 1;
212 } else {
213 m_colourRefCount[m_colour]++;
214 }
215 } 217 }
216 218
217 void 219 void
218 SingleColourLayer::setBaseColour(int colour) 220 SingleColourLayer::setBaseColour(int colour)
219 { 221 {
220 m_colourExplicitlySet = true; 222 m_colourExplicitlySet = true;
221 223
222 if (m_colour == colour) return; 224 if (m_colour == colour) return;
223 225
224 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() && 226 refColor();
225 m_colourRefCount[m_colour] > 0) {
226 m_colourRefCount[m_colour]--;
227 }
228
229 m_colour = colour; 227 m_colour = colour;
230 228 unrefColor();
231 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
232 m_colourRefCount[m_colour] = 1;
233 } else {
234 m_colourRefCount[m_colour]++;
235 }
236 229
237 flagBaseColourChanged(); 230 flagBaseColourChanged();
238 emit layerParametersChanged(); 231 emit layerParametersChanged();
239 } 232 }
240 233
316 309
317 #ifdef DEBUG_COLOUR_SELECTION 310 #ifdef DEBUG_COLOUR_SELECTION
318 SVDEBUG << "SingleColourLayer::setProperties: changing colour from " << m_colour << " to " << colour << endl; 311 SVDEBUG << "SingleColourLayer::setProperties: changing colour from " << m_colour << " to " << colour << endl;
319 #endif 312 #endif
320 313
321 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() && 314 unrefColor();
322 m_colourRefCount[m_colour] > 0) {
323 m_colourRefCount[m_colour]--;
324 }
325
326 m_colour = colour; 315 m_colour = colour;
327 316 refColor();
328 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
329 m_colourRefCount[m_colour] = 1;
330 } else {
331 m_colourRefCount[m_colour]++;
332 }
333 317
334 flagBaseColourChanged(); 318 flagBaseColourChanged();
335 } 319 }
336 } 320 }
337 321
322 void SingleColourLayer::refColor()
323 {
324 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
325 m_colourRefCount[m_colour] = 1;
326 } else {
327 m_colourRefCount[m_colour]++;
328 }
329 }
330
331 void SingleColourLayer::unrefColor()
332 {
333 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
334 m_colourRefCount[m_colour] > 0) {
335 m_colourRefCount[m_colour]--;
336 }
337 }