QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include <QFileDialog>
27 
28 #include "qgis_gui.h"
29 #include "qgis_sip.h"
31 
36 class GUI_EXPORT QgsFileWidget : public QWidget
37 {
38 
39 #ifdef SIP_RUN
41  if ( qobject_cast<QgsFileWidget *>( sipCpp ) )
42  sipType = sipType_QgsFileWidget;
43  else
44  sipType = NULL;
45  SIP_END
46 #endif
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  Q_PROPERTY( QFileDialog::Options options READ options WRITE setOptions )
58 
59  public:
60 
65  {
70  };
71  Q_ENUM( StorageMode )
72 
73 
77  {
80  RelativeDefaultPath
81  };
82  Q_ENUM( RelativeStorage )
83 
84 
87  explicit QgsFileWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
88 
95  QString filePath();
96 
101  static QStringList splitFilePaths( const QString &path );
102 
104  void setFilePath( QString path );
105 
107  void setReadOnly( bool readOnly );
108 
110  QString dialogTitle() const;
111 
116  void setDialogTitle( const QString &title );
117 
119  QString filter() const;
120 
125  void setFilter( const QString &filter );
126 
131  QFileDialog::Options options() const;
132 
137  void setOptions( QFileDialog::Options options );
138 
142  void setSelectedFilter( const QString &selectedFilter ) { mSelectedFilter = selectedFilter; }
143 
147  QString selectedFilter() const { return mSelectedFilter; }
148 
154  void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }
155 
159  bool confirmOverwrite() const { return mConfirmOverwrite; }
160 
162  bool fileWidgetButtonVisible() const;
164  void setFileWidgetButtonVisible( bool visible );
165 
167  bool useLink() const;
169  void setUseLink( bool useLink );
170 
172  bool fullUrl() const;
174  void setFullUrl( bool fullUrl );
175 
177  QString defaultRoot() const;
179  void setDefaultRoot( const QString &defaultRoot );
180 
182  QgsFileWidget::StorageMode storageMode() const;
184  void setStorageMode( QgsFileWidget::StorageMode storageMode );
185 
187  QgsFileWidget::RelativeStorage relativeStorage() const;
189  void setRelativeStorage( QgsFileWidget::RelativeStorage relativeStorage );
190 
196  QgsFilterLineEdit *lineEdit();
197 
198  signals:
199 
203  void fileChanged( const QString &path );
204 
205  private slots:
206  void openFileDialog();
207  void textEdited( const QString &path );
208  void editLink();
209 
210  private:
211  void updateLayout();
212 
213  QString mFilePath;
214  bool mButtonVisible = true;
215  bool mUseLink = false;
216  bool mFullUrl = false;
217  bool mReadOnly = false;
218  bool mIsLinkEdited = false;
219  QString mDialogTitle;
220  QString mFilter;
221  QString mSelectedFilter;
222  QString mDefaultRoot;
223  bool mConfirmOverwrite = true;
224  StorageMode mStorageMode = GetFile;
225  RelativeStorage mRelativeStorage = Absolute;
226  QFileDialog::Options mOptions = QFileDialog::Options();
227 
228  QLabel *mLinkLabel = nullptr;
229  QgsFileDropEdit *mLineEdit = nullptr;
230  QToolButton *mLinkEditButton = nullptr;
231  QToolButton *mFileWidgetButton = nullptr;
232  QHBoxLayout *mLayout = nullptr;
233 
235  QString toUrl( const QString &path ) const;
236 
238  QString relativePath( const QString &filePath, bool removeRelative ) const;
239 
240  friend class TestQgsFileWidget;
241 };
242 
244 
245 
246 
247 #ifndef SIP_RUN
248 
258 class GUI_EXPORT QgsFileDropEdit: public QgsHighlightableLineEdit
259 {
260  Q_OBJECT
261 
262  public:
263  QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
264 
265  void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
266 
267  void setFilters( const QString &filters );
268 
269  protected:
270 
271  void dragEnterEvent( QDragEnterEvent *event ) override;
272  void dragLeaveEvent( QDragLeaveEvent *event ) override;
273  void dropEvent( QDropEvent *event ) override;
274 
275  private:
276 
278  QString acceptableFilePath( QDropEvent *event ) const;
279 
280  QStringList mAcceptableExtensions;
282  friend class TestQgsFileWidget;
283 };
284 
285 #endif
287 
288 #endif // QGSFILEWIDGET_H
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:37
StorageMode
The StorageMode enum determines if the file picker should pick files or directories.
Definition: qgsfilewidget.h:65
@ GetMultipleFiles
Select multiple files.
Definition: qgsfilewidget.h:68
@ GetFile
Select a single file.
Definition: qgsfilewidget.h:66
@ GetDirectory
Select a directory.
Definition: qgsfilewidget.h:67
@ SaveFile
Select a single new or pre-existing file.
Definition: qgsfilewidget.h:69
void setConfirmOverwrite(bool confirmOverwrite)
Sets whether a confirmation to overwrite an existing file will appear.
void setSelectedFilter(const QString &selectedFilter)
Sets the selected filter when the file dialog opens.
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
QString selectedFilter() const
Returns the selected filter from the last opened file dialog.
bool confirmOverwrite() const
Returns whether a confirmation will be shown when overwriting an existing file.
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:77
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
A QgsFilterLineEdit subclass with the ability to "highlight" the edges of the widget.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_END
Definition: qgis_sip.h:194