41#include "moc_qgsfontbutton.cpp"
43using namespace Qt::StringLiterals;
46 : QToolButton( parent )
48 , mNullFormatString( tr(
"No Format" ) )
50 setText( tr(
"Font" ) );
52 setAcceptDrops(
true );
53 connect(
this, &QAbstractButton::clicked,
this, &QgsFontButton::showSettingsDialog );
56 mMenu =
new QMenu(
this );
57 connect( mMenu, &QMenu::aboutToShow,
this, &QgsFontButton::prepareMenu );
59 setPopupMode( QToolButton::MenuButtonPopup );
62 const QSize size = QToolButton::minimumSizeHint();
65 mSizeHint = QSize( std::max( minWidth, size.width() ), std::max( size.height(), fontHeight ) );
81void QgsFontButton::showSettingsDialog()
88 if ( mExpressionContextGenerator )
95 QgsSymbolWidgetContext symbolContext;
103 mActivePanel =
new QgsTextFormatPanelWidget( mFormat, mMapCanvas,
this, mLayer.data() );
104 mActivePanel->setPanelTitle( mDialogTitle );
105 mActivePanel->setContext( symbolContext );
115 QgsTextFormatDialog dialog( mFormat, mMapCanvas,
this, QgsGuiUtils::ModalDialogFlags, mLayer.data() );
116 dialog.setWindowTitle( mDialogTitle );
117 dialog.setContext( symbolContext );
166 if ( mActivePanel && !format.
isValid() )
167 mActivePanel->acceptPanel();
172 if ( mActivePanel && format.
isValid() )
173 mActivePanel->setFormat( format );
186 QColor opaque = color;
187 opaque.setAlphaF( 1.0 );
189 if ( mNullFormatAction )
190 mNullFormatAction->setChecked(
false );
192 if ( mFormat.color() != opaque )
194 mFormat.setColor( opaque );
205 QApplication::clipboard()->setMimeData( mFormat.toMimeData().release() );
218 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
223 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), font ) )
232 if ( e->type() == QEvent::ToolTip )
234 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>( e );
236 double fontSize = 0.0;
240 fontSize = mFormat.size();
244 fontSize = mFont.pointSizeF();
247 toolTip = u
"<b>%1</b><br>%2<br>Size: %3"_s.arg( text(), mMode ==
ModeTextRenderer ? mFormat.font().family() : mFont.family() ).arg( fontSize );
248 QToolTip::showText( helpEvent->globalPos(), toolTip );
250 return QToolButton::event( e );
255 if ( e->button() == Qt::LeftButton )
257 mDragStartPosition = e->pos();
259 QToolButton::mousePressEvent( e );
264 QToolButton::showMenu();
272 if ( !( e->buttons() & Qt::LeftButton ) )
275 QToolButton::mouseMoveEvent( e );
279 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
282 QToolButton::mouseMoveEvent( e );
287 QDrag *drag =
new QDrag(
this );
291 drag->setMimeData( mFormat.toMimeData().release() );
299 drag->setPixmap( createDragIcon( QSize( iconSize, iconSize ) ) );
300 drag->exec( Qt::CopyAction );
304bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
309 if ( mimeColor.isValid() )
311 resultColor = mimeColor;
325 bool hasAlpha =
false;
327 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
329 e->acceptProposedAction();
330 updatePreview( QColor(), &format );
332 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
334 e->acceptProposedAction();
335 updatePreview( QColor(),
nullptr, &font );
337 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
342 e->acceptProposedAction();
343 updatePreview( mimeColor );
360 bool hasAlpha =
false;
361 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
367 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
373 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
376 e->acceptProposedAction();
380 mFormat.setOpacity( mimeColor.alphaF() );
382 mimeColor.setAlphaF( 1.0 );
383 mFormat.setColor( mimeColor );
397 size = mFormat.size();
401 size = mFont.pointSizeF();
405 const double increment = (
event->modifiers() & Qt::ControlModifier ) ? 0.1 : 1;
406 if (
event->angleDelta().y() > 0 )
414 size = std::max( size, 1.0 );
428 QFont newFont = mFont;
429 newFont.setPointSizeF( size );
438QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const
442 QPixmap pixmap( iconSize, iconSize );
443 pixmap.fill( Qt::transparent );
449 p.setBrush( QBrush( color ) );
452 p.setPen( QColor( 197, 197, 197 ) );
453 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
458QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const
461 tempFormat = &mFormat;
466 QPixmap pixmap( size.width(), size.height() );
467 pixmap.fill( Qt::transparent );
470 p.setRenderHint( QPainter::Antialiasing );
471 const QRect rect( 0, 0, size.width(), size.height() );
473 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
475 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
476 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
480 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
481 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
484 p.setBrush( Qt::NoBrush );
485 p.setPen( Qt::NoPen );
491 QgsRenderContext context;
492 QgsMapToPixel newCoordXForm;
505 ? fontSize * tempFormat->
buffer().
size() / 100
515 ? fontSize * tempFormat->
buffer().
size() / 100
521 QRectF textRect = rect;
522 textRect.setLeft( xtrans );
523 textRect.setWidth( textRect.width() - xtrans );
524 textRect.setTop( ytrans );
525 if ( textRect.height() > 300 )
526 textRect.setHeight( 300 );
527 if ( textRect.width() > 2000 )
528 textRect.setWidth( 2000 );
535 p.setBrush( Qt::NoBrush );
536 p.setPen( QColor( 0, 0, 0 ) );
537 p.setFont( *tempFont );
538 QRectF textRect = rect;
539 textRect.setLeft( 2 );
540 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
549void QgsFontButton::prepareMenu()
559 mNullFormatAction =
new QAction( mNullFormatString,
this );
560 mMenu->addAction( mNullFormatAction );
562 if ( !mFormat.isValid() )
564 mNullFormatAction->setCheckable(
true );
565 mNullFormatAction->setChecked(
true );
569 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
570 QWidget *sizeWidget =
new QWidget();
571 QVBoxLayout *sizeLayout =
new QVBoxLayout();
572 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
573 sizeLayout->setSpacing( 2 );
575 QString fontHeaderLabel;
583 fontHeaderLabel = tr(
"Font size (pt)" );
587 QgsMenuHeader *sizeLabel =
new QgsMenuHeader( fontHeaderLabel );
588 sizeLayout->addWidget( sizeLabel );
590 QgsDoubleSpinBox *sizeSpin =
new QgsDoubleSpinBox(
nullptr );
591 sizeSpin->setDecimals( 4 );
592 sizeSpin->setMaximum( 1e+9 );
594 sizeSpin->setValue( mMode ==
ModeTextRenderer ? mFormat.size() : mFont.pointSizeF() );
595 connect( sizeSpin,
static_cast<void ( QgsDoubleSpinBox::* )(
double )
>( &QgsDoubleSpinBox::valueChanged ),
this, [
this](
double value ) {
599 if ( mNullFormatAction )
600 mNullFormatAction->setChecked(
false );
601 mFormat.setSize( value );
604 mFont.setPointSizeF( value );
610 QHBoxLayout *spinLayout =
new QHBoxLayout();
611 spinLayout->setContentsMargins( 4, 0, 4, 0 );
612 spinLayout->addWidget( sizeSpin );
613 sizeLayout->addLayout( spinLayout );
614 sizeWidget->setLayout( sizeLayout );
615 sizeAction->setDefaultWidget( sizeWidget );
616 sizeWidget->setFocusProxy( sizeSpin );
617 sizeWidget->setFocusPolicy( Qt::StrongFocus );
618 mMenu->addAction( sizeAction );
620 QMenu *recentFontMenu =
new QMenu( tr(
"Recent Fonts" ), mMenu );
622 for (
const QString &family : recentFontFamilies )
624 QAction *fontAction =
new QAction( family, recentFontMenu );
625 QFont f = fontAction->font();
627 fontAction->setFont( f );
628 fontAction->setToolTip( family );
629 recentFontMenu->addAction( fontAction );
630 if ( ( mMode ==
ModeTextRenderer && family == mFormat.font().family() ) || ( mMode ==
ModeQFont && family == mFont.family() ) )
632 fontAction->setCheckable(
true );
633 fontAction->setChecked(
true );
635 auto setFont = [
this, family] {
640 QgsTextFormat newFormat = mFormat;
641 QFont f = newFormat.
font();
658 connect( fontAction, &QAction::triggered,
this, setFont );
660 mMenu->addMenu( recentFontMenu );
662 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
663 mMenu->addAction( configureAction );
664 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
666 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
667 mMenu->addAction( copyFormatAction );
669 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
672 QgsTextFormat tempFormat;
675 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
679 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ), &tempFormat ) );
681 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
683 tempFont.setPointSize( 8 );
684 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ),
nullptr, &tempFont ) );
688 pasteFormatAction->setEnabled(
false );
690 mMenu->addAction( pasteFormatAction );
695 mMenu->addSeparator();
697 QgsColorWheel *colorWheel =
new QgsColorWheel( mMenu );
698 colorWheel->
setColor( mFormat.color() );
699 QgsColorWidgetAction *colorAction =
new QgsColorWidgetAction( colorWheel, mMenu, mMenu );
702 mMenu->addAction( colorAction );
705 QColor alphaColor = mFormat.color();
706 alphaColor.setAlphaF( mFormat.opacity() );
708 QgsColorWidgetAction *alphaAction =
new QgsColorWidgetAction( alphaRamp, mMenu, mMenu );
711 const double opacity = color.alphaF();
712 mFormat.setOpacity( opacity );
714 if ( mNullFormatAction )
715 mNullFormatAction->setChecked(
false );
719 mMenu->addAction( alphaAction );
723 QList<QgsColorScheme *>::iterator it = schemeList.begin();
724 for ( ; it != schemeList.end(); ++it )
726 QgsColorSwatchGridAction *colorAction =
new QgsColorSwatchGridAction( *it, mMenu, u
"labeling"_s,
this );
728 mMenu->addAction( colorAction );
733 mMenu->addSeparator();
735 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
736 mMenu->addAction( copyColorAction );
739 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
743 bool hasAlpha =
false;
744 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
746 pasteColorAction->setIcon( createColorIcon( clipColor ) );
750 pasteColorAction->setEnabled(
false );
752 mMenu->addAction( pasteColorAction );
757void QgsFontButton::addRecentColor(
const QColor &color )
779 mExpressionContextGenerator = generator;
800bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const
810bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const
822 if ( e->type() == QEvent::EnabledChange )
826 QToolButton::changeEvent( e );
832 QToolButton::showEvent( e );
837 QToolButton::resizeEvent(
event );
843void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
845 if ( mShowNoFormat && !mFormat.
isValid() )
847 setIcon( QPixmap() );
851 QgsTextFormat tempFormat;
855 tempFormat = *format;
857 tempFormat = mFormat;
863 if ( color.isValid() )
870 tempFormat.
setSize( QToolButton::font().pointSizeF() - 2 );
873 QSize currentIconSize;
877 if ( !mIconSize.isValid() )
880 QStyleOptionToolButton opt;
881 initStyleOption( &opt );
882 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
this );
885 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
886#elif defined( Q_OS_MAC )
887 mIconSize = QSize( buttonSize.width() - 10, height() - 2 );
889 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
892 currentIconSize = mIconSize;
898 currentIconSize = QSize( width() - 10, height() - 6 );
900 currentIconSize = QSize( width() - 10, height() - 12 );
904 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
910 QPixmap pixmap( currentIconSize * devicePixelRatioF() );
911 pixmap.fill( Qt::transparent );
912 pixmap.setDevicePixelRatio( devicePixelRatioF() );
915 p.setRenderHint( QPainter::Antialiasing );
916 const QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
922 QgsRenderContext context;
923 QgsMapToPixel newCoordXForm;
954 QRectF textRect = rect;
955 textRect.setLeft( xtrans );
956 textRect.setWidth( textRect.width() - xtrans );
957 textRect.setTop( ytrans );
958 if ( textRect.height() > 300 )
959 textRect.setHeight( 300 );
960 if ( textRect.width() > 2000 )
961 textRect.setWidth( 2000 );
968 p.setBrush( Qt::NoBrush );
969 p.setPen( QColor( 0, 0, 0 ) );
970 p.setFont( tempFont );
971 QRectF textRect = rect;
972 textRect.setLeft( 2 );
973 p.drawText( textRect, Qt::AlignVCenter, text() );
978 setIconSize( currentIconSize );
991 bool hasAlpha =
false;
992 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
1002 mDialogTitle = title;
1007 return mDialogTitle;
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
@ Points
Points (e.g., for font sizes).
@ Antialiasing
Use antialiasing while drawing.
static const double UI_SCALE_FACTOR
UI scaling factor.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
void setColor(const QColor &color, bool emitSignals=false) override
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static std::vector< std::unique_ptr< QgsExpressionContextScope > > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
static std::unique_ptr< QMimeData > toMimeData(const QFont &font)
Returns new mime data representing the specified font settings.
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
static void addRecentFontFamily(const QString &family)
Adds a font family to the list of recently used font families.
static QFont fromMimeData(const QMimeData *data, bool *ok=nullptr)
Attempts to parse the provided mime data as a QFont.
static QStringList recentFontFamilies()
Returns a list of recently used font families.
Map canvas is a class for displaying all GIS data types on a canvas.
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Sets parameters for use in transforming coordinates.
A bar for displaying non-blocking messages to the user.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
void setDevicePixelRatio(float ratio)
Sets the device pixel ratio.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
void setRasterizedRenderingPolicy(Qgis::RasterizedRenderingPolicy policy)
Sets the policy controlling when rasterisation of content during renders is permitted.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
A utility class for dynamic handling of changes to screen properties.
void screenDpiChanged(double dpi)
Emitted whenever the screen dpi associated with the widget is changed.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
static std::unique_ptr< QMimeData > colorToMimeData(const QColor &color)
Creates mime data from a color.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
QSizeF size() const
Returns the size of the background shape.
bool enabled() const
Returns whether the background is enabled.
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
Qgis::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
bool enabled() const
Returns whether the buffer is enabled.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the size.
void setFont(const QFont &font)
Sets the font used for rendering text.
static QgsTextFormat fromMimeData(const QMimeData *data, bool *ok=nullptr)
Attempts to parse the provided mime data as a QgsTextFormat.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
bool isValid() const
Returns true if the format is valid.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
Qgis::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
double size() const
Returns the size for rendered text.
QColor color() const
Returns the color that text will be rendered in.
QFont font() const
Returns the font used for rendering text.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
Represents a vector layer which manages a vector based dataset.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
QFont getFont(bool &ok, const QFont &initial, const QString &title)
Show font selection dialog.