Skip to content
Permalink
Browse files

Refresh attribute table for row styles. Tests

  • Loading branch information
NathanW2 committed Aug 23, 2015
1 parent d753626 commit b87073e1466d5fc6136df6bd795ccbd349dada38
@@ -24,6 +24,12 @@ class QgsConditionalStyle
*/
QPixmap renderPreview();

/**
* @brief Set the name of the style. Names are optional but handy for display
* @param value The name given to the style
*/
void setName( QString value );

/**
* @brief Set the rule for the style. Rules should be of QgsExpression syntax.
* Special value of @value is replaced at run time with the check value
@@ -55,6 +61,18 @@ class QgsConditionalStyle
*/
void setSymbol( QgsSymbolV2* value );

/**
* @brief The name of the style.
* @return The name of the style. Names are optional so might be empty.
*/
QString displayText() const;

/**
* @brief The name of the style.
* @return The name of the style. Names are optional so might be empty.
*/
QString name() const;

/**
* @brief The symbol used to generate the icon for the style
* @return The QgsSymbolV2 used for the icon
@@ -97,6 +115,32 @@ class QgsConditionalStyle
*/
bool isValid() const;

/**
* @brief Find and return the matching styles for the value and feature.
* If no match is found a invalid QgsCondtionalStyle is return.
*
* @return A condtional style that matches the value and feature.
* Check with QgsCondtionalStyle::isValid()
*/
static QList<QgsConditionalStyle> matchingConditionalStyles( QList<QgsConditionalStyle> styles, QVariant value, QgsFeature* feature );

/**
* @brief Find and return the matching style for the value and feature.
* If no match is found a invalid QgsCondtionalStyle is return.
*
* @return A condtional style that matches the value and feature.
* Check with QgsCondtionalStyle::isValid()
*/
static QgsConditionalStyle matchingConditionalStyle( QList<QgsConditionalStyle> styles, QVariant value, QgsFeature* feature );

/**
* @brief Compress a list of styles into a single style. This can be used to stack the elements of the
* styles. The font of the last style is used in the output.
* @param styles The list of styles to compress down
* @return A single style generated from joining each style property.
*/
static QgsConditionalStyle compressStyles( QList<QgsConditionalStyle> styles );

/** Reads vector conditional style specific state from layer Dom node.
*/
virtual bool readXml( const QDomNode& node );
@@ -23,16 +23,7 @@ class QgsFieldUIProperties
* @brief Returns the condtional styles set for the field UI properties
* @return A list of condtional styles that have been set.
*/
const QList<QgsConditionalStyle> getConditionalStyles();

/**
* @brief Find and return the matching style for the value and feature.
* If no match is found a invalid QgsCondtionalStyle is return.
*
* @return A condtional style that matches the value and feature.
* Check with QgsCondtionalStyle::isValid()
*/
const QgsConditionalStyle matchingConditionalStyle( QVariant value, QgsFeature* feature );
const QList<QgsConditionalStyle> conditionalStyles();

/** Reads field ui properties specific state from Dom node.
*/
@@ -140,7 +140,7 @@ QgsConditionalStyle QgsConditionalStyle::matchingConditionalStyle( QList<QgsCond
return QgsConditionalStyle();
}

QgsConditionalStyle QgsConditionalStyle::stackStyles( QList<QgsConditionalStyle> styles )
QgsConditionalStyle QgsConditionalStyle::compressStyles( QList<QgsConditionalStyle> styles )
{
QgsConditionalStyle style;
foreach ( QgsConditionalStyle s, styles )
@@ -147,7 +147,13 @@ class CORE_EXPORT QgsConditionalStyle
*/
static QgsConditionalStyle matchingConditionalStyle( QList<QgsConditionalStyle> styles, QVariant value, QgsFeature* feature );

static QgsConditionalStyle stackStyles( QList<QgsConditionalStyle> styles );
/**
* @brief Compress a list of styles into a single style. This can be used to stack the elements of the
* styles. The font of the last style is used in the output.
* @param styles The list of styles to compress down
* @return A single style generated from joining each style property.
*/
static QgsConditionalStyle compressStyles( QList<QgsConditionalStyle> styles );

/** Reads vector conditional style specific state from layer Dom node.
*/
@@ -387,6 +387,13 @@ void QgsAttributeTableModel::loadLayer()

void QgsAttributeTableModel::fieldConditionalStyleChanged( const QString &fieldName )
{
if ( fieldName.isNull() )
{
mRowStylesMap.clear();
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, columnCount() - 1 ) );
return;
}

