QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgslimitedrandomcolorrampdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslimitedrandomcolorrampdialog.cpp
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgssymbollayerutils.h"
19 #include "qgscolorramp.h"
20 #include "qgshelp.h"
21 
22 #include <QColorDialog>
23 #include <QDialogButtonBox>
24 
25 
27  : QgsPanelWidget( parent )
28  , mRamp( ramp )
29 {
30  setupUi( this );
31  spinCount->setClearValue( 10 );
32  spinHue1->setClearValue( 0 );
33  spinHue2->setClearValue( 359 );
34  spinSat1->setClearValue( 100 );
35  spinSat2->setClearValue( 240 );
36  spinVal1->setClearValue( 200 );
37  spinVal2->setClearValue( 240 );
38 
39  updateUi();
40 
41  connect( spinCount, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLimitedRandomColorRampWidget::setCount );
42  connect( spinHue1, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLimitedRandomColorRampWidget::setHue1 );
43  connect( spinHue2, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLimitedRandomColorRampWidget::setHue2 );
44  connect( spinSat1, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLimitedRandomColorRampWidget::setSat1 );
45  connect( spinSat2, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLimitedRandomColorRampWidget::setSat2 );
46  connect( spinVal1, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLimitedRandomColorRampWidget::setVal1 );
47  connect( spinVal2, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLimitedRandomColorRampWidget::setVal2 );
48 }
49 
51 {
52  mRamp = ramp;
53  updateUi();
54  emit changed();
55 }
56 
57 void QgsLimitedRandomColorRampWidget::updatePreview()
58 {
59  mRamp.updateColors();
60 
61  QSize size( 300, 40 );
62  lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
63 }
64 
65 void QgsLimitedRandomColorRampWidget::updateUi()
66 {
67  spinCount->setValue( mRamp.count() );
68  spinHue1->setValue( mRamp.hueMin() );
69  spinHue2->setValue( mRamp.hueMax() );
70  spinSat1->setValue( mRamp.satMin() );
71  spinSat2->setValue( mRamp.satMax() );
72  spinVal1->setValue( mRamp.valMin() );
73  spinVal2->setValue( mRamp.valMax() );
74  updatePreview();
75 }
76 
78 {
79  mRamp.setCount( val );
80  updatePreview();
81  emit changed();
82 }
83 
85 {
86  mRamp.setHueMin( val );
87  updatePreview();
88  emit changed();
89 }
90 
92 {
93  mRamp.setHueMax( val );
94  updatePreview();
95  emit changed();
96 }
97 
99 {
100  mRamp.setSatMin( val );
101  updatePreview();
102  emit changed();
103 }
104 
106 {
107  mRamp.setSatMax( val );
108  updatePreview();
109  emit changed();
110 }
111 
113 {
114  mRamp.setValMin( val );
115  updatePreview();
116  emit changed();
117 }
118 
120 {
121  mRamp.setValMax( val );
122  updatePreview();
123  emit changed();
124 }
125 
127  : QDialog( parent )
128 {
129  QVBoxLayout *vLayout = new QVBoxLayout();
130  mWidget = new QgsLimitedRandomColorRampWidget( ramp );
131 
132  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
133 
134  vLayout->addWidget( mWidget );
135  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
136  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
137  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
138  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLimitedRandomColorRampDialog::showHelp );
139  vLayout->addWidget( mButtonBox );
140  setLayout( vLayout );
141  setWindowTitle( tr( "Random Color Ramp" ) );
143 }
144 
146 {
147  return mButtonBox;
148 }
149 
150 void QgsLimitedRandomColorRampDialog::showHelp()
151 {
152  QgsHelp::openHelp( QStringLiteral( "style_library/style_manager.html#setting-a-color-ramp" ) );
153 }
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
void changed()
Emitted when the dialog settings change.
QgsLimitedRandomColorRampDialog(const QgsLimitedRandomColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsLimitedRandomColorRampDialog.
A widget which allows users to modify the properties of a QgsLimitedRandomColorRamp.
QgsLimitedRandomColorRampWidget(const QgsLimitedRandomColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsLimitedRandomColorRampWidget.
void setCount(int val)
Sets the number of colors to create in the ramp.
void setVal1(int val)
Sets the minimum value for colors in the ramp.
void setHue2(int val)
Sets the maximum hue for colors in the ramp.
void changed()
Emitted when the dialog settings change.
void setVal2(int val)
Sets the maximum value for colors in the ramp.
void setSat2(int val)
Sets the maximum saturation for colors in the ramp.
void setRamp(const QgsLimitedRandomColorRamp &ramp)
Sets the color ramp to show in the dialog.
void setHue1(int val)
Sets the minimum hue for colors in the ramp.
void setSat1(int val)
Sets the minimum saturation for colors in the ramp.
Constrained random color ramp, which returns random colors based on preset parameters.
Definition: qgscolorramp.h:303
void updateColors()
Must be called after changing the properties of the color ramp to regenerate the list of random color...
void setSatMin(int val)
Sets the minimum saturation for generated colors.
Definition: qgscolorramp.h:416
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:342
void setHueMin(int val)
Sets the minimum hue for generated colors.
Definition: qgscolorramp.h:404
void setSatMax(int val)
Sets the maximum saturation for generated colors.
Definition: qgscolorramp.h:422
int valMax() const
Returns the maximum value for generated colors.
Definition: qgscolorramp.h:393
int satMax() const
Returns the maximum saturation for generated colors.
Definition: qgscolorramp.h:381
void setHueMax(int val)
Sets the maximum hue for generated colors.
Definition: qgscolorramp.h:410
void setCount(int val)
Sets the number of colors contained in the ramp.
Definition: qgscolorramp.h:398
int hueMax() const
Returns the maximum hue for generated colors.
Definition: qgscolorramp.h:369
int hueMin() const
Returns the minimum hue for generated colors.
Definition: qgscolorramp.h:363
void setValMax(int val)
Sets the maximum value for generated colors.
Definition: qgscolorramp.h:434
int valMin() const
Returns the minimum value for generated colors.
Definition: qgscolorramp.h:387
void setValMin(int val)
Sets the minimum value for generated colors.
Definition: qgscolorramp.h:428
int satMin() const
Returns the minimum saturation for generated colors.
Definition: qgscolorramp.h:375
Base class for any widget that can be shown as a inline panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0, Qt::Orientation direction=Qt::Horizontal, bool flipDirection=false, bool drawTransparentBackground=true)
Returns a pixmap preview for a color ramp.