16#include <QFontMetrics>
18#include <QApplication>
19#include <QStyleOptionToolButton>
20#include <QPainterPath>
21#include <QColorDialog>
29#include <kconfiggroup.h>
30#include <ksharedconfig.h>
41 QLinearGradient defaultGradient;
45 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
46 setFocusPolicy(Qt::WheelFocus);
47 setMouseTracking(
true);
52 QWindow *window = this->window()->windowHandle();
61 QFontMetrics fm(font());
62 const int h = qMax(15,
static_cast<int>(std::ceil(fm.height() * 0.75)));
87 QPainter painter(
this);
95 painter.setRenderHint(QPainter::Antialiasing,
true);
97 const bool hasFocus = this->hasFocus();
98 const QColor highlightColor =
palette().color(QPalette::Highlight);
100 for (
int i = 0; i < handlePositions.count(); ++i) {
114 QPointF(handlesRect.left() + handlePositions[i].position * handlesRect.width(), handlesRect.top()),
118 { colorType, handlePositions[i].color.toQColor() }
132 QPointF(handlesRect.left() + handlePositions[
m_selectedStop].position * handlesRect.width(), handlesRect.top()),
134 true,
false, hasFocus,
140 painter.setPen(
palette().color(QPalette::Mid));
141 painter.drawRect(previewRect);
148 qreal minDistance = tolerance;
150 for (
int i = 0; i < stops.size(); i++) {
167 QWidget::mousePressEvent(e);
171 if (e->buttons() != Qt::LeftButton ) {
172 QWidget::mousePressEvent(e);
177 const qreal t = (qreal(e->x()) - handlesRect.x()) / handlesRect.width();
182 if (clickedStop >= 0) {
212 for (
int i = 0; i < stops.size(); i++) {
213 if (stop.
position <= stops[i].position)
break;
230 if (stops.size() > 2 && !augmentedRect.contains(e->pos()))
239 double t = qreal(e->pos().x() - handlesRect.left()) / handlesRect.width();
241 if (augmentedRect.contains(e->pos())) {
251 draggedStop.
position = qBound(0.0, t, 1.0);
255 stops.insert(newPos, draggedStop);
268 QWidget::mouseMoveEvent(e);
274 if (e->button() != Qt::LeftButton) {
275 QWidget::mouseDoubleClickEvent(e);
280 const qreal t = (qreal(e->x()) - handlesRect.x()) / handlesRect.width();
290 if (direction == 0) {
294 if (modifiers & Qt::ControlModifier) {
298 const qreal increment = modifiers & Qt::ShiftModifier ? 0.001 : 0.01;
300 draggedStop.
position += direction < 0 ? -increment : increment;
305 stops.insert(newPos, draggedStop);
315 if (e->angleDelta().y() != 0) {
319 QWidget::wheelEvent(e);
340 QWidget::keyPressEvent(e);
349 QWidget::leaveEvent(e);
354 const bool isInAllowedRegion =
357 if (isInAllowedRegion) {
359 const qreal t = (qreal(pos.x()) - handlesRect.x()) / handlesRect.width();
372 t = qBound(0.0, t, 1.0);
383 stops.insert(newPos, stop);
392 const qreal handleWidthOverTwo =
static_cast<qreal
>(
m_handleSize.width()) / 2.0;
393 const int hMargin =
static_cast<int>(std::ceil(handleWidthOverTwo)) + 2;
394 return rect().adjusted(hMargin, 0, -hMargin, 0);
400 return rc.adjusted(0, 0, 0, -
m_handleSize.height() - 4);
406 return rc.adjusted(0, rc.height() - (
m_handleSize.height() + 2), 0, -2);
443 }
else if (m_selectedStop < m_gradient->stops().size() - 1) {
456 if (stops.size() <= 2) {
462 if (selectNeighborStop) {
473 QFontMetrics fm(font());
474 const int h = fm.height();
476 QStyleOptionToolButton opt;
477 QSize sz = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(h, h),
this);
485 return QSize(2 * h, h);
497 if (m_selectedStop < 0 || m_selectedStop >= stops.count()) {
501 KConfigGroup cfg = KSharedConfig::openConfig()->group(
"colorselector");
502 bool usePlatformDialog = cfg.readEntry(
"UsePlatformColorDialog",
false);
503 QDialog *colorDialog =
nullptr;
505 if (!usePlatformDialog) {
509 auto setColorFn = [dialog, stops,
this]()
mutable
518 connect(dialog, &QDialog::accepted, setColorFn);
519 colorDialog = dialog;
521 QColorDialog *dialog =
new QColorDialog(
this);
523 auto setColorFn = [dialog, stops,
this]()
mutable
531 connect(dialog, &QColorDialog::currentColorChanged, setColorFn);
532 connect(dialog, &QDialog::accepted, setColorFn);
533 colorDialog = dialog;
536 connect(colorDialog, &QDialog::rejected, [stops,
this]()
542 colorDialog->setAttribute(Qt::WA_DeleteOnClose);
544 colorDialog->raise();
545 colorDialog->activateWindow();
int findNearestHandle(qreal t, const qreal tolerance, const QList< KoGradientStop > &stops)
int getNewInsertPosition(const KoGradientStop &stop, const QList< KoGradientStop > &stops)
qreal distance(const QPointF &p1, const QPointF &p2)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
The KisInternalColorSelector class.
void signalForegroundColorChosen(KoColor color)
signalForegroundColorChosen The most important signal. This will sent out when a color has been chose...
void wheelEvent(QWheelEvent *e) override
int handleClickTolerance() const
void paintEvent(QPaintEvent *) override
void sigSelectedStop(int stop)
KoGradientStop m_removedStop
KoStopGradientSP m_gradient
void selectPreviousStop()
void mouseMoveEvent(QMouseEvent *e) override
KisSignalCompressor m_updateCompressor
void setGradientResource(KoStopGradientSP gradient)
void updateHoveredStop(const QPoint &pos)
void leaveEvent(QEvent *e) override
QSize sizeHint() const override
KisStopGradientSlider(QWidget *parent=0, Qt::WindowFlags f=Qt::WindowFlags())
QRegion allowedClickRegion(int tolerance) const
QRect handlesStripeRect() const
void mouseReleaseEvent(QMouseEvent *e) override
void mouseDoubleClickEvent(QMouseEvent *e) override
KoStopGradientSP m_defaultGradient
static constexpr int removeStopDistance
void chooseSelectedStopColor()
void deleteSelectedStop(bool selectNeighborStop=true)
int minimalHeight() const
void insertStop(double t)
QRect gradientStripeRect() const
void setSelectedStop(int selected)
QSize minimumSizeHint() const override
void keyPressEvent(QKeyEvent *e) override
void mousePressEvent(QMouseEvent *e) override
void handleIncrementInput(int direction, Qt::KeyboardModifiers modifiers)
static QSharedPointer< KoStopGradient > fromQGradient(const QGradient *gradient)
Creates KoStopGradient from a QGradient.
#define KIS_ASSERT_RECOVER(cond)
T kisGrowRect(const T &rect, U offset)
rgba palette[MAX_PALETTE]