// $Id: QtUtils/RwFileDialog.hpp 2013/09/21 19:30:38 philw $ // A wrapper class for QFileDialog's static and instance-based APIs. // // ========================================= // SEE Class Description in RwFileDialog.cpp // SEE Class Description in RwFileDialog.cpp // SEE Class Description in RwFileDialog.cpp // ========================================= // // namespace RwFile // class RwFileDialog // class RwOpenFileDialog : public RwFileDialog // class RwSaveFileDialog : public RwFileDialog // //-- #pragma once #ifndef RwFileDialogINCLUDED #define RwFileDialogINCLUDED #include #include #include #include // (for former QFileDialog clients). #include // (for former QFileDialog clients). // ********************************** // *** namespace RwFile symbols *** // ********************************** namespace RwFile { typedef enum { // RiverWare File Application For_Und = 0, // For_Other, // For_Model, // Model File For_RuleSet, // RPL Rule Set For_OptSet, // Optimization Set For_GblSet, // Global RPL Set For_Obj, // Object (and such) Serializations For_Output, // Output Files (of all types). } UseType; typedef enum { // QFileDialog Modes ... AnyFile = 0, // The name of a file, whether it exists or not. ExistingFile = 1, // The name of a single existing file. DirectorySave = 2, // The name of a directory, for Save DirectoryLoad = 3, // The name of a directory, for Load ExistingFiles = 4, // The names of zero or more existing files. // Aliases SaveFile = AnyFile, LoadFile = ExistingFile, LoadFiles = ExistingFiles } FileMode; typedef enum { // QFileDialog Options (SUBSET) // Only show directories in the file dialog. By default both files and // directories are shown. (Valid only in the Directory file modes.) ShowDirsOnly = 0x00000001, // Don't ask for confirmation if an existing file is selected. // By default confirmation is requested. DontConfirmOverwrite = 0x00000004 } Options; typedef enum { // QFileDialog::DialogLabel // LookIn = 0, // FileName = 1, // FileType = 2, Accept = 3, Reject = 4 } LabelId; } // **************************** // *** class RwFileDialog *** // **************************** class RwFileDialog { protected: typedef RwFile::UseType UseType; typedef RwFile::FileMode FileMode; typedef RwFile::Options Options; typedef RwFile::LabelId LabelId; private: // Special RiverWare Fields static bool _useNativeChooser; UseType _useType; // for default history and filters // Input Fields QWidget* _parentWid; QString _windowTitle; FileMode _fileMode; int _options; // Options flags QString _initPath; QStringList _historyPaths; QString _nameFilters; // Dialog Customization QString _buttonLabel_Accept; QString _buttonLabel_Reject; // Output Fields QString _selNameFilterLocal; // I/O QString* _selNameFilterPtr; // I/O, from client QStringList _selectedFiles; public: // ************************ // *** Static Methods *** // ************************ static void setUseNativeChooser (bool on); static bool isUsingNativeChooser() { return _useNativeChooser; } static void setAndTestChooserType (bool native); // or Qt instance. // Note: The instance-based use of this class is preferred. These // static chooser functions are provided for compatibility. static QString getOpenFileName (UseType, QWidget* parentWid = NULL, const QString& caption = QString(), const QString& initPath = QString(), const QString& filter = QString(), QString* selectedFilter = NULL, int options = 0); // Options flags static QString getSaveFileName (UseType, QWidget* parentWid = NULL, const QString& caption = QString(), const QString& initPath = QString(), const QString& filter = QString(), QString* selectedFilter = NULL, int options = 0); // Options flags static QString getExistingDirectory (UseType, QWidget* parentWid = 0, const QString& caption = QString(), const QString& dir = QString(), int options = RwFile::ShowDirsOnly); // Static methods with UseType defaulted to For_Other, // identical parameters to QFileDialog static methods: static QString getOpenFileName ( QWidget* parentWid = NULL, const QString& caption = QString(), const QString& initPath = QString(), const QString& filter = QString(), QString* selectedFilter = NULL, int options = 0); // Options flags static QString getSaveFileName ( QWidget* parentWid = NULL, const QString& caption = QString(), const QString& initPath = QString(), const QString& filter = QString(), QString* selectedFilter = NULL, int options = 0); // Options flags static QString getExistingDirectory ( QWidget* parentWid = 0, const QString& caption = QString(), const QString& dir = QString(), int options = 0); // Options flags // ************************** // *** Instance Methods *** // ************************** // Constructors RwFileDialog (UseType useType = RwFile::For_Und, QWidget* parent = NULL); // construction helper subclasses // class RwOpenFileDialog; // : public RwFileDialog, see below. // class RwSaveFileDialog; // : public RwFileDialog, see below. virtual ~RwFileDialog(); // File Chooser Parameter Setters // Note: History is not supported by the native selector mode. void setWindowTitle (const QString& winTitle); void setFileMode (FileMode); void setOptions (int); // Options flags void selectFile (const QString& filename); // initPath void setDirectory (const QString& fname) { selectFile (fname); } void setHistory (const QStringList& paths); // File Filter (I/O) void setNameFilter (const QString&); // multiple: sep by ';;' void setNameFilters (const QStringList&); void selectNameFilter (const QString&); void setSelectedNameFilterPtr (QString* strPtr); QString selectedNameFilter() const { return _selNameFilterLocal; } // Dialog customization void setLabelText (LabelId, const QString& text); // File Chooser Results QStringList selectedFiles() const { return _selectedFiles; } QString selectedFile() const; // first file, if multiple were selected. // Show and Execute File Chooser Dialog int exec(); // Returns QDialog::Accepted or QDialog::Rejected private: void setDefaultsForUseType(); int execNative(); // show QFileDialog w/static functions (native). int execInstance(); // show QFileDialog as an instance (non-native). void recordPickedFileInHistory(); }; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- class RwOpenFileDialog : public RwFileDialog { public: RwOpenFileDialog (UseType useType = RwFile::For_Und, QWidget* parentWid = NULL) : RwFileDialog (useType, parentWid) { setFileMode (RwFile::LoadFile); } }; class RwSaveFileDialog : public RwFileDialog { public: RwSaveFileDialog (UseType useType = RwFile::For_Und, QWidget* parentWid = NULL) : RwFileDialog (useType, parentWid) { setFileMode (RwFile::SaveFile); } }; #endif // RwFileDialogINCLUDED //--- (end RwFileDialog.hpp) ---