27 #include <QColorDialog>
28 #include <QFontDialog>
40 connect( mHtmlCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutLabelWidget::mHtmlCheckBox_stateChanged );
41 connect( mTextEdit, &QPlainTextEdit::textChanged,
this, &QgsLayoutLabelWidget::mTextEdit_textChanged );
42 connect( mInsertExpressionButton, &QPushButton::clicked,
this, &QgsLayoutLabelWidget::mInsertExpressionButton_clicked );
43 connect( mMarginXDoubleSpinBox,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLabelWidget::mMarginXDoubleSpinBox_valueChanged );
44 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 );
54 mFontButton->setDialogTitle( tr(
"Label Font" ) );
55 mFontButton->registerExpressionContextGenerator(
this );
59 mainLayout->addWidget( mItemPropertiesWidget );
61 mMarginXDoubleSpinBox->setClearValue( 0.0 );
62 mMarginYDoubleSpinBox->setClearValue( 0.0 );
64 setGuiElementValues();
68 connect( mJustifyRadioButton, &QRadioButton::clicked,
this, &QgsLayoutLabelWidget::justifyClicked );
70 mDynamicTextMenu =
new QMenu(
this );
71 mDynamicTextButton->setMenu( mDynamicTextMenu );
73 connect( mDynamicTextMenu, &QMenu::aboutToShow,
this, [ = ]
75 mDynamicTextMenu->clear();
76 if ( mLabel->layout() )
79 buildInsertDynamicTextMenu( mLabel->layout(), mDynamicTextMenu, [ = ]( const QString & expression )
81 mLabel->beginCommand( tr(
"Insert dynamic text" ) );
82 mTextEdit->insertPlainText(
"[%" + expression +
"%]" );
88 QMenu *expressionMenu =
new QMenu(
this );
89 QAction *convertToStaticAction =
new QAction( tr(
"Convert to Static Text" ),
this );
90 expressionMenu->addAction( convertToStaticAction );
92 mInsertExpressionButton->setMenu( expressionMenu );
94 mFontButton->setLayer( coverageLayer() );
95 if ( mLabel->layout() )
103 if ( mItemPropertiesWidget )
109 return mLabel->createExpressionContext();
115 auto addExpression = [&callback]( QMenu * menu,
const QString & name,
const QString & expression )
117 QAction *action =
new QAction( name, menu );
118 connect( action, &QAction::triggered, action, [callback, expression]
120 callback( expression );
122 menu->addAction( action );
125 QMenu *dateMenu =
new QMenu( tr(
"Current Date" ), menu );
126 for (
const std::pair< QString, QString > &expression :
128 std::make_pair( tr(
"ISO Format (%1)" ).arg( QDateTime::currentDateTime().toString( QStringLiteral(
"yyyy-MM-dd" ) ) ), QStringLiteral(
"format_date(now(), 'yyyy-MM-dd')" ) ),
129 std::make_pair( tr(
"Day/Month/Year (%1)" ).arg( QDateTime::currentDateTime().toString( QStringLiteral(
"dd/MM/yyyy" ) ) ), QStringLiteral(
"format_date(now(), 'dd/MM/yyyy')" ) ),
130 std::make_pair( tr(
"Month/Day/Year (%1)" ).arg( QDateTime::currentDateTime().toString( QStringLiteral(
"MM/dd/yyyy" ) ) ), QStringLiteral(
"format_date(now(), 'MM/dd/yyyy')" ) ),
133 addExpression( dateMenu, expression.first, expression.second );
135 menu->addMenu( dateMenu );
137 QMenu *mapsMenu =
new QMenu( tr(
"Map Properties" ), menu );
138 QList< QgsLayoutItemMap * > maps;
143 if ( map->id().isEmpty() )
146 QMenu *mapMenu =
new QMenu( map->displayName(), mapsMenu );
147 for (
const std::pair< QString, QString > &expression :
149 std::make_pair( tr(
"Scale (%1)" ).arg( map->scale() ), QStringLiteral(
"item_variables('%1')['map_scale']" ).arg( map->id() ) ),
150 std::make_pair( tr(
"Rotation (%1)" ).arg( map->rotation() ), QStringLiteral(
"item_variables('%1')['map_rotation']" ).arg( map->id() ) ),
153 addExpression( mapMenu, expression.first, expression.second );
155 mapMenu->addSeparator();
156 for (
const std::pair< QString, QString > &expression :
158 std::make_pair( tr(
"CRS Identifier (%1)" ).arg( map->crs().authid() ), QStringLiteral(
"item_variables('%1')['map_crs']" ).arg( map->id() ) ),
159 std::make_pair( tr(
"CRS Name (%1)" ).arg( map->crs().description() ), QStringLiteral(
"item_variables('%1')['map_crs_description']" ).arg( map->id() ) ),
160 std::make_pair( tr(
"Ellipsoid Name (%1)" ).arg( map->crs().ellipsoidAcronym() ), QStringLiteral(
"item_variables('%1')['map_crs_ellipsoid']" ).arg( map->id() ) ),
161 std::make_pair( tr(
"Units (%1)" ).arg(
QgsUnitTypes::toString( map->crs().mapUnits() ) ), QStringLiteral(
"item_variables('%1')['map_units']" ).arg( map->id() ) ),
162 std::make_pair( tr(
"Projection (%1)" ).arg( map->crs().operation().description() ), QStringLiteral(
"item_variables('%1')['map_crs_projection']" ).arg( map->id() ) ),
165 addExpression( mapMenu, expression.first, expression.second );
167 mapMenu->addSeparator();
171 for (
const std::pair< QString, QString > &expression :
173 std::make_pair( tr(
"Center (X) (%1)" ).arg( center.
x() ), QStringLiteral(
"x(item_variables('%1')['map_extent_center'])" ).arg( map->id() ) ),
174 std::make_pair( tr(
"Center (Y) (%1)" ).arg( center.
y() ), QStringLiteral(
"y(item_variables('%1')['map_extent_center'])" ).arg( map->id() ) ),
175 std::make_pair( tr(
"X Minimum (%1)" ).arg( mapExtent.
xMinimum() ), QStringLiteral(
"x_min(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
176 std::make_pair( tr(
"Y Minimum (%1)" ).arg( mapExtent.
yMinimum() ), QStringLiteral(
"y_min(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
177 std::make_pair( tr(
"X Maximum (%1)" ).arg( mapExtent.
xMaximum() ), QStringLiteral(
"x_max(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
178 std::make_pair( tr(
"Y Maximum (%1)" ).arg( mapExtent.
yMaximum() ), QStringLiteral(
"y_max(item_variables('%1')['map_extent'])" ).arg( map->id() ) ),
181 addExpression( mapMenu, expression.first, expression.second );
183 mapMenu->addSeparator();
184 for (
const std::pair< QString, QString > &expression :
186 std::make_pair( tr(
"Layer Credits" ), QStringLiteral(
"array_to_string(map_credits('%1'))" ).arg( map->id() ) ),
189 addExpression( mapMenu, expression.first, expression.second );
191 mapsMenu->addMenu( mapMenu );
193 menu->addMenu( mapsMenu );
194 menu->addSeparator();
200 QMenu *fieldsMenu =
new QMenu( tr(
"Field" ), menu );
206 menu->addMenu( fieldsMenu );
207 menu->addSeparator();
210 for (
const std::pair< QString, QString > &expression :
212 std::make_pair( tr(
"Layout Name" ), QStringLiteral(
"@layout_name" ) ),
213 std::make_pair( tr(
"Layout Page Number" ), QStringLiteral(
"@layout_page" ) ),
214 std::make_pair( tr(
"Layout Page Count" ), QStringLiteral(
"@layout_numpages" ) )
217 addExpression( menu, expression.first, expression.second );
219 menu->addSeparator();
220 for (
const std::pair< QString, QString > &expression :
222 std::make_pair( tr(
"Project Author" ), QStringLiteral(
"@project_author" ) ),
223 std::make_pair( tr(
"Project Title" ), QStringLiteral(
"@project_title" ) ),
224 std::make_pair( tr(
"Project Path" ), QStringLiteral(
"@project_path" ) )
227 addExpression( menu, expression.first, expression.second );
229 menu->addSeparator();
230 for (
const std::pair< QString, QString > &expression :
232 std::make_pair( tr(
"Current User Name" ), QStringLiteral(
"@user_full_name" ) ),
233 std::make_pair( tr(
"Current User Account" ), QStringLiteral(
"@user_account_name" ) )
236 addExpression( menu, expression.first, expression.second );
250 mLabel = qobject_cast< QgsLayoutItemLabel * >( item );
251 mItemPropertiesWidget->
setItem( mLabel );
258 setGuiElementValues();
263 void QgsLayoutLabelWidget::mHtmlCheckBox_stateChanged(
int state )
267 mVerticalAlignementLabel->setDisabled( state );
268 mTopRadioButton->setDisabled( state );
269 mMiddleRadioButton->setDisabled( state );
270 mBottomRadioButton->setDisabled( state );
272 mLabel->beginCommand( tr(
"Change Label Mode" ) );
273 mLabel->blockSignals(
true );
275 mLabel->setText( mTextEdit->toPlainText() );
277 mLabel->blockSignals(
false );
278 mLabel->endCommand();
282 void QgsLayoutLabelWidget::mTextEdit_textChanged()
287 mLabel->blockSignals(
true );
288 mLabel->setText( mTextEdit->toPlainText() );
290 mLabel->blockSignals(
false );
291 mLabel->endCommand();
295 void QgsLayoutLabelWidget::fontChanged()
300 mLabel->setTextFormat( mFontButton->textFormat() );
302 mLabel->endCommand();
306 void QgsLayoutLabelWidget::justifyClicked()
310 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
311 mLabel->setHAlign( Qt::AlignJustify );
313 mLabel->endCommand();
317 void QgsLayoutLabelWidget::mMarginXDoubleSpinBox_valueChanged(
double d )
322 mLabel->setMarginX( d );
324 mLabel->endCommand();
328 void QgsLayoutLabelWidget::mMarginYDoubleSpinBox_valueChanged(
double d )
333 mLabel->setMarginY( d );
335 mLabel->endCommand();
339 void QgsLayoutLabelWidget::mInsertExpressionButton_clicked()
346 QString selText = mTextEdit->textCursor().selectedText();
349 selText = selText.replace( QChar( 0x2029 ), QChar(
'\n' ) );
352 if ( selText.startsWith( QLatin1String(
"[%" ) ) && selText.endsWith( QLatin1String(
"%]" ) ) )
353 selText = selText.mid( 2, selText.size() - 4 );
361 exprDlg.setWindowTitle( tr(
"Insert Expression" ) );
362 if ( exprDlg.exec() == QDialog::Accepted )
364 QString expression = exprDlg.expressionText();
365 if ( !expression.isEmpty() )
367 mLabel->beginCommand( tr(
"Insert expression" ) );
368 mTextEdit->insertPlainText(
"[%" + expression +
"%]" );
369 mLabel->endCommand();
374 void QgsLayoutLabelWidget::mCenterRadioButton_clicked()
378 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
379 mLabel->setHAlign( Qt::AlignHCenter );
381 mLabel->endCommand();
385 void QgsLayoutLabelWidget::mRightRadioButton_clicked()
389 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
390 mLabel->setHAlign( Qt::AlignRight );
392 mLabel->endCommand();
396 void QgsLayoutLabelWidget::mLeftRadioButton_clicked()
400 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
401 mLabel->setHAlign( Qt::AlignLeft );
403 mLabel->endCommand();
407 void QgsLayoutLabelWidget::mTopRadioButton_clicked()
411 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
412 mLabel->setVAlign( Qt::AlignTop );
414 mLabel->endCommand();
418 void QgsLayoutLabelWidget::mBottomRadioButton_clicked()
422 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
423 mLabel->setVAlign( Qt::AlignBottom );
425 mLabel->endCommand();
429 void QgsLayoutLabelWidget::mMiddleRadioButton_clicked()
433 mLabel->beginCommand( tr(
"Change Label Alignment" ) );
434 mLabel->setVAlign( Qt::AlignVCenter );
436 mLabel->endCommand();
440 void QgsLayoutLabelWidget::setGuiElementValues()
442 blockAllSignals(
true );
443 mTextEdit->setPlainText( mLabel->text() );
444 mTextEdit->moveCursor( QTextCursor::End, QTextCursor::MoveAnchor );
445 mMarginXDoubleSpinBox->setValue( mLabel->marginX() );
446 mMarginYDoubleSpinBox->setValue( mLabel->marginY() );
448 mTopRadioButton->setChecked( mLabel->vAlign() == Qt::AlignTop );
449 mMiddleRadioButton->setChecked( mLabel->vAlign() == Qt::AlignVCenter );
450 mBottomRadioButton->setChecked( mLabel->vAlign() == Qt::AlignBottom );
451 mLeftRadioButton->setChecked( mLabel->hAlign() == Qt::AlignLeft );
452 mJustifyRadioButton->setChecked( mLabel->hAlign() == Qt::AlignJustify );
453 mCenterRadioButton->setChecked( mLabel->hAlign() == Qt::AlignHCenter );
454 mRightRadioButton->setChecked( mLabel->hAlign() == Qt::AlignRight );
455 mFontButton->setTextFormat( mLabel->textFormat() );
461 blockAllSignals(
false );
464 void QgsLayoutLabelWidget::blockAllSignals(
bool block )
466 mTextEdit->blockSignals( block );
467 mHtmlCheckBox->blockSignals( block );
468 mMarginXDoubleSpinBox->blockSignals( block );
469 mMarginYDoubleSpinBox->blockSignals( block );
470 mTopRadioButton->blockSignals( block );
471 mMiddleRadioButton->blockSignals( block );
472 mBottomRadioButton->blockSignals( block );
473 mLeftRadioButton->blockSignals( block );
474 mCenterRadioButton->blockSignals( block );
475 mRightRadioButton->blockSignals( block );
476 mJustifyRadioButton->blockSignals( block );
477 mFontButton->blockSignals( block );