39 : QToolButton( parent )
40 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Text Format" ) : dialogTitle )
41 , mNullFormatString( tr(
"No Format" ) )
43 setText( tr(
"Font" ) );
45 setAcceptDrops(
true );
46 connect(
this, &QAbstractButton::clicked,
this, &QgsFontButton::showSettingsDialog );
49 mMenu =
new QMenu(
this );
50 connect( mMenu, &QMenu::aboutToShow,
this, &QgsFontButton::prepareMenu );
52 setPopupMode( QToolButton::MenuButtonPopup );
55 const QSize size = QToolButton::minimumSizeHint();
58 mSizeHint = QSize( std::max( minWidth, size.width() ), std::max( size.height(), fontHeight ) );
74void QgsFontButton::showSettingsDialog()
81 if ( mExpressionContextGenerator )
97 mActivePanel->setPanelTitle( mDialogTitle );
98 mActivePanel->setContext( symbolContext );
105 QgsTextFormatDialog dialog( mFormat, mMapCanvas,
this, QgsGuiUtils::ModalDialogFlags, mLayer.data() );
106 dialog.setWindowTitle( mDialogTitle );
107 dialog.setContext( symbolContext );
156 if ( mActivePanel && !format.
isValid() )
157 mActivePanel->acceptPanel();
162 if ( mActivePanel && format.
isValid() )
163 mActivePanel->setFormat( format );
176 QColor opaque = color;
177 opaque.setAlphaF( 1.0 );
179 if ( mNullFormatAction )
180 mNullFormatAction->setChecked(
false );
182 if ( mFormat.
color() != opaque )
195 QApplication::clipboard()->setMimeData( mFormat.
toMimeData() );
208 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
213 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), font ) )
222 if ( e->type() == QEvent::ToolTip )
224 QHelpEvent *helpEvent =
static_cast< QHelpEvent *
>( e );
226 double fontSize = 0.0;
230 fontSize = mFormat.
size();
234 fontSize = mFont.pointSizeF();
237 toolTip = QStringLiteral(
"<b>%1</b><br>%2<br>Size: %3" ).arg( text(), mMode ==
ModeTextRenderer ? mFormat.
font().family() : mFont.family() ).arg( fontSize );
238 QToolTip::showText( helpEvent->globalPos(), toolTip );
240 return QToolButton::event( e );
245 if ( e->button() == Qt::RightButton )
247 QToolButton::showMenu();
250 else if ( e->button() == Qt::LeftButton )
252 mDragStartPosition = e->pos();
254 QToolButton::mousePressEvent( e );
261 if ( !( e->buttons() & Qt::LeftButton ) )
264 QToolButton::mouseMoveEvent( e );
268 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
271 QToolButton::mouseMoveEvent( e );
276 QDrag *drag =
new QDrag(
this );
288 drag->setPixmap( createDragIcon( QSize( iconSize, iconSize ) ) );
289 drag->exec( Qt::CopyAction );
293bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
298 if ( mimeColor.isValid() )
300 resultColor = mimeColor;
314 bool hasAlpha =
false;
316 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
318 e->acceptProposedAction();
319 updatePreview( QColor(), &format );
321 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
323 e->acceptProposedAction();
324 updatePreview( QColor(),
nullptr, &font );
326 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
331 e->acceptProposedAction();
332 updatePreview( mimeColor );
349 bool hasAlpha =
false;
350 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
356 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
362 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
365 e->acceptProposedAction();
371 mimeColor.setAlphaF( 1.0 );
386 size = mFormat.
size();
390 size = mFont.pointSizeF();
394 const double increment = (
event->modifiers() & Qt::ControlModifier ) ? 0.1 : 1;
395 if (
event->angleDelta().y() > 0 )
403 size = std::max( size, 1.0 );
417 QFont newFont = mFont;
418 newFont.setPointSizeF( size );
427QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const
431 QPixmap pixmap( iconSize, iconSize );
432 pixmap.fill( Qt::transparent );
438 p.setBrush( QBrush( color ) );
441 p.setPen( QColor( 197, 197, 197 ) );
442 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
447QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const
450 tempFormat = &mFormat;
455 QPixmap pixmap( size.width(), size.height() );
456 pixmap.fill( Qt::transparent );
459 p.setRenderHint( QPainter::Antialiasing );
460 const QRect rect( 0, 0, size.width(), size.height() );
462 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
464 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
465 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
469 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
470 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
473 p.setBrush( Qt::NoBrush );
474 p.setPen( Qt::NoPen );
494 ? fontSize * tempFormat->
buffer().
size() / 100
502 ? fontSize * tempFormat->
buffer().
size() / 100
503 : context.convertToPainterUnits( tempFormat->buffer().size(), tempFormat->buffer().sizeUnit(), tempFormat->buffer().sizeMapUnitScale() ) );
507 QRectF textRect = rect;
508 textRect.setLeft( xtrans );
509 textRect.setWidth( textRect.width() - xtrans );
510 textRect.setTop( ytrans );
511 if ( textRect.height() > 300 )
512 textRect.setHeight( 300 );
513 if ( textRect.width() > 2000 )
514 textRect.setWidth( 2000 );
517 context, *tempFormat );
522 p.setBrush( Qt::NoBrush );
523 p.setPen( QColor( 0, 0, 0 ) );
524 p.setFont( *tempFont );
525 QRectF textRect = rect;
526 textRect.setLeft( 2 );
527 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
536void QgsFontButton::prepareMenu()
546 mNullFormatAction =
new QAction( mNullFormatString,
this );
547 mMenu->addAction( mNullFormatAction );
551 mNullFormatAction->setCheckable(
true );
552 mNullFormatAction->setChecked(
true );
556 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
557 QWidget *sizeWidget =
new QWidget();
558 QVBoxLayout *sizeLayout =
new QVBoxLayout();
559 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
560 sizeLayout->setSpacing( 2 );
562 QString fontHeaderLabel;
570 fontHeaderLabel = tr(
"Font size (pt)" );
575 sizeLayout->addWidget( sizeLabel );
578 sizeSpin->setDecimals( 4 );
579 sizeSpin->setMaximum( 1e+9 );
582 connect( sizeSpin,
static_cast < void (
QgsDoubleSpinBox::* )(
double )
> ( &QgsDoubleSpinBox::valueChanged ),
583 this, [ = ](
double value )
588 if ( mNullFormatAction )
589 mNullFormatAction->setChecked(
false );
593 mFont.setPointSizeF( value );
599 QHBoxLayout *spinLayout =
new QHBoxLayout();
600 spinLayout->setContentsMargins( 4, 0, 4, 0 );
601 spinLayout->addWidget( sizeSpin );
602 sizeLayout->addLayout( spinLayout );
603 sizeWidget->setLayout( sizeLayout );
604 sizeAction->setDefaultWidget( sizeWidget );
605 sizeWidget->setFocusProxy( sizeSpin );
606 sizeWidget->setFocusPolicy( Qt::StrongFocus );
607 mMenu->addAction( sizeAction );
609 QMenu *recentFontMenu =
new QMenu( tr(
"Recent Fonts" ), mMenu );
611 for (
const QString &family : recentFontFamilies )
613 QAction *fontAction =
new QAction( family, recentFontMenu );
614 QFont f = fontAction->font();
616 fontAction->setFont( f );
617 fontAction->setToolTip( family );
618 recentFontMenu->addAction( fontAction );
620 || ( mMode ==
ModeQFont && family == mFont.family() ) )
622 fontAction->setCheckable(
true );
623 fontAction->setChecked(
true );
625 auto setFont = [
this, family]
632 QFont f = newFormat.
font();
649 connect( fontAction, &QAction::triggered,
this, setFont );
651 mMenu->addMenu( recentFontMenu );
653 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
654 mMenu->addAction( configureAction );
655 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
657 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
658 mMenu->addAction( copyFormatAction );
660 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
666 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
670 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ), &tempFormat ) );
672 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
674 tempFont.setPointSize( 8 );
675 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ),
nullptr, &tempFont ) );
679 pasteFormatAction->setEnabled(
false );
681 mMenu->addAction( pasteFormatAction );
686 mMenu->addSeparator();
693 mMenu->addAction( colorAction );
696 QColor alphaColor = mFormat.
color();
697 alphaColor.setAlphaF( mFormat.
opacity() );
703 const double opacity = color.alphaF();
706 if ( mNullFormatAction )
707 mNullFormatAction->setChecked(
false );
712 mMenu->addAction( alphaAction );
716 QList< QgsColorScheme * >::iterator it = schemeList.begin();
717 for ( ; it != schemeList.end(); ++it )
721 mMenu->addAction( colorAction );
726 mMenu->addSeparator();
728 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
729 mMenu->addAction( copyColorAction );
732 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
736 bool hasAlpha =
false;
737 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
739 pasteColorAction->setIcon( createColorIcon( clipColor ) );
743 pasteColorAction->setEnabled(
false );
745 mMenu->addAction( pasteColorAction );
750void QgsFontButton::addRecentColor(
const QColor &color )
772 mExpressionContextGenerator = generator;
793bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const
800bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const
809 if ( e->type() == QEvent::EnabledChange )
813 QToolButton::changeEvent( e );
819 QToolButton::showEvent( e );
824 QToolButton::resizeEvent(
event );
830void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
832 if ( mShowNoFormat && !mFormat.
isValid() )
834 setIcon( QPixmap() );
842 tempFormat = *format;
844 tempFormat = mFormat;
850 if ( color.isValid() )
857 tempFormat.
setSize( QToolButton::font().pointSizeF() - 2 );
860 QSize currentIconSize;
864 if ( !mIconSize.isValid() )
867 QStyleOptionToolButton opt;
868 initStyleOption( &opt );
869 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
873 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
874#elif defined(Q_OS_MAC)
875 mIconSize = QSize( buttonSize.width() - 10, height() - 2 );
877 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
880 currentIconSize = mIconSize;
886 currentIconSize = QSize( width() - 10, height() - 6 );
888 currentIconSize = QSize( width() - 10, height() - 12 );
892 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
898 QPixmap pixmap( currentIconSize * devicePixelRatioF() );
899 pixmap.fill( Qt::transparent );
900 pixmap.setDevicePixelRatio( devicePixelRatioF() );
903 p.setRenderHint( QPainter::Antialiasing );
904 const QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
935 : context.convertToPainterUnits( tempFormat.buffer().size(), tempFormat.buffer().sizeUnit(), tempFormat.buffer().sizeMapUnitScale() ) );
939 QRectF textRect = rect;
940 textRect.setLeft( xtrans );
941 textRect.setWidth( textRect.width() - xtrans );
942 textRect.setTop( ytrans );
943 if ( textRect.height() > 300 )
944 textRect.setHeight( 300 );
945 if ( textRect.width() > 2000 )
946 textRect.setWidth( 2000 );
949 context, tempFormat );
954 p.setBrush( Qt::NoBrush );
955 p.setPen( QColor( 0, 0, 0 ) );
956 p.setFont( tempFont );
957 QRectF textRect = rect;
958 textRect.setLeft( 2 );
959 p.drawText( textRect, Qt::AlignVCenter, text() );
965 setIconSize( currentIconSize );
978 bool hasAlpha =
false;
979 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
989 mDialogTitle = title;
@ 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.
A color swatch grid which can be embedded into a 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
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
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 QList< 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 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.
Perform transforms between map coordinates and device coordinates.
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.
Contains information about the context of a rendering operation.
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.
void setUseAdvancedEffects(bool enabled)
Used to enable or disable advanced effects such as blend modes.
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 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.
double screenDpi() const
Returns the current screen DPI for the screen that the parent widget appears on.
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 QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
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.
A simple dialog for customizing text formatting settings.
A panel widget for customizing text formatting settings.
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.
void setOpacity(double opacity)
Sets the text's opacity.
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 opacity() const
Returns the text's opacity.
double size() const
Returns the size for rendered text.
QMimeData * toMimeData() const
Returns new mime data representing the text format settings.
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 data sets.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
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.