QGIS API Documentation 3.28.14-Firenze (exported)
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
288 QgsFilterLineEdit *lineEdit();
289
290 signals:
291
295 void fileChanged( const QString &path );
296
297 private slots:
298 void openFileDialog();
299 void textEdited( const QString &path );
300 void editLink();
301 void fileDropped( const QString &filePath );
302
303 protected:
304
308 virtual void updateLayout();
309
313 virtual void setSelectedFileNames( QStringList fileNames );
314
318 static bool isMultiFiles( const QString &path );
319
323 void setFilePaths( const QStringList &filePaths );
324
325 QString mFilePath;
326 bool mButtonVisible = true;
327 bool mUseLink = false;
328 bool mFullUrl = false;
329 bool mReadOnly = false;
330 bool mIsLinkEdited = false;
332 QString mFilter;
335 bool mConfirmOverwrite = true;
336 StorageMode mStorageMode = GetFile;
337 RelativeStorage mRelativeStorage = Absolute;
338 QFileDialog::Options mOptions = QFileDialog::Options();
339
340 QLabel *mLinkLabel = nullptr;
341 QgsFileDropEdit *mLineEdit = nullptr;
342 QToolButton *mLinkEditButton = nullptr;
343 QToolButton *mFileWidgetButton = nullptr;
344 QHBoxLayout *mLayout = nullptr;
345
347 QString toUrl( const QString &path ) const;
348
350 QString relativePath( const QString &filePath, bool removeRelative ) const;
351
352 friend class TestQgsFileWidget;
353 friend class TestQgsExternalStorageFileWidget;
354 friend class TestQgsExternalResourceWidgetWrapper;
355};
356
358
359
360
361#ifndef SIP_RUN
362
372class GUI_EXPORT QgsFileDropEdit: public QgsHighlightableLineEdit
373{
374 Q_OBJECT
375
376 public:
377 QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
378
379 void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
380
381 void setFilters( const QString &filters );
382
384 QStringList acceptableFilePaths( QDropEvent *event ) const;
385
386 signals:
387
391 void fileDropped( const QString &filePath );
392
393 protected:
394
396 QString acceptableFilePath( QDropEvent *event ) const;
397
398 void dragEnterEvent( QDragEnterEvent *event ) override;
399 void dragLeaveEvent( QDragLeaveEvent *event ) override;
400 void dropEvent( QDropEvent *event ) override;
401
402 private:
403
404
405 QStringList mAcceptableExtensions;
407 friend class TestQgsFileWidget;
408};
409
410#endif
412
413#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:186
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_END
Definition qgis_sip.h:203