QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsapplication.h"
19#include "qgsgeometry.h"
20#include "qgsguiutils.h"
21#include "qgsjsonutils.h"
23
24#include <QAction>
25#include <QClipboard>
26#include <QHBoxLayout>
27#include <QLineEdit>
28#include <QMenu>
29#include <QToolButton>
30
31#include "moc_qgsgeometrywidget.cpp"
32
34 : QWidget( parent )
35{
36 QHBoxLayout *layout = new QHBoxLayout();
37 layout->setContentsMargins( 0, 0, 0, 0 );
38
39 mLineEdit = new QLineEdit();
40 mLineEdit->setReadOnly( true );
41 mLineEdit->setStyleSheet( QStringLiteral( "font-style: italic;" ) );
42
43 // make text appear in disabled text color, as it's not directly editable
44 QPalette palette = mLineEdit->palette();
45 palette.setColor( QPalette::Text, palette.color( QPalette::Disabled, QPalette::Text ) );
46 mLineEdit->setPalette( palette );
47
48 mLineEdit->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
49
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); }" ) );
52 const int iconSize = QgsGuiUtils::scaleIconSize( 24 );
53 mButton->setIconSize( QSize( iconSize, iconSize ) );
54 // button width is 1.25 * icon size, height 1.1 * icon size. But we round to ensure even pixel sizes for equal margins
55 mButton->setFixedSize( 2 * static_cast<int>( 1.25 * iconSize / 2.0 ), 2 * static_cast<int>( iconSize * 1.1 / 2.0 ) );
56
57 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
58 mButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionOffsetCurve.svg" ) ) );
59
60 layout->addWidget( mLineEdit, 1 );
61 layout->addWidget( mButton );
62
63 setLayout( layout );
64
65 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
66
67 setFocusProxy( mLineEdit );
68
69 mMenu = new QMenu( this );
70
71 mCopyWktAction = new QAction( mMenu );
72 mCopyWktAction->setText( tr( "Copy as WKT" ) );
73 connect( mCopyWktAction, &QAction::triggered, this, &QgsGeometryWidget::copyAsWkt );
74
75 mCopyGeoJsonAction = new QAction( mMenu );
76 mCopyGeoJsonAction->setText( tr( "Copy as GeoJSON" ) );
77 connect( mCopyGeoJsonAction, &QAction::triggered, this, &QgsGeometryWidget::copyAsGeoJson );
78
79 mPasteAction = new QAction( mMenu );
80 mPasteAction->setText( tr( "Paste Geometry" ) );
81 connect( mPasteAction, &QAction::triggered, this, &QgsGeometryWidget::pasteTriggered );
82
83 mClearAction = new QAction( mMenu );
84 mClearAction->setText( tr( "Clear" ) );
85 connect( mClearAction, &QAction::triggered, this, &QgsGeometryWidget::clearGeometry );
86
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 );
93
94 mButton->setMenu( mMenu );
95 mButton->setPopupMode( QToolButton::InstantPopup );
96
97 updateLineEdit();
98}
99
101{
102 if ( geometry == mGeometry )
103 return;
104
105 if ( !typeIsAcceptable( geometry.wkbType() ) )
106 {
107 return;
108 }
109
110 mGeometry = geometry;
111 updateLineEdit();
112 emit geometryValueChanged( mGeometry );
113}
114
116{
117 return mGeometry;
118}
119
120void QgsGeometryWidget::setAcceptedWkbTypes( const QList<Qgis::WkbType> &types )
121{
122 mAcceptedTypes = types;
123}
124
125QList<Qgis::WkbType> QgsGeometryWidget::acceptedWkbTypes() const
126{
127 return mAcceptedTypes;
128}
129
131{
132 return mReadOnly;
133}
134
136{
137 mReadOnly = readOnly;
138}
139
141{
142 if ( mGeometry.isNull() )
143 return;
144
146}
147
149{
150 if ( mGeometry.isNull() )
151 return;
152
153 QApplication::clipboard()->setText( mGeometry.asWkt() );
154}
155
157{
158 if ( mGeometry.isNull() )
159 return;
160
161 QApplication::clipboard()->setText( mGeometry.asJson() );
162}
163
164void QgsGeometryWidget::pasteTriggered()
165{
166 if ( !mPastedGeom.isNull() )
167 {
168 QgsCoordinateReferenceSystem defaultCrs = mPastedGeom.crs();
169
170 // default to CRS of current geometry, if we have no better guesses as to what the clipboard CRS is
171 if ( !defaultCrs.isValid() )
172 defaultCrs = mGeometry.crs();
173
174 QgsProjectionSelectionDialog crsSelector( this );
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() )
179 {
180 mPastedGeom.setCrs( crsSelector.crs() );
181 setGeometryValue( mPastedGeom );
182 mPastedGeom = QgsReferencedGeometry();
183 }
184 }
185}
186
187void QgsGeometryWidget::fetchGeomFromClipboard()
188{
189 mPastedGeom = QgsReferencedGeometry();
190 if ( mReadOnly )
191 return;
192
193 const QString text = QApplication::clipboard()->text();
194 if ( text.isEmpty() )
195 return;
196
197 //try reading as a single wkt string
198 mPastedGeom = QgsReferencedGeometry( QgsGeometry::fromWkt( text ), QgsCoordinateReferenceSystem() );
199 if ( !mPastedGeom.isNull() && typeIsAcceptable( mPastedGeom.wkbType() ) )
200 {
201 return;
202 }
203 mPastedGeom = QgsReferencedGeometry();
204
205 //try reading as a list
206 const QStringList lines = text.split( "\n", Qt::SkipEmptyParts );
207 if ( !lines.isEmpty() )
208 {
209 for ( const QString &line : lines )
210 {
211 const QgsGeometry geometry = QgsGeometry::fromWkt( line );
212 if ( !geometry.isNull() && typeIsAcceptable( geometry.wkbType() ) )
213 {
214 mPastedGeom = QgsReferencedGeometry( geometry, QgsCoordinateReferenceSystem() );
215 return;
216 }
217 }
218 }
219
220 // try reading a GeoJSON
221 const QgsFeatureList features = QgsJsonUtils::stringToFeatureList( text );
222 if ( !features.isEmpty() && features.at( 0 ).hasGeometry() )
223 {
224 // assume EPSG:4326 for GeoJSON
225 mPastedGeom = QgsReferencedGeometry( features.at( 0 ).geometry(), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
226 return;
227 }
228}
229
230bool QgsGeometryWidget::typeIsAcceptable( Qgis::WkbType type ) const
231{
232 if ( mAcceptedTypes.isEmpty() )
233 {
234 return true;
235 }
236
237 return mAcceptedTypes.contains( type );
238}
239
240void QgsGeometryWidget::prepareMenu()
241{
242 fetchGeomFromClipboard();
243
244 mCopyGeoJsonAction->setEnabled( !mGeometry.isNull() );
245 mCopyWktAction->setEnabled( !mGeometry.isNull() );
246 mClearAction->setEnabled( !mGeometry.isNull() && !mReadOnly );
247 mPasteAction->setEnabled( !mPastedGeom.isNull() && !mReadOnly );
248}
249
250void QgsGeometryWidget::updateLineEdit()
251{
252 if ( mGeometry.isNull() )
253 {
254 mLineEdit->setText( QgsApplication::nullRepresentation() );
255 }
256 else
257 {
258 QString wkt = mGeometry.asWkt();
259 if ( wkt.length() >= 1050 )
260 {
261 wkt = wkt.left( QgsField::MAX_WKT_LENGTH ) + QChar( 0x2026 );
262 }
263
264 if ( mGeometry.crs().isValid() )
265 {
266 mLineEdit->setText( QStringLiteral( "%1 [%2]" ).arg( wkt, mGeometry.crs().userFriendlyIdentifier() ) );
267 }
268 else
269 {
270 mLineEdit->setText( wkt );
271 }
272 }
273}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
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
Definition qgsfield.h:578
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.
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