QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
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#ifdef SIP_RUN
42 if ( qobject_cast<QgsFileWidget *>( sipCpp ) )
43 sipType = sipType_QgsFileWidget;
44 else
45 sipType = NULL;
47#endif
48
49 Q_OBJECT
50 Q_PROPERTY( bool fileWidgetButtonVisible READ fileWidgetButtonVisible WRITE setFileWidgetButtonVisible )
51 Q_PROPERTY( bool useLink READ useLink WRITE setUseLink )
52 Q_PROPERTY( bool fullUrl READ fullUrl WRITE setFullUrl )
53 Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
54 Q_PROPERTY( QString filter READ filter WRITE setFilter )
55 Q_PROPERTY( QString defaultRoot READ defaultRoot WRITE setDefaultRoot )
56 Q_PROPERTY( StorageMode storageMode READ storageMode WRITE setStorageMode )
57 Q_PROPERTY( RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage )
58 Q_PROPERTY( QFileDialog::Options options READ options WRITE setOptions )
59
60 public:
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
98 QString filePath();
99
105 static QStringList splitFilePaths( const QString &path );
106
112 void setFilePath( const QString &path );
113
117 virtual void setReadOnly( bool readOnly );
118
124 QString dialogTitle() const;
125
133 void setDialogTitle( const QString &title );
134
136 QString filter() const;
137
142 void setFilter( const QString &filter );
143
151 QFileDialog::Options options() const;
152
163 void setOptions( QFileDialog::Options options );
164
170 void setSelectedFilter( const QString &selectedFilter ) { mSelectedFilter = selectedFilter; }
171
177 QString selectedFilter() const { return mSelectedFilter; }
178
188 void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }
189
195 bool confirmOverwrite() const { return mConfirmOverwrite; }
196
202 bool fileWidgetButtonVisible() const;
203
209 void setFileWidgetButtonVisible( bool visible );
210
216 bool useLink() const;
217
223 void setUseLink( bool useLink );
224
230 bool fullUrl() const;
231
237 void setFullUrl( bool fullUrl );
238
244 QString defaultRoot() const;
245
251 void setDefaultRoot( const QString &defaultRoot );
252
258 QgsFileWidget::StorageMode storageMode() const;
259
265 void setStorageMode( QgsFileWidget::StorageMode storageMode );
266
272 QgsFileWidget::RelativeStorage relativeStorage() const;
273
279 void setRelativeStorage( QgsFileWidget::RelativeStorage relativeStorage );
280
285 QgsFilterLineEdit *lineEdit();
286
287 signals:
288
292 void fileChanged( const QString &path );
293
294 private slots:
295 void openFileDialog();
296 void textEdited( const QString &path );
297 void editLink();
298 void fileDropped( const QString &filePath );
299
300 protected:
304 virtual void updateLayout();
305
309 virtual void setSelectedFileNames( QStringList fileNames );
310
314 static bool isMultiFiles( const QString &path );
315
319 void setFilePaths( const QStringList &filePaths );
320
321 QString mFilePath;
322 bool mButtonVisible = true;
323 bool mUseLink = false;
324 bool mFullUrl = false;
325 bool mReadOnly = false;
326 bool mIsLinkEdited = false;
328 QString mFilter;
331 bool mConfirmOverwrite = true;
332 StorageMode mStorageMode = GetFile;
333 RelativeStorage mRelativeStorage = Absolute;
334 QFileDialog::Options mOptions = QFileDialog::Options();
335
336 QLabel *mLinkLabel = nullptr;
337 QgsFileDropEdit *mLineEdit = nullptr;
338 QToolButton *mLinkEditButton = nullptr;
339 QToolButton *mFileWidgetButton = nullptr;
340 QHBoxLayout *mLayout = nullptr;
341
343 QString toUrl( const QString &path ) const;
344
346 QString relativePath( const QString &filePath, bool removeRelative ) const;
347
348 // QWidget interface
349 public:
350 QSize minimumSizeHint() const override;
351
352 friend class TestQgsFileWidget;
353 friend class TestQgsExternalStorageFileWidget;
354 friend class TestQgsExternalResourceWidgetWrapper;
355};
356
358
359
360#ifndef SIP_RUN
361
371class GUI_EXPORT QgsFileDropEdit : public QgsHighlightableLineEdit
372{
373 Q_OBJECT
374
375 public:
376 QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
377
378 void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
379
380 void setFilters( const QString &filters );
381
383 QStringList acceptableFilePaths( QDropEvent *event ) const;
384
385 signals:
386
390 void fileDropped( const QString &filePath );
391
392 protected:
394 QString acceptableFilePath( QDropEvent *event ) const;
395
396 void dragEnterEvent( QDragEnterEvent *event ) override;
397 void dragLeaveEvent( QDragLeaveEvent *event ) override;
398 void dropEvent( QDropEvent *event ) override;
399
400 private:
401 QStringList mAcceptableExtensions;
403 friend class TestQgsFileWidget;
404};
405
406#endif
408
409#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