Skip to content

Commit

Permalink
Fix issues #196 and #236: allow user to use deterministic color sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide authored Dec 19, 2019
1 parent e9c14a4 commit cab01d8
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 51 deletions.
27 changes: 0 additions & 27 deletions include/PlotJuggler/random_color.h

This file was deleted.

24 changes: 22 additions & 2 deletions plotter_gui/curvecolorpick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <QColorDialog>


CurveColorPick::CurveColorPick(const std::map<std::string, QColor> &mapped_colors, QWidget *parent) :
CurveColorPick::CurveColorPick(const std::map<std::string, QColor> &mapped_colors,
QWidget *parent) :
QDialog(parent),
ui(new Ui::CurveColorPick),
_any_modified(false),
Expand All @@ -23,16 +24,35 @@ CurveColorPick::CurveColorPick(const std::map<std::string, QColor> &mapped_color
_color_wheel->setMinimumWidth(150);
_color_wheel->setMinimumHeight(150);

ui->verticalLayoutRight->insertWidget(1, new QLabel("Default colors", this) );

_color_palette = new color_widgets::Swatch(this);
ui->verticalLayoutRight->insertWidget(2, _color_palette );
_color_palette->setMinimumWidth(150);
_color_palette->setMinimumHeight(30);
_color_palette->setMaximumHeight(30);

ui->verticalLayoutRight->insertWidget(3, new QLabel("Preview", this) );

_color_preview = new color_widgets::ColorPreview(this);
ui->verticalLayoutRight->insertWidget(1, _color_preview );
ui->verticalLayoutRight->insertWidget(4, _color_preview );
_color_preview->setMinimumWidth(150);
_color_preview->setMinimumHeight(100);

QVector<QColor> colors = {QColor("#1f77b4"), QColor("#d62728"), QColor("#1ac938"), QColor("#ff7f0e"),
QColor("#f14cc1"), QColor("#9467bd"), QColor("#17becf"), QColor("#bcbd22")};

color_widgets::ColorPalette palette(colors, "default colors", 8);
_color_palette->setPalette(palette);

connect(_color_wheel, &color_widgets::ColorWheel::colorChanged,
_color_preview, &color_widgets::ColorPreview::setColor );

connect(_color_wheel, &color_widgets::ColorWheel::colorChanged,
this, &CurveColorPick::on_colorChanged );

connect(_color_palette, &color_widgets::Swatch::colorSelected,
_color_wheel, &color_widgets::ColorWheel::setColor );
}

CurveColorPick::~CurveColorPick()
Expand Down
7 changes: 6 additions & 1 deletion plotter_gui/curvecolorpick.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <QListWidgetItem>
#include "color_wheel.hpp"
#include "color_preview.hpp"
#include "color_palette.hpp"
#include "swatch.hpp"


namespace Ui {
class CurveColorPick;
Expand All @@ -15,7 +18,8 @@ class CurveColorPick : public QDialog
Q_OBJECT

public:
explicit CurveColorPick(const std::map<std::string, QColor>& mapped_colors, QWidget *parent = 0);
explicit CurveColorPick(const std::map<std::string, QColor>& mapped_colors,
QWidget *parent = 0);
~CurveColorPick();

bool anyColorModified() const ;
Expand All @@ -36,6 +40,7 @@ private slots:
Ui::CurveColorPick *ui;
color_widgets::ColorWheel *_color_wheel;
color_widgets::ColorPreview *_color_preview;
color_widgets::Swatch *_color_palette;

const std::map<std::string, QColor>& _mapped_colors;
bool _any_modified;
Expand Down
2 changes: 1 addition & 1 deletion plotter_gui/curvecolorpick.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>Color Selector</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
Expand Down
5 changes: 0 additions & 5 deletions plotter_gui/curvelist_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,6 @@ void CurveListPanel::on_radioRegExp_toggled(bool checked)
}
}

void CurveListPanel::on_checkBoxCaseSensitive_toggled(bool )
{
updateFilter();
}

