diff layer/SingleColourLayer.cpp @ 293:15b8a4bfe855

* continue to pick "new" colours for coloured layers even when all colours have been used at least once, rather than sticking on the last one * some messing about with application palette settings * when replacing an audio file, retain the previous playback settings for any layers that depended on the old file * re-check plugin program setting when a parameter changes -- so a plugin can decide to reset the program if the parameters no longer match those for the current program * fix failure to update check-boxes for toggled plugin parameters when their parameters are changed by program changes
author Chris Cannam
date Thu, 09 Aug 2007 14:40:03 +0000
parents cd2492c5fe45
children 919740b20cc9
line wrap: on
line diff
--- a/layer/SingleColourLayer.cpp	Mon Aug 06 14:37:59 2007 +0000
+++ b/layer/SingleColourLayer.cpp	Thu Aug 09 14:40:03 2007 +0000
@@ -21,8 +21,8 @@
 
 #include <QApplication>
 
-SingleColourLayer::ColourIndexPool 
-SingleColourLayer::m_usedColourIndices;
+SingleColourLayer::ColourRefCount 
+SingleColourLayer::m_colourRefCount;
 
 SingleColourLayer::SingleColourLayer() :
     m_colour(0)
@@ -119,56 +119,87 @@
 {
     bool dark = false;
     if (v) {
-        ColourIndexPool::iterator i = m_usedColourIndices.find(m_colour);
-        if (i != m_usedColourIndices.end()) m_usedColourIndices.erase(i);
         dark = !v->hasLightBackground();
     } else {
         QColor bg = QApplication::palette().color(QPalette::Window);
         if (bg.red() + bg.green() + bg.blue() < 384) dark = true;
     }
 
-    m_colour = -1;
     ColourDatabase *cdb = ColourDatabase::getInstance();
 
     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
         hint = getDefaultColourHint(dark, impose);
-        std::cerr << "hint = " << hint << ", impose = " << impose << std::endl;
+//        std::cerr << "hint = " << hint << ", impose = " << impose << std::endl;
+    } else {
+//        std::cerr << "(from ctor)" << std::endl;
     }
 
     if (hint >= 0 && impose) {
-        m_colour = hint;
-        m_usedColourIndices.insert(m_colour);
+        setBaseColour(hint);
         return;
     }
 
+    int bestCount = 0, bestColour = -1;
+    
     for (int i = 0; i < cdb->getColourCount(); ++i) {
+
         int index = i;
         if (hint > 0) index = (index + hint) % cdb->getColourCount();
         if (cdb->useDarkBackground(index) != dark) continue;
-        if (m_colour < 0) m_colour = index;
-        if (m_usedColourIndices.find(index) == m_usedColourIndices.end()) {
-            m_colour = index;
-            break;
+
+        int count = 0;
+        if (m_colourRefCount.find(index) != m_colourRefCount.end()) {
+            count = m_colourRefCount[index];
         }
+
+//        std::cerr << "index = " << index << ", count = " << count;
+
+        if (bestColour < 0 || count < bestCount) {
+            bestColour = index;
+            bestCount = count;
+//            std::cerr << " *";
+        }
+
+//        std::cerr << std::endl;
     }
+    
+    if (bestColour < 0) m_colour = 0;
+    else m_colour = bestColour;
 
-    if (m_colour < 0) m_colour = 0;
-    m_usedColourIndices.insert(m_colour);
+    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
+        m_colourRefCount[m_colour] = 1;
+    } else {
+        m_colourRefCount[m_colour]++;
+    }
 }
 
 void
 SingleColourLayer::setBaseColour(int colour)
 {
     if (m_colour == colour) return;
-    ColourIndexPool::iterator i = m_usedColourIndices.find(m_colour);
-    if (i != m_usedColourIndices.end()) m_usedColourIndices.erase(i);
+
+    if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
+        m_colourRefCount[m_colour] > 0) {
+        m_colourRefCount[m_colour]--;
+    }
+
     m_colour = colour;
-    m_usedColourIndices.insert(m_colour);
+
+    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
+        m_colourRefCount[m_colour] = 1;
+    } else {
+        m_colourRefCount[m_colour]++;
+    }
+
     flagBaseColourChanged();
     emit layerParametersChanged();
 }