QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmeshvariablestrokewidthwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshvariablestrokewidthtwidget.cpp
3  -------------------------------------
4  begin : April 2020
5  copyright : (C) 2020 by Vincent Cloarec
6  email : vcloarec 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 <QDialog>
19 #include <QDialogButtonBox>
20 #include <QPointer>
21 
22 #include "qgssettings.h"
23 
25  const QgsInterpolatedLineWidth &variableStrokeWidth,
26  double defaultMinimumvalue,
27  double defaultMaximumValue,
28  QWidget *parent ):
29  QgsPanelWidget( parent ),
30  mDefaultMinimumValue( defaultMinimumvalue ),
31  mDefaultMaximumValue( defaultMaximumValue )
32 {
33  setupUi( this );
34 
35  mValueMinimumSpinBox->setSpecialValueText( QString( ) );
36  mValueMinimumSpinBox->setClearValueMode( QgsDoubleSpinBox::ClearValueMode::MinimumValue );
37  mValueMaximumSpinBox->setSpecialValueText( QString( ) );
38  mValueMaximumSpinBox->setClearValueMode( QgsDoubleSpinBox::ClearValueMode::MinimumValue );
39 
40  setPanelTitle( tr( "Variable Stroke Width" ) );
41 
43 
44  connect( mDefaultMinMaxButton, &QPushButton::clicked, this, &QgsMeshVariableStrokeWidthWidget::defaultMinMax );
45 
46  connect( mValueMinimumSpinBox, &QgsDoubleSpinBox::editingFinished, this, &QgsMeshVariableStrokeWidthWidget::widgetChanged );
47  connect( mValueMaximumSpinBox, &QgsDoubleSpinBox::editingFinished, this, &QgsMeshVariableStrokeWidthWidget::widgetChanged );
48  connect( mWidthMinimumSpinBox, &QgsDoubleSpinBox::editingFinished, this, &QgsMeshVariableStrokeWidthWidget::widgetChanged );
49  connect( mWidthMaximumSpinBox, &QgsDoubleSpinBox::editingFinished, this, &QgsMeshVariableStrokeWidthWidget::widgetChanged );
50  connect( mIgnoreOutOfRangecheckBox, &QCheckBox::toggled, this, &QgsMeshVariableStrokeWidthWidget::widgetChanged );
51  connect( mUseAbsoluteValueCheckBox, &QCheckBox::toggled, this, &QgsMeshVariableStrokeWidthWidget::widgetChanged );
52 }
53 
55 {
56  whileBlocking( mValueMinimumSpinBox )->setValue( variableStrokeWidth.minimumValue() );
57  whileBlocking( mValueMaximumSpinBox )->setValue( variableStrokeWidth.maximumValue() );
58  whileBlocking( mWidthMinimumSpinBox )->setValue( variableStrokeWidth.minimumWidth() );
59  whileBlocking( mWidthMaximumSpinBox )->setValue( variableStrokeWidth.maximumWidth() );
60  whileBlocking( mIgnoreOutOfRangecheckBox )->setChecked( variableStrokeWidth.ignoreOutOfRange() );
61  whileBlocking( mUseAbsoluteValueCheckBox )->setChecked( variableStrokeWidth.useAbsoluteValue() );
62 }
63 
64 void QgsMeshVariableStrokeWidthButton::setDefaultMinMaxValue( double minimum, double maximum )
65 {
66  mMinimumDefaultValue = minimum;
67  mMaximumDefaultValue = maximum;
68 }
69 
71 {
72  QgsInterpolatedLineWidth strokeWidth;
73  strokeWidth.setMinimumValue( lineEditValue( mValueMinimumSpinBox ) );
74  strokeWidth.setMaximumValue( lineEditValue( mValueMaximumSpinBox ) );
75  strokeWidth.setMinimumWidth( mWidthMinimumSpinBox->value() );
76  strokeWidth.setMaximumWidth( mWidthMaximumSpinBox->value() );
77  strokeWidth.setIgnoreOutOfRange( mIgnoreOutOfRangecheckBox->isChecked() );
78  strokeWidth.setUseAbsoluteValue( mUseAbsoluteValueCheckBox->isChecked() );
79  return strokeWidth;
80 }
81 
82 void QgsMeshVariableStrokeWidthWidget::defaultMinMax()
83 {
84  whileBlocking( mValueMinimumSpinBox )->setValue( mDefaultMinimumValue );
85  whileBlocking( mValueMaximumSpinBox )->setValue( mDefaultMaximumValue );
86  emit widgetChanged();
87 }
88 
90 {
91  updateText();
92  connect( this, &QPushButton::clicked, this, &QgsMeshVariableStrokeWidthButton::openWidget );
93 }
94 
96 {
97  return mVariableStrokeWidth;
98 }
99 
101 {
102  mVariableStrokeWidth = variableStrokeWidth;
103  updateText();
104 }
105 
106 void QgsMeshVariableStrokeWidthButton::openWidget()
107 {
109  QgsMeshVariableStrokeWidthWidget *widget =
110  new QgsMeshVariableStrokeWidthWidget( mVariableStrokeWidth,
111  mMinimumDefaultValue,
112  mMaximumDefaultValue,
113  panel );
114 
115  if ( panel && panel->dockMode() )
116  {
117  connect( widget, &QgsMeshVariableStrokeWidthWidget::widgetChanged, this, [this, widget]
118  {
119  // Update strokeWidth toward button
120  this->setVariableStrokeWidth( widget->variableStrokeWidth() );
121  this->emit widgetChanged();
122  } );
123 
124  panel->openPanel( widget );
125  return;
126  }
127  else
128  {
129  // Show the dialog version if not in a panel
130  QDialog *dlg = new QDialog( this );
131  const QString key = QStringLiteral( "/UI/paneldialog/%1" ).arg( widget->panelTitle() );
132  QgsSettings settings;
133  dlg->restoreGeometry( settings.value( key ).toByteArray() );
134  dlg->setWindowTitle( widget->panelTitle() );
135  dlg->setLayout( new QVBoxLayout() );
136  dlg->layout()->addWidget( widget );
137  QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
138  connect( buttonBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept );
139  connect( buttonBox, &QDialogButtonBox::rejected, dlg, &QDialog::reject );
140  dlg->layout()->addWidget( buttonBox );
141 
142  if ( dlg->exec() == QDialog::Accepted )
143  {
144  this->setVariableStrokeWidth( widget->variableStrokeWidth() );
145  emit widgetChanged();
146  }
147  settings.setValue( key, dlg->saveGeometry() );
148  }
149 }
150 
151 void QgsMeshVariableStrokeWidthButton::updateText()
152 {
153  setText( QString( "%1 - %2" ).
154  arg( QLocale().toString( mVariableStrokeWidth.minimumWidth(), 'g', 3 ),
155  QLocale().toString( mVariableStrokeWidth.maximumWidth(), 'g', 3 ) ) );
156 }
157 
158 double QgsMeshVariableStrokeWidthWidget::lineEditValue( const QgsDoubleSpinBox *lineEdit ) const
159 {
160  if ( lineEdit->value() == lineEdit->clearValue() )
161  {
162  return std::numeric_limits<double>::quiet_NaN();
163  }
164 
165  return lineEdit->value();
166 }
167 
QgsMeshVariableStrokeWidthButton::widgetChanged
void widgetChanged()
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
QgsPanelWidget::findParentPanel
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
Definition: qgspanelwidget.cpp:54
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
qgsmeshvariablestrokewidthwidget.h
QgsInterpolatedLineWidth::setIgnoreOutOfRange
void setIgnoreOutOfRange(bool ignoreOutOfRange)
Sets whether the variable width ignores out of range value.
Definition: qgsinterpolatedlinerenderer.cpp:408
QgsDoubleSpinBox::clearValue
bool clearValue
Definition: qgsdoublespinbox.h:56
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsInterpolatedLineWidth::setUseAbsoluteValue
void setUseAbsoluteValue(bool useAbsoluteValue)
Sets whether absolute value are used as input.
Definition: qgsinterpolatedlinerenderer.cpp:393
QgsPanelWidget::dockMode
bool dockMode()
Returns the dock mode state.
Definition: qgspanelwidget.h:93
QgsMeshVariableStrokeWidthButton::variableStrokeWidth
QgsInterpolatedLineWidth variableStrokeWidth() const
Returns the variable stroke width.
Definition: qgsmeshvariablestrokewidthwidget.cpp:95
QgsInterpolatedLineWidth::minimumValue
double minimumValue() const
Returns the minimum value used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:284
QgsMeshVariableStrokeWidthWidget::QgsMeshVariableStrokeWidthWidget
QgsMeshVariableStrokeWidthWidget(const QgsInterpolatedLineWidth &variableStrokeWidth, double defaultMinimumvalue, double defaultMaximumValue, QWidget *parent=nullptr)
Constructor.
Definition: qgsmeshvariablestrokewidthwidget.cpp:24
QgsInterpolatedLineWidth::setMinimumValue
void setMinimumValue(double minimumValue)
Sets the minimum value used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:289
QgsInterpolatedLineWidth::setMinimumWidth
void setMinimumWidth(double minimumWidth)
Sets the minimum width used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:311
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2191
QgsMeshVariableStrokeWidthWidget::variableStrokeWidth
QgsInterpolatedLineWidth variableStrokeWidth() const
Returns the variable stroke width.
Definition: qgsmeshvariablestrokewidthwidget.cpp:70
QgsMeshVariableStrokeWidthWidget::setVariableStrokeWidth
void setVariableStrokeWidth(const QgsInterpolatedLineWidth &variableStrokeWidth)
Sets the variable stroke width.
Definition: qgsmeshvariablestrokewidthwidget.cpp:54
QgsInterpolatedLineWidth
Represents a width than can vary depending on values.
Definition: qgsinterpolatedlinerenderer.h:126
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:279
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
QgsInterpolatedLineWidth::ignoreOutOfRange
bool ignoreOutOfRange() const
Returns whether the variable width ignores out of range value.
Definition: qgsinterpolatedlinerenderer.cpp:403
QgsMeshVariableStrokeWidthButton::setVariableStrokeWidth
void setVariableStrokeWidth(const QgsInterpolatedLineWidth &variableStrokeWidth)
Sets the variable stroke width.
Definition: qgsmeshvariablestrokewidthwidget.cpp:100
QgsInterpolatedLineWidth::useAbsoluteValue
bool useAbsoluteValue() const
Returns whether absolute value are used as input.
Definition: qgsinterpolatedlinerenderer.cpp:388
QgsInterpolatedLineWidth::setMaximumValue
void setMaximumValue(double maximumValue)
Sets the maximum value used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:300
qgssettings.h
QgsMeshVariableStrokeWidthButton::QgsMeshVariableStrokeWidthButton
QgsMeshVariableStrokeWidthButton(QWidget *parent=nullptr)
Constructor.
Definition: qgsmeshvariablestrokewidthwidget.cpp:89
QgsDoubleSpinBox
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value....
Definition: qgsdoublespinbox.h:42
QgsMeshVariableStrokeWidthButton::setDefaultMinMaxValue
void setDefaultMinMaxValue(double minimum, double maximum)
Sets the default min/max values that will be reload if needed.
Definition: qgsmeshvariablestrokewidthwidget.cpp:64
QgsInterpolatedLineWidth::setMaximumWidth
void setMaximumWidth(double maximumWidth)
Sets the maximum width used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:322
QgsInterpolatedLineWidth::minimumWidth
double minimumWidth() const
Returns the minimum width used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:306
QgsInterpolatedLineWidth::maximumWidth
double maximumWidth() const
Returns the maximum width used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:317
QgsInterpolatedLineWidth::maximumValue
double maximumValue() const
Returns the maximum value used to defined the variable width.
Definition: qgsinterpolatedlinerenderer.cpp:295