QGIS API Documentation  2.0.1-Dufour
 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  for ( int i = 0; i < nColors; ++i )
56  {
57  colorArray[i] = mTreeWidget->topLevelItem( i )->background( 1 ).color();
58  }
59  int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
60  return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors );
61 }
62 
63 void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
64 {
65  if ( column == 1 && item ) //change item color
66  {
67  QColor c = QColorDialog::getColor( item->background( column ).color() );
68  if ( c.isValid() )
69  {
70  item->setBackground( column, QBrush( c ) );
71  }
72  }
73 }
74 
76 {
77  const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
78  if ( pr )
79  {
80  //read values and colors and fill into tree widget
81  int nColors = pr->nColors();
82  QColor* colors = pr->colors();
83  for ( int i = 0; i < nColors; ++i )
84  {
85  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
86  item->setText( 0, QString::number( i ) );
87  item->setBackground( 1, QBrush( colors[i] ) );
88  }
89  delete[] colors;
90  }
91  else
92  {
93  //read default palette settings from layer
95  if ( provider )
96  {
97  QList<QgsColorRampShader::ColorRampItem> itemList = provider->colorTable( mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt() );
98  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
99  int index = 0;
100  for ( ; itemIt != itemList.constEnd(); ++itemIt )
101  {
102  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
103  item->setText( 0, QString::number( index ) );
104  item->setBackground( 1, QBrush( itemIt->color ) );
105  ++index;
106  }
107  }
108  }
109 }