Skip to content

Commit cab01d8

Browse files
Fix issues #196 and #236: allow user to use deterministic color sequence
1 parent e9c14a4 commit cab01d8

11 files changed

+158
-51
lines changed

include/PlotJuggler/random_color.h

-27
This file was deleted.

plotter_gui/curvecolorpick.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include <QColorDialog>
44

55

6-
CurveColorPick::CurveColorPick(const std::map<std::string, QColor> &mapped_colors, QWidget *parent) :
6+
CurveColorPick::CurveColorPick(const std::map<std::string, QColor> &mapped_colors,
7+
QWidget *parent) :
78
QDialog(parent),
89
ui(new Ui::CurveColorPick),
910
_any_modified(false),
@@ -23,16 +24,35 @@ CurveColorPick::CurveColorPick(const std::map<std::string, QColor> &mapped_color
2324
_color_wheel->setMinimumWidth(150);
2425
_color_wheel->setMinimumHeight(150);
2526

27+
ui->verticalLayoutRight->insertWidget(1, new QLabel("Default colors", this) );
28+
29+
_color_palette = new color_widgets::Swatch(this);
30+
ui->verticalLayoutRight->insertWidget(2, _color_palette );
31+
_color_palette->setMinimumWidth(150);
32+
_color_palette->setMinimumHeight(30);
33+
_color_palette->setMaximumHeight(30);
34+
35+
ui->verticalLayoutRight->insertWidget(3, new QLabel("Preview", this) );
36+
2637
_color_preview = new color_widgets::ColorPreview(this);
27-
ui->verticalLayoutRight->insertWidget(1, _color_preview );
38+
ui->verticalLayoutRight->insertWidget(4, _color_preview );
2839
_color_preview->setMinimumWidth(150);
2940
_color_preview->setMinimumHeight(100);
3041

42+
QVector<QColor> colors = {QColor("#1f77b4"), QColor("#d62728"), QColor("#1ac938"), QColor("#ff7f0e"),
43+
QColor("#f14cc1"), QColor("#9467bd"), QColor("#17becf"), QColor("#bcbd22")};
44+
45+
color_widgets::ColorPalette palette(colors, "default colors", 8);
46+
_color_palette->setPalette(palette);
47+
3148
connect(_color_wheel, &color_widgets::ColorWheel::colorChanged,
3249
_color_preview, &color_widgets::ColorPreview::setColor );
3350

3451
connect(_color_wheel, &color_widgets::ColorWheel::colorChanged,
3552
this, &CurveColorPick::on_colorChanged );
53+
54+
connect(_color_palette, &color_widgets::Swatch::colorSelected,
55+
_color_wheel, &color_widgets::ColorWheel::setColor );
3656
}
3757

3858
CurveColorPick::~CurveColorPick()

plotter_gui/curvecolorpick.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <QListWidgetItem>
66
#include "color_wheel.hpp"
77
#include "color_preview.hpp"
8+
#include "color_palette.hpp"
9+
#include "swatch.hpp"
10+
811

912
namespace Ui {
1013
class CurveColorPick;
@@ -15,7 +18,8 @@ class CurveColorPick : public QDialog
1518
Q_OBJECT
1619

1720
public:
18-
explicit CurveColorPick(const std::map<std::string, QColor>& mapped_colors, QWidget *parent = 0);
21+
explicit CurveColorPick(const std::map<std::string, QColor>& mapped_colors,
22+
QWidget *parent = 0);
1923
~CurveColorPick();
2024

2125
bool anyColorModified() const ;
@@ -36,6 +40,7 @@ private slots:
3640
Ui::CurveColorPick *ui;
3741
color_widgets::ColorWheel *_color_wheel;
3842
color_widgets::ColorPreview *_color_preview;
43+
color_widgets::Swatch *_color_palette;
3944

4045
const std::map<std::string, QColor>& _mapped_colors;
4146
bool _any_modified;

plotter_gui/curvecolorpick.ui

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
14-
<string>Dialog</string>
14+
<string>Color Selector</string>
1515
</property>
1616
<layout class="QHBoxLayout" name="horizontalLayout">
1717
<item>

plotter_gui/curvelist_panel.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,6 @@ void CurveListPanel::on_radioRegExp_toggled(bool checked)
276276
}
277277
}
278278

