69void QQuickSideBar::setDialog(QQuickDialog *dialog)
72 if (dialog == d->dialog)
75 if (
auto fileDialog = qobject_cast<QQuickFileDialogImpl *>(d->dialog))
76 QObjectPrivate::disconnect(fileDialog, &QQuickFileDialogImpl::currentFolderChanged, d,
77 &QQuickSideBarPrivate::folderChanged);
81 if (
auto fileDialog = qobject_cast<QQuickFileDialogImpl *>(d->dialog))
82 QObjectPrivate::connect(fileDialog, &QQuickFileDialogImpl::currentFolderChanged, d,
83 &QQuickSideBarPrivate::folderChanged);
203QQuickItem *QQuickSideBarPrivate::createDelegateItem(QQmlComponent *component,
204 const QVariantMap &initialProperties)
209 QQmlContext *context = component->creationContext();
213 context = qmlContext(q);
217 if (!component->isBound() && initialProperties.isEmpty()) {
218 context =
new QQmlContext(context, q);
219 context->setContextObject(q);
222 QQuickItem *item = qobject_cast<QQuickItem *>(
223 component->createWithInitialProperties(initialProperties, context));
225 QQml_setParent_noEvent(item, q);
229void QQuickSideBarPrivate::repopulate()
233 if (repopulating || !buttonDelegate || !separatorDelegate || !addFavoriteDelegate || !q->contentItem())
236 QScopedValueRollback repopulateGuard(repopulating,
true);
238 auto updateIconSourceAndSize = [
this](QQuickAbstractButton *button,
const QUrl &iconUrl) {
242 QQuickIcon icon = button->icon();
243 icon.setSource(iconUrl);
244 const QSize iconSize = dialogIconSize();
245 icon.setWidth(iconSize.width());
246 icon.setHeight(iconSize.height());
247 button->setIcon(icon);
250 auto createButtonDelegate = [
this, q, &updateIconSourceAndSize](
int index,
const QString &folderPath,
const QUrl &iconUrl) {
251 const QString displayName = displayNameFromFolderPath(folderPath);
252 QVariantMap initialProperties = {
253 {
"index"_L1, QVariant::fromValue(index) },
254 {
"folderName"_L1, QVariant::fromValue(displayName) },
257 if (QQuickItem *buttonItem = createDelegateItem(buttonDelegate, initialProperties)) {
258 if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(buttonItem)) {
259 QObjectPrivate::connect(button, &QQuickAbstractButton::clicked,
this,
260 &QQuickSideBarPrivate::buttonClicked);
261 updateIconSourceAndSize(button, iconUrl);
262#if QT_CONFIG(accessibility)
263 if (QQuickAccessibleAttached *accessibleAttached = QQuickControlPrivate::accessibleAttached(button))
264 accessibleAttached->setName(displayName);
267 insertItem(q->count(), buttonItem);
272 while (q->count() > 0)
273 q->removeItem(q->itemAt(0));
276 const auto folders = q->effectiveFolderPaths();
277 const auto favorites = q->favoritePaths();
278 showSeparator = !folders.isEmpty() && (!favorites.isEmpty() || showAddFavoriteDelegate());
281 for (
auto &folder : folders)
282 createButtonDelegate(insertIndex++, QStandardPaths::displayName(folder), folderIconSource(folder));
285 if (QQuickItem *separatorItem = createDelegateItem(separatorDelegate, {{
"visible"_L1,
false}})) {
286 separatorImplicitSize = separatorItem->implicitHeight();
288 separatorItem->setVisible(
true);
289 insertItem(insertIndex++, separatorItem);
291 separatorItem->deleteLater();
296 const QString labelText = QCoreApplication::translate(
"FileDialog",
"Add Favorite");
297 const QVariantMap initialProperties = {
298 {
"labelText"_L1, QVariant::fromValue(labelText) },
299 {
"dragHovering"_L1, QVariant::fromValue(addFavoriteDelegateHovered()) },
300 {
"visible"_L1,
false}
302 if (
auto *addFavoriteDelegateItem = createDelegateItem(addFavoriteDelegate, initialProperties)) {
303 addFavoriteButtonImplicitSize = addFavoriteDelegateItem->implicitHeight();
304 if (showAddFavoriteDelegate()) {
305 addFavoriteDelegateItem->setVisible(
true);
306 if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(addFavoriteDelegateItem))
307 updateIconSourceAndSize(button, addFavoriteIconUrl());
308 insertItem(insertIndex++, addFavoriteDelegateItem);
310 addFavoriteDelegateItem->deleteLater();
315 for (
auto &favorite : favorites)
316 createButtonDelegate(insertIndex++, favorite.toLocalFile(), folderIconSource());
318 q->setCurrentIndex(-1);
321void QQuickSideBarPrivate::buttonClicked()
324 if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender())) {
325 const int buttonIndex = contentModel->indexOf(button,
nullptr);
326 q->setCurrentIndex(buttonIndex);
328 currentButtonClickedUrl = QUrl();
330 const int offset = q->effectiveFolderPaths().size() + (showSeparator ? 1 : 0);
331 if (buttonIndex >= offset)
332 currentButtonClickedUrl = q->favoritePaths().at(buttonIndex - offset);
334 currentButtonClickedUrl = QUrl::fromLocalFile(
335 QStandardPaths::writableLocation(q->effectiveFolderPaths().at(buttonIndex)));
337 currentButtonClickedUrl.setScheme(
"file"_L1);
338 setDialogFolder(currentButtonClickedUrl);
510void QQuickSideBarPrivate::handleContextMenuRequested(QPointF pos)
513 const int offset = q->effectiveFolderPaths().size() + (showSeparator ? 1 : 0);
514 for (
int i = offset; i < q->count(); ++i) {
515 QQuickItem *itm = q->itemAt(i);
516 if (itm->contains(itm->mapFromItem(q, pos))) {
517 auto favorites = q->favoritePaths();
518 urlToBeRemoved = favorites.value(i - offset);
520 if (!urlToBeRemoved.isEmpty() && !menu) {
521 QQmlEngine *eng = qmlEngine(q);
523 QQmlContext *context = qmlContext(q);
524 QQmlComponent component(eng);
525 component.loadFromModule(
"QtQuick.Controls",
"Menu");
526 menu = qobject_cast<QQuickMenu*>(component.create(context));
528 auto *removeAction =
new QQuickAction(menu);
529 removeAction->setText(QCoreApplication::translate(
"FileDialog",
"Remove"));
530 menu->addAction(removeAction);
531 connect(removeAction, &QQuickAction::triggered,
this, &QQuickSideBarPrivate::handleRemoveAction);
534 contextMenu->setMenu(menu);
538 contextMenu->setMenu(
nullptr);
564qreal QQuickSideBarPrivate::getContentHeight()
const
566 Q_Q(
const QQuickSideBar);
570 const int modelCount = contentModel->count();
571 const int folderPathCount = q->effectiveFolderPaths().count();
574 QQuickListView *listView = contentItem->findChild<QQuickListView*>();
576 spacing = listView->spacing();
578 qreal totalHeight = 0;
580 for (; i < qMin(modelCount, folderPathCount); ++i) {
581 QQuickItem *item = q->itemAt(i);
583 totalHeight += item->implicitHeight();
588 totalHeight += (i - 1) * spacing;
590 if (!qFuzzyIsNull(separatorImplicitSize))
591 totalHeight += separatorImplicitSize + spacing;
592 if (!qFuzzyIsNull(addFavoriteButtonImplicitSize))
593 totalHeight += addFavoriteButtonImplicitSize + spacing;