aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickdialogs/quickdialogsquickimpl/qquickfolderdialogimpl.cpp
blob: 25d82a17aada1402539603fd776b8f6cbc40786e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquickfolderdialogimpl_p.h"
#include "qquickfolderdialogimpl_p_p.h"

#include <QtCore/qloggingcategory.h>
#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h>

#include "qquickfiledialogdelegate_p.h"
#include "qquickfolderbreadcrumbbar_p.h"

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcFolderDialogCurrentFolder, "qt.quick.dialogs.quickfolderdialogimpl.currentFolder")
Q_LOGGING_CATEGORY(lcFolderDialogSelectedFolder, "qt.quick.dialogs.quickfolderdialogimpl.selectedFolder")
Q_LOGGING_CATEGORY(lcFolderDialogOptions, "qt.quick.dialogs.quickfolderdialogimpl.options")

QQuickFolderDialogImplPrivate::QQuickFolderDialogImplPrivate()
{
}

void QQuickFolderDialogImplPrivate::updateEnabled()
{
    Q_Q(QQuickFolderDialogImpl);
    if (!buttonBox)
        return;

    QQuickFolderDialogImplAttached *attached = attachedOrWarn();
    if (!attached)
        return;

    auto openButton = buttonBox->standardButton(QPlatformDialogHelper::Open);
    if (!openButton) {
        qmlWarning(q).nospace() << "Can't update Open button's enabled state because it wasn't found";
        return;
    }

    openButton->setEnabled(!selectedFolder.isEmpty() && attached->breadcrumbBar()
        && !attached->breadcrumbBar()->textField()->isVisible());
}

/*!
    \internal

    Ensures that a folder is always selected after a change in \c currentFolder.

    \a oldFolderPath is the previous value of \c currentFolder.
*/
void QQuickFolderDialogImplPrivate::updateSelectedFolder(const QString &oldFolderPath)
{
    Q_Q(QQuickFolderDialogImpl);
    QQuickFolderDialogImplAttached *attached = attachedOrWarn();
    if (!attached || !attached->folderDialogListView())
        return;

    QString newSelectedFolderPath;
    int newSelectedFolderIndex = 0;
    const QString newFolderPath = QQmlFile::urlToLocalFileOrQrc(currentFolder);
    if (!oldFolderPath.isEmpty() && !newFolderPath.isEmpty()) {
        // If the user went up a directory (or several), we should set
        // selectedFolder to be the directory that we were in (or
        // its closest ancestor that is a child of the new directory).
        // E.g. if oldFolderPath is /foo/bar/baz/abc/xyz, and newFolderPath is /foo/bar,
        // then we want to set selectedFolder to be /foo/bar/baz.
        const int indexOfFolder = oldFolderPath.indexOf(newFolderPath);
        if (indexOfFolder != -1) {
            // [folder]
            // [   oldFolderPath  ]
            // /foo/bar/baz/abc/xyz
            //         [rel...Paths]
            QStringList relativePaths = oldFolderPath.mid(indexOfFolder + newFolderPath.size()).split(QLatin1Char('/'), Qt::SkipEmptyParts);
            newSelectedFolderPath = newFolderPath + QLatin1Char('/') + relativePaths.first();

            // Now find the index of that directory so that we can set the ListView's currentIndex to it.
            const QDir newFolderDir(newFolderPath);
            // Just to be safe...
            if (!newFolderDir.exists()) {
                qmlWarning(q) << "Directory" << newSelectedFolderPath << "doesn't exist; can't get a file entry list for it";
                return;
            }

            const QFileInfoList dirs = newFolderDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::DirsFirst);
            const QFileInfo newSelectedFileInfo(newSelectedFolderPath);
            // The directory can contain files, but since we put dirs first, that should never affect the indices.
            newSelectedFolderIndex = dirs.indexOf(newSelectedFileInfo);
        }
    }

    if (newSelectedFolderPath.isEmpty()) {
        // When entering into a directory that isn't a parent of the old one, the first
        // file delegate should be selected.
        // TODO: is there a cheaper way to do this? QDirIterator doesn't support sorting,
        // so we can't use that. QQuickFolderListModel uses threads to fetch its data,
        // so should be considered asynchronous. We might be able to use it, but it would
        // complicate the code even more...
        QDir newFolderDir(newFolderPath);
        if (newFolderDir.exists()) {
            const QFileInfoList files = newFolderDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::DirsFirst);
            if (!files.isEmpty())
                newSelectedFolderPath = files.first().absoluteFilePath();
        }
    }

    const bool folderSelected = !newSelectedFolderPath.isEmpty();
    q->setSelectedFolder(folderSelected ? QUrl::fromLocalFile(newSelectedFolderPath) : QUrl());
    {
        // Set the appropriate currentIndex for the selected folder. We block signals from ListView
        // because we don't want folderDialogListViewCurrentIndexChanged to be called, as the file
        // it gets from the delegate will not be up-to-date (but most importantly because we already
        // just set the selected folder).
        QSignalBlocker blocker(attached->folderDialogListView());
        attached->folderDialogListView()->setCurrentIndex(folderSelected ? newSelectedFolderIndex : -1);
    }
    if (folderSelected) {
        if (QQuickItem *currentItem = attached->folderDialogListView()->currentItem())
            currentItem->forceActiveFocus();
    }
}

