Skip to content
Permalink
Browse files
[ui][annotations] Add insert expression button in the point text widget
  • Loading branch information
nirvn authored and nyalldawson committed Sep 10, 2021
1 parent b111f51 commit 3b07f3b9486e770a68a03286252269c8bc53ce8a
@@ -23,6 +23,7 @@
#include "qgsannotationlineitem.h"
#include "qgsannotationmarkeritem.h"
#include "qgsannotationpointtextitem.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgstextformatwidget.h"

///@cond PRIVATE
@@ -319,6 +320,7 @@ QgsAnnotationPointTextItemWidget::QgsAnnotationPointTextItemWidget( QWidget *par
if ( !mBlockChangedSignal )
emit itemChanged();
} );
connect( mInsertExpressionButton, &QPushButton::clicked, this, &QgsAnnotationPointTextItemWidget::mInsertExpressionButton_clicked );
connect( mPropertiesWidget, &QgsAnnotationItemCommonPropertiesWidget::itemChanged, this, [ = ]
{
if ( !mBlockChangedSignal )
@@ -385,5 +387,35 @@ bool QgsAnnotationPointTextItemWidget::setNewItem( QgsAnnotationItem *item )
return true;
}

void QgsAnnotationPointTextItemWidget::mInsertExpressionButton_clicked()
{
QString selText = mTextEdit->textCursor().selectedText();

// html editor replaces newlines with Paragraph Separator characters - see https://github.com/qgis/QGIS/issues/27568
selText = selText.replace( QChar( 0x2029 ), QChar( '\n' ) );

// edit the selected expression if there's one
if ( selText.startsWith( QLatin1String( "[%" ) ) && selText.endsWith( QLatin1String( "%]" ) ) )
selText = selText.mid( 2, selText.size() - 4 );

QgsExpressionContext expressionContext;
if ( context().expressionContext() )
expressionContext = *( context().expressionContext() );
else
expressionContext = QgsProject::instance()->createExpressionContext();

QgsExpressionBuilderDialog exprDlg( nullptr, selText, this, QStringLiteral( "generic" ), expressionContext );

exprDlg.setWindowTitle( tr( "Insert Expression" ) );
if ( exprDlg.exec() == QDialog::Accepted )
{
QString expression = exprDlg.expressionText();
if ( !expression.isEmpty() )
{
mTextEdit->insertPlainText( "[%" + expression + "%]" );
}
}
}

///@endcond PRIVATE

@@ -128,6 +128,7 @@ class QgsAnnotationPointTextItemWidget : public QgsAnnotationItemBaseWidget, pri
bool setNewItem( QgsAnnotationItem *item ) override;

private:
void mInsertExpressionButton_clicked();

QgsTextFormatWidget *mTextFormatWidget = nullptr;
bool mBlockChangedSignal = false;
@@ -36,10 +36,32 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="1" column="0">
<widget class="QToolButton" name="mInsertExpressionButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Insert/Edit Expression…</string>
</property>
<property name="popupMode">
<enum>QToolButton::MenuButtonPopup</enum>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextOnly</enum>
</property>
<property name="arrowType">
<enum>Qt::DownArrow</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QgsAnnotationItemCommonPropertiesWidget" name="mPropertiesWidget" native="true"/>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QWidget" name="mTextFormatWidgetContainer" native="true"/>
</item>
</layout>

0 comments on commit 3b07f3b

Please sign in to comment.