Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ template <> struct WidgetRenderer<meta::ColorGradient>
widget);
layout->addWidget(picker);

widget->set_sync_from_model([picker]() { picker->update(); });
widget->set_sync_from_model(
[picker]()
{
picker->update_bar();
picker->update();
});

// Live edits
QObject::connect(picker,
Expand Down
100 changes: 64 additions & 36 deletions MetaUI/qt/include/meta_qt/widgets/gradient_picker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,68 @@

#include "meta/ext/color_gradient/color_gradient.hpp"

class QScrollArea;

namespace meta::qt
{

class PresetGridWidget;

// ---------------------------------------------------------------------------
// GradientBarWidget
//
// Custom-painted gradient bar with interactive stop handles.
// ---------------------------------------------------------------------------

class GradientBarWidget : public QWidget
{
Q_OBJECT

public:
static constexpr int BAR_H = 28;
static constexpr int STOP_R = 6;
static constexpr int PAD = 10;
static constexpr int TOTAL_H = PAD + BAR_H + STOP_R * 2 + 6;
static constexpr int RADIUS = 4;

explicit GradientBarWidget(std::vector<Stop> &stops,
QWidget *parent = nullptr);

void sort_stops();

Q_SIGNALS:
void value_changed();
void edit_ended();

protected:
void paintEvent(QPaintEvent *) override;
void mouseDoubleClickEvent(QMouseEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override;

private:
QRectF bar_rect() const;
QRectF stop_rect(const Stop &s) const;
int hit_test(const QPoint &pos) const;

std::vector<Stop> &stops_;
int selected_idx_ = -1;
bool dragging_ = false;
};

// ---------------------------------------------------------------------------
// GradientPicker
//
// A self-contained gradient editor widget.
//
// Gradient bar:
// Double-click empty area → add stop at cursor position
// Double-click stop → open QColorDialog to edit colour
// Left-drag stop → move stop
// Right-click stop → remove stop (min 2 kept)
// Top: Gradient bar with draggable stops (double-click to add/edit,
// drag to move, right-click to remove).
// Bottom: Vertically scrollable grid of preset swatches.
//
// Below the bar: a horizontal preset strip (always visible).
// Below that: a "Shuffle colors" button.
// The entire content is held within a vertical QScrollArea so that reducing
// the widget height never squashes or clips the gradient visualization.
// ---------------------------------------------------------------------------

class GradientPicker : public QWidget
Expand All @@ -40,49 +86,31 @@ class GradientPicker : public QWidget
// Called externally when the attribute's preset list changes.
void set_presets(const std::vector<Preset> &presets);

// Updates the gradient bar visualization from external model changes.
void update_bar();

Q_SIGNALS:
void value_changed(); // every incremental edit
void edit_ended(); // committed (drag release, colour picked, preset applied)

protected:
void paintEvent(QPaintEvent *) override;
void mouseDoubleClickEvent(QMouseEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
bool eventFilter(QObject *watched, QEvent *event) override;
QSize sizeHint() const override;
QSize minimumSizeHint() const override;

private:
// --- stop helpers

QRectF bar_rect() const; // gradient bar area
QRectF stop_rect(const Stop &s) const;
int hit_test(const QPoint &pos) const;
void sort_stops();
void rebuild_preset_strip();

// --- colour conversion

static QColor to_qcolor(const std::array<float, 4> &c);
static std::array<float, 4> from_qcolor(const QColor &c);
void rebuild_preset_grid();

std::vector<Stop> &stops_;
std::vector<Preset> presets_;

int selected_idx_ = -1;
bool dragging_ = false;

// Preset strip: one swatch widget per preset, held in a scroll area.
// Rebuilt whenever presets_ changes.
QWidget *preset_strip_ = nullptr;
GradientBarWidget *bar_widget_ = nullptr;
QScrollArea *scroll_area_ = nullptr;
PresetGridWidget *preset_grid_ = nullptr;

static constexpr int BAR_H = 28; // gradient bar height
static constexpr int STOP_R = 6; // stop handle radius
static constexpr int PAD = 10;
static constexpr int STRIP_H = 36; // preset strip height
static constexpr int SWATCH_W = 60; // each preset swatch width
static constexpr int RADIUS = 4;
static constexpr int SWATCH_H = 32; // each preset swatch height
};

} // namespace meta::qt
Loading