1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Dialogs module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquickqfiledialog_p.h"
41#include "qquickitem.h"
42
43#include <private/qguiapplication_p.h>
44#include <private/qqmlcontext_p.h>
45#include <QWindow>
46#include <QQuickWindow>
47#include <QFileDialog>
48
49QT_BEGIN_NAMESPACE
50
51/*!
52 \qmltype QtFileDialog
53 \instantiates QQuickQFileDialog
54 \inqmlmodule QtQuick.PrivateWidgets
55 \ingroup qtquick-visual
56 \brief Dialog component for choosing files from a local filesystem.
57 \since 5.1
58 \internal
59
60 QtFileDialog provides a means to instantiate and manage a QFileDialog.
61 It is not recommended to be used directly; it is an implementation
62 detail of \l FileDialog in the \l QtQuick.Dialogs module.
63
64 To use this type, you will need to import the module with the following line:
65 \code
66 import QtQuick.PrivateWidgets 1.0
67 \endcode
68*/
69
70/*!
71 \qmlsignal QtQuick::Dialogs::FileDialog::accepted
72
73 The \a accepted signal is emitted when the user has finished using the
74 dialog. You can then inspect the \a fileUrl or \a fileUrls properties to
75 get the selection.
76
77 Example:
78
79 \qml
80 FileDialog {
81 onAccepted: { console.log("Selected file: " + fileUrl) }
82 }
83 \endqml
84
85 The corresponding handler is \c onAccepted.
86*/
87
88/*!
89 \qmlsignal QtQuick::Dialogs::FileDialog::rejected
90
91 The \a rejected signal is emitted when the user has dismissed the dialog,
92 either by closing the dialog window or by pressing the Cancel button.
93
94 The corresponding handler is \c onRejected.
95*/
96
97/*!
98 \class QQuickQFileDialog
99 \inmodule QtQuick.PrivateWidgets
100 \internal
101
102 \brief The QQuickQFileDialog class is a wrapper for a QFileDialog.
103
104 \since 5.1
105*/
106
107/*!
108 Constructs a file dialog with parent window \a parent.
109*/
110QQuickQFileDialog::QQuickQFileDialog(QObject *parent)
111 : QQuickAbstractFileDialog(parent)
112{
113}
114
115/*!
116 Destroys the file dialog.
117*/
118QQuickQFileDialog::~QQuickQFileDialog()
119{
120 if (m_dlgHelper)
121 m_dlgHelper->hide();
122 delete m_dlgHelper;
123}
124
125QPlatformFileDialogHelper *QQuickQFileDialog::helper()
126{
127 QQuickItem *parentItem = qobject_cast<QQuickItem *>(object: parent());
128 if (parentItem)
129 m_parentWindow = parentItem->window();
130
131 if (!m_dlgHelper) {
132 m_dlgHelper = new QFileDialogHelper();
133 connect(sender: m_dlgHelper, SIGNAL(directoryEntered(QUrl)), receiver: this, SIGNAL(folderChanged()));
134 connect(sender: m_dlgHelper, SIGNAL(filterSelected(QString)), receiver: this, SIGNAL(filterSelected()));
135 connect(sender: m_dlgHelper, SIGNAL(accept()), receiver: this, SLOT(accept()));
136 connect(sender: m_dlgHelper, SIGNAL(reject()), receiver: this, SLOT(reject()));
137 }
138
139 return m_dlgHelper;
140}
141
142QList<QUrl> QQuickQFileDialog::fileUrls() const
143{
144 if (m_dialogHelperInUse)
145 return m_dlgHelper->selectedFiles();
146 return QList<QUrl>();
147}
148
149QFileDialogHelper::QFileDialogHelper() :
150 QPlatformFileDialogHelper()
151{
152 connect(sender: &m_dialog, SIGNAL(currentChanged(QString)), receiver: this, SLOT(currentChanged(QString)));
153 connect(sender: &m_dialog, SIGNAL(directoryEntered(QString)), receiver: this, SLOT(directoryEntered(QString)));
154 connect(sender: &m_dialog, SIGNAL(fileSelected(QString)), receiver: this, SLOT(fileSelected(QString)));
155 connect(sender: &m_dialog, SIGNAL(filesSelected(QStringList)), receiver: this, SLOT(filesSelected(QStringList)));
156 connect(sender: &m_dialog, SIGNAL(filterSelected(QString)), receiver: this, SIGNAL(filterSelected(QString)));
157 connect(sender: &m_dialog, SIGNAL(accepted()), receiver: this, SIGNAL(accept()));
158 connect(sender: &m_dialog, SIGNAL(rejected()), receiver: this, SIGNAL(reject()));
159}
160
161QList<QUrl> QFileDialogHelper::selectedFiles() const
162{
163 return m_dialog.selectedUrls();
164}
165
166void QFileDialogHelper::setFilter() {
167 m_dialog.setWindowTitle(QPlatformFileDialogHelper::options()->windowTitle());
168 if (QPlatformFileDialogHelper::options()->isLabelExplicitlySet(label: QFileDialogOptions::LookIn))
169 m_dialog.setLabelText(label: m_dialog.LookIn, text: QPlatformFileDialogHelper::options()->labelText(label: QFileDialogOptions::LookIn));
170 if (QPlatformFileDialogHelper::options()->isLabelExplicitlySet(label: QFileDialogOptions::FileName))
171 m_dialog.setLabelText(label: m_dialog.FileName, text: QPlatformFileDialogHelper::options()->labelText(label: QFileDialogOptions::FileName));
172 if (QPlatformFileDialogHelper::options()->isLabelExplicitlySet(label: QFileDialogOptions::FileType))
173 m_dialog.setLabelText(label: m_dialog.FileType, text: QPlatformFileDialogHelper::options()->labelText(label: QFileDialogOptions::FileType));
174 if (QPlatformFileDialogHelper::options()->isLabelExplicitlySet(label: QFileDialogOptions::Accept))
175 m_dialog.setLabelText(label: m_dialog.Accept, text: QPlatformFileDialogHelper::options()->labelText(label: QFileDialogOptions::Accept));
176 if (QPlatformFileDialogHelper::options()->isLabelExplicitlySet(label: QFileDialogOptions::Reject))
177 m_dialog.setLabelText(label: m_dialog.Reject, text: QPlatformFileDialogHelper::options()->labelText(label: QFileDialogOptions::Reject));
178 m_dialog.setFilter(QPlatformFileDialogHelper::options()->filter());
179 m_dialog.setNameFilters(QPlatformFileDialogHelper::options()->nameFilters());
180 m_dialog.selectNameFilter(filter: QPlatformFileDialogHelper::options()->initiallySelectedNameFilter());
181 m_dialog.setFileMode(QFileDialog::FileMode(QPlatformFileDialogHelper::options()->fileMode()));
182 m_dialog.setOptions((QFileDialog::Options)((int)(QPlatformFileDialogHelper::options()->options())));
183 m_dialog.setAcceptMode(QFileDialog::AcceptMode(QPlatformFileDialogHelper::options()->acceptMode()));
184}
185
186bool QFileDialogHelper::show(Qt::WindowFlags f, Qt::WindowModality m, QWindow *parent) {
187 m_dialog.winId();
188 QWindow *window = m_dialog.windowHandle();
189 Q_ASSERT(window);
190 window->setTransientParent(parent);
191 window->setFlags(f);
192 m_dialog.setWindowModality(m);
193 m_dialog.show();
194 return m_dialog.isVisible();
195}
196
197void QFileDialogHelper::currentChanged(const QString& path)
198{
199 emit QPlatformFileDialogHelper::currentChanged(path: QUrl::fromLocalFile(localfile: path));
200}
201
202void QFileDialogHelper::directoryEntered(const QString& path)
203{
204 emit QPlatformFileDialogHelper::directoryEntered(directory: QUrl::fromLocalFile(localfile: path));
205}
206
207void QFileDialogHelper::fileSelected(const QString& path)
208{
209 emit QPlatformFileDialogHelper::fileSelected(file: QUrl::fromLocalFile(localfile: path));
210}
211
212void QFileDialogHelper::filesSelected(const QStringList& paths)
213{
214 QList<QUrl> pathUrls;
215 pathUrls.reserve(alloc: paths.count());
216 for (const QString &path : paths)
217 pathUrls << QUrl::fromLocalFile(localfile: path);
218 emit QPlatformFileDialogHelper::filesSelected(files: pathUrls);
219}
220
221QT_END_NAMESPACE
222

source code of qtquickcontrols/src/widgets/qquickqfiledialog.cpp