|
| 1 | +#include <QPainter> |
| 2 | + |
| 3 | +#include "qgsconditionalstyle.h" |
| 4 | +#include "qgsexpression.h" |
| 5 | +#include "qgsfontutils.h" |
| 6 | +#include "qgssymbollayerv2utils.h" |
| 7 | +#include "qgsmarkersymbollayerv2.h" |
| 8 | + |
| 9 | +QgsConditionalStyle::QgsConditionalStyle() |
| 10 | + : mValid( false ) |
| 11 | + , mSymbol( 0 ) |
| 12 | +{} |
| 13 | + |
| 14 | +QgsConditionalStyle::QgsConditionalStyle( QString rule ) |
| 15 | + : mValid( false ) |
| 16 | + , mSymbol( 0 ) |
| 17 | +{ |
| 18 | + setRule( rule ); |
| 19 | +} |
| 20 | + |
| 21 | +QgsConditionalStyle::QgsConditionalStyle( const QgsConditionalStyle &other ) |
| 22 | + : mValid( other.mValid ) |
| 23 | + , mRule( other.mRule ) |
| 24 | + , mFont( other.mFont ) |
| 25 | + , mBackColor( other.mBackColor ) |
| 26 | + , mTextColor( other.mTextColor ) |
| 27 | + , mIcon( other.mIcon ) |
| 28 | +{ |
| 29 | + if ( other.mSymbol.data() ) |
| 30 | + mSymbol.reset( other.mSymbol->clone() ); |
| 31 | +} |
| 32 | + |
| 33 | +QgsConditionalStyle& QgsConditionalStyle::operator=( const QgsConditionalStyle & other ) |
| 34 | +{ |
| 35 | + mValid = other.mValid; |
| 36 | + mRule = other.mRule; |
| 37 | + mFont = other.mFont; |
| 38 | + mBackColor = other.mBackColor; |
| 39 | + mTextColor = other.mTextColor; |
| 40 | + mIcon = other.mIcon; |
| 41 | + if ( other.mSymbol.data() ) |
| 42 | + { |
| 43 | + mSymbol.reset( other.mSymbol->clone() ); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + mSymbol.reset(); |
| 48 | + } |
| 49 | + return ( *this ); |
| 50 | +} |
| 51 | + |
| 52 | +QgsConditionalStyle::~QgsConditionalStyle() |
| 53 | +{ |
| 54 | +} |
| 55 | + |
| 56 | +void QgsConditionalStyle::setSymbol( QgsSymbolV2* value ) |
| 57 | +{ |
| 58 | + mValid = true; |
| 59 | + if ( value ) |
| 60 | + { |
| 61 | + mSymbol.reset( value->clone() ); |
| 62 | + mIcon = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mSymbol.data(), QSize( 16, 16 ) ); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + mSymbol.reset(); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +bool QgsConditionalStyle::matches( QVariant value, QgsFeature *feature ) |
| 71 | +{ |
| 72 | + // TODO Replace with expression context |
| 73 | + QgsExpression exp( QString( mRule ).replace( "@value", value.toString() ) ); |
| 74 | + if ( feature ) |
| 75 | + { |
| 76 | + return exp.evaluate( feature, *feature->fields() ).toBool(); |
| 77 | + } |
| 78 | + { |
| 79 | + return exp.evaluate().toBool(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +QPixmap QgsConditionalStyle::renderPreview() |
| 84 | +{ |
| 85 | + QPixmap pixmap( 64, 32 ); |
| 86 | + QPainter painter( &pixmap ); |
| 87 | + |
| 88 | + if ( mBackColor.isValid() ) |
| 89 | + painter.setBrush( mBackColor ); |
| 90 | + else |
| 91 | + painter.setBrush( QColor( Qt::white ) ); |
| 92 | + |
| 93 | + QRect rect = QRect( 0, 0, 64, 32 ); |
| 94 | + painter.setPen( Qt::NoPen ); |
| 95 | + painter.drawRect( rect ); |
| 96 | + painter.drawPixmap( 8, 8, icon() ); |
| 97 | + |
| 98 | + if ( mTextColor.isValid() ) |
| 99 | + painter.setPen( mTextColor ); |
| 100 | + else |
| 101 | + painter.setPen( Qt::black ); |
| 102 | + |
| 103 | + painter.setRenderHint( QPainter::Antialiasing ); |
| 104 | + painter.setRenderHint( QPainter::HighQualityAntialiasing ); |
| 105 | + painter.setFont( font() ); |
| 106 | + rect = QRect( 32, 0, 32, 32 ); |
| 107 | + painter.drawText( rect, Qt::AlignCenter, "abc\n123" ); |
| 108 | + painter.end(); |
| 109 | + return pixmap; |
| 110 | +} |
| 111 | + |
| 112 | +bool QgsConditionalStyle::writeXml( QDomNode &node, QDomDocument &doc ) |
| 113 | +{ |
| 114 | + QDomElement stylesel = doc.createElement( "style" ); |
| 115 | + stylesel.setAttribute( "rule", mRule ); |
| 116 | + stylesel.setAttribute( "background_color", mBackColor.name() ); |
| 117 | + stylesel.setAttribute( "text_color", mTextColor.name() ); |
| 118 | + QDomElement labelFontElem = QgsFontUtils::toXmlElement( mFont, doc, "font" ); |
| 119 | + stylesel.appendChild( labelFontElem ); |
| 120 | + if ( ! mSymbol.isNull() ) |
| 121 | + { |
| 122 | + QDomElement symbolElm = QgsSymbolLayerV2Utils::saveSymbol( "icon", mSymbol.data(), doc ); |
| 123 | + stylesel.appendChild( symbolElm ); |
| 124 | + } |
| 125 | + node.appendChild( stylesel ); |
| 126 | + return true; |
| 127 | +} |
| 128 | + |
| 129 | +bool QgsConditionalStyle::readXml( const QDomNode &node ) |
| 130 | +{ |
| 131 | + QDomElement styleElm = node.toElement(); |
| 132 | + setRule( styleElm.attribute( "rule" ) ); |
| 133 | + setBackgroundColor( QColor( styleElm.attribute( "background_color" ) ) ); |
| 134 | + setTextColor( QColor( styleElm.attribute( "text_color" ) ) ); |
| 135 | + QgsFontUtils::setFromXmlChildNode( mFont, styleElm, "font" ); |
| 136 | + QDomElement symbolElm = styleElm.firstChildElement( "symbol" ); |
| 137 | + if ( !symbolElm.isNull() ) |
| 138 | + { |
| 139 | + QgsSymbolV2* symbol = QgsSymbolLayerV2Utils::loadSymbol<QgsMarkerSymbolV2>( symbolElm ); |
| 140 | + setSymbol( symbol ); |
| 141 | + } |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
0 commit comments