31 #include <QDesktopWidget> 35 : QToolButton( parent )
36 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Text Format" ) : dialogTitle )
39 setText( tr(
"Font" ) );
41 setAcceptDrops(
true );
42 connect(
this, &QAbstractButton::clicked,
this, &QgsFontButton::showSettingsDialog );
45 mMenu =
new QMenu(
this );
46 connect( mMenu, &QMenu::aboutToShow,
this, &QgsFontButton::prepareMenu );
48 setPopupMode( QToolButton::MenuButtonPopup );
51 QSize size = QToolButton::minimumSizeHint();
53 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
66 void QgsFontButton::showSettingsDialog()
84 dialog.setWindowTitle( mDialogTitle );
130 QColor opaque = color;
131 opaque.setAlphaF( 1.0 );
133 if ( mFormat.
color() != opaque )
146 QApplication::clipboard()->setMimeData( mFormat.
toMimeData() );
159 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
164 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), font ) )
173 if ( e->type() == QEvent::ToolTip )
175 QHelpEvent *helpEvent =
static_cast< QHelpEvent *
>( e );
177 double fontSize = 0.0;
181 fontSize = mFormat.
size();
185 fontSize = mFont.pointSizeF();
188 toolTip = QStringLiteral(
"<b>%1</b><br>%2<br>Size: %3" ).arg( text(), mFormat.
font().family() ).arg( fontSize );
189 QToolTip::showText( helpEvent->globalPos(), toolTip );
191 return QToolButton::event( e );
196 if ( e->button() == Qt::RightButton )
198 QToolButton::showMenu();
201 else if ( e->button() == Qt::LeftButton )
203 mDragStartPosition = e->pos();
205 QToolButton::mousePressEvent( e );
212 if ( !( e->buttons() & Qt::LeftButton ) )
215 QToolButton::mouseMoveEvent( e );
219 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
222 QToolButton::mouseMoveEvent( e );
227 QDrag *drag =
new QDrag(
this );
238 drag->setPixmap( createDragIcon() );
239 drag->exec( Qt::CopyAction );
243 bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
248 if ( mimeColor.isValid() )
250 resultColor = mimeColor;
264 bool hasAlpha =
false;
266 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
268 e->acceptProposedAction();
269 updatePreview( QColor(), &format );
271 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
273 e->acceptProposedAction();
274 updatePreview( QColor(),
nullptr, &font );
276 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
281 e->acceptProposedAction();
282 updatePreview( mimeColor );
299 bool hasAlpha =
false;
300 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
306 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
312 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
315 e->acceptProposedAction();
321 mimeColor.setAlphaF( 1.0 );
336 size = mFormat.
size();
340 size = mFont.pointSizeF();
344 double increment =
event->modifiers() & Qt::ControlModifier ? 0.1 : 1;
345 if ( event->delta() > 0 )
353 size = std::max( size, 1.0 );
367 QFont newFont = mFont;
368 newFont.setPointSizeF( size );
377 QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const 380 QPixmap pixmap( 16, 16 );
381 pixmap.fill( Qt::transparent );
387 p.setBrush( QBrush( color ) );
390 p.setPen( QColor( 197, 197, 197 ) );
391 p.drawRect( 0, 0, 15, 15 );
396 QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const 399 tempFormat = &mFormat;
404 QPixmap pixmap( size.width(), size.height() );
405 pixmap.fill( Qt::transparent );
408 p.setRenderHint( QPainter::Antialiasing );
409 QRect rect( 0, 0, size.width(), size.height() );
411 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
413 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
414 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
418 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
419 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
422 p.setBrush( Qt::NoBrush );
423 p.setPen( Qt::NoPen );
434 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
451 QRectF textRect = rect;
452 textRect.setLeft( xtrans );
453 textRect.setWidth( textRect.width() - xtrans );
454 textRect.setTop( ytrans );
455 if ( textRect.height() > 300 )
456 textRect.setHeight( 300 );
457 if ( textRect.width() > 2000 )
458 textRect.setWidth( 2000 );
461 context, *tempFormat );
466 p.setBrush( Qt::NoBrush );
467 p.setPen( QColor( 0, 0, 0 ) );
468 p.setFont( *tempFont );
469 QRectF textRect = rect;
470 textRect.setLeft( 2 );
471 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
480 void QgsFontButton::prepareMenu()
489 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
490 QWidget *sizeWidget =
new QWidget();
491 QVBoxLayout *sizeLayout =
new QVBoxLayout();
492 sizeLayout->setMargin( 0 );
493 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
494 sizeLayout->setSpacing( 2 );
496 QString fontHeaderLabel;
504 fontHeaderLabel = tr(
"Font size (pt)" );
509 sizeLayout->addWidget( sizeLabel );
512 sizeSpin->setDecimals( 4 );
513 sizeSpin->setMaximum( 1e+9 );
516 connect( sizeSpin, static_cast <
void (
QgsDoubleSpinBox::* )(
double ) > ( &QgsDoubleSpinBox::valueChanged ),
517 this, [ = ](
double value )
525 mFont.setPointSizeF( value );
531 QHBoxLayout *spinLayout =
new QHBoxLayout();
532 spinLayout->setMargin( 0 );
533 spinLayout->setContentsMargins( 4, 0, 4, 0 );
534 spinLayout->addWidget( sizeSpin );
535 sizeLayout->addLayout( spinLayout );
536 sizeWidget->setLayout( sizeLayout );
537 sizeAction->setDefaultWidget( sizeWidget );
538 sizeWidget->setFocusProxy( sizeSpin );
539 sizeWidget->setFocusPolicy( Qt::StrongFocus );
540 mMenu->addAction( sizeAction );
542 QMenu *recentFontMenu =
new QMenu( tr(
"Recent fonts" ), mMenu );
545 QAction *fontAction =
new QAction( family, recentFontMenu );
546 QFont
f = fontAction->font();
547 f.setFamily( family );
548 fontAction->setFont( f );
549 fontAction->setToolTip( family );
550 recentFontMenu->addAction( fontAction );
552 || ( mMode ==
ModeQFont && family == mFont.family() ) )
554 fontAction->setCheckable(
true );
555 fontAction->setChecked(
true );
557 auto setFont = [
this, family]
564 QFont f = newFormat.
font();
565 f.setFamily( family );
574 font.setFamily( family );
581 connect( fontAction, &QAction::triggered,
this, setFont );
583 mMenu->addMenu( recentFontMenu );
585 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
586 mMenu->addAction( configureAction );
587 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
589 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
590 mMenu->addAction( copyFormatAction );
592 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
597 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
601 pasteFormatAction->setIcon( createDragIcon( QSize( 16, 16 ), &tempFormat ) );
603 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
605 tempFont.setPointSize( 8 );
606 pasteFormatAction->setIcon( createDragIcon( QSize( 16, 16 ),
nullptr, &tempFont ) );
610 pasteFormatAction->setEnabled(
false );
612 mMenu->addAction( pasteFormatAction );
617 mMenu->addSeparator();
624 mMenu->addAction( colorAction );
627 QColor alphaColor = mFormat.
color();
628 alphaColor.setAlphaF( mFormat.
opacity() );
634 double opacity = color.alphaF();
641 mMenu->addAction( alphaAction );
645 QList< QgsColorScheme * >::iterator it = schemeList.begin();
646 for ( ; it != schemeList.end(); ++it )
650 mMenu->addAction( colorAction );
655 mMenu->addSeparator();
657 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
658 mMenu->addAction( copyColorAction );
661 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
665 bool hasAlpha =
false;
666 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
668 pasteColorAction->setIcon( createColorIcon( clipColor ) );
672 pasteColorAction->setEnabled(
false );
674 mMenu->addAction( pasteColorAction );
679 void QgsFontButton::addRecentColor(
const QColor &color )
707 bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const 714 bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const 723 if ( e->type() == QEvent::EnabledChange )
727 QToolButton::changeEvent( e );
733 QToolButton::showEvent( e );
738 QToolButton::resizeEvent( event );
744 void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
750 tempFormat = *format;
752 tempFormat = mFormat;
758 if ( color.isValid() )
761 QSize currentIconSize;
765 if ( !mIconSize.isValid() )
768 QStyleOptionToolButton opt;
769 initStyleOption( &opt );
770 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
774 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
776 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
779 currentIconSize = mIconSize;
785 currentIconSize = QSize( width() - 10, height() - 6 );
787 currentIconSize = QSize( width() - 10, height() - 12 );
791 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
797 QPixmap pixmap( currentIconSize );
798 pixmap.fill( Qt::transparent );
801 p.setRenderHint( QPainter::Antialiasing );
802 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
813 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
830 QRectF textRect = rect;
831 textRect.setLeft( xtrans );
832 textRect.setWidth( textRect.width() - xtrans );
833 textRect.setTop( ytrans );
834 if ( textRect.height() > 300 )
835 textRect.setHeight( 300 );
836 if ( textRect.width() > 2000 )
837 textRect.setWidth( 2000 );
840 context, tempFormat );
845 p.setBrush( Qt::NoBrush );
846 p.setPen( QColor( 0, 0, 0 ) );
847 p.setFont( tempFont );
848 QRectF textRect = rect;
849 textRect.setLeft( 2 );
850 p.drawText( textRect, Qt::AlignVCenter, text() );
856 setIconSize( currentIconSize );
869 bool hasAlpha =
false;
870 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
880 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.
void setFont(const QFont &font)
Sets the font used for rendering text.
QColor color() const
Returns the color that text will be rendered in.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
QgsTextFormat format() const
Returns the current formatting settings defined by the widget.
Show scheme in color button drop-down menu.
Map canvas is a class for displaying all GIS data types on a canvas.
void setShowClearButton(const bool showClearButton)
Sets whether the widget will show a clear button.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
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.
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.
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.
QMimeData * toMimeData() const
Returns new mime data representing the text format settings.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
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)
A panel widget for customising 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 setColor(const QColor &color, const bool emitSignals=false) override
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 customising text formatting settings.
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.