31#include "moc_qgsgeometrywidget.cpp"
36 QHBoxLayout *layout =
new QHBoxLayout();
37 layout->setContentsMargins( 0, 0, 0, 0 );
39 mLineEdit =
new QLineEdit();
40 mLineEdit->setReadOnly(
true );
41 mLineEdit->setStyleSheet( QStringLiteral(
"font-style: italic;" ) );
44 QPalette palette = mLineEdit->palette();
45 palette.setColor( QPalette::Text, palette.color( QPalette::Disabled, QPalette::Text ) );
46 mLineEdit->setPalette( palette );
48 mLineEdit->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
50 mButton =
new QToolButton();
51 mButton->setStyleSheet( QStringLiteral(
"QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }" ) );
53 mButton->setIconSize( QSize( iconSize, iconSize ) );
55 mButton->setFixedSize( 2 *
static_cast<int>( 1.25 * iconSize / 2.0 ), 2 *
static_cast<int>( iconSize * 1.1 / 2.0 ) );
57 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
60 layout->addWidget( mLineEdit, 1 );
61 layout->addWidget( mButton );
65 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
67 setFocusProxy( mLineEdit );
69 mMenu =
new QMenu(
this );
71 mCopyWktAction =
new QAction( mMenu );
72 mCopyWktAction->setText( tr(
"Copy as WKT" ) );
75 mCopyGeoJsonAction =
new QAction( mMenu );
76 mCopyGeoJsonAction->setText( tr(
"Copy as GeoJSON" ) );
79 mPasteAction =
new QAction( mMenu );
80 mPasteAction->setText( tr(
"Paste Geometry" ) );
81 connect( mPasteAction, &QAction::triggered,
this, &QgsGeometryWidget::pasteTriggered );
83 mClearAction =
new QAction( mMenu );
84 mClearAction->setText( tr(
"Clear" ) );
87 mMenu->addAction( mCopyWktAction );
88 mMenu->addAction( mCopyGeoJsonAction );
89 mMenu->addSeparator();
90 mMenu->addAction( mPasteAction );
91 mMenu->addAction( mClearAction );
92 connect( mMenu, &QMenu::aboutToShow,
this, &QgsGeometryWidget::prepareMenu );
94 mButton->setMenu( mMenu );
95 mButton->setPopupMode( QToolButton::InstantPopup );
102 if ( geometry == mGeometry )
105 if ( !typeIsAcceptable( geometry.
wkbType() ) )
110 mGeometry = geometry;
122 mAcceptedTypes = types;
127 return mAcceptedTypes;
137 mReadOnly = readOnly;
142 if ( mGeometry.isNull() )
150 if ( mGeometry.isNull() )
153 QApplication::clipboard()->setText( mGeometry.asWkt() );
158 if ( mGeometry.isNull() )
161 QApplication::clipboard()->setText( mGeometry.asJson() );
164void QgsGeometryWidget::pasteTriggered()
166 if ( !mPastedGeom.
isNull() )
172 defaultCrs = mGeometry.
crs();
175 crsSelector.setWindowTitle( tr(
"Paste Geometry" ) );
176 crsSelector.setMessage( tr(
"Please specify the Coordinate Reference System (CRS) for the pasted geometry." ) );
177 crsSelector.setCrs( defaultCrs );
178 if ( crsSelector.exec() )
180 mPastedGeom.
setCrs( crsSelector.crs() );
187void QgsGeometryWidget::fetchGeomFromClipboard()
189 mPastedGeom = QgsReferencedGeometry();
193 const QString text = QApplication::clipboard()->text();
194 if ( text.isEmpty() )
198 mPastedGeom = QgsReferencedGeometry(
QgsGeometry::fromWkt( text ), QgsCoordinateReferenceSystem() );
199 if ( !mPastedGeom.isNull() && typeIsAcceptable( mPastedGeom.wkbType() ) )
203 mPastedGeom = QgsReferencedGeometry();
206 const QStringList lines = text.split(
"\n", Qt::SkipEmptyParts );
207 if ( !lines.isEmpty() )
209 for (
const QString &line : lines )
212 if ( !geometry.
isNull() && typeIsAcceptable( geometry.
wkbType() ) )
214 mPastedGeom = QgsReferencedGeometry( geometry, QgsCoordinateReferenceSystem() );
222 if ( !features.isEmpty() && features.at( 0 ).hasGeometry() )
225 mPastedGeom = QgsReferencedGeometry( features.at( 0 ).geometry(), QgsCoordinateReferenceSystem( QStringLiteral(
"EPSG:4326" ) ) );
230bool QgsGeometryWidget::typeIsAcceptable(
Qgis::WkbType type )
const
232 if ( mAcceptedTypes.isEmpty() )
237 return mAcceptedTypes.contains( type );
240void QgsGeometryWidget::prepareMenu()
242 fetchGeomFromClipboard();
244 mCopyGeoJsonAction->setEnabled( !mGeometry.isNull() );
245 mCopyWktAction->setEnabled( !mGeometry.isNull() );
246 mClearAction->setEnabled( !mGeometry.isNull() && !mReadOnly );
247 mPasteAction->setEnabled( !mPastedGeom.isNull() && !mReadOnly );
250void QgsGeometryWidget::updateLineEdit()
252 if ( mGeometry.isNull() )
258 QString wkt = mGeometry.asWkt();
259 if ( wkt.length() >= 1050 )
264 if ( mGeometry.crs().isValid() )
266 mLineEdit->setText( QStringLiteral(
"%1 [%2]" ).arg( wkt, mGeometry.crs().userFriendlyIdentifier() ) );
270 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