25#include <QApplication>
27#include <QImageWriter>
28#include <QRegularExpression>
33 bool GUI_EXPORT
openFilesRememberingFilter( QString
const &filterName, QString
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
bool cancelAll )
38 QString lastUsedFilter = settings.
value(
"/UI/" + filterName,
"" ).toString();
39 const QString lastUsedDir = settings.
value(
"/UI/" + filterName +
"Dir", QDir::homePath() ).toString();
44 selectedFiles = QFileDialog::getOpenFileNames(
nullptr, title, lastUsedDir, filters, &lastUsedFilter );
51 openFileDialog->setFileMode( QFileDialog::ExistingFiles );
53 if ( !lastUsedFilter.isEmpty() )
55 openFileDialog->selectNameFilter( lastUsedFilter );
58 if ( openFileDialog->exec() == QDialog::Accepted )
60 selectedFiles = openFileDialog->selectedFiles();
72 if ( !selectedFiles.isEmpty() )
77 const QString firstFileName = selectedFiles.first();
78 const QFileInfo fi( firstFileName );
79 const QString path = fi.path();
83 settings.
setValue(
"/UI/" + filterName, lastUsedFilter );
84 settings.
setValue(
"/UI/" + filterName +
"Dir", path );
89 QPair<QString, QString> GUI_EXPORT
getSaveAsImageName( QWidget *parent,
const QString &message,
const QString &defaultFilename )
92 QMap<QString, QString> filterMap;
93 const auto supportedImageFormats { QImageWriter::supportedImageFormats() };
94 QStringList imageFormats;
96 imageFormats << QStringLiteral(
"*.png *.PNG" );
97 for (
const QByteArray &format : supportedImageFormats )
100 if ( format ==
"svg" )
107 if ( format !=
"png" )
109 imageFormats << QStringLiteral(
"*.%1 *.%2" ).arg( format, QString( format ).toUpper() );
112 const QString formatByExtension = QStringLiteral(
"%1 (%2)" ).arg( QObject::tr(
"Format by Extension" ), imageFormats.join( QLatin1Char(
' ' ) ) );
116 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
123 const QString lastUsedDir = settings.
value( QStringLiteral(
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
125 QString selectedFilter = settings.
value( QStringLiteral(
"UI/lastSaveAsImageFilter" ), QString() ).toString();
126 if ( selectedFilter.isEmpty() )
128 selectedFilter = formatByExtension;
132 if ( defaultFilename.isNull() )
135 initialPath = lastUsedDir;
140 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
143 QString outputFileName;
145#if defined( Q_OS_WIN ) || defined( Q_OS_MAC ) || defined( Q_OS_LINUX )
146 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, formatByExtension + QStringLiteral(
";;" ) +
qgsMapJoinKeys( filterMap, QStringLiteral(
";;" ) ), &selectedFilter );
149 auto fileDialog = std::make_unique<QFileDialog>( parent, message, initialPath, formatByExtension + QStringLiteral(
";;" ) +
qgsMapJoinKeys( filterMap, QStringLiteral(
";;" ) ) );
152 fileDialog->setFileMode( QFileDialog::AnyFile );
153 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
154 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
156 if ( !selectedFilter.isEmpty() )
158 fileDialog->selectNameFilter( selectedFilter );
162 if ( fileDialog->exec() == QDialog::Accepted )
164 outputFileName = fileDialog->selectedFiles().first();
168 if ( !outputFileName.isNull() )
170 if ( selectedFilter == formatByExtension )
172 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), QString() );
173 ext = QFileInfo( outputFileName ).suffix();
175 auto match = std::find_if( filterMap.begin(), filterMap.end(), [&ext](
const QString &filter ) { return filter == ext; } );
176 if ( match == filterMap.end() )
179 ext = QStringLiteral(
"png" );
186 ext = filterMap.value( selectedFilter, QString() );
187 if ( !ext.isEmpty() )
190 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), selectedFilter );
193 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
196 return qMakePair( outputFileName, ext );
201 return QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
206 const QString longName = format.toUpper() +
" format";
207 const QString glob =
"*." + format;
211 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
216#if defined( Q_OS_MAC )
221 return QFontDialog::getFont( &ok, initial,
nullptr, title, QFontDialog::DontUseNativeDialog );
223 return QFontDialog::getFont( &ok, initial,
nullptr, title );
231 settings.
setValue( key, widget->saveGeometry() );
238 return widget->restoreGeometry( settings.
value( key ).toByteArray() );
244 if ( !keyName.isEmpty() )
248 else if ( widget->objectName().isEmpty() )
250 subKey = QString( widget->metaObject()->className() );
254 subKey = widget->objectName();
256 QString key = QStringLiteral(
"Windows/%1/geometry" ).arg( subKey );
268 const int w = s.
value( QStringLiteral(
"/qgis/toolbarIconSize" ), 32 ).toInt();
271 if ( dockableToolbar )
281 int adjustedSize = 16;
282 if ( size.width() > 32 )
284 adjustedSize = size.width() - 16;
286 else if ( size.width() == 32 )
290 return QSize( adjustedSize, adjustedSize );
296 QString result { QLocale().toString( value,
'f', precision ) };
297 if ( !displayTrailingZeroes )
299 const QRegularExpression zeroesRe { QStringLiteral( R
"raw(\%1\d*?(0+$))raw" ).arg( QLocale().decimalPoint() ) };
300 if ( zeroesRe.match( result ).hasMatch() )
302 result.truncate( zeroesRe.match( result ).capturedStart( 1 ) );
303 if ( result.endsWith( QLocale().decimalPoint() ) )
314 switch ( rasterDataType )
332 return std::numeric_limits<float>::digits10 + 1;
337 return std::numeric_limits<double>::digits10 + 1;
341 return std::numeric_limits<double>::digits10 + 1;
354 QApplication::setOverrideCursor( cursor );
360 QApplication::restoreOverrideCursor();
368 mHasOverride =
false;
369 QApplication::restoreOverrideCursor();
379 while ( QApplication::overrideCursor() )
381 mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
382 QApplication::restoreOverrideCursor();
393 for (
auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
395 QApplication::setOverrideCursor( *it );
407 mWidget->setUpdatesEnabled(
false );
415 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).
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()
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.
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)