32 #include <QDesktopWidget> 36 : QToolButton( parent )
37 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Text Format" ) : dialogTitle )
40 setText( tr(
"Font" ) );
42 setAcceptDrops(
true );
43 connect(
this, &QAbstractButton::clicked,
this, &QgsFontButton::showSettingsDialog );
46 mMenu =
new QMenu(
this );
47 connect( mMenu, &QMenu::aboutToShow,
this, &QgsFontButton::prepareMenu );
49 setPopupMode( QToolButton::MenuButtonPopup );
52 QSize size = QToolButton::minimumSizeHint();
55 mSizeHint = QSize( std::max( minWidth, size.width() ), std::max( size.height(), fontHeight ) );
68 void QgsFontButton::showSettingsDialog()
86 dialog.setWindowTitle( mDialogTitle );
132 QColor opaque = color;
133 opaque.setAlphaF( 1.0 );
135 if ( mFormat.
color() != opaque )
148 QApplication::clipboard()->setMimeData( mFormat.
toMimeData() );
161 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
166 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), font ) )
175 if ( e->type() == QEvent::ToolTip )
177 QHelpEvent *helpEvent =
static_cast< QHelpEvent *
>( e );
179 double fontSize = 0.0;
183 fontSize = mFormat.
size();
187 fontSize = mFont.pointSizeF();
190 toolTip = QStringLiteral(
"<b>%1</b><br>%2<br>Size: %3" ).arg( text(), mFormat.
font().family() ).arg( fontSize );
191 QToolTip::showText( helpEvent->globalPos(), toolTip );
193 return QToolButton::event( e );
198 if ( e->button() == Qt::RightButton )
200 QToolButton::showMenu();
203 else if ( e->button() == Qt::LeftButton )
205 mDragStartPosition = e->pos();
207 QToolButton::mousePressEvent( e );
214 if ( !( e->buttons() & Qt::LeftButton ) )
217 QToolButton::mouseMoveEvent( e );
221 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
224 QToolButton::mouseMoveEvent( e );
229 QDrag *drag =
new QDrag(
this );
241 drag->setPixmap( createDragIcon( QSize( iconSize, iconSize ) ) );
242 drag->exec( Qt::CopyAction );
246 bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
251 if ( mimeColor.isValid() )
253 resultColor = mimeColor;
267 bool hasAlpha =
false;
269 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
271 e->acceptProposedAction();
272 updatePreview( QColor(), &format );
274 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
276 e->acceptProposedAction();
277 updatePreview( QColor(),
nullptr, &font );
279 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
284 e->acceptProposedAction();
285 updatePreview( mimeColor );
302 bool hasAlpha =
false;
303 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
309 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
315 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
318 e->acceptProposedAction();
324 mimeColor.setAlphaF( 1.0 );
339 size = mFormat.
size();
343 size = mFont.pointSizeF();
347 double increment =
event->modifiers() & Qt::ControlModifier ? 0.1 : 1;
348 if ( event->delta() > 0 )
356 size = std::max( size, 1.0 );
370 QFont newFont = mFont;
371 newFont.setPointSizeF( size );
380 QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const 384 QPixmap pixmap( iconSize, iconSize );
385 pixmap.fill( Qt::transparent );
391 p.setBrush( QBrush( color ) );
394 p.setPen( QColor( 197, 197, 197 ) );
395 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
400 QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const 403 tempFormat = &mFormat;
408 QPixmap pixmap( size.width(), size.height() );
409 pixmap.fill( Qt::transparent );
412 p.setRenderHint( QPainter::Antialiasing );
413 QRect rect( 0, 0, size.width(), size.height() );
415 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
417 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
418 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
422 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
423 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
426 p.setBrush( Qt::NoBrush );
427 p.setPen( Qt::NoPen );
438 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
455 QRectF textRect = rect;
456 textRect.setLeft( xtrans );
457 textRect.setWidth( textRect.width() - xtrans );
458 textRect.setTop( ytrans );
459 if ( textRect.height() > 300 )
460 textRect.setHeight( 300 );
461 if ( textRect.width() > 2000 )
462 textRect.setWidth( 2000 );
465 context, *tempFormat );
470 p.setBrush( Qt::NoBrush );
471 p.setPen( QColor( 0, 0, 0 ) );
472 p.setFont( *tempFont );
473 QRectF textRect = rect;
474 textRect.setLeft( 2 );
475 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
484 void QgsFontButton::prepareMenu()
493 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
494 QWidget *sizeWidget =
new QWidget();
495 QVBoxLayout *sizeLayout =
new QVBoxLayout();
496 sizeLayout->setMargin( 0 );
497 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
498 sizeLayout->setSpacing( 2 );
500 QString fontHeaderLabel;
508 fontHeaderLabel = tr(
"Font size (pt)" );
513 sizeLayout->addWidget( sizeLabel );
516 sizeSpin->setDecimals( 4 );
517 sizeSpin->setMaximum( 1e+9 );
520 connect( sizeSpin, static_cast <
void (
QgsDoubleSpinBox::* )(
double ) > ( &QgsDoubleSpinBox::valueChanged ),
521 this, [ = ](
double value )
529 mFont.setPointSizeF( value );
535 QHBoxLayout *spinLayout =
new QHBoxLayout();
536 spinLayout->setMargin( 0 );
537 spinLayout->setContentsMargins( 4, 0, 4, 0 );
538 spinLayout->addWidget( sizeSpin );
539 sizeLayout->addLayout( spinLayout );
540 sizeWidget->setLayout( sizeLayout );
541 sizeAction->setDefaultWidget( sizeWidget );
542 sizeWidget->setFocusProxy( sizeSpin );
543 sizeWidget->setFocusPolicy( Qt::StrongFocus );
544 mMenu->addAction( sizeAction );
546 QMenu *recentFontMenu =
new QMenu( tr(
"Recent Fonts" ), mMenu );
548 for (
const QString &family : recentFontFamilies )
550 QAction *fontAction =
new QAction( family, recentFontMenu );
551 QFont f = fontAction->font();
552 f.setFamily( family );
553 fontAction->setFont( f );
554 fontAction->setToolTip( family );
555 recentFontMenu->addAction( fontAction );
557 || ( mMode ==
ModeQFont && family == mFont.family() ) )
559 fontAction->setCheckable(
true );
560 fontAction->setChecked(
true );
562 auto setFont = [
this, family]
569 QFont f = newFormat.
font();
570 f.setFamily( family );
579 font.setFamily( family );
586 connect( fontAction, &QAction::triggered,
this, setFont );
588 mMenu->addMenu( recentFontMenu );
590 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
591 mMenu->addAction( configureAction );
592 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
594 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
595 mMenu->addAction( copyFormatAction );
597 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
603 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
607 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ), &tempFormat ) );
609 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
611 tempFont.setPointSize( 8 );
612 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ),
nullptr, &tempFont ) );
616 pasteFormatAction->setEnabled(
false );
618 mMenu->addAction( pasteFormatAction );
623 mMenu->addSeparator();
630 mMenu->addAction( colorAction );
633 QColor alphaColor = mFormat.
color();
634 alphaColor.setAlphaF( mFormat.
opacity() );
640 double opacity = color.alphaF();
647 mMenu->addAction( alphaAction );
651 QList< QgsColorScheme * >::iterator it = schemeList.begin();
652 for ( ; it != schemeList.end(); ++it )
656 mMenu->addAction( colorAction );
661 mMenu->addSeparator();
663 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
664 mMenu->addAction( copyColorAction );
667 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
671 bool hasAlpha =
false;
672 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
674 pasteColorAction->setIcon( createColorIcon( clipColor ) );
678 pasteColorAction->setEnabled(
false );
680 mMenu->addAction( pasteColorAction );
685 void QgsFontButton::addRecentColor(
const QColor &color )
713 bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const 720 bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const 729 if ( e->type() == QEvent::EnabledChange )
733 QToolButton::changeEvent( e );
739 QToolButton::showEvent( e );
744 QToolButton::resizeEvent( event );
750 void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
756 tempFormat = *format;
758 tempFormat = mFormat;
764 if ( color.isValid() )
767 QSize currentIconSize;
771 if ( !mIconSize.isValid() )
774 QStyleOptionToolButton opt;
775 initStyleOption( &opt );
776 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
780 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
781 #elif defined(Q_OS_MAC) 782 mIconSize = QSize( buttonSize.width() - 10, height() - 2 );
784 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
787 currentIconSize = mIconSize;
793 currentIconSize = QSize( width() - 10, height() - 6 );
795 currentIconSize = QSize( width() - 10, height() - 12 );
799 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
805 QPixmap pixmap( currentIconSize );
806 pixmap.fill( Qt::transparent );
809 p.setRenderHint( QPainter::Antialiasing );
810 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
821 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
838 QRectF textRect = rect;
839 textRect.setLeft( xtrans );
840 textRect.setWidth( textRect.width() - xtrans );
841 textRect.setTop( ytrans );
842 if ( textRect.height() > 300 )
843 textRect.setHeight( 300 );
844 if ( textRect.width() > 2000 )
845 textRect.setWidth( 2000 );
848 context, tempFormat );
853 p.setBrush( Qt::NoBrush );
854 p.setPen( QColor( 0, 0, 0 ) );
855 p.setFont( tempFont );
856 QRectF textRect = rect;
857 textRect.setLeft( 2 );
858 p.drawText( textRect, Qt::AlignVCenter, text() );
864 setIconSize( currentIconSize );
877 bool hasAlpha =
false;
878 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
888 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.
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.
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.
void setColor(const QColor &color, bool emitSignals=false) override
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.
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.
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.
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.