Skip to main content

src/WhiskerToolbox/Plots/ScatterPlotWidget/Core/ScatterPlotState.hpp

src/WhiskerToolbox/Plots/ScatterPlotWidget/Core/ScatterPlotState.hpp

State class for ScatterPlotWidget. More…

Classes

Name
struct ScatterPlotStateData
Serializable state data for ScatterPlotWidget.
class ScatterPlotState
State class for ScatterPlotWidget.

Types

Name
enum class ScatterSelectionMode { SinglePoint, Polygon}
Selection interaction mode for the scatter plot.

Detailed Description

State class for ScatterPlotWidget.

See:

ScatterPlotState manages the serializable state for the ScatterPlotWidget, with a single source of truth for view state (zoom/pan) and axis ranges. HorizontalAxisState and VerticalAxisState hold full axis ranges; view state holds zoom/pan. Both axes are analog value axes (no time).

Types Documentation

enum ScatterSelectionMode

Enumerator Value Description
SinglePoint Ctrl+Click to select/deselect individual points.
Polygon Draw polygon to select multiple points.

Selection interaction mode for the scatter plot.

Source code

#ifndef SCATTER_PLOT_STATE_HPP
#define SCATTER_PLOT_STATE_HPP


#include "CorePlotting/CoordinateTransform/ViewStateData.hpp"
#include "CorePlotting/DataTypes/GlyphStyleData.hpp"
#include "EditorState/EditorState.hpp"

#include "Plots/Common/HorizontalAxisWidget/Core/HorizontalAxisStateData.hpp"
#include "Plots/Common/VerticalAxisWidget/Core/VerticalAxisStateData.hpp"
#include "ScatterAxisSource.hpp" 
#include "ScatterColorConfig.hpp"

#include <rfl.hpp>
#include <rfl/json.hpp>

#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <vector>

class GlyphStyleState;
class HorizontalAxisState;
class VerticalAxisState;

enum class ScatterSelectionMode {
    SinglePoint,
    Polygon     
};

struct ScatterPlotStateData {
    std::string instance_id;
    std::string display_name = "Scatter Plot";
    std::string background_color = "#1A1A1A";
    CorePlotting::ViewStateData view_state;
    HorizontalAxisStateData horizontal_axis;
    VerticalAxisStateData vertical_axis;

    std::optional<ScatterAxisSource> x_source;
    std::optional<ScatterAxisSource> y_source;
    bool show_reference_line = false;         
    bool color_by_group = true;               
    bool show_cluster_labels = false;         

    CorePlotting::GlyphStyleData glyph_style{CorePlotting::GlyphType::Circle, 5.0f, "#3388FF", 0.8f};

    ScatterColorConfigData color_config;

    std::string selection_mode = "single_point";

    rfl::Skip<std::vector<std::size_t>> selected_indices;
};

class ScatterPlotState : public EditorState {
    Q_OBJECT

public:
    explicit ScatterPlotState(QObject * parent = nullptr);
    ~ScatterPlotState() override;

    [[nodiscard]] QString getTypeName() const override { return QStringLiteral("ScatterPlotWidget"); }
    [[nodiscard]] QString getDisplayName() const override;
    void setDisplayName(QString const & name) override;

    [[nodiscard]] QString getBackgroundColor() const;
    void setBackgroundColor(QString const & hex_color);

    // === Axis state access (for widgets and serialization) ===
    [[nodiscard]] HorizontalAxisState * horizontalAxisState();
    [[nodiscard]] VerticalAxisState * verticalAxisState();

    // === View state (zoom / pan / bounds) ===
    [[nodiscard]] CorePlotting::ViewStateData const & viewState() const { return _data.view_state; }
    void setXZoom(double zoom);
    void setYZoom(double zoom);
    void setPan(double x_pan, double y_pan);
    void setXBounds(double x_min, double x_max);
    void setYBounds(double y_min, double y_max);

    // === Data source configuration ===
    [[nodiscard]] std::optional<ScatterAxisSource> const & xSource() const { return _data.x_source; }
    [[nodiscard]] std::optional<ScatterAxisSource> const & ySource() const { return _data.y_source; }
    void setXSource(std::optional<ScatterAxisSource> source);
    void setYSource(std::optional<ScatterAxisSource> source);

    // === Reference line ===
    [[nodiscard]] bool showReferenceLine() const { return _data.show_reference_line; }
    void setShowReferenceLine(bool show);

    // === Color by group ===
    [[nodiscard]] bool colorByGroup() const { return _data.color_by_group; }
    void setColorByGroup(bool enabled);

    // === Cluster labels ===
    [[nodiscard]] bool showClusterLabels() const { return _data.show_cluster_labels; }
    void setShowClusterLabels(bool enabled);

    // === Selection mode ===
    [[nodiscard]] ScatterSelectionMode selectionMode() const;
    void setSelectionMode(ScatterSelectionMode mode);

    // === Selection state ===
    [[nodiscard]] std::vector<std::size_t> const & selectedIndices() const { return _data.selected_indices.value(); }
    void setSelectedIndices(std::vector<std::size_t> indices);
    void addSelectedIndex(std::size_t index);
    void removeSelectedIndex(std::size_t index);
    void clearSelection();
    [[nodiscard]] bool isSelected(std::size_t index) const;

    // === Glyph style ===
    [[nodiscard]] GlyphStyleState * glyphStyleState();

    // === Feature color config ===
    [[nodiscard]] ScatterColorConfigData const & colorConfig() const { return _data.color_config; }
    void setColorConfig(ScatterColorConfigData config);

    // === Serialization ===
    [[nodiscard]] std::string toJson() const override;
    bool fromJson(std::string const & json) override;

signals:
    void viewStateChanged();
    void xSourceChanged();
    void ySourceChanged();
    void referenceLineChanged();
    void colorByGroupChanged();
    void clusterLabelsChanged();
    void glyphStyleChanged();
    void selectionModeChanged();
    void selectionChanged();
    void colorConfigChanged();
    void backgroundColorChanged(QString const & hex_color);

private:
    ScatterPlotStateData _data;
    std::unique_ptr<HorizontalAxisState> _horizontal_axis_state;
    std::unique_ptr<VerticalAxisState> _vertical_axis_state;
    std::unique_ptr<GlyphStyleState> _glyph_style_state;
};

#endif// SCATTER_PLOT_STATE_HPP

Updated on 2026-11-13 at 20:25:45 +0000