LibreOffice Module vcl (master)  1
Qt5FilePicker.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include <vclpluginapi.h>
23 
24 #include <cppuhelper/compbase.hxx>
25 
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
29 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
30 #include <com/sun/star/ui/dialogs/XFolderPicker2.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
32 
33 #include <osl/conditn.hxx>
34 #include <osl/mutex.hxx>
35 
36 #include <rtl/ustrbuf.hxx>
37 
38 #include <QtCore/QObject>
39 #include <QtCore/QString>
40 #include <QtCore/QStringList>
41 #include <QtCore/QHash>
42 #include <QtWidgets/QFileDialog>
43 
44 #include <memory>
45 
46 class QComboBox;
47 class QGridLayout;
48 class QLabel;
49 class QWidget;
50 
51 typedef ::cppu::WeakComponentImplHelper<
52  css::ui::dialogs::XFilePicker3, css::ui::dialogs::XFilePickerControlAccess,
53  css::ui::dialogs::XFolderPicker2, css::lang::XInitialization, css::lang::XServiceInfo>
55 
56 class VCLPLUG_QT5_PUBLIC Qt5FilePicker : public QObject, public Qt5FilePicker_Base
57 {
58  Q_OBJECT
59 
60 private:
61  css::uno::Reference<css::uno::XComponentContext> m_context;
62 
63  // whether to show (i.e. not remove) the file extension in the filter title,
64  // e.g. whether to use "ODF Text Document (*.odt)" or just
65  // "ODF Text Document" as filter title
66  // (non-native QFileDialog e.g. adds that information by itself anyway)
68 
69 protected:
70  css::uno::Reference<css::ui::dialogs::XFilePickerListener> m_xListener;
71 
72  std::unique_ptr<QFileDialog> m_pFileDialog;
73 
74  osl::Mutex m_aHelperMutex;
75 
76  QStringList m_aNamedFilterList;
77  QHash<QString, QString> m_aTitleToFilterMap;
78  // to retrieve the filename extension for a given filter
79  QHash<QString, QString> m_aNamedFilterToExtensionMap;
81 
82  QWidget* m_pExtraControls;
83  QGridLayout* m_pLayout;
84  QLabel* m_pFilenameLabel;
85  QLabel* m_pFilterLabel;
86  QHash<sal_Int16, QWidget*> m_aCustomWidgetsMap;
87 
89 
90 public:
91  // use non-native file dialog by default; there's no easy way to add custom widgets
92  // in a generic way in the native one
93  explicit Qt5FilePicker(css::uno::Reference<css::uno::XComponentContext> const& context,
94  QFileDialog::FileMode, bool bShowFileExtensionInFilterTitle = false,
95  bool bUseNativeDialog = false);
96  virtual ~Qt5FilePicker() override;
97 
98  // XFilePickerNotifier
99  virtual void SAL_CALL addFilePickerListener(
100  const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
101  virtual void SAL_CALL removeFilePickerListener(
102  const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
103 
104  // XFilterManager functions
105  virtual void SAL_CALL appendFilter(const OUString& rTitle, const OUString& rFilter) override;
106  virtual void SAL_CALL setCurrentFilter(const OUString& rTitle) override;
107  virtual OUString SAL_CALL getCurrentFilter() override;
108 
109  // XFilterGroupManager functions
110  virtual void SAL_CALL
111  appendFilterGroup(const OUString& rGroupTitle,
112  const css::uno::Sequence<css::beans::StringPair>& rFilters) override;
113 
114  // XCancellable
115  virtual void SAL_CALL cancel() override;
116 
117  // XExecutableDialog functions
118  virtual void SAL_CALL setTitle(const OUString& rTitle) override;
119  virtual sal_Int16 SAL_CALL execute() override;
120 
121  // XFilePicker functions
122  virtual void SAL_CALL setMultiSelectionMode(sal_Bool bMode) override;
123  virtual void SAL_CALL setDefaultName(const OUString& rName) override;
124  virtual void SAL_CALL setDisplayDirectory(const OUString& rDirectory) override;
125  virtual OUString SAL_CALL getDisplayDirectory() override;
126  virtual css::uno::Sequence<OUString> SAL_CALL getFiles() override;
127 
128  // XFilePickerControlAccess functions
129  virtual void SAL_CALL setValue(sal_Int16 nControlId, sal_Int16 nControlAction,
130  const css::uno::Any& rValue) override;
131  virtual css::uno::Any SAL_CALL getValue(sal_Int16 nControlId,
132  sal_Int16 nControlAction) override;
133  virtual void SAL_CALL enableControl(sal_Int16 nControlId, sal_Bool bEnable) override;
134  virtual void SAL_CALL setLabel(sal_Int16 nControlId, const OUString& rLabel) override;
135  virtual OUString SAL_CALL getLabel(sal_Int16 nControlId) override;
136 
137  // XFilePicker2 functions
138  virtual css::uno::Sequence<OUString> SAL_CALL getSelectedFiles() override;
139 
140  // XInitialization
141  virtual void SAL_CALL initialize(const css::uno::Sequence<css::uno::Any>& rArguments) override;
142 
143  // XEventListener
145  virtual void disposing(const css::lang::EventObject& rEvent);
146  using cppu::WeakComponentImplHelperBase::disposing;
147 
148  // XServiceInfo
149  virtual OUString SAL_CALL getImplementationName() override;
150  virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
151  virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
152 
153  // XFolderPicker functions
154  virtual OUString SAL_CALL getDirectory() override;
155  virtual void SAL_CALL setDescription(const OUString& rDescription) override;
156 
157 protected:
158  static css::uno::Any handleGetListValue(const QComboBox* pWidget, sal_Int16 nControlAction);
159  static void handleSetListValue(QComboBox* pQComboBox, sal_Int16 nAction,
160  const css::uno::Any& rValue);
161  virtual void addCustomControl(sal_Int16 controlId);
162  void setCustomControlWidgetLayout(QGridLayout* pLayout) { m_pLayout = pLayout; }
163 
164 private:
165  Qt5FilePicker(const Qt5FilePicker&) = delete;
166  Qt5FilePicker& operator=(const Qt5FilePicker&) = delete;
167 
168  static QString getResString(const char* pRedId);
169 
170 private Q_SLOTS:
171  // emit XFilePickerListener controlStateChanged event
172  void filterSelected(const QString&);
173  // emit XFilePickerListener fileSelectionChanged event
174  void currentChanged(const QString&);
175  // (un)set automatic file extension
176  void updateAutomaticFileExtension();
177 };
178 
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, rtl_uString *keyName, RegValueType valueType, RegValue pData, sal_uInt32 valueSize)
bool m_bShowFileExtensionInFilterTitle
QHash< sal_Int16, QWidget * > m_aCustomWidgetsMap
map of SAL control ID's to widget
::cppu::WeakComponentImplHelper< css::ui::dialogs::XFilePicker3, css::ui::dialogs::XFilePickerControlAccess, css::ui::dialogs::XFolderPicker2, css::lang::XInitialization, css::lang::XServiceInfo > Qt5FilePicker_Base
css::beans::Optional< css::uno::Any > getValue(OUString const &id)
OUString getImplementationName()
QWidget * m_pExtraControls
widget to contain extra custom controls
#define VCLPLUG_QT5_PUBLIC
Definition: vclpluginapi.h:53
css::uno::Reference< css::ui::dialogs::XFilePickerListener > m_xListener
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
QHash< QString, QString > m_aTitleToFilterMap
osl::Mutex m_aHelperMutex
mutex used by the WeakComponentImplHelper
QHash< QString, QString > m_aNamedFilterToExtensionMap
QLabel * m_pFilterLabel
label to display the filter
css::uno::Sequence< OUString > getSupportedServiceNames()
QStringList m_aNamedFilterList
to keep the original sequence
QLabel * m_pFilenameLabel
label to display the filename
unsigned char sal_Bool
css::uno::Reference< css::uno::XComponentContext > m_context
void setCustomControlWidgetLayout(QGridLayout *pLayout)
QString m_aCurrentFilter
OUString getResString(sal_Int16 aId)
QGridLayout * m_pLayout
layout for extra custom controls
std::unique_ptr< QFileDialog > m_pFileDialog
the non-native file picker dialog