QGIS API Documentation 3.41.0-Master (57ec4277f5e)
Loading...
Searching...
No Matches
qgsgeometrywidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometrywidget.cpp
3 --------------------------------------
4 Date : March 2015
5 Copyright : (C) 2015 Nyall Dawson
7***************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
16#include "qgsgeometrywidget.h"
17#include "moc_qgsgeometrywidget.cpp"
18#include "qgsapplication.h"
19#include "qgsgeometry.h"
20#include "qgsjsonutils.h"
21#include "qgsguiutils.h"
23#include <QLineEdit>
24#include <QHBoxLayout>
25#include <QToolButton>
26#include <QMenu>
27#include <QAction>
28#include <QClipboard>
29
31 : QWidget( parent )
32{
33 QHBoxLayout *layout = new QHBoxLayout();
34 layout->setContentsMargins( 0, 0, 0, 0 );
35
36 mLineEdit = new QLineEdit();
37 mLineEdit->setReadOnly( true );
38 mLineEdit->setStyleSheet( QStringLiteral( "font-style: italic;" ) );
39
40 // make text appear in disabled text color, as it's not directly editable
41 QPalette palette = mLineEdit->palette();
42 palette.setColor( QPalette::Text, palette.color( QPalette::Disabled, QPalette::Text ) );
43 mLineEdit->setPalette( palette );
44
45 mLineEdit->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
46
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); }" ) );
49 const int iconSize = QgsGuiUtils::scaleIconSize( 24 );
50 mButton->setIconSize( QSize( iconSize, iconSize ) );
51 // button width is 1.25 * icon size, height 1.1 * icon size. But we round to ensure even pixel sizes for equal margins
52 mButton->setFixedSize( 2 * static_cast<int>( 1.25 * iconSize / 2.0 ), 2 * static_cast<int>( iconSize * 1.1 / 2.0 ) );
53
54 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
55 mButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionOffsetCurve.svg" ) ) );
56
57 layout->addWidget( mLineEdit, 1 );
58 layout->addWidget( mButton );
59
60 setLayout( layout );
61
62 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
63
64 setFocusProxy( mLineEdit );
65
66 mMenu = new QMenu( this );
67
68 mCopyWktAction = new QAction( mMenu );
69 mCopyWktAction->setText( tr( "Copy as WKT" ) );
70 connect( mCopyWktAction, &QAction::triggered, this, &QgsGeometryWidget::copyAsWkt );
71
72 mCopyGeoJsonAction = new QAction( mMenu );
73 mCopyGeoJsonAction->setText( tr( "Copy as GeoJSON" ) );
74 connect( mCopyGeoJsonAction, &QAction::triggered, this, &QgsGeometryWidget::copyAsGeoJson );
75
76 mPasteAction = new QAction( mMenu );
77 mPasteAction->setText( tr( "Paste Geometry" ) );
78 connect( mPasteAction, &QAction::triggered, this, &QgsGeometryWidget::pasteTriggered );
79
80 mClearAction = new QAction( mMenu );
81 mClearAction->setText( tr( "Clear" ) );
82 connect( mClearAction, &QAction::triggered, this, &QgsGeometryWidget::clearGeometry );
83
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 );
90
91 mButton->setMenu( mMenu );
92 mButton->setPopupMode( QToolButton::InstantPopup );
93
94 updateLineEdit();
95}
96
98{
99 if ( geometry == mGeometry )
100 return;
101
102 if ( !typeIsAcceptable( geometry.wkbType() ) )
103 {
104 return;
105 }
106
107 mGeometry = geometry;
108 updateLineEdit();
109 emit geometryValueChanged( mGeometry );
110}
111
113{
114 return mGeometry;
115}
116
117void QgsGeometryWidget::setAcceptedWkbTypes( const QList<Qgis::WkbType> &types )
118{
119 mAcceptedTypes = types;
120}
121
122QList<Qgis::WkbType> QgsGeometryWidget::acceptedWkbTypes() const
123{
124 return mAcceptedTypes;
125}
126
128{
129 return mReadOnly;
130}
131
133{
134 mReadOnly = readOnly;
135}
136
138{
139 if ( mGeometry.isNull() )
140 return;
141
143}
144
146{
147 if ( mGeometry.isNull() )
148 return;
149
150 QApplication::clipboard()->setText( mGeometry.asWkt() );
151}
152
154{
155 if ( mGeometry.isNull() )
156 return;
157
158 QApplication::clipboard()->setText( mGeometry.asJson() );
159}
160
161void QgsGeometryWidget::pasteTriggered()
162{
163 if ( !mPastedGeom.isNull() )
164 {
165 QgsCoordinateReferenceSystem defaultCrs = mPastedGeom.crs();
166
167 // default to CRS of current geometry, if we have no better guesses as to what the clipboard CRS is
168 if ( !defaultCrs.isValid() )
169 defaultCrs = mGeometry.crs();
170
171 QgsProjectionSelectionDialog crsSelector( this );
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() )
176 {
177 mPastedGeom.setCrs( crsSelector.crs() );
178 setGeometryValue( mPastedGeom );
179 mPastedGeom = QgsReferencedGeometry();
180 }
181 }
182}
183
184void QgsGeometryWidget::fetchGeomFromClipboard()
185{
186 mPastedGeom = QgsReferencedGeometry();
187 if ( mReadOnly )
188 return;
189
190 const QString text = QApplication::clipboard()->text();
191 if ( text.isEmpty() )
192 return;
193
194 //try reading as a single wkt string
196 if ( !mPastedGeom.isNull() && typeIsAcceptable( mPastedGeom.wkbType() ) )
197 {
198 return;
199 }
200 mPastedGeom = QgsReferencedGeometry();
201
202 //try reading as a list
203 const QStringList lines = text.split( "\n", Qt::SkipEmptyParts );
204 if ( !lines.isEmpty() )
205 {
206 for ( const QString &line : lines )
207 {
208 const QgsGeometry geometry = QgsGeometry::fromWkt( line );
209 if ( !geometry.isNull() && typeIsAcceptable( geometry.wkbType() ) )
210 {
211 mPastedGeom = QgsReferencedGeometry( geometry, QgsCoordinateReferenceSystem() );
212 return;
213 }
214 }
215 }
216
217 // try reading a GeoJSON
218 const QgsFeatureList features = QgsJsonUtils::stringToFeatureList( text );
219 if ( !features.isEmpty() && features.at( 0 ).hasGeometry() )
220 {
221 // assume EPSG:4326 for GeoJSON
222 mPastedGeom = QgsReferencedGeometry( features.at( 0 ).geometry(), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
223 return;
224 }
225}
226
227bool QgsGeometryWidget::typeIsAcceptable( Qgis::WkbType type ) const
228{
229 if ( mAcceptedTypes.isEmpty() )
230 {
231 return true;
232 }
233
234 return mAcceptedTypes.contains( type );
235}
236
237void QgsGeometryWidget::prepareMenu()
238{
239 fetchGeomFromClipboard();
240
241 mCopyGeoJsonAction->setEnabled( !mGeometry.isNull() );
242 mCopyWktAction->setEnabled( !mGeometry.isNull() );
243 mClearAction->setEnabled( !mGeometry.isNull() && !mReadOnly );
244 mPasteAction->setEnabled( !mPastedGeom.isNull() && !mReadOnly );
245}
246
247void QgsGeometryWidget::updateLineEdit()
248{
249 if ( mGeometry.isNull() )
250 {
251 mLineEdit->setText( QgsApplication::nullRepresentation() );
252 }
253 else
254 {
255 QString wkt = mGeometry.asWkt();
256 if ( wkt.length() >= 1050 )
257 {
258 wkt = wkt.left( QgsField::MAX_WKT_LENGTH ) + QChar( 0x2026 );
259 }
260
261 if ( mGeometry.crs().isValid() )
262 {
263 mLineEdit->setText( QStringLiteral( "%1 [%2]" ).arg( wkt, mGeometry.crs().userFriendlyIdentifier() ) );
264 }
265 else
266 {
267 mLineEdit->setText( wkt );
268 }
269 }
270}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
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
Definition qgsfield.h:557
void setGeometryValue(const QgsReferencedGeometry &geometry)
Sets the current geometry value for the widget.
QgsReferencedGeometry geometryValue
void setReadOnly(bool readOnly)
Sets whether the widget should be in a read-only state.
QgsGeometryWidget(QWidget *parent=nullptr)
Constructor for QgsGeometryWidget, with the specified parent widget.
void copyAsWkt()
Copies the current geometry value to the clipboard, as a WKT string.
void setAcceptedWkbTypes(const QList< Qgis::WkbType > &types)
Sets the list of WKB geometry types which are permitted for the widget.
QList< Qgis::WkbType > acceptedWkbTypes() const
Returns the list of WKB geometry types which are permitted for the widget.
void clearGeometry()
Clears the current geometry value stored in the widget.
void geometryValueChanged(const QgsReferencedGeometry &value)
Emitted whenever the geometry value of the widget is changed.
bool isReadOnly() const
Returns whether the widget is in a read-only state.
void copyAsGeoJson()
Copies the current geometry value to the clipboard, as a GeoJSON string.
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