diff --git a/src/filewidgets/kdiroperator.h b/src/filewidgets/kdiroperator.h --- a/src/filewidgets/kdiroperator.h +++ b/src/filewidgets/kdiroperator.h @@ -912,6 +912,12 @@ */ void currentIconSizeChanged(int size); + /** + * Triggered when the user hit Enter/Return + * @since 5.57 + */ + void keyEnterReturnPressed(); + private: class Private; Private *const d; diff --git a/src/filewidgets/kdiroperator.cpp b/src/filewidgets/kdiroperator.cpp --- a/src/filewidgets/kdiroperator.cpp +++ b/src/filewidgets/kdiroperator.cpp @@ -164,6 +164,8 @@ { if (!(e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)) { QWidget::keyPressEvent(e); + } else { + emit keyEnterReturnPressed(); } } diff --git a/src/filewidgets/kfilewidget.h b/src/filewidgets/kfilewidget.h --- a/src/filewidgets/kfilewidget.h +++ b/src/filewidgets/kfilewidget.h @@ -613,6 +613,7 @@ Q_PRIVATE_SLOT(d, void _k_slotIconSizeSliderMoved(int)) Q_PRIVATE_SLOT(d, void _k_slotIconSizeChanged(int)) Q_PRIVATE_SLOT(d, void _k_slotViewDoubleClicked(const QModelIndex&)) + Q_PRIVATE_SLOT(d, void _k_slotViewKeyEnterReturnPressed()) }; #endif diff --git a/src/filewidgets/kfilewidget.cpp b/src/filewidgets/kfilewidget.cpp --- a/src/filewidgets/kfilewidget.cpp +++ b/src/filewidgets/kfilewidget.cpp @@ -198,6 +198,7 @@ void _k_slotIconSizeSliderMoved(int); void _k_slotIconSizeChanged(int); void _k_slotViewDoubleClicked(const QModelIndex&); + void _k_slotViewKeyEnterReturnPressed(); void addToRecentDocuments(); @@ -441,6 +442,8 @@ SLOT(_k_fileSelected(KFileItem))); connect(d->ops, SIGNAL(finishedLoading()), SLOT(_k_slotLoadingFinished())); + connect(d->ops, SIGNAL(keyEnterReturnPressed()), + SLOT(_k_slotViewKeyEnterReturnPressed())); d->ops->setupMenu(KDirOperator::SortActions | KDirOperator::FileActions | @@ -2198,6 +2201,15 @@ } } +void KFileWidgetPrivate::_k_slotViewKeyEnterReturnPressed() +{ + // an enter/return event occured in the view + // when we are saving one file and there is no selection in the view (otherwise we get an activated event) + if (operationMode == KFileWidget::Saving && (ops->mode() & KFile::File) && ops->selectedItems().isEmpty()) { + q->slotOk(); + } +} + static QString getExtensionFromPatternList(const QStringList &patternList) { // qDebug(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,5 +40,6 @@ kurlnavigatortest_gui kprotocolinfo_dumper kfilewidgettest_gui + kfilewidgettest_saving_gui runapplication ) diff --git a/tests/kfilewidgettest_saving_gui.cpp b/tests/kfilewidgettest_saving_gui.cpp new file mode 100644 --- /dev/null +++ b/tests/kfilewidgettest_saving_gui.cpp @@ -0,0 +1,58 @@ +/* This file is part of the KDE libraries + Copyright (C) 2019 Méven Car + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + KFileWidget* fileWidget = new KFileWidget(QUrl(QStringLiteral("kfiledialog:///SaveDialog")), nullptr); + fileWidget->setOperationMode(KFileWidget::Saving); + fileWidget->setMode(KFile::File); + fileWidget->setAttribute(Qt::WA_DeleteOnClose); + + fileWidget->okButton()->show(); + fileWidget->cancelButton()->show(); + app.connect(fileWidget->okButton(), &QPushButton::clicked, fileWidget, &KFileWidget::slotOk); + app.connect(fileWidget->cancelButton(), &QPushButton::clicked, fileWidget, [&app, fileWidget]() { + qDebug() << "canceled"; + fileWidget->slotCancel(); + app.exit(); + }); + + app.connect(fileWidget, &KFileWidget::accepted, fileWidget, [&app, fileWidget]() { + qDebug() << "accepted"; + fileWidget->accept(); + qDebug() << fileWidget->selectedFile(); + qDebug() << fileWidget->selectedUrl(); + qDebug() << fileWidget->selectedFiles(); + qDebug() << fileWidget->selectedUrls(); + app.exit(); + }); + + fileWidget->show(); + + return app.exec(); +} +