25#include <QApplication>
27#include <QImageWriter>
28#include <QRegularExpression>
34 bool GUI_EXPORT
openFilesRememberingFilter( QString
const &filterName, QString
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
bool cancelAll )
39 QString lastUsedFilter = settings.
value(
"/UI/" + filterName,
"" ).toString();
40 const QString lastUsedDir = settings.
value(
"/UI/" + filterName +
"Dir", QDir::homePath() ).toString();
45 selectedFiles = QFileDialog::getOpenFileNames(
nullptr, title, lastUsedDir, filters, &lastUsedFilter );
52 openFileDialog->setFileMode( QFileDialog::ExistingFiles );
54 if ( !lastUsedFilter.isEmpty() )
56 openFileDialog->selectNameFilter( lastUsedFilter );
59 if ( openFileDialog->exec() == QDialog::Accepted )
61 selectedFiles = openFileDialog->selectedFiles();
73 if ( !selectedFiles.isEmpty() )
78 const QString firstFileName = selectedFiles.first();
79 const QFileInfo fi( firstFileName );
80 const QString path = fi.path();
84 settings.
setValue(
"/UI/" + filterName, lastUsedFilter );
85 settings.
setValue(
"/UI/" + filterName +
"Dir", path );
90 QPair<QString, QString> GUI_EXPORT
getSaveAsImageName( QWidget *parent,
const QString &message,
const QString &defaultFilename )
93 QMap<QString, QString> filterMap;
94 const auto supportedImageFormats { QImageWriter::supportedImageFormats() };
95 QStringList imageFormats;
97 imageFormats << QStringLiteral(
"*.png *.PNG" );
98 for (
const QByteArray &format : supportedImageFormats )
101 if ( format ==
"svg" )
108 if ( format !=
"png" )
110 imageFormats << QStringLiteral(
"*.%1 *.%2" ).arg( format, QString( format ).toUpper() );
113 const QString formatByExtension = QStringLiteral(
"%1 (%2)" ).arg( QObject::tr(
"Format by Extension" ), imageFormats.join( QLatin1Char(
' ' ) ) );
117 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
124 const QString lastUsedDir = settings.
value( QStringLiteral(
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
126 QString selectedFilter = settings.
value( QStringLiteral(
"UI/lastSaveAsImageFilter" ), QString() ).toString();
127 if ( selectedFilter.isEmpty() )
129 selectedFilter = formatByExtension;
133 if ( defaultFilename.isNull() )
136 initialPath = lastUsedDir;
141 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
144 QString outputFileName;
146#if defined( Q_OS_WIN ) || defined( Q_OS_MAC ) || defined( Q_OS_LINUX )
147 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, formatByExtension + QStringLiteral(
";;" ) +
qgsMapJoinKeys( filterMap, QStringLiteral(
";;" ) ), &selectedFilter );
150 std::unique_ptr<QFileDialog> fileDialog(
new QFileDialog( parent, message, initialPath, formatByExtension + QStringLiteral(
";;" ) +
qgsMapJoinKeys( filterMap, QStringLiteral(
";;" ) ) ) );
153 fileDialog->setFileMode( QFileDialog::AnyFile );
154 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
155 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
157 if ( !selectedFilter.isEmpty() )
159 fileDialog->selectNameFilter( selectedFilter );
163 if ( fileDialog->exec() == QDialog::Accepted )
165 outputFileName = fileDialog->selectedFiles().first();
169 if ( !outputFileName.isNull() )
171 if ( selectedFilter == formatByExtension )
173 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), QString() );
174 ext = QFileInfo( outputFileName ).suffix();
176 auto match = std::find_if( filterMap.begin(), filterMap.end(), [&ext](
const QString &filter ) { return filter == ext; } );
177 if ( match == filterMap.end() )
180 ext = QStringLiteral(
"png" );
187 ext = filterMap.value( selectedFilter, QString() );
188 if ( !ext.isEmpty() )
191 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), selectedFilter );
194 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
197 return qMakePair( outputFileName, ext );
202 return QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
207 const QString longName = format.toUpper() +
" format";
208 const QString glob =
"*." + format;
212 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
217#if defined( Q_OS_MAC )
222 return QFontDialog::getFont( &ok, initial,
nullptr, title, QFontDialog::DontUseNativeDialog );
224 return QFontDialog::getFont( &ok, initial,
nullptr, title );
232 settings.
setValue( key, widget->saveGeometry() );
239 return widget->restoreGeometry( settings.
value( key ).toByteArray() );
245 if ( !keyName.isEmpty() )
249 else if ( widget->objectName().isEmpty() )
251 subKey = QString( widget->metaObject()->className() );
255 subKey = widget->objectName();
257 QString key = QStringLiteral(
"Windows/%1/geometry" ).arg( subKey );
269 const int w = s.
value( QStringLiteral(
"/qgis/toolbarIconSize" ), 32 ).toInt();
272 if ( dockableToolbar )
282 int adjustedSize = 16;
283 if ( size.width() > 32 )
285 adjustedSize = size.width() - 16;
287 else if ( size.width() == 32 )
291 return QSize( adjustedSize, adjustedSize );
297 QString result { QLocale().toString( value,
'f',
precision ) };
298 if ( !displayTrailingZeroes )
300 const QRegularExpression zeroesRe { QStringLiteral( R
"raw(\%1\d*?(0+$))raw" ).arg( QLocale().decimalPoint() ) };
301 if ( zeroesRe.match( result ).hasMatch() )
303 result.truncate( zeroesRe.match( result ).capturedStart( 1 ) );
304 if ( result.endsWith( QLocale().decimalPoint() ) )
315 switch ( rasterDataType )
333 return std::numeric_limits<float>::digits10 + 1;
338 return std::numeric_limits<double>::digits10 + 1;
342 return std::numeric_limits<double>::digits10 + 1;
355 QApplication::setOverrideCursor( cursor );
361 QApplication::restoreOverrideCursor();
369 mHasOverride =
false;
370 QApplication::restoreOverrideCursor();
380 while ( QApplication::overrideCursor() )
382 mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
383 QApplication::restoreOverrideCursor();
394 for (
auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
396 QApplication::setOverrideCursor( *it );
408 mWidget->setUpdatesEnabled(
false );
416 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.
bool cancelAll()
Returns true if the user clicked 'Cancel All'.
void addCancelAll()
Adds a 'Cancel All' button for the user to click.
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.
This class is a composition of two QSettings instances:
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)