22 #include <QImageWriter> 23 #include <QFontDialog> 30 QString
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
36 QString lastUsedFilter = settings.
value(
"/UI/" + filterName,
"" ).toString();
37 QString lastUsedDir = settings.
value(
"/UI/" + filterName +
"Dir", QDir::homePath() ).toString();
39 QgsDebugMsg(
"Opening file dialog with filters: " + filters );
42 selectedFiles = QFileDialog::getOpenFileNames(
nullptr, title, lastUsedDir, filters, &lastUsedFilter );
49 openFileDialog->setFileMode( QFileDialog::ExistingFiles );
51 if ( !lastUsedFilter.isEmpty() )
53 openFileDialog->selectNameFilter( lastUsedFilter );
56 if ( openFileDialog->exec() == QDialog::Accepted )
58 selectedFiles = openFileDialog->selectedFiles();
70 if ( !selectedFiles.isEmpty() )
75 QString firstFileName = selectedFiles.first();
76 QFileInfo fi( firstFileName );
77 QString path = fi.path();
81 settings.
setValue(
"/UI/" + filterName, lastUsedFilter );
82 settings.
setValue(
"/UI/" + filterName +
"Dir", path );
87 QPair<QString, QString> GUI_EXPORT
getSaveAsImageName( QWidget *parent,
const QString &message,
const QString &defaultFilename )
90 QMap<QString, QString> filterMap;
91 Q_FOREACH (
const QByteArray &format, QImageWriter::supportedImageFormats() )
94 if ( format ==
"svg" )
102 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
109 QString lastUsedDir = settings.
value( QStringLiteral(
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
112 QString pngExtension = QStringLiteral(
"png" );
114 QString selectedFilter = settings.value( QStringLiteral(
"UI/lastSaveAsImageFilter" ), pngFilter ).toString();
117 if ( defaultFilename.isNull() )
120 initialPath = lastUsedDir;
125 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
128 QString outputFileName;
130 #if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX) 131 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, QStringList( filterMap.keys() ).join( QStringLiteral(
";;" ) ), &selectedFilter );
133 if ( !outputFileName.isNull() )
135 ext = filterMap.value( selectedFilter, QString() );
137 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), selectedFilter );
138 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
143 std::unique_ptr<QFileDialog> fileDialog(
new QFileDialog( parent, message, initialPath, QStringList( filterMap.keys() ).join(
";;" ) ) );
146 fileDialog->setFileMode( QFileDialog::AnyFile );
147 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
148 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
150 if ( !selectedFilter.isEmpty() )
152 fileDialog->selectNameFilter( selectedFilter );
156 if ( fileDialog->exec() == QDialog::Accepted )
158 outputFileName = fileDialog->selectedFiles().first();
161 selectedFilter = fileDialog->selectedNameFilter();
162 QgsDebugMsg(
"Selected filter: " + selectedFilter );
163 ext = filterMap.value( selectedFilter, QString() );
166 settings.setValue(
"/UI/lastSaveAsImageFilter", selectedFilter );
168 settings.setValue(
"/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
172 if ( !ext.isNull() && !outputFileName.endsWith(
'.' + ext.toLower(), Qt::CaseInsensitive ) )
174 outputFileName +=
'.' + ext;
177 return qMakePair<QString, QString>( outputFileName, ext );
182 return QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
187 QString longName = format.toUpper() +
" format";
188 QString glob =
"*." + format;
192 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
197 #if defined(Q_OS_MAC) && defined(QT_MAC_USE_COCOA) 209 settings.
setValue( key, widget->saveGeometry() );
216 return widget->restoreGeometry( settings.
value( key ).toByteArray() );
222 if ( !keyName.isEmpty() )
226 else if ( widget->objectName().isEmpty() )
228 subKey = QString( widget->metaObject()->className() );
232 subKey = widget->objectName();
234 QString key = QStringLiteral(
"Windows/%1/geometry" ).arg( subKey );
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.
This class is a composition of two QSettings instances:
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.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
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.
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.