Skip to content
Permalink
Browse files

Add full row styles

  • Loading branch information
NathanW2 committed Aug 21, 2015
1 parent bbf2fe8 commit 323977fb6e7790458a97347daf0d0eb0633d0a72
@@ -109,6 +109,43 @@ QPixmap QgsConditionalStyle::renderPreview()
return pixmap;
}

QList<QgsConditionalStyle> QgsConditionalStyle::matchingConditionalStyles( QList<QgsConditionalStyle> styles, QVariant value, QgsFeature *feature )
{
QList<QgsConditionalStyle> matchingstyles;
foreach ( QgsConditionalStyle style, styles )
{
if ( style.matches( value, feature ) )
matchingstyles.append( style );
}
return matchingstyles;
}

QgsConditionalStyle QgsConditionalStyle::matchingConditionalStyle( QList<QgsConditionalStyle> styles, QVariant value, QgsFeature *feature )
{
foreach ( QgsConditionalStyle style, styles )
{
if ( style.matches( value, feature ) )
return style;
}
return QgsConditionalStyle();
}

QgsConditionalStyle QgsConditionalStyle::stackStyles( QList<QgsConditionalStyle> styles )
{
QgsConditionalStyle style;
foreach ( QgsConditionalStyle s, styles )
{
style.setFont( s.font() );
if ( s.backgroundColor().isValid() && s.backgroundColor().alpha() != 0 )
style.setBackgroundColor( s.backgroundColor() );
if ( s.textColor().isValid() && s.textColor().alpha() != 0 )
style.setTextColor( s.textColor() );
if ( s.symbol() )
style.setSymbol( s.symbol() );
}
return style;
}

