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 Q_FOREACH (
const QByteArray &format, QImageWriter::supportedImageFormats() )
96 if ( format ==
"svg" )
103 QgsDebugMsg( QStringLiteral(
"Available Filters Map: " ) );
104 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
111 QString lastUsedDir = settings.
value( QStringLiteral(
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
114 QString pngExtension = QStringLiteral(
"png" );
116 QString selectedFilter = settings.value( QStringLiteral(
"UI/lastSaveAsImageFilter" ), pngFilter ).toString();
119 if ( defaultFilename.isNull() )
122 initialPath = lastUsedDir;
127 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
130 QString outputFileName;
132 #if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX) 133 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, QStringList( filterMap.keys() ).join( QStringLiteral(
";;" ) ), &selectedFilter );
135 if ( !outputFileName.isNull() )
137 ext = filterMap.value( selectedFilter, QString() );
139 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), selectedFilter );
140 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
145 std::unique_ptr<QFileDialog> fileDialog(
new QFileDialog( parent, message, initialPath, QStringList( filterMap.keys() ).join(
";;" ) ) );
148 fileDialog->setFileMode( QFileDialog::AnyFile );
149 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
150 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
152 if ( !selectedFilter.isEmpty() )
154 fileDialog->selectNameFilter( selectedFilter );
158 if ( fileDialog->exec() == QDialog::Accepted )
160 outputFileName = fileDialog->selectedFiles().first();
163 selectedFilter = fileDialog->selectedNameFilter();
164 QgsDebugMsg(
"Selected filter: " + selectedFilter );
165 ext = filterMap.value( selectedFilter, QString() );
168 settings.setValue(
"/UI/lastSaveAsImageFilter", selectedFilter );
170 settings.setValue(
"/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
174 if ( !ext.isNull() && !outputFileName.endsWith(
'.' + ext.toLower(), Qt::CaseInsensitive ) )
176 outputFileName +=
'.' + ext;
179 return qMakePair<QString, QString>( outputFileName, ext );
184 return QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
189 QString longName = format.toUpper() +
" format";
190 QString glob =
"*." + format;
194 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
199 #if defined(Q_OS_MAC) 214 settings.
setValue( key, widget->saveGeometry() );
221 return widget->restoreGeometry( settings.
value( key ).toByteArray() );
227 if ( !keyName.isEmpty() )
231 else if ( widget->objectName().isEmpty() )
233 subKey = QString( widget->metaObject()->className() );
237 subKey = widget->objectName();
239 QString key = QStringLiteral(
"Windows/%1/geometry" ).arg( subKey );
245 QFontMetrics fm( ( QFont() ) );
246 const double scale = 1.1 * standardSize / 24;
247 return static_cast< int >( std::floor( std::max(
Qgis::UI_SCALE_FACTOR * fm.height() * scale,
static_cast< double >( standardSize ) ) ) );
258 QApplication::setOverrideCursor( cursor );
264 QApplication::restoreOverrideCursor();
272 mHasOverride =
false;
273 QApplication::restoreOverrideCursor();
A file dialog which lets the user select the preferred encoding type for a data provider.
QString createFileFilter_(QString const &longName, QString const &glob)
Convenience function for readily creating file filters.
static const double UI_SCALE_FACTOR
UI scaling factor.
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.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
~QgsTemporaryCursorOverride()
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.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
The QgsGuiUtils namespace contains constants and helper functions used throughout the QGIS GUI...
void addCancelAll()
Adds a 'Cancel All' button for the user to click.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void release()
Releases the cursor override early (i.e.
bool cancelAll()
Returns true if the user clicked 'Cancel All'.
QString createWidgetKey(QWidget *widget, const QString &keyName)
Creates a key for the given widget that can be used to store related data in settings.
QgsTemporaryCursorOverride(const QCursor &cursor)
Constructor for QgsTemporaryCursorOverride.
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...
QFont getFont(bool &ok, const QFont &initial, const QString &title)
Show font selection dialog.