Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix cannot modify diagram attribute expression (fix #15514)
Also clean up some code

(cherry-picked from 111106c)
  • Loading branch information
nyalldawson committed Oct 6, 2016
1 parent ffafafd commit 7c46784
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 37 deletions.
105 changes: 69 additions & 36 deletions src/app/qgsdiagramproperties.cpp
Expand Up @@ -96,6 +96,9 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer* layer,

mMaxValueSpinBox->setShowClearButton( false );

mDiagramAttributesTreeWidget->setItemDelegateForColumn( ColumnAttributeExpression, new EditBlockerDelegate( this ) );
mDiagramAttributesTreeWidget->setItemDelegateForColumn( ColumnColor, new EditBlockerDelegate( this ) );

connect( mFixedSizeRadio, SIGNAL( toggled( bool ) ), this, SLOT( scalingTypeChanged() ) );
connect( mAttributeBasedScalingRadio, SIGNAL( toggled( bool ) ), this, SLOT( scalingTypeChanged() ) );

Expand Down Expand Up @@ -184,7 +187,7 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer* layer,
QTreeWidgetItem *newItem = new QTreeWidgetItem( mAttributesTreeWidget );
QString name = QString( "\"%1\"" ).arg( layerFields[idx].name() );
newItem->setText( 0, name );
newItem->setData( 0, Qt::UserRole, name );
newItem->setData( 0, RoleAttributeExpression, name );
newItem->setFlags( newItem->flags() & ~Qt::ItemIsDropEnabled );

mDataDefinedXComboBox->addItem( layerFields[idx].name(), idx );
Expand Down Expand Up @@ -341,7 +344,7 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer* layer,
{
QTreeWidgetItem *newItem = new QTreeWidgetItem( mDiagramAttributesTreeWidget );
newItem->setText( 0, *catIt );
newItem->setData( 0, Qt::UserRole, *catIt );
newItem->setData( 0, RoleAttributeExpression, *catIt );
newItem->setFlags( newItem->flags() & ~Qt::ItemIsDropEnabled );
QColor col( *coIt );
col.setAlpha( 255 );
Expand Down Expand Up @@ -519,7 +522,7 @@ void QgsDiagramProperties::addAttribute( QTreeWidgetItem * item )

newItem->setText( 0, item->text( 0 ) );
newItem->setText( 2, guessLegendText( item->text( 0 ) ) );
newItem->setData( 0, Qt::UserRole, item->data( 0, Qt::UserRole ) );
newItem->setData( 0, RoleAttributeExpression, item->data( 0, RoleAttributeExpression ) );
newItem->setFlags(( newItem->flags() | Qt::ItemIsEditable ) & ~Qt::ItemIsDropEnabled );

//set initial color for diagram category
Expand Down Expand Up @@ -605,13 +608,33 @@ void QgsDiagramProperties::on_mDiagramFontButton_clicked()

void QgsDiagramProperties::on_mDiagramAttributesTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
{
if ( column == 1 ) //change color
switch ( column )
{
QColor newColor = QgsColorDialogV2::getColor( item->background( 1 ).color(), nullptr );
if ( newColor.isValid() )
case ColumnAttributeExpression:
{
item->setBackground( 1, QBrush( newColor ) );
QString currentExpression = item->data( 0, RoleAttributeExpression ).toString();

QString newExpression = showExpressionBuilder( currentExpression );
if ( !newExpression.isEmpty() )
{
item->setData( 0, Qt::DisplayRole, newExpression );
item->setData( 0, RoleAttributeExpression, newExpression );
}
break;
}

case ColumnColor:
{
QColor newColor = QgsColorDialogV2::getColor( item->background( 1 ).color(), nullptr );
if ( newColor.isValid() )
{
item->setBackground( 1, QBrush( newColor ) );
}
break;
}

case ColumnLegendText:
break;
}
}

Expand Down Expand Up @@ -714,7 +737,7 @@ void QgsDiagramProperties::apply()
QColor color = mDiagramAttributesTreeWidget->topLevelItem( i )->background( 1 ).color();
color.setAlpha( 255 - ds.transparency );
categoryColors.append( color );
categoryAttributes.append( mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, Qt::UserRole ).toString() );
categoryAttributes.append( mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, RoleAttributeExpression ).toString() );
categoryLabels.append( mDiagramAttributesTreeWidget->topLevelItem( i )->text( 2 ) );
}
ds.categoryColors = categoryColors;
Expand Down Expand Up @@ -832,23 +855,16 @@ void QgsDiagramProperties::apply()
mLayer->triggerRepaint();
}

