Skip to main content

src/WhiskerToolbox/Plots/PSTHWidget/Core/PSTHState.hpp

src/WhiskerToolbox/Plots/PSTHWidget/Core/PSTHState.hpp

State class for PSTHWidget. More…

Classes

Name
struct PSTHEventOptions
Options for plotting an event series in the PSTH plot.
struct PSTHStateData
Serializable state data for PSTHWidget.
class PSTHState
State class for PSTHWidget.

Types

Name
enum class PSTHStyle { Bar, Line}
Enumeration for PSTH plot style.

Detailed Description

State class for PSTHWidget.

See: EditorState for base class documentation

PSTHState manages the serializable state for the PSTHWidget, enabling workspace save/restore and inter-widget communication via SelectionContext.

Types Documentation

enum PSTHStyle

Enumerator Value Description
Bar Bar chart style (default).
Line Line plot style.

Enumeration for PSTH plot style.

Source code

#ifndef PSTH_STATE_HPP
#define PSTH_STATE_HPP


#include "CorePlotting/CoordinateTransform/ViewStateData.hpp"
#include "EditorState/EditorState.hpp"
#include "Plots/Common/EventRateEstimation/EstimationParams.hpp"
#include "Plots/Common/EventRateEstimation/RateEstimate.hpp"
#include "Plots/Common/PlotAlignmentWidget/Core/PlotAlignmentData.hpp"
#include "Plots/Common/PlotAlignmentWidget/Core/PlotAlignmentState.hpp"
#include "Plots/Common/RelativeTimeAxisWidget/Core/RelativeTimeAxisState.hpp"
#include "Plots/Common/RelativeTimeAxisWidget/Core/RelativeTimeAxisStateData.hpp"
#include "Plots/Common/VerticalAxisWidget/Core/VerticalAxisState.hpp"
#include "Plots/Common/VerticalAxisWidget/Core/VerticalAxisStateData.hpp"

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

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


enum class PSTHStyle {
    Bar,
    Line
};

struct PSTHEventOptions {
    std::string event_key;            
    std::string hex_color = "#000000";
};

struct PSTHStateData {
    std::string instance_id;
    std::string display_name = "PSTH Plot";
    std::string background_color = "#1A1A1A";                                                 
    PlotAlignmentData alignment;                                                              
    std::map<std::string, PSTHEventOptions> plot_events;                                      
    PSTHStyle style = PSTHStyle::Bar;                                                         
    Neuralyzer::Plots::EstimationParams estimation_params;                                
    Neuralyzer::Plots::ScalingMode scaling = Neuralyzer::Plots::ScalingMode::RawCount;
    CorePlotting::ViewStateData view_state;                                                   
    RelativeTimeAxisStateData time_axis;                                                      
    VerticalAxisStateData vertical_axis;                                                      
};

class PSTHState : public EditorState {
    Q_OBJECT

public:
    explicit PSTHState(QObject * parent = nullptr);

    ~PSTHState() override = default;

    // === Type Identification ===

    [[nodiscard]] QString getTypeName() const override { return QStringLiteral("PSTHWidget"); }

    [[nodiscard]] QString getDisplayName() const override;

    void setDisplayName(QString const & name) override;

    // === Alignment Event ===

    [[nodiscard]] QString getAlignmentEventKey() const;

    void setAlignmentEventKey(QString const & key);

    // === Interval Alignment ===

    [[nodiscard]] IntervalAlignmentType getIntervalAlignmentType() const;

    void setIntervalAlignmentType(IntervalAlignmentType type);

    // === Offset ===

    [[nodiscard]] double getOffset() const;

    void setOffset(double offset);

    // === Window Size ===

    [[nodiscard]] double getWindowSize() const;

    void setWindowSize(double window_size);

    [[nodiscard]] PlotAlignmentState * alignmentState() { return _alignment_state.get(); }

    [[nodiscard]] RelativeTimeAxisState * relativeTimeAxisState() { return _relative_time_axis_state.get(); }

    [[nodiscard]] VerticalAxisState * verticalAxisState() { return _vertical_axis_state.get(); }

    // === Plot Events Management ===

    void addPlotEvent(QString const & event_name, QString const & event_key);

    void removePlotEvent(QString const & event_name);

    [[nodiscard]] std::vector<QString> getPlotEventNames() const;

    [[nodiscard]] std::optional<PSTHEventOptions> getPlotEventOptions(QString const & event_name) const;

    void updatePlotEventOptions(QString const & event_name, PSTHEventOptions const & options);

    // === Global Plot Options ===

    [[nodiscard]] PSTHStyle getStyle() const;

    void setStyle(PSTHStyle style);

    [[nodiscard]] Neuralyzer::Plots::EstimationParams const & estimationParams() const;

    void setEstimationParams(Neuralyzer::Plots::EstimationParams const & params);

    [[nodiscard]] Neuralyzer::Plots::ScalingMode scaling() const;

    void setScaling(Neuralyzer::Plots::ScalingMode mode);

    [[nodiscard]] QString getBackgroundColor() const;

    void setBackgroundColor(QString const & hex_color);

    // === 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);

    // === Serialization ===

    [[nodiscard]] std::string toJson() const override;

    bool fromJson(std::string const & json) override;

signals:
    void alignmentEventKeyChanged(QString const & key);

    void intervalAlignmentTypeChanged(IntervalAlignmentType type);

    void offsetChanged(double offset);

    void windowSizeChanged(double window_size);

    void plotEventAdded(QString const & event_name);

    void plotEventRemoved(QString const & event_name);

    void plotEventOptionsChanged(QString const & event_name);

    void styleChanged(PSTHStyle style);

    void estimationParamsChanged();

    void scalingChanged(Neuralyzer::Plots::ScalingMode mode);

    void backgroundColorChanged(QString const & hex_color);

    void viewStateChanged();

private:
    PSTHStateData _data;
    std::unique_ptr<PlotAlignmentState> _alignment_state;
    std::unique_ptr<RelativeTimeAxisState> _relative_time_axis_state;
    std::unique_ptr<VerticalAxisState> _vertical_axis_state;
};

#endif// PSTH_STATE_HPP

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