##// END OF EJS Templates
Call the calculation of the thresholds in the scale editor (with each click on the 'automatic' mode)
Alexandre Leroux -
r1062:b014e09f2329
parent child
Show More
@@ -1,164 +1,174
1 1 #include "Visualization/ColorScaleEditor.h"
2 2 #include "Visualization/SqpColorScale.h"
3 3
4 4 #include "ui_ColorScaleEditor.h"
5 5
6 6 namespace {
7 7
8 8 const auto GRADIENTS = QVariantMap{{"Candy", QCPColorGradient::gpCandy},
9 9 {"Cold", QCPColorGradient::gpCold},
10 10 {"Geography", QCPColorGradient::gpGeography},
11 11 {"Grayscale", QCPColorGradient::gpGrayscale},
12 12 {"Hot", QCPColorGradient::gpHot},
13 13 {"Hues", QCPColorGradient::gpHues},
14 14 {"Ion", QCPColorGradient::gpIon},
15 15 {"Jet", QCPColorGradient::gpJet},
16 16 {"Night", QCPColorGradient::gpNight},
17 17 {"Polar", QCPColorGradient::gpPolar},
18 18 {"Spectrum", QCPColorGradient::gpSpectrum},
19 19 {"Thermal", QCPColorGradient::gpThermal}};
20 20
21 21 } // namespace
22 22
23 23 ColorScaleEditor::ColorScaleEditor(SqpColorScale &scale, QWidget *parent)
24 24 : QDialog{parent},
25 25 ui{new Ui::ColorScaleEditor},
26 26 m_ThresholdGroup{new QButtonGroup{this}},
27 27 m_Scale{scale}
28 28 {
29 29 ui->setupUi(this);
30 30
31 31 // Inits gradient combobox content
32 32 for (auto it = GRADIENTS.begin(), end = GRADIENTS.end(); it != end; ++it) {
33 33 ui->gradientComboBox->addItem(it.key(), it.value());
34 34 }
35 35
36 36 // Creates threshold group
37 37 m_ThresholdGroup->addButton(ui->thresholdAutoButton);
38 38 m_ThresholdGroup->addButton(ui->thresholdManualButton);
39 39
40 40 // Inits min/max spinboxes' properties
41 41 auto setSpinBoxProperties = [](auto &spinBox) {
42 42 spinBox.setDecimals(3);
43 43 spinBox.setMinimum(-std::numeric_limits<double>::max());
44 44 spinBox.setMaximum(std::numeric_limits<double>::max());
45 45 };
46 46 setSpinBoxProperties(*ui->minSpinBox);
47 47 setSpinBoxProperties(*ui->maxSpinBox);
48 48
49 49 // Creates color scale preview
50 50 m_PreviewScale = new QCPColorScale{ui->plot};
51 51 m_PreviewScale->setType(QCPAxis::atTop);
52 52 m_PreviewScale->setMinimumMargins(QMargins{5, 5, 5, 5});
53 53 m_PreviewScale->axis()->setScaleType(QCPAxis::stLogarithmic);
54 54 m_PreviewScale->axis()->setNumberPrecision(0);
55 55 m_PreviewScale->axis()->setNumberFormat("eb");
56 56 m_PreviewScale->axis()->setTicker(QSharedPointer<QCPAxisTickerLog>::create());
57 57 m_PreviewScale->setGradient(QCPColorGradient{QCPColorGradient::gpJet});
58 58
59 59 ui->plot->plotLayout()->clear();
60 60 ui->plot->plotLayout()->insertRow(0);
61 61 ui->plot->plotLayout()->addElement(0, 0, m_PreviewScale);
62 62
63 63 // Inits connections
64 64 connect(ui->gradientComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePreview()));
65 65 connect(ui->thresholdAutoButton, SIGNAL(toggled(bool)), this, SLOT(onThresholdChanged(bool)));
66 66 connect(ui->thresholdManualButton, SIGNAL(toggled(bool)), this, SLOT(onThresholdChanged(bool)));
67 67 connect(ui->minSpinBox, SIGNAL(editingFinished()), this, SLOT(onMinChanged()));
68 68 connect(ui->maxSpinBox, SIGNAL(editingFinished()), this, SLOT(onMaxChanged()));
69 69
70 70 // OK/cancel buttons
71 71 connect(ui->okButton, SIGNAL(clicked(bool)), this, SLOT(accept()));
72 72 connect(ui->cancelButton, SIGNAL(clicked(bool)), this, SLOT(reject()));
73 73
74 74 // Loads color scale
75 75 loadScale();
76 76 }
77 77
78 78 ColorScaleEditor::~ColorScaleEditor() noexcept
79 79 {
80 80 delete ui;
81 81 }
82 82
83 83 void ColorScaleEditor::loadScale()
84 84 {
85 85 // Gradient
86 86 auto gradientPresetIndex = ui->gradientComboBox->findData(m_Scale.m_GradientPreset);
87 87 ui->gradientComboBox->setCurrentIndex(gradientPresetIndex);
88 88
89 89 // Threshold mode
90 90 (m_Scale.m_AutomaticThreshold ? ui->thresholdAutoButton : ui->thresholdManualButton)
91 91 ->setChecked(true);
92 92
93 93 // Min/max
94 94 auto qcpColorScale = m_Scale.m_Scale;
95 95 auto range = qcpColorScale->dataRange();
96 96 ui->minSpinBox->setValue(range.lower);
97 97 ui->maxSpinBox->setValue(range.upper);
98 98
99 99 updatePreview();
100 100 }
101 101
102 102 void ColorScaleEditor::saveScale()
103 103 {
104 104 auto qcpColorScale = m_Scale.m_Scale;
105 105
106 106 // Gradient
107 107 auto gradientPreset
108 108 = ui->gradientComboBox->currentData().value<QCPColorGradient::GradientPreset>();
109 109 qcpColorScale->setGradient(gradientPreset);
110 110 m_Scale.m_GradientPreset = gradientPreset;
111 111
112 112 // Threshold mode
113 113 m_Scale.m_AutomaticThreshold = ui->thresholdAutoButton->isChecked();
114 114
115 115 // Min/max
116 116 qcpColorScale->setDataRange(QCPRange{ui->minSpinBox->value(), ui->maxSpinBox->value()});
117 117 }
118 118
119 119 void ColorScaleEditor::accept()
120 120 {
121 121 saveScale();
122 122 QDialog::accept();
123 123 }
124 124
125 125 void ColorScaleEditor::onMaxChanged()
126 126 {
127 127 // Ensures that max >= min
128 128 auto maxValue = ui->maxSpinBox->value();
129 129 if (maxValue < ui->minSpinBox->value()) {
130 130 ui->minSpinBox->setValue(maxValue);
131 131 }
132 132
133 133 updatePreview();
134 134 }
135 135
136 136 void ColorScaleEditor::onMinChanged()
137 137 {
138 138 // Ensures that min <= max
139 139 auto minValue = ui->minSpinBox->value();
140 140 if (minValue > ui->maxSpinBox->value()) {
141 141 ui->maxSpinBox->setValue(minValue);
142 142 }
143 143
144 144 updatePreview();
145 145 }
146 146
147 147 void ColorScaleEditor::onThresholdChanged(bool checked)
148 148 {
149 149 if (checked) {
150 150 auto isAutomatic = ui->thresholdAutoButton == m_ThresholdGroup->checkedButton();
151 151
152 152 ui->minSpinBox->setEnabled(!isAutomatic);
153 153 ui->maxSpinBox->setEnabled(!isAutomatic);
154
155 // Computes automatic thresholds
156 if (isAutomatic) {
157 double minThreshold, maxThreshold;
158 std::tie(minThreshold, maxThreshold) = SqpColorScale::computeThresholds(m_Scale);
159 ui->minSpinBox->setValue(minThreshold);
160 ui->maxSpinBox->setValue(maxThreshold);
161
162 updatePreview();
163 }
154 164 }
155 165 }
156 166
157 167 void ColorScaleEditor::updatePreview()
158 168 {
159 169 m_PreviewScale->setDataRange(QCPRange{ui->minSpinBox->value(), ui->maxSpinBox->value()});
160 170 m_PreviewScale->setGradient(
161 171 ui->gradientComboBox->currentData().value<QCPColorGradient::GradientPreset>());
162 172
163 173 ui->plot->replot();
164 174 }
@@ -1,169 +1,163
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ui version="4.0">
3 3 <class>ColorScaleEditor</class>
4 4 <widget class="QDialog" name="ColorScaleEditor">
5 5 <property name="geometry">
6 6 <rect>
7 7 <x>0</x>
8 8 <y>0</y>
9 9 <width>258</width>
10 10 <height>232</height>
11 11 </rect>
12 12 </property>
13 13 <property name="windowTitle">
14 14 <string>Color scale editor</string>
15 15 </property>
16 16 <layout class="QGridLayout" name="gridLayout">
17 17 <item row="3" column="0">
18 18 <layout class="QHBoxLayout" name="horizontalLayout">
19 19 <item>
20 20 <spacer name="horizontalSpacer">
21 21 <property name="orientation">
22 22 <enum>Qt::Horizontal</enum>
23 23 </property>
24 24 <property name="sizeHint" stdset="0">
25 25 <size>
26 26 <width>40</width>
27 27 <height>20</height>
28 28 </size>
29 29 </property>
30 30 </spacer>
31 31 </item>
32 32 <item>
33 33 <widget class="QPushButton" name="okButton">
34 34 <property name="text">
35 35 <string>OK</string>
36 36 </property>
37 <property name="default">
38 <bool>true</bool>
39 </property>
40 37 </widget>
41 38 </item>
42 39 <item>
43 40 <widget class="QPushButton" name="cancelButton">
44 41 <property name="text">
45 42 <string>Cancel</string>
46 43 </property>
47 44 </widget>
48 45 </item>
49 46 </layout>
50 47 </item>
51 48 <item row="1" column="0">
52 49 <widget class="QGroupBox" name="groupBox">
53 50 <property name="title">
54 51 <string>Preview</string>
55 52 </property>
56 53 <layout class="QGridLayout" name="gridLayout_3">
57 54 <item row="0" column="0">
58 55 <widget class="QCustomPlot" name="plot" native="true"/>
59 56 </item>
60 57 </layout>
61 58 </widget>
62 59 </item>
63 60 <item row="0" column="0">
64 61 <layout class="QGridLayout" name="gridLayout_2">
65 62 <item row="1" column="0">
66 63 <widget class="QLabel" name="thresholdLabel">
67 64 <property name="text">
68 65 <string>Thresholds:</string>
69 66 </property>
70 67 </widget>
71 68 </item>
72 69 <item row="1" column="1">
73 70 <widget class="QRadioButton" name="thresholdAutoButton">
74 <property name="enabled">
75 <bool>false</bool>
76 </property>
77 71 <property name="text">
78 72 <string>Automatic</string>
79 73 </property>
80 74 <property name="checkable">
81 75 <bool>true</bool>
82 76 </property>
83 77 <property name="checked">
84 78 <bool>false</bool>
85 79 </property>
86 80 </widget>
87 81 </item>
88 82 <item row="1" column="2">
89 83 <widget class="QRadioButton" name="thresholdManualButton">
90 84 <property name="text">
91 85 <string>Manual</string>
92 86 </property>
93 87 </widget>
94 88 </item>
95 89 <item row="2" column="0">
96 90 <widget class="QLabel" name="minLabel">
97 91 <property name="text">
98 92 <string>Min:</string>
99 93 </property>
100 94 </widget>
101 95 </item>
102 96 <item row="3" column="0">
103 97 <widget class="QLabel" name="maxLabel">
104 98 <property name="text">
105 99 <string>Max:</string>
106 100 </property>
107 101 </widget>
108 102 </item>
109 103 <item row="2" column="1" colspan="2">
110 104 <widget class="QDoubleSpinBox" name="minSpinBox">
111 105 <property name="showGroupSeparator" stdset="0">
112 106 <bool>true</bool>
113 107 </property>
114 108 </widget>
115 109 </item>
116 110 <item row="3" column="1" colspan="2">
117 111 <widget class="QDoubleSpinBox" name="maxSpinBox">
118 112 <property name="showGroupSeparator" stdset="0">
119 113 <bool>true</bool>
120 114 </property>
121 115 </widget>
122 116 </item>
123 117 <item row="0" column="0">
124 118 <widget class="QLabel" name="gradientLabel">
125 119 <property name="text">
126 120 <string>Gradient:</string>
127 121 </property>
128 122 </widget>
129 123 </item>
130 124 <item row="0" column="1" colspan="2">
131 125 <widget class="QComboBox" name="gradientComboBox"/>
132 126 </item>
133 127 </layout>
134 128 </item>
135 129 <item row="2" column="0">
136 130 <spacer name="verticalSpacer">
137 131 <property name="orientation">
138 132 <enum>Qt::Vertical</enum>
139 133 </property>
140 134 <property name="sizeHint" stdset="0">
141 135 <size>
142 136 <width>20</width>
143 137 <height>40</height>
144 138 </size>
145 139 </property>
146 140 </spacer>
147 141 </item>
148 142 </layout>
149 143 </widget>
150 144 <customwidgets>
151 145 <customwidget>
152 146 <class>QCustomPlot</class>
153 147 <extends>QWidget</extends>
154 148 <header>Visualization/qcustomplot.h</header>
155 149 <container>1</container>
156 150 </customwidget>
157 151 </customwidgets>
158 152 <tabstops>
159 153 <tabstop>gradientComboBox</tabstop>
160 154 <tabstop>thresholdAutoButton</tabstop>
161 <tabstop>okButton</tabstop>
162 155 <tabstop>thresholdManualButton</tabstop>
163 <tabstop>maxSpinBox</tabstop>
164 156 <tabstop>minSpinBox</tabstop>
157 <tabstop>maxSpinBox</tabstop>
158 <tabstop>okButton</tabstop>
165 159 <tabstop>cancelButton</tabstop>
166 160 </tabstops>
167 161 <resources/>
168 162 <connections/>
169 163 </ui>
General Comments 0
You need to be logged in to leave comments. Login now