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