279-
void CurveListPanel::on_checkBoxCaseSensitive_toggled(bool )
280-
{
281-
updateFilter();
282-
}
283-
284279
void CurveListPanel::on_lineEdit_textChanged(const QString &search_string)
285280
{
286281
bool updated = false;

plotter_gui/curvelist_panel.h

-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ private slots:
5757

5858
void on_radioRegExp_toggled(bool checked);
5959

60-
void on_checkBoxCaseSensitive_toggled(bool checked);
61-
6260
void on_lineEdit_textChanged(const QString &search_string);
6361

6462
void on_pushButtonSettings_toggled(bool checked);

plotter_gui/curvelist_view.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ bool CurvesView::eventFilterBase(QObject *object, QEvent *event)
248248
QByteArray mdata;
249249
QDataStream stream(&mdata, QIODevice::WriteOnly);
250250

251-
for (const auto &curve_name : getSelectedNames())
251+
auto selected_names = getSelectedNames();
252+
std::sort( selected_names.begin(), selected_names.end() );
253+
254+
for (const auto &curve_name : selected_names)
252255
{
253256
stream << QString::fromStdString(curve_name);
254257
}
@@ -259,9 +262,9 @@ bool CurvesView::eventFilterBase(QObject *object, QEvent *event)
259262
}
260263
else
261264
{
262-
if (getSelectedNames().size() != 2)
265+
if (selected_names.size() != 2)
263266
{
264-
if (getSelectedNames().size() >= 1)
267+
if (selected_names.size() >= 1)
265268
{
266269
QMessageBox::warning(
267270
table_widget, "New in version 2.3+",

plotter_gui/plotwidget.cpp

+43-9
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,50 @@
3333
#include "qwt_plot_renderer.h"
3434
#include "qwt_series_data.h"
3535
#include "qwt_date_scale_draw.h"
36-
#include "PlotJuggler/random_color.h"
3736
#include "point_series_xy.h"
3837
#include "suggest_dialog.h"
3938
#include "transforms/custom_function.h"
4039
#include "transforms/custom_timeseries.h"
4140

41+
int PlotWidget::global_color_index = 0;
42+
43+
QColor PlotWidget::getColorHint(PlotData* data)
44+
{
45+
QSettings settings;
46+
bool remember_color = settings.value("Preferences::remember_color", true).toBool();
47+
if( data && remember_color && data->getColorHint() != Qt::black )
48+
{
49+
return data->getColorHint();
50+
}
51+
QColor color;
52+
bool use_plot_color_index = settings.value("Preferences::use_plot_color_index", false).toBool();
53+
int index = _curve_list.size();
54+
55+
if( !use_plot_color_index )
56+
{
57+
index = (PlotWidget::global_color_index++);
58+
}
59+
60+
// https://matplotlib.org/3.1.1/users/dflt_style_changes.html
61+
switch( index%8 )
62+
{
63+
case 0: color = QColor("#1f77b4"); break;
64+
case 1: color = QColor("#d62728"); break;
65+
case 2: color = QColor("#1ac938"); break;
66+
case 3: color = QColor("#ff7f0e"); break;
67+
68+
case 4: color = QColor("#f14cc1"); break;
69+
case 5: color = QColor("#9467bd"); break;
70+
case 6: color = QColor("#17becf"); break;
71+
case 7: color = QColor("#bcbd22"); break;
72+
}
73+
if( data ){
74+
data->setColorHint(color);
75+
}
76+
77+
return color;
78+
}
79+
4280
class TimeScaleDraw: public QwtScaleDraw
4381
{
4482
virtual QwtText label(double v) const
@@ -82,6 +120,7 @@ PlotWidget::PlotWidget(PlotDataMapRef &datamap, QWidget *parent):
82120
_xy_mode(false),
83121
_transform_select_dialog(nullptr),
84122
_use_date_time_scale(false),
123+
_color_index(0),
85124
_zoom_enabled(true),
86125
_keep_aspect_ratio(true)
87126
{
@@ -369,12 +408,8 @@ bool PlotWidget::addCurve(const std::string &name)
369408

370409
curve->setStyle( _curve_style );
371410

372-
QColor color = data.getColorHint();
373-
if( color == Qt::black)
374-
{
375-
color = randomColorHint();
376-
data.setColorHint(color);
377-
}
411+
QColor color = getColorHint(&data);
412+
378413
curve->setPen( color, (_curve_style == QwtPlotCurve::Dots) ? 4 : 1.0 );
379414
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
380415

@@ -443,7 +478,6 @@ bool PlotWidget::addCurveXY(std::string name_x, std::string name_y,
443478
return false;
444479
}
445480

446-
PlotData& data = it->second;
447481
const auto qname = QString::fromStdString( name );
448482

449483
auto curve = new QwtPlotCurve( qname );
@@ -463,7 +497,7 @@ bool PlotWidget::addCurveXY(std::string name_x, std::string name_y,
463497

464498
curve->setStyle( _curve_style );
465499

466-
QColor color = randomColorHint();
500+
QColor color = getColorHint(nullptr);
467501

468502
curve->setPen( color, (_curve_style == QwtPlotCurve::Dots) ? 4 : 1.0 );
469503
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );

plotter_gui/plotwidget.h

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class PlotWidget : public QwtPlot
8383

8484
bool canvasEventFilter(QEvent *event);
8585

86+
QColor getColorHint(PlotData *data);
87+
8688
signals:
8789
void swapWidgetsRequested(PlotWidget* source, PlotWidget* destination);
8890
void rectChanged(PlotWidget* self, QRectF rect );
@@ -182,6 +184,9 @@ private slots:
182184

183185
bool _use_date_time_scale;
184186

187+
int _color_index;
188+
static int global_color_index;
189+
185190
PlotDataMapRef& _mapped_data;
186191
QString _default_transform;
187192
std::map<std::string, QString> _curves_transform;

plotter_gui/preferences_dialog.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) :
1616
else{
1717
ui->comboBoxTheme->setCurrentIndex(0);
1818
}
19+
20+
bool use_plot_color_index = settings.value("Preferences::use_plot_color_index", false).toBool();
21+
bool remember_color = settings.value("Preferences::remember_color", true).toBool();
22+
23+
ui->checkBoxRememberColor->setChecked(remember_color);
24+
ui->radioLocalColorIndex->setChecked(use_plot_color_index);
25+
ui->radioGlobalColorIndex->setChecked(!use_plot_color_index);
1926
}
2027

2128
PreferencesDialog::~PreferencesDialog()
@@ -29,4 +36,10 @@ void PreferencesDialog::on_buttonBox_accepted()
2936
settings.setValue("Preferences::theme",
3037
ui->comboBoxTheme->currentIndex() == 1 ? "style_dark" : "style_light");
3138

39+
settings.setValue("Preferences::remember_color",
40+
ui->checkBoxRememberColor->isChecked());
41+
42+
settings.setValue("Preferences::use_plot_color_index",
43+
ui->radioLocalColorIndex->isChecked());
44+
3245
}

plotter_gui/preferences_dialog.ui

+62-1
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>400</width>
9+
<width>450</width>
1010
<height>300</height>
1111
</rect>
1212
</property>
13+
<property name="minimumSize">
14+
<size>
15+
<width>450</width>
16+
<height>0</height>
17+
</size>
18+
</property>
1319
<property name="windowTitle">
1420
<string>Dialog</string>
1521
</property>
1622
<layout class="QVBoxLayout" name="verticalLayout">
1723
<item>
1824
<widget class="QGroupBox" name="groupBox">
25+
<property name="font">
26+
<font>
27+
<weight>50</weight>
28+
<bold>false</bold>
29+
</font>
30+
</property>
1931
<property name="title">
2032
<string>Preferences</string>
2133
</property>
@@ -41,6 +53,55 @@
4153
</item>
4254
</widget>
4355
</item>
56+
<item row="2" column="1">
57+
<widget class="QFrame" name="frame">
58+
<property name="frameShape">
59+
<enum>QFrame::StyledPanel</enum>
60+
</property>
61+
<property name="frameShadow">
62+
<enum>QFrame::Raised</enum>
63+
</property>
64+
<layout class="QVBoxLayout" name="verticalLayout_2">
65+
<property name="spacing">
66+
<number>0</number>
67+
</property>
68+
<item>
69+
<widget class="QRadioButton" name="radioGlobalColorIndex">
70+
<property name="text">
71+
<string>global color sequence</string>
72+
</property>
73+
<property name="checked">
74+
<bool>true</bool>
75+
</property>
76+
</widget>
77+
</item>
78+
<item>
79+
<widget class="QRadioButton" name="radioLocalColorIndex">
80+
<property name="text">
81+
<string>reset color sequence in each plot area</string>
82+
</property>
83+
</widget>
84+
</item>
85+
<item>
86+
<widget class="QCheckBox" name="checkBoxRememberColor">
87+
<property name="text">
88+
<string>remember curve color</string>
89+
</property>
90+
<property name="checked">
91+
<bool>true</bool>
92+
</property>
93+
</widget>
94+
</item>
95+
</layout>
96+
</widget>
97+
</item>
98+
<item row="2" column="0">
99+
<widget class="QLabel" name="label_2">
100+
<property name="text">
101+
<string>Curve color</string>
102+
</property>
103+
</widget>
104+
</item>
44105
</layout>
45106
</widget>
46107
</item>

0 commit comments

Comments
 (0)