Skip to content

Commit

Permalink
[FEATURE][raster] support setting of color and transparency on multiple
Browse files Browse the repository at this point in the history
items in paletted UI
  • Loading branch information
nirvn committed Dec 9, 2016
1 parent 4894a6d commit ffb6ba7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/gui/raster/qgspalettedrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
}
14 changes: 14 additions & 0 deletions src/gui/raster/qgspalettedrendererwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.