QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
21 #include "qgsnumericformat.h"
22 #include <QDialogButtonBox>
23 
25  : QgsPanelWidget( parent )
26 {
27  setupUi( this );
28 
29  mDirectionComboBox->addItem( tr( "Maximum on Top" ), QgsColorRampLegendNodeSettings::MinimumToMaximum );
30  mDirectionComboBox->addItem( tr( "Minimum on Top" ), QgsColorRampLegendNodeSettings::MaximumToMinimum );
31 
32  mOrientationComboBox->addItem( tr( "Vertical" ), Qt::Vertical );
33  mOrientationComboBox->addItem( tr( "Horizontal" ), Qt::Horizontal );
34 
35  mMinLabelLineEdit->setPlaceholderText( tr( "Default" ) );
36  mMaxLabelLineEdit->setPlaceholderText( tr( "Default" ) );
37 
38  mFontButton->setShowNullFormat( true );
39  mFontButton->setNoFormatString( tr( "Default" ) );
40 
41  connect( mUseContinuousLegendCheckBox, &QCheckBox::stateChanged, this, [ = ]( bool checked )
42  {
43  mLayoutGroup->setEnabled( checked );
44  mLabelsGroup->setEnabled( checked );
45  onChanged();
46  } );
47 
48  connect( mMinLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
49  connect( mMaxLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
50  connect( mPrefixLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
51  connect( mSuffixLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
52  connect( mDirectionComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsColorRampLegendNodeWidget::onChanged );
53  connect( mOrientationComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsColorRampLegendNodeWidget::onOrientationChanged );
54  connect( mNumberFormatPushButton, &QPushButton::clicked, this, &QgsColorRampLegendNodeWidget::changeNumberFormat );
55  connect( mFontButton, &QgsFontButton::changed, this, &QgsColorRampLegendNodeWidget::onChanged );
56 }
57 
59 {
61  settings.setUseContinuousLegend( mUseContinuousLegendCheckBox->isChecked() );
62  settings.setDirection( static_cast< QgsColorRampLegendNodeSettings::Direction >( mDirectionComboBox->currentData().toInt() ) );
63  settings.setOrientation( static_cast< Qt::Orientation >( mOrientationComboBox->currentData().toInt() ) );
64  settings.setMinimumLabel( mMinLabelLineEdit->text() );
65  settings.setMaximumLabel( mMaxLabelLineEdit->text() );
66  settings.setPrefix( mPrefixLineEdit->text() );
67  settings.setSuffix( mSuffixLineEdit->text() );
68  settings.setNumericFormat( mSettings.numericFormat()->clone() );
69  settings.setTextFormat( mFontButton->textFormat() );
70  return settings;
71 }
72 
74 {
75  mBlockSignals = true;
76 
77  mSettings = settings;
78  mUseContinuousLegendCheckBox->setChecked( settings.useContinuousLegend() );
79  mMinLabelLineEdit->setText( settings.minimumLabel() );
80  mMaxLabelLineEdit->setText( settings.maximumLabel() );
81  mPrefixLineEdit->setText( settings.prefix() );
82  mSuffixLineEdit->setText( settings.suffix() );
83  mDirectionComboBox->setCurrentIndex( mDirectionComboBox->findData( settings.direction() ) );
84  mOrientationComboBox->setCurrentIndex( mOrientationComboBox->findData( settings.orientation() ) );
85  mFontButton->setTextFormat( settings.textFormat() );
86  onOrientationChanged();
87  mBlockSignals = false;
88 }
89 
91 {
92  mUseContinuousLegendCheckBox->setVisible( visible );
93 }
94 
95 void QgsColorRampLegendNodeWidget::changeNumberFormat()
96 {
98  widget->setPanelTitle( tr( "Number Format" ) );
99  widget->setFormat( mSettings.numericFormat() );
100  connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [ = ]
101  {
102  mSettings.setNumericFormat( widget->format() );
103  onChanged();
104  } );
105  openPanel( widget );
106  return;
107 }
108 
109 void QgsColorRampLegendNodeWidget::onOrientationChanged()
110 {
111  switch ( static_cast< Qt::Orientation >( mOrientationComboBox->currentData().toInt() ) )
112  {
113  case Qt::Vertical:
114  mDirectionComboBox->setItemText( 0, tr( "Maximum on Top" ) );
115  mDirectionComboBox->setItemText( 1, tr( "Minimum on Top" ) );
116  break;
117 
118  case Qt::Horizontal:
119  mDirectionComboBox->setItemText( 0, tr( "Maximum on Right" ) );
120  mDirectionComboBox->setItemText( 1, tr( "Minimum on Right" ) );
121  break;
122  }
123 
124  onChanged();
125 }
126 
127 void QgsColorRampLegendNodeWidget::onChanged()
128 {
129  if ( mBlockSignals )
130  return;
131 
132  emit widgetChanged();
133 }
134 
135 //
136 // QgsColorRampLegendNodeDialog
137 //
138 
140  : QDialog( parent )
141 {
142  QVBoxLayout *vLayout = new QVBoxLayout();
143  mWidget = new QgsColorRampLegendNodeWidget( nullptr );
144  vLayout->addWidget( mWidget );
145  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal );
146  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
147  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
148  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
149  vLayout->addWidget( mButtonBox );
150  setLayout( vLayout );
151  setWindowTitle( tr( "Legend Node Settings" ) );
152 
153  mWidget->setSettings( settings );
154 }
155 
157 {
158  return mWidget->settings();
159 }
160 
162 {
163  return mButtonBox;
164 }
165 
167 {
168  mWidget->setUseContinuousRampCheckBoxVisibility( visible );
169 }
void setUseContinuousRampCheckBoxVisibility(bool visible)
Sets visibility for the "Use Continuous Legend" checkbox in the legend settings dialog to visible.
QgsColorRampLegendNodeDialog(const QgsColorRampLegendNodeSettings &settings, QWidget *parent SIP_TRANSFERTHIS=nullptr)
Constructor for QgsColorRampLegendNodeDialog, initially showing the specified settings.
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
QgsColorRampLegendNodeSettings settings() const
Returns the legend node settings as defined by the dialog.
Settings for a color ramp legend node.
void setDirection(QgsColorRampLegendNodeSettings::Direction direction)
Sets the direction of the ramp.
void setMaximumLabel(const QString &label)
Sets the label for the maximum value on the ramp.
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
QString maximumLabel() const
Returns the label for the maximum value on the ramp.
QString suffix() const
Returns the suffix to show after legend text.
void setPrefix(const QString &prefix)
Sets the prefix to show before legend text.
void setUseContinuousLegend(bool useContinuousLegend)
Sets the flag to use a continuos gradient legend to useContinuousLegend.
void setOrientation(Qt::Orientation orientation)
Sets the ramp orientation (i.e.
@ MaximumToMinimum
Maximum value on bottom, minimum value on top.
@ MinimumToMaximum
Minimum value on bottom, maximum value on top.
void setSuffix(const QString &suffix)
Sets the suffix to show after legend text.
QString prefix() const
Returns the prefix to show before legend text.
Qt::Orientation orientation() const
Returns the ramp orientation (i.e.
QgsColorRampLegendNodeSettings::Direction direction() const
Returns the direction of the ramp.
QgsTextFormat textFormat() const
Returns the text format used to render text in the legend item.
void setMinimumLabel(const QString &label)
Sets the label for the minimum value on the ramp.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used to render text in the legend item.
QString minimumLabel() const
Returns the label for the minimum value on the ramp.
A widget for properties relating to a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
QgsColorRampLegendNodeSettings settings() const
Returns the legend node settings as defined by the widget.
QgsColorRampLegendNodeWidget(QWidget *parent=nullptr)
Constructor for QgsColorRampLegendNodeWidget, with the specified parent widget.
void setSettings(const QgsColorRampLegendNodeSettings &settings)
Sets the settings to show in the widget.
void setUseContinuousRampCheckBoxVisibility(bool visible)
Sets visibility for the "Use Continuous Legend" checkbox to visible.
void changed()
Emitted when the widget's text format settings are changed.
A widget which allows choice of numeric formats and the properties of them.
QgsNumericFormat * format() const
Returns a new format object representing the settings currently configured in the widget.
void changed()
Emitted whenever the format configured55 in the widget is changed.
void setFormat(const QgsNumericFormat *format)
Sets the format to show in the widget.
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
void widgetChanged()
Emitted when the widget state changes.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.