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();
54 mSizeHint = QSize( std::max( minWidth, size.width() ), std::max( size.height(), fontHeight ) );
67 void QgsFontButton::showSettingsDialog()
85 dialog.setWindowTitle( mDialogTitle );
131 QColor opaque = color;
132 opaque.setAlphaF( 1.0 );
134 if ( mFormat.
color() != opaque )
147 QApplication::clipboard()->setMimeData( mFormat.
toMimeData() );
160 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
165 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), font ) )
174 if ( e->type() == QEvent::ToolTip )
176 QHelpEvent *helpEvent =
static_cast< QHelpEvent *
>( e );
178 double fontSize = 0.0;
182 fontSize = mFormat.
size();
186 fontSize = mFont.pointSizeF();
189 toolTip = QStringLiteral(
"<b>%1</b><br>%2<br>Size: %3" ).arg( text(), mFormat.
font().family() ).arg( fontSize );
190 QToolTip::showText( helpEvent->globalPos(), toolTip );
192 return QToolButton::event( e );
197 if ( e->button() == Qt::RightButton )
199 QToolButton::showMenu();
202 else if ( e->button() == Qt::LeftButton )
204 mDragStartPosition = e->pos();
206 QToolButton::mousePressEvent( e );
213 if ( !( e->buttons() & Qt::LeftButton ) )
216 QToolButton::mouseMoveEvent( e );
220 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
223 QToolButton::mouseMoveEvent( e );
228 QDrag *drag =
new QDrag(
this );
239 drag->setPixmap( createDragIcon() );
240 drag->exec( Qt::CopyAction );
244 bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
249 if ( mimeColor.isValid() )
251 resultColor = mimeColor;
265 bool hasAlpha =
false;
267 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
269 e->acceptProposedAction();
270 updatePreview( QColor(), &format );
272 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
274 e->acceptProposedAction();
275 updatePreview( QColor(),
nullptr, &font );
277 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
282 e->acceptProposedAction();
283 updatePreview( mimeColor );
300 bool hasAlpha =
false;
301 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
307 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
313 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
316 e->acceptProposedAction();
322 mimeColor.setAlphaF( 1.0 );
337 size = mFormat.
size();
341 size = mFont.pointSizeF();
345 double increment =
event->modifiers() & Qt::ControlModifier ? 0.1 : 1;
346 if ( event->delta() > 0 )
354 size = std::max( size, 1.0 );
368 QFont newFont = mFont;
369 newFont.setPointSizeF( size );
378 QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const 381 QPixmap pixmap( 16, 16 );
382 pixmap.fill( Qt::transparent );
388 p.setBrush( QBrush( color ) );
391 p.setPen( QColor( 197, 197, 197 ) );
392 p.drawRect( 0, 0, 15, 15 );
397 QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const 400 tempFormat = &mFormat;
405 QPixmap pixmap( size.width(), size.height() );
406 pixmap.fill( Qt::transparent );
409 p.setRenderHint( QPainter::Antialiasing );
410 QRect rect( 0, 0, size.width(), size.height() );
412 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
414 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
415 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
419 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
420 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
423 p.setBrush( Qt::NoBrush );
424 p.setPen( Qt::NoPen );
435 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
452 QRectF textRect = rect;
453 textRect.setLeft( xtrans );
454 textRect.setWidth( textRect.width() - xtrans );
455 textRect.setTop( ytrans );
456 if ( textRect.height() > 300 )
457 textRect.setHeight( 300 );
458 if ( textRect.width() > 2000 )
459 textRect.setWidth( 2000 );
462 context, *tempFormat );
467 p.setBrush( Qt::NoBrush );
468 p.setPen( QColor( 0, 0, 0 ) );
469 p.setFont( *tempFont );
470 QRectF textRect = rect;
471 textRect.setLeft( 2 );
472 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
481 void QgsFontButton::prepareMenu()
490 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
491 QWidget *sizeWidget =
new QWidget();
492 QVBoxLayout *sizeLayout =
new QVBoxLayout();
493 sizeLayout->setMargin( 0 );
494 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
495 sizeLayout->setSpacing( 2 );
497 QString fontHeaderLabel;
505 fontHeaderLabel = tr(
"Font size (pt)" );
510 sizeLayout->addWidget( sizeLabel );
513 sizeSpin->setDecimals( 4 );
514 sizeSpin->setMaximum( 1e+9 );
517 connect( sizeSpin, static_cast <
void (
QgsDoubleSpinBox::* )(
double ) > ( &QgsDoubleSpinBox::valueChanged ),
518 this, [ = ](
double value )
526 mFont.setPointSizeF( value );
532 QHBoxLayout *spinLayout =
new QHBoxLayout();
533 spinLayout->setMargin( 0 );
534 spinLayout->setContentsMargins( 4, 0, 4, 0 );
535 spinLayout->addWidget( sizeSpin );
536 sizeLayout->addLayout( spinLayout );
537 sizeWidget->setLayout( sizeLayout );
538 sizeAction->setDefaultWidget( sizeWidget );
539 sizeWidget->setFocusProxy( sizeSpin );
540 sizeWidget->setFocusPolicy( Qt::StrongFocus );
541 mMenu->addAction( sizeAction );
543 QMenu *recentFontMenu =
new QMenu( tr(
"Recent Fonts" ), mMenu );
546 QAction *fontAction =
new QAction( family, recentFontMenu );
547 QFont f = fontAction->font();
548 f.setFamily( family );
549 fontAction->setFont( f );
550 fontAction->setToolTip( family );
551 recentFontMenu->addAction( fontAction );
553 || ( mMode ==
ModeQFont && family == mFont.family() ) )
555 fontAction->setCheckable(
true );
556 fontAction->setChecked(
true );
558 auto setFont = [
this, family]
565 QFont f = newFormat.
font();
566 f.setFamily( family );
575 font.setFamily( family );
582 connect( fontAction, &QAction::triggered,
this, setFont );
584 mMenu->addMenu( recentFontMenu );
586 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
587 mMenu->addAction( configureAction );
588 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
590 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
591 mMenu->addAction( copyFormatAction );
593 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
598 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
602 pasteFormatAction->setIcon( createDragIcon( QSize( 16, 16 ), &tempFormat ) );
604 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
606 tempFont.setPointSize( 8 );
607 pasteFormatAction->setIcon( createDragIcon( QSize( 16, 16 ),
nullptr, &tempFont ) );
611 pasteFormatAction->setEnabled(
false );
613 mMenu->addAction( pasteFormatAction );
618 mMenu->addSeparator();
625 mMenu->addAction( colorAction );
628 QColor alphaColor = mFormat.
color();
629 alphaColor.setAlphaF( mFormat.
opacity() );
635 double opacity = color.alphaF();
642 mMenu->addAction( alphaAction );
646 QList< QgsColorScheme * >::iterator it = schemeList.begin();
647 for ( ; it != schemeList.end(); ++it )
651 mMenu->addAction( colorAction );
656 mMenu->addSeparator();
658 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
659 mMenu->addAction( copyColorAction );
662 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
666 bool hasAlpha =
false;
667 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
669 pasteColorAction->setIcon( createColorIcon( clipColor ) );
673 pasteColorAction->setEnabled(
false );
675 mMenu->addAction( pasteColorAction );
680 void QgsFontButton::addRecentColor(
const QColor &color )
708 bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const 715 bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const 724 if ( e->type() == QEvent::EnabledChange )
728 QToolButton::changeEvent( e );
734 QToolButton::showEvent( e );
739 QToolButton::resizeEvent( event );
745 void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
751 tempFormat = *format;
753 tempFormat = mFormat;
759 if ( color.isValid() )
762 QSize currentIconSize;
766 if ( !mIconSize.isValid() )
769 QStyleOptionToolButton opt;
770 initStyleOption( &opt );
771 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
775 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
777 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
780 currentIconSize = mIconSize;
786 currentIconSize = QSize( width() - 10, height() - 6 );
788 currentIconSize = QSize( width() - 10, height() - 12 );
792 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
798 QPixmap pixmap( currentIconSize );
799 pixmap.fill( Qt::transparent );
802 p.setRenderHint( QPainter::Antialiasing );
803 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
814 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
831 QRectF textRect = rect;
832 textRect.setLeft( xtrans );
833 textRect.setWidth( textRect.width() - xtrans );
834 textRect.setTop( ytrans );
835 if ( textRect.height() > 300 )
836 textRect.setHeight( 300 );
837 if ( textRect.width() > 2000 )
838 textRect.setWidth( 2000 );
841 context, tempFormat );
846 p.setBrush( Qt::NoBrush );
847 p.setPen( QColor( 0, 0, 0 ) );
848 p.setFont( tempFont );
849 QRectF textRect = rect;
850 textRect.setLeft( 2 );
851 p.drawText( textRect, Qt::AlignVCenter, text() );
857 setIconSize( currentIconSize );
870 bool hasAlpha =
false;
871 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
881 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 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.