File indexing completed on 2025-04-27 03:58:22
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * date : 2014-09-12 0007 * Description : a file or folder selector widget 0008 * 0009 * SPDX-FileCopyrightText: 2014-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "dfileselector.h" 0016 0017 // Qt includes 0018 0019 #include <QPointer> 0020 0021 // KDE includes 0022 0023 #include <klocalizedstring.h> 0024 0025 // Local includes 0026 0027 #include "digikam_debug.h" 0028 0029 namespace Digikam 0030 { 0031 0032 class Q_DECL_HIDDEN DFileSelector::Private 0033 { 0034 public: 0035 0036 Private() = default; 0037 0038 QLineEdit* edit = nullptr; 0039 QPushButton* btn = nullptr; 0040 0041 QFileDialog::FileMode fdMode = QFileDialog::ExistingFile; 0042 QString fdFilter; 0043 QString fdTitle; 0044 QFileDialog::Options fdOptions = QFileDialog::Options(); 0045 }; 0046 0047 DFileSelector::DFileSelector(QWidget* const parent) 0048 : DHBox(parent), 0049 d (new Private) 0050 { 0051 d->edit = new QLineEdit(this); 0052 d->edit->setClearButtonEnabled(true); 0053 d->btn = new QPushButton(i18n("Browse..."), this); 0054 setStretchFactor(d->edit, 10); 0055 setContentsMargins(0, 0, 0, 0); 0056 0057 connect(d->btn, SIGNAL(clicked()), 0058 this, SLOT(slotBtnClicked())); 0059 } 0060 0061 DFileSelector::~DFileSelector() 0062 { 0063 delete d; 0064 } 0065 0066 QLineEdit* DFileSelector::lineEdit() const 0067 { 0068 return d->edit; 0069 } 0070 0071 void DFileSelector::setFileDlgPath(const QString& path) 0072 { 0073 d->edit->setText(QDir::toNativeSeparators(path)); 0074 } 0075 0076 QString DFileSelector::fileDlgPath() const 0077 { 0078 return QDir::fromNativeSeparators(d->edit->text()); 0079 } 0080 0081 void DFileSelector::setFileDlgMode(QFileDialog::FileMode mode) 0082 { 0083 d->fdMode = mode; 0084 } 0085 0086 void DFileSelector::setFileDlgFilter(const QString& filter) 0087 { 0088 d->fdFilter = filter; 0089 } 0090 0091 void DFileSelector::setFileDlgTitle(const QString& title) 0092 { 0093 d->fdTitle = title; 0094 } 0095 0096 void DFileSelector::setFileDlgOptions(QFileDialog::Options opts) 0097 { 0098 d->fdOptions = opts; 0099 } 0100 0101 void DFileSelector::slotBtnClicked() 0102 { 0103 if (d->fdMode == QFileDialog::ExistingFiles) 0104 { 0105 qCDebug(DIGIKAM_WIDGETS_LOG) << "Multiple selection is not supported"; 0106 return; 0107 } 0108 0109 // Never pass a parent to File Dialog, else duplicate dialogs will be shown 0110 0111 QPointer<DFileDialog> fileDlg = new DFileDialog; 0112 0113 fileDlg->setDirectory(QFileInfo(fileDlgPath()).filePath()); 0114 0115 // It is important to set up the mode first and then the options 0116 0117 fileDlg->setFileMode(d->fdMode); 0118 fileDlg->setOptions(d->fdOptions); 0119 0120 if (!d->fdFilter.isNull()) 0121 { 0122 fileDlg->setNameFilter(d->fdFilter); 0123 } 0124 0125 if (!d->fdTitle.isNull()) 0126 { 0127 fileDlg->setWindowTitle(d->fdTitle); 0128 } 0129 0130 Q_EMIT signalOpenFileDialog(); 0131 0132 fileDlg->exec(); 0133 0134 if (fileDlg->hasAcceptedUrls()) 0135 { 0136 QStringList sel = fileDlg->selectedFiles(); 0137 setFileDlgPath(sel.first()); 0138 0139 Q_EMIT signalUrlSelected(QUrl::fromLocalFile(sel.first())); 0140 } 0141 0142 delete fileDlg; 0143 } 0144 0145 } // namespace Digikam 0146 0147 #include "moc_dfileselector.cpp"