23 #include <QImageWriter>
24 #include <QFontDialog>
25 #include <QApplication>
32 QString
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
38 QString lastUsedFilter = settings.
value(
"/UI/" + filterName,
"" ).toString();
39 QString lastUsedDir = settings.
value(
"/UI/" + filterName +
"Dir", QDir::homePath() ).toString();
41 QgsDebugMsg(
"Opening file dialog with filters: " + filters );
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 QString firstFileName = selectedFiles.first();
78 QFileInfo fi( firstFileName );
79 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 for (
const QByteArray &format : supportedImageFormats )
97 if ( format ==
"svg" )
105 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
112 QString lastUsedDir = settings.
value( QStringLiteral(
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
115 QString pngExtension = QStringLiteral(
"png" );
117 QString selectedFilter = settings.
value( QStringLiteral(
"UI/lastSaveAsImageFilter" ), pngFilter ).toString();
120 if ( defaultFilename.isNull() )
123 initialPath = lastUsedDir;
128 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
131 QString outputFileName;
133 #if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX)
134 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, QStringList( filterMap.keys() ).join( QStringLiteral(
";;" ) ), &selectedFilter );
136 if ( !outputFileName.isNull() )
138 ext = filterMap.value( selectedFilter, QString() );
140 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), selectedFilter );
141 settings.
setValue( QStringLiteral(
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
146 std::unique_ptr<QFileDialog> fileDialog(
new QFileDialog( parent, message, initialPath, QStringList( filterMap.keys() ).join(
";;" ) ) );
149 fileDialog->setFileMode( QFileDialog::AnyFile );
150 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
151 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
153 if ( !selectedFilter.isEmpty() )
155 fileDialog->selectNameFilter( selectedFilter );
159 if ( fileDialog->exec() == QDialog::Accepted )
161 outputFileName = fileDialog->selectedFiles().first();
164 selectedFilter = fileDialog->selectedNameFilter();
165 QgsDebugMsg(
"Selected filter: " + selectedFilter );
166 ext = filterMap.value( selectedFilter, QString() );
169 settings.
setValue(
"/UI/lastSaveAsImageFilter", selectedFilter );
171 settings.
setValue(
"/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
175 if ( !ext.isNull() && !outputFileName.endsWith(
'.' + ext.toLower(), Qt::CaseInsensitive ) )
177 outputFileName +=
'.' + ext;
180 return qMakePair<QString, QString>( outputFileName, ext );
185 return QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
190 QString longName = format.toUpper() +
" format";
191 QString glob =
"*." + format;
195 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
200 #if defined(Q_OS_MAC)
211 void saveGeometry( QWidget *widget,
const QString &keyName )
215 settings.
setValue( key, widget->saveGeometry() );
222 return widget->restoreGeometry( settings.
value( key ).toByteArray() );
228 if ( !keyName.isEmpty() )
232 else if ( widget->objectName().isEmpty() )
234 subKey = QString( widget->metaObject()->className() );
238 subKey = widget->objectName();
240 QString key = QStringLiteral(
"Windows/%1/geometry" ).arg( subKey );
246 QFontMetrics fm( ( QFont() ) );
247 const double scale = 1.1 * standardSize / 24;
248 return static_cast< int >( std::floor( std::max(
Qgis::UI_SCALE_FACTOR * fm.height() * scale,
static_cast< double >( standardSize ) ) ) );
251 QSize
iconSize(
bool dockableToolbar )
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 );
286 QApplication::setOverrideCursor( cursor );
292 QApplication::restoreOverrideCursor();
300 mHasOverride =
false;
301 QApplication::restoreOverrideCursor();
311 while ( QApplication::overrideCursor() )
313 mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
314 QApplication::restoreOverrideCursor();
325 for (
auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
327 QApplication::setOverrideCursor( *it );