Skip to content

Commit 8d180c1

Browse files
committed
[needs-docs] Tweak custom dash button appearance
- remove "Change" label and replace with larger dash preview icon. The "change" text is unnecessary and adds to dialog clutter, better to use the space for a wider preview icon (especially given that the previous narrow icon never really showed enough of the pattern to be useful!) - don't offset the line in the preview if the symbol has an offset set - respond correctly to dash pattern, line width unit changes, cap style changes - show a nice big preview tooltip on hover
1 parent 4aaa523 commit 8d180c1

File tree

4 files changed

+93
-29
lines changed

4 files changed

+93
-29
lines changed

python/gui/auto_generated/symbology/qgssymbollayerwidget.sip.in

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ Creates a new QgsSimpleLineSymbolLayerWidget.
125125

126126
void updatePatternIcon();
127127

128+
virtual void resizeEvent( QResizeEvent *event );
129+
130+
128131
};
129132

130133

src/gui/symbology/qgssymbollayerwidget.cpp

+48-5
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ void QgsSimpleLineSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer *layer )
298298
mCustomCheckBox->setCheckState( useCustomDashPattern ? Qt::Checked : Qt::Unchecked );
299299
mCustomCheckBox->blockSignals( false );
300300

301+
//make sure height of custom dash button looks good under different platforms
302+
QSize size = mChangePatternButton->minimumSizeHint();
303+
int fontHeight = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.4 );
304+
mChangePatternButton->setMinimumSize( QSize( size.width(), std::max( size.height(), fontHeight ) ) );
305+
301306
//draw inside polygon?
302307
const bool drawInsidePolygon = mLayer->drawInsidePolygon();
303308
whileBlocking( mDrawInsideCheckBox )->setCheckState( drawInsidePolygon ? Qt::Checked : Qt::Unchecked );
@@ -332,7 +337,6 @@ void QgsSimpleLineSymbolLayerWidget::penWidthChanged()
332337
void QgsSimpleLineSymbolLayerWidget::colorChanged( const QColor &color )
333338
{
334339
mLayer->setColor( color );
335-
updatePatternIcon();
336340
emit changed();
337341
}
338342

@@ -341,6 +345,7 @@ void QgsSimpleLineSymbolLayerWidget::penStyleChanged()
341345
mLayer->setPenStyle( cboPenStyle->penStyle() );
342346
mLayer->setPenJoinStyle( cboJoinStyle->penJoinStyle() );
343347
mLayer->setPenCapStyle( cboCapStyle->penCapStyle() );
348+
updatePatternIcon();
344349
emit changed();
345350
}
346351

