QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
16 #include <QApplication>
17 #include <QDesktopWidget>
39 , mScreenDensity( calculateScreenDensity() )
63 return QgsPoint( coor.longitude(), coor.latitude(), coor.altitude() );
78 if ( mapSettings ==
nullptr )
return 0.0;
87 QPoint pointCenter( s.width() / 2, s.height() / 2 );
95 QFileInfo check_file( path );
97 return ( check_file.exists() && check_file.isFile() );
102 QString modPath = path;
103 QString filePrefix(
"file://" );
105 if ( path.startsWith( filePrefix ) )
107 modPath = modPath.replace( filePrefix, QString() );
110 if ( prefixPath.isEmpty() )
return modPath;
113 if ( !QFileInfo( path ).exists() )
115 if ( !prefixPath.isEmpty() && modPath.startsWith( prefixPath ) )
117 return modPath.replace( prefixPath, QString() );
122 QDir absoluteDir( modPath );
123 QDir prefixDir( prefixPath );
124 QString canonicalPath = absoluteDir.canonicalPath();
125 QString prefixCanonicalPath = prefixDir.canonicalPath() +
"/";
127 if ( prefixCanonicalPath.length() > 1 && canonicalPath.startsWith( prefixCanonicalPath ) )
129 return canonicalPath.replace( prefixCanonicalPath, QString() );
148 QString path = QStringLiteral(
"qrc:/%1.svg" ).arg( name );
149 QgsDebugMsg( QStringLiteral(
"Using icon %1 from %2" ).arg( name, path ) );
155 QString path(
"qgsquick%1.qml" );
156 QStringList supportedWidgets = { QStringLiteral(
"textedit" ),
157 QStringLiteral(
"valuemap" ),
158 QStringLiteral(
"valuerelation" ),
159 QStringLiteral(
"checkbox" ),
160 QStringLiteral(
"externalresource" ),
161 QStringLiteral(
"datetime" ),
162 QStringLiteral(
"range" )
164 if ( supportedWidgets.contains( widgetName ) )
166 return QUrl( path.arg( widgetName ) );
170 return QUrl( path.arg( QLatin1String(
"textedit" ) ) );
178 QgsCoordinateFormatter::FormatFlags flags )
193 return QStringLiteral(
"%1 %2" )
194 .arg( QString::number( destDistance,
'f', decimals ) )
200 QFile file( filePath );
201 return file.remove( filePath );
211 return formatToMetricDistance( srcDistance, srcUnits, destDistance, destUnits );
215 return formatToImperialDistance( srcDistance, srcUnits, destDistance, destUnits );
219 return formatToUSCSDistance( srcDistance, srcUnits, destDistance, destUnits );
227 void QgsQuickUtils::formatToMetricDistance(
double srcDistance,
229 double &destDistance,
243 destDistance = dist / mmToKm;
251 destDistance = dist / mmToM;
259 destDistance = dist / mmToCm;
268 void QgsQuickUtils::formatToImperialDistance(
double srcDistance,
270 double &destDistance,
282 if ( dist > feetToMile )
284 destDistance = dist / feetToMile;
290 if ( dist > feetToYard )
292 destDistance = dist / feetToYard;
302 void QgsQuickUtils::formatToUSCSDistance(
double srcDistance,
304 double &destDistance,
316 if ( dist > feetToMile )
318 destDistance = dist / feetToMile;
324 if ( dist > feetToYard )
326 destDistance = dist / feetToYard;
338 QRect rec = QApplication::desktop()->screenGeometry();
339 int dpiX = QApplication::desktop()->physicalDpiX();
340 int dpiY = QApplication::desktop()->physicalDpiY();
341 int height = rec.height();
342 int width = rec.width();
343 double sizeX =
static_cast<double>( width ) / dpiX * 25.4;
344 double sizeY =
static_cast<double>( height ) / dpiY * 25.4;
347 msg += tr(
"screen resolution: %1x%2 px\n" ).arg( width ).arg( height );
348 msg += tr(
"screen DPI: %1x%2\n" ).arg( dpiX ).arg( dpiY );
349 msg += tr(
"screen size: %1x%2 mm\n" ).arg( QString::number( sizeX,
'f', 0 ), QString::number( sizeY,
'f', 0 ) );
350 msg += tr(
"screen density: %1" ).arg( mScreenDensity );
356 QVariantMap valueMap;
361 valueMap.insert( item.key.toString(), item.value );
368 QList<QgsExpressionContextScope *> scopes;
376 return expr.
evaluate( &context ).toString();
382 for (
const int &fid : fids )
389 return QVariant(
field.
type() ).typeName();
396 return QString(
"Date" );
400 return QString(
"Time" );
404 return QString(
"Date Time" );
408 return QString(
"Date Time" );
414 return mScreenDensity;
417 qreal QgsQuickUtils::calculateScreenDensity()
420 int dpiX = QApplication::desktop()->physicalDpiX();
421 int dpiY = QApplication::desktop()->physicalDpiY();
422 int dpi = dpiX < dpiY ? dpiX : dpiY;
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Contains information about the context in which a coordinate transform is executed.
@ 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.
static Q_INVOKABLE QString dateTimeFieldFormat(const QString &fieldFormat)
Returns field format's name for given string representing field format defined in QgsDateTimeFieldFor...
DistanceUnit
Units of distance.
@ 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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static Q_INVOKABLE QString formatDistance(double distance, QgsUnitTypes::DistanceUnit units, int decimals, QgsUnitTypes::SystemOfMeasurement destSystem=QgsUnitTypes::MetricSystem)
Converts distance to human readable distance.
The QgsQuickMapSettings class encapsulates QgsMapSettings class to offer settings of configuration of...
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.
Q_GADGET QgsVectorLayer * layer
Vector layer to which the feature belongs.
@ 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)
This class represents a coordinate reference system (CRS).
QVariant evaluate()
Evaluate the feature and return the result.
static Q_INVOKABLE bool fileExists(const QString &path)
Returns whether file on path exists.
static Q_INVOKABLE QString fieldType(const QgsField &field)
Returns the QVariant typeName of a field.
A class to represent a 2D point.
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 Q_INVOKABLE const 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.
Represents a vector layer which manages a vector based data sets.
@ DistanceCentimeters
Centimeters.
Pair of QgsFeature and QgsVectorLayer.
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.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
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.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
@ DistanceNauticalMiles
Nautical miles.
static Q_INVOKABLE const 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.
Class for parsing and evaluation of expressions (formerly called "search strings").
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.
Encapsulate a field in an attribute table or data source.