17#include "moc_qgsgeometrywidget.cpp"
33 QHBoxLayout *layout =
new QHBoxLayout();
34 layout->setContentsMargins( 0, 0, 0, 0 );
36 mLineEdit =
new QLineEdit();
37 mLineEdit->setReadOnly(
true );
38 mLineEdit->setStyleSheet( QStringLiteral(
"font-style: italic;" ) );
41 QPalette palette = mLineEdit->palette();
42 palette.setColor( QPalette::Text, palette.color( QPalette::Disabled, QPalette::Text ) );
43 mLineEdit->setPalette( palette );
45 mLineEdit->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
47 mButton =
new QToolButton();
48 mButton->setStyleSheet( QStringLiteral(
"QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }" ) );
50 mButton->setIconSize( QSize( iconSize, iconSize ) );
52 mButton->setFixedSize( 2 *
static_cast< int >( 1.25 * iconSize / 2.0 ), 2 *
static_cast< int >( iconSize * 1.1 / 2.0 ) );
54 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
57 layout->addWidget( mLineEdit, 1 );
58 layout->addWidget( mButton );
62 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
64 setFocusProxy( mLineEdit );
66 mMenu =
new QMenu(
this );
68 mCopyWktAction =
new QAction( mMenu );
69 mCopyWktAction->setText( tr(
"Copy as WKT" ) );
72 mCopyGeoJsonAction =
new QAction( mMenu );
73 mCopyGeoJsonAction->setText( tr(
"Copy as GeoJSON" ) );
76 mPasteAction =
new QAction( mMenu );
77 mPasteAction->setText( tr(
"Paste Geometry" ) );
78 connect( mPasteAction, &QAction::triggered,
this, &QgsGeometryWidget::pasteTriggered );
80 mClearAction =
new QAction( mMenu );
81 mClearAction->setText( tr(
"Clear" ) );
84 mMenu->addAction( mCopyWktAction );
85 mMenu->addAction( mCopyGeoJsonAction );
86 mMenu->addSeparator();
87 mMenu->addAction( mPasteAction );
88 mMenu->addAction( mClearAction );
89 connect( mMenu, &QMenu::aboutToShow,
this, &QgsGeometryWidget::prepareMenu );
91 mButton->setMenu( mMenu );
92 mButton->setPopupMode( QToolButton::InstantPopup );
99 if ( geometry == mGeometry )
102 if ( !typeIsAcceptable( geometry.
wkbType() ) )
107 mGeometry = geometry;
119 mAcceptedTypes = types;
124 return mAcceptedTypes;
134 mReadOnly = readOnly;
150 QApplication::clipboard()->setText( mGeometry.
asWkt() );
158 QApplication::clipboard()->setText( mGeometry.
asJson() );
161void QgsGeometryWidget::pasteTriggered()
163 if ( !mPastedGeom.
isNull() )
169 defaultCrs = mGeometry.
crs();
172 crsSelector.setWindowTitle( tr(
"Paste Geometry" ) );
173 crsSelector.setMessage( tr(
"Please specify the Coordinate Reference System (CRS) for the pasted geometry." ) );
174 crsSelector.setCrs( defaultCrs );
175 if ( crsSelector.exec() )
177 mPastedGeom.
setCrs( crsSelector.crs() );
184void QgsGeometryWidget::fetchGeomFromClipboard()
190 const QString text = QApplication::clipboard()->text();
191 if ( text.isEmpty() )
196 if ( !mPastedGeom.
isNull() && typeIsAcceptable( mPastedGeom.
wkbType() ) )
203 const QStringList lines = text.split(
"\n", Qt::SkipEmptyParts );
204 if ( !lines.isEmpty() )
206 for (
const QString &line : lines )
209 if ( !geometry.
isNull() && typeIsAcceptable( geometry.
wkbType() ) )
219 if ( !features.isEmpty() && features.at( 0 ).hasGeometry() )
227bool QgsGeometryWidget::typeIsAcceptable(
Qgis::WkbType type )
const
229 if ( mAcceptedTypes.isEmpty() )
234 return mAcceptedTypes.contains( type );
237void QgsGeometryWidget::prepareMenu()
239 fetchGeomFromClipboard();
241 mCopyGeoJsonAction->setEnabled( !mGeometry.
isNull() );
242 mCopyWktAction->setEnabled( !mGeometry.
isNull() );
243 mClearAction->setEnabled( !mGeometry.
isNull() && !mReadOnly );
244 mPasteAction->setEnabled( !mPastedGeom.
isNull() && !mReadOnly );
247void QgsGeometryWidget::updateLineEdit()
255 QString wkt = mGeometry.
asWkt();
256 if ( wkt.length() >= 1050 )
263 mLineEdit->setText( QStringLiteral(
"%1 [%2]" ).arg( wkt,
268 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