25#include <QApplication> 
   27#include <QImageWriter> 
   28#include <QRegularExpression> 
   35      QString 
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
 
   41    QString lastUsedFilter = settings.
value( 
"/UI/" + filterName, 
"" ).toString();
 
   42    const QString lastUsedDir = settings.
value( 
"/UI/" + filterName + 
"Dir", QDir::homePath() ).toString();
 
   47      selectedFiles = QFileDialog::getOpenFileNames( 
nullptr, title, lastUsedDir, filters, &lastUsedFilter );
 
   54      openFileDialog->setFileMode( QFileDialog::ExistingFiles );
 
   56      if ( !lastUsedFilter.isEmpty() )
 
   58        openFileDialog->selectNameFilter( lastUsedFilter );
 
   61      if ( openFileDialog->exec() == QDialog::Accepted )
 
   63        selectedFiles = openFileDialog->selectedFiles();
 
   75    if ( !selectedFiles.isEmpty() )
 
   80      const QString firstFileName = selectedFiles.first();
 
   81      const QFileInfo fi( firstFileName );
 
   82      const QString path = fi.path();
 
   86      settings.
setValue( 
"/UI/" + filterName, lastUsedFilter );
 
   87      settings.
setValue( 
"/UI/" + filterName + 
"Dir", path );
 
 
   92  QPair<QString, QString> GUI_EXPORT 
getSaveAsImageName( QWidget *parent, 
const QString &message, 
const QString &defaultFilename )
 
   95    QMap<QString, QString> filterMap;
 
   96    const auto supportedImageFormats { QImageWriter::supportedImageFormats() };
 
   97    QStringList imageFormats;
 
   99    imageFormats << QStringLiteral( 
"*.png *.PNG" );
 
  100    for ( 
const QByteArray &format : supportedImageFormats )
 
  103      if ( format == 
"svg" )
 
  110      if ( format != 
"png" )
 
  112        imageFormats << QStringLiteral( 
"*.%1 *.%2" ).arg( format, QString( format ).toUpper() );
 
  115    const QString formatByExtension = QStringLiteral( 
"%1 (%2)" ).arg( QObject::tr( 
"Format by Extension" ), imageFormats.join( QLatin1Char( 
' ' ) ) );
 
  119    for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
 
  126    const QString lastUsedDir = settings.
value( QStringLiteral( 
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
 
  128    QString selectedFilter = settings.
value( QStringLiteral( 
"UI/lastSaveAsImageFilter" ), QString() ).toString();
 
  129    if ( selectedFilter.isEmpty() )
 
  131      selectedFilter = formatByExtension;
 
  135    if ( defaultFilename.isNull() )
 
  138      initialPath = lastUsedDir;
 
  143      initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
 
  146    QString outputFileName;
 
  148#if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX) 
  149    outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, formatByExtension + QStringLiteral( 
";;" ) + 
qgsMapJoinKeys( filterMap, QStringLiteral( 
";;" ) ), &selectedFilter );
 
  152    std::unique_ptr<QFileDialog> fileDialog( 
new QFileDialog( parent, message, initialPath, formatByExtension + QStringLiteral( 
";;" ) + 
qgsMapJoinKeys( filterMap, QStringLiteral( 
";;" ) ) ) );
 
  155    fileDialog->setFileMode( QFileDialog::AnyFile );
 
  156    fileDialog->setAcceptMode( QFileDialog::AcceptSave );
 
  157    fileDialog->setOption( QFileDialog::DontConfirmOverwrite, 
false );
 
  159    if ( !selectedFilter.isEmpty() )     
 
  161      fileDialog->selectNameFilter( selectedFilter );
 
  165    if ( fileDialog->exec() == QDialog::Accepted )
 
  167      outputFileName = fileDialog->selectedFiles().first();
 
  171    if ( !outputFileName.isNull() )
 
  173      if ( selectedFilter == formatByExtension )
 
  175        settings.
setValue( QStringLiteral( 
"UI/lastSaveAsImageFilter" ), QString() );
 
  176        ext = QFileInfo( outputFileName ).suffix();
 
  178        auto match = std::find_if( filterMap.begin(), filterMap.end(), [&ext]( 
const QString & filter ) { return filter == ext; } );
 
  179        if ( match == filterMap.end() )
 
  182          ext = QStringLiteral( 
"png" );
 
  189        ext = filterMap.value( selectedFilter, QString() );
 
  190        if ( !ext.isEmpty() )
 
  193          settings.
setValue( QStringLiteral( 
"UI/lastSaveAsImageFilter" ), selectedFilter );
 
  196      settings.
setValue( QStringLiteral( 
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
 
  199    return qMakePair( outputFileName, ext );
 
 
  204    return QStringLiteral( 
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
 
 
  209    const QString longName = format.toUpper() + 
" format";
 
  210    const QString glob = 
"*." + format;
 
 
  214  QFont 
getFont( 
bool &ok, 
const QFont &initial, 
const QString &title )
 
  224    return QFontDialog::getFont( &ok, initial, 
nullptr, title, QFontDialog::DontUseNativeDialog );
 
  226    return QFontDialog::getFont( &ok, initial, 
nullptr, title );
 
 
  234    settings.
setValue( key, widget->saveGeometry() );
 
 
  241    return widget->restoreGeometry( settings.
value( key ).toByteArray() );
 
 
  247    if ( !keyName.isEmpty() )
 
  251    else if ( widget->objectName().isEmpty() )
 
  253      subKey = QString( widget->metaObject()->className() );
 
  257      subKey = widget->objectName();
 
  259    QString key = QStringLiteral( 
"Windows/%1/geometry" ).arg( subKey );
 
 
  271    const int w = s.
value( QStringLiteral( 
"/qgis/toolbarIconSize" ), 32 ).toInt();
 
  274    if ( dockableToolbar )
 
 
  284    int adjustedSize = 16;
 
  285    if ( size.width() > 32 )
 
  287      adjustedSize = size.width() - 16;
 
  289    else if ( size.width() == 32 )
 
  293    return QSize( adjustedSize, adjustedSize );
 
 
  299    QString result { QLocale().toString( value, 
'f', 
precision ) };
 
  300    if ( ! displayTrailingZeroes )
 
  302      const QRegularExpression zeroesRe { QStringLiteral( R
"raw(\%1\d*?(0+$))raw" ).arg( QLocale().decimalPoint() ) }; 
  303      if ( zeroesRe.match( result ).hasMatch() )
 
  305        result.truncate( zeroesRe.match( result ).capturedStart( 1 ) );
 
  306        if ( result.endsWith( QLocale().decimalPoint( ) ) )
 
 
  317    switch ( rasterDataType )
 
  335        return std::numeric_limits<float>::digits10 + 1;
 
  340        return std::numeric_limits<double>::digits10 + 1;
 
  344        return std::numeric_limits<double>::digits10 + 1;
 
 
 
  357  QApplication::setOverrideCursor( cursor );
 
 
  363    QApplication::restoreOverrideCursor();
 
 
  371  mHasOverride = 
false;
 
  372  QApplication::restoreOverrideCursor();
 
 
  382  while ( QApplication::overrideCursor() )
 
  384    mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
 
  385    QApplication::restoreOverrideCursor();
 
 
  396  for ( 
auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
 
  398    QApplication::setOverrideCursor( *it );
 
 
  410  mWidget->setUpdatesEnabled( 
false );
 
 
  418  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)