File indexing completed on 2024-06-16 06:57:01

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2019 Méven Car <meven.car@kdemail.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <KFileWidget>
0009 #include <QApplication>
0010 #include <QCommandLineOption>
0011 #include <QCommandLineParser>
0012 #include <QDebug>
0013 #include <QPushButton>
0014 #include <QUrl>
0015 
0016 int main(int argc, char **argv)
0017 {
0018     QApplication app(argc, argv);
0019 
0020     // Do some args
0021     QCommandLineParser parser;
0022     parser.addOption(QCommandLineOption(QStringLiteral("multiple"), QStringLiteral("Allows multiple files selection")));
0023     parser.addPositionalArgument(QStringLiteral("folder"), QStringLiteral("The initial folder"));
0024     parser.process(app);
0025     QStringList posargs = parser.positionalArguments();
0026 
0027     QUrl folder = QUrl(QStringLiteral("kfiledialog:///SaveDialog"));
0028     if (!posargs.isEmpty()) {
0029         folder = QUrl::fromUserInput(posargs.at(0));
0030     }
0031     qDebug() << "Starting at" << folder;
0032     KFileWidget *fileWidget = new KFileWidget(folder);
0033     fileWidget->setOperationMode(KFileWidget::Saving);
0034     if (parser.isSet(QStringLiteral("multiple"))) {
0035         fileWidget->setMode(KFile::Files);
0036     } else {
0037         fileWidget->setMode(KFile::File);
0038     }
0039     fileWidget->setAttribute(Qt::WA_DeleteOnClose);
0040 
0041     fileWidget->okButton()->show();
0042     fileWidget->cancelButton()->show();
0043     app.connect(fileWidget->okButton(), &QPushButton::clicked, fileWidget, &KFileWidget::slotOk);
0044     app.connect(fileWidget->cancelButton(), &QPushButton::clicked, fileWidget, [&app, fileWidget]() {
0045         qDebug() << "canceled";
0046         fileWidget->slotCancel();
0047         app.exit();
0048     });
0049 
0050     app.connect(fileWidget, &KFileWidget::accepted, fileWidget, [&app, fileWidget]() {
0051         qDebug() << "accepted";
0052         fileWidget->accept();
0053         qDebug() << "Selected File:" << fileWidget->selectedFile();
0054         qDebug() << "Selected Url:" << fileWidget->selectedUrl();
0055         qDebug() << "Selected Files:" << fileWidget->selectedFiles();
0056         qDebug() << "Selected Urls:" << fileWidget->selectedUrls();
0057         app.exit();
0058     });
0059 
0060     fileWidget->show();
0061 
0062     return app.exec();
0063 }