18 #include "qgssettings.h"
24 #include <QImageWriter>
25 #include <QFontDialog>
26 #include <QApplication>
27 #include <QRegularExpression>
34 QString
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
40 QString lastUsedFilter = settings.value(
"/UI/" + filterName,
"" ).toString();
41 QString lastUsedDir = settings.value(
"/UI/" + filterName +
"Dir", QDir::homePath() ).toString();
43 QgsDebugMsg(
"Opening file dialog with filters: " + filters );
46 selectedFiles = QFileDialog::getOpenFileNames(
nullptr, title, lastUsedDir, filters, &lastUsedFilter );
53 openFileDialog->setFileMode( QFileDialog::ExistingFiles );
55 if ( !lastUsedFilter.isEmpty() )
57 openFileDialog->selectNameFilter( lastUsedFilter );
60 if ( openFileDialog->exec() == QDialog::Accepted )
62 selectedFiles = openFileDialog->selectedFiles();
74 if ( !selectedFiles.isEmpty() )
79 QString firstFileName = selectedFiles.first();
80 QFileInfo fi( firstFileName );
81 QString path = fi.path();
85 settings.setValue(
"/UI/" + filterName, lastUsedFilter );
86 settings.setValue(
"/UI/" + filterName +
"Dir", path );
91 QPair<QString, QString> GUI_EXPORT
getSaveAsImageName( QWidget *parent,
const QString &message,
const QString &defaultFilename )
94 QMap<QString, QString> filterMap;
95 const auto supportedImageFormats { QImageWriter::supportedImageFormats() };
96 for (
const QByteArray &format : supportedImageFormats )
99 if ( format ==
"svg" )
107 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
113 QgsSettings settings;
114 QString lastUsedDir = settings.value( QStringLiteral(
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
117 QString pngExtension = QStringLiteral(
"png" );
119 QString selectedFilter = settings.value( QStringLiteral(
"UI/lastSaveAsImageFilter" ), pngFilter ).toString();
122 if ( defaultFilename.isNull() )
125 initialPath = lastUsedDir;
130 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
133 QString outputFileName;
135 #if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX)
136 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, QStringList( filterMap.keys() ).join( QLatin1String(
";;" ) ), &selectedFilter );
138 if ( !outputFileName.isNull() )
140 ext = filterMap.value( selectedFilter, QString() );
142 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), selectedFilter );
143 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
148 std::unique_ptr<QFileDialog> fileDialog(
new QFileDialog( parent, message, initialPath, QStringList( filterMap.keys() ).join(
";;" ) ) );
151 fileDialog->setFileMode( QFileDialog::AnyFile );
152 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
153 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
155 if ( !selectedFilter.isEmpty() )
157 fileDialog->selectNameFilter( selectedFilter );
161 if ( fileDialog->exec() == QDialog::Accepted )
163 outputFileName = fileDialog->selectedFiles().first();
166 selectedFilter = fileDialog->selectedNameFilter();
167 QgsDebugMsg(
"Selected filter: " + selectedFilter );
168 ext = filterMap.value( selectedFilter, QString() );
171 settings.setValue(
"/UI/lastSaveAsImageFilter", selectedFilter );
173 settings.setValue(
"/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
177 if ( !ext.isNull() && !outputFileName.endsWith(
'.' + ext.toLower(), Qt::CaseInsensitive ) )
179 outputFileName +=
'.' + ext;
182 return qMakePair<QString, QString>( outputFileName, ext );
187 return QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
192 QString longName = format.toUpper() +
" format";
193 QString glob =
"*." + format;
197 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
202 #if defined(Q_OS_MAC)
215 QgsSettings settings;
217 settings.setValue( key, widget->saveGeometry() );
222 QgsSettings settings;
224 return widget->restoreGeometry( settings.value( key ).toByteArray() );
230 if ( !keyName.isEmpty() )
234 else if ( widget->objectName().isEmpty() )
236 subKey = QString( widget->metaObject()->className() );
240 subKey = widget->objectName();
242 QString key = QStringLiteral(
"Windows/%1/geometry" ).arg( subKey );
254 int w = s.value( QStringLiteral(
"/qgis/iconSize" ), 32 ).toInt();
257 if ( dockableToolbar )
267 int adjustedSize = 16;
268 if ( size.width() > 32 )
270 adjustedSize = size.width() - 16;
272 else if ( size.width() == 32 )
276 return QSize( adjustedSize, adjustedSize );
282 QString result { QLocale().toString( value,
'f',
precision ) };
283 if ( ! displayTrailingZeroes )
285 const QRegularExpression zeroesRe { QStringLiteral( R
"raw(\%1\d*?(0+$))raw" ).arg( QLocale().decimalPoint() ) };
286 if ( zeroesRe.match( result ).hasMatch() )
288 result.truncate( zeroesRe.match( result ).capturedStart( 1 ) );
289 if ( result.endsWith( QLocale().decimalPoint( ) ) )
300 switch ( rasterDataType )
317 return std::numeric_limits<float>::digits10 + 1;
322 return std::numeric_limits<double>::digits10 + 1;
326 return std::numeric_limits<double>::digits10 + 1;
339 QApplication::setOverrideCursor( cursor );
345 QApplication::restoreOverrideCursor();
353 mHasOverride =
false;
354 QApplication::restoreOverrideCursor();
364 while ( QApplication::overrideCursor() )
366 mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
367 QApplication::restoreOverrideCursor();
378 for (
auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
380 QApplication::setOverrideCursor( *it );
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.
@ 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.
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...
#define QgsDebugMsgLevel(str, level)