| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#ifndef GUI_FILEDIALOG_H |
| 25 |
#define GUI_FILEDIALOG_H |
| 26 |
|
| 27 |
#include <QCompleter> |
| 28 |
#include <QFileDialog> |
| 29 |
#include <QFileIconProvider> |
| 30 |
#include <QFileSystemModel> |
| 31 |
#include <QPointer> |
| 32 |
#include <FCGlobal.h> |
| 33 |
|
| 34 |
class QButtonGroup; |
| 35 |
class QDialogButtonBox; |
| 36 |
class QGridLayout; |
| 37 |
class QGroupBox; |
| 38 |
class QHBoxLayout; |
| 39 |
class QLineEdit; |
| 40 |
class QSpacerItem; |
| 41 |
|
| 42 |
namespace Gui |
| 43 |
{ |
| 44 |
|
| 45 |
/*! |
| 46 |
* \brief The DialogOptions class |
| 47 |
* Helper class to control whether to use native or Qt dialogs. |
| 48 |
*/ |
| 49 |
class GuiExport DialogOptions |
| 50 |
{ |
| 51 |
public: |
| 52 |
static bool dontUseNativeFileDialog(); |
| 53 |
static bool dontUseNativeColorDialog(); |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* The FileDialog class provides dialogs that allow users to select files or directories. |
| 58 |
* \author Werner Mayer |
| 59 |
*/ |
| 60 |
class GuiExport FileDialog: public QFileDialog |
| 61 |
{ |
| 62 |
Q_OBJECT |
| 63 |
|
| 64 |
public: |
| 65 |
static QString getOpenFileName( |
| 66 |
QWidget* parent = nullptr, |
| 67 |
const QString& caption = QString(), |
| 68 |
const QString& dir = QString(), |
| 69 |
const QString& filter = QString(), |
| 70 |
QString* selectedFilter = nullptr, |
| 71 |
Options options = Options() |
| 72 |
); |
| 73 |
static QString getSaveFileName( |
| 74 |
QWidget* parent = nullptr, |
| 75 |
const QString& caption = QString(), |
| 76 |
const QString& dir = QString(), |
| 77 |
const QString& filter = QString(), |
| 78 |
QString* selectedFilter = nullptr, |
| 79 |
Options options = Options() |
| 80 |
); |
| 81 |
static QString getExistingDirectory( |
| 82 |
QWidget* parent = nullptr, |
| 83 |
const QString& caption = QString(), |
| 84 |
const QString& dir = QString(), |
| 85 |
Options options = ShowDirsOnly |
| 86 |
); |
| 87 |
static QStringList getOpenFileNames( |
| 88 |
QWidget* parent = nullptr, |
| 89 |
const QString& caption = QString(), |
| 90 |
const QString& dir = QString(), |
| 91 |
const QString& filter = QString(), |
| 92 |
QString* selectedFilter = nullptr, |
| 93 |
Options options = Options() |
| 94 |
); |
| 95 |
|
| 96 |
/*! Return the last directory a file was read from or saved to. */ |
| 97 |
static QString getWorkingDirectory(); |
| 98 |
/*! Set the directory a file was read from or saved to. */ |
| 99 |
static void setWorkingDirectory(const QString&); |
| 100 |
static QString restoreLocation(); |
| 101 |
static void saveLocation(const QString&); |
| 102 |
|
| 103 |
explicit FileDialog(QWidget* parent = nullptr); |
| 104 |
~FileDialog() override; |
| 105 |
|
| 106 |
void accept() override; |
| 107 |
|
| 108 |
private Q_SLOTS: |
| 109 |
void onSelectedFilter(const QString&); |
| 110 |
|
| 111 |
private: |
| 112 |
bool hasSuffix(const QString&) const; |
| 113 |
static QList<QUrl> fetchSidebarUrls(); |
| 114 |
static QString workingDirectory; |
| 115 |
static void getSuffixesDescription(QStringList& suffixes, const QString* suffixDescriptions); |
| 116 |
}; |
| 117 |
|
| 118 |
// ---------------------------------------------------------------------- |
| 119 |
|
| 120 |
/** |
| 121 |
* The FileOptionsDialog class provides an extensible file dialog with an additional widget either |
| 122 |
* at the right or at the bottom, that can be shown or hidden with the 'Extended' button. |
| 123 |
* @author Werner Mayer |
| 124 |
*/ |
| 125 |
class GuiExport FileOptionsDialog: public QFileDialog |
| 126 |
{ |
| 127 |
Q_OBJECT |
| 128 |
|
| 129 |
public: |
| 130 |
enum ExtensionPosition |
| 131 |
{ |
| 132 |
ExtensionRight = 0, |
| 133 |
ExtensionBottom = 1 |
| 134 |
}; |
| 135 |
|
| 136 |
FileOptionsDialog(QWidget* parent, Qt::WindowFlags); |
| 137 |
~FileOptionsDialog() override; |
| 138 |
|
| 139 |
void accept() override; |
| 140 |
|
| 141 |
void setOptionsWidget(ExtensionPosition pos, QWidget*, bool show = false); |
| 142 |
QWidget* getOptionsWidget() const; |
| 143 |
|
| 144 |
protected Q_SLOTS: |
| 145 |
void toggleExtension(); |
| 146 |
|
| 147 |
private: |
| 148 |
QSize oldSize; |
| 149 |
ExtensionPosition extensionPos; |
| 150 |
QPushButton* extensionButton; |
| 151 |
QPointer<QWidget> extensionWidget; |
| 152 |
}; |
| 153 |
|
| 154 |
// ---------------------------------------------------------------------- |
| 155 |
|
| 156 |
/** |
| 157 |
* The FileIconProvider class provides icons for FileDialog to use. |
| 158 |
* \author Werner Mayer |
| 159 |
*/ |
| 160 |
class FileIconProvider: public QFileIconProvider |
| 161 |
{ |
| 162 |
public: |
| 163 |
FileIconProvider(); |
| 164 |
~FileIconProvider() override; |
| 165 |
|
| 166 |
QIcon icon(IconType type) const override; |
| 167 |
QIcon icon(const QFileInfo& info) const override; |
| 168 |
QString type(const QFileInfo& info) const override; |
| 169 |
}; |
| 170 |
|
| 171 |
// ---------------------------------------------------------------------- |
| 172 |
|
| 173 |
/** |
| 174 |
* The FileChooser class provides a lineedit with a button on the right side |
| 175 |
* to specify a file or directory. |
| 176 |
* \author Werner Mayer |
| 177 |
*/ |
| 178 |
class GuiExport FileChooser: public QWidget |
| 179 |
{ |
| 180 |
Q_OBJECT |
| 181 |
|
| 182 |
public: |
| 183 |
enum Mode |
| 184 |
{ |
| 185 |
File, |
| 186 |
Directory |
| 187 |
}; |
| 188 |
enum AcceptMode |
| 189 |
{ |
| 190 |
AcceptOpen, |
| 191 |
AcceptSave |
| 192 |
}; |
| 193 |
|
| 194 |
Q_ENUM(Mode) |
| 195 |
Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged) |
| 196 |
Q_ENUM(AcceptMode) |
| 197 |
Q_PROPERTY(AcceptMode acceptMode READ acceptMode WRITE setAcceptMode NOTIFY acceptModeChanged) |
| 198 |
Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged) |
| 199 |
Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged) |
| 200 |
Q_PROPERTY(QString buttonText READ buttonText WRITE setButtonText NOTIFY buttonTextChanged) |
| 201 |
|
| 202 |
public: |
| 203 |
explicit FileChooser(QWidget* parent = nullptr); |
| 204 |
~FileChooser() override; |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the set filter. |
| 208 |
*/ |
| 209 |
QString filter() const; |
| 210 |
|
| 211 |
/** |
| 212 |
* Returns the filename. |
| 213 |
*/ |
| 214 |
QString fileName() const; |
| 215 |
|
| 216 |
/** |
| 217 |
* Returns true if this widgets is set to choose a file, if it is |
| 218 |
* set to choose false is returned. |
| 219 |
*/ |
| 220 |
Mode mode() const; |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns the button's text. |
| 224 |
*/ |
| 225 |
QString buttonText() const; |
| 226 |
|
| 227 |
/** |
| 228 |
* Sets the accept mode. |
| 229 |
*/ |
| 230 |
void setAcceptMode(AcceptMode mode); |
| 231 |
/** |
| 232 |
* Returns the accept mode. |
| 233 |
*/ |
| 234 |
AcceptMode acceptMode() const |
| 235 |
{ |
| 236 |
return accMode; |
| 237 |
} |
| 238 |
|
| 239 |
public Q_SLOTS: |
| 240 |
virtual void setFileName(const QString& fn); |
| 241 |
virtual void setMode(Gui::FileChooser::Mode m); |
| 242 |
virtual void setFilter(const QString&); |
| 243 |
virtual void setButtonText(const QString&); |
| 244 |
|
| 245 |
Q_SIGNALS: |
| 246 |
void fileNameChanged(const QString&); |
| 247 |
void fileNameSelected(const QString&); |
| 248 |
void filterChanged(const QString&); |
| 249 |
void buttonTextChanged(const QString&); |
| 250 |
void modeChanged(Gui::FileChooser::Mode); |
| 251 |
void acceptModeChanged(Gui::FileChooser::AcceptMode); |
| 252 |
|
| 253 |
private Q_SLOTS: |
| 254 |
void chooseFile(); |
| 255 |
void editingFinished(); |
| 256 |
|
| 257 |
protected: |
| 258 |
void resizeEvent(QResizeEvent*) override; |
| 259 |
|
| 260 |
private: |
| 261 |
QLineEdit* lineEdit; |
| 262 |
QCompleter* completer; |
| 263 |
QFileSystemModel* fs_model; |
| 264 |
QPushButton* button; |
| 265 |
Mode md; |
| 266 |
AcceptMode accMode; |
| 267 |
QString _filter; |
| 268 |
}; |
| 269 |
|
| 270 |
// ---------------------------------------------------------------------- |
| 271 |
|
| 272 |
/** |
| 273 |
* The SelectModule class provides a list of radio buttons to choose |
| 274 |
* the module that should handle a certain file type. |
| 275 |
* @author Werner Mayer |
| 276 |
*/ |
| 277 |
class GuiExport SelectModule: public QDialog |
| 278 |
{ |
| 279 |
Q_OBJECT |
| 280 |
|
| 281 |
public: |
| 282 |
using Dict = QMap<QString, QString>; |
| 283 |
|
| 284 |
SelectModule(const QString& type, const Dict&, QWidget* parent); |
| 285 |
~SelectModule() override; |
| 286 |
QString getModule() const; |
| 287 |
|
| 288 |
/** @name Import/Export handler |
| 289 |
* These methods accepts a file name or a list of file names and return |
| 290 |
* a map of file names with the associated Python module that should open |
| 291 |
* the file. |
| 292 |
*/ |
| 293 |
//@{ |
| 294 |
static Dict exportHandler(const QString& fileName, const QString& filter = QString()); |
| 295 |
static Dict exportHandler(const QStringList& fileNames, const QString& filter = QString()); |
| 296 |
static Dict importHandler(const QString& fileName, const QString& filter = QString()); |
| 297 |
static Dict importHandler(const QStringList& fileNames, const QString& filter = QString()); |
| 298 |
//@} |
| 299 |
|
| 300 |
void accept() override; |
| 301 |
void reject() override; |
| 302 |
|
| 303 |
private Q_SLOTS: |
| 304 |
void onButtonClicked(); |
| 305 |
|
| 306 |
private: |
| 307 |
QDialogButtonBox* buttonBox; |
| 308 |
QButtonGroup* group; |
| 309 |
QGridLayout* gridLayout; |
| 310 |
QHBoxLayout* hboxLayout; |
| 311 |
QGroupBox* groupBox; |
| 312 |
QGridLayout* gridLayout1; |
| 313 |
QSpacerItem* spacerItem; |
| 314 |
QSpacerItem* spacerItem1; |
| 315 |
}; |
| 316 |
|
| 317 |
} // namespace Gui |
| 318 |
|
| 319 |
#endif // GUI_FILEDIALOG_H |