QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgssvgselectorwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssvgselectorwidget.h - group and preview selector for SVG files
3  built off of work in qgssymbollayerwidget
4 
5  ---------------------
6  begin : April 2, 2013
7  copyright : (C) 2013 by Larry Shaffer
8  email : larrys at dakcarto dot com
9  ***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSSVGSELECTORWIDGET_H
18 #define QGSSVGSELECTORWIDGET_H
19 
20 #include "ui_widget_svgselector.h"
21 #include "qgis_sip.h"
22 #include "qgis_gui.h"
23 #include "qgsguiutils.h"
24 #include "qgsproperty.h"
25 
26 #include <QAbstractListModel>
27 #include <QDialog>
28 #include <QDialogButtonBox>
29 #include <QLayout>
30 #include <QStandardItemModel>
31 #include <QWidget>
32 #include <QThread>
33 #include <QElapsedTimer>
34 #include <QStyledItemDelegate>
35 
36 
37 class QCheckBox;
38 class QLayout;
39 class QLineEdit;
40 class QListView;
41 class QPushButton;
42 class QTreeView;
43 
45 
46 
47 #ifndef SIP_RUN
49 
50 
57 class GUI_EXPORT QgsSvgParametersModel : public QAbstractTableModel
58 {
59  Q_OBJECT
60 
61  public:
62  enum class Column : int
63  {
64  NameColumn = 0,
65  ExpressionColumn = 1,
66  };
67 
68  QgsSvgParametersModel( QObject *parent = nullptr );
69 
71  void setParameters( const QMap<QString, QgsProperty> &parameters );
73  QMap<QString, QgsProperty> parameters() const;
74 
76  void removeParameters( const QModelIndexList &indexList );
77 
79  void setLayer( QgsVectorLayer *layer );
81  QgsVectorLayer *layer() const {return mLayer;}
82 
84  void setExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
86  const QgsExpressionContextGenerator *expressionContextGenerator() const {return mExpressionContextGenerator;}
87 
88  int rowCount( const QModelIndex &parent ) const override;
89  int columnCount( const QModelIndex &parent ) const override;
90  QVariant data( const QModelIndex &index, int role ) const override;
91  bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
92  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
93  Qt::ItemFlags flags( const QModelIndex &index ) const override;
94 
95  public slots:
97  void addParameter();
98 
99  signals:
101  void parametersChanged( const QMap<QString, QgsProperty> &parameters );
102 
103  private:
104  struct Parameter
105  {
106  Parameter( const QString &name, const QgsProperty &property )
107  : name( name ), property( property ) {}
108 
109  QString name;
110  QgsProperty property;
111  };
112 
113  QList<Parameter> mParameters;
114  QgsVectorLayer *mLayer = nullptr;
115  const QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
116 };
117 
124 class GUI_EXPORT QgsSvgParameterValueDelegate : public QStyledItemDelegate
125 {
126  Q_OBJECT
127 
128  public:
129  QgsSvgParameterValueDelegate( QObject *parent = nullptr )
130  : QStyledItemDelegate( parent )
131  {}
132 
133  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
134  void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
135  void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
136  void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
137 };
138 
139 
140 
147 class GUI_EXPORT QgsSvgSelectorLoader : public QThread
148 {
149  Q_OBJECT
150 
151  public:
152 
157  QgsSvgSelectorLoader( QObject *parent = nullptr );
158 
159  ~QgsSvgSelectorLoader() override;
160 
166  void run() override;
167 
172  virtual void stop();
173 
178  void setPath( const QString &path )
179  {
180  mPath = path;
181  }
182 
183  signals:
184 
190  void foundSvgs( QStringList svgs );
191 
192  private:
193 
194  QString mPath;
195  bool mCanceled = false;
196  QStringList mQueuedSvgs;
197 
198  QElapsedTimer mTimer;
199  int mTimerThreshold = 0;
200  QSet< QString > mTraversedPaths;
201 
202  void loadPath( const QString &path );
203  void loadImages( const QString &path );
204 
205 };
206 
213 class GUI_EXPORT QgsSvgGroupLoader : public QThread
214 {
215  Q_OBJECT
216 
217  public:
218 
223  QgsSvgGroupLoader( QObject *parent = nullptr );
224 
225  ~QgsSvgGroupLoader() override;
226 
231  void run() override;
232 
237  virtual void stop();
238 
243  void setParentPaths( const QStringList &parentPaths )
244  {
245  mParentPaths = parentPaths;
246  }
247 
248  signals:
249 
255  void foundPath( const QString &parentPath, const QString &path );
256 
257  private:
258 
259  QStringList mParentPaths;
260  bool mCanceled = false;
261  QSet< QString > mTraversedPaths;
262 
263  void loadGroup( const QString &parentPath );
264 
265 };
266 
268 #endif
269 
277 class GUI_EXPORT QgsSvgSelectorListModel : public QAbstractListModel
278 {
279  Q_OBJECT
280 
281  public:
282 
289  QgsSvgSelectorListModel( QObject *parent SIP_TRANSFERTHIS, int iconSize = 30 );
290 
297  QgsSvgSelectorListModel( QObject *parent SIP_TRANSFERTHIS, const QString &path, int iconSize = 30 );
298 
299  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
300  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
301 
302  protected:
303  QStringList mSvgFiles;
304 
305  private:
306  QPixmap createPreview( const QString &entry ) const;
307  QgsSvgSelectorLoader *mSvgLoader = nullptr;
308 
309  int mIconSize = 30;
310 
311  private slots:
312 
317  void addSvgs( const QStringList &svgs );
318 
319 };
320 
321 
329 class GUI_EXPORT QgsSvgSelectorGroupsModel : public QStandardItemModel
330 {
331  Q_OBJECT
332 
333  public:
334  QgsSvgSelectorGroupsModel( QObject *parent SIP_TRANSFERTHIS );
335  ~QgsSvgSelectorGroupsModel() override;
336 
337  private:
338  QgsSvgGroupLoader *mLoader = nullptr;
339  QHash< QString, QStandardItem * > mPathItemHash;
340 
341  private slots:
342 
343  void addPath( const QString &parentPath, const QString &path );
344 };
345 
350 class GUI_EXPORT QgsSvgSelectorWidget : public QWidget, private Ui::WidgetSvgSelector
351 {
352  Q_OBJECT
353 
354  public:
355 
357  QgsSvgSelectorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
358 
363  void initParametersModel( const QgsExpressionContextGenerator *generator, QgsVectorLayer *layer );
364 
365  QString currentSvgPath() const;
366 
371  QgsSvgSourceLineEdit *sourceLineEdit() const {return mSvgSourceLineEdit;}
372 
377  void setAllowParameters( bool allow );
378 
383  bool allowParamerters() const {return mAllowParameters;}
384 
385  public slots:
387  void setSvgPath( const QString &svgPath );
388 
393  void setSvgParameters( const QMap<QString, QgsProperty> &parameters );
394 
395  signals:
396  void svgSelected( const QString &path );
397 
402  void svgParametersChanged( const QMap<QString, QgsProperty> &parameters );
403 
404  protected:
405  void populateList();
406 
407  private slots:
408  void populateIcons( const QModelIndex &idx );
409  void svgSelectionChanged( const QModelIndex &idx );
410  void updateCurrentSvgPath( const QString &svgPath );
411  void svgSourceChanged( const QString &text );
412 
413  private:
414  int mIconSize = 30;
415  QString mCurrentSvgPath;
416  bool mAllowParameters = false;
417  QgsSvgParametersModel *mParametersModel = nullptr;
418 };
419 
424 class GUI_EXPORT QgsSvgSelectorDialog : public QDialog
425 {
426  Q_OBJECT
427  public:
428 
432  QgsSvgSelectorDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr,
433  Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags,
434  QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Close | QDialogButtonBox::Ok,
435  Qt::Orientation orientation = Qt::Horizontal );
436 
438  QgsSvgSelectorWidget *svgSelector() { return mSvgSelector; }
439 
440  protected:
441  QVBoxLayout *mLayout = nullptr;
442  QDialogButtonBox *mButtonBox = nullptr;
443  QgsSvgSelectorWidget *mSvgSelector = nullptr;
444 };
445 
446 #endif // QGSSVGSELECTORWIDGET_H
Abstract interface for generating an expression context.
A store for object properties.
Definition: qgsproperty.h:232
QgsSvgSelectorWidget * svgSelector()
Returns pointer to the embedded SVG selector widget.
A model for displaying SVG search paths.
A model for displaying SVG files with a preview icon.
void svgParametersChanged(const QMap< QString, QgsProperty > &parameters)
Emitted when the parameters have changed.
void svgSelected(const QString &path)
bool allowParamerters() const
Returns if the group box to fill parameters is visible.
QgsSvgSourceLineEdit * sourceLineEdit() const
Returns the source line edit.
A line edit widget with toolbutton for setting an SVG image path.
Represents a vector layer which manages a vector based data sets.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53