16 #include <QGuiApplication>
40 , mScreenDensity( calculateScreenDensity() )
64 return QgsPoint( coor.longitude(), coor.latitude(), coor.altitude() );
90 if ( mapSettings ==
nullptr )
return 0.0;
99 QPoint pointCenter( s.width() / 2, s.height() / 2 );
107 QFileInfo check_file( path );
109 return ( check_file.exists() && check_file.isFile() );
114 QString modPath = path;
115 QString filePrefix(
"file://" );
117 if ( path.startsWith( filePrefix ) )
119 modPath = modPath.replace( filePrefix, QString() );
122 if ( prefixPath.isEmpty() )
return modPath;
125 if ( !QFileInfo( path ).exists() )
127 if ( !prefixPath.isEmpty() && modPath.startsWith( prefixPath ) )
129 return modPath.replace( prefixPath, QString() );
134 QDir absoluteDir( modPath );
135 QDir prefixDir( prefixPath );
136 QString canonicalPath = absoluteDir.canonicalPath();
137 QString prefixCanonicalPath = prefixDir.canonicalPath() +
"/";
139 if ( prefixCanonicalPath.length() > 1 && canonicalPath.startsWith( prefixCanonicalPath ) )
141 return canonicalPath.replace( prefixCanonicalPath, QString() );
160 QString path = QStringLiteral(
"qrc:/%1.svg" ).arg( name );
161 QgsDebugMsg( QStringLiteral(
"Using icon %1 from %2" ).arg( name, path ) );
167 QString path(
"qgsquick%1.qml" );
168 QStringList supportedWidgets = { QStringLiteral(
"textedit" ),
169 QStringLiteral(
"valuemap" ),
170 QStringLiteral(
"valuerelation" ),
171 QStringLiteral(
"checkbox" ),
172 QStringLiteral(
"externalresource" ),
173 QStringLiteral(
"datetime" ),
174 QStringLiteral(
"range" )
176 if ( supportedWidgets.contains( widgetName ) )
178 return QUrl( path.arg( widgetName ) );
182 return QUrl( path.arg( QLatin1String(
"textedit" ) ) );
190 QgsCoordinateFormatter::FormatFlags flags )
205 return QStringLiteral(
"%1 %2" )
206 .arg( QString::number( destDistance,
'f', decimals ) )
212 QFile file( filePath );
213 return file.remove( filePath );
223 return formatToMetricDistance( srcDistance, srcUnits, destDistance, destUnits );
227 return formatToImperialDistance( srcDistance, srcUnits, destDistance, destUnits );
231 return formatToUSCSDistance( srcDistance, srcUnits, destDistance, destUnits );
239 void QgsQuickUtils::formatToMetricDistance(
double srcDistance,
241 double &destDistance,
255 destDistance = dist / mmToKm;
263 destDistance = dist / mmToM;
271 destDistance = dist / mmToCm;
280 void QgsQuickUtils::formatToImperialDistance(
double srcDistance,
282 double &destDistance,
294 if ( dist > feetToMile )
296 destDistance = dist / feetToMile;
302 if ( dist > feetToYard )
304 destDistance = dist / feetToYard;
314 void QgsQuickUtils::formatToUSCSDistance(
double srcDistance,
316 double &destDistance,
328 if ( dist > feetToMile )
330 destDistance = dist / feetToMile;
336 if ( dist > feetToYard )
338 destDistance = dist / feetToYard;
351 QScreen *screen = QGuiApplication::topLevelWindows().at( 0 )->screen();
352 double dpiX = screen->physicalDotsPerInchX();
353 double dpiY = screen->physicalDotsPerInchY();
354 int height = screen->geometry().height();
355 int width = screen->geometry().width();
356 double sizeX =
static_cast<double>( width ) / dpiX * 25.4;
357 double sizeY =
static_cast<double>( height ) / dpiY * 25.4;
360 msg += tr(
"screen resolution: %1x%2 px\n" ).arg( width ).arg( height );
361 msg += tr(
"screen DPI: %1x%2\n" ).arg( dpiX ).arg( dpiY );
362 msg += tr(
"screen size: %1x%2 mm\n" ).arg( QString::number( sizeX,
'f', 0 ), QString::number( sizeY,
'f', 0 ) );
363 msg += tr(
"screen density: %1" ).arg( mScreenDensity );
369 QVariantMap valueMap;
374 valueMap.insert( item.key.toString(), item.value );
381 QList<QgsExpressionContextScope *> scopes;
389 return expr.
evaluate( &context ).toString();
395 for (
const int &fid : fids )
402 return QVariant(
field.
type() ).typeName();
409 return QString(
"Date" );
413 return QString(
"Time" );
417 return QString(
"Date Time" );
421 return QString(
"Date Time" );
427 return QModelIndex();
432 return mScreenDensity;
434 qreal QgsQuickUtils::calculateScreenDensity()
438 QScreen *screen = QGuiApplication::topLevelWindows().at( 0 )->screen();
439 double dpiX = screen->physicalDotsPerInchX();
440 double dpiY = screen->physicalDotsPerInchY();
441 double dpi = dpiX < dpiY ? dpiX : dpiY;
MessageLevel
Level for messages This will be used both for message log and message bar in application.
This class represents a coordinate reference system (CRS).
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Contains information about the context in which a coordinate transform is executed.
Custom exception class for Coordinate Reference System related exceptions.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
QVariant evaluate()
Evaluate the feature and return the result.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Encapsulate a field in an attribute table or data source.
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).
A class to represent a 2D point.
Point geometry type, with support for z-dimension and m-values.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Pair of QgsFeature and QgsVectorLayer.
QgsFeature feature
Feature that belongs to layer.
Q_GADGET QgsVectorLayer * layer
Vector layer to which the feature belongs.
The QgsQuickMapSettings class encapsulates QgsMapSettings class to offer settings of configuration of...
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Q_INVOKABLE QgsPoint screenToCoordinate(const QPointF &point) const
Convert a screen coordinate to a map coordinate.
QgsCoordinateReferenceSystem destinationCrs
CRS of destination coordinate reference system.
QSize outputSize
The size of the resulting map image.
static Q_INVOKABLE double screenUnitsToMeters(QgsQuickMapSettings *mapSettings, int baseLengthPixels)
Calculates the distance in meter representing baseLengthPixels pixels on the screen based on the curr...
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...
static Q_INVOKABLE QgsPointXY transformPoint(const QgsCoordinateReferenceSystem &srcCrs, const QgsCoordinateReferenceSystem &destCrs, const QgsCoordinateTransformContext &context, const QgsPointXY &srcPoint)
Transforms point between different crs from QML.
static Q_INVOKABLE bool removeFile(const QString &filePath)
Deletes file from a given path.
static Q_INVOKABLE QString fieldType(const QgsField &field)
Returns the QVariant typeName of a field.
static Q_INVOKABLE QString formatDistance(double distance, QgsUnitTypes::DistanceUnit units, int decimals, QgsUnitTypes::SystemOfMeasurement destSystem=QgsUnitTypes::MetricSystem)
Converts distance to human readable distance.
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.
QString dumpScreenInfo() const
Returns a string with information about screen size and resolution - useful for debugging.
static Q_INVOKABLE QString evaluateExpression(const QgsQuickFeatureLayerPair &pair, QgsProject *activeProject, const QString &expression)
Evaluates expression.
static Q_INVOKABLE void logMessage(const QString &message, const QString &tag=QString("QgsQuick"), Qgis::MessageLevel level=Qgis::Warning)
Log message in QgsMessageLog.
static Q_INVOKABLE QgsCoordinateReferenceSystem coordinateReferenceSystemFromEpsgId(long epsg)
Creates crs from epsg code in QML.
static Q_INVOKABLE const QUrl getThemeIcon(const QString &name)
Returns QUrl to image from library's /images folder.
static Q_INVOKABLE bool fileExists(const QString &path)
Returns whether file on path exists.
static Q_INVOKABLE QgsPoint coordinateToPoint(const QGeoCoordinate &coor)
Converts QGeoCoordinate to QgsPoint.
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.
qreal screenDensity() const
"dp" is useful for building building components that work well with different screen densities.
static Q_INVOKABLE QString getRelativePath(const QString &path, const QString &prefixPath)
Returns relative path of the file to given prefixPath.
static Q_INVOKABLE const QUrl getEditorComponentSource(const QString &widgetName)
Returns url to field editor component for a feature form.
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.
QgsQuickUtils(QObject *parent=nullptr)
Create new utilities.
static Q_INVOKABLE QString dateTimeFieldFormat(const QString &fieldFormat)
Returns field format's name for given string representing field format defined in QgsDateTimeFieldFor...
static Q_INVOKABLE QVariantMap createValueRelationCache(const QVariantMap &config, const QgsFeature &formFeature=QgsFeature())
Creates a cache for a value relation field.
static Q_INVOKABLE QModelIndex invalidIndex()
invalidIndex returns invalid index
static Q_INVOKABLE QgsPointXY pointXY(double x, double y)
Creates QgsPointXY in QML.
static Q_INVOKABLE QgsQuickFeatureLayerPair featureFactory(const QgsFeature &feature, QgsVectorLayer *layer=nullptr)
QgsQuickFeatureLayerPair factory for tuple of QgsFeature and QgsVectorLayer used in QgsQUick library.
DistanceUnit
Units of distance.
@ DistanceKilometers
Kilometers.
@ DistanceMiles
Terrestrial miles.
@ DistanceMillimeters
Millimeters.
@ DistanceYards
Imperial yards.
@ DistanceFeet
Imperial feet.
@ DistanceNauticalMiles
Nautical miles.
@ DistanceCentimeters
Centimeters.
SystemOfMeasurement
Systems of unit measurement.
@ MetricSystem
International System of Units (SI)
@ UnknownSystem
Unknown system of measurement.
@ ImperialSystem
British Imperial.
@ USCSSystem
United States customary system.
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
Represents a vector layer which manages a vector based data sets.
SelectBehavior
Selection behavior.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects matching features using a list of feature IDs.
QSet< QgsFeatureId > QgsFeatureIds