bool QgsConditionalStyle::writeXml( QDomNode &node, QDomDocument &doc )
{
QDomElement stylesel = doc.createElement( "style" );
@@ -111,6 +111,26 @@ class CORE_EXPORT QgsConditionalStyle
*/
bool isValid() const { return mValid; }

/**
* @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 );

static QgsConditionalStyle stackStyles( QList<QgsConditionalStyle> styles );

/** Reads vector conditional style specific state from layer Dom node.
*/
virtual bool readXml( const QDomNode& node );
@@ -119,6 +139,7 @@ class CORE_EXPORT QgsConditionalStyle
*/
virtual bool writeXml( QDomNode & node, QDomDocument & doc );


private:

bool mValid;
@@ -13,31 +13,11 @@ void QgsFieldUIProperties::setConditionalStyles( QList<QgsConditionalStyle> styl
mStyles = styles;
}

QList<QgsConditionalStyle> QgsFieldUIProperties::getConditionalStyles()
QList<QgsConditionalStyle> QgsFieldUIProperties::conditionalStyles()
{
return mStyles;
}

QList<QgsConditionalStyle> QgsFieldUIProperties::matchingConditionalStyles( QVariant value, QgsFeature *feature )
{
QList<QgsConditionalStyle> styles;
foreach ( QgsConditionalStyle style, mStyles )
{
if ( style.matches( value, feature ) )
styles.append( style );
}
return styles;
}

QgsConditionalStyle QgsFieldUIProperties::matchingConditionalStyle( QVariant value, QgsFeature *feature )
{
foreach ( QgsConditionalStyle style, mStyles )
{
if ( style.matches( value, feature ) )
return style;
}
return QgsConditionalStyle();
}

bool QgsFieldUIProperties::writeXml( QDomNode &node, QDomDocument &doc )
{
@@ -30,25 +30,7 @@ class CORE_EXPORT QgsFieldUIProperties
* @brief Returns the condtional styles set for the field UI properties
* @return A list of condtional styles that have been set.
*/
QList<QgsConditionalStyle> getConditionalStyles();

/**
* @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()
*/
QList<QgsConditionalStyle> matchingConditionalStyles( 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()
*/
QgsConditionalStyle matchingConditionalStyle( QVariant value, QgsFeature* feature );
QList<QgsConditionalStyle> conditionalStyles();

/** Reads field ui properties specific state from Dom node.
*/
@@ -2038,6 +2038,16 @@ QgsFieldUIProperties QgsVectorLayer::fieldUIProperties( QString fieldName )
return QgsFieldUIProperties();
}

QList<QgsConditionalStyle> QgsVectorLayer::rowStyles()
{
return mRowStyles;
}

void QgsVectorLayer::setRowStyles( QList<QgsConditionalStyle> styles )
{
mRowStyles = styles;
}

void QgsVectorLayer::setFieldUIProperties( QString fieldName, QgsFieldUIProperties props )
{
mFieldProperties.insert( fieldName, props );
@@ -27,6 +27,7 @@

#include "qgis.h"
#include "qgsmaplayer.h"
#include "qgsconditionalstyle.h"
#include "qgsfeature.h"
#include "qgsfeatureiterator.h"
#include "qgseditorwidgetconfig.h"
@@ -1764,6 +1765,16 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
QgsFieldUIProperties fieldUIProperties( QString fieldName );

QList<QgsConditionalStyle> rowStyles();

/**
* @brief Set the conditional styles that apply to full rows of data in the attribute table.
* Each row will check be checked against each rule.
* @param styles The styles to assign to all the rows
* @note added in QGIS 2.12
*/
void setRowStyles( QList<QgsConditionalStyle> styles );

/**
* @brief Set the the field UI properties for a given field.
* @param fieldName The field name.
@@ -2026,6 +2037,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
private: // Private attributes

QHash<QString, QgsFieldUIProperties> mFieldProperties;
QList<QgsConditionalStyle> mRowStyles;

/** Pointer to data provider derived from the abastract base class QgsDataProvider */
QgsVectorDataProvider *mDataProvider;
@@ -583,19 +583,38 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
return mWidgetFactories[ index.column()]->representValue( layer(), fieldId, mWidgetConfigs[ index.column()], mAttributeWidgetCaches[ index.column()], val );
}

QgsFieldUIProperties props = layer()->fieldUIProperties( field.name() );
QList<QgsConditionalStyle> styles = props.matchingConditionalStyles( val, &mFeat );
QgsConditionalStyle style;
foreach ( QgsConditionalStyle s, styles )
{
style.setFont( s.font() );
if ( s.backgroundColor().isValid() && s.backgroundColor().alpha() != 0 )
style.setBackgroundColor( s.backgroundColor() );
if ( s.textColor().isValid() && s.textColor().alpha() != 0 )
style.setTextColor( s.textColor() );
if ( s.symbol() )
style.setSymbol( s.symbol() );
QList<QgsConditionalStyle> styles;
if ( mRowStylesMap.contains( index.row() ) )
{
styles = mRowStylesMap[index.row()];
}
else
{
styles = QgsConditionalStyle::matchingConditionalStyles( layer()->rowStyles(), QVariant(), &mFeat );
mRowStylesMap.insert( index.row(), styles );

}
QgsConditionalStyle style = QgsConditionalStyle::stackStyles( styles );
// TODO Extract me out
foreach ( QgsConditionalStyle style, styles )
{
if ( style.isValid() )
{
if ( role == Qt::BackgroundColorRole && style.backgroundColor().isValid() )
return style.backgroundColor();
if ( role == Qt::TextColorRole && style.textColor().isValid() )
return style.textColor();
if ( role == Qt::DecorationRole )
return style.icon();
if ( role == Qt::FontRole )
return style.font();
}

}

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

if ( style.isValid() )
{
@@ -299,6 +299,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel

QHash<QgsFeatureId, int> mIdRowMap;
QHash<int, QgsFeatureId> mRowIdMap;
mutable QHash<int, QList<QgsConditionalStyle>> mRowStylesMap;

/**
* Gets mFieldCount, mAttributes and mValueMaps
@@ -14,6 +14,8 @@ QgsFieldConditionalFormatWidget::QgsFieldConditionalFormatWidget( QWidget *paren
setupUi( this );
mDeleteButton->hide();
connect( mFieldCombo, SIGNAL( fieldChanged( QString ) ), SLOT( fieldChanged( QString ) ) );
connect( fieldRadio, SIGNAL( clicked() ), SLOT( reloadStyles() ) );
connect( rowRadio, SIGNAL( clicked() ), SLOT( reloadStyles() ) );
connect( mNewButton, SIGNAL( clicked() ), SLOT( addNewRule() ) );
connect( mSaveRule, SIGNAL( clicked() ), SLOT( saveRule() ) );
connect( mCancelButton, SIGNAL( clicked() ), SLOT( cancelRule() ) );
@@ -73,8 +75,7 @@ void QgsFieldConditionalFormatWidget::setLayer( QgsVectorLayer *theLayer )

void QgsFieldConditionalFormatWidget::ruleClicked( QModelIndex index )
{
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
QList<QgsConditionalStyle> styles = props.getConditionalStyles();
QList<QgsConditionalStyle> styles = getStyles();
QgsConditionalStyle style = styles.at( index.row() );
editStyle( index.row(), style );
}
@@ -116,13 +117,36 @@ void QgsFieldConditionalFormatWidget::editStyle( int editIndex, QgsConditionalSt
mFontFamilyCmbBx->setFont( font );
}

QList<QgsConditionalStyle> QgsFieldConditionalFormatWidget::getStyles()
{
QList<QgsConditionalStyle> styles;
if ( fieldRadio->isChecked() )
{
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
styles = props.conditionalStyles();
}
if ( rowRadio->isChecked() )
{
styles = mLayer->rowStyles();
}
return styles;
}

void QgsFieldConditionalFormatWidget::deleteRule()
{
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
QList<QgsConditionalStyle> styles = props.getConditionalStyles();
QList<QgsConditionalStyle> styles = getStyles();
styles.removeAt( mEditIndex );
props.setConditionalStyles( styles );
mLayer->setFieldUIProperties( mFieldCombo->currentField(), props );
if ( fieldRadio->isChecked() )
{
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
props.setConditionalStyles( styles );
mLayer->setFieldUIProperties( mFieldCombo->currentField(), props );
}
if ( rowRadio->isChecked() )
{
mLayer->setRowStyles( styles );
}

pages->setCurrentIndex( 0 );
reloadStyles();
emit rulesUpdated( mFieldCombo->currentField() );
@@ -144,7 +168,11 @@ void QgsFieldConditionalFormatWidget::addNewRule()
void QgsFieldConditionalFormatWidget::reset()
{
mSymbol = 0;
mRuleEdit->setText( "@value " );
mRuleEdit->clear();
if ( fieldRadio->isChecked() )
{
mRuleEdit->setText( "@value " );
}
btnBackgroundColor->setColor( QColor() );
btnTextColor->setColor( QColor() );
mDefault1->toggle();
@@ -162,8 +190,17 @@ void QgsFieldConditionalFormatWidget::reset()

void QgsFieldConditionalFormatWidget::saveRule()
{
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
QList<QgsConditionalStyle> styles = props.getConditionalStyles();
QList<QgsConditionalStyle> styles;
if ( fieldRadio->isChecked() )
{
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
styles = props.conditionalStyles();
}
if ( rowRadio->isChecked() )
{
styles = mLayer->rowStyles();
}

QgsConditionalStyle style = QgsConditionalStyle();

style.setRule( mRuleEdit->text() );
@@ -195,8 +232,16 @@ void QgsFieldConditionalFormatWidget::saveRule()
{
styles.append( style );
}
props.setConditionalStyles( styles );
mLayer->setFieldUIProperties( mFieldCombo->currentField(), props );
if ( fieldRadio->isChecked() )
{
QgsFieldUIProperties props = QgsFieldUIProperties();
props.setConditionalStyles( styles );
mLayer->setFieldUIProperties( mFieldCombo->currentField(), props );
}
if ( rowRadio->isChecked() )
{
mLayer->setRowStyles( styles );
}
pages->setCurrentIndex( 0 );
reloadStyles();
emit rulesUpdated( mFieldCombo->currentField() );
@@ -206,9 +251,8 @@ void QgsFieldConditionalFormatWidget::saveRule()
void QgsFieldConditionalFormatWidget::reloadStyles()
{
mModel->clear();
QgsFieldUIProperties props = mLayer->fieldUIProperties( mFieldCombo->currentField() );
QList<QgsConditionalStyle> styles = props.getConditionalStyles();
foreach ( QgsConditionalStyle style, styles )

foreach ( QgsConditionalStyle style, getStyles() )
{
QStandardItem* item = new QStandardItem( style.rule() );
item->setIcon( QIcon( style.renderPreview() ) );
@@ -76,6 +76,8 @@ class GUI_EXPORT QgsFieldConditionalFormatWidget : public QWidget, private Ui::Q
QStandardItemModel* mModel;
QgsSymbolV2* mSymbol;

QList<QgsConditionalStyle> getStyles();

private slots:
void setExpression();
void updateIcon();

0 comments on commit 323977f

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