summaryrefslogtreecommitdiff
path: root/vcl/qt5/Qt5FilePicker.cxx
diff options
context:
space:
mode:
authorMichael Weghorn <m.weghorn@posteo.de>2019-02-05 10:59:15 +0100
committerMichael Weghorn <m.weghorn@posteo.de>2019-02-05 16:12:57 +0100
commit9a6818bd73198e6dba3414579a35db6e87dbeb66 (patch)
tree18b7342187452862e991bfa14953ac63d36edee5 /vcl/qt5/Qt5FilePicker.cxx
parent8e3afdb5989d571410350f1d43fcf26492a4eaff (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>
Diffstat (limited to 'vcl/qt5/Qt5FilePicker.cxx')
-rw-r--r--vcl/qt5/Qt5FilePicker.cxx41
1 files changed, 41 insertions, 0 deletions
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;