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();
57 mSizeHint = QSize( std::max( minWidth, size.width() ), std::max( size.height(), fontHeight ) );
70 void QgsFontButton::showSettingsDialog()
77 if ( mExpressionContextGenerator )
101 QgsTextFormatDialog dialog( mFormat, mMapCanvas,
this, QgsGuiUtils::ModalDialogFlags, mLayer.data() );
102 dialog.setWindowTitle( mDialogTitle );
159 QColor opaque = color;
160 opaque.setAlphaF( 1.0 );
162 if ( mFormat.
color() != opaque )
175 QApplication::clipboard()->setMimeData( mFormat.
toMimeData() );
188 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
193 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), font ) )
202 if ( e->type() == QEvent::ToolTip )
204 QHelpEvent *helpEvent =
static_cast< QHelpEvent *
>( e );
206 double fontSize = 0.0;
210 fontSize = mFormat.
size();
214 fontSize = mFont.pointSizeF();
217 toolTip = QStringLiteral(
"<b>%1</b><br>%2<br>Size: %3" ).arg( text(), mMode ==
ModeTextRenderer ? mFormat.
font().family() : mFont.family() ).arg( fontSize );
218 QToolTip::showText( helpEvent->globalPos(), toolTip );
220 return QToolButton::event( e );
225 if ( e->button() == Qt::RightButton )
227 QToolButton::showMenu();
230 else if ( e->button() == Qt::LeftButton )
232 mDragStartPosition = e->pos();
234 QToolButton::mousePressEvent( e );
241 if ( !( e->buttons() & Qt::LeftButton ) )
244 QToolButton::mouseMoveEvent( e );
248 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
251 QToolButton::mouseMoveEvent( e );
256 QDrag *drag =
new QDrag(
this );
268 drag->setPixmap( createDragIcon( QSize( iconSize, iconSize ) ) );
269 drag->exec( Qt::CopyAction );
273 bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
278 if ( mimeColor.isValid() )
280 resultColor = mimeColor;
294 bool hasAlpha =
false;
296 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
298 e->acceptProposedAction();
299 updatePreview( QColor(), &format );
301 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
303 e->acceptProposedAction();
304 updatePreview( QColor(),
nullptr, &font );
306 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
311 e->acceptProposedAction();
312 updatePreview( mimeColor );
329 bool hasAlpha =
false;
330 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
336 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
342 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
345 e->acceptProposedAction();
351 mimeColor.setAlphaF( 1.0 );
366 size = mFormat.
size();
370 size = mFont.pointSizeF();
374 double increment =
event->modifiers() & Qt::ControlModifier ? 0.1 : 1;
375 if ( event->delta() > 0 )
383 size = std::max( size, 1.0 );
397 QFont newFont = mFont;
398 newFont.setPointSizeF( size );
407 QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const 411 QPixmap pixmap( iconSize, iconSize );
412 pixmap.fill( Qt::transparent );
418 p.setBrush( QBrush( color ) );
421 p.setPen( QColor( 197, 197, 197 ) );
422 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
427 QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const 430 tempFormat = &mFormat;
435 QPixmap pixmap( size.width(), size.height() );
436 pixmap.fill( Qt::transparent );
439 p.setRenderHint( QPainter::Antialiasing );
440 QRect rect( 0, 0, size.width(), size.height() );
442 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
444 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
445 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
449 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
450 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
453 p.setBrush( Qt::NoBrush );
454 p.setPen( Qt::NoPen );
465 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
482 QRectF textRect = rect;
483 textRect.setLeft( xtrans );
484 textRect.setWidth( textRect.width() - xtrans );
485 textRect.setTop( ytrans );
486 if ( textRect.height() > 300 )
487 textRect.setHeight( 300 );
488 if ( textRect.width() > 2000 )
489 textRect.setWidth( 2000 );
492 context, *tempFormat );
497 p.setBrush( Qt::NoBrush );
498 p.setPen( QColor( 0, 0, 0 ) );
499 p.setFont( *tempFont );
500 QRectF textRect = rect;
501 textRect.setLeft( 2 );
502 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
511 void QgsFontButton::prepareMenu()
520 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
521 QWidget *sizeWidget =
new QWidget();
522 QVBoxLayout *sizeLayout =
new QVBoxLayout();
523 sizeLayout->setMargin( 0 );
524 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
525 sizeLayout->setSpacing( 2 );
527 QString fontHeaderLabel;
535 fontHeaderLabel = tr(
"Font size (pt)" );
540 sizeLayout->addWidget( sizeLabel );
543 sizeSpin->setDecimals( 4 );
544 sizeSpin->setMaximum( 1e+9 );
547 connect( sizeSpin, static_cast <
void (
QgsDoubleSpinBox::* )(
double ) > ( &QgsDoubleSpinBox::valueChanged ),
548 this, [ = ](
double value )
556 mFont.setPointSizeF( value );
562 QHBoxLayout *spinLayout =
new QHBoxLayout();
563 spinLayout->setMargin( 0 );
564 spinLayout->setContentsMargins( 4, 0, 4, 0 );
565 spinLayout->addWidget( sizeSpin );
566 sizeLayout->addLayout( spinLayout );
567 sizeWidget->setLayout( sizeLayout );
568 sizeAction->setDefaultWidget( sizeWidget );
569 sizeWidget->setFocusProxy( sizeSpin );
570 sizeWidget->setFocusPolicy( Qt::StrongFocus );
571 mMenu->addAction( sizeAction );
573 QMenu *recentFontMenu =
new QMenu( tr(
"Recent Fonts" ), mMenu );
575 for (
const QString &family : recentFontFamilies )
577 QAction *fontAction =
new QAction( family, recentFontMenu );
578 QFont f = fontAction->font();
579 f.setFamily( family );
580 fontAction->setFont( f );
581 fontAction->setToolTip( family );
582 recentFontMenu->addAction( fontAction );
584 || ( mMode ==
ModeQFont && family == mFont.family() ) )
586 fontAction->setCheckable(
true );
587 fontAction->setChecked(
true );
589 auto setFont = [
this, family]
596 QFont f = newFormat.
font();
597 f.setFamily( family );
606 font.setFamily( family );
613 connect( fontAction, &QAction::triggered,
this, setFont );
615 mMenu->addMenu( recentFontMenu );
617 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
618 mMenu->addAction( configureAction );
619 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
621 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
622 mMenu->addAction( copyFormatAction );
624 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
630 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
634 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ), &tempFormat ) );
636 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
638 tempFont.setPointSize( 8 );
639 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ),
nullptr, &tempFont ) );
643 pasteFormatAction->setEnabled(
false );
645 mMenu->addAction( pasteFormatAction );
650 mMenu->addSeparator();
657 mMenu->addAction( colorAction );
660 QColor alphaColor = mFormat.
color();
661 alphaColor.setAlphaF( mFormat.
opacity() );
667 double opacity = color.alphaF();
674 mMenu->addAction( alphaAction );
678 QList< QgsColorScheme * >::iterator it = schemeList.begin();
679 for ( ; it != schemeList.end(); ++it )
683 mMenu->addAction( colorAction );
688 mMenu->addSeparator();
690 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
691 mMenu->addAction( copyColorAction );
694 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
698 bool hasAlpha =
false;
699 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
701 pasteColorAction->setIcon( createColorIcon( clipColor ) );
705 pasteColorAction->setEnabled(
false );
707 mMenu->addAction( pasteColorAction );
712 void QgsFontButton::addRecentColor(
const QColor &color )
734 mExpressionContextGenerator = generator;
755 bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const 762 bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const 771 if ( e->type() == QEvent::EnabledChange )
775 QToolButton::changeEvent( e );
781 QToolButton::showEvent( e );
786 QToolButton::resizeEvent( event );
792 void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
798 tempFormat = *format;
800 tempFormat = mFormat;
806 if ( color.isValid() )
809 QSize currentIconSize;
813 if ( !mIconSize.isValid() )
816 QStyleOptionToolButton opt;
817 initStyleOption( &opt );
818 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
822 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
823 #elif defined(Q_OS_MAC) 824 mIconSize = QSize( buttonSize.width() - 10, height() - 2 );
826 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
829 currentIconSize = mIconSize;
835 currentIconSize = QSize( width() - 10, height() - 6 );
837 currentIconSize = QSize( width() - 10, height() - 12 );
841 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
847 QPixmap pixmap( currentIconSize );
848 pixmap.fill( Qt::transparent );
851 p.setRenderHint( QPainter::Antialiasing );
852 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
863 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
880 QRectF textRect = rect;
881 textRect.setLeft( xtrans );
882 textRect.setWidth( textRect.width() - xtrans );
883 textRect.setTop( ytrans );
884 if ( textRect.height() > 300 )
885 textRect.setHeight( 300 );
886 if ( textRect.width() > 2000 )
887 textRect.setWidth( 2000 );
890 context, tempFormat );
895 p.setBrush( Qt::NoBrush );
896 p.setPen( QColor( 0, 0, 0 ) );
897 p.setFont( tempFont );
898 QRectF textRect = rect;
899 textRect.setLeft( 2 );
900 p.drawText( textRect, Qt::AlignVCenter, text() );
906 setIconSize( currentIconSize );
919 bool hasAlpha =
false;
920 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
930 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.