QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgslayoutscalebarwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutscalebarwidget.cpp
3  -----------------------------
4  begin : 11 June 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 #include "qgslayoutitemmap.h"
19 #include "qgslayoutitemscalebar.h"
20 #include "qgslayout.h"
21 #include "qgsguiutils.h"
22 #include "qgsvectorlayer.h"
24 
25 #include <QColorDialog>
26 #include <QFontDialog>
27 #include <QWidget>
28 
30  : QgsLayoutItemBaseWidget( nullptr, scaleBar )
31  , mScalebar( scaleBar )
32 {
33  setupUi( this );
34  connect( mHeightSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mHeightSpinBox_valueChanged );
35  connect( mLineWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mLineWidthSpinBox_valueChanged );
36  connect( mSegmentSizeSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mSegmentSizeSpinBox_valueChanged );
37  connect( mSegmentsLeftSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mSegmentsLeftSpinBox_valueChanged );
38  connect( mNumberOfSegmentsSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mNumberOfSegmentsSpinBox_valueChanged );
39  connect( mUnitLabelLineEdit, &QLineEdit::textChanged, this, &QgsLayoutScaleBarWidget::mUnitLabelLineEdit_textChanged );
40  connect( mMapUnitsPerBarUnitSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mMapUnitsPerBarUnitSpinBox_valueChanged );
41  connect( mFillColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutScaleBarWidget::mFillColorButton_colorChanged );
42  connect( mFillColor2Button, &QgsColorButton::colorChanged, this, &QgsLayoutScaleBarWidget::mFillColor2Button_colorChanged );
43  connect( mStrokeColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutScaleBarWidget::mStrokeColorButton_colorChanged );
44  connect( mStyleComboBox, &QComboBox::currentTextChanged, this, &QgsLayoutScaleBarWidget::mStyleComboBox_currentIndexChanged );
45  connect( mLabelBarSpaceSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mLabelBarSpaceSpinBox_valueChanged );
46  connect( mBoxSizeSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mBoxSizeSpinBox_valueChanged );
47  connect( mAlignmentComboBox, &QgsAlignmentComboBox::changed, this, &QgsLayoutScaleBarWidget::alignmentChanged );
48  connect( mLabelVerticalPlacementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutScaleBarWidget::mLabelVerticalPlacementComboBox_currentIndexChanged );
49  connect( mLabelHorizontalPlacementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutScaleBarWidget::mLabelHorizontalPlacementComboBox_currentIndexChanged );
50  connect( mUnitsComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutScaleBarWidget::mUnitsComboBox_currentIndexChanged );
51  connect( mLineJoinStyleCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutScaleBarWidget::mLineJoinStyleCombo_currentIndexChanged );
52  connect( mLineCapStyleCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutScaleBarWidget::mLineCapStyleCombo_currentIndexChanged );
53  connect( mMinWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mMinWidthSpinBox_valueChanged );
54  connect( mMaxWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mMaxWidthSpinBox_valueChanged );
55  connect( mNumberFormatPushButton, &QPushButton::clicked, this, &QgsLayoutScaleBarWidget::changeNumberFormat );
56  setPanelTitle( tr( "Scalebar Properties" ) );
57 
58  mFontButton->registerExpressionContextGenerator( this );
59 
60  connectUpdateSignal();
61 
62  //add widget for general composer item properties
63  mItemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, scaleBar );
64  mainLayout->addWidget( mItemPropertiesWidget );
65 
66  mSegmentSizeRadioGroup.addButton( mFixedSizeRadio );
67  mSegmentSizeRadioGroup.addButton( mFitWidthRadio );
68  connect( &mSegmentSizeRadioGroup, static_cast < void ( QButtonGroup::* )( QAbstractButton * ) > ( &QButtonGroup::buttonClicked ), this, &QgsLayoutScaleBarWidget::segmentSizeRadioChanged );
69 
70  blockMemberSignals( true );
71 
72  //style combo box
73  mStyleComboBox->insertItem( 0, tr( "Single Box" ) );
74  mStyleComboBox->insertItem( 1, tr( "Double Box" ) );
75  mStyleComboBox->insertItem( 2, tr( "Line Ticks Middle" ) );
76  mStyleComboBox->insertItem( 3, tr( "Line Ticks Down" ) );
77  mStyleComboBox->insertItem( 4, tr( "Line Ticks Up" ) );
78  mStyleComboBox->insertItem( 5, tr( "Numeric" ) );
79 
80  //label vertical/horizontal placement combo box
81  mLabelVerticalPlacementComboBox->addItem( tr( "Above Segments" ), static_cast< int >( QgsScaleBarSettings::LabelAboveSegment ) );
82  mLabelVerticalPlacementComboBox->addItem( tr( "Below Segments" ), static_cast< int >( QgsScaleBarSettings::LabelBelowSegment ) );
83  mLabelHorizontalPlacementComboBox->addItem( tr( "Centered at Segment Edge" ), static_cast< int >( QgsScaleBarSettings::LabelCenteredEdge ) );
84  mLabelHorizontalPlacementComboBox->addItem( tr( "Centered at Center of Segment" ), static_cast< int >( QgsScaleBarSettings::LabelCenteredSegment ) );
85 
86  //alignment combo box
87  mAlignmentComboBox->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight );
88 
89  //units combo box
90  mUnitsComboBox->addItem( tr( "Map units" ), QgsUnitTypes::DistanceUnknownUnit );
91  mUnitsComboBox->addItem( tr( "Meters" ), QgsUnitTypes::DistanceMeters );
92  mUnitsComboBox->addItem( tr( "Kilometers" ), QgsUnitTypes::DistanceKilometers );
93  mUnitsComboBox->addItem( tr( "Feet" ), QgsUnitTypes::DistanceFeet );
94  mUnitsComboBox->addItem( tr( "Yards" ), QgsUnitTypes::DistanceYards );
95  mUnitsComboBox->addItem( tr( "Miles" ), QgsUnitTypes::DistanceMiles );
96  mUnitsComboBox->addItem( tr( "Nautical Miles" ), QgsUnitTypes::DistanceNauticalMiles );
97  mUnitsComboBox->addItem( tr( "Centimeters" ), QgsUnitTypes::DistanceCentimeters );
98  mUnitsComboBox->addItem( tr( "Millimeters" ), QgsUnitTypes::DistanceMillimeters );
99 
100  mFillColorButton->setColorDialogTitle( tr( "Select Fill Color" ) );
101  mFillColorButton->setAllowOpacity( true );
102  mFillColorButton->setContext( QStringLiteral( "composer" ) );
103  mFillColorButton->setNoColorString( tr( "Transparent Fill" ) );
104  mFillColorButton->setShowNoColor( true );
105 
106  mFillColor2Button->setColorDialogTitle( tr( "Select Alternate Fill Color" ) );
107  mFillColor2Button->setAllowOpacity( true );
108  mFillColor2Button->setContext( QStringLiteral( "composer" ) );
109  mFillColor2Button->setNoColorString( tr( "Transparent Fill" ) );
110  mFillColor2Button->setShowNoColor( true );
111 
112  mFontButton->setDialogTitle( tr( "Scalebar Font" ) );
113  mFontButton->setMode( QgsFontButton::ModeTextRenderer );
114 
115  mStrokeColorButton->setColorDialogTitle( tr( "Select Line Color" ) );
116  mStrokeColorButton->setAllowOpacity( true );
117  mStrokeColorButton->setContext( QStringLiteral( "composer" ) );
118  mStrokeColorButton->setNoColorString( tr( "Transparent Line" ) );
119  mStrokeColorButton->setShowNoColor( true );
120 
121  mFillColorDDBtn->registerLinkedWidget( mFillColorButton );
122  mFillColor2DDBtn->registerLinkedWidget( mFillColor2Button );
123  mLineColorDDBtn->registerLinkedWidget( mStrokeColorButton );
124 
125  if ( mScalebar )
126  {
127  mFillColorDDBtn->registerExpressionContextGenerator( mScalebar );
128  mFillColor2DDBtn->registerExpressionContextGenerator( mScalebar );
129  mLineColorDDBtn->registerExpressionContextGenerator( mScalebar );
130  mLineWidthDDBtn->registerExpressionContextGenerator( mScalebar );
131  QgsLayout *scaleBarLayout = mScalebar->layout();
132  if ( scaleBarLayout )
133  {
134  mMapItemComboBox->setCurrentLayout( scaleBarLayout );
135  mMapItemComboBox->setItemType( QgsLayoutItemRegistry::LayoutMap );
136  }
137  }
138 
139  connect( mMapItemComboBox, &QgsLayoutItemComboBox::itemChanged, this, &QgsLayoutScaleBarWidget::mapChanged );
140 
145 
146  blockMemberSignals( false );
147  setGuiElements(); //set the GUI elements to the state of scaleBar
148 
149  connect( mFontButton, &QgsFontButton::changed, this, &QgsLayoutScaleBarWidget::textFormatChanged );
150  mFontButton->setLayer( coverageLayer() );
151  if ( mScalebar->layout() )
152  {
153  connect( &mScalebar->layout()->reportContext(), &QgsLayoutReportContext::layerChanged, mFontButton, &QgsFontButton::setLayer );
154  }
155 }
156 
158 {
159  if ( mItemPropertiesWidget )
160  mItemPropertiesWidget->setMasterLayout( masterLayout );
161 }
162 
164 {
165  QgsExpressionContext context = mScalebar->createExpressionContext();
166  QgsExpressionContextScope *scaleScope = new QgsExpressionContextScope( QStringLiteral( "scalebar_text" ) );
167  scaleScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "scale_value" ), 0, true, false ) );
168  context.appendScope( scaleScope );
169  context.setHighlightedVariables( QStringList() << QStringLiteral( "scale_value" ) );
170  return context;
171 }
172 
174 {
176  return false;
177 
178  disconnectUpdateSignal();
179 
180  mScalebar = qobject_cast< QgsLayoutItemScaleBar * >( item );
181  mItemPropertiesWidget->setItem( mScalebar );
182 
183  if ( mScalebar )
184  {
185  connectUpdateSignal();
186  mFillColorDDBtn->registerExpressionContextGenerator( mScalebar );
187  mFillColor2DDBtn->registerExpressionContextGenerator( mScalebar );
188  mLineColorDDBtn->registerExpressionContextGenerator( mScalebar );
189  mLineWidthDDBtn->registerExpressionContextGenerator( mScalebar );
190  }
191 
192  setGuiElements();
193 
194  return true;
195 }
196 
197 void QgsLayoutScaleBarWidget::setGuiElements()
198 {
199  if ( !mScalebar )
200  {
201  return;
202  }
203 
204  blockMemberSignals( true );
205  mNumberOfSegmentsSpinBox->setValue( mScalebar->numberOfSegments() );
206  mSegmentsLeftSpinBox->setValue( mScalebar->numberOfSegmentsLeft() );
207  mSegmentSizeSpinBox->setValue( mScalebar->unitsPerSegment() );
208  mLineWidthSpinBox->setValue( mScalebar->lineWidth() );
209  mHeightSpinBox->setValue( mScalebar->height() );
210  mMapUnitsPerBarUnitSpinBox->setValue( mScalebar->mapUnitsPerScaleBarUnit() );
211  mLabelBarSpaceSpinBox->setValue( mScalebar->labelBarSpace() );
212  mBoxSizeSpinBox->setValue( mScalebar->boxContentSpace() );
213  mUnitLabelLineEdit->setText( mScalebar->unitLabel() );
214  mLineJoinStyleCombo->setPenJoinStyle( mScalebar->lineJoinStyle() );
215  mLineCapStyleCombo->setPenCapStyle( mScalebar->lineCapStyle() );
216  mFillColorButton->setColor( mScalebar->fillColor() );
217  mFillColor2Button->setColor( mScalebar->fillColor2() );
218  mStrokeColorButton->setColor( mScalebar->lineColor() );
219  mFontButton->setTextFormat( mScalebar->textFormat() );
220 
221  //map combo box
222  mMapItemComboBox->setItem( mScalebar->linkedMap() );
223 
224  //style...
225  QString style = mScalebar->style();
226  mStyleComboBox->setCurrentIndex( mStyleComboBox->findText( tr( style.toLocal8Bit().data() ) ) );
227  toggleStyleSpecificControls( style );
228 
229  //label vertical/horizontal placement
230  mLabelVerticalPlacementComboBox->setCurrentIndex( mLabelVerticalPlacementComboBox->findData( static_cast< int >( mScalebar->labelVerticalPlacement() ) ) );
231  mLabelHorizontalPlacementComboBox->setCurrentIndex( mLabelHorizontalPlacementComboBox->findData( static_cast< int >( mScalebar->labelHorizontalPlacement() ) ) );
232 
233  //alignment
234 
235  Qt::Alignment a = Qt::AlignLeft;
236  switch ( mScalebar->alignment() )
237  {
239  a = Qt::AlignLeft;
240  break;
242  a = Qt::AlignRight;
243  break;
245  a = Qt::AlignHCenter;
246  break;
247  }
248  mAlignmentComboBox->setCurrentAlignment( a );
249 
250  //units
251  mUnitsComboBox->setCurrentIndex( mUnitsComboBox->findData( static_cast< int >( mScalebar->units() ) ) );
252 
253  if ( mScalebar->segmentSizeMode() == QgsScaleBarSettings::SegmentSizeFixed )
254  {
255  mFixedSizeRadio->setChecked( true );
256  mSegmentSizeSpinBox->setEnabled( true );
257  mMinWidthSpinBox->setEnabled( false );
258  mMaxWidthSpinBox->setEnabled( false );
259  }
260  else /*if(mComposerScaleBar->segmentSizeMode() == QgsComposerScaleBar::SegmentSizeFitWidth)*/
261  {
262  mFitWidthRadio->setChecked( true );
263  mSegmentSizeSpinBox->setEnabled( false );
264  mMinWidthSpinBox->setEnabled( true );
265  mMaxWidthSpinBox->setEnabled( true );
266  }
267  mMinWidthSpinBox->setValue( mScalebar->minimumBarWidth() );
268  mMaxWidthSpinBox->setValue( mScalebar->maximumBarWidth() );
269  updateDataDefinedButton( mFillColorDDBtn );
270  updateDataDefinedButton( mFillColor2DDBtn );
271  updateDataDefinedButton( mLineColorDDBtn );
272  updateDataDefinedButton( mLineWidthDDBtn );
273  blockMemberSignals( false );
274 }
275 
276 //slots
277 
278 void QgsLayoutScaleBarWidget::mLineWidthSpinBox_valueChanged( double d )
279 {
280  if ( !mScalebar )
281  {
282  return;
283  }
284 
285  mScalebar->beginCommand( tr( "Set Scalebar Line Width" ), QgsLayoutItem::UndoScaleBarLineWidth );
286  disconnectUpdateSignal();
287  mScalebar->setLineWidth( d );
288  mScalebar->update();
289  connectUpdateSignal();
290  mScalebar->endCommand();
291 }
292 
293 void QgsLayoutScaleBarWidget::mSegmentSizeSpinBox_valueChanged( double d )
294 {
295  if ( !mScalebar )
296  {
297  return;
298  }
299 
300  mScalebar->beginCommand( tr( "Set Scalebar Segment Size" ), QgsLayoutItem::UndoScaleBarSegmentSize );
301  disconnectUpdateSignal();
302  mScalebar->setUnitsPerSegment( d );
303  mScalebar->update();
304  connectUpdateSignal();
305  mScalebar->endCommand();
306 }
307 
308 void QgsLayoutScaleBarWidget::mSegmentsLeftSpinBox_valueChanged( int i )
309 {
310  if ( !mScalebar )
311  {
312  return;
313  }
314 
315  mScalebar->beginCommand( tr( "Set Scalebar Segments" ), QgsLayoutItem::UndoScaleBarSegmentsLeft );
316  disconnectUpdateSignal();
317  mScalebar->setNumberOfSegmentsLeft( i );
318  mScalebar->update();
319  connectUpdateSignal();
320  mScalebar->endCommand();
321 }
322 
323 void QgsLayoutScaleBarWidget::mNumberOfSegmentsSpinBox_valueChanged( int i )
324 {
325  if ( !mScalebar )
326  {
327  return;
328  }
329 
330  mScalebar->beginCommand( tr( "Set Scalebar Segments" ), QgsLayoutItem::UndoScaleBarSegments );
331  disconnectUpdateSignal();
332  mScalebar->setNumberOfSegments( i );
333  mScalebar->update();
334  connectUpdateSignal();
335  mScalebar->endCommand();
336 }
337 
338 void QgsLayoutScaleBarWidget::mHeightSpinBox_valueChanged( double d )
339 {
340  if ( !mScalebar )
341  {
342  return;
343  }
344  mScalebar->beginCommand( tr( "Set Scalebar Height" ), QgsLayoutItem::UndoScaleBarHeight );
345  disconnectUpdateSignal();
346  mScalebar->setHeight( d );
347  mScalebar->update();
348  connectUpdateSignal();
349  mScalebar->endCommand();
350 }
351 
352 void QgsLayoutScaleBarWidget::textFormatChanged()
353 {
354  if ( !mScalebar )
355  {
356  return;
357  }
358 
359  mScalebar->beginCommand( tr( "Set Scalebar Font" ) );
360  disconnectUpdateSignal();
361  mScalebar->setTextFormat( mFontButton->textFormat() );
362  connectUpdateSignal();
363  mScalebar->endCommand();
364  mScalebar->update();
365 }
366 
367 void QgsLayoutScaleBarWidget::changeNumberFormat()
368 {
369  if ( !mScalebar )
370  {
371  return;
372  }
373 
375  widget->setPanelTitle( tr( "Number Format" ) );
376  widget->setFormat( mScalebar->numericFormat() );
377  connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [ = ]
378  {
379  mScalebar->beginCommand( tr( "Set Scalebar Number Format" ) );
380  disconnectUpdateSignal();
381  mScalebar->setNumericFormat( widget->format() );
382  connectUpdateSignal();
383  mScalebar->endCommand();
384  mScalebar->update();
385  } );
386  openPanel( widget );
387  return;
388 }
389 
390 void QgsLayoutScaleBarWidget::mFillColorButton_colorChanged( const QColor &newColor )
391 {
392  if ( !mScalebar )
393  {
394  return;
395  }
396 
397  mScalebar->beginCommand( tr( "Set Scalebar Fill Color" ), QgsLayoutItem::UndoScaleBarFillColor );
398  disconnectUpdateSignal();
399  mScalebar->setFillColor( newColor );
400  mScalebar->update();
401  connectUpdateSignal();
402  mScalebar->endCommand();
403 }
404 
405 void QgsLayoutScaleBarWidget::mFillColor2Button_colorChanged( const QColor &newColor )
406 {
407  if ( !mScalebar )
408  {
409  return;
410  }
411 
412  mScalebar->beginCommand( tr( "Set Scalebar Fill Color" ), QgsLayoutItem::UndoScaleBarFillColor2 );
413  disconnectUpdateSignal();
414  mScalebar->setFillColor2( newColor );
415  mScalebar->update();
416  connectUpdateSignal();
417  mScalebar->endCommand();
418 }
419 
420 void QgsLayoutScaleBarWidget::mStrokeColorButton_colorChanged( const QColor &newColor )
421 {
422  if ( !mScalebar )
423  {
424  return;
425  }
426 
427  mScalebar->beginCommand( tr( "Set Scalebar Stroke Color" ), QgsLayoutItem::UndoScaleBarStrokeColor );
428  disconnectUpdateSignal();
429  mScalebar->setLineColor( newColor );
430  mScalebar->update();
431  connectUpdateSignal();
432  mScalebar->endCommand();
433 }
434 
435 void QgsLayoutScaleBarWidget::mUnitLabelLineEdit_textChanged( const QString &text )
436 {
437  if ( !mScalebar )
438  {
439  return;
440  }
441 
442  mScalebar->beginCommand( tr( "Set Scalebar Unit Text" ), QgsLayoutItem::UndoScaleBarUnitText );
443  disconnectUpdateSignal();
444  mScalebar->setUnitLabel( text );
445  mScalebar->update();
446  connectUpdateSignal();
447  mScalebar->endCommand();
448 }
449 
450 void QgsLayoutScaleBarWidget::mMapUnitsPerBarUnitSpinBox_valueChanged( double d )
451 {
452  if ( !mScalebar )
453  {
454  return;
455  }
456 
457  mScalebar->beginCommand( tr( "Set Scalebar Map Units per Segment" ), QgsLayoutItem::UndoScaleBarMapUnitsSegment );
458  disconnectUpdateSignal();
459  mScalebar->setMapUnitsPerScaleBarUnit( d );
460  mScalebar->update();
461  connectUpdateSignal();
462  mScalebar->endCommand();
463 }
464 
465 void QgsLayoutScaleBarWidget::mStyleComboBox_currentIndexChanged( const QString &text )
466 {
467  if ( !mScalebar )
468  {
469  return;
470  }
471 
472  mScalebar->beginCommand( tr( "Set Scalebar Style" ) );
473  disconnectUpdateSignal();
474  QString untranslatedStyleName;
475  if ( text == tr( "Single Box" ) )
476  {
477  untranslatedStyleName = QStringLiteral( "Single Box" );
478  }
479  else if ( text == tr( "Double Box" ) )
480  {
481  untranslatedStyleName = QStringLiteral( "Double Box" );
482  }
483  else if ( text == tr( "Line Ticks Middle" ) )
484  {
485  untranslatedStyleName = QStringLiteral( "Line Ticks Middle" );
486  }
487  else if ( text == tr( "Line Ticks Middle" ) )
488  {
489  untranslatedStyleName = QStringLiteral( "Line Ticks Middle" );
490  }
491  else if ( text == tr( "Line Ticks Down" ) )
492  {
493  untranslatedStyleName = QStringLiteral( "Line Ticks Down" );
494  }
495  else if ( text == tr( "Line Ticks Up" ) )
496  {
497  untranslatedStyleName = QStringLiteral( "Line Ticks Up" );
498  }
499  else if ( text == tr( "Numeric" ) )
500  {
501  untranslatedStyleName = QStringLiteral( "Numeric" );
502  }
503 
504  //disable or enable controls which apply to specific scale bar styles
505  toggleStyleSpecificControls( untranslatedStyleName );
506 
507  mScalebar->setStyle( untranslatedStyleName );
508  mScalebar->update();
509  connectUpdateSignal();
510  mScalebar->endCommand();
511 }
512 
513 void QgsLayoutScaleBarWidget::toggleStyleSpecificControls( const QString &style )
514 {
515  if ( style == QLatin1String( "Numeric" ) )
516  {
517  //Disable controls which don't apply to numeric scale bars
518  mUnitsComboBox->setEnabled( false );
519  mUnitsLabel->setEnabled( false );
520  mMapUnitsPerBarUnitSpinBox->setEnabled( false );
521  mMapUnitsPerBarUnitLabel->setEnabled( false );
522  mUnitLabelLineEdit->setEnabled( false );
523  mUnitLabelLabel->setEnabled( false );
524  mGroupBoxSegments->setEnabled( false );
525  mGroupBoxSegments->setCollapsed( true );
526  mLabelBarSpaceSpinBox->setEnabled( false );
527  mLineWidthSpinBox->setEnabled( false );
528  mFillColorButton->setEnabled( false );
529  mFillColor2Button->setEnabled( false );
530  mStrokeColorButton->setEnabled( false );
531  mLineJoinStyleCombo->setEnabled( false );
532  mLineCapStyleCombo->setEnabled( false );
533  mLabelVerticalPlacementComboBox->setEnabled( false );
534  mLabelHorizontalPlacementComboBox->setEnabled( false );
535  mAlignmentComboBox->setEnabled( true );
536  }
537  else
538  {
539  //Enable controls
540  mUnitsComboBox->setEnabled( true );
541  mUnitsLabel->setEnabled( true );
542  mMapUnitsPerBarUnitSpinBox->setEnabled( true );
543  mMapUnitsPerBarUnitLabel->setEnabled( true );
544  mUnitLabelLineEdit->setEnabled( true );
545  mUnitLabelLabel->setEnabled( true );
546  mGroupBoxSegments->setEnabled( true );
547  mLabelBarSpaceSpinBox->setEnabled( true );
548  mLineWidthSpinBox->setEnabled( true );
549  mFillColorButton->setEnabled( true );
550  mFillColor2Button->setEnabled( true );
551  mStrokeColorButton->setEnabled( true );
552  mLabelVerticalPlacementComboBox->setEnabled( true );
553  mLabelHorizontalPlacementComboBox->setEnabled( true );
554  mAlignmentComboBox->setEnabled( false );
555  if ( style == QLatin1String( "Single Box" ) || style == QLatin1String( "Double Box" ) )
556  {
557  mLineJoinStyleCombo->setEnabled( true );
558  mLineCapStyleCombo->setEnabled( false );
559  }
560  else
561  {
562  mLineJoinStyleCombo->setEnabled( false );
563  mLineCapStyleCombo->setEnabled( true );
564  }
565 
566  }
567 }
568 
569 void QgsLayoutScaleBarWidget::mLabelBarSpaceSpinBox_valueChanged( double d )
570 {
571  if ( !mScalebar )
572  {
573  return;
574  }
575 
576  mScalebar->beginCommand( tr( "Set Scalebar Label Space" ), QgsLayoutItem::UndoScaleBarLabelBarSize );
577  disconnectUpdateSignal();
578  mScalebar->setLabelBarSpace( d );
579  mScalebar->update();
580  connectUpdateSignal();
581  mScalebar->endCommand();
582 }
583 
584 void QgsLayoutScaleBarWidget::mBoxSizeSpinBox_valueChanged( double d )
585 {
586  if ( !mScalebar )
587  {
588  return;
589  }
590 
591  mScalebar->beginCommand( tr( "Set Scalebar Box Space" ), QgsLayoutItem::UndoScaleBarBoxContentSpace );
592  disconnectUpdateSignal();
593  mScalebar->setBoxContentSpace( d );
594  mScalebar->update();
595  connectUpdateSignal();
596  mScalebar->endCommand();
597 }
598 
599 void QgsLayoutScaleBarWidget::mLabelVerticalPlacementComboBox_currentIndexChanged( int index )
600 {
601  if ( !mScalebar )
602  {
603  return;
604  }
605 
606  mScalebar->beginCommand( tr( "Set Scalebar Label Vertical Placement" ) );
607  disconnectUpdateSignal();
608  mScalebar->setLabelVerticalPlacement( static_cast<QgsScaleBarSettings::LabelVerticalPlacement>( mLabelVerticalPlacementComboBox->itemData( index ).toInt() ) );
609  mScalebar->update();
610  connectUpdateSignal();
611  mScalebar->endCommand();
612 }
613 
614 void QgsLayoutScaleBarWidget::mLabelHorizontalPlacementComboBox_currentIndexChanged( int index )
615 {
616  if ( !mScalebar )
617  {
618  return;
619  }
620 
621  mScalebar->beginCommand( tr( "Set Scalebar Label Horizontal Placement" ) );
622  disconnectUpdateSignal();
623  mScalebar->setLabelHorizontalPlacement( static_cast<QgsScaleBarSettings::LabelHorizontalPlacement>( mLabelHorizontalPlacementComboBox->itemData( index ).toInt() ) );
624  mScalebar->update();
625  connectUpdateSignal();
626  mScalebar->endCommand();
627 }
628 
629 void QgsLayoutScaleBarWidget::alignmentChanged()
630 {
631  if ( !mScalebar )
632  {
633  return;
634  }
635 
636  mScalebar->beginCommand( tr( "Set Scalebar Alignment" ) );
637  disconnectUpdateSignal();
638 
639  const QgsScaleBarSettings::Alignment a = mAlignmentComboBox->currentAlignment() & Qt::AlignLeft ? QgsScaleBarSettings::AlignLeft
640  : mAlignmentComboBox->currentAlignment() & Qt::AlignRight ? QgsScaleBarSettings::AlignRight : QgsScaleBarSettings::AlignMiddle;
641  mScalebar->setAlignment( a );
642  mScalebar->update();
643  connectUpdateSignal();
644  mScalebar->endCommand();
645 }
646 
647 void QgsLayoutScaleBarWidget::mUnitsComboBox_currentIndexChanged( int index )
648 {
649  if ( !mScalebar )
650  {
651  return;
652  }
653 
654  QVariant unitData = mUnitsComboBox->itemData( index );
655  if ( unitData.type() == QVariant::Invalid )
656  {
657  return;
658  }
659 
660  disconnectUpdateSignal();
661  mScalebar->beginCommand( tr( "Set Scalebar Units" ) );
662  mScalebar->applyDefaultSize( static_cast< QgsUnitTypes::DistanceUnit >( unitData.toInt() ) );
663  mScalebar->update();
664 
665  mUnitLabelLineEdit->setText( mScalebar->unitLabel() );
666  mSegmentSizeSpinBox->setValue( mScalebar->unitsPerSegment() );
667  mMapUnitsPerBarUnitSpinBox->setValue( mScalebar->mapUnitsPerScaleBarUnit() );
668 
669  connectUpdateSignal();
670  mScalebar->endCommand();
671 }
672 
673 void QgsLayoutScaleBarWidget::blockMemberSignals( bool block )
674 {
675  mSegmentSizeSpinBox->blockSignals( block );
676  mNumberOfSegmentsSpinBox->blockSignals( block );
677  mSegmentsLeftSpinBox->blockSignals( block );
678  mStyleComboBox->blockSignals( block );
679  mUnitLabelLineEdit->blockSignals( block );
680  mMapUnitsPerBarUnitSpinBox->blockSignals( block );
681  mHeightSpinBox->blockSignals( block );
682  mLineWidthSpinBox->blockSignals( block );
683  mLabelBarSpaceSpinBox->blockSignals( block );
684  mBoxSizeSpinBox->blockSignals( block );
685  mLabelVerticalPlacementComboBox->blockSignals( block );
686  mLabelHorizontalPlacementComboBox->blockSignals( block );
687  mAlignmentComboBox->blockSignals( block );
688  mUnitsComboBox->blockSignals( block );
689  mLineJoinStyleCombo->blockSignals( block );
690  mLineCapStyleCombo->blockSignals( block );
691  mFillColorButton->blockSignals( block );
692  mFillColor2Button->blockSignals( block );
693  mStrokeColorButton->blockSignals( block );
694  mSegmentSizeRadioGroup.blockSignals( block );
695  mMapItemComboBox->blockSignals( block );
696  mFontButton->blockSignals( block );
697  mMinWidthSpinBox->blockSignals( block );
698  mMaxWidthSpinBox->blockSignals( block );
699 }
700 
701 void QgsLayoutScaleBarWidget::connectUpdateSignal()
702 {
703  if ( mScalebar )
704  {
705  connect( mScalebar, &QgsLayoutObject::changed, this, &QgsLayoutScaleBarWidget::setGuiElements );
706  }
707 }
708 
709 void QgsLayoutScaleBarWidget::disconnectUpdateSignal()
710 {
711  if ( mScalebar )
712  {
713  disconnect( mScalebar, &QgsLayoutObject::changed, this, &QgsLayoutScaleBarWidget::setGuiElements );
714  }
715 }
716 
717 void QgsLayoutScaleBarWidget::mLineJoinStyleCombo_currentIndexChanged( int index )
718 {
719  Q_UNUSED( index )
720  if ( !mScalebar )
721  {
722  return;
723  }
724 
725  mScalebar->beginCommand( tr( "Set Scalebar Join Style" ) );
726  mScalebar->setLineJoinStyle( mLineJoinStyleCombo->penJoinStyle() );
727  mScalebar->endCommand();
728 }
729 
730 void QgsLayoutScaleBarWidget::mLineCapStyleCombo_currentIndexChanged( int index )
731 {
732  Q_UNUSED( index )
733  if ( !mScalebar )
734  {
735  return;
736  }
737 
738  mScalebar->beginCommand( tr( "Set Scalebar Cap Style" ) );
739  mScalebar->setLineCapStyle( mLineCapStyleCombo->penCapStyle() );
740  mScalebar->endCommand();
741 }
742 
743 void QgsLayoutScaleBarWidget::segmentSizeRadioChanged( QAbstractButton *radio )
744 {
745  bool fixedSizeMode = radio == mFixedSizeRadio;
746  mMinWidthSpinBox->setEnabled( !fixedSizeMode );
747  mMaxWidthSpinBox->setEnabled( !fixedSizeMode );
748  mSegmentSizeSpinBox->setEnabled( fixedSizeMode );
749 
750  if ( !mScalebar )
751  {
752  return;
753  }
754 
755  mScalebar->beginCommand( tr( "Set Scalebar Size Mode" ), QgsLayoutItem::UndoScaleBarSegmentSize );
756  disconnectUpdateSignal();
757  if ( mFixedSizeRadio->isChecked() )
758  {
759  mScalebar->setSegmentSizeMode( QgsScaleBarSettings::SegmentSizeFixed );
760  mScalebar->setUnitsPerSegment( mSegmentSizeSpinBox->value() );
761  }
762  else /*if(mFitWidthRadio->isChecked())*/
763  {
764  mScalebar->setSegmentSizeMode( QgsScaleBarSettings::SegmentSizeFitWidth );
765  }
766  mScalebar->update();
767  connectUpdateSignal();
768  mScalebar->endCommand();
769 }
770 
771 void QgsLayoutScaleBarWidget::mapChanged( QgsLayoutItem *item )
772 {
773  QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap * >( item );
774  if ( !map )
775  {
776  return;
777  }
778 
779  //set it to scale bar
780  mScalebar->beginCommand( tr( "Set Scalebar Map" ) );
781  disconnectUpdateSignal();
782  mScalebar->setLinkedMap( map );
783  mScalebar->update();
784  connectUpdateSignal();
785  mScalebar->endCommand();
786 }
787 
788 void QgsLayoutScaleBarWidget::mMinWidthSpinBox_valueChanged( double )
789 {
790  if ( !mScalebar )
791  {
792  return;
793  }
794 
795  mScalebar->beginCommand( tr( "Set Scalebar Size Mode" ), QgsLayoutItem::UndoScaleBarSegmentSize );
796  disconnectUpdateSignal();
797  mScalebar->setMinimumBarWidth( mMinWidthSpinBox->value() );
798  mScalebar->update();
799  connectUpdateSignal();
800  mScalebar->endCommand();
801 }
802 
803 void QgsLayoutScaleBarWidget::mMaxWidthSpinBox_valueChanged( double )
804 {
805  if ( !mScalebar )
806  {
807  return;
808  }
809 
810  mScalebar->beginCommand( tr( "Set Scalebar Size Mode" ), QgsLayoutItem::UndoScaleBarSegmentSize );
811  disconnectUpdateSignal();
812  mScalebar->setMaximumBarWidth( mMaxWidthSpinBox->value() );
813  mScalebar->update();
814  connectUpdateSignal();
815  mScalebar->endCommand();
816 }
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Single variable definition for use within a QgsExpressionContextScope.
Scale bar segment size is calculated to fit a size range.
void setMasterLayout(QgsMasterLayoutInterface *masterLayout) override
Sets the master layout associated with the item.
Base class for graphical items within a QgsLayout.
int type() const override
Returns a unique graphics item type identifier.
QgsLayoutScaleBarWidget(QgsLayoutItemScaleBar *scaleBar)
constructor
A widget for controlling the common properties of layout items (e.g.
Scalebar secondary fill color.
Alignment
Scalebar alignment.
void updateDataDefinedButton(QgsPropertyOverrideButton *button)
Updates a previously registered data defined button to reflect the item&#39;s current properties...
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
void setLayer(QgsVectorLayer *layer)
Sets a layer to associate with the widget.
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property)
Registers a data defined button, setting up its initial value, connections and description.
Layout graphical items for displaying a map.
Scalebar secondary fill color.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void changed()
Emitted when the widget&#39;s text format settings are changed.
Scalebar map units per segment.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context&#39;s layer is changed.
Configure font settings for use with QgsTextRenderer.
Definition: qgsfontbutton.h:58
Single scope for storing variables and functions for use within a QgsExpressionContext.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
Scale bar segment size is fixed to a map unit.
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
bool setNewItem(QgsLayoutItem *item) override
Attempts to update the widget to show the properties for the specified item.
Labels are drawn above the scalebar.
Labels are drawn centered relative to segment&#39;s edge.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
Unknown distance unit.
Definition: qgsunittypes.h:77
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
A base class for property widgets for layout items.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Labels are drawn centered relative to segment.
void changed()
Emitted when the alignment is changed.
A layout item subclass for scale bars.
void setFormat(const QgsNumericFormat *format)
Sets the format to show in the widget.
void setItem(QgsLayoutItem *item)
Sets the layout item.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
void changed()
Emitted whenever the format configured55 in the widget is changed.
Terrestrial miles.
Definition: qgsunittypes.h:73
Interface for master layout type objects, such as print layouts and reports.
Scalebar number of segments.
void changed()
Emitted when the object&#39;s properties change.
Labels are drawn below the scalebar.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
A widget which allows choice of numeric formats and the properties of them.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setMasterLayout(QgsMasterLayoutInterface *masterLayout)
Sets the master layout associated with the item.
QgsNumericFormat * format() const
Returns a new format object representing the settings currently configured in the widget...