QGIS API Documentation  2.99.0-Master (a18066b)
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 "qgsrasterdataprovider.h"
21 #include "qgsrasterlayer.h"
22 #include "qgscolordialog.h"
23 
24 #include <QColorDialog>
25 #include <QInputDialog>
26 #include <QMenu>
27 
29 {
30  setupUi( this );
31 
32  contextMenu = new QMenu( tr( "Options" ), this );
33  contextMenu->addAction( tr( "Change color" ), this, SLOT( changeColor() ) );
34  contextMenu->addAction( tr( "Change transparency" ), this, SLOT( changeTransparency() ) );
35 
36  mTreeWidget->setColumnWidth( ColorColumn, 50 );
37  mTreeWidget->setContextMenuPolicy( Qt::CustomContextMenu );
38  mTreeWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
39  connect( mTreeWidget, &QTreeView::customContextMenuRequested, [=]( const QPoint& ) { contextMenu->exec( QCursor::pos() ); }
40  );
41 
42  if ( mRasterLayer )
43  {
45  if ( !provider )
46  {
47  return;
48  }
49 
50  //fill available bands into combo box
51  int nBands = provider->bandCount();
52  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
53  {
54  mBandComboBox->addItem( displayBandName( i ), i );
55  }
56 
58  connect( mBandComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
59  }
60 }
61 
63 {
64  int nColors = mTreeWidget->topLevelItemCount();
65  QColor* colorArray = new QColor[nColors];
66  QVector<QString> labels;
67  for ( int i = 0; i < nColors; ++i )
68  {
69  colorArray[i] = mTreeWidget->topLevelItem( i )->background( 1 ).color();
70  QString label = mTreeWidget->topLevelItem( i )->text( 2 );
71  if ( !label.isEmpty() )
72  {
73  if ( i >= labels.size() ) labels.resize( i + 1 );
74  labels[i] = label;
75  }
76  }
77  int bandNumber = mBandComboBox->currentData().toInt();
78  return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors, labels );
79 }
80 
81 void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
82 {
83  if ( column == ColorColumn && item ) //change item color
84  {
85  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
86  QColor c = QgsColorDialog::getColor( item->background( column ).color(), this, QStringLiteral( "Change color" ), true );
87  if ( c.isValid() )
88  {
89  item->setBackground( column, QBrush( c ) );
90  emit widgetChanged();
91  }
92  }
93  else if ( column == LabelColumn && item )
94  {
95  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
96  }
97 }
98 
99 void QgsPalettedRendererWidget::on_mTreeWidget_itemChanged( QTreeWidgetItem * item, int column )
100 {
101  if ( column == LabelColumn && item ) //palette label modified
102  {
103  emit widgetChanged();
104  }
105 }
106 
108 {
109  const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
110  if ( pr )
111  {
112  //read values and colors and fill into tree widget
113  int nColors = pr->nColors();
114  QColor* colors = pr->colors();
115  for ( int i = 0; i < nColors; ++i )
116  {
117  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
118  item->setText( 0, QString::number( i ) );
119  item->setBackground( 1, QBrush( colors[i] ) );
120  item->setText( 2, pr->label( i ) );
121  }
122  delete[] colors;
123  }
124  else
125  {
126  //read default palette settings from layer
128  if ( provider )
129  {
130  QList<QgsColorRampShader::ColorRampItem> itemList = provider->colorTable( mBandComboBox->currentData().toInt() );
131  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
132  int index = 0;
133  for ( ; itemIt != itemList.constEnd(); ++itemIt )
134  {
135  QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
136  item->setText( 0, QString::number( index ) );
137  item->setBackground( 1, QBrush( itemIt->color ) );
138  item->setText( 2, itemIt->label );
139  ++index;
140  }
141  }
142  }
143 }
144 
145 void QgsPalettedRendererWidget::changeColor()
146 {
147  QList<QTreeWidgetItem *> itemList;
148  itemList = mTreeWidget->selectedItems();
149  if ( itemList.isEmpty() )
150  {
151  return;
152  }
153  QTreeWidgetItem* firstItem = itemList.first();
154 
155  QColor newColor = QgsColorDialog::getColor( firstItem->background( ColorColumn ).color(), this, QStringLiteral( "Change color" ), true );
156  if ( newColor.isValid() )
157  {
158  Q_FOREACH ( QTreeWidgetItem *item, itemList )
159  {
160  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
161  item->setBackground( ColorColumn, QBrush( newColor ) );
162  }
163  emit widgetChanged();
164  }
165 }
166 
167 void QgsPalettedRendererWidget::changeTransparency()
168 {
169  QList<QTreeWidgetItem *> itemList;
170  itemList = mTreeWidget->selectedItems();
171  if ( itemList.isEmpty() )
172  {
173  return;
174  }
175  QTreeWidgetItem* firstItem = itemList.first();
176 
177  bool ok;
178  double oldTransparency = ( firstItem->background( ColorColumn ).color().alpha() / 255.0 ) * 100.0;
179  double transparency = QInputDialog::getDouble( this, tr( "Transparency" ), tr( "Change color transparency [%]" ), oldTransparency, 0.0, 100.0, 0, &ok );
180  if ( ok )
181  {
182  int newTransparency = transparency / 100 * 255;
183  Q_FOREACH ( QTreeWidgetItem *item, itemList )
184  {
185  QColor newColor = item->background( ColorColumn ).color();
186  newColor.setAlpha( newTransparency );
187  item->setBackground( ColorColumn, QBrush( newColor ) );
188  }
189  emit widgetChanged();
190  }
191 }
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:36
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.
void setFromRenderer(const QgsRasterRenderer *r)
QString displayBandName(int band) const
Returns a band name for display. First choice is color name, otherwise band number.
QString label(int idx) const
Return optional category label.
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowAlpha=false)
Return a color selection from a color dialog.
QgsRasterRenderer * renderer() override
QColor * colors() const
Returns copy of color array (caller takes ownership)
QgsRasterDataProvider * dataProvider()
Returns the data provider.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
Base class for raster data providers.