diff options
author | Michael Weghorn <m.weghorn@posteo.de> | 2019-02-05 10:59:15 +0100 |
---|---|---|
committer | Katarina Behrens <Katarina.Behrens@cib.de> | 2019-04-16 10:47:00 +0200 |
commit | 0eeb3ef5816084afb96f5803375e6a3a1dd89400 (patch) | |
tree | 8db10772b0dc99e13a834ae8d706c162581b6aa2 /vcl | |
parent | 7bba93a99ebb4250f884a68a50aa1912d96f4ba8 (diff) |
tdf#123058 qt5 fpicker: Honor autoextension setting
Store the file extension associated with the named
filters in a map, and use that information to
set the default file extension in QFileDialog
accordingly if the corresponding checkbox in the dialog
is enabled.
Change-Id: I66f4f35da5d4378ac6337429e39260a4ed710a24
Reviewed-on: https://gerrit.libreoffice.org/67392
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
(cherry picked from commit 9a6818bd73198e6dba3414579a35db6e87dbeb66)
Reviewed-on: https://gerrit.libreoffice.org/70763
Reviewed-by: Katarina Behrens <Katarina.Behrens@cib.de>
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/inc/qt5/Qt5FilePicker.hxx | 4 | ||||
-rw-r--r-- | vcl/qt5/Qt5FilePicker.cxx | 41 |
2 files changed, 45 insertions, 0 deletions
diff --git a/vcl/inc/qt5/Qt5FilePicker.hxx b/vcl/inc/qt5/Qt5FilePicker.hxx index d4802795c588..dbe8020d30ca 100644 --- a/vcl/inc/qt5/Qt5FilePicker.hxx +++ b/vcl/inc/qt5/Qt5FilePicker.hxx @@ -63,6 +63,8 @@ protected: QStringList m_aNamedFilterList; ///< to keep the original sequence QHash<QString, QString> m_aTitleToFilterMap; + // to retrieve the filename extension for a given filter + QHash<QString, QString> m_aNamedFilterToExtensionMap; QString m_aCurrentFilter; QWidget* m_pExtraControls; ///< widget to contain extra custom controls @@ -262,6 +264,8 @@ private Q_SLOTS: void filterSelected(const QString&); // emit XFilePickerListener fileSelectionChanged event void currentChanged(const QString&); + // (un)set automatic file extension + void updateAutomaticFileExtension(); }; /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/qt5/Qt5FilePicker.cxx b/vcl/qt5/Qt5FilePicker.cxx index 7817f4de0488..6db64b3b0824 100644 --- a/vcl/qt5/Qt5FilePicker.cxx +++ b/vcl/qt5/Qt5FilePicker.cxx @@ -150,6 +150,10 @@ Qt5FilePicker::Qt5FilePicker(QFileDialog::FileMode eMode) SLOT(filterSelected(const QString&))); connect(m_pFileDialog.get(), SIGNAL(currentChanged(const QString&)), this, SLOT(currentChanged(const QString&))); + + // update automatic file extension when filter is changed + connect(m_pFileDialog.get(), SIGNAL(filterSelected(const QString&)), this, + SLOT(updateAutomaticFileExtension())); } Qt5FilePicker::~Qt5FilePicker() {} @@ -212,6 +216,8 @@ sal_Int16 SAL_CALL Qt5FilePicker::execute() m_pFileDialog->setFocusProxy(pTransientParent); } + updateAutomaticFileExtension(); + int result = m_pFileDialog->exec(); if (QFileDialog::Rejected == result) return ExecutableDialogResults::CANCEL; @@ -319,6 +325,7 @@ void SAL_CALL Qt5FilePicker::appendFilter(const OUString& title, const OUString& m_aNamedFilterList << QStringLiteral("%1 (%2)").arg(n, f); m_aTitleToFilterMap[t] = m_aNamedFilterList.constLast(); + m_aNamedFilterToExtensionMap[m_aNamedFilterList.constLast()] = f; } void SAL_CALL Qt5FilePicker::setCurrentFilter(const OUString& title) @@ -562,6 +569,7 @@ void Qt5FilePicker::addCustomControl(sal_Int16 controlId) QWidget* widget = nullptr; QLabel* label = nullptr; const char* resId = nullptr; + QCheckBox* pCheckbox = nullptr; switch (controlId) { @@ -611,6 +619,12 @@ void Qt5FilePicker::addCustomControl(sal_Int16 controlId) switch (controlId) { case CHECKBOX_AUTOEXTENSION: + pCheckbox = new QCheckBox(getResString(resId), m_pExtraControls); + // to add/remove automatic file extension based on checkbox + connect(pCheckbox, SIGNAL(stateChanged(int)), this, + SLOT(updateAutomaticFileExtension())); + widget = pCheckbox; + break; case CHECKBOX_PASSWORD: case CHECKBOX_FILTEROPTIONS: case CHECKBOX_READONLY: @@ -796,6 +810,33 @@ uno::Sequence<OUString> SAL_CALL Qt5FilePicker::getSupportedServiceNames() return FilePicker_getSupportedServiceNames(); } +void Qt5FilePicker::updateAutomaticFileExtension() +{ + bool bSetAutoExtension + = getValue(CHECKBOX_AUTOEXTENSION, ControlActions::GET_SELECTED_ITEM).get<bool>(); + if (bSetAutoExtension) + { + QString sSuffix = m_aNamedFilterToExtensionMap.value(m_pFileDialog->selectedNameFilter()); + // string is "*.<SUFFIX>" if a specific filter was selected that has exactly one possible file extension + if (sSuffix.lastIndexOf("*.") == 0) + { + sSuffix = sSuffix.remove("*."); + m_pFileDialog->setDefaultSuffix(sSuffix); + } + else + { + // fall back to setting none otherwise + SAL_INFO( + "vcl.qt5", + "Unable to retrieve unambiguous file extension. Will not add any automatically."); + bSetAutoExtension = false; + } + } + + if (!bSetAutoExtension) + m_pFileDialog->setDefaultSuffix(""); +} + void Qt5FilePicker::filterSelected(const QString&) { FilePickerEvent aEvent; |