32 QHBoxLayout *layout =
new QHBoxLayout();
33 layout->setContentsMargins( 0, 0, 0, 0 );
35 mLineEdit =
new QLineEdit();
36 mLineEdit->setReadOnly(
true );
37 mLineEdit->setStyleSheet( QStringLiteral(
"font-style: italic;" ) );
40 QPalette palette = mLineEdit->palette();
41 palette.setColor( QPalette::Text, palette.color( QPalette::Disabled, QPalette::Text ) );
42 mLineEdit->setPalette( palette );
44 mLineEdit->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
46 mButton =
new QToolButton();
47 mButton->setStyleSheet( QStringLiteral(
"QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }" ) );
49 mButton->setIconSize( QSize( iconSize, iconSize ) );
51 mButton->setFixedSize( 2 *
static_cast< int >( 1.25 * iconSize / 2.0 ), 2 *
static_cast< int >( iconSize * 1.1 / 2.0 ) );
53 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
56 layout->addWidget( mLineEdit, 1 );
57 layout->addWidget( mButton );
61 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
63 setFocusProxy( mLineEdit );
65 mMenu =
new QMenu(
this );
67 mCopyWktAction =
new QAction( mMenu );
68 mCopyWktAction->setText( tr(
"Copy as WKT" ) );
71 mCopyGeoJsonAction =
new QAction( mMenu );
72 mCopyGeoJsonAction->setText( tr(
"Copy as GeoJSON" ) );
75 mPasteAction =
new QAction( mMenu );
76 mPasteAction->setText( tr(
"Paste Geometry" ) );
77 connect( mPasteAction, &QAction::triggered,
this, &QgsGeometryWidget::pasteTriggered );
79 mClearAction =
new QAction( mMenu );
80 mClearAction->setText( tr(
"Clear" ) );
83 mMenu->addAction( mCopyWktAction );
84 mMenu->addAction( mCopyGeoJsonAction );
85 mMenu->addSeparator();
86 mMenu->addAction( mPasteAction );
87 mMenu->addAction( mClearAction );
88 connect( mMenu, &QMenu::aboutToShow,
this, &QgsGeometryWidget::prepareMenu );
90 mButton->setMenu( mMenu );
91 mButton->setPopupMode( QToolButton::InstantPopup );
98 if ( geometry == mGeometry )
101 if ( !typeIsAcceptable( geometry.
wkbType() ) )
106 mGeometry = geometry;
118 mAcceptedTypes = types;
123 return mAcceptedTypes;
133 mReadOnly = readOnly;
149 QApplication::clipboard()->setText( mGeometry.
asWkt() );
157 QApplication::clipboard()->setText( mGeometry.
asJson() );
160void QgsGeometryWidget::pasteTriggered()
162 if ( !mPastedGeom.
isNull() )
168 defaultCrs = mGeometry.
crs();
171 crsSelector.setWindowTitle( tr(
"Paste Geometry" ) );
172 crsSelector.setMessage( tr(
"Please specify the Coordinate Reference System (CRS) for the pasted geometry." ) );
173 crsSelector.setCrs( defaultCrs );
174 if ( crsSelector.exec() )
176 mPastedGeom.
setCrs( crsSelector.crs() );
183void QgsGeometryWidget::fetchGeomFromClipboard()
189 const QString text = QApplication::clipboard()->text();
190 if ( text.isEmpty() )
195 if ( !mPastedGeom.
isNull() && typeIsAcceptable( mPastedGeom.
wkbType() ) )
202 const QStringList lines = text.split(
"\n", Qt::SkipEmptyParts );
203 if ( !lines.isEmpty() )
205 for (
const QString &line : lines )
208 if ( !geometry.
isNull() && typeIsAcceptable( geometry.
wkbType() ) )
218 if ( !features.isEmpty() && features.at( 0 ).hasGeometry() )
226bool QgsGeometryWidget::typeIsAcceptable(
Qgis::WkbType type )
const
228 if ( mAcceptedTypes.isEmpty() )
233 return mAcceptedTypes.contains( type );
236void QgsGeometryWidget::prepareMenu()
238 fetchGeomFromClipboard();
240 mCopyGeoJsonAction->setEnabled( !mGeometry.
isNull() );
241 mCopyWktAction->setEnabled( !mGeometry.
isNull() );
242 mClearAction->setEnabled( !mGeometry.
isNull() && !mReadOnly );
243 mPasteAction->setEnabled( !mPastedGeom.
isNull() && !mReadOnly );
246void QgsGeometryWidget::updateLineEdit()
254 QString wkt = mGeometry.
asWkt();
255 if ( wkt.length() >= 1050 )
262 mLineEdit->setText( QStringLiteral(
"%1 [%2]" ).arg( wkt,
267 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.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString userFriendlyIdentifier(Qgis::CrsIdentifierType type=Qgis::CrsIdentifierType::MediumString) const
Returns a user friendly identifier for the CRS.
static constexpr int MAX_WKT_LENGTH
A geometry is the spatial representation of a feature.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QString asJson(int precision=17) const
Exports the geometry to a GeoJSON string.
Q_INVOKABLE QString asWkt(int precision=17) const
Exports the geometry to WKT.
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