QGIS API Documentation  2.99.0-Master (dcec6bb)
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 class QgsRasterLayer;
31 
32 #ifndef SIP_RUN
33 
38 class QgsPalettedRendererClassGatherer: public QThread
39 {
40  Q_OBJECT
41 
42  public:
43  QgsPalettedRendererClassGatherer( QgsRasterLayer *layer, int bandNumber, const QgsPalettedRasterRenderer::ClassData &existingClasses, QgsColorRamp *ramp = nullptr )
44  : mLayer( layer )
45  , mBandNumber( bandNumber )
46  , mRamp( ramp )
47  , mClasses( existingClasses )
48  , mFeedback( nullptr )
49  , mWasCanceled( false )
50  {}
51 
52  virtual void run() override
53  {
54  mWasCanceled = false;
55 
56  // allow responsive cancelation
57  mFeedback = new QgsRasterBlockFeedback();
58  connect( mFeedback, &QgsRasterBlockFeedback::progressChanged, this, &QgsPalettedRendererClassGatherer::progressChanged );
59 
60  QgsPalettedRasterRenderer::ClassData newClasses = QgsPalettedRasterRenderer::classDataFromRaster( mLayer->dataProvider(), mBandNumber, mRamp.get(), mFeedback );
61 
62  // combine existing classes with new classes
63  QgsPalettedRasterRenderer::ClassData::iterator classIt = newClasses.begin();
64  for ( ; classIt != newClasses.end(); ++classIt )
65  {
66  // check if existing classes contains this same class
67  Q_FOREACH ( const QgsPalettedRasterRenderer::Class &existingClass, mClasses )
68  {
69  if ( existingClass.value == classIt->value )
70  {
71  classIt->color = existingClass.color;
72  classIt->label = existingClass.label;
73  break;
74  }
75  }
76  }
77  mClasses = newClasses;
78 
79  // be overly cautious - it's *possible* stop() might be called between deleting mFeedback and nulling it
80  mFeedbackMutex.lock();
81  delete mFeedback;
82  mFeedback = nullptr;
83  mFeedbackMutex.unlock();
84 
85  emit collectedClasses();
86  }
87 
89  void stop()
90  {
91  // be cautious, in case gatherer stops naturally just as we are canceling it and mFeedback gets deleted
92  mFeedbackMutex.lock();
93  if ( mFeedback )
94  mFeedback->cancel();
95  mFeedbackMutex.unlock();
96 
97  mWasCanceled = true;
98  }
99 
101  bool wasCanceled() const { return mWasCanceled; }
102 
103  QgsPalettedRasterRenderer::ClassData classes() const { return mClasses; }
104 
105  signals:
106 
109  void collectedClasses();
110 
111  signals:
113  void canceled();
114 
115  void progressChanged( double progress );
116 
117  private:
118 
119  QgsRasterLayer *mLayer = nullptr;
120  int mBandNumber;
121  std::unique_ptr< QgsColorRamp > mRamp;
122  QString mSubstring;
124  QgsRasterBlockFeedback *mFeedback = nullptr;
125  QMutex mFeedbackMutex;
126  bool mWasCanceled;
127 };
128 
129 class QgsPalettedRendererModel : public QAbstractItemModel
130 {
131  Q_OBJECT
132 
133  public:
134 
135  enum Column
136  {
137  ValueColumn = 0,
138  ColorColumn = 1,
139  LabelColumn = 2,
140  };
141 
142  QgsPalettedRendererModel( QObject *parent = nullptr );
143 
144  void setClassData( const QgsPalettedRasterRenderer::ClassData &data );
145 
146  QgsPalettedRasterRenderer::ClassData classData() const { return mData; }
147 
148  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
149  QModelIndex parent( const QModelIndex &index ) const override;
150  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
151  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
152  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
153  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
154  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
155  Qt::ItemFlags flags( const QModelIndex &index ) const override;
156  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
157  virtual bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
158  Qt::DropActions supportedDropActions() const override;
159  QStringList mimeTypes() const override;
160  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
161  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
162 
163  QModelIndex addEntry( const QColor &color );
164 
165  public slots:
166 
167  void deleteAll();
168 
169  signals:
170 
171  void classesChanged();
172 
173  private:
174 
176 
177 
178 };
180 #endif
181 
185 class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, private Ui::QgsPalettedRendererWidgetBase
186 {
187  Q_OBJECT
188 
189  public:
190 
193  static QgsRasterRendererWidget *create( QgsRasterLayer *layer, const QgsRectangle &extent ) SIP_FACTORY { return new QgsPalettedRendererWidget( layer, extent ); }
194 
195  QgsRasterRenderer *renderer() override;
196 
197  void setFromRenderer( const QgsRasterRenderer *r );
198 
199  private:
200 
201  QMenu *mContextMenu = nullptr;
202  QMenu *mAdvancedMenu = nullptr;
203  QAction *mLoadFromLayerAction = nullptr;
204  QgsPalettedRendererModel *mModel = nullptr;
205  QgsColorSwatchDelegate *mSwatchDelegate = nullptr;
206 
208  QgsPalettedRendererClassGatherer *mGatherer = nullptr;
209 
210  int mBand = -1;
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
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:54
QColor color
Color to render value.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
Abstract base class for color ramps.
Definition: qgscolorramp.h:30
Properties of a single value class.
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of value to class properties.
#define SIP_FACTORY
Definition: qgis_sip.h:69
static QgsPalettedRasterRenderer::ClassData classDataFromRaster(QgsRasterInterface *raster, int bandNumber, QgsColorRamp *ramp=nullptr, QgsRasterBlockFeedback *feedback=nullptr)
Generates class data from a raster, for the specified bandNumber.
Feedback object tailored for raster block reading.
Raster renderer pipe that applies colors to a raster.
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
A delegate for showing a color swatch in a list.