Salome HOME
Merge remote-tracking branch 'remotes/origin/Dev_FolderFeature'
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFileSelector.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef MODULEBASE_WIDGETFILESELECTOR_H_
22 #define MODULEBASE_WIDGETFILESELECTOR_H_
23
24 #include <ModuleBase.h>
25 #include <ModuleBase_ModelWidget.h>
26
27 #include <QList>
28 #include <QString>
29 #include <QStringList>
30
31 class QFileDialog;
32 class QLineEdit;
33 class QWidget;
34
35 /**
36 * \ingroup GUI
37 *  Implementation of model widget for open file widget.
38 *  It can be defined as following:
39 *  \code
40 *      <file_selector
41 *          id="import_file_selector"
42 *          type="open"
43 *          title="Import file"
44 *          path="">
45 *          <validator id="ExchangePlugin_ImportFormat" parameters="BREP:BREPImport,STEP:STEPImport" />
46 *      </file_selector>
47 *  \endcode
48 * It can use following parameters:
49 * - id - name of object attribute
50 * - type - type of dialog. Possible values: open, save.
51 */
52 class MODULEBASE_EXPORT ModuleBase_WidgetFileSelector : public ModuleBase_ModelWidget
53 {
54   Q_OBJECT
55  public:
56   /// Constructor
57   /// \param theParent the parent object
58   /// \param theData the widget configuration. The attribute of the model widget is obtained from
59   ModuleBase_WidgetFileSelector(QWidget* theParent,
60                                 const Config_WidgetAPI* theData);
61   virtual ~ModuleBase_WidgetFileSelector();
62
63   virtual QList<QWidget*> getControls() const;
64
65   /// Returns true if a file on the current path in the line edit
66   /// exists and has supported format
67   bool isCurrentPathValid();
68
69 public slots:
70    /// Processing of path selection button press
71   void onPathSelectionBtn();
72
73   /// Processing of path changing
74   void onPathChanged();
75
76 protected:
77   /// Reject the current editor dialog if it is shown and returns true.
78   virtual bool processEscape();
79
80   /// Saves the internal parameters to the given feature
81   /// \return True in success
82   virtual bool storeValueCustom();
83
84   virtual bool restoreValueCustom();
85
86 protected:
87   /// Converts format to filter string
88   static QString formatToFilter(const QString & theFormat);
89
90   /// Returns a short format string of theFilter string
91   static QString filterToShortFormat(const QString & theFilter);
92
93   /// Returns a list of extensions from theFilter
94   static QStringList filterToExtensions(const QString & theFilter);
95
96   /// Returns list of validator formats
97   QStringList getValidatorFormats() const;
98
99   /// Returns string containing formats
100   QString filterString() const;
101
102   /// Add an extension to file name if the extension is not defined
103   /// \param theFileName a file name
104   /// \param theFilter a file filter
105   /// \return the file name with extension
106   static QString applyExtension(const QString& theFileName, const QString& theFilter);
107
108 protected:
109    /// A control for path input
110   QLineEdit* myPathField;
111   QFileDialog* myFileDialog; ///< started dialog
112
113   /// A title of open file dialog box
114   QString myTitle;
115
116   /// A current format
117   QString mySelectedFilter;
118
119   /// A title of open file dialog box
120   enum {
121     WFS_OPEN, ///< open file
122     WFS_SAVE ///< save file
123   } myType; ///< type of dialog
124
125   /// Default path
126   QString myDefaultPath;
127 };
128
129 #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */