19 #include <QStyleOptionFrameV3> 20 #include <QMouseEvent> 22 #define MARKER_WIDTH 11 23 #define MARKER_HEIGHT 14 24 #define MARKER_GAP 1.5 25 #define MARGIN_BOTTOM ( MARKER_HEIGHT + 2 ) 26 #define MARGIN_X ( MARKER_WIDTH / 2 ) 27 #define FRAME_MARGIN 2 28 #define CLICK_THRESHOLD ( MARKER_WIDTH / 2 + 3 ) 36 mStops = mGradient.
stops();
62 mStops = mGradient.
stops();
71 return QSize( 200, 80 );
86 option.state =
hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
87 option.rect = frameRect;
95 option.state = QStyle::State_KeyboardFocusChange;
96 option.rect = frameRect;
103 painter.
setPen( Qt::NoPen );
112 for (
int i = 0; i < box.width() + 1; ++i )
114 QPen pen( mGradient.
color( static_cast< double >( i ) / box.width() ) );
116 painter.
drawLine( box.left() + i, box.top(), box.left() + i, box.height() + 1 );
120 int markerTop = frameRect.
bottom() + 1;
121 drawStopMarker( painter,
QPoint( box.left(), markerTop ), mGradient.
color1(), mSelectedStop == 0 );
122 drawStopMarker( painter,
QPoint( box.right(), markerTop ), mGradient.
color2(), mSelectedStop == mGradient.
count() - 1 );
126 int x = stop.
offset * box.width() + box.left();
127 drawStopMarker( painter,
QPoint( x, markerTop ), stop.
color, mSelectedStop == i );
136 if ( index > 0 && index < mGradient.
count() - 1 )
143 if ( stop == selectedStop )
151 mSelectedStop =
index;
158 if ( mSelectedStop > 0 && mSelectedStop < mGradient.
count() - 1 )
160 return mStops.
at( mSelectedStop - 1 );
162 else if ( mSelectedStop == 0 )
174 if ( mSelectedStop > 0 && mSelectedStop < mGradient.
count() - 1 )
176 mStops[ mSelectedStop - 1 ].color = color;
179 else if ( mSelectedStop == 0 )
193 if ( mSelectedStop > 0 && mSelectedStop < mGradient.
count() - 1 )
195 mStops[ mSelectedStop - 1 ].offset = offset;
204 if ( mSelectedStop > 0 && mSelectedStop < mGradient.
count() - 1 )
206 mStops[ mSelectedStop - 1 ].color = color;
207 mStops[ mSelectedStop - 1 ].offset = offset;
210 else if ( mSelectedStop == 0 )
225 if ( selectedStopIsMovable() )
228 double stopOffset = mStops.
at( mSelectedStop - 1 ).offset;
229 mStops.
removeAt( mSelectedStop - 1 );
232 int closest = findClosestStop( relativePositionToPoint( stopOffset ) );
256 if ( e->
buttons() & Qt::LeftButton )
258 if ( selectedStopIsMovable() )
260 double offset = pointToRelativePosition( e->
pos().
x() );
264 mStops[ mSelectedStop - 1 ].offset = offset;
274 int QgsGradientStopEditor::findClosestStop(
int x,
int threshold )
const 276 int closestStop = -1;
277 int closestDiff = INT_MAX;
278 int currentDiff = INT_MAX;
286 currentDiff = qAbs( relativePositionToPoint( stop.
offset ) + 1 -
x );
287 if (( threshold < 0 || currentDiff < threshold ) && currentDiff < closestDiff )
290 closestDiff = currentDiff;
296 currentDiff = qAbs( relativePositionToPoint( 0.0 ) + 1 -
x );
297 if (( threshold < 0 || currentDiff < threshold ) && currentDiff < closestDiff )
300 closestDiff = currentDiff;
304 currentDiff = qAbs( relativePositionToPoint( 1.0 ) + 1 -
x );
305 if (( threshold < 0 || currentDiff < threshold ) && currentDiff < closestDiff )
307 closestStop = mGradient.
count() - 1;
308 closestDiff = currentDiff;
320 if ( closestStop >= 0 )
331 if ( e->
buttons() & Qt::LeftButton )
334 double offset = pointToRelativePosition( e->
pos().
x() );
336 mSelectedStop = mStops.
length();
346 if (( e->
key() == Qt::Key_Backspace || e->
key() == Qt::Key_Delete ) )
352 else if ( e->
key() == Qt::Key_Left || e->
key() == Qt::Key_Right )
354 if ( selectedStopIsMovable() )
359 if ( e->
modifiers() & Qt::ShiftModifier )
362 if ( e->
key() == Qt::Key_Left )
365 mStops[ mSelectedStop - 1 ].offset = qBound( 0.0, mStops[ mSelectedStop - 1 ].offset + offsetDiff, 1.0 );
377 const QPixmap& QgsGradientStopEditor::transparentBackground()
381 if ( transpBkgrd.
isNull() )
387 void QgsGradientStopEditor::drawStopMarker(
QPainter& painter,
QPoint topMiddle,
const QColor &color,
bool selected )
391 painter.
setBrush( selected ?
QColor( 150, 150, 150 ) : Qt::white );
392 painter.
setPen( selected ? Qt::black :
QColor( 150, 150, 150 ) );
399 painter.
setPen( Qt::NoPen );
408 double QgsGradientStopEditor::pointToRelativePosition(
int x )
const 415 else if ( x >= right )
418 return static_cast< double >( x - left ) / ( right - left );
421 int QgsGradientStopEditor::relativePositionToPoint(
double position )
const 428 else if ( position >= 1.0 )
431 return left + ( right - left ) * position;
434 bool QgsGradientStopEditor::selectedStopIsMovable()
const 437 return mSelectedStop > 0 && mSelectedStop < mGradient.
count() - 1;
457 bool hasAlpha =
false;
466 double offset = pointToRelativePosition( e->
pos().
x() );
468 mSelectedStop = mStops.
length();
Qt::KeyboardModifiers modifiers() const
const QMimeData * mimeData() const
Represents a color stop within a gradient color ramp.
void setRenderHint(RenderHint hint, bool on)
void changed()
Emitted when the gradient ramp is changed by a user.
QColor color2() const
Returns the gradient end color.
const T & at(int i) const
const QPoint & pos() const
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
QColor color1() const
Returns the gradient start color.
void setSelectedStopDetails(const QColor &color, double offset)
Sets the color and offset for the current selected stop.
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule)
void drawLine(const QLineF &line)
void setColor1(const QColor &color)
Sets the color for the first stop.
void acceptProposedAction()
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
void setColor2(const QColor &color)
Sets the gradient end color.
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
void setColor1(const QColor &color)
Sets the gradient start color.
void initFrom(const QWidget *widget)
void drawRect(const QRectF &rectangle)
void deleteSelectedStop()
Deletes the current selected stop.
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
virtual void mouseMoveEvent(QMouseEvent *event) override
void setPen(const QColor &color)
double offset
Relative positional offset, between 0 and 1.
void setStops(const QgsGradientStopsList &stops)
Sets the list of intermediate gradient stops for the ramp.
void setBrush(const QBrush &brush)
QgsGradientStopEditor(QWidget *parent=nullptr, QgsVectorGradientColorRampV2 *ramp=nullptr)
Constructor for QgsGradientStopEditor.
void setColor2(const QColor &color)
Sets the color for the last stop.
virtual QColor color(double value) const override
Returns the color corresponding to a specified value.
void dropEvent(QDropEvent *e) override
void selectedStopChanged(const QgsGradientStop &stop)
Emitted when the current selected stop changes.
void setGradientRamp(const QgsVectorGradientColorRampV2 &ramp)
Sets the current ramp shown in the editor.
void paintEvent(QPaintEvent *event) override
void setSelectedStopColor(const QColor &color)
Sets the color for the current selected stop.
virtual QSize sizeHint() const override
void translate(const QPointF &offset)
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const=0
virtual void mousePressEvent(QMouseEvent *event) override
void dragEnterEvent(QDragEnterEvent *e) override
const QPoint & pos() const
QgsGradientStop selectedStop() const
Returns details about the currently selected stop.
virtual void mouseDoubleClickEvent(QMouseEvent *event) override
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
virtual void keyPressEvent(QKeyEvent *event) override
void setSelectedStopOffset(double offset)
Sets the offset for the current selected stop.
QColor color
Gradient color at stop.
void selectStop(int index)
Sets the currently selected stop.