QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgspalettedrendererwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspalettedrendererwidget.h
3  ---------------------------
4  begin : February 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
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 
18 #ifndef QGSPALETTEDRENDERERWIDGET_H
19 #define QGSPALETTEDRENDERERWIDGET_H
20 
22 #include "qgis_sip.h"
24 #include "qgscolorschemelist.h"
25 #include "qgsrasterlayer.h"
26 #include "qgsrasterdataprovider.h"
27 #include "ui_qgspalettedrendererwidgetbase.h"
28 #include "qgis_gui.h"
29 
30 #include <QThread>
31 #include <QSortFilterProxyModel>
32 
33 class QgsRasterLayer;
34 class QgsLocaleAwareNumericLineEditDelegate;
35 
36 #ifndef SIP_RUN
38 
43 class QgsPalettedRendererClassGatherer: public QThread
44 {
45  Q_OBJECT
46 
47  public:
48  QgsPalettedRendererClassGatherer( QgsRasterLayer *layer, int bandNumber, const QgsPalettedRasterRenderer::ClassData &existingClasses, QgsColorRamp *ramp = nullptr );
49 
50  void run() override;
51 
53  void stop()
54  {
55  // be cautious, in case gatherer stops naturally just as we are canceling it and mFeedback gets deleted
56  mFeedbackMutex.lock();
57  if ( mFeedback )
58  mFeedback->cancel();
59  mFeedbackMutex.unlock();
60 
61  mWasCanceled = true;
62  }
63 
65  bool wasCanceled() const { return mWasCanceled; }
66 
67  QgsPalettedRasterRenderer::ClassData classes() const { return mClasses; }
68 
69  signals:
70 
74  void collectedClasses();
75 
76  signals:
78  void canceled();
79 
80  void progressChanged( double progress );
81 
82  private:
83 
84  QgsRasterLayer *mLayer = nullptr;
85  int mBandNumber;
86  std::unique_ptr< QgsColorRamp > mRamp;
87  QString mSubstring;
89  QgsRasterBlockFeedback *mFeedback = nullptr;
90  QMutex mFeedbackMutex;
91  bool mWasCanceled;
92 };
93 
94 class QgsPalettedRendererModel : public QAbstractItemModel
95 {
96  Q_OBJECT
97 
98  public:
99 
100  enum Column
101  {
102  ValueColumn = 0,
103  ColorColumn = 1,
104  LabelColumn = 2,
105  };
106 
107  QgsPalettedRendererModel( QObject *parent = nullptr );
108 
109  void setClassData( const QgsPalettedRasterRenderer::ClassData &data );
110 
111  QgsPalettedRasterRenderer::ClassData classData() const { return mData; }
112  QgsPalettedRasterRenderer::Class classAtIndex( const QModelIndex &index ) const { return mData.at( index.row() ); }
113 
114  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
115  QModelIndex parent( const QModelIndex &index ) const override;
116  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
117  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
118  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
119  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
120  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
121  Qt::ItemFlags flags( const QModelIndex &index ) const override;
122  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
123  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
124  Qt::DropActions supportedDropActions() const override;
125  QStringList mimeTypes() const override;
126  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
127  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
128 
129  QModelIndex addEntry( const QColor &color );
130 
131  public slots:
132 
133  void deleteAll();
134 
135  signals:
136 
137  void classesChanged();
138 
139  private:
140 
142 
143 
144 };
145 
146 class QgsPalettedRendererProxyModel: public QSortFilterProxyModel
147 {
148  Q_OBJECT
149 
150  public:
151 
152  QgsPalettedRendererProxyModel( QObject *parent = 0 )
153  : QSortFilterProxyModel( parent )
154  {
155  }
156 
158  QgsPalettedRasterRenderer::ClassData classData() const;
159 
160  protected:
161 
162  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
163  {
164  const QModelIndex lv { left.model()->index( left.row(), static_cast<int>( QgsPalettedRendererModel::Column::ValueColumn ), left.parent() ) };
165  const QModelIndex rv { right.model()->index( right.row(), static_cast<int>( QgsPalettedRendererModel::Column::ValueColumn ), right.parent() ) };
166  const double leftData { sourceModel()->data( lv ).toDouble( ) };
167  const double rightData { sourceModel()->data( rv ).toDouble( ) };
168  return leftData < rightData;
169  }
170 
171 };
172 
174 #endif
175 
180 class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, private Ui::QgsPalettedRendererWidgetBase
181 {
182  Q_OBJECT
183 
184  public:
185 
187  ~QgsPalettedRendererWidget() override;
188  static QgsRasterRendererWidget *create( QgsRasterLayer *layer, const QgsRectangle &extent ) SIP_FACTORY { return new QgsPalettedRendererWidget( layer, extent ); }
189 
191 
195  void setFromRenderer( const QgsRasterRenderer *r );
196 
197  private:
198 
199  QMenu *mContextMenu = nullptr;
200  QMenu *mAdvancedMenu = nullptr;
201  QAction *mLoadFromLayerAction = nullptr;
202  QgsPalettedRendererModel *mModel = nullptr;
203  QgsPalettedRendererProxyModel *mProxyModel = nullptr;
204 
206  QgsPalettedRendererClassGatherer *mGatherer = nullptr;
207 
208  int mBand = -1;
209 
210  QgsLocaleAwareNumericLineEditDelegate *mValueDelegate = nullptr;
211 
212  void setSelectionColor( const QItemSelection &selection, const QColor &color );
213 
214  private slots:
215 
216  void deleteEntry();
217  void addEntry();
218  void changeColor();
219  void changeOpacity();
220  void changeLabel();
221  void applyColorRamp();
222  void loadColorTable();
223  void saveColorTable();
224  void classify();
225  void loadFromLayer();
226  void bandChanged( int band );
227 
228  void gatheredClasses();
229  void gathererThreadFinished();
230  void layerWillBeRemoved( QgsMapLayer *layer );
231 
232 };
233 
234 #endif // QGSPALETTEDRENDERERWIDGET_H
Abstract base class for color ramps.
Definition: qgscolorramp.h:30
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of value to class properties.
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
Feedback object tailored for raster block reading.
Represents a raster layer.
Abstract base class for widgets which configure a QgsRasterRenderer.
virtual QgsRasterRenderer * renderer()=0
Creates a new renderer, using the properties defined in the widget.
Raster renderer pipe that applies colors to a raster.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
#define SIP_FACTORY
Definition: qgis_sip.h:76
Properties of a single value class.