25#include <QApplication>
27#include <QImageWriter>
29#include <QRegularExpression>
32using namespace Qt::StringLiterals;
37 bool GUI_EXPORT
openFilesRememberingFilter( QString
const &filterName, QString
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
bool cancelAll )
42 QString lastUsedFilter = settings.
value(
"/UI/" + filterName,
"" ).toString();
43 const QString lastUsedDir = settings.
value(
"/UI/" + filterName +
"Dir", QDir::homePath() ).toString();
48 selectedFiles = QFileDialog::getOpenFileNames(
nullptr, title, lastUsedDir, filters, &lastUsedFilter );
55 openFileDialog->setFileMode( QFileDialog::ExistingFiles );
57 if ( !lastUsedFilter.isEmpty() )
59 openFileDialog->selectNameFilter( lastUsedFilter );
62 if ( openFileDialog->exec() == QDialog::Accepted )
64 selectedFiles = openFileDialog->selectedFiles();
76 if ( !selectedFiles.isEmpty() )
81 const QString firstFileName = selectedFiles.first();
82 const QFileInfo fi( firstFileName );
83 const QString path = fi.path();
87 settings.
setValue(
"/UI/" + filterName, lastUsedFilter );
88 settings.
setValue(
"/UI/" + filterName +
"Dir", path );
93 QPair<QString, QString> GUI_EXPORT
getSaveAsImageName( QWidget *parent,
const QString &message,
const QString &defaultFilename )
96 QMap<QString, QString> filterMap;
97 const auto supportedImageFormats { QImageWriter::supportedImageFormats() };
98 QStringList imageFormats;
100 imageFormats << u
"*.png *.PNG"_s;
101 for (
const QByteArray &format : supportedImageFormats )
104 if ( format ==
"svg" )
111 if ( format !=
"png" )
113 imageFormats << u
"*.%1 *.%2"_s.arg( format, QString( format ).toUpper() );
116 const QString formatByExtension = u
"%1 (%2)"_s.arg( QObject::tr(
"Format by Extension" ), imageFormats.join(
' '_L1 ) );
120 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
127 const QString lastUsedDir = settings.
value( u
"UI/lastSaveAsImageDir"_s, QDir::homePath() ).toString();
129 QString selectedFilter = settings.
value( u
"UI/lastSaveAsImageFilter"_s, QString() ).toString();
130 if ( selectedFilter.isEmpty() )
132 selectedFilter = formatByExtension;
136 if ( defaultFilename.isNull() )
139 initialPath = lastUsedDir;
144 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
147 QString outputFileName;
149#if defined( Q_OS_WIN ) || defined( Q_OS_MAC ) || defined( Q_OS_LINUX )
150 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, formatByExtension + u
";;"_s +
qgsMapJoinKeys( filterMap, u
";;"_s ), &selectedFilter );
153 auto fileDialog = std::make_unique<QFileDialog>( parent, message, initialPath, formatByExtension + u
";;"_s +
qgsMapJoinKeys( filterMap, u
";;"_s ) );
156 fileDialog->setFileMode( QFileDialog::AnyFile );
157 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
158 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
160 if ( !selectedFilter.isEmpty() )
162 fileDialog->selectNameFilter( selectedFilter );
166 if ( fileDialog->exec() == QDialog::Accepted )
168 outputFileName = fileDialog->selectedFiles().first();
172 if ( !outputFileName.isNull() )
174 if ( selectedFilter == formatByExtension )
176 settings.
setValue( u
"UI/lastSaveAsImageFilter"_s, QString() );
177 ext = QFileInfo( outputFileName ).suffix();
179 auto match = std::find_if( filterMap.begin(), filterMap.end(), [&ext](
const QString &filter ) { return filter == ext; } );
180 if ( match == filterMap.end() )
190 ext = filterMap.value( selectedFilter, QString() );
191 if ( !ext.isEmpty() )
194 settings.
setValue( u
"UI/lastSaveAsImageFilter"_s, selectedFilter );
197 settings.
setValue( u
"UI/lastSaveAsImageDir"_s, QFileInfo( outputFileName ).absolutePath() );
200 return qMakePair( outputFileName, ext );
205 return u
"%1 (%2 %3)"_s.arg( longName, glob.toLower(), glob.toUpper() );
210 const QString longName = format.toUpper() +
" format";
211 const QString glob =
"*." + format;
215 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
220#if defined( Q_OS_MAC )
225 return QFontDialog::getFont( &ok, initial,
nullptr, title, QFontDialog::DontUseNativeDialog );
227 return QFontDialog::getFont( &ok, initial,
nullptr, title );
235 settings.
setValue( key, widget->saveGeometry() );
242 return widget->restoreGeometry( settings.
value( key ).toByteArray() );
248 if ( !keyName.isEmpty() )
252 else if ( widget->objectName().isEmpty() )
254 subKey = QString( widget->metaObject()->className() );
258 subKey = widget->objectName();
260 QString key = u
"Windows/%1/geometry"_s.arg( subKey );
272 const int w = s.
value( u
"/qgis/toolbarIconSize"_s, 32 ).toInt();
275 if ( dockableToolbar )
285 int adjustedSize = 16;
286 if ( size.width() > 32 )
288 adjustedSize = size.width() - 16;
290 else if ( size.width() == 32 )
294 return QSize( adjustedSize, adjustedSize );
300 QString result { QLocale().toString( value,
'f', precision ) };
301 if ( !displayTrailingZeroes )
303 const QRegularExpression zeroesRe { QStringLiteral( R
"raw(\%1\d*?(0+$))raw" ).arg( QLocale().decimalPoint() ) };
304 if ( zeroesRe.match( result ).hasMatch() )
306 result.truncate( zeroesRe.match( result ).capturedStart( 1 ) );
307 if ( result.endsWith( QLocale().decimalPoint() ) )
318 switch ( rasterDataType )
336 return std::numeric_limits<float>::digits10 + 1;
341 return std::numeric_limits<double>::digits10 + 1;
345 return std::numeric_limits<double>::digits10 + 1;
363 *isNonStandard = nonStandard;
376 return QMessageBox::question( parent, dialogTitle, QObject::tr(
"PolyhedralSurface, TIN and Triangle are non-standard GeoPackage geometry types "
377 "and may not be recognized by other software.\n\n"
378 "Do you want to continue?" ),
379 QMessageBox::Yes | QMessageBox::No, QMessageBox::No )
390 QApplication::setOverrideCursor( cursor );
396 QApplication::restoreOverrideCursor();
404 mHasOverride =
false;
405 QApplication::restoreOverrideCursor();
415 while ( QApplication::overrideCursor() )
417 mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
418 QApplication::restoreOverrideCursor();
429 for (
auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
431 QApplication::setOverrideCursor( *it );
443 mWidget->setUpdatesEnabled(
false );
451 mWidget->setUpdatesEnabled(
true );
DataType
Raster data types.
@ Float32
Thirty two bit floating point (float).
@ CFloat64
Complex Float64.
@ Int16
Sixteen bit signed integer (qint16).
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30).
@ UInt16
Sixteen bit unsigned integer (quint16).
@ Byte
Eight bit unsigned integer (quint8).
@ UnknownDataType
Unknown or unspecified type.
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
@ Int32
Thirty two bit signed integer (qint32).
@ Float64
Sixty four bit floating point (double).
@ CFloat32
Complex Float32.
@ UInt32
Thirty two bit unsigned integer (quint32).
WkbType
The WKB type describes the number of dimensions a geometry has.
@ PolyhedralSurface
PolyhedralSurface.
static int scaleIconSize(int standardSize, bool applyDevicePixelRatio=false)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
A file dialog which lets the user select the preferred encoding type for a data provider.
void addCancelAll()
Adds a 'Cancel All' button for the user to click.
bool cancelAll() const
Returns true if the user clicked 'Cancel All'.
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsTemporaryCursorOverride(const QCursor &cursor)
Constructor for QgsTemporaryCursorOverride.
~QgsTemporaryCursorOverride()
void release()
Releases the cursor override early (i.e.
QgsTemporaryCursorRestoreOverride()
Constructor for QgsTemporaryCursorRestoreOverride.
void restore()
Restores the cursor override early (i.e.
~QgsTemporaryCursorRestoreOverride()
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
The QgsGuiUtils namespace contains constants and helper functions used throughout the QGIS GUI.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
QString createWidgetKey(QWidget *widget, const QString &keyName)
Creates a key for the given widget that can be used to store related data in settings.
QPair< QString, QString > GUI_EXPORT getSaveAsImageName(QWidget *parent, const QString &message, const QString &defaultFilename)
A helper function to get an image name from the user.
bool isNonStandardGeoPackageGeometryType(Qgis::WkbType wkbType)
Returns true if the given wkbType is a non-standard GeoPackage geometry type (PolyhedralSurface,...
bool warnAboutNonStandardGeoPackageGeometryType(Qgis::WkbType wkbType, QWidget *parent, const QString &dialogTitle, bool showDialog, bool *isNonStandard)
Checks if the given wkbType is a non-standard GeoPackage geometry type (PolyhedralSurface,...
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
int significantDigits(const Qgis::DataType rasterDataType)
Returns the maximum number of significant digits a for the given rasterDataType.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
QFont getFont(bool &ok, const QFont &initial, const QString &title)
Show font selection dialog.
bool GUI_EXPORT openFilesRememberingFilter(QString const &filterName, QString const &filters, QStringList &selectedFiles, QString &enc, QString &title, bool cancelAll)
Open files, preferring to have the default file selector be the last one used, if any; also,...
QString createFileFilter_(QString const &longName, QString const &glob)
Convenience function for readily creating file filters.
QSize panelIconSize(QSize size)
Returns dockable panel toolbar icon width based on the provided window toolbar width.
QString displayValueWithMaximumDecimals(const Qgis::DataType dataType, const double value, bool displayTrailingZeroes)
Returns a localized string representation of the value with the appropriate number of decimals suppor...
QString qgsMapJoinKeys(const QMap< Key, Value > &map, const QString &separator)
Joins all the map keys into a single string with each element separated by the given separator.
#define QgsDebugMsgLevel(str, level)