QGIS API Documentation 3.99.0-Master (26c88405ac0)
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:
60
71 Q_ENUM( StorageMode )
72
73
82 Q_ENUM( RelativeStorage )
83
84
87 explicit QgsFileWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
88
98 QString filePath() const;
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
171
177 QString selectedFilter() const { return mSelectedFilter; }
178
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:
301
305 virtual void updateLayout();
306
310 virtual void setSelectedFileNames( QStringList fileNames );
311
315 static bool isMultiFiles( const QString &path );
316
320 void setFilePaths( const QStringList &filePaths );
321
322 QString mFilePath;
323 bool mButtonVisible = true;
324 bool mUseLink = false;
325 bool mFullUrl = false;
326 bool mReadOnly = false;
327 bool mIsLinkEdited = false;
329 QString mFilter;
332 bool mConfirmOverwrite = true;
335 QFileDialog::Options mOptions = QFileDialog::Options();
336
337 QLabel *mLinkLabel = nullptr;
338 QgsFileDropEdit *mLineEdit = nullptr;
339 QToolButton *mLinkEditButton = nullptr;
340 QToolButton *mFileWidgetButton = nullptr;
341 QHBoxLayout *mLayout = nullptr;
342
344 QString toUrl( const QString &path ) const;
345
347 QString relativePath( const QString &filePath, bool removeRelative ) const;
348
349 // QWidget interface
350 public:
351 QSize minimumSizeHint() const override;
352
353 friend class TestQgsFileWidget;
356};
357
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:
395 QString acceptableFilePath( QDropEvent *event ) const;
396
397 void dragEnterEvent( QDragEnterEvent *event ) override;
398 void dragLeaveEvent( QDragLeaveEvent *event ) override;
399 void dropEvent( QDropEvent *event ) override;
400
401 private:
402 QStringList mAcceptableExtensions;
404 friend class TestQgsFileWidget;
405};
406
407#endif
409
410#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