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(), mMode ==
ModeTextRenderer ? mFormat.
font().family() : mFont.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 );
240 drag->setPixmap( createDragIcon( QSize( iconSize, iconSize ) ) );
241 drag->exec( Qt::CopyAction );
245 bool QgsFontButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
250 if ( mimeColor.isValid() )
252 resultColor = mimeColor;
266 bool hasAlpha =
false;
268 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
270 e->acceptProposedAction();
271 updatePreview( QColor(), &format );
273 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
275 e->acceptProposedAction();
276 updatePreview( QColor(),
nullptr, &font );
278 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
283 e->acceptProposedAction();
284 updatePreview( mimeColor );
301 bool hasAlpha =
false;
302 if ( mMode ==
ModeTextRenderer && formatFromMimeData( e->mimeData(), format ) )
308 else if ( mMode ==
ModeQFont && fontFromMimeData( e->mimeData(), font ) )
314 else if ( mMode ==
ModeTextRenderer && colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
317 e->acceptProposedAction();
323 mimeColor.setAlphaF( 1.0 );
338 size = mFormat.
size();
342 size = mFont.pointSizeF();
346 double increment =
event->modifiers() & Qt::ControlModifier ? 0.1 : 1;
347 if ( event->delta() > 0 )
355 size = std::max( size, 1.0 );
369 QFont newFont = mFont;
370 newFont.setPointSizeF( size );
379 QPixmap QgsFontButton::createColorIcon(
const QColor &color )
const 383 QPixmap pixmap( iconSize, iconSize );
384 pixmap.fill( Qt::transparent );
390 p.setBrush( QBrush( color ) );
393 p.setPen( QColor( 197, 197, 197 ) );
394 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
399 QPixmap QgsFontButton::createDragIcon( QSize size,
const QgsTextFormat *tempFormat,
const QFont *tempFont )
const 402 tempFormat = &mFormat;
407 QPixmap pixmap( size.width(), size.height() );
408 pixmap.fill( Qt::transparent );
411 p.setRenderHint( QPainter::Antialiasing );
412 QRect rect( 0, 0, size.width(), size.height() );
414 if ( mMode ==
ModeQFont || tempFormat->
color().lightnessF() < 0.7 )
416 p.setBrush( QBrush( QColor( 255, 255, 255 ) ) );
417 p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) );
421 p.setBrush( QBrush( QColor( 0, 0, 0 ) ) );
422 p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) );
425 p.setBrush( Qt::NoBrush );
426 p.setPen( Qt::NoPen );
437 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
454 QRectF textRect = rect;
455 textRect.setLeft( xtrans );
456 textRect.setWidth( textRect.width() - xtrans );
457 textRect.setTop( ytrans );
458 if ( textRect.height() > 300 )
459 textRect.setHeight( 300 );
460 if ( textRect.width() > 2000 )
461 textRect.setWidth( 2000 );
464 context, *tempFormat );
469 p.setBrush( Qt::NoBrush );
470 p.setPen( QColor( 0, 0, 0 ) );
471 p.setFont( *tempFont );
472 QRectF textRect = rect;
473 textRect.setLeft( 2 );
474 p.drawText( textRect, Qt::AlignVCenter, tr(
"Aa" ) );
483 void QgsFontButton::prepareMenu()
492 QWidgetAction *sizeAction =
new QWidgetAction( mMenu );
493 QWidget *sizeWidget =
new QWidget();
494 QVBoxLayout *sizeLayout =
new QVBoxLayout();
495 sizeLayout->setMargin( 0 );
496 sizeLayout->setContentsMargins( 0, 0, 0, 3 );
497 sizeLayout->setSpacing( 2 );
499 QString fontHeaderLabel;
507 fontHeaderLabel = tr(
"Font size (pt)" );
512 sizeLayout->addWidget( sizeLabel );
515 sizeSpin->setDecimals( 4 );
516 sizeSpin->setMaximum( 1e+9 );
519 connect( sizeSpin, static_cast <
void (
QgsDoubleSpinBox::* )(
double ) > ( &QgsDoubleSpinBox::valueChanged ),
520 this, [ = ](
double value )
528 mFont.setPointSizeF( value );
534 QHBoxLayout *spinLayout =
new QHBoxLayout();
535 spinLayout->setMargin( 0 );
536 spinLayout->setContentsMargins( 4, 0, 4, 0 );
537 spinLayout->addWidget( sizeSpin );
538 sizeLayout->addLayout( spinLayout );
539 sizeWidget->setLayout( sizeLayout );
540 sizeAction->setDefaultWidget( sizeWidget );
541 sizeWidget->setFocusProxy( sizeSpin );
542 sizeWidget->setFocusPolicy( Qt::StrongFocus );
543 mMenu->addAction( sizeAction );
545 QMenu *recentFontMenu =
new QMenu( tr(
"Recent Fonts" ), mMenu );
548 QAction *fontAction =
new QAction( family, recentFontMenu );
549 QFont f = fontAction->font();
550 f.setFamily( family );
551 fontAction->setFont( f );
552 fontAction->setToolTip( family );
553 recentFontMenu->addAction( fontAction );
555 || ( mMode ==
ModeQFont && family == mFont.family() ) )
557 fontAction->setCheckable(
true );
558 fontAction->setChecked(
true );
560 auto setFont = [
this, family]
567 QFont f = newFormat.
font();
568 f.setFamily( family );
577 font.setFamily( family );
584 connect( fontAction, &QAction::triggered,
this, setFont );
586 mMenu->addMenu( recentFontMenu );
588 QAction *configureAction =
new QAction( tr(
"Configure Format…" ),
this );
589 mMenu->addAction( configureAction );
590 connect( configureAction, &QAction::triggered,
this, &QgsFontButton::showSettingsDialog );
592 QAction *copyFormatAction =
new QAction( tr(
"Copy Format" ),
this );
593 mMenu->addAction( copyFormatAction );
595 QAction *pasteFormatAction =
new QAction( tr(
"Paste Format" ),
this );
601 if ( mMode ==
ModeTextRenderer && formatFromMimeData( QApplication::clipboard()->mimeData(), tempFormat ) )
605 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ), &tempFormat ) );
607 else if ( mMode ==
ModeQFont && fontFromMimeData( QApplication::clipboard()->mimeData(), tempFont ) )
609 tempFont.setPointSize( 8 );
610 pasteFormatAction->setIcon( createDragIcon( QSize( iconSize, iconSize ),
nullptr, &tempFont ) );
614 pasteFormatAction->setEnabled(
false );
616 mMenu->addAction( pasteFormatAction );
621 mMenu->addSeparator();
628 mMenu->addAction( colorAction );
631 QColor alphaColor = mFormat.
color();
632 alphaColor.setAlphaF( mFormat.
opacity() );
638 double opacity = color.alphaF();
645 mMenu->addAction( alphaAction );
649 QList< QgsColorScheme * >::iterator it = schemeList.begin();
650 for ( ; it != schemeList.end(); ++it )
654 mMenu->addAction( colorAction );
659 mMenu->addSeparator();
661 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
662 mMenu->addAction( copyColorAction );
665 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
669 bool hasAlpha =
false;
670 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
672 pasteColorAction->setIcon( createColorIcon( clipColor ) );
676 pasteColorAction->setEnabled(
false );
678 mMenu->addAction( pasteColorAction );
683 void QgsFontButton::addRecentColor(
const QColor &color )
711 bool QgsFontButton::formatFromMimeData(
const QMimeData *mimeData,
QgsTextFormat &resultFormat )
const 718 bool QgsFontButton::fontFromMimeData(
const QMimeData *mimeData, QFont &resultFont )
const 727 if ( e->type() == QEvent::EnabledChange )
731 QToolButton::changeEvent( e );
737 QToolButton::showEvent( e );
742 QToolButton::resizeEvent( event );
748 void QgsFontButton::updatePreview(
const QColor &color,
QgsTextFormat *format, QFont *font )
754 tempFormat = *format;
756 tempFormat = mFormat;
762 if ( color.isValid() )
765 QSize currentIconSize;
769 if ( !mIconSize.isValid() )
772 QStyleOptionToolButton opt;
773 initStyleOption( &opt );
774 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
778 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
779 #elif defined(Q_OS_MAC) 780 mIconSize = QSize( buttonSize.width() - 10, height() - 2 );
782 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
785 currentIconSize = mIconSize;
791 currentIconSize = QSize( width() - 10, height() - 6 );
793 currentIconSize = QSize( width() - 10, height() - 12 );
797 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
803 QPixmap pixmap( currentIconSize );
804 pixmap.fill( Qt::transparent );
807 p.setRenderHint( QPainter::Antialiasing );
808 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
819 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
836 QRectF textRect = rect;
837 textRect.setLeft( xtrans );
838 textRect.setWidth( textRect.width() - xtrans );
839 textRect.setTop( ytrans );
840 if ( textRect.height() > 300 )
841 textRect.setHeight( 300 );
842 if ( textRect.width() > 2000 )
843 textRect.setWidth( 2000 );
846 context, tempFormat );
851 p.setBrush( Qt::NoBrush );
852 p.setPen( QColor( 0, 0, 0 ) );
853 p.setFont( tempFont );
854 QRectF textRect = rect;
855 textRect.setLeft( 2 );
856 p.drawText( textRect, Qt::AlignVCenter, text() );
862 setIconSize( currentIconSize );
875 bool hasAlpha =
false;
876 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
886 mDialogTitle = title;
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value...
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
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.
QFont font() const
Returns the font used for rendering text.
QSizeF size() const
Returns the size of the background shape.
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.
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.
void setFont(const QFont &font)
Sets the font used for rendering text.
double size() const
Returns the size for rendered text.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
bool enabled() const
Returns whether the background is enabled.
QMimeData * toMimeData() const
Returns new mime data representing the text format settings.
double opacity() const
Returns the text's opacity.
QColor color() const
Returns the color that text will be rendered in.
Show scheme in color button drop-down menu.
Map canvas is a class for displaying all GIS data types on a canvas.
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.
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.
bool enabled() const
Returns whether the buffer is enabled.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
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 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.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
QgsTextFormat format() const
Returns the current formatting settings defined by the widget.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes. ...
Contains information about the context of a rendering operation.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
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.
double size() const
Returns the size of the buffer.
QgsTextFormat format() const
Returns the current formatting settings defined by the widget.
static QMimeData * toMimeData(const QFont &font)
Returns new mime data representing the specified font settings.
Container for all settings relating to text rendering.
A simple dialog for customizing text formatting settings.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
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.