void CurveListPanel::on_lineEdit_textChanged(const QString &search_string)
{
bool updated = false;
Expand Down
2 changes: 0 additions & 2 deletions plotter_gui/curvelist_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ private slots:

void on_radioRegExp_toggled(bool checked);

void on_checkBoxCaseSensitive_toggled(bool checked);

void on_lineEdit_textChanged(const QString &search_string);

void on_pushButtonSettings_toggled(bool checked);
Expand Down
9 changes: 6 additions & 3 deletions plotter_gui/curvelist_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ bool CurvesView::eventFilterBase(QObject *object, QEvent *event)
QByteArray mdata;
QDataStream stream(&mdata, QIODevice::WriteOnly);

for (const auto &curve_name : getSelectedNames())
auto selected_names = getSelectedNames();
std::sort( selected_names.begin(), selected_names.end() );

for (const auto &curve_name : selected_names)
{
stream << QString::fromStdString(curve_name);
}
Expand All @@ -259,9 +262,9 @@ bool CurvesView::eventFilterBase(QObject *object, QEvent *event)
}
else
{
if (getSelectedNames().size() != 2)
if (selected_names.size() != 2)
{
if (getSelectedNames().size() >= 1)
if (selected_names.size() >= 1)
{
QMessageBox::warning(
table_widget, "New in version 2.3+",
Expand Down
52 changes: 43 additions & 9 deletions plotter_gui/plotwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,50 @@
#include "qwt_plot_renderer.h"
#include "qwt_series_data.h"
#include "qwt_date_scale_draw.h"
#include "PlotJuggler/random_color.h"
#include "point_series_xy.h"
#include "suggest_dialog.h"
#include "transforms/custom_function.h"
#include "transforms/custom_timeseries.h"

int PlotWidget::global_color_index = 0;

QColor PlotWidget::getColorHint(PlotData* data)
{
QSettings settings;
bool remember_color = settings.value("Preferences::remember_color", true).toBool();
if( data && remember_color && data->getColorHint() != Qt::black )
{
return data->getColorHint();
}
QColor color;
bool use_plot_color_index = settings.value("Preferences::use_plot_color_index", false).toBool();
int index = _curve_list.size();

if( !use_plot_color_index )
{
index = (PlotWidget::global_color_index++);
}

// https://matplotlib.org/3.1.1/users/dflt_style_changes.html
switch( index%8 )
{
case 0: color = QColor("#1f77b4"); break;
case 1: color = QColor("#d62728"); break;
case 2: color = QColor("#1ac938"); break;
case 3: color = QColor("#ff7f0e"); break;

case 4: color = QColor("#f14cc1"); break;
case 5: color = QColor("#9467bd"); break;
case 6: color = QColor("#17becf"); break;
case 7: color = QColor("#bcbd22"); break;
}
if( data ){
data->setColorHint(color);
}

return color;
}

class TimeScaleDraw: public QwtScaleDraw
{
virtual QwtText label(double v) const
Expand Down Expand Up @@ -82,6 +120,7 @@ PlotWidget::PlotWidget(PlotDataMapRef &datamap, QWidget *parent):
_xy_mode(false),
_transform_select_dialog(nullptr),
_use_date_time_scale(false),
_color_index(0),
_zoom_enabled(true),
_keep_aspect_ratio(true)
{
Expand Down Expand Up @@ -369,12 +408,8 @@ bool PlotWidget::addCurve(const std::string &name)

curve->setStyle( _curve_style );

QColor color = data.getColorHint();
if( color == Qt::black)
{
color = randomColorHint();
data.setColorHint(color);
}
QColor color = getColorHint(&data);

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

Expand Down Expand Up @@ -443,7 +478,6 @@ bool PlotWidget::addCurveXY(std::string name_x, std::string name_y,
return false;
}

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

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

curve->setStyle( _curve_style );

QColor color = randomColorHint();
QColor color = getColorHint(nullptr);

curve->setPen( color, (_curve_style == QwtPlotCurve::Dots) ? 4 : 1.0 );
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
Expand Down
5 changes: 5 additions & 0 deletions plotter_gui/plotwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class PlotWidget : public QwtPlot

bool canvasEventFilter(QEvent *event);

QColor getColorHint(PlotData *data);

signals:
void swapWidgetsRequested(PlotWidget* source, PlotWidget* destination);
void rectChanged(PlotWidget* self, QRectF rect );
Expand Down Expand Up @@ -182,6 +184,9 @@ private slots:

bool _use_date_time_scale;

int _color_index;
static int global_color_index;

PlotDataMapRef& _mapped_data;
QString _default_transform;
std::map<std::string, QString> _curves_transform;
Expand Down
13 changes: 13 additions & 0 deletions plotter_gui/preferences_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) :
else{
ui->comboBoxTheme->setCurrentIndex(0);
}

bool use_plot_color_index = settings.value("Preferences::use_plot_color_index", false).toBool();
bool remember_color = settings.value("Preferences::remember_color", true).toBool();

ui->checkBoxRememberColor->setChecked(remember_color);
ui->radioLocalColorIndex->setChecked(use_plot_color_index);
ui->radioGlobalColorIndex->setChecked(!use_plot_color_index);
}

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

settings.setValue("Preferences::remember_color",
ui->checkBoxRememberColor->isChecked());

settings.setValue("Preferences::use_plot_color_index",
ui->radioLocalColorIndex->isChecked());

}
63 changes: 62 additions & 1 deletion plotter_gui/preferences_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<width>450</width>
<height>300</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>450</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="title">
<string>Preferences</string>
</property>
Expand All @@ -41,6 +53,55 @@
</item>
</widget>
</item>
<item row="2" column="1">
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QRadioButton" name="radioGlobalColorIndex">
<property name="text">
<string>global color sequence</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioLocalColorIndex">
<property name="text">
<string>reset color sequence in each plot area</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBoxRememberColor">
<property name="text">
<string>remember curve color</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Curve color</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit cab01d8

Please sign in to comment.