void QgsDiagramProperties::showAddAttributeExpressionDialog()
QString QgsDiagramProperties::showExpressionBuilder( const QString& initialExpression )
{
QString expression;
QList<QTreeWidgetItem *> selections = mAttributesTreeWidget->selectedItems();
if ( !selections.empty() )
{
expression = selections[0]->text( 0 );
}

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( QgisApp::instance()->mapCanvas()->mapSettings() )
<< QgsExpressionContextUtils::layerScope( mLayer );

QgsExpressionBuilderDialog dlg( mLayer, expression, this, "generic", context );
QgsExpressionBuilderDialog dlg( mLayer, initialExpression, this, "generic", context );
dlg.setWindowTitle( tr( "Expression based attribute" ) );

QgsDistanceArea myDa;
Expand All @@ -859,25 +875,42 @@ void QgsDiagramProperties::showAddAttributeExpressionDialog()

if ( dlg.exec() == QDialog::Accepted )
{
QString expression = dlg.expressionText();
//Only add the expression if the user has entered some text.
if ( !expression.isEmpty() )
{
QTreeWidgetItem *newItem = new QTreeWidgetItem( mDiagramAttributesTreeWidget );

newItem->setText( 0, expression );
newItem->setText( 2, expression );
newItem->setData( 0, Qt::UserRole, expression );
newItem->setFlags(( newItem->flags() | Qt::ItemIsEditable ) & ~Qt::ItemIsDropEnabled );

//set initial color for diagram category
int red = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int green = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int blue = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
QColor randomColor( red, green, blue );
newItem->setBackground( 1, QBrush( randomColor ) );
mDiagramAttributesTreeWidget->addTopLevelItem( newItem );
}
return dlg.expressionText();
}
else
{
return QString();
}
}

void QgsDiagramProperties::showAddAttributeExpressionDialog()
{
QString expression;
QList<QTreeWidgetItem *> selections = mAttributesTreeWidget->selectedItems();
if ( !selections.empty() )
{
expression = selections[0]->text( 0 );
}

QString newExpression = showExpressionBuilder( expression );

//Only add the expression if the user has entered some text.
if ( !newExpression.isEmpty() )
{
QTreeWidgetItem *newItem = new QTreeWidgetItem( mDiagramAttributesTreeWidget );

newItem->setText( 0, newExpression );
newItem->setText( 2, newExpression );
newItem->setData( 0, RoleAttributeExpression, newExpression );
newItem->setFlags(( newItem->flags() | Qt::ItemIsEditable ) & ~Qt::ItemIsDropEnabled );

//set initial color for diagram category
int red = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int green = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int blue = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
QColor randomColor( red, green, blue );
newItem->setBackground( 1, QBrush( randomColor ) );
mDiagramAttributesTreeWidget->addTopLevelItem( newItem );
}
activateWindow(); // set focus back parent
}
Expand Down
30 changes: 30 additions & 0 deletions src/app/qgsdiagramproperties.h
Expand Up @@ -21,6 +21,7 @@
#include "qgssymbolv2.h"
#include <QDialog>
#include <ui_qgsdiagrampropertiesbase.h>
#include <QStyledItemDelegate>

class QgsVectorLayer;
class QgsMapCanvas;
Expand Down Expand Up @@ -59,6 +60,21 @@ class APP_EXPORT QgsDiagramProperties : public QWidget, private Ui::QgsDiagramPr
QgsVectorLayer* mLayer;

private:

enum Columns
{
ColumnAttributeExpression = 0,
ColumnColor,
ColumnLegendText,
};

enum Roles
{
RoleAttributeExpression = Qt::UserRole,
};

QString showExpressionBuilder( const QString& initialExpression );

// Keeps track of the diagram type to properly save / restore settings when the diagram type combo box is set to no diagram.
QString mDiagramType;
QScopedPointer< QgsMarkerSymbolV2 > mSizeLegendSymbol;
Expand All @@ -67,4 +83,18 @@ class APP_EXPORT QgsDiagramProperties : public QWidget, private Ui::QgsDiagramPr
QgsMapCanvas *mMapCanvas;
};

class EditBlockerDelegate: public QStyledItemDelegate
{
public:
EditBlockerDelegate( QObject* parent = nullptr )
: QStyledItemDelegate( parent )
{}

virtual QWidget* createEditor( QWidget *, const QStyleOptionViewItem &, const QModelIndex & ) const override
{
return nullptr;
}
};


#endif // QGSDIAGRAMPROPERTIES_H
2 changes: 1 addition & 1 deletion src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -245,7 +245,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(

QVBoxLayout* diagLayout = new QVBoxLayout( mDiagramFrame );
diagLayout->setMargin( 0 );
diagramPropertiesDialog = new QgsDiagramProperties( mLayer, mDiagramFrame, nullptr );
diagramPropertiesDialog = new QgsDiagramProperties( mLayer, mDiagramFrame, QgisApp::instance()->mapCanvas() );
diagramPropertiesDialog->layout()->setContentsMargins( -1, 0, -1, 0 );
diagLayout->addWidget( diagramPropertiesDialog );
mDiagramFrame->setLayout( diagLayout );
Expand Down

0 comments on commit 7c46784

Please sign in to comment.