QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <QColorDialog>
22 
24 {
25  setupUi( this );
26 
27  if ( mRasterLayer )
28  {
30  if ( !provider )
31  {
32  return;
33  }
34 
35  //fill available bands into combo box
36  int nBands = provider->bandCount();
37  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
38  {
39  mBandComboBox->addItem( displayBandName( i ), i );
40  }
41 
43  }
44 }
45 
47 {
48 
49 }
50 
52 {
53  int nColors = mTreeWidget->topLevelItemCount();
54  QColor* colorArray = new QColor[nColors];
55  QVector<QString> labels;
56  for ( int i = 0; i < nColors; ++i )
57  {
58  colorArray[i] = mTreeWidget->topLevelItem( i )->background( 1 ).color();
59  QString label = mTreeWidget->topLevelItem( i )->text( 2 );
60  if ( !label.isEmpty() )
61  {
62  if ( i >= labels.size() ) labels.resize( i + 1 );
63  labels[i] = label;
64  }
65  }
66  int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
67  return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors, labels );
68 }
69 
70 void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
71 {
72  if ( column == 1 && item ) //change item color
73  {
74  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
75  QColor c = QColorDialog::getColor( item->background( column ).color() );
76  if ( c.isValid() )
77  {
78  item->setBackground( column, QBrush( c ) );
79  }
80  }
81  else if ( column == 2 && item )
82  {
83  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
84  }
85 }
86 
88 {
89  const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
90  if ( pr )
91  {
92  //read values and colors and fill into tree widget
93  int nColors = pr->nColors();
94  QColor* colors = pr->colors();
95  for ( int i = 0; i < nColors; ++i )
96  {
97  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
98  item->setText( 0, QString::number( i ) );
99  item->setBackground( 1, QBrush( colors[i] ) );
100  item->setText( 2, pr->label( i ) );
101  }
102  delete[] colors;
103  }
104  else
105  {
106  //read default palette settings from layer
108  if ( provider )
109  {
110  QList<QgsColorRampShader::ColorRampItem> itemList = provider->colorTable( mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt() );
111  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
112  int index = 0;
113  for ( ; itemIt != itemList.constEnd(); ++itemIt )
114  {
115  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
116  item->setText( 0, QString::number( index ) );
117  item->setBackground( 1, QBrush( itemIt->color ) );
118  item->setText( 2, itemIt->label );
119  ++index;
120  }
121  }
122  }
123 }
QgsPalettedRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
virtual int bandCount() const =0
Get number of bands.
void on_mTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
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.
void setFromRenderer(const QgsRasterRenderer *r)
QgsRasterRenderer * renderer() const
int nColors() const
Returns number of colors.
QString displayBandName(int band) const
Returns a band name for display.
QgsRasterDataProvider * dataProvider()
Returns the data provider.
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
Raster renderer pipe that applies colors to a raster.
QColor * colors() const
Returns copy of color array (caller takes ownership)
Base class for raster data providers.