QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsfilewidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfilewidget.h
3 
4  ---------------------
5  begin : 17.12.2015
6  copyright : (C) 2015 by Denis Rouzaud
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSFILEWIDGET_H
18 #define QGSFILEWIDGET_H
19 
20 class QLabel;
21 class QToolButton;
22 class QVariant;
23 class QgsFileDropEdit;
24 class QHBoxLayout;
25 #include <QWidget>
26 
27 #include "qgis_gui.h"
28 #include "qgis_sip.h"
29 #include "qgsfilterlineedit.h"
30 
35 class GUI_EXPORT QgsFileWidget : public QWidget
36 {
37 
38 #ifdef SIP_RUN
40  if ( qobject_cast<QgsFileWidget *>( sipCpp ) )
41  sipType = sipType_QgsFileWidget;
42  else
43  sipType = NULL;
44  SIP_END
45 #endif
46 
47 
48  Q_OBJECT
49  Q_PROPERTY( bool fileWidgetButtonVisible READ fileWidgetButtonVisible WRITE setFileWidgetButtonVisible )
50  Q_PROPERTY( bool useLink READ useLink WRITE setUseLink )
51  Q_PROPERTY( bool fullUrl READ fullUrl WRITE setFullUrl )
52  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
53  Q_PROPERTY( QString filter READ filter WRITE setFilter )
54  Q_PROPERTY( QString defaultRoot READ defaultRoot WRITE setDefaultRoot )
55  Q_PROPERTY( StorageMode storageMode READ storageMode WRITE setStorageMode )
56  Q_PROPERTY( RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage )
57 
58  public:
59 
64  {
69  };
70 
75  {
78  RelativeDefaultPath
79  };
80 
84  explicit QgsFileWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
85 
92  QString filePath();
93 
98  static QStringList splitFilePaths( const QString &path );
99 
101  void setFilePath( QString path );
102 
104  void setReadOnly( bool readOnly );
105 
107  QString dialogTitle() const;
108 
113  void setDialogTitle( const QString &title );
114 
116  QString filter() const;
117 
122  void setFilter( const QString &filter );
123 
127  void setSelectedFilter( const QString &selectedFilter ) { mSelectedFilter = selectedFilter; }
128 
132  QString selectedFilter() const { return mSelectedFilter; }
133 
139  void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }
140 
144  bool confirmOverwrite() const { return mConfirmOverwrite; }
145 
147  bool fileWidgetButtonVisible() const;
149  void setFileWidgetButtonVisible( bool visible );
150 
152  bool useLink() const;
154  void setUseLink( bool useLink );
155 
157  bool fullUrl() const;
159  void setFullUrl( bool fullUrl );
160 
162  QString defaultRoot() const;
164  void setDefaultRoot( const QString &defaultRoot );
165 
167  QgsFileWidget::StorageMode storageMode() const;
169  void setStorageMode( QgsFileWidget::StorageMode storageMode );
170 
172  QgsFileWidget::RelativeStorage relativeStorage() const;
174  void setRelativeStorage( QgsFileWidget::RelativeStorage relativeStorage );
175 
181  QgsFilterLineEdit *lineEdit();
182 
183  signals:
184 
188  void fileChanged( const QString &path );
189 
196  void blockEvents( bool ) SIP_SKIP;
197 
198  private slots:
199  void openFileDialog();
200  void textEdited( const QString &path );
201 
202  private:
203  QString mFilePath;
204  bool mButtonVisible = true;
205  bool mUseLink = false;
206  bool mFullUrl = false;
207  QString mDialogTitle;
208  QString mFilter;
209  QString mSelectedFilter;
210  QString mDefaultRoot;
211  bool mConfirmOverwrite = true;
212  StorageMode mStorageMode = GetFile;
213  RelativeStorage mRelativeStorage = Absolute;
214 
215  QLabel *mLinkLabel = nullptr;
216  QgsFileDropEdit *mLineEdit = nullptr;
217  QToolButton *mFileWidgetButton = nullptr;
218  QHBoxLayout *mLayout = nullptr;
219 
221  QString toUrl( const QString &path ) const;
222 
224  QString relativePath( const QString &filePath, bool removeRelative ) const;
225 
226  friend class TestQgsFileWidget;
227 };
228 
229 
230 
232 
233 #ifndef SIP_RUN
234 
244 class GUI_EXPORT QgsFileDropEdit: public QgsFilterLineEdit
245 {
246  Q_OBJECT
247 
248  public:
249  QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
250 
251  void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
252 
253  void setFilters( const QString &filters );
254 
255  protected:
256 
257  void dragEnterEvent( QDragEnterEvent *event ) override;
258  void dragLeaveEvent( QDragLeaveEvent *event ) override;
259  void dropEvent( QDropEvent *event ) override;
260  void paintEvent( QPaintEvent *e ) override;
261 
262  private:
263 
265  QString acceptableFilePath( QDropEvent *event ) const;
266 
267  QStringList mAcceptableExtensions;
269  bool mDragActive;
270  friend class TestQgsFileWidget;
271 };
272 
273 #endif
274 
276 #endif // QGSFILEWIDGET_H
QString selectedFilter() const
Returns the selected filter from the last opened file dialog.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Select a directory.
Definition: qgsfilewidget.h:66
bool confirmOverwrite() const
Returns whether a confirmation will be shown when overwriting an existing file.
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:35
#define SIP_SKIP
Definition: qgis_sip.h:126
Select a single file.
Definition: qgsfilewidget.h:65
#define SIP_END
Definition: qgis_sip.h:189
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
void setConfirmOverwrite(bool confirmOverwrite)
Sets whether a confirmation to overwrite an existing file will appear.
StorageMode
The StorageMode enum determines if the file picker should pick files or directories.
Definition: qgsfilewidget.h:63
Select multiple files.
Definition: qgsfilewidget.h:67
Select a single new or pre-existing file.
Definition: qgsfilewidget.h:68
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
void setSelectedFilter(const QString &selectedFilter)
Sets the selected filter when the file dialog opens.
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:74