int fieldIndex = mLayerCache->layer()->fieldNameIndex( fieldName );
if ( fieldIndex == -1 )
return;
@@ -595,11 +602,11 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons

}

QgsConditionalStyle rowstyle = QgsConditionalStyle::stackStyles( styles );
QgsConditionalStyle rowstyle = QgsConditionalStyle::compressStyles( styles );
QgsFieldUIProperties props = layer()->fieldUIProperties( field.name() );
styles = QgsConditionalStyle::matchingConditionalStyles( props.conditionalStyles(), val, &mFeat );
styles.insert( 0, rowstyle );
QgsConditionalStyle style = QgsConditionalStyle::stackStyles( styles );
QgsConditionalStyle style = QgsConditionalStyle::compressStyles( styles );

if ( style.isValid() )
{
@@ -677,7 +684,6 @@ void QgsAttributeTableModel::reload( const QModelIndex &index1, const QModelInde
}



void QgsAttributeTableModel::executeAction( int action, const QModelIndex &idx ) const
{
QgsFeature f = feature( idx );
@@ -150,11 +150,13 @@ void QgsFieldConditionalFormatWidget::deleteRule()
{
QList<QgsConditionalStyle> styles = getStyles();
styles.removeAt( mEditIndex );
QString fieldName;
if ( fieldRadio->isChecked() )
{
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
props.setConditionalStyles( styles );
mLayer->setFieldUIProperties( mFieldCombo->currentField(), props );
fieldName = mFieldCombo->currentField();
}
if ( rowRadio->isChecked() )
{
@@ -163,7 +165,7 @@ void QgsFieldConditionalFormatWidget::deleteRule()

pages->setCurrentIndex( 0 );
reloadStyles();
emit rulesUpdated( mFieldCombo->currentField() );
emit rulesUpdated( fieldName );
}

void QgsFieldConditionalFormatWidget::cancelRule()
@@ -295,19 +297,22 @@ void QgsFieldConditionalFormatWidget::saveRule()
{
styles.append( style );
}

QString fieldName;
if ( fieldRadio->isChecked() )
{
QgsFieldUIProperties props = QgsFieldUIProperties();
props.setConditionalStyles( styles );
mLayer->setFieldUIProperties( mFieldCombo->currentField(), props );
fieldName = mFieldCombo->currentField();
}
if ( rowRadio->isChecked() )
{
mLayer->setRowStyles( styles );
}
pages->setCurrentIndex( 0 );
reloadStyles();
emit rulesUpdated( mFieldCombo->currentField() );
emit rulesUpdated( fieldName );
reset();
}

@@ -27,6 +27,14 @@


class TestPyQgsConditionalStyle(TestCase):
def new_feature(self):
feature = QgsFeature()
fields = QgsFields()
fields.append(QgsField("testfield", QVariant.Int))
feature.setFields(fields, True)
feature["testfield"] = 20
return feature

def test_MatchesReturnsTrueForSimpleMatch(self):
style = QgsConditionalStyle("@value > 10")
assert style.matches(20)
@@ -44,6 +52,21 @@ def test_MatchesTrueForFields(self):
style = QgsConditionalStyle('"testfield" = @value')
assert style.matches(20, feature)

def test_MatchingStylesReturnsListOfCorrectStyles(self):
styles = []
style = QgsConditionalStyle("@value > 10")
style.setName("1")
styles.append(style)
style = QgsConditionalStyle("@value > 10")
style.setName("2")
styles.append(style)
style = QgsConditionalStyle("@value < 5")
style.setName("3")
styles.append(style)
out = QgsConditionalStyle.matchingConditionalStyles(styles, 20, self.new_feature())
assert len(out) == 2
out[0].name() == "1"
out[1].name() == "2"

if __name__ == '__main__':
unittest.main()

0 comments on commit b87073e

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