23 #include <QColorDialog> 24 #include <QInputDialog> 27 #include <QTableWidget> 31 #include <qwt_global.h> 32 #include <qwt_plot_canvas.h> 34 #include <qwt_plot_curve.h> 35 #include <qwt_plot_grid.h> 36 #include <qwt_plot_marker.h> 37 #include <qwt_plot_picker.h> 38 #include <qwt_picker_machine.h> 39 #include <qwt_plot_layout.h> 40 #include <qwt_symbol.h> 41 #include <qwt_legend.h> 46 , mCurrentPlotColorComponent( -1 )
47 , mCurrentPlotMarkerIndex( 0 )
54 mPositionSpinBox->setShowClearButton(
false );
55 btnColor1->setAllowAlpha(
true );
56 btnColor1->setColorDialogTitle(
tr(
"Select ramp color" ) );
57 btnColor1->setContext(
"symbology" );
58 btnColor1->setShowNoColor(
true );
59 btnColor1->setNoColorString(
tr(
"Transparent" ) );
60 btnColor2->setAllowAlpha(
true );
61 btnColor2->setColorDialogTitle(
tr(
"Select ramp color" ) );
62 btnColor2->setContext(
"symbology" );
63 btnColor2->setShowNoColor(
true );
64 btnColor2->setNoColorString(
tr(
"Transparent" ) );
70 cboType->blockSignals(
true );
71 cboType->addItem(
tr(
"Discrete" ) );
72 cboType->addItem(
tr(
"Continuous" ) );
74 cboType->setCurrentIndex( 0 );
76 cboType->setCurrentIndex( 1 );
77 cboType->blockSignals(
false );
80 btnInformation->setEnabled(
false );
82 mStopEditor->setGradientRamp( *
mRamp );
83 connect( mStopEditor, SIGNAL( changed() ),
this, SLOT( updateRampFromStopEditor() ) );
85 connect( mColorWidget, SIGNAL( currentColorChanged(
QColor ) ),
this, SLOT( colorWidgetChanged(
QColor ) ) );
86 connect( mDeleteStopButton, SIGNAL( clicked() ), mStopEditor, SLOT( deleteSelectedStop() ) );
92 mPlot->setFrameStyle( QFrame::NoFrame );
93 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 94 QFrame* plotCanvasFrame =
dynamic_cast<QFrame*
>( mPlot->canvas() );
95 if ( plotCanvasFrame )
98 mPlot->canvas()->setFrameStyle( QFrame::NoFrame );
101 mPlot->setAxisScale( QwtPlot::yLeft, 0.0, 1.0 );
102 mPlot->enableAxis( QwtPlot::yLeft,
false );
104 mLightnessCurve =
new QwtPlotCurve();
105 mLightnessCurve->setTitle(
"Lightness" );
106 mLightnessCurve->setPen(
QPen(
QColor( 70, 150, 255 ), 0.0 ) ),
107 mLightnessCurve->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
108 mLightnessCurve->attach( mPlot );
110 mHueCurve =
new QwtPlotCurve();
111 mHueCurve->setTitle(
"Hue" );
112 mHueCurve->setPen(
QPen(
QColor( 255, 215, 70 ), 0.0 ) ),
113 mHueCurve->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
114 mHueCurve->attach( mPlot );
116 mSaturationCurve =
new QwtPlotCurve();
117 mSaturationCurve->setTitle(
"Saturation" );
118 mSaturationCurve->setPen(
QPen(
QColor( 255, 70, 150 ), 0.0 ) ),
119 mSaturationCurve->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
120 mSaturationCurve->attach( mPlot );
122 mAlphaCurve =
new QwtPlotCurve();
123 mAlphaCurve->setTitle(
"Alpha" );
124 mAlphaCurve->setPen(
QPen(
QColor( 50, 50, 50 ), 0.0 ) ),
125 mAlphaCurve->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
126 mAlphaCurve->attach( mPlot );
128 mPlotFilter =
new QgsGradientPlotEventFilter( mPlot );
130 connect( mPlotFilter, SIGNAL( mouseRelease(
QPointF ) ),
this, SLOT( plotMouseRelease(
QPointF ) ) );
133 mPlotHueCheckbox->setChecked( settings.
value(
"/GradientEditor/plotHue",
false ).
toBool() );
134 mPlotLightnessCheckbox->setChecked( settings.
value(
"/GradientEditor/plotLightness",
true ).
toBool() );
135 mPlotSaturationCheckbox->setChecked( settings.
value(
"/GradientEditor/plotSaturation",
false ).
toBool() );
136 mPlotAlphaCheckbox->setChecked( settings.
value(
"/GradientEditor/plotAlpha",
false ).
toBool() );
138 mHueCurve->setVisible( mPlotHueCheckbox->isChecked() );
139 mLightnessCurve->setVisible( mPlotLightnessCheckbox->isChecked() );
140 mSaturationCurve->setVisible( mPlotSaturationCheckbox->isChecked() );
141 mAlphaCurve->setVisible( mPlotAlphaCheckbox->isChecked() );
144 mStopEditor->selectStop( 0 );
151 settings.
setValue(
"/GradientEditor/plotHue", mPlotHueCheckbox->isChecked() );
152 settings.
setValue(
"/GradientEditor/plotLightness", mPlotLightnessCheckbox->isChecked() );
153 settings.
setValue(
"/GradientEditor/plotSaturation", mPlotSaturationCheckbox->isChecked() );
154 settings.
setValue(
"/GradientEditor/plotAlpha", mPlotAlphaCheckbox->isChecked() );
164 updateColorButtons();
188 if ( it.key().startsWith(
"cpt-city" ) )
206 if ( ! gradientFile.
isNull() )
208 QString fileName = gradientFile;
212 fileName = gradientFile;
213 fileName.
replace(
"<cpt-city>",
"http://soliton.vm.bytemark.co.uk/pub/cpt-city" );
215 label =
new QLabel(
tr(
"Gradient file : %1" ).arg( fileName ), dlg );
223 if ( !licenseFile.
isNull() )
225 QString fileName = licenseFile;
229 fileName = licenseFile;
230 fileName.
replace(
"<cpt-city>",
"http://soliton.vm.bytemark.co.uk/pub/cpt-city" );
232 label =
new QLabel(
tr(
"License file : %1" ).arg( fileName ), dlg );
240 QFile file( fileName );
241 if ( file.
open( QIODevice::ReadOnly | QIODevice::Text ) )
255 void QgsVectorGradientColorRampV2Dialog::updateColorButtons()
257 btnColor1->blockSignals(
true );
259 btnColor1->blockSignals(
false );
260 btnColor2->blockSignals(
true );
262 btnColor2->blockSignals(
false );
265 void QgsVectorGradientColorRampV2Dialog::updateStopEditor()
267 mStopEditor->blockSignals(
true );
268 mStopEditor->setGradientRamp( *
mRamp );
269 mStopEditor->blockSignals(
false );
272 void QgsVectorGradientColorRampV2Dialog::selectedStopChanged(
const QgsGradientStop& stop )
274 mColorWidget->blockSignals(
true );
275 mColorWidget->setColor( stop.
color );
276 mColorWidget->blockSignals(
false );
277 mPositionSpinBox->blockSignals(
true );
278 mPositionSpinBox->setValue( stop.
offset * 100 );
279 mPositionSpinBox->blockSignals(
false );
284 mPositionSpinBox->setDisabled(
true );
285 mDeleteStopButton->setDisabled(
true );
289 mPositionSpinBox->setDisabled(
false );
290 mDeleteStopButton->setDisabled(
false );
296 void QgsVectorGradientColorRampV2Dialog::colorWidgetChanged(
const QColor &color )
298 mStopEditor->setSelectedStopColor( color );
301 void QgsVectorGradientColorRampV2Dialog::on_mPositionSpinBox_valueChanged(
double val )
303 mStopEditor->setSelectedStopOffset( val / 100.0 );
306 void QgsVectorGradientColorRampV2Dialog::on_mPlotHueCheckbox_toggled(
bool checked )
308 mHueCurve->setVisible( checked );
312 void QgsVectorGradientColorRampV2Dialog::on_mPlotLightnessCheckbox_toggled(
bool checked )
314 mLightnessCurve->setVisible( checked );
318 void QgsVectorGradientColorRampV2Dialog::on_mPlotSaturationCheckbox_toggled(
bool checked )
320 mSaturationCurve->setVisible( checked );
324 void QgsVectorGradientColorRampV2Dialog::on_mPlotAlphaCheckbox_toggled(
bool checked )
326 mAlphaCurve->setVisible( checked );
330 void QgsVectorGradientColorRampV2Dialog::plotMousePress(
QPointF point )
335 mCurrentPlotColorComponent = -1;
336 mCurrentPlotMarkerIndex = -1;
342 double currentOff = 0.0;
360 if ( mPlotHueCheckbox->isChecked() )
362 currentDist = qPow( point.
x() - currentOff, 2.0 ) + qPow( point.
y() - currentCol.
hslHueF(), 2.0 );
363 if ( currentDist < minDist )
365 minDist = currentDist;
366 mCurrentPlotColorComponent = 0;
367 mCurrentPlotMarkerIndex = i;
370 if ( mPlotLightnessCheckbox->isChecked() )
372 currentDist = qPow( point.
x() - currentOff, 2.0 ) + qPow( point.
y() - currentCol.
lightnessF(), 2.0 );
373 if ( currentDist < minDist )
375 minDist = currentDist;
376 mCurrentPlotColorComponent = 1;
377 mCurrentPlotMarkerIndex = i;
380 if ( mPlotSaturationCheckbox->isChecked() )
382 currentDist = qPow( point.
x() - currentOff, 2.0 ) + qPow( point.
y() - currentCol.
hslSaturationF(), 2.0 );
383 if ( currentDist < minDist )
385 minDist = currentDist;
386 mCurrentPlotColorComponent = 2;
387 mCurrentPlotMarkerIndex = i;
390 if ( mPlotAlphaCheckbox->isChecked() )
392 currentDist = qPow( point.
x() - currentOff, 2.0 ) + qPow( point.
y() - currentCol.
alphaF(), 2.0 );
393 if ( currentDist < minDist )
395 minDist = currentDist;;
396 mCurrentPlotColorComponent = 3;
397 mCurrentPlotMarkerIndex = i;
403 if ( mCurrentPlotMarkerIndex >= 0 )
404 mStopEditor->selectStop( mCurrentPlotMarkerIndex );
407 void QgsVectorGradientColorRampV2Dialog::plotMouseRelease(
QPointF )
409 mCurrentPlotColorComponent = -1;
412 void QgsVectorGradientColorRampV2Dialog::plotMouseMove(
QPointF point )
414 QColor newColor = mStopEditor->selectedStop().color;
416 if ( mCurrentPlotColorComponent == 0 )
418 else if ( mCurrentPlotColorComponent == 1 )
420 else if ( mCurrentPlotColorComponent == 2 )
422 else if ( mCurrentPlotColorComponent == 3 )
425 mStopEditor->setSelectedStopDetails( newColor, qBound( qreal( 0.0 ), point.
x(), qreal( 1.0 ) ) );
430 return p1.
x() < p2.
x();
433 void QgsVectorGradientColorRampV2Dialog::addPlotMarker(
double x,
double y,
const QColor& color,
bool isSelected )
438 QColor brushColor = color;
441 QwtPlotMarker *marker =
new QwtPlotMarker();
442 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 443 marker->setSymbol(
new QwtSymbol( QwtSymbol::Ellipse,
QBrush( brushColor ),
QPen( borderColor, isSelected ? 2 : 1 ),
QSize( 10, 10 ) ) );
445 marker->setSymbol( QwtSymbol( QwtSymbol::Ellipse,
QBrush( brushColor ),
QPen( borderColor, isSelected ? 2 : 1 ),
QSize( 10, 10 ) ) );
447 marker->setValue(
x,
y );
448 marker->attach( mPlot );
449 marker->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
453 void QgsVectorGradientColorRampV2Dialog::addMarkersForColor(
double x,
const QColor& color,
bool isSelected )
455 if ( mPlotHueCheckbox->isChecked() )
456 addPlotMarker(
x, color.
hslHueF(), color, isSelected && mCurrentPlotColorComponent == 0 );
457 if ( mPlotLightnessCheckbox->isChecked() )
458 addPlotMarker(
x, color.
lightnessF(), color, isSelected && mCurrentPlotColorComponent == 1 );
459 if ( mPlotSaturationCheckbox->isChecked() )
460 addPlotMarker(
x, color.
hslSaturationF(), color, isSelected && mCurrentPlotColorComponent == 2 );
461 if ( mPlotAlphaCheckbox->isChecked() )
462 addPlotMarker(
x, color.
alphaF(), color, isSelected && mCurrentPlotColorComponent == 3 );
465 void QgsVectorGradientColorRampV2Dialog::updatePlot()
468 Q_FOREACH ( QwtPlotMarker* marker, mMarkers )
483 addMarkersForColor( 0,
mRamp->
color1(), mCurrentPlotMarkerIndex == 0 );
493 addMarkersForColor( stop.
offset, stop.
color, mCurrentPlotMarkerIndex == i );
498 for (
double p = 0.001; p < 1.0; p += 0.001 )
511 addMarkersForColor( 1.0,
mRamp->
color2(), mCurrentPlotMarkerIndex == i );
513 qSort( lightnessPoints.
begin(), lightnessPoints.
end(),
byX );
515 qSort( saturationPoints.
begin(), saturationPoints.
end(),
byX );
516 qSort( alphaPoints.
begin(), alphaPoints.
end(),
byX );
518 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 519 mLightnessCurve->setSamples( lightnessPoints );
520 mHueCurve->setSamples( huePoints );
521 mSaturationCurve->setSamples( saturationPoints );
522 mAlphaCurve->setSamples( alphaPoints );
524 mLightnessCurve->setData( lightnessPoints );
525 mHueCurve->setData( huePoints );
526 mSaturationCurve->setData( saturationPoints );
527 mAlphaCurve->setData( alphaPoints );
532 void QgsVectorGradientColorRampV2Dialog::updateRampFromStopEditor()
534 *
mRamp = mStopEditor->gradientRamp();
535 mPositionSpinBox->blockSignals(
true );
536 mPositionSpinBox->setValue( mStopEditor->selectedStop().offset * 100 );
537 mPositionSpinBox->blockSignals(
false );
538 mColorWidget->blockSignals(
true );
539 mColorWidget->setColor( mStopEditor->selectedStop().color );
540 mColorWidget->blockSignals(
false );
542 updateColorButtons();
548 mStopEditor->setColor1( color );
549 updateColorButtons();
554 mStopEditor->setColor2( color );
555 updateColorButtons();
561 QgsGradientPlotEventFilter::QgsGradientPlotEventFilter( QwtPlot *plot )
565 mPlot->canvas()->installEventFilter(
this );
570 if ( !mPlot->isEnabled() )
573 switch ( event->
type() )
575 case QEvent::MouseButtonPress:
578 if ( mouseEvent->
button() == Qt::LeftButton )
580 emit mousePress( mapPoint( mouseEvent->
pos() ) );
584 case QEvent::MouseMove:
587 if ( mouseEvent->
buttons() & Qt::LeftButton )
590 emit mouseMove( mapPoint( mouseEvent->
pos() ) );
594 case QEvent::MouseButtonRelease:
597 if ( mouseEvent->
button() == Qt::LeftButton )
599 emit mouseRelease( mapPoint( mouseEvent->
pos() ) );
610 QPointF QgsGradientPlotEventFilter::mapPoint(
QPointF point )
const 615 return QPointF( mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.
x() ),
616 mPlot->canvasMap( QwtPlot::yLeft ).invTransform( point.
y() ) );
QByteArray toByteArray() const
virtual bool event(QEvent *e)
QColor darker(int factor) const
Represents a color stop within a gradient color ramp.
static QString defaultBaseDir()
void on_btnInformation_pressed()
QColor color2() const
Returns the gradient end color.
void resizeColumnToContents(int column)
const_iterator constBegin() const
const T & at(int i) const
A generic dialog with layout and button box.
QColor color1() const
Returns the gradient start color.
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
void setColor2(const QColor &color)
void setFrameStyle(int style)
void addSpacing(int size)
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
QString tr(const char *sourceText, const char *disambiguation, int n)
int rowHeight(int row) const
void setValue(const QString &key, const QVariant &value)
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (eg authorship notes)
~QgsVectorGradientColorRampV2Dialog()
double offset
Relative positional offset, between 0 and 1.
const_iterator constEnd() const
QgsVectorGradientColorRampV2Dialog(QgsVectorGradientColorRampV2 *ramp, QWidget *parent=nullptr)
virtual bool eventFilter(QObject *watched, QEvent *event)
QgsVectorGradientColorRampV2 * mRamp
void convertToDiscrete(bool discrete)
Converts a gradient with existing color stops to or from discrete interpolation.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
virtual QColor color(double value) const override
Returns the color corresponding to a specified value.
void setTextInteractionFlags(QFlags< Qt::TextInteractionFlag > flags)
QString & replace(int position, int n, QChar after)
QVariant value(const QString &key, const QVariant &defaultValue) const
bool byX(QPointF p1, QPointF p2)
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
qreal hslSaturationF() const
void on_cboType_currentIndexChanged(int index)
void setReadOnly(bool ro)
const QPoint & pos() const
void resizeRowToContents(int row)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setColor1(const QColor &color)
void setText(const QString &text)
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
int count(const Key &key) const
QColor fromHslF(qreal h, qreal s, qreal l, qreal a)
const T value(const Key &key) const
QColor color
Gradient color at stop.