22 #include <QImageWriter> 23 #include <QFontDialog> 24 #include <QApplication> 31 QString
const &filters, QStringList &selectedFiles, QString &enc, QString &title,
37 QString lastUsedFilter = settings.
value(
"/UI/" + filterName,
"" ).toString();
38 QString lastUsedDir = settings.
value(
"/UI/" + filterName +
"Dir", QDir::homePath() ).toString();
40 QgsDebugMsg(
"Opening file dialog with filters: " + filters );
43 selectedFiles = QFileDialog::getOpenFileNames(
nullptr, title, lastUsedDir, filters, &lastUsedFilter );
50 openFileDialog->setFileMode( QFileDialog::ExistingFiles );
52 if ( !lastUsedFilter.isEmpty() )
54 openFileDialog->selectNameFilter( lastUsedFilter );
57 if ( openFileDialog->exec() == QDialog::Accepted )
59 selectedFiles = openFileDialog->selectedFiles();
71 if ( !selectedFiles.isEmpty() )
76 QString firstFileName = selectedFiles.first();
77 QFileInfo fi( firstFileName );
78 QString path = fi.path();
82 settings.
setValue(
"/UI/" + filterName, lastUsedFilter );
83 settings.
setValue(
"/UI/" + filterName +
"Dir", path );
88 QPair<QString, QString> GUI_EXPORT
getSaveAsImageName( QWidget *parent,
const QString &message,
const QString &defaultFilename )
91 QMap<QString, QString> filterMap;
92 Q_FOREACH (
const QByteArray &format, QImageWriter::supportedImageFormats() )
95 if ( format ==
"svg" )
103 for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
110 QString lastUsedDir = settings.
value( QStringLiteral(
"UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
113 QString pngExtension = QStringLiteral(
"png" );
115 QString selectedFilter = settings.value( QStringLiteral(
"UI/lastSaveAsImageFilter" ), pngFilter ).toString();
118 if ( defaultFilename.isNull() )
121 initialPath = lastUsedDir;
126 initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
129 QString outputFileName;
131 #if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX) 132 outputFileName = QFileDialog::getSaveFileName( parent, message, initialPath, QStringList( filterMap.keys() ).join( QStringLiteral(
";;" ) ), &selectedFilter );
134 if ( !outputFileName.isNull() )
136 ext = filterMap.value( selectedFilter, QString() );
138 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageFilter" ), selectedFilter );
139 settings.setValue( QStringLiteral(
"UI/lastSaveAsImageDir" ), QFileInfo( outputFileName ).absolutePath() );
144 std::unique_ptr<QFileDialog> fileDialog(
new QFileDialog( parent, message, initialPath, QStringList( filterMap.keys() ).join(
";;" ) ) );
147 fileDialog->setFileMode( QFileDialog::AnyFile );
148 fileDialog->setAcceptMode( QFileDialog::AcceptSave );
149 fileDialog->setOption( QFileDialog::DontConfirmOverwrite,
false );
151 if ( !selectedFilter.isEmpty() )
153 fileDialog->selectNameFilter( selectedFilter );
157 if ( fileDialog->exec() == QDialog::Accepted )
159 outputFileName = fileDialog->selectedFiles().first();
162 selectedFilter = fileDialog->selectedNameFilter();
163 QgsDebugMsg(
"Selected filter: " + selectedFilter );
164 ext = filterMap.value( selectedFilter, QString() );
167 settings.setValue(
"/UI/lastSaveAsImageFilter", selectedFilter );
169 settings.setValue(
"/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
173 if ( !ext.isNull() && !outputFileName.endsWith(
'.' + ext.toLower(), Qt::CaseInsensitive ) )
175 outputFileName +=
'.' + ext;
178 return qMakePair<QString, QString>( outputFileName, ext );
183 return QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() );
188 QString longName = format.toUpper() +
" format";
189 QString glob =
"*." + format;
193 QFont
getFont(
bool &ok,
const QFont &initial,
const QString &title )
198 #if defined(Q_OS_MAC) && defined(QT_MAC_USE_COCOA) 210 settings.
setValue( key, widget->saveGeometry() );
217 return widget->restoreGeometry( settings.
value( key ).toByteArray() );
223 if ( !keyName.isEmpty() )
227 else if ( widget->objectName().isEmpty() )
229 subKey = QString( widget->metaObject()->className() );
233 subKey = widget->objectName();
235 QString key = QStringLiteral(
"Windows/%1/geometry" ).arg( subKey );
246 QApplication::setOverrideCursor( cursor );
252 QApplication::restoreOverrideCursor();
260 mHasOverride =
false;
261 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.
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.
~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.