comparison layer/SliceLayer.cpp @ 287:cd2492c5fe45

* Add SingleColourLayer to manage colours for layers that have a single predominant colour (i.e. most of them).
author Chris Cannam
date Thu, 12 Jul 2007 16:14:59 +0000
parents 9dd432665059
children 3101c68a00c1
comparison
equal deleted inserted replaced
286:7554ae119882 287:cd2492c5fe45
19 #include "view/View.h" 19 #include "view/View.h"
20 #include "base/AudioLevel.h" 20 #include "base/AudioLevel.h"
21 #include "base/RangeMapper.h" 21 #include "base/RangeMapper.h"
22 #include "base/RealTime.h" 22 #include "base/RealTime.h"
23 #include "base/ColourMapper.h" 23 #include "base/ColourMapper.h"
24 #include "base/ColourDatabase.h"
24 25
25 #include "PaintAssistant.h" 26 #include "PaintAssistant.h"
26 27
27 #include <QPainter> 28 #include <QPainter>
28 #include <QPainterPath> 29 #include <QPainterPath>
29 30
30 SliceLayer::SliceLayer() : 31 SliceLayer::SliceLayer() :
31 m_sliceableModel(0), 32 m_sliceableModel(0),
32 m_colour(Qt::darkBlue),
33 m_colourMap(0), 33 m_colourMap(0),
34 m_energyScale(dBScale), 34 m_energyScale(dBScale),
35 m_samplingMode(SampleMean), 35 m_samplingMode(SampleMean),
36 m_plotStyle(PlotSteps), 36 m_plotStyle(PlotSteps),
37 m_binScale(LinearBins), 37 m_binScale(LinearBins),
328 paint.drawLine(0, m_scalePoints[i], rect.width(), m_scalePoints[i]); 328 paint.drawLine(0, m_scalePoints[i], rect.width(), m_scalePoints[i]);
329 } 329 }
330 } 330 }
331 } 331 }
332 332
333 paint.setPen(m_colour); 333 paint.setPen(getBaseQColor());
334 334
335 int xorigin = getVerticalScaleWidth(v, paint) + 1; 335 int xorigin = getVerticalScaleWidth(v, paint) + 1;
336 int w = v->width() - xorigin - 1; 336 int w = v->width() - xorigin - 1;
337 337
338 m_xorigins[v] = xorigin; // for use in getFeatureDescription 338 m_xorigins[v] = xorigin; // for use in getFeatureDescription
547 } 547 }
548 548
549 Layer::PropertyList 549 Layer::PropertyList
550 SliceLayer::getProperties() const 550 SliceLayer::getProperties() const
551 { 551 {
552 PropertyList list; 552 PropertyList list = SingleColourLayer::getProperties();
553 list.push_back("Colour");
554 list.push_back("Plot Type"); 553 list.push_back("Plot Type");
555 // list.push_back("Sampling Mode"); 554 // list.push_back("Sampling Mode");
556 list.push_back("Scale"); 555 list.push_back("Scale");
557 list.push_back("Normalize"); 556 list.push_back("Normalize");
558 list.push_back("Threshold"); 557 list.push_back("Threshold");
563 } 562 }
564 563
565 QString 564 QString
566 SliceLayer::getPropertyLabel(const PropertyName &name) const 565 SliceLayer::getPropertyLabel(const PropertyName &name) const
567 { 566 {
568 if (name == "Colour") return tr("Colour");
569 if (name == "Plot Type") return tr("Plot Type"); 567 if (name == "Plot Type") return tr("Plot Type");
570 if (name == "Energy Scale") return tr("Scale"); 568 if (name == "Energy Scale") return tr("Scale");
571 if (name == "Normalize") return tr("Normalize"); 569 if (name == "Normalize") return tr("Normalize");
572 if (name == "Threshold") return tr("Threshold"); 570 if (name == "Threshold") return tr("Threshold");
573 if (name == "Gain") return tr("Gain"); 571 if (name == "Gain") return tr("Gain");
574 if (name == "Sampling Mode") return tr("Sampling Mode"); 572 if (name == "Sampling Mode") return tr("Sampling Mode");
575 if (name == "Bin Scale") return tr("Plot X Scale"); 573 if (name == "Bin Scale") return tr("Plot X Scale");
576 return ""; 574 return SingleColourLayer::getPropertyLabel(name);
577 } 575 }
578 576
579 Layer::PropertyType 577 Layer::PropertyType
580 SliceLayer::getPropertyType(const PropertyName &name) const 578 SliceLayer::getPropertyType(const PropertyName &name) const
581 { 579 {
582 if (name == "Gain") return RangeProperty; 580 if (name == "Gain") return RangeProperty;
583 if (name == "Normalize") return ToggleProperty; 581 if (name == "Normalize") return ToggleProperty;
584 if (name == "Threshold") return RangeProperty; 582 if (name == "Threshold") return RangeProperty;
585 return ValueProperty; 583 if (name == "Plot Type") return ValueProperty;
584 if (name == "Energy Scale") return ValueProperty;
585 if (name == "Sampling Mode") return ValueProperty;
586 if (name == "Bin Scale") return ValueProperty;
587 if (name == "Colour" && m_plotStyle == PlotFilledBlocks) return ValueProperty;
588 return SingleColourLayer::getPropertyType(name);
586 } 589 }
587 590
588 QString 591 QString
589 SliceLayer::getPropertyGroupName(const PropertyName &name) const 592 SliceLayer::getPropertyGroupName(const PropertyName &name) const
590 { 593 {
593 name == "Sampling Mode" || 596 name == "Sampling Mode" ||
594 name == "Threshold" || 597 name == "Threshold" ||
595 name == "Gain") return tr("Scale"); 598 name == "Gain") return tr("Scale");
596 if (name == "Plot Type" || 599 if (name == "Plot Type" ||
597 name == "Bin Scale") return tr("Plot Type"); 600 name == "Bin Scale") return tr("Plot Type");
598 return QString(); 601 return SingleColourLayer::getPropertyGroupName(name);
599 } 602 }
600 603
601 int 604 int
602 SliceLayer::getPropertyRangeAndValue(const PropertyName &name, 605 SliceLayer::getPropertyRangeAndValue(const PropertyName &name,
603 int *min, int *max, int *deflt) const 606 int *min, int *max, int *deflt) const
637 } else if (name == "Normalize") { 640 } else if (name == "Normalize") {
638 641
639 val = (m_normalize ? 1 : 0); 642 val = (m_normalize ? 1 : 0);
640 *deflt = 0; 643 *deflt = 0;
641 644
642 } else if (name == "Colour") { 645 } else if (name == "Colour" && m_plotStyle == PlotFilledBlocks) {
643
644 if (m_plotStyle == PlotFilledBlocks) {
645 646
646 *min = 0; 647 *min = 0;
647 *max = ColourMapper::getColourMapCount() - 1; 648 *max = ColourMapper::getColourMapCount() - 1;
648 *deflt = 0; 649 *deflt = 0;
649 650
650 val = m_colourMap; 651 val = m_colourMap;
651
652 } else {
653
654 *min = 0;
655 *max = 5;
656 *deflt = 0;
657
658 if (m_colour == Qt::black) val = 0;
659 else if (m_colour == Qt::darkRed) val = 1;
660 else if (m_colour == Qt::darkBlue) val = 2;
661 else if (m_colour == Qt::darkGreen) val = 3;
662 else if (m_colour == QColor(200, 50, 255)) val = 4;
663 else if (m_colour == QColor(255, 150, 50)) val = 5;
664 }
665 652
666 } else if (name == "Scale") { 653 } else if (name == "Scale") {
667 654
668 *min = 0; 655 *min = 0;
669 *max = 2; 656 *max = 2;
695 // *max = 1; // I don't think we really do want to offer inverted log 682 // *max = 1; // I don't think we really do want to offer inverted log
696 683
697 val = (int)m_binScale; 684 val = (int)m_binScale;
698 685
699 } else { 686 } else {
700 val = Layer::getPropertyRangeAndValue(name, min, max, deflt); 687 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
701 } 688 }
702 689
703 return val; 690 return val;
704 } 691 }
705 692
706 QString 693 QString
707 SliceLayer::getPropertyValueLabel(const PropertyName &name, 694 SliceLayer::getPropertyValueLabel(const PropertyName &name,
708 int value) const 695 int value) const
709 { 696 {
710 if (name == "Colour") { 697 if (name == "Colour" && m_plotStyle == PlotFilledBlocks) {
711 if (m_plotStyle == PlotFilledBlocks) { 698 return ColourMapper::getColourMapName(value);
712 return ColourMapper::getColourMapName(value);
713 } else {
714 switch (value) {
715 default:
716 case 0: return tr("Black");
717 case 1: return tr("Red");
718 case 2: return tr("Blue");
719 case 3: return tr("Green");
720 case 4: return tr("Purple");
721 case 5: return tr("Orange");
722 }
723 }
724 } 699 }
725 if (name == "Scale") { 700 if (name == "Scale") {
726 switch (value) { 701 switch (value) {
727 default: 702 default:
728 case 0: return tr("Linear"); 703 case 0: return tr("Linear");
753 case 0: return tr("Linear Bins"); 728 case 0: return tr("Linear Bins");
754 case 1: return tr("Log Bins"); 729 case 1: return tr("Log Bins");
755 case 2: return tr("Rev Log Bins"); 730 case 2: return tr("Rev Log Bins");
756 } 731 }
757 } 732 }
758 return tr("<unknown>"); 733 return SingleColourLayer::getPropertyValueLabel(name, value);
759 } 734 }
760 735
761 RangeMapper * 736 RangeMapper *
762 SliceLayer::getNewPropertyRangeMapper(const PropertyName &name) const 737 SliceLayer::getNewPropertyRangeMapper(const PropertyName &name) const
763 { 738 {
765 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB")); 740 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
766 } 741 }
767 if (name == "Threshold") { 742 if (name == "Threshold") {
768 return new LinearRangeMapper(-80, 0, -80, 0, tr("dB")); 743 return new LinearRangeMapper(-80, 0, -80, 0, tr("dB"));
769 } 744 }
770 return 0; 745 return SingleColourLayer::getNewPropertyRangeMapper(name);
771 } 746 }
772 747
773 void 748 void
774 SliceLayer::setProperty(const PropertyName &name, int value) 749 SliceLayer::setProperty(const PropertyName &name, int value)
775 { 750 {
776 if (name == "Gain") { 751 if (name == "Gain") {
777 setGain(pow(10, float(value)/20.0)); 752 setGain(pow(10, float(value)/20.0));
778 } else if (name == "Threshold") { 753 } else if (name == "Threshold") {
779 if (value == -80) setThreshold(0.0); 754 if (value == -80) setThreshold(0.0);
780 else setThreshold(AudioLevel::dB_to_multiplier(value)); 755 else setThreshold(AudioLevel::dB_to_multiplier(value));
781 } else if (name == "Colour") { 756 } else if (name == "Colour" && m_plotStyle == PlotFilledBlocks) {
782 if (m_plotStyle == PlotFilledBlocks) { 757 setFillColourMap(value);
783 setFillColourMap(value);
784 } else {
785 switch (value) {
786 default:
787 case 0: setBaseColour(Qt::black); break;
788 case 1: setBaseColour(Qt::darkRed); break;
789 case 2: setBaseColour(Qt::darkBlue); break;
790 case 3: setBaseColour(Qt::darkGreen); break;
791 case 4: setBaseColour(QColor(200, 50, 255)); break;
792 case 5: setBaseColour(QColor(255, 150, 50)); break;
793 }
794 }
795 } else if (name == "Scale") { 758 } else if (name == "Scale") {
796 switch (value) { 759 switch (value) {
797 default: 760 default:
798 case 0: setEnergyScale(LinearScale); break; 761 case 0: setEnergyScale(LinearScale); break;
799 case 1: setEnergyScale(MeterScale); break; 762 case 1: setEnergyScale(MeterScale); break;
815 case 1: setBinScale(LogBins); break; 778 case 1: setBinScale(LogBins); break;
816 case 2: setBinScale(InvertedLogBins); break; 779 case 2: setBinScale(InvertedLogBins); break;
817 } 780 }
818 } else if (name == "Normalize") { 781 } else if (name == "Normalize") {
819 setNormalize(value ? true : false); 782 setNormalize(value ? true : false);
820 } 783 } else {
821 } 784 SingleColourLayer::setProperty(name, value);
822 785 }
823 void
824 SliceLayer::setBaseColour(QColor colour)
825 {
826 if (m_colour == colour) return;
827 m_colour = colour;
828 emit layerParametersChanged();
829 } 786 }
830 787
831 void 788 void
832 SliceLayer::setFillColourMap(int map) 789 SliceLayer::setFillColourMap(int map)
833 { 790 {
903 if (m_threshold == 0.0) return -80.f; 860 if (m_threshold == 0.0) return -80.f;
904 float db = AudioLevel::multiplier_to_dB(m_threshold); 861 float db = AudioLevel::multiplier_to_dB(m_threshold);
905 return db; 862 return db;
906 } 863 }
907 864
865 int
866 SliceLayer::getDefaultColourHint(bool darkbg, bool &impose)
867 {
868 impose = false;
869 return ColourDatabase::getInstance()->getColourIndex
870 (QString(darkbg ? "Bright Blue" : "Blue"));
871 }
872
908 QString 873 QString
909 SliceLayer::toXmlString(QString indent, QString extraAttributes) const 874 SliceLayer::toXmlString(QString indent, QString extraAttributes) const
910 { 875 {
911 QString s; 876 QString s;
912 877
913 s += QString("colour=\"%1\" " 878 s += QString("colourScheme=\"%1\" "
914 "colourScheme=\"%2\" " 879 "energyScale=\"%2\" "
915 "energyScale=\"%3\" " 880 "samplingMode=\"%3\" "
916 "samplingMode=\"%4\" " 881 "gain=\"%4\" "
917 "gain=\"%5\" " 882 "normalize=\"%5\"")
918 "normalize=\"%6\"")
919 .arg(encodeColour(m_colour))
920 .arg(m_colourMap) 883 .arg(m_colourMap)
921 .arg(m_energyScale) 884 .arg(m_energyScale)
922 .arg(m_samplingMode) 885 .arg(m_samplingMode)
923 .arg(m_gain) 886 .arg(m_gain)
924 .arg(m_normalize ? "true" : "false"); 887 .arg(m_normalize ? "true" : "false");
925 888
926 return Layer::toXmlString(indent, extraAttributes + " " + s); 889 return SingleColourLayer::toXmlString(indent, extraAttributes + " " + s);
927 } 890 }
928 891
929 void 892 void
930 SliceLayer::setProperties(const QXmlAttributes &attributes) 893 SliceLayer::setProperties(const QXmlAttributes &attributes)
931 { 894 {
932 bool ok = false; 895 bool ok = false;
933 896
934 QString colourSpec = attributes.value("colour"); 897 SingleColourLayer::setProperties(attributes);
935 if (colourSpec != "") {
936 QColor colour(colourSpec);
937 if (colour.isValid()) {
938 setBaseColour(QColor(colourSpec));
939 }
940 }
941 898
942 EnergyScale scale = (EnergyScale) 899 EnergyScale scale = (EnergyScale)
943 attributes.value("energyScale").toInt(&ok); 900 attributes.value("energyScale").toInt(&ok);
944 if (ok) setEnergyScale(scale); 901 if (ok) setEnergyScale(scale);
945 902