void QQuickFolderDialogImplPrivate::handleAccept()
{
    // Let handleClick take care of calling accept().
}

void QQuickFolderDialogImplPrivate::handleClick(QQuickAbstractButton *button)
{
    Q_Q(QQuickFolderDialogImpl);
    if (buttonRole(button) == QPlatformDialogHelper::AcceptRole && selectedFolder.isValid()) {
        q->setSelectedFolder(selectedFolder);
        q->accept();
    }
}

/*!
    \class QQuickFolderDialogImpl
    \internal

    An interface that QQuickFolderDialog can use to access the non-native Qt Quick FolderDialog.

    Both this and the native implementations are created in QQuickAbstractDialog::create().
*/

QQuickFolderDialogImpl::QQuickFolderDialogImpl(QObject *parent)
    : QQuickDialog(*(new QQuickFolderDialogImplPrivate), parent)
{
}

QQuickFolderDialogImplAttached *QQuickFolderDialogImpl::qmlAttachedProperties(QObject *object)
{
    return new QQuickFolderDialogImplAttached(object);
}

QUrl QQuickFolderDialogImpl::currentFolder() const
{
    Q_D(const QQuickFolderDialogImpl);
    return d->currentFolder;
}

void QQuickFolderDialogImpl::setCurrentFolder(const QUrl &currentFolder)
{
    qCDebug(lcFolderDialogCurrentFolder) << "setCurrentFolder called with" << currentFolder;
    Q_D(QQuickFolderDialogImpl);
    if (currentFolder == d->currentFolder)
        return;

    const QString oldFolderPath = QQmlFile::urlToLocalFileOrQrc(d->currentFolder);

    d->currentFolder = currentFolder;
    d->updateSelectedFolder(oldFolderPath);
    emit currentFolderChanged(d->currentFolder);
}

QUrl QQuickFolderDialogImpl::selectedFolder() const
{
    Q_D(const QQuickFolderDialogImpl);
    return d->selectedFolder;
}

void QQuickFolderDialogImpl::setSelectedFolder(const QUrl &selectedFolder)
{
    Q_D(QQuickFolderDialogImpl);
    qCDebug(lcFolderDialogSelectedFolder).nospace() << "setSelectedFolder called with selectedFolder "
        << selectedFolder << " (d->selectedFolder is " << d->selectedFolder << ")";
    if (selectedFolder == d->selectedFolder)
        return;

    d->selectedFolder = selectedFolder;
    d->updateEnabled();
    emit selectedFolderChanged(selectedFolder);
}

QSharedPointer<QFileDialogOptions> QQuickFolderDialogImpl::options() const
{
    Q_D(const QQuickFolderDialogImpl);
    return d->options;
}

void QQuickFolderDialogImpl::setOptions(const QSharedPointer<QFileDialogOptions> &options)
{
    qCDebug(lcFolderDialogOptions).nospace() << "setOptions called with:"
        << " acceptMode=" << options->acceptMode()
        << " fileMode=" << options->fileMode()
        << " initialDirectory=" << options->initialDirectory();

    Q_D(QQuickFolderDialogImpl);
    d->options = options;
}

/*!
    \internal

    These allow QQuickPlatformFileDialog::show() to set custom labels on the
    dialog buttons without having to know about/go through QQuickFolderDialogImplAttached
    and QQuickDialogButtonBox.
*/
void QQuickFolderDialogImpl::setAcceptLabel(const QString &label)
{
    Q_D(QQuickFolderDialogImpl);
    d->acceptLabel = label;
    QQuickFolderDialogImplAttached *attached = d->attachedOrWarn();
    if (!attached)
        return;

    auto acceptButton = d->buttonBox->standardButton(QPlatformDialogHelper::Open);
    if (!acceptButton) {
        qmlWarning(this).nospace() << "Can't set accept label to " << label
            << "; failed to find Open button in DialogButtonBox of " << this;
        return;
    }

    acceptButton->setText(!label.isEmpty()
        ? label : QQuickDialogButtonBoxPrivate::buttonText(QPlatformDialogHelper::Open));
}

void QQuickFolderDialogImpl::setRejectLabel(const QString &label)
{
    Q_D(QQuickFolderDialogImpl);
    d->rejectLabel = label;
    if (!d->buttonBox)
        return;

    auto rejectButton = d->buttonBox->standardButton(QPlatformDialogHelper::Cancel);
    if (!rejectButton) {
        qmlWarning(this).nospace() << "Can't set reject label to " << label
            << "; failed to find Open button in DialogButtonBox of " << this;
        return;
    }

    rejectButton->setText(!label.isEmpty()
        ? label : QQuickDialogButtonBoxPrivate::buttonText(QPlatformDialogHelper::Cancel));
}

