QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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:
185  void fileChanged( const QString & );
186 
187  private slots:
188  void openFileDialog();
189  void textEdited( const QString &path );
190 
191  private:
192  QString mFilePath;
193  bool mButtonVisible = true;
194  bool mUseLink = false;
195  bool mFullUrl = false;
196  QString mDialogTitle;
197  QString mFilter;
198  QString mSelectedFilter;
199  QString mDefaultRoot;
200  bool mConfirmOverwrite = true;
201  StorageMode mStorageMode = GetFile;
202  RelativeStorage mRelativeStorage = Absolute;
203 
204  QLabel *mLinkLabel = nullptr;
205  QgsFileDropEdit *mLineEdit = nullptr;
206  QToolButton *mFileWidgetButton = nullptr;
207  QHBoxLayout *mLayout = nullptr;
208 
210  QString toUrl( const QString &path ) const;
211 
213  QString relativePath( const QString &filePath, bool removeRelative ) const;
214 
215  friend class TestQgsFileWidget;
216 };
217 
218 
219 
221 
222 #ifndef SIP_RUN
223 
233 class GUI_EXPORT QgsFileDropEdit: public QgsFilterLineEdit
234 {
235  Q_OBJECT
236 
237  public:
238  QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
239 
240  void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
241 
242  void setFilters( const QString &filters );
243 
244  protected:
245 
246  void dragEnterEvent( QDragEnterEvent *event ) override;
247  void dragLeaveEvent( QDragLeaveEvent *event ) override;
248  void dropEvent( QDropEvent *event ) override;
249  void paintEvent( QPaintEvent *e ) override;
250 
251  private:
252 
254  QString acceptableFilePath( QDropEvent *event ) const;
255 
256  QStringList mAcceptableExtensions;
258  bool mDragActive;
259  friend class TestQgsFileWidget;
260 };
261 
262 #endif
263 
265 #endif // QGSFILEWIDGET_H
QString selectedFilter() const
Returns the selected filter from the last opened file dialog.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
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
Select a single file.
Definition: qgsfilewidget.h:65
#define SIP_END
Definition: qgis_sip.h:182
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:165
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