32#include "moc_qgsgeometrywidget.cpp"
34using namespace Qt::StringLiterals;
39 QHBoxLayout *layout =
new QHBoxLayout();
40 layout->setContentsMargins( 0, 0, 0, 0 );
42 mLineEdit =
new QLineEdit();
43 mLineEdit->setReadOnly(
true );
44 mLineEdit->setStyleSheet( u
"font-style: italic;"_s );
47 QPalette palette = mLineEdit->palette();
48 palette.setColor( QPalette::Text, palette.color( QPalette::Disabled, QPalette::Text ) );
49 mLineEdit->setPalette( palette );
51 mLineEdit->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
53 mButton =
new QToolButton();
54 mButton->setStyleSheet( u
"QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }"_s );
56 mButton->setIconSize( QSize( iconSize, iconSize ) );
58 mButton->setFixedSize( 2 *
static_cast<int>( 1.25 * iconSize / 2.0 ), 2 *
static_cast<int>( iconSize * 1.1 / 2.0 ) );
60 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
63 layout->addWidget( mLineEdit, 1 );
64 layout->addWidget( mButton );
68 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
70 setFocusProxy( mLineEdit );
72 mMenu =
new QMenu(
this );
74 mCopyWktAction =
new QAction( mMenu );
75 mCopyWktAction->setText( tr(
"Copy as WKT" ) );
78 mCopyGeoJsonAction =
new QAction( mMenu );
79 mCopyGeoJsonAction->setText( tr(
"Copy as GeoJSON" ) );
82 mPasteAction =
new QAction( mMenu );
83 mPasteAction->setText( tr(
"Paste Geometry" ) );
84 connect( mPasteAction, &QAction::triggered,
this, &QgsGeometryWidget::pasteTriggered );
86 mClearAction =
new QAction( mMenu );
87 mClearAction->setText( tr(
"Clear" ) );
90 mMenu->addAction( mCopyWktAction );
91 mMenu->addAction( mCopyGeoJsonAction );
92 mMenu->addSeparator();
93 mMenu->addAction( mPasteAction );
94 mMenu->addAction( mClearAction );
95 connect( mMenu, &QMenu::aboutToShow,
this, &QgsGeometryWidget::prepareMenu );
97 mButton->setMenu( mMenu );
98 mButton->setPopupMode( QToolButton::InstantPopup );
105 if ( geometry == mGeometry )
108 if ( !typeIsAcceptable( geometry.
wkbType() ) )
113 mGeometry = geometry;
125 mAcceptedTypes = types;
130 return mAcceptedTypes;
140 mReadOnly = readOnly;
145 if ( mGeometry.isNull() )
153 if ( mGeometry.isNull() )
156 QApplication::clipboard()->setText( mGeometry.asWkt() );
161 if ( mGeometry.isNull() )
164 QApplication::clipboard()->setText( mGeometry.asJson() );
167void QgsGeometryWidget::pasteTriggered()
169 if ( !mPastedGeom.
isNull() )
175 defaultCrs = mGeometry.
crs();
178 crsSelector.setWindowTitle( tr(
"Paste Geometry" ) );
179 crsSelector.setMessage( tr(
"Please specify the Coordinate Reference System (CRS) for the pasted geometry." ) );
180 crsSelector.setCrs( defaultCrs );
181 if ( crsSelector.exec() )
183 mPastedGeom.
setCrs( crsSelector.crs() );
190void QgsGeometryWidget::fetchGeomFromClipboard()
192 mPastedGeom = QgsReferencedGeometry();
196 const QString text = QApplication::clipboard()->text();
197 if ( text.isEmpty() )
201 mPastedGeom = QgsReferencedGeometry(
QgsGeometry::fromWkt( text ), QgsCoordinateReferenceSystem() );
202 if ( !mPastedGeom.isNull() && typeIsAcceptable( mPastedGeom.wkbType() ) )
206 mPastedGeom = QgsReferencedGeometry();
209 const QStringList lines = text.split(
"\n", Qt::SkipEmptyParts );
210 if ( !lines.isEmpty() )
212 for (
const QString &line : lines )
215 if ( !geometry.
isNull() && typeIsAcceptable( geometry.
wkbType() ) )
217 mPastedGeom = QgsReferencedGeometry( geometry, QgsCoordinateReferenceSystem() );
225 if ( !features.isEmpty() && features.at( 0 ).hasGeometry() )
228 mPastedGeom = QgsReferencedGeometry( features.at( 0 ).geometry(), QgsCoordinateReferenceSystem( u
"EPSG:4326"_s ) );
233bool QgsGeometryWidget::typeIsAcceptable(
Qgis::WkbType type )
const
235 if ( mAcceptedTypes.isEmpty() )
240 return mAcceptedTypes.contains( type );
243void QgsGeometryWidget::prepareMenu()
245 fetchGeomFromClipboard();
247 mCopyGeoJsonAction->setEnabled( !mGeometry.isNull() );
248 mCopyWktAction->setEnabled( !mGeometry.isNull() );
249 mClearAction->setEnabled( !mGeometry.isNull() && !mReadOnly );
250 mPasteAction->setEnabled( !mPastedGeom.isNull() && !mReadOnly );
253void QgsGeometryWidget::updateLineEdit()
255 if ( mGeometry.isNull() )
261 QString wkt = mGeometry.asWkt();
262 if ( wkt.length() >= 1050 )
267 if ( mGeometry.crs().isValid() )
269 mLineEdit->setText( u
"%1 [%2]"_s.arg( wkt, mGeometry.crs().userFriendlyIdentifier() ) );
273 mLineEdit->setText( wkt );
WkbType
The WKB type describes the number of dimensions a geometry has.
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
static constexpr int MAX_WKT_LENGTH
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields=QgsFields(), QTextCodec *encoding SIP_PYARGREMOVE6=nullptr)
Attempts to parse a GeoJSON string to a collection of features.
A generic dialog to prompt the user for a Coordinate Reference System.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the associated crs.
A QgsGeometry with associated coordinate reference system.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
QList< QgsFeature > QgsFeatureList