Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 QWidget;
32 class QLineEdit;
33
34 /**
35 * \ingroup GUI
36 *  Implementation of model widget for open file widget.
37 *  It can be defined as following:
38 *  \code
39 *      <file_selector
40 *          id="import_file_selector"
41 *          type="open"
42 *          title="Import file"
43 *          path="">
44 *          <validator id="ExchangePlugin_ImportFormat" parameters="BREP:BREPImport,STEP:STEPImport" />
45 *      </file_selector>
46 *  \endcode
47 * It can use following parameters:
48 * - id - name of object attribute
49 * - type - type of dialog. Possible values: open, save.
50 */
51 class MODULEBASE_EXPORT ModuleBase_WidgetFileSelector : public ModuleBase_ModelWidget
52 {
53   Q_OBJECT
54  public:
55   /// Constructor
56   /// \param theParent the parent object
57   /// \param theData the widget configuration. The attribute of the model widget is obtained from
58   ModuleBase_WidgetFileSelector(QWidget* theParent,
59                                 const Config_WidgetAPI* theData);
60   virtual ~ModuleBase_WidgetFileSelector();
61
62   virtual QList<QWidget*> getControls() const;
63
64   /// Returns true if a file on the current path in the line edit
65   /// exists and has supported format
66   bool isCurrentPathValid();
67
68  public slots:
69    /// Processing of path selection button press
70   void onPathSelectionBtn();
71
72   /// Processing of path changing
73   void onPathChanged();
74
75 protected:
76   /// Saves the internal parameters to the given feature
77   /// \return True in success
78   virtual bool storeValueCustom();
79
80   virtual bool restoreValueCustom();
81
82 protected:
83   /// Converts format to filter string
84   static QString formatToFilter(const QString & theFormat);
85
86   /// Returns a short format string of theFilter string
87   static QString filterToShortFormat(const QString & theFilter);
88
89   /// Returns a list of extensions from theFilter
90   static QStringList filterToExtensions(const QString & theFilter);
91
92   /// Returns list of validator formats
93   QStringList getValidatorFormats() const;
94
95   /// Returns string containing formats
96   QString filterString() const;
97
98   /// Add an extension to file name if the extension is not defined
99   /// \param theFileName a file name
100   /// \param theFilter a file filter
101   /// \return the file name with extension
102   static QString applyExtension(const QString& theFileName, const QString& theFilter);
103
104 protected:
105    /// A control for path input
106   QLineEdit* myPathField;
107
108   /// A title of open file dialog box
109   QString myTitle;
110
111   /// A current format
112   QString mySelectedFilter;
113
114   /// A title of open file dialog box
115   enum {
116     WFS_OPEN, ///< open file
117     WFS_SAVE ///< save file
118   } myType; ///< type of dialog
119
120   /// Default path
121   QString myDefaultPath;
122 };
123
124 #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */