QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "qgis_gui.h"
27#include "qgis_sip.h"
29
30#include <QFileDialog>
31#include <QWidget>
32
37class GUI_EXPORT QgsFileWidget : public QWidget
38{
39#ifdef SIP_RUN
41 if ( qobject_cast<QgsFileWidget *>( sipCpp ) )
42 sipType = sipType_QgsFileWidget;
43 else
44 sipType = NULL;
46#endif
47
48 Q_OBJECT
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 )
57 Q_PROPERTY( QFileDialog::Options options READ options WRITE setOptions )
58
59 public:
70 Q_ENUM( StorageMode )
71
72
81 Q_ENUM( RelativeStorage )
82
83
86 explicit QgsFileWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
87
97 QString filePath() const;
98
104 static QStringList splitFilePaths( const QString &path );
105
111 void setFilePath( const QString &path );
112
116 virtual void setReadOnly( bool readOnly );
117
123 QString dialogTitle() const;
124
132 void setDialogTitle( const QString &title );
133
135 QString filter() const;
136
141 void setFilter( const QString &filter );
142
150 QFileDialog::Options options() const;
151
162 void setOptions( QFileDialog::Options options );
163
170
176 QString selectedFilter() const { return mSelectedFilter; }
177
188
194 bool confirmOverwrite() const { return mConfirmOverwrite; }
195
201 bool fileWidgetButtonVisible() const;
202
208 void setFileWidgetButtonVisible( bool visible );
209
215 bool useLink() const;
216
222 void setUseLink( bool useLink );
223
229 bool fullUrl() const;
230
236 void setFullUrl( bool fullUrl );
237
243 QString defaultRoot() const;
244
250 void setDefaultRoot( const QString &defaultRoot );
251
257 QgsFileWidget::StorageMode storageMode() const;
258
264 void setStorageMode( QgsFileWidget::StorageMode storageMode );
265
271 QgsFileWidget::RelativeStorage relativeStorage() const;
272
278 void setRelativeStorage( QgsFileWidget::RelativeStorage relativeStorage );
279
284 QgsFilterLineEdit *lineEdit();
285
286 signals:
287
291 void fileChanged( const QString &path );
292
293 private slots:
294 void openFileDialog();
295 void textEdited( const QString &path );
296 void editLink();
297 void fileDropped( const QString &filePath );
298
299 protected:
303 virtual void updateLayout();
304
308 virtual void setSelectedFileNames( QStringList fileNames );
309
313 static bool isMultiFiles( const QString &path );
314
318 void setFilePaths( const QStringList &filePaths );
319
320 QString mFilePath;
321 bool mButtonVisible = true;
322 bool mUseLink = false;
323 bool mFullUrl = false;
324 bool mReadOnly = false;
325 bool mIsLinkEdited = false;
327 QString mFilter;
330 bool mConfirmOverwrite = true;
333 QFileDialog::Options mOptions = QFileDialog::Options();
334
335 QLabel *mLinkLabel = nullptr;
336 QgsFileDropEdit *mLineEdit = nullptr;
337 QToolButton *mLinkEditButton = nullptr;
338 QToolButton *mFileWidgetButton = nullptr;
339 QHBoxLayout *mLayout = nullptr;
340
342 QString toUrl( const QString &path ) const;
343
345 QString relativePath( const QString &filePath, bool removeRelative ) const;
346
347 // QWidget interface
348 public:
349 QSize minimumSizeHint() const override;
350
351 friend class TestQgsFileWidget;
354};
355
357
358
359#ifndef SIP_RUN
360
370class GUI_EXPORT QgsFileDropEdit : public QgsHighlightableLineEdit
371{
372 Q_OBJECT
373
374 public:
375 QgsFileDropEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
376
377 void setStorageMode( QgsFileWidget::StorageMode storageMode ) { mStorageMode = storageMode; }
378
379 void setFilters( const QString &filters );
380
382 QStringList acceptableFilePaths( QDropEvent *event ) const;
383
384 signals:
385
389 void fileDropped( const QString &filePath );
390
391 protected:
393 QString acceptableFilePath( QDropEvent *event ) const;
394
395 void dragEnterEvent( QDragEnterEvent *event ) override;
396 void dragLeaveEvent( QDragLeaveEvent *event ) override;
397 void dropEvent( QDropEvent *event ) override;
398
399 private:
400 QStringList mAcceptableExtensions;
402 friend class TestQgsFileWidget;
403};
404
405#endif
407
408#endif // QGSFILEWIDGET_H
A widget for selecting a file or a folder.
QString relativePath(const QString &filePath, bool removeRelative) const
Returns a filePath with relative path options applied (or not) !
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.
StorageMode mStorageMode
friend class TestQgsExternalStorageFileWidget
void setConfirmOverwrite(bool confirmOverwrite)
Sets whether a confirmation to overwrite an existing file will appear.
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Sets whether the relative path is with respect to the project path or the default path.
void setSelectedFilter(const QString &selectedFilter)
Sets the selected filter when the file dialog opens.
void setOptions(QFileDialog::Options options)
Set additional options used for QFileDialog.
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
bool fileWidgetButtonVisible
QFileDialog::Options options
static bool isMultiFiles(const QString &path)
Returns true if path is a multifiles.
void setFullUrl(bool fullUrl)
Sets whether links shown use the full path.
QString dialogTitle
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
QgsFileWidget(QWidget *parent=nullptr)
QgsFileWidget creates a widget for selecting a file or a folder.
void setStorageMode(QgsFileWidget::StorageMode storageMode)
Sets the widget's storage mode (i.e.
void setUseLink(bool useLink)
Sets whether the file path will be shown as a link.
void setFilePaths(const QStringList &filePaths)
Update filePath according to filePaths list.
void setDefaultRoot(const QString &defaultRoot)
Returns the default root path used as the first shown location when picking a file and used if the Re...
QHBoxLayout * mLayout
RelativeStorage mRelativeStorage
QFileDialog::Options mOptions
QString filePath() const
Returns the current file path(s).
QString toUrl(const QString &path) const
returns a HTML code with a link to the given file path
QString mDefaultRoot
void setDialogTitle(const QString &title)
Sets the title to use for the open file dialog.
RelativeStorage relativeStorage
void setFileWidgetButtonVisible(bool visible)
Sets whether the tool button is visible.
virtual void setSelectedFileNames(QStringList fileNames)
Called whenever user select fileNames from dialog.
QgsFileDropEdit * mLineEdit
QToolButton * mLinkEditButton
void setFilter(const QString &filter)
setFilter sets the filter used by the model to filters.
QToolButton * mFileWidgetButton
QLabel * mLinkLabel
StorageMode storageMode
friend class TestQgsExternalResourceWidgetWrapper
QString mDialogTitle
QString defaultRoot
friend class TestQgsFileWidget
QSize minimumSizeHint() const override
virtual void updateLayout()
Update buttons visibility.
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:199
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_END
Definition qgis_sip.h:216