QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 : denis.rouzaud@gmail.com
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
20class QLabel;
21class QToolButton;
22class QVariant;
23class QHBoxLayout;
24class QgsFileDropEdit;
25
26#include <QWidget>
27#include <QFileDialog>
28
29#include "qgis_gui.h"
30#include "qgis_sip.h"
32
33
38class GUI_EXPORT QgsFileWidget : public QWidget
39{
40
41#ifdef SIP_RUN
43 if ( qobject_cast<QgsFileWidget *>( sipCpp ) )
44 sipType = sipType_QgsFileWidget;
45 else
46 sipType = NULL;
48#endif
49
50 Q_OBJECT
51 Q_PROPERTY( bool fileWidgetButtonVisible READ fileWidgetButtonVisible WRITE setFileWidgetButtonVisible )
52 Q_PROPERTY( bool useLink READ useLink WRITE setUseLink )
53 Q_PROPERTY( bool fullUrl READ fullUrl WRITE setFullUrl )
54 Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
55 Q_PROPERTY( QString filter READ filter WRITE setFilter )
56 Q_PROPERTY( QString defaultRoot READ defaultRoot WRITE setDefaultRoot )
57 Q_PROPERTY( StorageMode storageMode READ storageMode WRITE setStorageMode )
58 Q_PROPERTY( RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage )
59 Q_PROPERTY( QFileDialog::Options options READ options WRITE setOptions )
60
61 public:
62
73 Q_ENUM( StorageMode )
74
75
79 {
82 RelativeDefaultPath
83 };
84 Q_ENUM( RelativeStorage )
85
86
89 explicit QgsFileWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
90
100 QString filePath();
101
107 static QStringList splitFilePaths( const QString &path );
108
114 void setFilePath( const QString &path );
115
119 virtual void setReadOnly( bool readOnly );
120
126 QString dialogTitle() const;
127
135 void setDialogTitle( const QString &title );
136
138 QString filter() const;
139
144 void setFilter( const QString &filter );
145
153 QFileDialog::Options options() const;
154
165 void setOptions( QFileDialog::Options options );
166
172 void setSelectedFilter( const QString &selectedFilter ) { mSelectedFilter = selectedFilter; }
173
179 QString selectedFilter() const { return mSelectedFilter; }
180
190 void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }
191
197 bool confirmOverwrite() const { return mConfirmOverwrite; }
198
204 bool fileWidgetButtonVisible() const;
205
211 void setFileWidgetButtonVisible( bool visible );
212
218 bool useLink() const;
219
225 void setUseLink( bool useLink );
226
232 bool fullUrl() const;
233
239 void setFullUrl( bool fullUrl );
240
246 QString defaultRoot() const;
247
253 void setDefaultRoot( const QString &defaultRoot );
254
260 QgsFileWidget::StorageMode storageMode() const;
261
267 void setStorageMode( QgsFileWidget::StorageMode storageMode );
268
274 QgsFileWidget::RelativeStorage relativeStorage() const;
275
281 void setRelativeStorage( QgsFileWidget::RelativeStorage relativeStorage );
282
287 QgsFilterLineEdit *lineEdit();
288
289 signals:
290
294 void fileChanged( const QString &path );
295
296 private slots:
297 void openFileDialog();
298 void textEdited( const QString &path );
299 void editLink();
300 void fileDropped( const QString &filePath );
301
302 protected:
303
307 virtual void updateLayout();
308
312 virtual void setSelectedFileNames( QStringList fileNames );
313
317 static bool isMultiFiles( const QString &path );
318
322 void setFilePaths( const QStringList &filePaths );
323
324 QString mFilePath;
325 bool mButtonVisible = true;
326 bool mUseLink = false;
327 bool mFullUrl = false;
328 bool mReadOnly = false;
329 bool mIsLinkEdited = false;
331 QString mFilter;
334 bool mConfirmOverwrite = true;
335 StorageMode mStorageMode = GetFile;
336 RelativeStorage mRelativeStorage = Absolute;
337 QFileDialog::Options mOptions = QFileDialog::Options();
338
339 QLabel *mLinkLabel = nullptr;
340 QgsFileDropEdit *mLineEdit = nullptr;
341 QToolButton *mLinkEditButton = nullptr;
342 QToolButton *mFileWidgetButton = nullptr;
343 QHBoxLayout *mLayout = nullptr;
344
346 QString toUrl( const QString &path ) const;
347
349 QString relativePath( const QString &filePath, bool removeRelative ) const;
350
351 // QWidget interface
352 public:
353 QSize minimumSizeHint() const override;
354
355 friend class TestQgsFileWidget;
356 friend class TestQgsExternalStorageFileWidget;
357 friend class TestQgsExternalResourceWidgetWrapper;
358};
359
361
362
363
364#ifndef SIP_RUN
365
375class GUI_EXPORT QgsFileDropEdit: public QgsHighlightableLineEdit
376{
377 Q_OBJECT
378
379 public:
380 QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
381
382 void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
383
384 void setFilters( const QString &filters );
385
387 QStringList acceptableFilePaths( QDropEvent *event ) const;
388
389 signals:
390
394 void fileDropped( const QString &filePath );
395
396 protected:
397
399 QString acceptableFilePath( QDropEvent *event ) const;
400
401 void dragEnterEvent( QDragEnterEvent *event ) override;
402 void dragLeaveEvent( QDragLeaveEvent *event ) override;
403 void dropEvent( QDropEvent *event ) override;
404
405 private:
406
407
408 QStringList mAcceptableExtensions;
410 friend class TestQgsFileWidget;
411};
412
413#endif
415
416#endif // QGSFILEWIDGET_H
The QgsFileWidget class creates a widget for selecting a file or a folder.
StorageMode
The StorageMode enum determines if the file picker should pick files or directories.
@ GetMultipleFiles
Select multiple files.
@ GetFile
Select a single file.
@ GetDirectory
Select a directory.
@ SaveFile
Select a single new or pre-existing file.
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.
QString mSelectedFilter
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
QString mDefaultRoot
QString mDialogTitle
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:191
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_END
Definition qgis_sip.h:208