26 #include <QColorDialog>
27 #include <QFontDialog>
39 connect( mHtmlCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutLabelWidget::mHtmlCheckBox_stateChanged );
40 connect( mTextEdit, &QPlainTextEdit::textChanged,
this, &QgsLayoutLabelWidget::mTextEdit_textChanged );
41 connect( mInsertExpressionButton, &QPushButton::clicked,
this, &QgsLayoutLabelWidget::mInsertExpressionButton_clicked );
42 connect( mMarginXDoubleSpinBox,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLabelWidget::mMarginXDoubleSpinBox_valueChanged );
43 connect( mMarginYDoubleSpinBox,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLabelWidget::mMarginYDoubleSpinBox_valueChanged );
45 connect( mCenterRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::mCenterRadioButton_clicked );
46 connect( mLeftRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::mLeftRadioButton_clicked );
47 connect( mRightRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::mRightRadioButton_clicked );
48 connect( mTopRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::mTopRadioButton_clicked );
49 connect( mBottomRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::mBottomRadioButton_clicked );
50 connect( mMiddleRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::mMiddleRadioButton_clicked );
57 mainLayout->addWidget( mItemPropertiesWidget );
59 mFontColorButton->setColorDialogTitle( tr(
"Select Font Color" ) );
60 mFontColorButton->setContext( QStringLiteral(
"composer" ) );
61 mFontColorButton->setAllowOpacity(
true );
63 mMarginXDoubleSpinBox->setClearValue( 0.0 );
64 mMarginYDoubleSpinBox->setClearValue( 0.0 );
66 setGuiElementValues();
70 connect( mJustifyRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::justifyClicked );
72 mDynamicTextMenu =
new QMenu(
this );
73 mDynamicTextButton->setMenu( mDynamicTextMenu );
75 connect( mDynamicTextMenu, &QMenu::aboutToShow,
this, [ = ]
77 mDynamicTextMenu->clear();
81 mLabel->beginCommand( tr(
"Insert dynamic text" ) );
82 mTextEdit->insertPlainText(
"[%" + expression +
"%]" );
91 if ( mItemPropertiesWidget )
97 auto addExpression = [&callback]( QMenu * menu,
const QString & name,
const QString & expression )
99 QAction *action =
new QAction( name, menu );
100 connect( action, &QAction::triggered, action, [callback, expression]
102 callback( expression );
104 menu->addAction( action );
107 QMenu *dateMenu =
new QMenu( tr(
"Current Date" ), menu );
108 for (
const std::pair< QString, QString > &expression :
110 std::make_pair( tr(
"ISO Format (%1)" ).arg( QDateTime::currentDateTime().toString( QStringLiteral(
"yyyy-MM-dd" ) ) ), QStringLiteral(
"format_date(now(), 'yyyy-MM-dd')" ) ),
111 std::make_pair( tr(
"Day/Month/Year (%1)" ).arg( QDateTime::currentDateTime().toString( QStringLiteral(
"dd/MM/yyyy" ) ) ), QStringLiteral(
"format_date(now(), 'dd/MM/yyyy')" ) ),
112 std::make_pair( tr(
"Month/Day/Year (%1)" ).arg( QDateTime::currentDateTime().toString( QStringLiteral(
"MM/dd/yyyy" ) ) ), QStringLiteral(
"format_date(now(), 'MM/dd/yyyy')" ) ),
115 addExpression( dateMenu, expression.first, expression.second );
117 menu->addMenu( dateMenu );
119 QMenu *mapsMenu =
new QMenu( tr(
"Map Properties" ), menu );
120 QList< QgsLayoutItemMap * > maps;
125 if ( map->id().isEmpty() )
128 QMenu *mapMenu =
new QMenu( map->displayName(), mapsMenu );
129 for (
const std::pair< QString, QString > &expression :
131 std::make_pair( tr(
"Scale (%1)" ).arg( map->scale() ), QStringLiteral(
"item_variables('%1')['map_scale']" ).arg( map->id() ) ),
132 std::make_pair( tr(
"Rotation (%1)" ).arg( map->rotation() ), QStringLiteral(
"item_variables('%1')['map_rotation']" ).arg( map->id() ) ),
135 addExpression( mapMenu, expression.first, expression.second );
137 mapMenu->addSeparator();
138 for (
const std::pair< QString, QString > &expression :
140 std::make_pair( tr(
"CRS Identifier (%1)" ).arg( map->crs().authid() ), QStringLiteral(
"item_variables('%1')['map_crs']" ).arg( map->id() ) ),
141 std::make_pair( tr(
"CRS Name (%1)" ).arg( map->crs().description() ), QStringLiteral(
"item_variables('%1')['map_crs_description']" ).arg( map->id() ) ),
142 std::make_pair( tr(
"Ellipsoid Name (%1)" ).arg( map->crs().ellipsoidAcronym() ), QStringLiteral(
"item_variables('%1')['map_crs_ellipsoid']" ).arg( map->id() ) ),
143 std::make_pair( tr(
"Units (%1)" ).arg(
QgsUnitTypes::toString( map->crs().mapUnits() ) ), QStringLiteral(
"item_variables('%1')['map_units']" ).arg( map->id() ) ),
146 addExpression( mapMenu, expression.first, expression.second );
148 mapMenu->addSeparator();
152 for (
const std::pair< QString, QString > &expression :
154 std::make_pair( tr(
"Center (X) (%1)" ).arg( center.
x() ), QStringLiteral(
"x(item_variables('%1')['map_extent_center'])" ).arg( map->id() ) ),
155 std::make_pair( tr(
"Center (Y) (%1)" ).arg( center.
y() ), QStringLiteral(
"y(item_variables('%1')['map_extent_center'])" ).arg( map->id() ) ),
156 std::make_pair( tr(
"X Minimum (%1)" ).arg( mapExtent.
xMinimum() ), QStringLiteral(
"x_min(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
157 std::make_pair( tr(
"Y Minimum (%1)" ).arg( mapExtent.
yMinimum() ), QStringLiteral(
"y_min(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
158 std::make_pair( tr(
"X Maximum (%1)" ).arg( mapExtent.
xMaximum() ), QStringLiteral(
"x_max(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
159 std::make_pair( tr(
"Y Maximum (%1)" ).arg( mapExtent.
yMaximum() ), QStringLiteral(
"y_max(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
162 addExpression( mapMenu, expression.first, expression.second );
164 mapMenu->addSeparator();
165 for (
const std::pair< QString, QString > &expression :
167 std::make_pair( tr(
"Layer Credits" ), QStringLiteral(
"array_to_string(map_credits('%1'))" ).arg( map->id() ) ),
170 addExpression( mapMenu, expression.first, expression.second );
172 mapsMenu->addMenu( mapMenu );
174 menu->addMenu( mapsMenu );
175 menu->addSeparator();
181 QMenu *fieldsMenu =
new QMenu( tr(
"Field" ), menu );
187 menu->addMenu( fieldsMenu );
188 menu->addSeparator();
191 for (
const std::pair< QString, QString > &expression :
193 std::make_pair( tr(
"Layout Name" ), QStringLiteral(
"@layout_name" ) ),
194 std::make_pair( tr(
"Layout Page Number" ), QStringLiteral(
"@layout_page" ) ),
195 std::make_pair( tr(
"Layout Page Count" ), QStringLiteral(
"@layout_numpages" ) )
198 addExpression( menu, expression.first, expression.second );
200 menu->addSeparator();
201 for (
const std::pair< QString, QString > &expression :
203 std::make_pair( tr(
"Project Author" ), QStringLiteral(
"@project_author" ) ),
204 std::make_pair( tr(
"Project Title" ), QStringLiteral(
"@project_title" ) ),
205 std::make_pair( tr(
"Project Path" ), QStringLiteral(
"@project_path" ) )
208 addExpression( menu, expression.first, expression.second );
210 menu->addSeparator();
211 for (
const std::pair< QString, QString > &expression :
213 std::make_pair( tr(
"Current User Name" ), QStringLiteral(
"@user_full_name" ) ),
214 std::make_pair( tr(
"Current User Account" ), QStringLiteral(
"@user_account_name" ) )
217 addExpression( menu, expression.first, expression.second );
231 mLabel = qobject_cast< QgsLayoutItemLabel * >( item );
232 mItemPropertiesWidget->
setItem( mLabel );
239 setGuiElementValues();
244 void QgsLayoutLabelWidget::mHtmlCheckBox_stateChanged(
int state )
248 mVerticalAlignementLabel->setDisabled( state );
249 mTopRadioButton->setDisabled( state );
250 mMiddleRadioButton->setDisabled( state );
251 mBottomRadioButton->setDisabled( state );
253 mLabel->beginCommand( tr(
"Change Label Mode" ) );
254 mLabel->blockSignals(
true );
256 mLabel->setText( mTextEdit->toPlainText() );
258 mLabel->blockSignals(
false );
259 mLabel->endCommand();
263 void QgsLayoutLabelWidget::mTextEdit_textChanged()
268 mLabel->blockSignals(
true );
269 mLabel->setText( mTextEdit->toPlainText() );
271 mLabel->blockSignals(
false );
272 mLabel->endCommand();
276 void QgsLayoutLabelWidget::fontChanged()
280 QFont newFont = mFontButton->currentFont();
282 mLabel->setFont( newFont );
284 mLabel->endCommand();
288 void QgsLayoutLabelWidget::justifyClicked()
292 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
293 mLabel->setHAlign( Qt::AlignJustify );
295 mLabel->endCommand();
299 void QgsLayoutLabelWidget::mMarginXDoubleSpinBox_valueChanged(
double d )
304 mLabel->setMarginX( d );
306 mLabel->endCommand();
310 void QgsLayoutLabelWidget::mMarginYDoubleSpinBox_valueChanged(
double d )
315 mLabel->setMarginY( d );
317 mLabel->endCommand();
321 void QgsLayoutLabelWidget::mFontColorButton_colorChanged(
const QColor &newLabelColor )
329 mLabel->setFontColor( newLabelColor );
331 mLabel->endCommand();
334 void QgsLayoutLabelWidget::mInsertExpressionButton_clicked()
341 QString selText = mTextEdit->textCursor().selectedText();
344 selText = selText.replace( QChar( 0x2029 ), QChar(
'\n' ) );
347 if ( selText.startsWith( QLatin1String(
"[%" ) ) && selText.endsWith( QLatin1String(
"%]" ) ) )
348 selText = selText.mid( 2, selText.size() - 4 );
356 exprDlg.setWindowTitle( tr(
"Insert Expression" ) );
357 if ( exprDlg.exec() == QDialog::Accepted )
359 QString expression = exprDlg.expressionText();
360 if ( !expression.isEmpty() )
362 mLabel->beginCommand( tr(
"Insert expression" ) );
363 mTextEdit->insertPlainText(
"[%" + expression +
"%]" );
364 mLabel->endCommand();
369 void QgsLayoutLabelWidget::mCenterRadioButton_clicked()
373 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
374 mLabel->setHAlign( Qt::AlignHCenter );
376 mLabel->endCommand();
380 void QgsLayoutLabelWidget::mRightRadioButton_clicked()
384 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
385 mLabel->setHAlign( Qt::AlignRight );
387 mLabel->endCommand();
391 void QgsLayoutLabelWidget::mLeftRadioButton_clicked()
395 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
396 mLabel->setHAlign( Qt::AlignLeft );
398 mLabel->endCommand();
402 void QgsLayoutLabelWidget::mTopRadioButton_clicked()
406 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
407 mLabel->setVAlign( Qt::AlignTop );
409 mLabel->endCommand();
413 void QgsLayoutLabelWidget::mBottomRadioButton_clicked()
417 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
418 mLabel->setVAlign( Qt::AlignBottom );
420 mLabel->endCommand();
424 void QgsLayoutLabelWidget::mMiddleRadioButton_clicked()
428 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
429 mLabel->setVAlign( Qt::AlignVCenter );
431 mLabel->endCommand();
435 void QgsLayoutLabelWidget::setGuiElementValues()
437 blockAllSignals(
true );
438 mTextEdit->setPlainText( mLabel->text() );
439 mTextEdit->moveCursor( QTextCursor::End, QTextCursor::MoveAnchor );
440 mMarginXDoubleSpinBox->setValue( mLabel->marginX() );
441 mMarginYDoubleSpinBox->setValue( mLabel->marginY() );
443 mTopRadioButton->setChecked( mLabel->vAlign() == Qt::AlignTop );
444 mMiddleRadioButton->setChecked( mLabel->vAlign() == Qt::AlignVCenter );
445 mBottomRadioButton->setChecked( mLabel->vAlign() == Qt::AlignBottom );
446 mLeftRadioButton->setChecked( mLabel->hAlign() == Qt::AlignLeft );
447 mJustifyRadioButton->setChecked( mLabel->hAlign() == Qt::AlignJustify );
448 mCenterRadioButton->setChecked( mLabel->hAlign() == Qt::AlignHCenter );
449 mRightRadioButton->setChecked( mLabel->hAlign() == Qt::AlignRight );
450 mFontColorButton->setColor( mLabel->fontColor() );
451 mFontButton->setCurrentFont( mLabel->font() );
457 blockAllSignals(
false );
460 void QgsLayoutLabelWidget::blockAllSignals(
bool block )
462 mTextEdit->blockSignals( block );
463 mHtmlCheckBox->blockSignals( block );
464 mMarginXDoubleSpinBox->blockSignals( block );
465 mMarginYDoubleSpinBox->blockSignals( block );
466 mTopRadioButton->blockSignals( block );
467 mMiddleRadioButton->blockSignals( block );
468 mBottomRadioButton->blockSignals( block );
469 mLeftRadioButton->blockSignals( block );
470 mCenterRadioButton->blockSignals( block );
471 mRightRadioButton->blockSignals( block );
472 mJustifyRadioButton->blockSignals( block );
473 mFontColorButton->blockSignals( block );
474 mFontButton->blockSignals( block );
A generic dialog for building expression strings.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Encapsulate a field in an attribute table or data source.
QString displayName() const
Returns the name to use when displaying this field.
Container of fields for a vector layer.
A layout item subclass for text labels.
@ ModeHtml
Label displays rendered HTML content.
@ ModeFont
Label displays text rendered using a single font.
Layout graphical items for displaying a map.
Base class for graphical items within a QgsLayout.
@ UndoLabelFontColor
Label color.
@ UndoLabelMargin
Label margin.
@ UndoLabelFont
Label font.
@ UndoLabelText
Label text.
int type() const override
Returns a unique graphics item type identifier.
void changed()
Emitted when the object's properties change.
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout's context.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
Interface for master layout type objects, such as print layouts and reports.
A class to represent a 2D point.
A rectangle specified with double values.
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.