QGIS API Documentation  3.0.2-Girona (307d082)
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 
39 class QgsPalettedRendererClassGatherer: public QThread
40 {
41  Q_OBJECT
42 
43  public:
44  QgsPalettedRendererClassGatherer( QgsRasterLayer *layer, int bandNumber, const QgsPalettedRasterRenderer::ClassData &existingClasses, QgsColorRamp *ramp = nullptr )
45  : mLayer( layer )
46  , mBandNumber( bandNumber )
47  , mRamp( ramp )
48  , mClasses( existingClasses )
49  , mWasCanceled( false )
50  {}
51 
52  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 
110  void collectedClasses();
111 
112  signals:
114  void canceled();
115 
116  void progressChanged( double progress );
117 
118  private:
119 
120  QgsRasterLayer *mLayer = nullptr;
121  int mBandNumber;
122  std::unique_ptr< QgsColorRamp > mRamp;
123  QString mSubstring;
125  QgsRasterBlockFeedback *mFeedback = nullptr;
126  QMutex mFeedbackMutex;
127  bool mWasCanceled;
128 };
129 
130 class QgsPalettedRendererModel : public QAbstractItemModel
131 {
132  Q_OBJECT
133 
134  public:
135 
136  enum Column
137  {
138  ValueColumn = 0,
139  ColorColumn = 1,
140  LabelColumn = 2,
141  };
142 
143  QgsPalettedRendererModel( QObject *parent = nullptr );
144 
145  void setClassData( const QgsPalettedRasterRenderer::ClassData &data );
146 
147  QgsPalettedRasterRenderer::ClassData classData() const { return mData; }
148 
149  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
150  QModelIndex parent( const QModelIndex &index ) const override;
151  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
152  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
153  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
154  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
155  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
156  Qt::ItemFlags flags( const QModelIndex &index ) const override;
157  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
158  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
159  Qt::DropActions supportedDropActions() const override;
160  QStringList mimeTypes() const override;
161  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
162  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
163 
164  QModelIndex addEntry( const QColor &color );
165 
166  public slots:
167 
168  void deleteAll();
169 
170  signals:
171 
172  void classesChanged();
173 
174  private:
175 
177 
178 
179 };
181 #endif
182 
187 class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, private Ui::QgsPalettedRendererWidgetBase
188 {
189  Q_OBJECT
190 
191  public:
192 
194  ~QgsPalettedRendererWidget() override;
195  static QgsRasterRendererWidget *create( QgsRasterLayer *layer, const QgsRectangle &extent ) SIP_FACTORY { return new QgsPalettedRendererWidget( layer, extent ); }
196 
197  QgsRasterRenderer *renderer() override;
198 
199  void setFromRenderer( const QgsRasterRenderer *r );
200 
201  private:
202 
203  QMenu *mContextMenu = nullptr;
204  QMenu *mAdvancedMenu = nullptr;
205  QAction *mLoadFromLayerAction = nullptr;
206  QgsPalettedRendererModel *mModel = nullptr;
207  QgsColorSwatchDelegate *mSwatchDelegate = nullptr;
208 
210  QgsPalettedRendererClassGatherer *mGatherer = nullptr;
211 
212  int mBand = -1;
213 
214  void setSelectionColor( const QItemSelection &selection, const QColor &color );
215 
216  private slots:
217 
218  void deleteEntry();
219  void addEntry();
220  void changeColor();
221  void changeOpacity();
222  void changeLabel();
223  void applyColorRamp();
224  void loadColorTable();
225  void saveColorTable();
226  void classify();
227  void loadFromLayer();
228  void bandChanged( int band );
229 
230  void gatheredClasses();
231  void gathererThreadFinished();
232  void layerWillBeRemoved( QgsMapLayer *layer );
233 
234 };
235 
236 #endif // QGSPALETTEDRENDERERWIDGET_H
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
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:31
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.