QGIS API Documentation  3.13.0-Master (740be229cb)
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"
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  Q_ENUM( StorageMode )
71 
72 
76  {
79  RelativeDefaultPath
80  };
81  Q_ENUM( RelativeStorage )
82 
83 
86  explicit QgsFileWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
87 
94  QString filePath();
95 
100  static QStringList splitFilePaths( const QString &path );
101 
103  void setFilePath( QString path );
104 
106  void setReadOnly( bool readOnly );
107 
109  QString dialogTitle() const;
110 
115  void setDialogTitle( const QString &title );
116 
118  QString filter() const;
119 
124  void setFilter( const QString &filter );
125 
129  void setSelectedFilter( const QString &selectedFilter ) { mSelectedFilter = selectedFilter; }
130 
134  QString selectedFilter() const { return mSelectedFilter; }
135 
141  void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }
142 
146  bool confirmOverwrite() const { return mConfirmOverwrite; }
147 
149  bool fileWidgetButtonVisible() const;
151  void setFileWidgetButtonVisible( bool visible );
152 
154  bool useLink() const;
156  void setUseLink( bool useLink );
157 
159  bool fullUrl() const;
161  void setFullUrl( bool fullUrl );
162 
164  QString defaultRoot() const;
166  void setDefaultRoot( const QString &defaultRoot );
167 
169  QgsFileWidget::StorageMode storageMode() const;
171  void setStorageMode( QgsFileWidget::StorageMode storageMode );
172 
174  QgsFileWidget::RelativeStorage relativeStorage() const;
176  void setRelativeStorage( QgsFileWidget::RelativeStorage relativeStorage );
177 
183  QgsFilterLineEdit *lineEdit();
184 
185  signals:
186 
190  void fileChanged( const QString &path );
191 
198  void blockEvents( bool ) SIP_SKIP;
199 
200  private slots:
201  void openFileDialog();
202  void textEdited( const QString &path );
203 
204  private:
205  QString mFilePath;
206  bool mButtonVisible = true;
207  bool mUseLink = false;
208  bool mFullUrl = false;
209  QString mDialogTitle;
210  QString mFilter;
211  QString mSelectedFilter;
212  QString mDefaultRoot;
213  bool mConfirmOverwrite = true;
214  StorageMode mStorageMode = GetFile;
215  RelativeStorage mRelativeStorage = Absolute;
216 
217  QLabel *mLinkLabel = nullptr;
218  QgsFileDropEdit *mLineEdit = nullptr;
219  QToolButton *mFileWidgetButton = nullptr;
220  QHBoxLayout *mLayout = nullptr;
221 
223  QString toUrl( const QString &path ) const;
224 
226  QString relativePath( const QString &filePath, bool removeRelative ) const;
227 
228  friend class TestQgsFileWidget;
229 };
230 
231 
232 
234 
235 #ifndef SIP_RUN
236 
246 class GUI_EXPORT QgsFileDropEdit: public QgsHighlightableLineEdit
247 {
248  Q_OBJECT
249 
250  public:
251  QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
252 
253  void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
254 
255  void setFilters( const QString &filters );
256 
257  protected:
258 
259  void dragEnterEvent( QDragEnterEvent *event ) override;
260  void dragLeaveEvent( QDragLeaveEvent *event ) override;
261  void dropEvent( QDropEvent *event ) override;
262 
263  private:
264 
266  QString acceptableFilePath( QDropEvent *event ) const;
267 
268  QStringList mAcceptableExtensions;
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...
A QgsFilterLineEdit subclass with the ability to "highlight" the edges of the widget.
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:75