1 #ifndef PATHPICKER_H
2 #define PATHPICKER_H
3 
4 #include <QLineEdit>
5 #include <QToolButton>
6 
7 
8 namespace QSint
9 {
10 
11 
12 /**
13     \brief Widget combining a line edit and a tool button used for
14     selection of file system objects.
15     \since 0.2.1
16 
17     \image html PathPicker.png An example of PathPicker
18 
19     Click on the tool button invokes a modal dialog where you can choose a file,
20     a directory or a number of files to open (or a file to save). The selected
21     path(es) will be then transferred to the line edit along with emitting the
22     picked() signal.
23 
24     Using setObjectsToPick() and setDialogType(), you can specify the style of the
25     pick dialog and the file system objects (see PickFlags and DialogFlags enums).
26 
27     Use setCaption() to modify the default caption of the pick dialog.
28 
29     Method setDefaultDirectory() lets you set the initial directory where to point the
30     pick dialog.
31 
32     Via setFilters() you can specify additional file name wildcards (See Qt documentation
33     on the QFileDialog class).
34 
35     To customize the look of the tool button (i.e. icon, text, etc.), retrieve the
36     pointer to the tool button via button() method. Use editor() method to get the
37     line edit widget.
38 */
39 class PathPicker : public QWidget
40 {
41     Q_OBJECT
42 
43 public:
44     /// \brief Defines file system objects to pick.
45     enum PickFlags {
46         /// single existing file can be picked (default)
47         PF_EXISTING_FILE,
48         /// one or more existing files can be picked
49         PF_EXISTING_FILES,
50         /// single existing directory can be picked
51         PF_EXISTING_DIR,
52         /// a file to save can be picked (non-existing as well)
53         PF_SAVE_FILE
54     };
55 
56     /// \brief Defines pick dialog type
57     enum DialogFlags {
58         /// use system dialog
59         DF_SYSTEM = 0,
60         /// use default Qt dialog
61         DF_DEFAULT
62     };
63 
64     /** Constructor.
65       */
66     explicit PathPicker(QWidget *parent = 0);
67 
68 	/** Returns current path text.
69 	*/
currentPath()70 	QString currentPath() const { return m_editor->text(); }
71 
72 	/** Sets current path text.
73 	*/
setCurrentPath(const QString & path)74 	void setCurrentPath(const QString& path) { m_editor->setText(path); }
75 
76     /** Enables (default) or disables manual path editor according to \a set parameter.
77       */
78     void setEditorEnabled(bool set);
79 
80     /** Returns \a true if manual path editor is enabled, \a false otherwise.
81       */
isEditorEnabled()82     inline bool isEditorEnabled() const { return m_editorEnabled; }
83 
84     /** Defines objects being picked as \a PickFlags.
85      * If \a updateIcon is \a true then style default icon will be assigned to the pick button.
86       */
87     void setObjectsToPick(int flags, bool updateIcon = true);
88 
89     /** Returns \a PickFlags defining objects to pick.
90       */
objectsToPick()91     inline int objectsToPick() const { return m_pickMode; }
92 
93     /** Defines pick dialog type as \a DialogFlags.
94       */
setDialogType(int flags)95     void setDialogType(int flags) { m_dialogMode = flags; }
96 
97     /** Returns \a DialogFlags defining dialog type.
98       */
dialogType()99     inline int dialogType() const { return m_dialogMode; }
100 
101     /** Defines the default directory to open when showing the dialog.
102       */
setDefaultDirectory(const QString & dirName)103     void setDefaultDirectory(const QString& dirName) { m_dir = dirName; }
104 
105     /** Returns the default directory which is open when showing the dialog.
106       */
defaultDirectory()107 	inline QString defaultDirectory() const { return m_dir; }
108 
109 	/** Returns the currently chosen directory.
110 	 * \since 0.3
111 	  */
currentDirectory()112 	inline QString currentDirectory() const { return m_editor->text(); }
113 
114     /** Defines the file name filters applied to the filenames.
115       */
setFilters(const QString & filters)116     void setFilters(const QString& filters) { m_filter = filters; }
117 
118     /** Returns the active file name filters applied to the filenames.
119       */
filters()120     inline QString filters() const { return m_filter; }
121 
122     /** Defines the caption of the pick dialog. If no caption has been
123         specified, then the default one will be shown.
124       */
setCaption(const QString & text)125     void setCaption(const QString& text) { m_caption = text; }
126 
127     /** Returns the caption of the pick dialog, or empty string if the
128         default one should be used.
129       */
caption()130     inline QString caption() const { return m_caption; }
131 
132     /** Returns the tool button widget.
133       */
button()134     QToolButton* button() { return m_button; }
135 
136     /** Returns the line edit widget.
137       */
editor()138     QLineEdit* editor() { return m_editor; }
139 
140 Q_SIGNALS:
141     /** Emitted when user is about to pick an object from the dialog or editor.
142       */
143     void beforePicked();
144 
145     /** Emitted when user picks an object from the dialog or editor.
146         \a path
147       */
148     void picked(const QString& path);
149 
150     /** Emitted when path is changed by some way.
151       */
152 	void changed();
153 
154 protected Q_SLOTS:
155     /** Invokes file object pick dialog. When a valid object(s) has been picked
156         from the filesystem, the implementation should set the content of the editor
157         and emit \a picked() signal.
158       */
159     virtual void showPickDialog();
160 
161 protected:
162     QLineEdit *m_editor;
163     bool m_editorEnabled;
164 
165     QToolButton *m_button;
166 
167     int m_pickMode;
168     int m_dialogMode;
169 
170     QString m_filter;
171     QString m_dir;
172     QString m_caption;
173 };
174 
175 
176 }
177 
178 
179 #endif // PATHPICKER_H
180