[Libreoffice-commits] core.git: vcl/inc vcl/qt5

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Tue Feb 5 15:13:22 UTC 2019


 vcl/inc/qt5/Qt5FilePicker.hxx |    4 ++++
 vcl/qt5/Qt5FilePicker.cxx     |   41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

New commits:
commit 9a6818bd73198e6dba3414579a35db6e87dbeb66
Author:     Michael Weghorn <m.weghorn at posteo.de>
AuthorDate: Tue Feb 5 10:59:15 2019 +0100
Commit:     Michael Weghorn <m.weghorn at posteo.de>
CommitDate: Tue Feb 5 16:12:57 2019 +0100

    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 at posteo.de>

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;


More information about the Libreoffice-commits mailing list