diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -117,6 +117,7 @@ kurlcomboboxtest.cpp kdiroperatortest.cpp kfilewidgettest.cpp + kfilecustomdialogtest.cpp knewfilemenutest.cpp kfilecopytomenutest.cpp kfileplacesmodeltest.cpp diff --git a/autotests/kfilecustomdialogtest.h b/autotests/kfilecustomdialogtest.h new file mode 100644 --- /dev/null +++ b/autotests/kfilecustomdialogtest.h @@ -0,0 +1,33 @@ +/* This file is part of the KDE libraries + Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group + company, info@kdab.com. Work sponsored by the + LiMux project of the city of Munich + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KFILECUSTOMDIALOGTEST_H +#define KFILECUSTOMDIALOGTEST_H + +#include + +class KFileCustomDialogTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void shouldHaveDefaultValue(); +}; + +#endif // KFILECUSTOMDIALOGTEST_H diff --git a/autotests/kfilecustomdialogtest.cpp b/autotests/kfilecustomdialogtest.cpp new file mode 100644 --- /dev/null +++ b/autotests/kfilecustomdialogtest.cpp @@ -0,0 +1,42 @@ +/* This file is part of the KDE libraries + Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group + company, info@kdab.com. Work sponsored by the + LiMux project of the city of Munich + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kfilecustomdialogtest.h" +#include "kfilecustomdialog.h" + +#include +#include +#include +#include +QTEST_MAIN(KFileCustomDialogTest) + +void KFileCustomDialogTest::shouldHaveDefaultValue() +{ + KFileCustomDialog dlg; + dlg.show(); + QVERIFY(QTest::qWaitForWindowExposed(&dlg)); + + QVBoxLayout *mainLayout = dlg.findChild(); + QVERIFY(mainLayout); + + KFileWidget *mFileWidget = dlg.findChild(QStringLiteral("filewidget")); + QVERIFY(mFileWidget); + QCOMPARE(dlg.fileWidget(), mFileWidget); +} diff --git a/src/filewidgets/CMakeLists.txt b/src/filewidgets/CMakeLists.txt --- a/src/filewidgets/CMakeLists.txt +++ b/src/filewidgets/CMakeLists.txt @@ -19,6 +19,7 @@ kencodingfiledialog.cpp kfilebookmarkhandler.cpp kfilecopytomenu.cpp + kfilecustomdialog.cpp kfilefiltercombo.cpp kfilewidget.cpp kfileplacesitem.cpp @@ -37,6 +38,7 @@ kurlnavigator.cpp kurlnavigatormenu.cpp + ) qt5_add_resources(kiofilewidgets_SRCS ../new_file_templates/templates.qrc) diff --git a/src/filewidgets/kfilecustomdialog.h b/src/filewidgets/kfilecustomdialog.h new file mode 100644 --- /dev/null +++ b/src/filewidgets/kfilecustomdialog.h @@ -0,0 +1,91 @@ +/* This file is part of the KDE libraries + Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group + company, info@kdab.com. Work sponsored by the + LiMux project of the city of Munich + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KFILECUSTOMDIALOG_H +#define KFILECUSTOMDIALOG_H + +#include "kiofilewidgets_export.h" +#include "kfilewidget.h" +#include +class KFileWidget; +class KFileCustomDialogPrivate; + +/** + * This class implement a custom file dialog. + * It uses a KFileWidget and allows the application to provide a custom widget. + * @since 5.42 + */ +class KIOFILEWIDGETS_EXPORT KFileCustomDialog : public QDialog +{ + Q_OBJECT +public: + explicit KFileCustomDialog(QWidget *parent = nullptr); + ~KFileCustomDialog(); + + /** + * Sets the directory to view. + * + * @param url URL to show. + */ + void setUrl(const QUrl &url); + + /** + * Set a custom widget that should be added to the file dialog. + * @param widget A widget, or a widget of widgets, for displaying custom + * data in the file widget. This can be used, for example, to + * display a check box with the caption "Open as read-only". + * When creating this widget, you don't need to specify a parent, + * since the widget's parent will be set automatically by KFileWidget. + */ + void setCustomWidget(QWidget *widget); + + /** + * @brief fileWidget + * @return the filewidget element + */ + KFileWidget *fileWidget() const; + + /** + * Sets the operational mode of the filedialog to @p Saving, @p Opening + * or @p Other. This will set some flags that are specific to loading + * or saving files. E.g. setKeepLocation() makes mostly sense for + * a save-as dialog. So setOperationMode( KFileWidget::Saving ); sets + * setKeepLocation for example. + * + * The mode @p Saving, together with a default filter set via + * setMimeFilter() will make the filter combobox read-only. + * + * The default mode is @p Opening. + * + * Call this method right after instantiating KFileWidget. + * + * @see operationMode + * @see KFileWidget::OperationMode + */ + void setOperationMode(KFileWidget::OperationMode op); + +public Q_SLOTS: + void accept() override; + +private: + KFileCustomDialogPrivate *const d; +}; + +#endif // KFILECUSTOMDIALOG_H diff --git a/src/filewidgets/kfilecustomdialog.cpp b/src/filewidgets/kfilecustomdialog.cpp new file mode 100644 --- /dev/null +++ b/src/filewidgets/kfilecustomdialog.cpp @@ -0,0 +1,120 @@ +/* This file is part of the KDE libraries + Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group + company, info@kdab.com. Work sponsored by the + LiMux project of the city of Munich + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kfilecustomdialog.h" +#include "kfilewidget.h" + +#include +#include +#include + +class KFileCustomDialogPrivate +{ +public: + KFileCustomDialogPrivate(KFileCustomDialog *qq) + : q(qq) + { + init(); + } + void init(); + void setUrl(const QUrl &url); + void setCustomWidget(QWidget *widget); + void setOperationMode(KFileWidget::OperationMode op); + KFileWidget *fileWidget() const; + + KFileWidget *mFileWidget = nullptr; + KFileCustomDialog *q; +}; + +void KFileCustomDialogPrivate::init() +{ + QVBoxLayout *mainLayout = new QVBoxLayout(q); + mainLayout->setObjectName(QStringLiteral("mainlayout")); + + mFileWidget = new KFileWidget(QUrl(), q); + mFileWidget->setObjectName(QStringLiteral("filewidget")); + mainLayout->addWidget(mFileWidget); + + mFileWidget->okButton()->show(); + q->connect(mFileWidget->okButton(), &QPushButton::clicked, q, [this]() { mFileWidget->slotOk(); }); + mFileWidget->cancelButton()->show(); + q->connect(mFileWidget->cancelButton(), &QPushButton::clicked, q, [this]() { + mFileWidget->slotCancel(); + q->reject(); + }); + q->connect(mFileWidget, &KFileWidget::accepted, q, [this] { q->accept(); }); +} + +void KFileCustomDialogPrivate::setUrl(const QUrl &url) +{ + mFileWidget->setUrl(url); +} + +void KFileCustomDialogPrivate::setCustomWidget(QWidget *widget) +{ + mFileWidget->setCustomWidget(QString(), widget); +} + +void KFileCustomDialogPrivate::setOperationMode(KFileWidget::OperationMode op) +{ + mFileWidget->setOperationMode(op); +} + +KFileWidget *KFileCustomDialogPrivate::fileWidget() const +{ + return mFileWidget; +} + +KFileCustomDialog::KFileCustomDialog(QWidget *parent) + : QDialog(parent), + d(new KFileCustomDialogPrivate(this)) +{ +} + +KFileCustomDialog::~KFileCustomDialog() +{ + delete d; +} + +void KFileCustomDialog::setUrl(const QUrl &url) +{ + d->setUrl(url); +} + +void KFileCustomDialog::setCustomWidget(QWidget *widget) +{ + d->setCustomWidget(widget); +} + +KFileWidget *KFileCustomDialog::fileWidget() const +{ + return d->fileWidget(); +} + +void KFileCustomDialog::setOperationMode(KFileWidget::OperationMode op) +{ + d->setOperationMode(op); +} + +void KFileCustomDialog::accept() +{ + d->mFileWidget->accept(); + QDialog::accept(); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,6 +28,7 @@ kencodingfiledialogtest_gui kdirmodeltest_gui kdirlistertest_gui + kfilecustomdialogtest_gui previewtest kurlrequestertest_gui kpropertiesdialogtest diff --git a/tests/kfilecustomdialogtest_gui.h b/tests/kfilecustomdialogtest_gui.h new file mode 100644 --- /dev/null +++ b/tests/kfilecustomdialogtest_gui.h @@ -0,0 +1,32 @@ +/* This file is part of the KDE libraries + Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group + company, info@kdab.com. Work sponsored by the + LiMux project of the city of Munich + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +#ifndef KFILECUSTOMDIALOGTEST_GUI_H +#define KFILECUSTOMDIALOGTEST_GUI_H + + +class KFileCustomDialogTest_gui +{ +public: + KFileCustomDialogTest_gui(); +}; + +#endif // KFILECUSTOMDIALOGTEST_GUI_H diff --git a/tests/kfilecustomdialogtest_gui.cpp b/tests/kfilecustomdialogtest_gui.cpp new file mode 100644 --- /dev/null +++ b/tests/kfilecustomdialogtest_gui.cpp @@ -0,0 +1,50 @@ +/* This file is part of the KDE libraries + Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group + company, info@kdab.com. Work sponsored by the + LiMux project of the city of Munich + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kfilecustomdialog.h" +#include +#include + +int main(int argc, char **argv) +{ + QApplication::setApplicationName(QStringLiteral("KFileCustomDialogTest_gui")); + QApplication app(argc, argv); + KFileCustomDialog dlg; + QLabel *lab = new QLabel(QStringLiteral("foo")); + dlg.setCustomWidget(lab); + dlg.exec(); + + + //Save dialog box + KFileCustomDialog dlg2; + dlg2.setOperationMode(KFileWidget::Saving); + lab = new QLabel(QStringLiteral("Second")); + dlg2.setCustomWidget(lab); + dlg2.exec(); + + //Open dialog box + KFileCustomDialog dlg3; + dlg3.setOperationMode(KFileWidget::Opening); + lab = new QLabel(QStringLiteral("Third")); + dlg3.setCustomWidget(lab); + dlg3.exec(); + + return 0; +}