andre@50: /* Copyright (C) 2016 by ETH Zürich andre@50: * Software engineering by Intevation GmbH andre@50: * andre@50: * This file is Free Software under the GNU GPL (v>=2) andre@50: * and comes with ABSOLUTELY NO WARRANTY! andre@50: * See LICENSE.txt for details. andre@50: */ andre@50: andre@50: /* This file was originally taken from Libkleo where the license was: andre@50: ui/filenamerequester.cpp andre@50: andre@50: This file is part of Kleopatra, the KDE keymanager andre@50: Copyright (c) 2007 Klarälvdalens Datakonsult AB andre@50: andre@50: Kleopatra is free software; you can redistribute it and/or modify andre@50: it under the terms of the GNU General Public License as published by andre@50: the Free Software Foundation; either version 2 of the License, or andre@50: (at your option) any later version. andre@50: andre@50: Kleopatra is distributed in the hope that it will be useful, andre@50: but WITHOUT ANY WARRANTY; without even the implied warranty of andre@50: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU andre@50: General Public License for more details. andre@50: andre@50: You should have received a copy of the GNU General Public License andre@50: along with this program; if not, write to the Free Software andre@50: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA andre@50: andre@50: In addition, as a special exception, the copyright holders give andre@50: permission to link the code of this program with any edition of andre@50: the Qt library by Trolltech AS, Norway (or with modified versions andre@50: of Qt that use the same license as Qt), and distribute linked andre@50: combinations including the two. You must obey the GNU General andre@50: Public License in all respects for all of the code used other than andre@50: Qt. If you modify this file, you may extend this exception to andre@50: your version of the file, but you are not obligated to do so. If andre@50: you do not wish to do so, delete this exception statement from andre@50: your version. andre@50: */ andre@50: andre@50: #include "filenamerequester.h" andre@50: andre@50: #include andre@50: andre@50: #include andre@50: #include andre@50: #include andre@50: #include andre@50: #include andre@50: #include andre@50: #include andre@50: #include andre@50: andre@50: class FileNameRequester::Private andre@50: { andre@50: friend class FileNameRequester; andre@50: FileNameRequester *const q; andre@50: public: andre@50: explicit Private(FileNameRequester *qq); andre@50: ~Private(); andre@50: andre@50: private: andre@50: void slotButtonClicked(); andre@50: andre@50: private: andre@50: QDirModel dirmodel; andre@50: QCompleter completer; andre@50: andre@50: QLineEdit lineedit; andre@50: QToolButton button; andre@50: QHBoxLayout hlay; andre@50: andre@50: QString nameFilter; andre@50: bool existingOnly; andre@50: }; andre@50: andre@50: FileNameRequester::Private::Private(FileNameRequester *qq) andre@50: : q(qq), andre@50: dirmodel(), andre@50: completer(&dirmodel), andre@50: lineedit(q), andre@50: button(q), andre@50: hlay(q), andre@50: nameFilter(), andre@50: existingOnly(true) andre@50: { andre@50: dirmodel.setObjectName(QStringLiteral("dirmodel")); andre@50: completer.setObjectName(QStringLiteral("completer")); andre@50: lineedit.setObjectName(QStringLiteral("lineedit")); andre@50: button.setObjectName(QStringLiteral("button")); andre@50: hlay.setObjectName(QStringLiteral("hlay")); andre@50: andre@50: button.setIcon(QApplication::style()->standardIcon(QStyle::SP_DirIcon)); andre@50: lineedit.setCompleter(&completer); andre@50: lineedit.setClearButtonEnabled(true); andre@50: hlay.setMargin(0); andre@50: hlay.addWidget(&lineedit); andre@50: hlay.addWidget(&button); andre@50: andre@50: connect(&button, SIGNAL(clicked()), q, SLOT(slotButtonClicked())); andre@50: connect(&lineedit, &QLineEdit::textChanged, q, &FileNameRequester::fileNameChanged); andre@50: } andre@50: andre@50: FileNameRequester::Private::~Private() {} andre@50: andre@50: FileNameRequester::FileNameRequester(QWidget *p) andre@50: : QWidget(p), d(new Private(this)) andre@50: { andre@50: andre@50: } andre@50: andre@50: FileNameRequester::FileNameRequester(QDir::Filters f, QWidget *p) andre@50: : QWidget(p), d(new Private(this)) andre@50: { andre@50: d->dirmodel.setFilter(f); andre@50: } andre@50: andre@50: FileNameRequester::~FileNameRequester() andre@50: { andre@50: delete d; andre@50: } andre@50: andre@50: void FileNameRequester::setFileName(const QString &file) andre@50: { andre@50: d->lineedit.setText(file); andre@50: } andre@50: andre@50: QString FileNameRequester::fileName() const andre@50: { andre@50: return d->lineedit.text(); andre@50: } andre@50: andre@50: void FileNameRequester::setExistingOnly(bool on) andre@50: { andre@50: d->existingOnly = on; andre@50: } andre@50: andre@50: bool FileNameRequester::existingOnly() const andre@50: { andre@50: return d->existingOnly; andre@50: } andre@50: andre@50: void FileNameRequester::setFilter(QDir::Filters f) andre@50: { andre@50: d->dirmodel.setFilter(f); andre@50: } andre@50: andre@50: QDir::Filters FileNameRequester::filter() const andre@50: { andre@50: return d->dirmodel.filter(); andre@50: } andre@50: andre@50: void FileNameRequester::setNameFilter(const QString &nameFilter) andre@50: { andre@50: d->nameFilter = nameFilter; andre@50: } andre@50: andre@50: QString FileNameRequester::nameFilter() const andre@50: { andre@50: return d->nameFilter; andre@50: } andre@50: andre@50: void FileNameRequester::Private::slotButtonClicked() andre@50: { andre@50: const QString fileName = q->requestFileName(); andre@50: if (!fileName.isEmpty()) { andre@50: q->setFileName(fileName); andre@50: } andre@50: } andre@50: andre@50: QString FileNameRequester::requestFileName() andre@50: { andre@50: const QDir::Filters filters = filter(); andre@50: if ((filters & QDir::Dirs) && !(filters & QDir::Files)) { andre@50: return QFileDialog::getExistingDirectory(this); andre@50: } else if (d->existingOnly) { andre@50: return QFileDialog::getOpenFileName(this, QString(), QString(), d->nameFilter); andre@50: } else { andre@50: return QFileDialog::getSaveFileName(this, QString(), QString(), d->nameFilter); andre@50: } andre@50: } andre@50: andre@50: #include "moc_filenamerequester.cpp"