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/frame/XTerminateListener.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
30 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
31 #include <com/sun/star/ui/dialogs/XFolderPicker2.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 
34 #include <osl/conditn.hxx>
35 #include <osl/mutex.hxx>
36 
37 #include <QtCore/QObject>
38 #include <QtCore/QString>
39 #include <QtCore/QStringList>
40 #include <QtCore/QHash>
41 #include <QtWidgets/QFileDialog>
42 
43 #include <memory>
44 
45 class QComboBox;
46 class QGridLayout;
47 class QLabel;
48 class QWidget;
49 
50 typedef ::cppu::WeakComponentImplHelper<css::frame::XTerminateListener, css::lang::XInitialization,
51  css::lang::XServiceInfo, css::ui::dialogs::XFilePicker3,
52  css::ui::dialogs::XFilePickerControlAccess,
53  css::ui::dialogs::XFolderPicker2>
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  css::uno::Reference<css::ui::dialogs::XFilePickerListener> m_xListener;
64 
65  osl::Mutex m_aHelperMutex;
66 
67  QStringList m_aNamedFilterList;
68  QHash<QString, QString> m_aTitleToFilterMap;
69  // to retrieve the filename extension for a given filter
70  QHash<QString, QString> m_aNamedFilterToExtensionMap;
72 
73  QGridLayout* m_pLayout;
74  QHash<sal_Int16, QWidget*> m_aCustomWidgetsMap;
75 
76  const bool m_bIsFolderPicker;
77 
78  QWidget* m_pParentWidget;
79 
80 protected:
81  std::unique_ptr<QFileDialog> m_pFileDialog;
82  QWidget* m_pExtraControls;
83 
84 public:
85  // use non-native file dialog by default; there's no easy way to add custom widgets
86  // in a generic way in the native one
87  explicit Qt5FilePicker(css::uno::Reference<css::uno::XComponentContext> const& context,
88  QFileDialog::FileMode, bool bUseNative = false);
89  virtual ~Qt5FilePicker() override;
90 
91  // XFilePickerNotifier
92  virtual void SAL_CALL addFilePickerListener(
93  const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
94  virtual void SAL_CALL removeFilePickerListener(
95  const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
96 
97  // XFilterManager functions
98  virtual void SAL_CALL appendFilter(const OUString& rTitle, const OUString& rFilter) override;
99  virtual void SAL_CALL setCurrentFilter(const OUString& rTitle) override;
100  virtual OUString SAL_CALL getCurrentFilter() override;
101 
102  // XFilterGroupManager functions
103  virtual void SAL_CALL
104  appendFilterGroup(const OUString& rGroupTitle,
105  const css::uno::Sequence<css::beans::StringPair>& rFilters) override;
106 
107  // XCancellable
108  virtual void SAL_CALL cancel() override;
109 
110  // XExecutableDialog functions
111  virtual void SAL_CALL setTitle(const OUString& rTitle) override;
112  virtual sal_Int16 SAL_CALL execute() override;
113 
114  // XFilePicker functions
115  virtual void SAL_CALL setMultiSelectionMode(sal_Bool bMode) override;
116  virtual void SAL_CALL setDefaultName(const OUString& rName) override;
117  virtual void SAL_CALL setDisplayDirectory(const OUString& rDirectory) override;
118  virtual OUString SAL_CALL getDisplayDirectory() override;
119  virtual css::uno::Sequence<OUString> SAL_CALL getFiles() override;
120 
121  // XFilePickerControlAccess functions
122  virtual void SAL_CALL setValue(sal_Int16 nControlId, sal_Int16 nControlAction,
123  const css::uno::Any& rValue) override;
124  virtual css::uno::Any SAL_CALL getValue(sal_Int16 nControlId,
125  sal_Int16 nControlAction) override;
126  virtual void SAL_CALL enableControl(sal_Int16 nControlId, sal_Bool bEnable) override;
127  virtual void SAL_CALL setLabel(sal_Int16 nControlId, const OUString& rLabel) override;
128  virtual OUString SAL_CALL getLabel(sal_Int16 nControlId) override;
129 
130  // XFilePicker2 functions
131  virtual css::uno::Sequence<OUString> SAL_CALL getSelectedFiles() override;
132 
133  // XInitialization
134  virtual void SAL_CALL initialize(const css::uno::Sequence<css::uno::Any>& rArguments) override;
135 
136  // XEventListener
137  void SAL_CALL disposing(const css::lang::EventObject& rEvent) override;
138  using cppu::WeakComponentImplHelperBase::disposing;
139 
140  // XServiceInfo
141  virtual OUString SAL_CALL getImplementationName() override;
142  virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
143  virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
144 
145  // XFolderPicker functions
146  virtual OUString SAL_CALL getDirectory() override;
147  virtual void SAL_CALL setDescription(const OUString& rDescription) override;
148 
149  // XTerminateListener
150  void SAL_CALL queryTermination(const css::lang::EventObject& aEvent) override;
151  void SAL_CALL notifyTermination(const css::lang::EventObject& aEvent) override;
152 
153 protected:
154  virtual void addCustomControl(sal_Int16 controlId);
155  void setCustomControlWidgetLayout(QGridLayout* pLayout) { m_pLayout = pLayout; }
156 
157 private:
158  Qt5FilePicker(const Qt5FilePicker&) = delete;
159  Qt5FilePicker& operator=(const Qt5FilePicker&) = delete;
160 
161  static QString getResString(const char* pRedId);
162  static css::uno::Any handleGetListValue(const QComboBox* pWidget, sal_Int16 nControlAction);
163  static void handleSetListValue(QComboBox* pQComboBox, sal_Int16 nAction,
164  const css::uno::Any& rValue);
165 
166 private Q_SLOTS:
167  // emit XFilePickerListener controlStateChanged event
168  void filterSelected(const QString&);
169  // emit XFilePickerListener fileSelectionChanged event
170  void currentChanged(const QString&);
171  // (un)set automatic file extension
172  virtual void updateAutomaticFileExtension();
173 };
174 
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, rtl_uString *keyName, RegValueType valueType, RegValue pData, sal_uInt32 valueSize)
QHash< sal_Int16, QWidget * > m_aCustomWidgetsMap
map of SAL control ID's to widget
css::beans::Optional< css::uno::Any > getValue(OUString const &id)
OUString getImplementationName()
QWidget * m_pExtraControls
widget to contain extra custom controls
::cppu::WeakComponentImplHelper< css::frame::XTerminateListener, css::lang::XInitialization, css::lang::XServiceInfo, css::ui::dialogs::XFilePicker3, css::ui::dialogs::XFilePickerControlAccess, css::ui::dialogs::XFolderPicker2 > Qt5FilePicker_Base
#define VCLPLUG_QT5_PUBLIC
Definition: vclpluginapi.h:53
css::uno::Reference< css::ui::dialogs::XFilePickerListener > m_xListener
QWidget * m_pParentWidget
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
css::uno::Sequence< OUString > getSupportedServiceNames()
QStringList m_aNamedFilterList
to keep the original sequence
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 file picker dialog
const bool m_bIsFolderPicker