QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsrasterlayer.h"
21 #include "qgscolordialog.h"
22 #include <QColorDialog>
23 
25 {
26  setupUi( this );
27 
28  if ( mRasterLayer )
29  {
31  if ( !provider )
32  {
33  return;
34  }
35 
36  //fill available bands into combo box
37  int nBands = provider->bandCount();
38  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
39  {
40  mBandComboBox->addItem( displayBandName( i ), i );
41  }
42 
44  connect( mBandComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
45  }
46 }
47 
49 {
50 
51 }
52 
54 {
55  int nColors = mTreeWidget->topLevelItemCount();
56  QColor* colorArray = new QColor[nColors];
57  QVector<QString> labels;
58  for ( int i = 0; i < nColors; ++i )
59  {
60  colorArray[i] = mTreeWidget->topLevelItem( i )->background( 1 ).color();
61  QString label = mTreeWidget->topLevelItem( i )->text( 2 );
62  if ( !label.isEmpty() )
63  {
64  if ( i >= labels.size() ) labels.resize( i + 1 );
65  labels[i] = label;
66  }
67  }
68  int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
69  return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors, labels );
70 }
71 
72 void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
73 {
74  if ( column == 1 && item ) //change item color
75  {
76  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
77  QColor c = QgsColorDialogV2::getColor( item->background( column ).color(), nullptr );
78  if ( c.isValid() )
79  {
80  item->setBackground( column, QBrush( c ) );
81  emit widgetChanged();
82  }
83  }
84  else if ( column == 2 && item )
85  {
86  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
87  }
88 }
89 
90 void QgsPalettedRendererWidget::on_mTreeWidget_itemChanged( QTreeWidgetItem * item, int column )
91 {
92  if ( column == 2 && item ) //palette label modified
93  {
94  emit widgetChanged();
95  }
96 }
97 
99 {
100  const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
101  if ( pr )
102  {
103  //read values and colors and fill into tree widget
104  int nColors = pr->nColors();
105  QColor* colors = pr->colors();
106  for ( int i = 0; i < nColors; ++i )
107  {
108  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
109  item->setText( 0, QString::number( i ) );
110  item->setBackground( 1, QBrush( colors[i] ) );
111  item->setText( 2, pr->label( i ) );
112  }
113  delete[] colors;
114  }
115  else
116  {
117  //read default palette settings from layer
119  if ( provider )
120  {
121  QList<QgsColorRampShader::ColorRampItem> itemList = provider->colorTable( mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt() );
123  int index = 0;
124  for ( ; itemIt != itemList.constEnd(); ++itemIt )
125  {
126  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
127  item->setText( 0, QString::number( index ) );
128  item->setBackground( 1, QBrush( itemIt->color ) );
129  item->setText( 2, itemIt->label );
130  ++index;
131  }
132  }
133  }
134 }
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
void setBackground(int column, const QBrush &brush)
void setupUi(QWidget *widget)
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.
const QColor & color() const
void setFlags(QFlags< Qt::ItemFlag > flags)
void setFromRenderer(const QgsRasterRenderer *r)
QString number(int n, int base)
void resize(int size)
bool isEmpty() const
QString displayBandName(int band) const
Returns a band name for display.
QString label(int idx) const
Return optional category label.
QgsRasterRenderer * renderer() override
QColor * colors() const
Returns copy of color array (caller takes ownership)
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowAlpha=false)
Return a color selection from a color dialog.
void setText(int column, const QString &text)
QgsRasterDataProvider * dataProvider()
Returns the data provider.
const_iterator constEnd() const
const_iterator constBegin() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int size() const
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
QBrush background(int column) const
bool isValid() const
Base class for raster data providers.