File indexing completed on 2024-05-12 16:40:54

0001 /* This file is part of the KDE project
0002    Copyright (C) 2013 - 2014 Yue Liu <yue.liu@mail.com>
0003    Copyright (C) 2017 Jarosław Staniek <staniek@kde.org>
0004 
0005    Based on Calligra libs' KoFileDialog
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef KEXIFILEDIALOG_H
0024 #define KEXIFILEDIALOG_H
0025 
0026 #include "kexiextwidgets_export.h"
0027 
0028 #include <QFileDialog>
0029 #include <QString>
0030 #include <QUrl>
0031 #include <QStringList>
0032 #include <QList>
0033 
0034 
0035 /**
0036  * Wrapper around QFileDialog providing native file dialogs
0037  * on KDE/Gnome/Windows/OSX/etc.
0038  */
0039 class KEXIEXTWIDGETS_EXPORT KexiFileDialog : public QObject
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044     enum DialogType {
0045         OpenFile,
0046         OpenFiles,
0047         OpenDirectory,
0048         ImportFile,
0049         ImportFiles,
0050         ImportDirectory,
0051         SaveFile
0052     };
0053 
0054     /**
0055      * @brief constructor
0056      * @param parent The parent of the file dialog
0057      * @param dialogType usage of the file dialog
0058      * @param dialogName the name for the file dialog. This will be used to open
0059      * the filedialog in the last open location, instead the specified directory.
0060      *
0061      * @return The name of the entry user selected in the file dialog
0062      *
0063      */
0064     KexiFileDialog(QWidget *parent,
0065                  KexiFileDialog::DialogType type,
0066                  const QString &dialogName);
0067 
0068     ~KexiFileDialog();
0069 
0070     void setCaption(const QString &caption);
0071 
0072     /**
0073      * @brief setDefaultDir set the default directory to defaultDir
0074      *
0075      * @param defaultDir a path to a file or directory
0076      */
0077     void setDefaultDir(const QString &defaultDir, bool override = false);
0078 
0079     /**
0080      * @brief setOverrideDir override both the default dir and the saved dir found by dialogName
0081      * @param overrideDir a path to a file or directory
0082      */
0083     void setOverrideDir(const QString &overrideDir);
0084 
0085     /**
0086      * @brief setImageFilters sets the name filters for the file dialog to all
0087      * image formats Qt's QImageReader supports.
0088      */
0089     void setImageFilters();
0090 
0091     void setNameFilter(const QString &filter);
0092 
0093     /**
0094      * @brief setNameFilters set a list of description/extension pairs.
0095      *
0096      * These are not registered mimetypes. In contrast with Qt's filedialog namefilters,
0097      * you can only have _one_ pair per line. I.e.
0098      *
0099      * Gif Image (*gif)
0100      * Tiff Image (*tif)
0101      *
0102      * And NOT Images (*gif *tif)
0103      *
0104      * @param filterList
0105      * @param defaultFilter
0106      */
0107     void setNameFilters(const QStringList &filterList,
0108                         QString defaultFilter = QString());
0109     void setMimeTypeFilters(const QStringList &filterList,
0110                             QString defaultFilter = QString());
0111     void setHideNameFilterDetailsOption();
0112 
0113     QStringList nameFilters() const;
0114 
0115     QStringList fileNames();
0116     QString fileName();
0117 
0118     /**
0119      * @brief selectedNameFilter returns the name filter the user selected, either
0120      *    directory or by clicking on it.
0121      * @return
0122      */
0123     QString selectedNameFilter() const;
0124 
0125     QString selectedMimeType() const;
0126 
0127 private Q_SLOTS:
0128 
0129     void filterSelected(const QString &filter);
0130 
0131 private:
0132     /**
0133      * @brief splitNameFilter take a single line of a QDialog name filter and split it
0134      *   into several lines. This is needed because a single line name filter can contain
0135      *   more than one mimetype, making it impossible to figure out the correct extension.
0136      *
0137      *   The methods takes care of some duplicated extensions, like jpeg and jpg.
0138      * @param nameFilter the namefilter to be split
0139      * @param mimeList a pointer to the list with mimes that shouldn't be added.
0140      * @return a stringlist of all name filters.
0141      */
0142     static QStringList splitNameFilter(const QString &nameFilter, QStringList *mimeList);
0143 
0144     void createFileDialog();
0145 
0146     QString getUsedDir(const QString &dialogName);
0147     void saveUsedDir(const QString &fileName, const QString &dialogName);
0148 
0149     const QStringList getFilterStringListFromMime(const QStringList &mimeList,
0150                                                   bool withAllSupportedEntry = false);
0151 
0152 
0153 
0154     class Private;
0155     Private * const d;
0156 };
0157 
0158 #endif /* KEXIFILEDIALOG_H */