Skip to content

Commit

Permalink
[needs-docs] Tweak custom dash button appearance
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
nyalldawson committed Apr 30, 2019
1 parent 4aaa523 commit 8d180c1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Creates a new QgsSimpleLineSymbolLayerWidget.

void updatePatternIcon();

virtual void resizeEvent( QResizeEvent *event );


};


Expand Down
53 changes: 48 additions & 5 deletions src/gui/symbology/qgssymbollayerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ void QgsSimpleLineSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer *layer )
mCustomCheckBox->setCheckState( useCustomDashPattern ? Qt::Checked : Qt::Unchecked );
mCustomCheckBox->blockSignals( false );

//make sure height of custom dash button looks good under different platforms
QSize size = mChangePatternButton->minimumSizeHint();
int fontHeight = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.4 );
mChangePatternButton->setMinimumSize( QSize( size.width(), std::max( size.height(), fontHeight ) ) );

//draw inside polygon?
const bool drawInsidePolygon = mLayer->drawInsidePolygon();
whileBlocking( mDrawInsideCheckBox )->setCheckState( drawInsidePolygon ? Qt::Checked : Qt::Unchecked );
Expand Down Expand Up @@ -332,7 +337,6 @@ void QgsSimpleLineSymbolLayerWidget::penWidthChanged()
void QgsSimpleLineSymbolLayerWidget::colorChanged( const QColor &color )
{
mLayer->setColor( color );
updatePatternIcon();
emit changed();
}

Expand All @@ -341,6 +345,7 @@ void QgsSimpleLineSymbolLayerWidget::penStyleChanged()
mLayer->setPenStyle( cboPenStyle->penStyle() );
mLayer->setPenJoinStyle( cboJoinStyle->penJoinStyle() );
mLayer->setPenCapStyle( cboCapStyle->penCapStyle() );
updatePatternIcon();
emit changed();
}

Expand Down Expand Up @@ -396,6 +401,7 @@ void QgsSimpleLineSymbolLayerWidget::mPenWidthUnitWidget_changed()
{
mLayer->setWidthUnit( mPenWidthUnitWidget->unit() );
mLayer->setWidthMapUnitScale( mPenWidthUnitWidget->getMapUnitScale() );
updatePatternIcon();
emit changed();
}
}
Expand All @@ -416,6 +422,7 @@ void QgsSimpleLineSymbolLayerWidget::mDashPatternUnitWidget_changed()
{
mLayer->setCustomDashPatternUnit( mDashPatternUnitWidget->unit() );
mLayer->setCustomDashPatternMapUnitScale( mDashPatternUnitWidget->getMapUnitScale() );
updatePatternIcon();
emit changed();
}
}
Expand All @@ -434,17 +441,53 @@ void QgsSimpleLineSymbolLayerWidget::updatePatternIcon()
{
return;
}
QgsSimpleLineSymbolLayer *layerCopy = mLayer->clone();
std::unique_ptr< QgsSimpleLineSymbolLayer > layerCopy( mLayer->clone() );
if ( !layerCopy )
{
return;
}
QColor color = qApp->palette().color( QPalette::WindowText );
layerCopy->setColor( color );
// reset offset, we don't want to show that in the preview
layerCopy->setOffset( 0 );
layerCopy->setUseCustomDashPattern( true );
QIcon buttonIcon = QgsSymbolLayerUtils::symbolLayerPreviewIcon( layerCopy, QgsUnitTypes::RenderMillimeters, mChangePatternButton->iconSize() );
mChangePatternButton->setIcon( buttonIcon );
delete layerCopy;

QSize currentIconSize;
//icon size is button size with a small margin
#ifdef Q_OS_WIN
currentIconSize = QSize( mChangePatternButton->width() - 10, mChangePatternButton->height() - 6 );
#else
currentIconSize = QSize( mChangePatternButton->width() - 10, mChangePatternButton->height() - 12 );
#endif

if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
{
return;
}

//create an icon pixmap
std::unique_ptr< QgsLineSymbol > previewSymbol = qgis::make_unique< QgsLineSymbol >( QgsSymbolLayerList() << layerCopy.release() );
const QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( previewSymbol.get(), currentIconSize );
mChangePatternButton->setIconSize( currentIconSize );
mChangePatternButton->setIcon( icon );

// set tooltip
// create very large preview image
int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
int height = static_cast< int >( width / 1.61803398875 ); // golden ratio

QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( previewSymbol.get(), QSize( width, height ), height / 20 );
QByteArray data;
QBuffer buffer( &data );
pm.save( &buffer, "PNG", 100 );
mChangePatternButton->setToolTip( QStringLiteral( "<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
}

void QgsSimpleLineSymbolLayerWidget::resizeEvent( QResizeEvent *event )
{
QgsSymbolLayerWidget::resizeEvent( event );
// redraw custom dash pattern icon -- the button size has changed
updatePatternIcon();
}


Expand Down
2 changes: 2 additions & 0 deletions src/gui/symbology/qgssymbollayerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class GUI_EXPORT QgsSimpleLineSymbolLayerWidget : public QgsSymbolLayerWidget, p
//creates a new icon for the 'change pattern' button
void updatePatternIcon();

void resizeEvent( QResizeEvent *event ) override;

private slots:

void updateAssistantSymbol();
Expand Down
64 changes: 40 additions & 24 deletions src/ui/symbollayer/widget_simpleline.ui
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,6 @@
</property>
</widget>
</item>
<item row="7" column="2">
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<widget class="QPushButton" name="mChangePatternButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Change</string>
</property>
</widget>
</item>
<item>
<widget class="QgsUnitSelectionWidget" name="mDashPatternUnitWidget" native="true">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="3">
<widget class="QgsPropertyOverrideButton" name="mJoinStyleDDBtn">
<property name="text">
Expand Down Expand Up @@ -287,6 +263,46 @@
<item row="9" column="2" colspan="2">
<widget class="QComboBox" name="mRingFilterComboBox"/>
</item>
<item row="7" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Maximum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>1</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="mChangePatternButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QgsUnitSelectionWidget" name="mDashPatternUnitWidget" native="true">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
Expand Down

0 comments on commit 8d180c1

Please sign in to comment.