File indexing completed on 2024-05-12 16:02:30

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2013-2014 Yue Liu <yue.liu@mail.com>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KOFILEDIALOG_H
0008 #define KOFILEDIALOG_H
0009 
0010 #include "kritawidgetutils_export.h"
0011 
0012 #include <QFileDialog>
0013 #include <QString>
0014 #include <QStringList>
0015 #include <QList>
0016 
0017 
0018 /**
0019  * Wrapper around QFileDialog providing native file dialogs
0020  * on KDE/Gnome/Windows/OSX/etc.
0021  */
0022 class KRITAWIDGETUTILS_EXPORT KoFileDialog : public QObject
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     enum DialogType {
0028         OpenFile,
0029         OpenFiles,
0030         OpenDirectory,
0031         ImportFile,
0032         ImportFiles,
0033         ImportDirectory,
0034         SaveFile
0035     };
0036 
0037     /**
0038      * @brief constructor
0039      * @param parent The parent of the file dialog
0040      * @param dialogType usage of the file dialog
0041      * @param dialogName the name for the file dialog. This will be used to open
0042      * the filedialog in the last open location, instead the specified directory.
0043      *
0044      * @return The name of the entry user selected in the file dialog
0045      *
0046      */
0047     KoFileDialog(QWidget *parent,
0048                  KoFileDialog::DialogType type,
0049                  const QString &dialogName);
0050 
0051     ~KoFileDialog() override;
0052 
0053     void setCaption(const QString &caption);
0054 
0055     /**
0056      * @brief setDefaultDir set the default directory to defaultDir.
0057      *
0058      * @param defaultDir a path to a file or directory
0059      */
0060     void setDefaultDir(const QString &defaultDir, bool force = false);
0061 
0062     /**
0063      * @brief setDirectoryUrl set the default URI to defaultUri.
0064      * @param defaultUri a Uri to a file from some ContentProvider
0065      *
0066      * Used only on Android.
0067      */
0068     void setDirectoryUrl(const QUrl &defaultUri);
0069 
0070     /**
0071      * @brief setImageFilters sets the name filters for the file dialog to all
0072      * image formats Qt's QImageReader supports.
0073      */
0074     void setImageFilters();
0075 
0076     /**
0077      * @brief setMimeTypeFilters Update the list of file filters from mime types.
0078      * @param mimeTypeList A list of mime types that forms the basis of this dialog's file filters
0079      * @param defaultMimeType Sets the default filter based on this mime type
0080      */
0081     void setMimeTypeFilters(const QStringList &mimeTypeList,
0082                             QString defaultMimeType = QString());
0083 
0084     /// Get the file names the user selected in the file dialog
0085     QStringList filenames();
0086 
0087     /// Get the file name the user selected in the file dialog
0088     QString filename();
0089 
0090     /**
0091      * @brief selectedNameFilter returns the name filter the user selected, either
0092      *    directory or by clicking on it.
0093      * @return
0094      */
0095     QString selectedNameFilter() const;
0096 
0097     QString selectedMimeType() const;
0098 
0099 public Q_SLOTS:
0100     // Set default file extension matching the filter.
0101     void onFilterSelected(const QString &filter);
0102 
0103 private:
0104     /**
0105      * @brief splitNameFilter take a single line of a QDialog name filter and split it
0106      *   into several lines. This is needed because a single line name filter can contain
0107      *   more than one mimetype, making it impossible to figure out the correct extension.
0108      *
0109      *   The methods takes care of some duplicated extensions, like jpeg and jpg.
0110      * @param nameFilter the namefilter to be split
0111      * @param mimeList a pointer to the list with mimes that shouldn't be added.
0112      * @return a stringlist of all name filters.
0113      */
0114     static QStringList splitNameFilter(const QString &nameFilter, QStringList *mimeList);
0115 
0116     void createFileDialog();
0117 
0118     QString getUsedDir(const QString &dialogName);
0119     void saveUsedDir(const QString &fileName, const QString &dialogName);
0120 
0121     // Get list of user friendly descriptions and map of these descriptions to the proposed file extension.
0122     const QPair<QStringList, QMap<QString, QString>> getFilterStringListFromMime(const QStringList &mimeList,
0123                                                   bool withAllSupportedEntry = false);
0124 
0125     class Private;
0126     Private * const d;
0127 };
0128 
0129 #endif /* KOFILEDIALOG_H */