diff layer/TimeValueLayer.cpp @ 1362:d79e21855aef

Add mechanism for saving/loading colour maps by name/id rather than by numerical index, for future compatibility when adding to or changing the supported colour maps. Add two new colour maps (and one old one). Write out backward-compatible numerical indices for use when reloading in older versions. Also add a mechanism to invert the colour map, though I don't think it turns out useful enough to include in the UI.
author Chris Cannam
date Thu, 18 Oct 2018 13:21:56 +0100
parents 1d7921b1852f
children a1393b4384a5
line wrap: on
line diff
--- a/layer/TimeValueLayer.cpp	Fri Oct 12 11:17:29 2018 +0100
+++ b/layer/TimeValueLayer.cpp	Thu Oct 18 13:21:56 2018 +0100
@@ -60,6 +60,7 @@
     m_editingPoint(0, 0.0, tr("New Point")),
     m_editingCommand(0),
     m_colourMap(0),
+    m_colourInverted(false),
     m_plotStyle(PlotConnectedPoints),
     m_verticalScale(AutoAlignScale),
     m_drawSegmentDivisions(true),
@@ -223,7 +224,7 @@
                                     int value) const
 {
     if (name == "Colour" && m_plotStyle == PlotSegmentation) {
-        return ColourMapper::getColourMapName(value);
+        return ColourMapper::getColourMapLabel(value);
     } else if (name == "Plot Type") {
         switch (value) {
         default:
@@ -896,7 +897,7 @@
               << max << ", log " << log << ", value " << val << endl;
 #endif
 
-    QColor solid = ColourMapper(m_colourMap, min, max).map(val);
+    QColor solid = ColourMapper(m_colourMap, m_colourInverted, min, max).map(val);
     return QColor(solid.red(), solid.green(), solid.blue(), 120);
 }
 
@@ -1907,16 +1908,33 @@
 TimeValueLayer::toXml(QTextStream &stream,
                       QString indent, QString extraAttributes) const
 {
-    SingleColourLayer::toXml(stream, indent,
-                             extraAttributes +
-                             QString(" colourMap=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\" scaleMinimum=\"%4\" scaleMaximum=\"%5\" drawDivisions=\"%6\" derivative=\"%7\" ")
-                             .arg(m_colourMap)
-                             .arg(m_plotStyle)
-                             .arg(m_verticalScale)
-                             .arg(m_scaleMinimum)
-                             .arg(m_scaleMaximum)
-                             .arg(m_drawSegmentDivisions ? "true" : "false")
-                             .arg(m_derivative ? "true" : "false"));
+    QString s;
+
+    s += QString("plotStyle=\"%1\" "
+                 "verticalScale=\"%2\" "
+                 "scaleMinimum=\"%3\" "
+                 "scaleMaximum=\"%4\" "
+                 "drawDivisions=\"%5\" "
+                 "derivative=\"%6\" ")
+        .arg(m_plotStyle)
+        .arg(m_verticalScale)
+        .arg(m_scaleMinimum)
+        .arg(m_scaleMaximum)
+        .arg(m_drawSegmentDivisions ? "true" : "false")
+        .arg(m_derivative ? "true" : "false");
+    
+    // New-style colour map attribute, by string id rather than by
+    // number
+
+    s += QString("fillColourMap=\"%1\" ")
+        .arg(ColourMapper::getColourMapId(m_colourMap));
+
+    // Old-style colour map attribute
+
+    s += QString("colourMap=\"%1\" ")
+        .arg(ColourMapper::getBackwardCompatibilityColourMap(m_colourMap));
+    
+    SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
 }
 
 void
@@ -1926,8 +1944,16 @@
 
     bool ok, alsoOk;
 
-    int cmap = attributes.value("colourMap").toInt(&ok);
-    if (ok) setFillColourMap(cmap);
+    QString colourMapId = attributes.value("fillColourMap");
+    int colourMap = ColourMapper::getColourMapById(colourMapId);
+    if (colourMap >= 0) {
+        setFillColourMap(colourMap);
+    } else {
+        colourMap = attributes.value("colourMap").toInt(&ok);
+        if (ok && colourMap < ColourMapper::getColourMapCount()) {
+            setFillColourMap(colourMap);
+        }
+    }
 
     PlotStyle style = (PlotStyle)
         attributes.value("plotStyle").toInt(&ok);