QGIS API Documentation  2.15.0-Master (972fc9f)
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 
91 {
92  const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
93  if ( pr )
94  {
95  //read values and colors and fill into tree widget
96  int nColors = pr->nColors();
97  QColor* colors = pr->colors();
98  for ( int i = 0; i < nColors; ++i )
99  {
100  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
101  item->setText( 0, QString::number( i ) );
102  item->setBackground( 1, QBrush( colors[i] ) );
103  item->setText( 2, pr->label( i ) );
104  }
105  delete[] colors;
106  }
107  else
108  {
109  //read default palette settings from layer
111  if ( provider )
112  {
113  QList<QgsColorRampShader::ColorRampItem> itemList = provider->colorTable( mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt() );
115  int index = 0;
116  for ( ; itemIt != itemList.constEnd(); ++itemIt )
117  {
118  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
119  item->setText( 0, QString::number( index ) );
120  item->setBackground( 1, QBrush( itemIt->color ) );
121  item->setText( 2, itemIt->label );
122  ++index;
123  }
124  }
125  }
126 }
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)
QString label(int idx) const
Return optional category label.
Renderer for paletted raster images.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
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)
QgsRasterRenderer * renderer() const
bool isEmpty() const
int nColors() const
Returns number of colors.
QgsRasterRenderer * renderer() override
QString displayBandName(int band) const
Returns a band name for display.
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)
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
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
QColor * colors() const
Returns copy of color array (caller takes ownership)
bool isValid() const
Base class for raster data providers.