Skip to content
Permalink
Browse files

[FEATURE][raster] support setting of color and transparency on multiple

items in paletted UI
  • Loading branch information
nirvn committed Dec 9, 2016
1 parent 4894a6d commit ffb6ba7a3e66efdd8c7a5d1af4e81102ae9748d4
Showing with 51 additions and 3 deletions.
  1. +37 −3 src/gui/raster/qgspalettedrendererwidget.cpp
  2. +14 −0 src/gui/raster/qgspalettedrendererwidget.h
@@ -20,12 +20,24 @@
#include "qgsrasterdataprovider.h"
#include "qgsrasterlayer.h"
#include "qgscolordialog.h"

#include <QColorDialog>
#include <QInputDialog>
#include <QMenu>

QgsPalettedRendererWidget::QgsPalettedRendererWidget( QgsRasterLayer* layer, const QgsRectangle &extent ): QgsRasterRendererWidget( layer, extent )
{
setupUi( this );

contextMenu = new QMenu( tr( "Options" ), this );
contextMenu->addAction( tr( "Change color" ), this, SLOT( changeColor() ) );

mTreeWidget->setColumnWidth( ColorColumn, 50 );
mTreeWidget->setContextMenuPolicy( Qt::CustomContextMenu );
mTreeWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
connect( mTreeWidget, &QTreeView::customContextMenuRequested, [=]( const QPoint& ) { contextMenu->exec( QCursor::pos() ); }
);

if ( mRasterLayer )
{
QgsRasterDataProvider* provider = mRasterLayer->dataProvider();
@@ -72,7 +84,7 @@ QgsRasterRenderer* QgsPalettedRendererWidget::renderer()

void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
{
if ( column == 1 && item ) //change item color
if ( column == ColorColumn && item ) //change item color
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QColor c = QgsColorDialog::getColor( item->background( column ).color(), nullptr );
@@ -82,15 +94,15 @@ void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetIte
emit widgetChanged();
}
}
else if ( column == 2 && item )
else if ( column == LabelColumn && item )
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
}
}

void QgsPalettedRendererWidget::on_mTreeWidget_itemChanged( QTreeWidgetItem * item, int column )
{
if ( column == 2 && item ) //palette label modified
if ( column == LabelColumn && item ) //palette label modified
{
emit widgetChanged();
}
@@ -133,3 +145,25 @@ void QgsPalettedRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
}
}
}

void QgsPalettedRendererWidget::changeColor()
{
QList<QTreeWidgetItem *> itemList;
itemList = mTreeWidget->selectedItems();
if ( itemList.isEmpty() )
{
return;
}
QTreeWidgetItem* firstItem = itemList.first();

QColor newColor = QgsColorDialog::getColor( firstItem->background( ColorColumn ).color(), this, QStringLiteral( "Change color" ), true );
if ( newColor.isValid() )
{
Q_FOREACH ( QTreeWidgetItem *item, itemList )
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
item->setBackground( ColorColumn, QBrush( newColor ) );
}
emit widgetChanged();
}
}
@@ -31,6 +31,7 @@ class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, priv
Q_OBJECT

public:

QgsPalettedRendererWidget( QgsRasterLayer* layer, const QgsRectangle &extent = QgsRectangle() );
static QgsRasterRendererWidget* create( QgsRasterLayer* layer, const QgsRectangle &theExtent ) { return new QgsPalettedRendererWidget( layer, theExtent ); }
~QgsPalettedRendererWidget();
@@ -39,9 +40,22 @@ class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, priv

void setFromRenderer( const QgsRasterRenderer* r );

private:

enum Column
{
ValueColumn = 0,
ColorColumn = 1,
LabelColumn = 2,
};

QMenu* contextMenu;

private slots:

void on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column );
void on_mTreeWidget_itemChanged( QTreeWidgetItem * item, int column );
void changeColor();
};

#endif // QGSPALETTEDRENDERERWIDGET_H

0 comments on commit ffb6ba7

Please sign in to comment.
You can’t perform that action at this time.