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 );
549 QAction *fontAction =
new QAction( family, recentFontMenu );
550 QFont f = fontAction->font();
551 f.setFamily( family );
552 fontAction->setFont( f );
553 fontAction->setToolTip( family );
554 recentFontMenu->addAction( fontAction );
556 || ( mMode ==
ModeQFont && family == mFont.family() ) )
558 fontAction->setCheckable(
true );
559 fontAction->setChecked(
true );
561 auto setFont = [
this, family]
568 QFont f = newFormat.
font();
569 f.setFamily( family );
578 font.setFamily( family );
585 connect( fontAction, &QAction::triggered,
this, setFont );
587 mMenu->addMenu( recentFontMenu );
589 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
590 mMenu->addAction( configureAction );
591 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
593 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
594 mMenu->addAction( copyFormatAction );
596 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
602 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
606 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ), &tempFormat ) );
608 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
610 tempFont.setPointSize( 8 );
611 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ),
nullptr, &tempFont ) );
615 pasteFormatAction->setEnabled(
false );
617 mMenu->addAction( pasteFormatAction );
622 mMenu->addSeparator();
629 mMenu->addAction( colorAction );
632 QColor alphaColor = mFormat.
color();
633 alphaColor.setAlphaF( mFormat.
opacity() );
639 double opacity = color.alphaF();
646 mMenu->addAction( alphaAction );
650 QList< QgsColorScheme * >::iterator it = schemeList.begin();
651 for ( ; it != schemeList.end(); ++it )
655 mMenu->addAction( colorAction );
660 mMenu->addSeparator();
662 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
663 mMenu->addAction( copyColorAction );
666 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
670 bool hasAlpha =
false;
671 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
673 pasteColorAction->setIcon( createColorIcon( clipColor ) );
677 pasteColorAction->setEnabled(
false );
679 mMenu->addAction( pasteColorAction );
684 void QgsFontButton::addRecentColor(
const QColor &color )
712 bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const 719 bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const 728 if ( e->type() == QEvent::EnabledChange )
732 QToolButton::changeEvent( e );
738 QToolButton::showEvent( e );
743 QToolButton::resizeEvent( event );
749 void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
755 tempFormat = *format;
757 tempFormat = mFormat;
763 if ( color.isValid() )
766 QSize currentIconSize;
770 if ( !mIconSize.isValid() )
773 QStyleOptionToolButton opt;
774 initStyleOption( &opt );
775 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
779 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
780 #elif defined(Q_OS_MAC) 781 mIconSize = QSize( buttonSize.width() - 10, height() - 2 );
783 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
786 currentIconSize = mIconSize;
792 currentIconSize = QSize( width() - 10, height() - 6 );
794 currentIconSize = QSize( width() - 10, height() - 12 );
798 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
804 QPixmap pixmap( currentIconSize );
805 pixmap.fill( Qt::transparent );
808 p.setRenderHint( QPainter::Antialiasing );
809 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
820 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
837 QRectF textRect = rect;
838 textRect.setLeft( xtrans );
839 textRect.setWidth( textRect.width() - xtrans );
840 textRect.setTop( ytrans );
841 if ( textRect.height() > 300 )
842 textRect.setHeight( 300 );
843 if ( textRect.width() > 2000 )
844 textRect.setWidth( 2000 );
847 context, tempFormat );
852 p.setBrush( Qt::NoBrush );
853 p.setPen( QColor( 0, 0, 0 ) );
854 p.setFont( tempFont );
855 QRectF textRect = rect;
856 textRect.setLeft( 2 );
857 p.drawText( textRect, Qt::AlignVCenter, text() );
863 setIconSize( currentIconSize );
876 bool hasAlpha =
false;
877 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
887 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.
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 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.