| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_QTGUI_FILEFORMATDIALOG_H |
| 7 |
#define AVOGADRO_QTGUI_FILEFORMATDIALOG_H |
| 8 |
|
| 9 |
#include "avogadroqtguiexport.h" |
| 10 |
#include <QtWidgets/QFileDialog> |
| 11 |
|
| 12 |
#include <avogadro/io/fileformat.h> |
| 13 |
|
| 14 |
#include <vector> |
| 15 |
|
| 16 |
namespace Avogadro { |
| 17 |
namespace Core { |
| 18 |
class Molecule; |
| 19 |
} |
| 20 |
|
| 21 |
namespace QtGui { |
| 22 |
/** |
| 23 |
* @class FileFormatDialog fileformatdialog.h |
| 24 |
* <avogadro/qtgui/fileformatdialog.h> |
| 25 |
* |
| 26 |
* Allow users to select save/open filenames that can be handled by registered |
| 27 |
* FileFormats. |
| 28 |
*/ |
| 29 |
class AVOGADROQTGUI_EXPORT FileFormatDialog : private QFileDialog |
| 30 |
{ |
| 31 |
Q_OBJECT |
| 32 |
public: |
| 33 |
/** |
| 34 |
* Container for a filename and a compatible file format, used as return |
| 35 |
* values in static functions. |
| 36 |
* @note The FileFormat will be set to nullptr to indicate an error. |
| 37 |
* @note The FileFormat points to the reference instance held by the |
| 38 |
* FileFormatManager. Use FileFormat::newInstance() to create a usable copy. |
| 39 |
*/ |
| 40 |
typedef QPair<const Io::FileFormat*, QString> FormatFilePair; |
| 41 |
|
| 42 |
/** |
| 43 |
* @brief Show a QFileDialog to prompt the user for a file to open and resolve |
| 44 |
* any file format conflicts. This method returns the selected file and |
| 45 |
* FileFormat reader. |
| 46 |
* @param parent The parent of the dialog windows. |
| 47 |
* @param caption The dialog window titles. |
| 48 |
* @param dir The initial directory shown to the user. |
| 49 |
* @param filter A list of filters for limiting the files shown to the user. |
| 50 |
* See the QFileDialog documentation for format. If the string is empty, a |
| 51 |
* default list of all suitable registered formats will be used. |
| 52 |
* @return A FormatFilePair object containing the absolute file path and a |
| 53 |
* compatible file reader. If an error occurs, the format pointer will be |
| 54 |
* nullptr. |
| 55 |
*/ |
| 56 |
static FormatFilePair fileToRead(QWidget* parent, |
| 57 |
const QString& caption = QString(), |
| 58 |
const QString& dir = QString(), |
| 59 |
const QString& filter = QString()); |
| 60 |
|
| 61 |
/** |
| 62 |
* @brief Show a QFileDialog to prompt the user for a file to save and resolve |
| 63 |
* any file format conflicts. This method returns the selected file and |
| 64 |
* FileFormat writer. |
| 65 |
* @param parent The parent of the dialog windows. |
| 66 |
* @param caption The dialog window titles. |
| 67 |
* @param dir The initial directory shown to the user. |
| 68 |
* @param filter A list of filters for limiting the files shown to the user. |
| 69 |
* See the QFileDialog documentation for format. If the string is empty, a |
| 70 |
* default list of all suitable registered formats will be used. |
| 71 |
* @return A FormatFilePair object containing the absolute file path and a |
| 72 |
* compatible file writer. If an error occurs, the format pointer will be |
| 73 |
* nullptr. |
| 74 |
*/ |
| 75 |
static FormatFilePair fileToWrite(QWidget* parent, |
| 76 |
const QString& caption = QString(), |
| 77 |
const QString& dir = QString(), |
| 78 |
const QString& filter = QString()); |
| 79 |
|
| 80 |
/** |
| 81 |
* Given a filename and a set of Io::FileFormat::Operation flags, find a |
| 82 |
* suitable file format from the FileFormatManager. If multiple readers are |
| 83 |
* found, ask the user to select one. If no suitable format is found, return |
| 84 |
* nullptr. |
| 85 |
* @param parentWidget Parent for any dialog windows that will appear. |
| 86 |
* @param caption Window title for any dialog windows. |
| 87 |
* @param fileName Filename to use when searching for a format. Formats are |
| 88 |
* chosen based on the file extension. |
| 89 |
* @param formatFlags Operations that the format must support. Most likely |
| 90 |
* (Io::FileFormat::)Read | File or Write | File. |
| 91 |
* @param formatPrefix Filter on the supplied prefix (default to none). |
| 92 |
* @return The selected matching reader, or nullptr if no reader is found. |
| 93 |
*/ |
| 94 |
static const Io::FileFormat* findFileFormat( |
| 95 |
QWidget* parentWidget, const QString& caption, const QString& fileName, |
| 96 |
const Io::FileFormat::Operations formatFlags, |
| 97 |
const QString& formatPrefix = QString()); |
| 98 |
|
| 99 |
private: |
| 100 |
/** |
| 101 |
* Constructor is private for now to force use of static methods. This |
| 102 |
* may be made public at some point if additional API is needed or more |
| 103 |
* complex use cases arise. |
| 104 |
*/ |
| 105 |
explicit FileFormatDialog(QWidget* parent = nullptr); |
| 106 |
~FileFormatDialog() override; |
| 107 |
|
| 108 |
/** |
| 109 |
* @return A filter string for use with a QFileDialog, containing entries |
| 110 |
* for all file extensions registered with FileFormatManager, as well as a |
| 111 |
* "catch-all" entry with all known formats, and an "All files (*)" entry. |
| 112 |
* Only formats registered with (Read | File) will be used. |
| 113 |
*/ |
| 114 |
static QString readFileFilter(); |
| 115 |
|
| 116 |
/** |
| 117 |
* @return A filter string for use with a QFileDialog, containing entries |
| 118 |
* for all file extensions registered with FileFormatManager. An |
| 119 |
* "All files (*)" entry is added as well. |
| 120 |
* Only formats registered with (Write | File) will be used. |
| 121 |
*/ |
| 122 |
static QString writeFileFilter(); |
| 123 |
|
| 124 |
/** |
| 125 |
* Used internally by readFileFilter() and writeFileFilter(). |
| 126 |
* @{ |
| 127 |
*/ |
| 128 |
public: // Must be public for operator declarations |
| 129 |
enum FilterStringOption |
| 130 |
{ |
| 131 |
NoFilterStringOption = 0x0, |
| 132 |
AllFormats = 0x1, |
| 133 |
AllFiles = 0x2, |
| 134 |
WriteFormats = 0x4, |
| 135 |
}; |
| 136 |
Q_DECLARE_FLAGS(FilterStringOptions, FilterStringOption) |
| 137 |
|
| 138 |
private: |
| 139 |
static QString generateFilterString( |
| 140 |
const std::vector<const Io::FileFormat*>& ffs, FilterStringOptions options); |
| 141 |
/** @} */ |
| 142 |
|
| 143 |
/** |
| 144 |
* Show a dialog to resolve file format conflicts. This allows the user to |
| 145 |
* select between multiple instances of FileFormat that can handle their |
| 146 |
* chosen file extension. |
| 147 |
* @param parentWidget Widget to use as dialog parent. |
| 148 |
* @param ffs Conflicting FileFormats that the user should choose from. |
| 149 |
* @param caption The title of the dialog window. |
| 150 |
* @param prompt The text in the dialog window. |
| 151 |
* @param settingsKey An optional QSettings key that will be used to |
| 152 |
* store the user's choice for initializing the dialog next time. |
| 153 |
* @return The selected FileFormat instance. |
| 154 |
*/ |
| 155 |
static const Io::FileFormat* selectFileFormat( |
| 156 |
QWidget* parentWidget, const std::vector<const Io::FileFormat*>& ffs, |
| 157 |
const QString& caption, const QString& prompt, |
| 158 |
const QString& settingsKey = QString(), |
| 159 |
const QString& formatPrefix = QString()); |
| 160 |
}; |
| 161 |
|
| 162 |
} // namespace QtGui |
| 163 |
} // namespace Avogadro |
| 164 |
|
| 165 |
Q_DECLARE_OPERATORS_FOR_FLAGS( |
| 166 |
Avogadro::QtGui::FileFormatDialog::FilterStringOptions) |
| 167 |
|
| 168 |
#endif // AVOGADRO_QTGUI_FILEFORMATDIALOG_H |