Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refresh attribute table for row styles. Tests
  • Loading branch information
NathanW2 committed Aug 23, 2015
1 parent d753626 commit b87073e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 17 deletions.
44 changes: 44 additions & 0 deletions python/core/qgsconditionalstyle.sip
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down
11 changes: 1 addition & 10 deletions python/core/qgsfielduiproperties.sip
Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsconditionalstyle.cpp
Expand Up @@ -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 )
Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsconditionalstyle.h
Expand Up @@ -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.
*/
Expand Down
12 changes: 9 additions & 3 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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 );
Expand Down
9 changes: 7 additions & 2 deletions src/gui/attributetable/qgsfieldconditionalformatwidget.cpp
Expand Up @@ -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() )
{
Expand All @@ -163,7 +165,7 @@ void QgsFieldConditionalFormatWidget::deleteRule()

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

void QgsFieldConditionalFormatWidget::cancelRule()
Expand Down Expand Up @@ -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();
}

Expand Down
23 changes: 23 additions & 0 deletions tests/src/python/test_qgsconditionalstyle.py
Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit b87073e

Please sign in to comment.