void QQuickFolderDialogImpl::componentComplete()
{
    Q_D(QQuickFolderDialogImpl);
    QQuickDialog::componentComplete();

    // Find the right-most button and set its key navigation so that
    // tab moves focus to the breadcrumb bar's up button. I tried
    // doing this via KeyNavigation on the DialogButtonBox in QML,
    // but it didn't work (probably because it's not the right item).
    QQuickFolderDialogImplAttached *attached = d->attachedOrWarn();
    if (!attached)
        return;

    Q_ASSERT(d->buttonBox);
    const int buttonCount = d->buttonBox->count();
    if (buttonCount == 0)
        return;

    QQuickAbstractButton *rightMostButton = qobject_cast<QQuickAbstractButton *>(
        d->buttonBox->itemAt(buttonCount - 1));
    if (!rightMostButton) {
        qmlWarning(this) << "Can't find right-most button in DialogButtonBox";
        return;
    }

    auto keyNavigationAttached = QQuickKeyNavigationAttached::qmlAttachedProperties(rightMostButton);
    if (!keyNavigationAttached) {
        qmlWarning(this) << "Can't create attached KeyNavigation object on" << QDebug::toString(rightMostButton);
        return;
    }

    keyNavigationAttached->setTab(attached->breadcrumbBar()->upButton());
}

void QQuickFolderDialogImpl::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
{
    Q_D(QQuickFolderDialogImpl);
    QQuickDialog::itemChange(change, data);

    if (change != QQuickItem::ItemVisibleHasChanged || !isComponentComplete() || !data.boolValue)
        return;

    QQuickFolderDialogImplAttached *attached = d->attachedOrWarn();
    if (!attached)
        return;

    attached->folderDialogListView()->forceActiveFocus();
    d->updateEnabled();
}

QQuickFolderDialogImplAttached *QQuickFolderDialogImplPrivate::attachedOrWarn()
{
    Q_Q(QQuickFolderDialogImpl);
    QQuickFolderDialogImplAttached *attached = static_cast<QQuickFolderDialogImplAttached*>(
        qmlAttachedPropertiesObject<QQuickFolderDialogImpl>(q));
    if (!attached)
        qmlWarning(q) << "Expected FileDialogImpl attached object to be present on" << this;
    return attached;
}

void QQuickFolderDialogImplAttachedPrivate::folderDialogListViewCurrentIndexChanged()
{
    auto folderDialogImpl = qobject_cast<QQuickFolderDialogImpl*>(parent);
    if (!folderDialogImpl)
        return;

    auto folderDialogDelegate = qobject_cast<QQuickFileDialogDelegate*>(folderDialogListView->currentItem());
    if (!folderDialogDelegate)
        return;

    folderDialogImpl->setSelectedFolder(folderDialogDelegate->file());
}

QQuickFolderDialogImplAttached::QQuickFolderDialogImplAttached(QObject *parent)
    : QObject(*(new QQuickFolderDialogImplAttachedPrivate), parent)
{
    if (!qobject_cast<QQuickFolderDialogImpl*>(parent)) {
        qmlWarning(this) << "FolderDialogImpl attached properties should only be "
            << "accessed through the root FileDialogImpl instance";
    }
}

QQuickListView *QQuickFolderDialogImplAttached::folderDialogListView() const
{
    Q_D(const QQuickFolderDialogImplAttached);
    return d->folderDialogListView;
}

void QQuickFolderDialogImplAttached::setFolderDialogListView(QQuickListView *folderDialogListView)
{
    Q_D(QQuickFolderDialogImplAttached);
    if (folderDialogListView == d->folderDialogListView)
        return;

    d->folderDialogListView = folderDialogListView;

    QObjectPrivate::connect(d->folderDialogListView, &QQuickListView::currentIndexChanged,
        d, &QQuickFolderDialogImplAttachedPrivate::folderDialogListViewCurrentIndexChanged);

    emit folderDialogListViewChanged();
}

QQuickFolderBreadcrumbBar *QQuickFolderDialogImplAttached::breadcrumbBar() const
{
    Q_D(const QQuickFolderDialogImplAttached);
    return d->breadcrumbBar;
}

void QQuickFolderDialogImplAttached::setBreadcrumbBar(QQuickFolderBreadcrumbBar *breadcrumbBar)
{
    Q_D(QQuickFolderDialogImplAttached);
    if (breadcrumbBar == d->breadcrumbBar)
        return;

    d->breadcrumbBar = breadcrumbBar;
    emit breadcrumbBarChanged();
}

QT_END_NAMESPACE

#include "moc_qquickfolderdialogimpl_p.cpp"