##// END OF EJS Templates
Implements color scale save when the editor is closed
Alexandre Leroux -
r1053:18abbd5ba9c1
parent child
Show More
@@ -1,48 +1,54
1 1 #ifndef SCIQLOP_COLORSCALEEDITOR_H
2 2 #define SCIQLOP_COLORSCALEEDITOR_H
3 3
4 4 #include <QButtonGroup>
5 5 #include <QDialog>
6 6
7 7 namespace Ui {
8 8 class ColorScaleEditor;
9 9 } // Ui
10 10
11 11 class SqpColorScale;
12 12 class QCPColorScale;
13 13
14 14 /**
15 15 * @brief The ColorScaleEditor class represents the widget to set properties of color scale's graphs
16 16 */
17 17 class ColorScaleEditor : public QDialog {
18 18 Q_OBJECT
19 19
20 20 public:
21 21 explicit ColorScaleEditor(SqpColorScale &scale, QWidget *parent = 0);
22 22 virtual ~ColorScaleEditor() noexcept;
23 23
24 24 private:
25 25 /// Fills the editor fields from color scale data
26 26 void loadScale();
27 /// Updates the color scale from editor fields
28 void saveScale();
29
27 30 Ui::ColorScaleEditor *ui;
28 31 QButtonGroup *m_ThresholdGroup;
29 32 /// Scale in editing
30 33 /// @remarks reference must remain valid throughout the existence of the ColorScaleEditor
31 34 /// instance
32 35 SqpColorScale &m_Scale;
33 36 /// Scale shown as preview
34 37 QCPColorScale *m_PreviewScale;
35 38
36 39 private slots:
40 /// @sa QDialog::accept()
41 void accept() override;
42
37 43 /// Slot called when max threshold value changes
38 44 void onMaxChanged();
39 45 /// Slot called when min threshold value changes
40 46 void onMinChanged();
41 47 /// Slot called when the threshold mode (auto or manual) changes
42 48 void onThresholdChanged(bool checked);
43 49
44 50 /// Slot called when a property of the color scale changed
45 51 void updatePreview();
46 52 };
47 53
48 54 #endif // SCIQLOP_COLORSCALEEDITOR_H
@@ -1,137 +1,164
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_Scale{scale},
27 27 m_ThresholdGroup{new QButtonGroup{this}}
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 // OK/cancel buttons
71 connect(ui->okButton, SIGNAL(clicked(bool)), this, SLOT(accept()));
72 connect(ui->cancelButton, SIGNAL(clicked(bool)), this, SLOT(reject()));
73
70 74 // Loads color scale
71 75 loadScale();
72 76 }
73 77
74 78 ColorScaleEditor::~ColorScaleEditor() noexcept
75 79 {
76 80 delete ui;
77 81 }
78 82
79 83 void ColorScaleEditor::loadScale()
80 84 {
81 85 // Gradient
82 86 auto gradientPresetIndex = ui->gradientComboBox->findData(m_Scale.m_GradientPreset);
83 87 ui->gradientComboBox->setCurrentIndex(gradientPresetIndex);
84 88
85 89 // Threshold mode
86 90 (m_Scale.m_AutomaticThreshold ? ui->thresholdAutoButton : ui->thresholdManualButton)
87 91 ->setChecked(true);
88 92
89 93 // Min/max
90 94 auto qcpColorScale = m_Scale.m_Scale;
91 95 auto range = qcpColorScale->dataRange();
92 96 ui->minSpinBox->setValue(range.lower);
93 97 ui->maxSpinBox->setValue(range.upper);
94 98
95 99 updatePreview();
96 100 }
97 101
102 void ColorScaleEditor::saveScale()
103 {
104 auto qcpColorScale = m_Scale.m_Scale;
105
106 // Gradient
107 auto gradientPreset
108 = ui->gradientComboBox->currentData().value<QCPColorGradient::GradientPreset>();
109 qcpColorScale->setGradient(gradientPreset);
110 m_Scale.m_GradientPreset = gradientPreset;
111
112 // Threshold mode
113 m_Scale.m_AutomaticThreshold = ui->thresholdAutoButton->isChecked();
114
115 // Min/max
116 qcpColorScale->setDataRange(QCPRange{ui->minSpinBox->value(), ui->maxSpinBox->value()});
117 }
118
119 void ColorScaleEditor::accept()
120 {
121 saveScale();
122 QDialog::accept();
123 }
124
98 125 void ColorScaleEditor::onMaxChanged()
99 126 {
100 127 // Ensures that max >= min
101 128 auto maxValue = ui->maxSpinBox->value();
102 129 if (maxValue < ui->minSpinBox->value()) {
103 130 ui->minSpinBox->setValue(maxValue);
104 131 }
105 132
106 133 updatePreview();
107 134 }
108 135
109 136 void ColorScaleEditor::onMinChanged()
110 137 {
111 138 // Ensures that min <= max
112 139 auto minValue = ui->minSpinBox->value();
113 140 if (minValue > ui->maxSpinBox->value()) {
114 141 ui->maxSpinBox->setValue(minValue);
115 142 }
116 143
117 144 updatePreview();
118 145 }
119 146
120 147 void ColorScaleEditor::onThresholdChanged(bool checked)
121 148 {
122 149 if (checked) {
123 150 auto isAutomatic = ui->thresholdAutoButton == m_ThresholdGroup->checkedButton();
124 151
125 152 ui->minSpinBox->setEnabled(!isAutomatic);
126 153 ui->maxSpinBox->setEnabled(!isAutomatic);
127 154 }
128 155 }
129 156
130 157 void ColorScaleEditor::updatePreview()
131 158 {
132 159 m_PreviewScale->setDataRange(QCPRange{ui->minSpinBox->value(), ui->maxSpinBox->value()});
133 160 m_PreviewScale->setGradient(
134 161 ui->gradientComboBox->currentData().value<QCPColorGradient::GradientPreset>());
135 162
136 163 ui->plot->replot();
137 164 }
General Comments 0
You need to be logged in to leave comments. Login now