QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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.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 
193  void blockEvents( bool ) SIP_SKIP;
194 
195  private slots:
196  void openFileDialog();
197  void textEdited( const QString &path );
198 
199  private:
200  QString mFilePath;
201  bool mButtonVisible = true;
202  bool mUseLink = false;
203  bool mFullUrl = false;
204  QString mDialogTitle;
205  QString mFilter;
206  QString mSelectedFilter;
207  QString mDefaultRoot;
208  bool mConfirmOverwrite = true;
209  StorageMode mStorageMode = GetFile;
210  RelativeStorage mRelativeStorage = Absolute;
211 
212  QLabel *mLinkLabel = nullptr;
213  QgsFileDropEdit *mLineEdit = nullptr;
214  QToolButton *mFileWidgetButton = nullptr;
215  QHBoxLayout *mLayout = nullptr;
216 
218  QString toUrl( const QString &path ) const;
219 
221  QString relativePath( const QString &filePath, bool removeRelative ) const;
222 
223  friend class TestQgsFileWidget;
224 };
225 
226 
227 
229 
230 #ifndef SIP_RUN
231 
241 class GUI_EXPORT QgsFileDropEdit: public QgsFilterLineEdit
242 {
243  Q_OBJECT
244 
245  public:
246  QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
247 
248  void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
249 
250  void setFilters( const QString &filters );
251 
252  protected:
253 
254  void dragEnterEvent( QDragEnterEvent *event ) override;
255  void dragLeaveEvent( QDragLeaveEvent *event ) override;
256  void dropEvent( QDropEvent *event ) override;
257  void paintEvent( QPaintEvent *e ) override;
258 
259  private:
260 
262  QString acceptableFilePath( QDropEvent *event ) const;
263 
264  QStringList mAcceptableExtensions;
266  bool mDragActive;
267  friend class TestQgsFileWidget;
268 };
269 
270 #endif
271 
273 #endif // QGSFILEWIDGET_H
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
bool confirmOverwrite() const
Returns whether a confirmation will be shown when overwriting an existing file.
Select a single file.
Definition: qgsfilewidget.h:66
QString selectedFilter() const
Returns the selected filter from the last opened file dialog.
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:35
#define SIP_SKIP
Definition: qgis_sip.h:119
#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: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