QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  : mLayer( layer )
50  , mBandNumber( bandNumber )
51  , mRamp( ramp )
52  , mClasses( existingClasses )
53  , mWasCanceled( false )
54  {}
55 
56  void run() override;
57 
59  void stop()
60  {
61  // be cautious, in case gatherer stops naturally just as we are canceling it and mFeedback gets deleted
62  mFeedbackMutex.lock();
63  if ( mFeedback )
64  mFeedback->cancel();
65  mFeedbackMutex.unlock();
66 
67  mWasCanceled = true;
68  }
69 
71  bool wasCanceled() const { return mWasCanceled; }
72 
73  QgsPalettedRasterRenderer::ClassData classes() const { return mClasses; }
74 
75  signals:
76 
80  void collectedClasses();
81 
82  signals:
84  void canceled();
85 
86  void progressChanged( double progress );
87 
88  private:
89 
90  QgsRasterLayer *mLayer = nullptr;
91  int mBandNumber;
92  std::unique_ptr< QgsColorRamp > mRamp;
93  QString mSubstring;
95  QgsRasterBlockFeedback *mFeedback = nullptr;
96  QMutex mFeedbackMutex;
97  bool mWasCanceled;
98 };
99 
100 class QgsPalettedRendererModel : public QAbstractItemModel
101 {
102  Q_OBJECT
103 
104  public:
105 
106  enum Column
107  {
108  ValueColumn = 0,
109  ColorColumn = 1,
110  LabelColumn = 2,
111  };
112 
113  QgsPalettedRendererModel( QObject *parent = nullptr );
114 
115  void setClassData( const QgsPalettedRasterRenderer::ClassData &data );
116 
117  QgsPalettedRasterRenderer::ClassData classData() const { return mData; }
118  QgsPalettedRasterRenderer::Class classAtIndex( const QModelIndex &index ) const { return mData.at( index.row() ); }
119 
120  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
121  QModelIndex parent( const QModelIndex &index ) const override;
122  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
123  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
124  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
125  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
126  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
127  Qt::ItemFlags flags( const QModelIndex &index ) const override;
128  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
129  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
130  Qt::DropActions supportedDropActions() const override;
131  QStringList mimeTypes() const override;
132  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
133  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
134 
135  QModelIndex addEntry( const QColor &color );
136 
137  public slots:
138 
139  void deleteAll();
140 
141  signals:
142 
143  void classesChanged();
144 
145  private:
146 
148 
149 
150 };
151 
152 class QgsPalettedRendererProxyModel: public QSortFilterProxyModel
153 {
154  Q_OBJECT
155 
156  public:
157 
158  QgsPalettedRendererProxyModel( QObject *parent = 0 )
159  : QSortFilterProxyModel( parent )
160  {
161  }
162 
164  QgsPalettedRasterRenderer::ClassData classData() const;
165 
166  protected:
167 
168  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
169  {
170  const QModelIndex lv { left.model()->index( left.row(), static_cast<int>( QgsPalettedRendererModel::Column::ValueColumn ), left.parent() ) };
171  const QModelIndex rv { right.model()->index( right.row(), static_cast<int>( QgsPalettedRendererModel::Column::ValueColumn ), right.parent() ) };
172  const double leftData { sourceModel()->data( lv ).toDouble( ) };
173  const double rightData { sourceModel()->data( rv ).toDouble( ) };
174  return leftData < rightData;
175  }
176 
177 };
178 
180 #endif
181 
186 class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, private Ui::QgsPalettedRendererWidgetBase
187 {
188  Q_OBJECT
189 
190  public:
191 
193  ~QgsPalettedRendererWidget() override;
194  static QgsRasterRendererWidget *create( QgsRasterLayer *layer, const QgsRectangle &extent ) SIP_FACTORY { return new QgsPalettedRendererWidget( layer, extent ); }
195 
196  QgsRasterRenderer *renderer() override;
197 
198  void setFromRenderer( const QgsRasterRenderer *r );
199 
200  private:
201 
202  QMenu *mContextMenu = nullptr;
203  QMenu *mAdvancedMenu = nullptr;
204  QAction *mLoadFromLayerAction = nullptr;
205  QgsPalettedRendererModel *mModel = nullptr;
206  QgsPalettedRendererProxyModel *mProxyModel = nullptr;
207 
209  QgsPalettedRendererClassGatherer *mGatherer = nullptr;
210 
211  int mBand = -1;
212 
213  QgsLocaleAwareNumericLineEditDelegate *mValueDelegate = nullptr;
214 
215  void setSelectionColor( const QItemSelection &selection, const QColor &color );
216 
217  private slots:
218 
219  void deleteEntry();
220  void addEntry();
221  void changeColor();
222  void changeOpacity();
223  void changeLabel();
224  void applyColorRamp();
225  void loadColorTable();
226  void saveColorTable();
227  void classify();
228  void loadFromLayer();
229  void bandChanged( int band );
230 
231  void gatheredClasses();
232  void gathererThreadFinished();
233  void layerWillBeRemoved( QgsMapLayer *layer );
234 
235 };
236 
237 #endif // QGSPALETTEDRENDERERWIDGET_H
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
Base class for all map layer types.
Definition: qgsmaplayer.h:70
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.
virtual QgsRasterRenderer * renderer()=0
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.