QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
16 #include <QApplication>
17 #include <QDesktopWidget>
38 , mScreenDensity( calculateScreenDensity() )
62 return QgsPoint( coor.longitude(), coor.latitude(), coor.altitude() );
77 if ( mapSettings ==
nullptr )
return 0.0;
86 QPoint pointCenter( s.width() / 2, s.height() / 2 );
94 QFileInfo check_file( path );
96 return ( check_file.exists() && check_file.isFile() );
101 QString modPath = path;
102 QString filePrefix(
"file://" );
104 if ( path.startsWith( filePrefix ) )
106 modPath = modPath.replace( filePrefix, QString() );
109 if ( prefixPath.isEmpty() )
return modPath;
112 if ( !QFileInfo( path ).exists() )
114 if ( !prefixPath.isEmpty() && modPath.startsWith( prefixPath ) )
116 return modPath.replace( prefixPath, QString() );
121 QDir absoluteDir( modPath );
122 QDir prefixDir( prefixPath );
123 QString canonicalPath = absoluteDir.canonicalPath();
124 QString prefixCanonicalPath = prefixDir.canonicalPath() +
"/";
126 if ( prefixCanonicalPath.length() > 1 && canonicalPath.startsWith( prefixCanonicalPath ) )
128 return canonicalPath.replace( prefixCanonicalPath, QString() );
147 QString path = QStringLiteral(
"qrc:/%1.svg" ).arg( name );
148 QgsDebugMsg( QStringLiteral(
"Using icon %1 from %2" ).arg( name, path ) );
154 QString path(
"qgsquick%1.qml" );
155 QStringList supportedWidgets = { QStringLiteral(
"textedit" ),
156 QStringLiteral(
"valuemap" ),
157 QStringLiteral(
"valuerelation" ),
158 QStringLiteral(
"checkbox" ),
159 QStringLiteral(
"externalresource" ),
160 QStringLiteral(
"datetime" ),
161 QStringLiteral(
"range" )
163 if ( supportedWidgets.contains( widgetName ) )
165 return QUrl( path.arg( widgetName ) );
169 return QUrl( path.arg( QStringLiteral(
"textedit" ) ) );
177 QgsCoordinateFormatter::FormatFlags flags )
192 return QStringLiteral(
"%1 %2" )
193 .arg( QString::number( destDistance,
'f', decimals ) )
199 QFile file( filePath );
200 return file.remove( filePath );
210 return formatToMetricDistance( srcDistance, srcUnits, destDistance, destUnits );
214 return formatToImperialDistance( srcDistance, srcUnits, destDistance, destUnits );
218 return formatToUSCSDistance( srcDistance, srcUnits, destDistance, destUnits );
226 void QgsQuickUtils::formatToMetricDistance(
double srcDistance,
228 double &destDistance,
242 destDistance = dist / mmToKm;
250 destDistance = dist / mmToM;
258 destDistance = dist / mmToCm;
267 void QgsQuickUtils::formatToImperialDistance(
double srcDistance,
269 double &destDistance,
281 if ( dist > feetToMile )
283 destDistance = dist / feetToMile;
289 if ( dist > feetToYard )
291 destDistance = dist / feetToYard;
301 void QgsQuickUtils::formatToUSCSDistance(
double srcDistance,
303 double &destDistance,
315 if ( dist > feetToMile )
317 destDistance = dist / feetToMile;
323 if ( dist > feetToYard )
325 destDistance = dist / feetToYard;
337 QRect rec = QApplication::desktop()->screenGeometry();
338 int dpiX = QApplication::desktop()->physicalDpiX();
339 int dpiY = QApplication::desktop()->physicalDpiY();
340 int height = rec.height();
341 int width = rec.width();
342 double sizeX =
static_cast<double>( width ) / dpiX * 25.4;
343 double sizeY =
static_cast<double>( height ) / dpiY * 25.4;
346 msg += tr(
"screen resolution: %1x%2 px\n" ).arg( width ).arg( height );
347 msg += tr(
"screen DPI: %1x%2\n" ).arg( dpiX ).arg( dpiY );
348 msg += tr(
"screen size: %1x%2 mm\n" ).arg( QString::number( sizeX,
'f', 0 ), QString::number( sizeY,
'f', 0 ) );
349 msg += tr(
"screen density: %1" ).arg( mScreenDensity );
355 QVariantMap valueMap;
360 valueMap.insert( item.key.toString(), item.value );
367 QList<QgsExpressionContextScope *> scopes;
375 return expr.
evaluate( &context ).toString();
381 for (
const int &fid : fids )
388 return mScreenDensity;
391 qreal QgsQuickUtils::calculateScreenDensity()
394 int dpiX = QApplication::desktop()->physicalDpiX();
395 int dpiY = QApplication::desktop()->physicalDpiY();
396 int dpi = dpiX < dpiY ? dpiX : dpiY;
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
@ ImperialSystem
British Imperial.
static Q_INVOKABLE QString evaluateExpression(const QgsQuickFeatureLayerPair &pair, QgsProject *activeProject, const QString &expression)
Evaluates expression.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Point geometry type, with support for z-dimension and m-values.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static Q_INVOKABLE QgsPointXY pointXY(double x, double y)
Creates QgsPointXY in QML.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects matching features using a list of feature IDs.
static Q_INVOKABLE QVariantMap createValueRelationCache(const QVariantMap &config, const QgsFeature &formFeature=QgsFeature())
Creates a cache for a value relation field.
DistanceUnit
Units of distance.
QgsVectorLayer layer
Vector layer to which the feature belongs.
@ DistanceKilometers
Kilometers.
SystemOfMeasurement
Systems of unit measurement.
static Q_INVOKABLE bool removeFile(const QString &filePath)
Deletes file from a given path.
QString dumpScreenInfo() const
Returns a string with information about screen size and resolution - useful for debugging.
Q_INVOKABLE QgsPoint screenToCoordinate(const QPointF &point) const
Convert a screen coordinate to a map coordinate.
static Q_INVOKABLE QString formatDistance(double distance, QgsUnitTypes::DistanceUnit units, int decimals, QgsUnitTypes::SystemOfMeasurement destSystem=QgsUnitTypes::MetricSystem)
Converts distance to human readable distance.
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
static Q_INVOKABLE QString formatPoint(const QgsPoint &point, QgsCoordinateFormatter::Format format=QgsCoordinateFormatter::FormatPair, int decimals=3, QgsCoordinateFormatter::FormatFlags flags=QgsCoordinateFormatter::FlagDegreesUseStringSuffix)
Formats a point according to the specified parameters.
QgsCoordinateReferenceSystem destinationCrs
CRS of destination coordinate reference system.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
QSize outputSize
The size of the resulting map image.
static Q_INVOKABLE void logMessage(const QString &message, const QString &tag=QString("QgsQuick"), Qgis::MessageLevel level=Qgis::Warning)
Log message in QgsMessageLog.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
@ DistanceFeet
Imperial feet.
static Q_INVOKABLE QgsQuickFeatureLayerPair featureFactory(const QgsFeature &feature, QgsVectorLayer *layer=nullptr)
QgsQuickFeatureLayerPair factory for tuple of QgsFeature and QgsVectorLayer used in QgsQUick library.
QgsFeature feature
Feature that belongs to layer.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
SelectBehavior
Selection behavior.
QSet< QgsFeatureId > QgsFeatureIds
@ MetricSystem
International System of Units (SI)
QVariant evaluate()
Evaluate the feature and return the result.
static Q_INVOKABLE bool fileExists(const QString &path)
Returns whether file on path exists.
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
qreal screenDensity() const
"dp" is useful for building building components that work well with different screen densities.
static const Q_INVOKABLE QUrl getEditorComponentSource(const QString &widgetName)
Returns url to field editor component for a feature form.
@ USCSSystem
United States customary system.
static Q_INVOKABLE QgsPoint coordinateToPoint(const QGeoCoordinate &coor)
Converts QGeoCoordinate to QgsPoint.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
@ DistanceMillimeters
Millimeters.
@ DistanceCentimeters
Centimeters.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
static Q_INVOKABLE double screenUnitsToMeters(QgsQuickMapSettings *mapSettings, int baseLengthPixels)
Calculates the distance in meter representing baseLengthPixels pixels on the screen based on the curr...
@ DistanceYards
Imperial yards.
@ UnknownSystem
Unknown system of measurement.
static Q_INVOKABLE QgsCoordinateReferenceSystem coordinateReferenceSystemFromEpsgId(long epsg)
Creates crs from epsg code in QML.
@ DistanceMiles
Terrestrial miles.
static Q_INVOKABLE QString getRelativePath(const QString &path, const QString &prefixPath)
Returns relative path of the file to given prefixPath.
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
@ DistanceNauticalMiles
Nautical miles.
static const Q_INVOKABLE QUrl getThemeIcon(const QString &name)
Returns QUrl to image from library's /images folder.
static Q_INVOKABLE QgsPointXY transformPoint(const QgsCoordinateReferenceSystem &srcCrs, const QgsCoordinateReferenceSystem &destCrs, const QgsCoordinateTransformContext &context, const QgsPointXY &srcPoint)
Transforms point between different crs from QML.
QgsQuickUtils(QObject *parent=nullptr)
Create new utilities.
static Q_INVOKABLE QgsPoint point(double x, double y, double z=std::numeric_limits< double >::quiet_NaN(), double m=std::numeric_limits< double >::quiet_NaN())
Creates QgsPoint in QML.
static Q_INVOKABLE void selectFeaturesInLayer(QgsVectorLayer *layer, const QList< int > &fids, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects features in a layer This method is required since QML cannot perform the conversion of a feat...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
static void humanReadableDistance(double srcDistance, QgsUnitTypes::DistanceUnit srcUnits, QgsUnitTypes::SystemOfMeasurement destSystem, double &destDistance, QgsUnitTypes::DistanceUnit &destUnits)
Converts distance to human readable distance in destination system of measurement.