34 #include <QDesktopWidget> 38 : QToolButton( parent )
39 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Text Format" ) : dialogTitle )
42 setText( tr(
"Font" ) );
44 setAcceptDrops(
true );
45 connect(
this, &QAbstractButton::clicked,
this, &QgsFontButton::showSettingsDialog );
48 mMenu =
new QMenu(
this );
49 connect( mMenu, &QMenu::aboutToShow,
this, &QgsFontButton::prepareMenu );
51 setPopupMode( QToolButton::MenuButtonPopup );
54 QSize size = QToolButton::minimumSizeHint();
56 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) 61 mSizeHint = QSize( std::max( minWidth, size.width() ), std::max( size.height(), fontHeight ) );
74 void QgsFontButton::showSettingsDialog()
81 if ( mExpressionContextGenerator )
105 QgsTextFormatDialog dialog( mFormat, mMapCanvas,
this, QgsGuiUtils::ModalDialogFlags, mLayer.data() );
106 dialog.setWindowTitle( mDialogTitle );
163 QColor opaque = color;
164 opaque.setAlphaF( 1.0 );
166 if ( mFormat.
color() != opaque )
179 QApplication::clipboard()->setMimeData( mFormat.
toMimeData() );
192 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
197 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), font ) )
206 if ( e->type() == QEvent::ToolTip )
208 QHelpEvent *helpEvent =
static_cast< QHelpEvent *
>( e );
210 double fontSize = 0.0;
214 fontSize = mFormat.
size();
218 fontSize = mFont.pointSizeF();
221 toolTip = QStringLiteral(
"<b>%1</b><br>%2<br>Size: %3" ).arg( text(), mMode ==
ModeTextRenderer ? mFormat.
font().family() : mFont.family() ).arg( fontSize );
222 QToolTip::showText( helpEvent->globalPos(), toolTip );
224 return QToolButton::event( e );
229 if ( e->button() == Qt::RightButton )
231 QToolButton::showMenu();
234 else if ( e->button() == Qt::LeftButton )
236 mDragStartPosition = e->pos();
238 QToolButton::mousePressEvent( e );
245 if ( !( e->buttons() & Qt::LeftButton ) )
248 QToolButton::mouseMoveEvent( e );
252 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
255 QToolButton::mouseMoveEvent( e );
260 QDrag *drag =
new QDrag(
this );
272 drag->setPixmap( createDragIcon( QSize( iconSize, iconSize ) ) );
273 drag->exec( Qt::CopyAction );
277 bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
282 if ( mimeColor.isValid() )
284 resultColor = mimeColor;
298 bool hasAlpha =
false;
300 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
302 e->acceptProposedAction();
303 updatePreview( QColor(), &format );
305 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
307 e->acceptProposedAction();
308 updatePreview( QColor(),
nullptr, &font );
310 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
315 e->acceptProposedAction();
316 updatePreview( mimeColor );
333 bool hasAlpha =
false;
334 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
340 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
346 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
349 e->acceptProposedAction();
355 mimeColor.setAlphaF( 1.0 );
370 size = mFormat.
size();
374 size = mFont.pointSizeF();
378 double increment =
event->modifiers() & Qt::ControlModifier ? 0.1 : 1;
379 if ( event->delta() > 0 )
387 size = std::max( size, 1.0 );
401 QFont newFont = mFont;
402 newFont.setPointSizeF( size );
411 QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const 415 QPixmap pixmap( iconSize, iconSize );
416 pixmap.fill( Qt::transparent );
422 p.setBrush( QBrush( color ) );
425 p.setPen( QColor( 197, 197, 197 ) );
426 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
431 QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const 434 tempFormat = &mFormat;
439 QPixmap pixmap( size.width(), size.height() );
440 pixmap.fill( Qt::transparent );
443 p.setRenderHint( QPainter::Antialiasing );
444 QRect rect( 0, 0, size.width(), size.height() );
446 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
448 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
449 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
453 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
454 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
457 p.setBrush( Qt::NoBrush );
458 p.setPen( Qt::NoPen );
469 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
486 QRectF textRect = rect;
487 textRect.setLeft( xtrans );
488 textRect.setWidth( textRect.width() - xtrans );
489 textRect.setTop( ytrans );
490 if ( textRect.height() > 300 )
491 textRect.setHeight( 300 );
492 if ( textRect.width() > 2000 )
493 textRect.setWidth( 2000 );
496 context, *tempFormat );
501 p.setBrush( Qt::NoBrush );
502 p.setPen( QColor( 0, 0, 0 ) );
503 p.setFont( *tempFont );
504 QRectF textRect = rect;
505 textRect.setLeft( 2 );
506 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
515 void QgsFontButton::prepareMenu()
524 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
525 QWidget *sizeWidget =
new QWidget();
526 QVBoxLayout *sizeLayout =
new QVBoxLayout();
527 sizeLayout->setMargin( 0 );
528 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
529 sizeLayout->setSpacing( 2 );
531 QString fontHeaderLabel;
539 fontHeaderLabel = tr(
"Font size (pt)" );
544 sizeLayout->addWidget( sizeLabel );
547 sizeSpin->setDecimals( 4 );
548 sizeSpin->setMaximum( 1e+9 );
551 connect( sizeSpin, static_cast <
void (
QgsDoubleSpinBox::* )(
double ) > ( &QgsDoubleSpinBox::valueChanged ),
552 this, [ = ](
double value )
560 mFont.setPointSizeF( value );
566 QHBoxLayout *spinLayout =
new QHBoxLayout();
567 spinLayout->setMargin( 0 );
568 spinLayout->setContentsMargins( 4, 0, 4, 0 );
569 spinLayout->addWidget( sizeSpin );
570 sizeLayout->addLayout( spinLayout );
571 sizeWidget->setLayout( sizeLayout );
572 sizeAction->setDefaultWidget( sizeWidget );
573 sizeWidget->setFocusProxy( sizeSpin );
574 sizeWidget->setFocusPolicy( Qt::StrongFocus );
575 mMenu->addAction( sizeAction );
577 QMenu *recentFontMenu =
new QMenu( tr(
"Recent Fonts" ), mMenu );
579 for (
const QString &family : recentFontFamilies )
581 QAction *fontAction =
new QAction( family, recentFontMenu );
582 QFont f = fontAction->font();
583 f.setFamily( family );
584 fontAction->setFont( f );
585 fontAction->setToolTip( family );
586 recentFontMenu->addAction( fontAction );
588 || ( mMode ==
ModeQFont && family == mFont.family() ) )
590 fontAction->setCheckable(
true );
591 fontAction->setChecked(
true );
593 auto setFont = [
this, family]
600 QFont f = newFormat.
font();
601 f.setFamily( family );
610 font.setFamily( family );
617 connect( fontAction, &QAction::triggered,
this, setFont );
619 mMenu->addMenu( recentFontMenu );
621 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
622 mMenu->addAction( configureAction );
623 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
625 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
626 mMenu->addAction( copyFormatAction );
628 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
634 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
638 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ), &tempFormat ) );
640 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
642 tempFont.setPointSize( 8 );
643 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ),
nullptr, &tempFont ) );
647 pasteFormatAction->setEnabled(
false );
649 mMenu->addAction( pasteFormatAction );
654 mMenu->addSeparator();
661 mMenu->addAction( colorAction );
664 QColor alphaColor = mFormat.
color();
665 alphaColor.setAlphaF( mFormat.
opacity() );
671 double opacity = color.alphaF();
678 mMenu->addAction( alphaAction );
682 QList< QgsColorScheme * >::iterator it = schemeList.begin();
683 for ( ; it != schemeList.end(); ++it )
687 mMenu->addAction( colorAction );
692 mMenu->addSeparator();
694 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
695 mMenu->addAction( copyColorAction );
698 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
702 bool hasAlpha =
false;
703 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
705 pasteColorAction->setIcon( createColorIcon( clipColor ) );
709 pasteColorAction->setEnabled(
false );
711 mMenu->addAction( pasteColorAction );
716 void QgsFontButton::addRecentColor(
const QColor &color )
738 mExpressionContextGenerator = generator;
759 bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const 766 bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const 775 if ( e->type() == QEvent::EnabledChange )
779 QToolButton::changeEvent( e );
785 QToolButton::showEvent( e );
790 QToolButton::resizeEvent( event );
796 void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
802 tempFormat = *format;
804 tempFormat = mFormat;
810 if ( color.isValid() )
813 QSize currentIconSize;
817 if ( !mIconSize.isValid() )
820 QStyleOptionToolButton opt;
821 initStyleOption( &opt );
822 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
826 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
827 #elif defined(Q_OS_MAC) 828 mIconSize = QSize( buttonSize.width() - 10, height() - 2 );
830 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
833 currentIconSize = mIconSize;
839 currentIconSize = QSize( width() - 10, height() - 6 );
841 currentIconSize = QSize( width() - 10, height() - 12 );
845 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
851 QPixmap pixmap( currentIconSize );
852 pixmap.fill( Qt::transparent );
855 p.setRenderHint( QPainter::Antialiasing );
856 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
867 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
884 QRectF textRect = rect;
885 textRect.setLeft( xtrans );
886 textRect.setWidth( textRect.width() - xtrans );
887 textRect.setTop( ytrans );
888 if ( textRect.height() > 300 )
889 textRect.setHeight( 300 );
890 if ( textRect.width() > 2000 )
891 textRect.setWidth( 2000 );
894 context, tempFormat );
899 p.setBrush( Qt::NoBrush );
900 p.setPen( QColor( 0, 0, 0 ) );
901 p.setFont( tempFont );
902 QRectF textRect = rect;
903 textRect.setLeft( 2 );
904 p.drawText( textRect, Qt::AlignVCenter, text() );
910 setIconSize( currentIconSize );
923 bool hasAlpha =
false;
924 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
934 mDialogTitle = title;
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value...
A color swatch grid which can be embedded into a menu.
static QFont fromMimeData(const QMimeData *data, bool *ok=nullptr)
Attempts to parse the provided mime data as a QFont.
void setOpacity(double opacity)
Sets the text's opacity.
QSizeF size() const
Returns the size of the background shape.
double opacity() const
Returns the text's opacity.
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
static const double UI_SCALE_FACTOR
UI scaling factor.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
void setFont(const QFont &font)
Sets the font used for rendering text.
QColor color() const
Returns the color that text will be rendered in.
A bar for displaying non-blocking messages to the user.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
Show scheme in color button drop-down menu.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
Map canvas is a class for displaying all GIS data types on a canvas.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
static QgsTextFormat fromMimeData(const QMimeData *data, bool *ok=nullptr)
Attempts to parse the provided mime data as a QgsTextFormat.
Perform transforms between map coordinates and device coordinates.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setUseAdvancedEffects(bool enabled)
Used to enable or disable advanced effects such as blend modes.
static QStringList recentFontFamilies()
Returns a list of recently used font families.
void setSize(double size)
Sets the size for rendered text.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
void setColor(const QColor &color, bool emitSignals=false) override
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
QMimeData * toMimeData() const
Returns new mime data representing the text format settings.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the widget is shown, e.g., the associated map canvas and expression context...
Abstract interface for generating an expression context.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the widget is shown, e.g., the associated map canvas and expression context...
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true)
Draws text within a rectangle using the specified settings.
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Set parameters for use in transforming coordinates.
static void addRecentFontFamily(const QString &family)
Adds a font family to the list of recently used font families.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes. ...
QgsTextFormat format() const
Returns the current formatting settings defined by the widget.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
double size() const
Returns the size for rendered text.
bool enabled() const
Returns whether the background is enabled.
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
A panel widget for customizing text formatting settings.
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
bool enabled() const
Returns whether the buffer is enabled.
void appendScopes(const QList< QgsExpressionContextScope *> &scopes)
Appends a list of scopes to the end of the context.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
static QMimeData * toMimeData(const QFont &font)
Returns new mime data representing the specified font settings.
Container for all settings relating to text rendering.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
A simple dialog for customizing text formatting settings.
Represents a vector layer which manages a vector based data sets.
QFont font() const
Returns the font used for rendering text.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
QFont getFont(bool &ok, const QFont &initial, const QString &title)
Show font selection dialog.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.