Skip to content

Commit

Permalink
Allow aligning color wheel widget to the top
Browse files Browse the repository at this point in the history
Instead of the center, since that may match expectations better
depending on where in the UI the wheel is placed. Various other widgets
tend to grow downwards, not in both directions, so this fits better in
with those. The default continues to be off.
  • Loading branch information
askmeaboutlo0m committed Aug 23, 2024
1 parent 4aace61 commit ee72d3e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/desktop/bundled/QtColorWidgets/color_wheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ void ColorWheel::paintEvent(QPaintEvent * )

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(geometry().width()/2,geometry().height()/2);

int width = geometry().width();
int height = geometry().height();
int x = width / 2;
int y = height > width && p->align_top ? x : height / 2;
painter.translate(x, y);

// hue wheel
if(p->hue_ring.isNull())
Expand Down Expand Up @@ -285,6 +290,11 @@ bool ColorWheel::mirroredSelector() const
return p->mirrored_selector;
}

bool ColorWheel::alignTop() const
{
return p->align_top;
}


void ColorWheel::setColorSpace(color_widgets::ColorWheel::ColorSpaceEnum space)
{
Expand Down Expand Up @@ -351,6 +361,16 @@ void ColorWheel::setMirroredSelector(bool mirrored)
}
}

void ColorWheel::setAlignTop(bool top)
{
if ( top != p->align_top )
{
p->align_top = top;
update();
Q_EMIT alignTopChanged(top);
}
}

void ColorWheel::dragEnterEvent(QDragEnterEvent* event)
{
if ( event->mimeData()->hasColor() ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class QCP_EXPORT ColorWheel : public QWidget
Q_PROPERTY(bool rotatingSelector READ rotatingSelector WRITE setRotatingSelector NOTIFY rotatingSelectorChanged DESIGNABLE true )
Q_PROPERTY(ColorSpaceEnum colorSpace READ colorSpace WRITE setColorSpace NOTIFY colorSpaceChanged DESIGNABLE true )
Q_PROPERTY(bool mirroredSelector READ mirroredSelector WRITE setMirroredSelector NOTIFY mirroredSelectorChanged DESIGNABLE true )
Q_PROPERTY(bool alignTop READ alignTop WRITE setAlignTop NOTIFY alignTop DESIGNABLE false )

public:
enum ShapeEnum
Expand Down Expand Up @@ -93,6 +94,9 @@ class QCP_EXPORT ColorWheel : public QWidget
/// Whether the internal selector's should be mirrored horizontally
bool mirroredSelector() const;

/// Whether the color wheel is aligned to the top or the center
bool alignTop() const;

public Q_SLOTS:

/// Set current color
Expand Down Expand Up @@ -125,6 +129,9 @@ public Q_SLOTS:
/// Sets whether the internal selector should be mirrored horizontally
void setMirroredSelector(bool mirrored);

/// Sets whether the wheel should be aligned to the top or the center
void setAlignTop(bool top);

Q_SIGNALS:
/**
* Emitted when the user selects a color or setColor is called
Expand All @@ -146,6 +153,8 @@ public Q_SLOTS:

void mirroredSelectorChanged(bool mirrored);

void alignTopChanged(bool alignTop);

/**
* Emitted when the user releases from dragging
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ColorWheel::Private
QColor (*rainbow_from_hue)(qreal);
int max_size = 128;
bool mirrored_selector = false;
bool align_top = false;
qreal device_pixel_ratio = 1.0;

Private(ColorWheel *widget)
Expand Down Expand Up @@ -94,7 +95,11 @@ class ColorWheel::Private
/// return line from center to given point
QLineF line_to_point(const QPoint &p) const
{
return QLineF (w->geometry().width()/2, w->geometry().height()/2, p.x(), p.y());
int width = w->geometry().width();
int height = w->geometry().height();
int x1 = width / 2;
int y1 = height > width && align_top ? x1 : height / 2;
return QLineF (x1, y1, p.x(), p.y());
}

/**
Expand Down

0 comments on commit ee72d3e

Please sign in to comment.