31#include "moc_qgslayouthtmlwidget.cpp"
33using namespace Qt::StringLiterals;
37 , mHtml( frame ? qobject_cast<
QgsLayoutItemHtml *>( frame->multiFrame() ) : nullptr )
41 connect( mUrlLineEdit, &QLineEdit::editingFinished,
this, &QgsLayoutHtmlWidget::mUrlLineEdit_editingFinished );
42 connect( mFileToolButton, &QToolButton::clicked,
this, &QgsLayoutHtmlWidget::mFileToolButton_clicked );
43 connect( mResizeModeComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsLayoutHtmlWidget::mResizeModeComboBox_currentIndexChanged );
44 connect( mEvaluateExpressionsCheckbox, &QCheckBox::toggled,
this, &QgsLayoutHtmlWidget::mEvaluateExpressionsCheckbox_toggled );
45 connect( mUseSmartBreaksCheckBox, &QgsCollapsibleGroupBoxBasic::toggled,
this, &QgsLayoutHtmlWidget::mUseSmartBreaksCheckBox_toggled );
46 connect( mMaxDistanceSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutHtmlWidget::mMaxDistanceSpinBox_valueChanged );
47 connect( mUserStylesheetCheckBox, &QgsCollapsibleGroupBoxBasic::toggled,
this, &QgsLayoutHtmlWidget::mUserStylesheetCheckBox_toggled );
48 connect( mRadioManualSource, &QRadioButton::clicked,
this, &QgsLayoutHtmlWidget::mRadioManualSource_clicked );
49 connect( mRadioUrlSource, &QRadioButton::clicked,
this, &QgsLayoutHtmlWidget::mRadioUrlSource_clicked );
50 connect( mInsertExpressionButton, &QPushButton::clicked,
this, &QgsLayoutHtmlWidget::mInsertExpressionButton_clicked );
51 connect( mReloadPushButton, &QPushButton::clicked,
this, &QgsLayoutHtmlWidget::mReloadPushButton_clicked );
52 connect( mReloadPushButton2, &QPushButton::clicked,
this, &QgsLayoutHtmlWidget::mReloadPushButton_clicked );
53 connect( mAddFramePushButton, &QPushButton::clicked,
this, &QgsLayoutHtmlWidget::mAddFramePushButton_clicked );
54 connect( mEmptyFrameCheckBox, &QCheckBox::toggled,
this, &QgsLayoutHtmlWidget::mEmptyFrameCheckBox_toggled );
55 connect( mHideEmptyBgCheckBox, &QCheckBox::toggled,
this, &QgsLayoutHtmlWidget::mHideEmptyBgCheckBox_toggled );
60 connect( mHtmlEditor, &QsciScintilla::textChanged,
this, &QgsLayoutHtmlWidget::htmlEditorChanged );
61 htmlEditorLayout->addWidget( mHtmlEditor );
65 connect( mStylesheetEditor, &QsciScintilla::textChanged,
this, &QgsLayoutHtmlWidget::stylesheetEditorChanged );
66 stylesheetEditorLayout->addWidget( mStylesheetEditor );
73 blockSignals(
false );
74 setGuiElementValues();
86 mainLayout->addWidget( mItemPropertiesWidget );
96 if ( mItemPropertiesWidget )
97 mItemPropertiesWidget->setMasterLayout( masterLayout );
118 mHtml = qobject_cast<QgsLayoutItemHtml *>( multiFrame );
120 mItemPropertiesWidget->setItem( frame );
127 setGuiElementValues();
132void QgsLayoutHtmlWidget::blockSignals(
bool block )
134 mUrlLineEdit->blockSignals( block );
135 mFileToolButton->blockSignals( block );
136 mResizeModeComboBox->blockSignals( block );
137 mUseSmartBreaksCheckBox->blockSignals( block );
138 mMaxDistanceSpinBox->blockSignals( block );
139 mHtmlEditor->blockSignals( block );
140 mStylesheetEditor->blockSignals( block );
141 mUserStylesheetCheckBox->blockSignals( block );
142 mRadioManualSource->blockSignals( block );
143 mRadioUrlSource->blockSignals( block );
144 mEvaluateExpressionsCheckbox->blockSignals( block );
145 mEmptyFrameCheckBox->blockSignals( block );
146 mHideEmptyBgCheckBox->blockSignals( block );
149void QgsLayoutHtmlWidget::mUrlLineEdit_editingFinished()
153 const QUrl newUrl( mUrlLineEdit->text() );
154 if ( newUrl == mHtml->url() )
159 mHtml->beginCommand( tr(
"Change HTML Url" ) );
160 mHtml->setUrl( newUrl );
166void QgsLayoutHtmlWidget::mFileToolButton_clicked()
169 const QString lastDir = s.
value( u
"/UI/lastHtmlDir"_s, QDir::homePath() ).toString();
170 const QString file = QFileDialog::getOpenFileName(
this, tr(
"Select HTML document" ), lastDir, u
"HTML (*.html *.htm);;All files (*.*)"_s );
171 if ( !file.isEmpty() )
173 const QUrl url = QUrl::fromLocalFile( file );
174 mUrlLineEdit->setText( url.toString() );
175 mUrlLineEdit_editingFinished();
177 s.
setValue( u
"/UI/lastHtmlDir"_s, QFileInfo( file ).absolutePath() );
181void QgsLayoutHtmlWidget::mResizeModeComboBox_currentIndexChanged(
int index )
188 mHtml->beginCommand( tr(
"Change Resize Mode" ) );
195void QgsLayoutHtmlWidget::mEvaluateExpressionsCheckbox_toggled(
bool checked )
202 blockSignals(
true );
203 mHtml->beginCommand( tr(
"Change Evaluate Expressions" ) );
204 mHtml->setEvaluateExpressions( checked );
206 blockSignals(
false );
209void QgsLayoutHtmlWidget::mUseSmartBreaksCheckBox_toggled(
bool checked )
216 blockSignals(
true );
217 mHtml->beginCommand( tr(
"Change Smart Breaks" ) );
218 mHtml->setUseSmartBreaks( checked );
220 blockSignals(
false );
223void QgsLayoutHtmlWidget::mMaxDistanceSpinBox_valueChanged(
double val )
230 blockSignals(
true );
232 mHtml->setMaxBreakDistance( val );
234 blockSignals(
false );
237void QgsLayoutHtmlWidget::htmlEditorChanged()
244 blockSignals(
true );
246 mHtml->setHtml( mHtmlEditor->text() );
248 blockSignals(
false );
251void QgsLayoutHtmlWidget::stylesheetEditorChanged()
258 blockSignals(
true );
260 mHtml->setUserStylesheet( mStylesheetEditor->text() );
262 blockSignals(
false );
265void QgsLayoutHtmlWidget::mUserStylesheetCheckBox_toggled(
bool checked )
272 blockSignals(
true );
273 mHtml->beginCommand( tr(
"Toggle User Stylesheet" ) );
274 mHtml->setUserStylesheetEnabled( checked );
276 blockSignals(
false );
279void QgsLayoutHtmlWidget::mEmptyFrameCheckBox_toggled(
bool checked )
286 mFrame->beginCommand( tr(
"Toggle Empty Frame Mode" ) );
287 mFrame->setHidePageIfEmpty( checked );
288 mFrame->endCommand();
291void QgsLayoutHtmlWidget::mHideEmptyBgCheckBox_toggled(
bool checked )
298 mFrame->beginCommand( tr(
"Toggle Hide Background" ) );
299 mFrame->setHideBackgroundIfEmpty( checked );
300 mFrame->endCommand();
303void QgsLayoutHtmlWidget::mRadioManualSource_clicked(
bool checked )
310 blockSignals(
true );
311 mHtml->beginCommand( tr(
"Change HTML Source" ) );
313 blockSignals(
false );
315 mHtmlEditor->setEnabled( checked );
316 mInsertExpressionButton->setEnabled( checked );
317 mUrlLineEdit->setEnabled( !checked );
318 mFileToolButton->setEnabled( !checked );
324void QgsLayoutHtmlWidget::mRadioUrlSource_clicked(
bool checked )
331 blockSignals(
true );
332 mHtml->beginCommand( tr(
"Change HTML Source" ) );
334 blockSignals(
false );
336 mHtmlEditor->setEnabled( !checked );
337 mInsertExpressionButton->setEnabled( !checked );
338 mUrlLineEdit->setEnabled( checked );
339 mFileToolButton->setEnabled( checked );
345void QgsLayoutHtmlWidget::mInsertExpressionButton_clicked()
357 const QgsExpressionContext context = mHtml->createExpressionContext();
358 QgsExpressionBuilderDialog exprDlg( layer, expression,
this, u
"generic"_s, context );
359 exprDlg.setWindowTitle( tr(
"Insert Expression" ) );
360 if ( exprDlg.exec() == QDialog::Accepted )
362 expression = exprDlg.expressionText();
363 if ( !expression.isEmpty() )
365 blockSignals(
true );
366 mHtml->beginCommand( tr(
"Change HTML Source" ) );
367 mHtmlEditor->insertText(
"[%" + expression.trimmed() +
"%]" );
368 mHtml->setHtml( mHtmlEditor->text() );
370 blockSignals(
false );
375void QgsLayoutHtmlWidget::mReloadPushButton_clicked()
382 if ( mHtml->layout() )
383 mHtml->layout()->undoStack()->blockCommands(
true );
385 if ( mHtml->layout() )
386 mHtml->layout()->undoStack()->blockCommands(
false );
389void QgsLayoutHtmlWidget::mAddFramePushButton_clicked()
391 if ( !mHtml || !mFrame )
397 QPointF pos = mFrame->pos();
399 pos.ry() += mFrame->rect().height() + 10;
401 QgsLayoutFrame *newFrame = mHtml->createNewFrame( mFrame, pos, mFrame->rect().size() );
402 mHtml->recalculateFrameSizes();
405 if ( QgsLayout *layout = mHtml->layout() )
407 layout->setSelectedItem( newFrame );
411void QgsLayoutHtmlWidget::setGuiElementValues()
413 if ( !mHtml || !mFrame )
418 blockSignals(
true );
419 mUrlLineEdit->setText( mHtml->url().toString() );
420 mResizeModeComboBox->setCurrentIndex( mResizeModeComboBox->findData( mHtml->resizeMode() ) );
421 mEvaluateExpressionsCheckbox->setChecked( mHtml->evaluateExpressions() );
422 mUseSmartBreaksCheckBox->setChecked( mHtml->useSmartBreaks() );
423 mMaxDistanceSpinBox->setValue( mHtml->maxBreakDistance() );
426 mHtmlEditor->setText( mHtml->html() );
435 mUserStylesheetCheckBox->setChecked( mHtml->userStylesheetEnabled() );
436 mStylesheetEditor->setText( mHtml->userStylesheet() );
438 mEmptyFrameCheckBox->setChecked( mFrame->hidePageIfEmpty() );
439 mHideEmptyBgCheckBox->setChecked( mFrame->hideBackgroundIfEmpty() );
443 blockSignals(
false );
451 mUrlLineEdit->setEnabled( !mUrlDDBtn->isActive() );
A CSS editor based on QScintilla2.
A HTML editor based on QScintilla2.
static QString findAndSelectActiveExpression(QgsCodeEditor *editor, const QString &pattern=QString())
Find the expression under the cursor in the given editor and select it.
Base class for frame items, which form a layout multiframe item.
QgsLayoutMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
A layout multiframe subclass for HTML content.
@ ManualHtml
HTML content is manually set for the item.
@ Url
Using this mode item fetches its content via a url.
@ LayoutHtml
Html multiframe item.
Base class for graphical items within a QgsLayout.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
virtual int type() const =0
Returns unique multiframe type id.
ResizeMode
Specifies the behavior for creating new frames to fit the multiframe's content.
@ UseExistingFrames
Don't automatically create new frames, just use existing frames.
@ RepeatOnEveryPage
Repeats the same frame on every page.
@ ExtendToNextPage
Creates new full page frames on the following page(s) until the entire multiframe content is visible.
@ UndoHtmlBreakDistance
HTML page break distance.
@ UndoHtmlStylesheet
HTML stylesheet.
@ UndoHtmlSource
HTML source.
void changed()
Emitted when the object's properties change.
@ SourceUrl
Html source url.
Interface for master layout type objects, such as print layouts and reports.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.