QGIS API Documentation  2.8.2-Wien
 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 "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  }
45 }
46 
48 {
49 
50 }
51 
53 {
54  int nColors = mTreeWidget->topLevelItemCount();
55  QColor* colorArray = new QColor[nColors];
56  QVector<QString> labels;
57  for ( int i = 0; i < nColors; ++i )
58  {
59  colorArray[i] = mTreeWidget->topLevelItem( i )->background( 1 ).color();
60  QString label = mTreeWidget->topLevelItem( i )->text( 2 );
61  if ( !label.isEmpty() )
62  {
63  if ( i >= labels.size() ) labels.resize( i + 1 );
64  labels[i] = label;
65  }
66  }
67  int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
68  return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors, labels );
69 }
70 
71 void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
72 {
73  if ( column == 1 && item ) //change item color
74  {
75  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
76  QColor c = QgsColorDialogV2::getColor( item->background( column ).color(), 0 );
77  if ( c.isValid() )
78  {
79  item->setBackground( column, QBrush( c ) );
80  }
81  }
82  else if ( column == 2 && item )
83  {
84  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
85  }
86 }
87 
89 {
90  const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
91  if ( pr )
92  {
93  //read values and colors and fill into tree widget
94  int nColors = pr->nColors();
95  QColor* colors = pr->colors();
96  for ( int i = 0; i < nColors; ++i )
97  {
98  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
99  item->setText( 0, QString::number( i ) );
100  item->setBackground( 1, QBrush( colors[i] ) );
101  item->setText( 2, pr->label( i ) );
102  }
103  delete[] colors;
104  }
105  else
106  {
107  //read default palette settings from layer
109  if ( provider )
110  {
111  QList<QgsColorRampShader::ColorRampItem> itemList = provider->colorTable( mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt() );
112  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
113  int index = 0;
114  for ( ; itemIt != itemList.constEnd(); ++itemIt )
115  {
116  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
117  item->setText( 0, QString::number( index ) );
118  item->setBackground( 1, QBrush( itemIt->color ) );
119  item->setText( 2, itemIt->label );
120  ++index;
121  }
122  }
123  }
124 }