QGIS API Documentation  2.99.0-Master (6a61179)
qgspalettedrendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspalettedrendererwidget.cpp
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 
20 #include "qgsrasterdataprovider.h"
21 #include "qgsrasterlayer.h"
22 #include "qgscolordialog.h"
23 #include <QColorDialog>
24 
26 {
27  setupUi( this );
28 
29  if ( mRasterLayer )
30  {
32  if ( !provider )
33  {
34  return;
35  }
36 
37  //fill available bands into combo box
38  int nBands = provider->bandCount();
39  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
40  {
41  mBandComboBox->addItem( displayBandName( i ), i );
42  }
43 
45  connect( mBandComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
46  }
47 }
48 
50 {
51 
52 }
53 
55 {
56  int nColors = mTreeWidget->topLevelItemCount();
57  QColor* colorArray = new QColor[nColors];
58  QVector<QString> labels;
59  for ( int i = 0; i < nColors; ++i )
60  {
61  colorArray[i] = mTreeWidget->topLevelItem( i )->background( 1 ).color();
62  QString label = mTreeWidget->topLevelItem( i )->text( 2 );
63  if ( !label.isEmpty() )
64  {
65  if ( i >= labels.size() ) labels.resize( i + 1 );
66  labels[i] = label;
67  }
68  }
69  int bandNumber = mBandComboBox->currentData().toInt();
70  return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors, labels );
71 }
72 
73 void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
74 {
75  if ( column == 1 && item ) //change item color
76  {
77  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
78  QColor c = QgsColorDialog::getColor( item->background( column ).color(), nullptr );
79  if ( c.isValid() )
80  {
81  item->setBackground( column, QBrush( c ) );
82  emit widgetChanged();
83  }
84  }
85  else if ( column == 2 && item )
86  {
87  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
88  }
89 }
90 
92 {
93  const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
94  if ( pr )
95  {
96  //read values and colors and fill into tree widget
97  int nColors = pr->nColors();
98  QColor* colors = pr->colors();
99  for ( int i = 0; i < nColors; ++i )
100  {
101  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
102  item->setText( 0, QString::number( i ) );
103  item->setBackground( 1, QBrush( colors[i] ) );
104  item->setText( 2, pr->label( i ) );
105  }
106  delete[] colors;
107  }
108  else
109  {
110  //read default palette settings from layer
112  if ( provider )
113  {
114  QList<QgsColorRampShader::ColorRampItem> itemList = provider->colorTable( mBandComboBox->currentData().toInt() );
115  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
116  int index = 0;
117  for ( ; itemIt != itemList.constEnd(); ++itemIt )
118  {
119  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
120  item->setText( 0, QString::number( index ) );
121  item->setBackground( 1, QBrush( itemIt->color ) );
122  item->setText( 2, itemIt->label );
123  ++index;
124  }
125  }
126  }
127 }
QgsPalettedRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
virtual int bandCount() const =0
Get number of bands.
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Renderer for paletted raster images.
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QgsRasterRenderer * renderer() const
int nColors() const
Returns number of colors.
void setFromRenderer(const QgsRasterRenderer *r)
QString displayBandName(int band) const
Returns a band name for display. First choice is color name, otherwise band number.
QString label(int idx) const
Return optional category label.
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowAlpha=false)
Return a color selection from a color dialog.
QgsRasterRenderer * renderer() override
QColor * colors() const
Returns copy of color array (caller takes ownership)
QgsRasterDataProvider * dataProvider()
Returns the data provider.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
Base class for raster data providers.