@@ -396,6 +401,7 @@ void QgsSimpleLineSymbolLayerWidget::mPenWidthUnitWidget_changed()
396401
{
397402
mLayer->setWidthUnit( mPenWidthUnitWidget->unit() );
398403
mLayer->setWidthMapUnitScale( mPenWidthUnitWidget->getMapUnitScale() );
404+
updatePatternIcon();
399405
emit changed();
400406
}
401407
}
@@ -416,6 +422,7 @@ void QgsSimpleLineSymbolLayerWidget::mDashPatternUnitWidget_changed()
416422
{
417423
mLayer->setCustomDashPatternUnit( mDashPatternUnitWidget->unit() );
418424
mLayer->setCustomDashPatternMapUnitScale( mDashPatternUnitWidget->getMapUnitScale() );
425+
updatePatternIcon();
419426
emit changed();
420427
}
421428
}
@@ -434,17 +441,53 @@ void QgsSimpleLineSymbolLayerWidget::updatePatternIcon()
434441
{
435442
return;
436443
}
437-
QgsSimpleLineSymbolLayer *layerCopy = mLayer->clone();
444+
std::unique_ptr< QgsSimpleLineSymbolLayer > layerCopy( mLayer->clone() );
438445
if ( !layerCopy )
439446
{
440447
return;
441448
}
442449
QColor color = qApp->palette().color( QPalette::WindowText );
443450
layerCopy->setColor( color );
451+
// reset offset, we don't want to show that in the preview
452+
layerCopy->setOffset( 0 );
444453
layerCopy->setUseCustomDashPattern( true );
445-
QIcon buttonIcon = QgsSymbolLayerUtils::symbolLayerPreviewIcon( layerCopy, QgsUnitTypes::RenderMillimeters, mChangePatternButton->iconSize() );
446-
mChangePatternButton->setIcon( buttonIcon );
447-
delete layerCopy;
454+
455+
QSize currentIconSize;
456+
//icon size is button size with a small margin
457+
#ifdef Q_OS_WIN
458+
currentIconSize = QSize( mChangePatternButton->width() - 10, mChangePatternButton->height() - 6 );
459+
#else
460+
currentIconSize = QSize( mChangePatternButton->width() - 10, mChangePatternButton->height() - 12 );
461+
#endif
462+
463+
if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
464+
{
465+
return;
466+
}
467+
468+
//create an icon pixmap
469+
std::unique_ptr< QgsLineSymbol > previewSymbol = qgis::make_unique< QgsLineSymbol >( QgsSymbolLayerList() << layerCopy.release() );
470+
const QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( previewSymbol.get(), currentIconSize );
471+
mChangePatternButton->setIconSize( currentIconSize );
472+
mChangePatternButton->setIcon( icon );
473+
474+
// set tooltip
475+
// create very large preview image
476+
int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
477+
int height = static_cast< int >( width / 1.61803398875 ); // golden ratio
478+
479+
QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( previewSymbol.get(), QSize( width, height ), height / 20 );
480+
QByteArray data;
481+
QBuffer buffer( &data );
482+
pm.save( &buffer, "PNG", 100 );
483+
mChangePatternButton->setToolTip( QStringLiteral( "<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
484+
}
485+
486+
void QgsSimpleLineSymbolLayerWidget::resizeEvent( QResizeEvent *event )
487+
{
488+
QgsSymbolLayerWidget::resizeEvent( event );
489+
// redraw custom dash pattern icon -- the button size has changed
490+
updatePatternIcon();
448491
}
449492

450493

src/gui/symbology/qgssymbollayerwidget.h

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class GUI_EXPORT QgsSimpleLineSymbolLayerWidget : public QgsSymbolLayerWidget, p
153153
//creates a new icon for the 'change pattern' button
154154
void updatePatternIcon();
155155

156+
void resizeEvent( QResizeEvent *event ) override;
157+
156158
private slots:
157159

158160
void updateAssistantSymbol();

src/ui/symbollayer/widget_simpleline.ui

+40-24
Original file line numberDiff line numberDiff line change
@@ -225,30 +225,6 @@
225225
</property>
226226
</widget>
227227
</item>
228-
<item row="7" column="2">
229-
<layout class="QHBoxLayout" name="horizontalLayout_12">
230-
<item>
231-
<widget class="QPushButton" name="mChangePatternButton">
232-
<property name="sizePolicy">
233-
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
234-
<horstretch>0</horstretch>
235-
<verstretch>0</verstretch>
236-
</sizepolicy>
237-
</property>
238-
<property name="text">
239-
<string>Change</string>
240-
</property>
241-
</widget>
242-
</item>
243-
<item>
244-
<widget class="QgsUnitSelectionWidget" name="mDashPatternUnitWidget" native="true">
245-
<property name="focusPolicy">
246-
<enum>Qt::StrongFocus</enum>
247-
</property>
248-
</widget>
249-
</item>
250-
</layout>
251-
</item>
252228
<item row="4" column="3">
253229
<widget class="QgsPropertyOverrideButton" name="mJoinStyleDDBtn">
254230
<property name="text">
@@ -287,6 +263,46 @@
287263
<item row="9" column="2" colspan="2">
288264
<widget class="QComboBox" name="mRingFilterComboBox"/>
289265
</item>
266+
<item row="7" column="0" colspan="3">
267+
<layout class="QHBoxLayout" name="horizontalLayout_12">
268+
<item>
269+
<spacer name="horizontalSpacer">
270+
<property name="orientation">
271+
<enum>Qt::Horizontal</enum>
272+
</property>
273+
<property name="sizeType">
274+
<enum>QSizePolicy::Maximum</enum>
275+
</property>
276+
<property name="sizeHint" stdset="0">
277+
<size>
278+
<width>20</width>
279+
<height>1</height>
280+
</size>
281+
</property>
282+
</spacer>
283+
</item>
284+
<item>
285+
<widget class="QPushButton" name="mChangePatternButton">
286+
<property name="sizePolicy">
287+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
288+
<horstretch>0</horstretch>
289+
<verstretch>0</verstretch>
290+
</sizepolicy>
291+
</property>
292+
<property name="text">
293+
<string/>
294+
</property>
295+
</widget>
296+
</item>
297+
<item>
298+
<widget class="QgsUnitSelectionWidget" name="mDashPatternUnitWidget" native="true">
299+
<property name="focusPolicy">
300+
<enum>Qt::StrongFocus</enum>
301+
</property>
302+
</widget>
303+
</item>
304+
</layout>
305+
</item>
290306
</layout>
291307
</widget>
292308
<customwidgets>

0 commit comments

Comments
 (0)