diff layer/SingleColourLayer.cpp @ 770:734ee80286c3 tony_integration

Merge from default branch
author Chris Cannam
date Fri, 09 May 2014 17:15:50 +0100
parents 8b614632568c
children 1d526ba11a24
line wrap: on
line diff
--- a/layer/SingleColourLayer.cpp	Wed May 07 16:51:04 2014 +0100
+++ b/layer/SingleColourLayer.cpp	Fri May 09 17:15:50 2014 +0100
@@ -32,9 +32,17 @@
     m_colourExplicitlySet(false),
     m_defaultColourSet(false)
 {
+    // Reference current colour because setDefaulColourFor
+    // will unreference it before (possibly) changing it.
+    refColor();
     setDefaultColourFor(0);
 }
 
+SingleColourLayer::~SingleColourLayer()
+{
+    unrefColor();
+}
+
 QPixmap
 SingleColourLayer::getLayerPresentationPixmap(QSize size) const
 {
@@ -152,10 +160,6 @@
     int hint = -1;
     bool impose = false;
     if (v) {
-        if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
-            m_colourRefCount[m_colour] > 0) {
-            m_colourRefCount[m_colour]--;
-        }
         // We don't want to call this if !v because that probably
         // means we're being called from the constructor, and this is
         // a virtual function
@@ -174,6 +178,8 @@
         return;
     }
 
+    unrefColor();
+
     int bestCount = 0, bestColour = -1;
     
     for (int i = 0; i < cdb->getColourCount(); ++i) {
@@ -203,15 +209,11 @@
         cerr << endl;
 #endif
     }
-    
+
     if (bestColour < 0) m_colour = 0;
     else m_colour = bestColour;
 
-    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
-        m_colourRefCount[m_colour] = 1;
-    } else {
-        m_colourRefCount[m_colour]++;
-    }
+    refColor();
 }
 
 void
@@ -221,18 +223,9 @@
 
     if (m_colour == colour) return;
 
-    if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
-        m_colourRefCount[m_colour] > 0) {
-        m_colourRefCount[m_colour]--;
-    }
-
+    refColor();
     m_colour = colour;
-
-    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
-        m_colourRefCount[m_colour] = 1;
-    } else {
-        m_colourRefCount[m_colour]++;
-    }
+    unrefColor();
 
     flagBaseColourChanged();
     emit layerParametersChanged();
@@ -307,6 +300,9 @@
     int colour = ColourDatabase::getInstance()->putStringValues
         (colourName, colourSpec, darkbg);
 
+    if (colour == -1)
+      return;
+
     m_colourExplicitlySet = true;
 
     if (m_colour != colour) {
@@ -315,20 +311,27 @@
         SVDEBUG << "SingleColourLayer::setProperties: changing colour from " << m_colour << " to " << colour << endl;
 #endif
 
-        if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
-            m_colourRefCount[m_colour] > 0) {
-            m_colourRefCount[m_colour]--;
-        }
-
+        unrefColor();
         m_colour = colour;
-
-        if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
-            m_colourRefCount[m_colour] = 1;
-        } else {
-            m_colourRefCount[m_colour]++;
-        }
+        refColor();
 
         flagBaseColourChanged();
     }
 }
 
+void SingleColourLayer::refColor()
+{
+    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
+        m_colourRefCount[m_colour] = 1;
+    } else {
+        m_colourRefCount[m_colour]++;
+    }
+}
+
+void SingleColourLayer::unrefColor()
+{
+    if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
+        m_colourRefCount[m_colour] > 0) {
+        m_colourRefCount[m_colour]--;
+    }
+}