QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgscolorramplegendnodewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorramplegendnodewidget.h
3  -----------------------
4  begin : December 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgscolorramplegendnode.h"
20 #include "qgshelp.h"
22 #include "qgsnumericformat.h"
23 #include <QDialogButtonBox>
24 
26  : QgsPanelWidget( parent )
27 {
28  setupUi( this );
29 
30  mDirectionComboBox->addItem( tr( "Maximum on Top" ), QgsColorRampLegendNodeSettings::MinimumToMaximum );
31  mDirectionComboBox->addItem( tr( "Minimum on Top" ), QgsColorRampLegendNodeSettings::MaximumToMinimum );
32 
33  mOrientationComboBox->addItem( tr( "Vertical" ), Qt::Vertical );
34  mOrientationComboBox->addItem( tr( "Horizontal" ), Qt::Horizontal );
35 
36  mMinLabelLineEdit->setPlaceholderText( tr( "Default" ) );
37  mMaxLabelLineEdit->setPlaceholderText( tr( "Default" ) );
38 
39  mFontButton->setShowNullFormat( true );
40  mFontButton->setNoFormatString( tr( "Default" ) );
41 
42  connect( mUseContinuousLegendCheckBox, &QCheckBox::stateChanged, this, [ = ]( bool checked )
43  {
44  mLayoutGroup->setEnabled( checked );
45  mLabelsGroup->setEnabled( checked );
46  onChanged();
47  } );
48 
49  connect( mMinLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
50  connect( mMaxLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
51  connect( mPrefixLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
52  connect( mSuffixLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
53  connect( mDirectionComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsColorRampLegendNodeWidget::onChanged );
54  connect( mOrientationComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsColorRampLegendNodeWidget::onOrientationChanged );
55  connect( mNumberFormatPushButton, &QPushButton::clicked, this, &QgsColorRampLegendNodeWidget::changeNumberFormat );
56  connect( mFontButton, &QgsFontButton::changed, this, &QgsColorRampLegendNodeWidget::onChanged );
57 }
58 
60 {
62  settings.setUseContinuousLegend( mUseContinuousLegendCheckBox->isChecked() );
63  settings.setDirection( static_cast< QgsColorRampLegendNodeSettings::Direction >( mDirectionComboBox->currentData().toInt() ) );
64  settings.setOrientation( static_cast< Qt::Orientation >( mOrientationComboBox->currentData().toInt() ) );
65  settings.setMinimumLabel( mMinLabelLineEdit->text() );
66  settings.setMaximumLabel( mMaxLabelLineEdit->text() );
67  settings.setPrefix( mPrefixLineEdit->text() );
68  settings.setSuffix( mSuffixLineEdit->text() );
69  settings.setNumericFormat( mSettings.numericFormat()->clone() );
70  settings.setTextFormat( mFontButton->textFormat() );
71  return settings;
72 }
73 
75 {
76  mBlockSignals = true;
77 
78  mSettings = settings;
79  mUseContinuousLegendCheckBox->setChecked( settings.useContinuousLegend() );
80  mMinLabelLineEdit->setText( settings.minimumLabel() );
81  mMaxLabelLineEdit->setText( settings.maximumLabel() );
82  mPrefixLineEdit->setText( settings.prefix() );
83  mSuffixLineEdit->setText( settings.suffix() );
84  mDirectionComboBox->setCurrentIndex( mDirectionComboBox->findData( settings.direction() ) );
85  mOrientationComboBox->setCurrentIndex( mOrientationComboBox->findData( settings.orientation() ) );
86  mFontButton->setTextFormat( settings.textFormat() );
87  onOrientationChanged();
88  mBlockSignals = false;
89 }
90 
92 {
93  mUseContinuousLegendCheckBox->setVisible( visible );
94 }
95 
96 void QgsColorRampLegendNodeWidget::changeNumberFormat()
97 {
99  widget->setPanelTitle( tr( "Number Format" ) );
100  widget->setFormat( mSettings.numericFormat() );
101  connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [ = ]
102  {
103  mSettings.setNumericFormat( widget->format() );
104  onChanged();
105  } );
106  openPanel( widget );
107  return;
108 }
109 
110 void QgsColorRampLegendNodeWidget::onOrientationChanged()
111 {
112  switch ( static_cast< Qt::Orientation >( mOrientationComboBox->currentData().toInt() ) )
113  {
114  case Qt::Vertical:
115  mDirectionComboBox->setItemText( 0, tr( "Maximum on Top" ) );
116  mDirectionComboBox->setItemText( 1, tr( "Minimum on Top" ) );
117  break;
118 
119  case Qt::Horizontal:
120  mDirectionComboBox->setItemText( 0, tr( "Maximum on Right" ) );
121  mDirectionComboBox->setItemText( 1, tr( "Minimum on Right" ) );
122  break;
123  }
124 
125  onChanged();
126 }
127 
128 void QgsColorRampLegendNodeWidget::onChanged()
129 {
130  if ( mBlockSignals )
131  return;
132 
133  emit widgetChanged();
134 }
135 
136 //
137 // QgsColorRampLegendNodeDialog
138 //
139 
141  : QDialog( parent )
142 {
143  QVBoxLayout *vLayout = new QVBoxLayout();
144  mWidget = new QgsColorRampLegendNodeWidget( nullptr );
145  vLayout->addWidget( mWidget );
146  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
147  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
148  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
149  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, [ = ]
150  {
151  QgsHelp::openHelp( QStringLiteral( "working_with_raster/raster_properties.html#raster-legend-settings" ) );
152  } );
153  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
154  vLayout->addWidget( mButtonBox );
155  setLayout( vLayout );
156  setWindowTitle( tr( "Legend Node Settings" ) );
157 
158  mWidget->setSettings( settings );
159 }
160 
162 {
163  return mWidget->settings();
164 }
165 
167 {
168  return mButtonBox;
169 }
170 
172 {
173  mWidget->setUseContinuousRampCheckBoxVisibility( visible );
174 }
QgsColorRampLegendNodeSettings::setOrientation
void setOrientation(Qt::Orientation orientation)
Sets the ramp orientation (i.e.
Definition: qgscolorramplegendnodesettings.cpp:188
QgsColorRampLegendNodeSettings::setUseContinuousLegend
void setUseContinuousLegend(bool useContinuousLegend)
Sets the flag to use a continuos gradient legend to useContinuousLegend.
Definition: qgscolorramplegendnodesettings.cpp:198
QgsColorRampLegendNodeWidget::QgsColorRampLegendNodeWidget
QgsColorRampLegendNodeWidget(QWidget *parent=nullptr)
Constructor for QgsColorRampLegendNodeWidget, with the specified parent widget.
Definition: qgscolorramplegendnodewidget.cpp:25
QgsColorRampLegendNodeSettings::textFormat
QgsTextFormat textFormat() const
Returns the text format used to render text in the legend item.
Definition: qgscolorramplegendnodesettings.cpp:173
QgsColorRampLegendNodeWidget::settings
QgsColorRampLegendNodeSettings settings() const
Returns the legend node settings as defined by the widget.
Definition: qgscolorramplegendnodewidget.cpp:59
QgsColorRampLegendNodeDialog::settings
QgsColorRampLegendNodeSettings settings() const
Returns the legend node settings as defined by the dialog.
Definition: qgscolorramplegendnodewidget.cpp:161
QgsNumericFormatSelectorWidget::format
QgsNumericFormat * format() const
Returns a new format object representing the settings currently configured in the widget.
Definition: qgsnumericformatselectorwidget.cpp:73
QgsColorRampLegendNodeWidget
A widget for properties relating to a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
Definition: qgscolorramplegendnodewidget.h:41
QgsColorRampLegendNodeSettings::MinimumToMaximum
@ MinimumToMaximum
Minimum value on bottom, maximum value on top.
Definition: qgscolorramplegendnodesettings.h:46
QgsPanelWidget::openPanel
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Definition: qgspanelwidget.cpp:84
qgscolorramplegendnode.h
QgsColorRampLegendNodeDialog::setUseContinuousRampCheckBoxVisibility
void setUseContinuousRampCheckBoxVisibility(bool visible)
Sets visibility for the "Use Continuous Legend" checkbox in the legend settings dialog to visible.
Definition: qgscolorramplegendnodewidget.cpp:171
QgsColorRampLegendNodeDialog::QgsColorRampLegendNodeDialog
QgsColorRampLegendNodeDialog(const QgsColorRampLegendNodeSettings &settings, QWidget *parent SIP_TRANSFERTHIS=nullptr)
Constructor for QgsColorRampLegendNodeDialog, initially showing the specified settings.
Definition: qgscolorramplegendnodewidget.cpp:140
QgsColorRampLegendNodeSettings::Direction
Direction
Ramp directions.
Definition: qgscolorramplegendnodesettings.h:44
QgsColorRampLegendNodeSettings::setTextFormat
void setTextFormat(const QgsTextFormat &format)
Sets the text format used to render text in the legend item.
Definition: qgscolorramplegendnodesettings.cpp:178
QgsNumericFormatSelectorWidget
A widget which allows choice of numeric formats and the properties of them.
Definition: qgsnumericformatselectorwidget.h:34
QgsColorRampLegendNodeSettings::numericFormat
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
Definition: qgscolorramplegendnodesettings.cpp:88
QgsNumericFormatSelectorWidget::setFormat
void setFormat(const QgsNumericFormat *format)
Sets the format to show in the widget.
Definition: qgsnumericformatselectorwidget.cpp:51
QgsColorRampLegendNodeDialog::buttonBox
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
Definition: qgscolorramplegendnodewidget.cpp:166
QgsColorRampLegendNodeWidget::setUseContinuousRampCheckBoxVisibility
void setUseContinuousRampCheckBoxVisibility(bool visible)
Sets visibility for the "Use Continuous Legend" checkbox to visible.
Definition: qgscolorramplegendnodewidget.cpp:91
QgsColorRampLegendNodeSettings::setSuffix
void setSuffix(const QString &suffix)
Sets the suffix to show after legend text.
Definition: qgscolorramplegendnodesettings.cpp:168
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
QgsColorRampLegendNodeWidget::setSettings
void setSettings(const QgsColorRampLegendNodeSettings &settings)
Sets the settings to show in the widget.
Definition: qgscolorramplegendnodewidget.cpp:74
QgsColorRampLegendNodeSettings::minimumLabel
QString minimumLabel() const
Returns the label for the minimum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:68
QgsPanelWidget::panelAccepted
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
qgsnumericformatselectorwidget.h
QgsColorRampLegendNodeSettings::setPrefix
void setPrefix(const QString &prefix)
Sets the prefix to show before legend text.
Definition: qgscolorramplegendnodesettings.cpp:158
QgsColorRampLegendNodeSettings::prefix
QString prefix() const
Returns the prefix to show before legend text.
Definition: qgscolorramplegendnodesettings.cpp:153
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
QgsColorRampLegendNodeSettings::useContinuousLegend
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
Definition: qgscolorramplegendnodesettings.cpp:193
qgsnumericformat.h
QgsColorRampLegendNodeSettings::MaximumToMinimum
@ MaximumToMinimum
Maximum value on bottom, minimum value on top.
Definition: qgscolorramplegendnodesettings.h:47
QgsColorRampLegendNodeSettings::direction
QgsColorRampLegendNodeSettings::Direction direction() const
Returns the direction of the ramp.
Definition: qgscolorramplegendnodesettings.cpp:58
QgsColorRampLegendNodeSettings::setDirection
void setDirection(QgsColorRampLegendNodeSettings::Direction direction)
Sets the direction of the ramp.
Definition: qgscolorramplegendnodesettings.cpp:63
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
qgscolorramplegendnodewidget.h
QgsColorRampLegendNodeSettings::setMinimumLabel
void setMinimumLabel(const QString &label)
Sets the label for the minimum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:73
QgsColorRampLegendNodeSettings::maximumLabel
QString maximumLabel() const
Returns the label for the maximum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:78
QgsNumericFormat::clone
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
QgsColorRampLegendNodeSettings::suffix
QString suffix() const
Returns the suffix to show after legend text.
Definition: qgscolorramplegendnodesettings.cpp:163
QgsColorRampLegendNodeSettings::setMaximumLabel
void setMaximumLabel(const QString &label)
Sets the label for the maximum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:83
qgshelp.h
QgsNumericFormatSelectorWidget::changed
void changed()
Emitted whenever the format configured55 in the widget is changed.
QgsColorRampLegendNodeSettings::setNumericFormat
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
Definition: qgscolorramplegendnodesettings.cpp:93
QgsColorRampLegendNodeSettings
Settings for a color ramp legend node.
Definition: qgscolorramplegendnodesettings.h:37
QgsColorRampLegendNodeSettings::orientation
Qt::Orientation orientation() const
Returns the ramp orientation (i.e.
Definition: qgscolorramplegendnodesettings.cpp:183
QgsFontButton::changed
void changed()
Emitted when the widget's text format settings are changed.