Skip to content
Permalink
Browse files

Use combo box for preset

  • Loading branch information
NathanW2 committed Aug 22, 2015
1 parent b355420 commit d75362687f25ec99cfdfd7b6802033df374b788d
@@ -25,6 +25,7 @@ QgsConditionalStyle::QgsConditionalStyle( const QgsConditionalStyle &other )
, mBackColor( other.mBackColor )
, mTextColor( other.mTextColor )
, mIcon( other.mIcon )
, mName( other.mName )
{
if ( other.mSymbol.data() )
mSymbol.reset( other.mSymbol->clone() );
@@ -38,6 +39,7 @@ QgsConditionalStyle& QgsConditionalStyle::operator=( const QgsConditionalStyle &
mBackColor = other.mBackColor;
mTextColor = other.mTextColor;
mIcon = other.mIcon;
mName = other.mName;
if ( other.mSymbol.data() )
{
mSymbol.reset( other.mSymbol->clone() );
@@ -53,6 +55,14 @@ QgsConditionalStyle::~QgsConditionalStyle()
{
}

QString QgsConditionalStyle::displayText() const
{
if ( name().isEmpty() )
return rule();
else
return QString( "%1 \n%2" ).arg( name() ).arg( rule() );
}

void QgsConditionalStyle::setSymbol( QgsSymbolV2* value )
{
mValid = true;
@@ -150,6 +160,7 @@ bool QgsConditionalStyle::writeXml( QDomNode &node, QDomDocument &doc )
{
QDomElement stylesel = doc.createElement( "style" );
stylesel.setAttribute( "rule", mRule );
stylesel.setAttribute( "name", mName );
stylesel.setAttribute( "background_color", mBackColor.name() );
stylesel.setAttribute( "text_color", mTextColor.name() );
QDomElement labelFontElem = QgsFontUtils::toXmlElement( mFont, doc, "font" );
@@ -167,6 +178,7 @@ bool QgsConditionalStyle::readXml( const QDomNode &node )
{
QDomElement styleElm = node.toElement();
setRule( styleElm.attribute( "rule" ) );
setName( styleElm.attribute( "name" ) );
setBackgroundColor( QColor( styleElm.attribute( "background_color" ) ) );
setTextColor( QColor( styleElm.attribute( "text_color" ) ) );
QgsFontUtils::setFromXmlChildNode( mFont, styleElm, "font" );
@@ -37,6 +37,12 @@ class CORE_EXPORT 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 ) { mName = value; mValid = true; }

/**
* @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
@@ -68,6 +74,18 @@ class CORE_EXPORT 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 { return mName; }

/**
* @brief The icon set for style generated from the set symbol
* @return A QPixmap that was set for the icon using the symbol
@@ -143,6 +161,7 @@ class CORE_EXPORT QgsConditionalStyle
private:

bool mValid;
QString mName;
QString mRule;
QScopedPointer<QgsSymbolV2> mSymbol;
QFont mFont;
@@ -21,15 +21,19 @@ QgsFieldConditionalFormatWidget::QgsFieldConditionalFormatWidget( QWidget *paren
connect( mCancelButton, SIGNAL( clicked() ), SLOT( cancelRule() ) );
connect( mDeleteButton, SIGNAL( clicked() ), SLOT( deleteRule() ) );
connect( listView, SIGNAL( clicked( QModelIndex ) ), SLOT( ruleClicked( QModelIndex ) ) );
connect( mDefaultButtons , SIGNAL( buttonPressed( QAbstractButton* ) ), SLOT( defaultPressed( QAbstractButton* ) ) );
connect( btnChangeIcon , SIGNAL( clicked() ), SLOT( updateIcon() ) );
connect( btnBuildExpression , SIGNAL( clicked() ), SLOT( setExpression() ) );
connect( mPresetsList , SIGNAL( currentIndexChanged( int ) ), SLOT( presetSet( int ) ) );
btnBackgroundColor->setAllowAlpha( true );
btnBackgroundColor->setShowNoColor( true );
btnTextColor->setAllowAlpha( true );
btnTextColor->setShowNoColor( true );
mModel = new QStandardItemModel();
mPresetsModel = new QStandardItemModel();
listView->setModel( mModel );
mPresetsList->setModel( mPresetsModel );

setPresets( defaultPresets() );
}

void QgsFieldConditionalFormatWidget::updateIcon()
@@ -58,12 +62,13 @@ void QgsFieldConditionalFormatWidget::setExpression()
}
}

void QgsFieldConditionalFormatWidget::defaultPressed( QAbstractButton *button )
void QgsFieldConditionalFormatWidget::presetSet( int index )
{
QColor backColor = button->property( "backColor" ).value<QColor>();
QColor fontColor = button->property( "fontColor" ).value<QColor>();
btnBackgroundColor->setColor( backColor );
btnTextColor->setColor( fontColor );
if ( index == -1 || mPresets.count() == 0 )
return;

QgsConditionalStyle style = mPresets.at( index );
setFormattingFromStyle( style );
}

void QgsFieldConditionalFormatWidget::setLayer( QgsVectorLayer *theLayer )
@@ -85,9 +90,18 @@ void QgsFieldConditionalFormatWidget::editStyle( int editIndex, QgsConditionalSt
pages->setCurrentIndex( 1 );
mEditIndex = editIndex;
mEditing = true;
mDeleteButton->show();
loadStyle( style );
}

void QgsFieldConditionalFormatWidget::loadStyle( QgsConditionalStyle style )
{
mRuleEdit->setText( style.rule() );
mDeleteButton->show();
mNameEdit->setText( style.name() );
setFormattingFromStyle( style );
}
void QgsFieldConditionalFormatWidget::setFormattingFromStyle( QgsConditionalStyle style )
{
btnBackgroundColor->setColor( style.backgroundColor() );
btnTextColor->setColor( style.textColor() );
if ( !style.icon().isNull() )
@@ -168,15 +182,15 @@ void QgsFieldConditionalFormatWidget::addNewRule()
void QgsFieldConditionalFormatWidget::reset()
{
mSymbol = 0;
mNameEdit->clear();
mRuleEdit->clear();
if ( fieldRadio->isChecked() )
{
mRuleEdit->setText( "@value " );
}
btnBackgroundColor->setColor( QColor() );
btnTextColor->setColor( QColor() );
mDefault1->toggle();
defaultPressed( mDefault1 );
mPresetsList->setCurrentIndex( 0 );
mDeleteButton->hide();
mEditing = false;
checkIcon->setChecked( false );
@@ -188,6 +202,54 @@ void QgsFieldConditionalFormatWidget::reset()
mFontUnderlineBtn->setChecked( false );
}


void QgsFieldConditionalFormatWidget::setPresets( QList<QgsConditionalStyle> styles )
{
mPresets.clear();
mPresetsModel->clear();
foreach ( QgsConditionalStyle style, styles )
{
if ( style.isValid() )
{
QStandardItem* item = new QStandardItem( "abc - 123" );
if ( style.backgroundColor().isValid() )
item->setBackground( style.backgroundColor() );
if ( style.textColor().isValid() )
item->setForeground( style.textColor() );
if ( style.symbol() )
item->setIcon( style.icon() );
item->setFont( style.font() );
mPresetsModel->appendRow( item );
mPresets.append( style );
}
}
mPresetsList->setCurrentIndex( 0 );
}

QList<QgsConditionalStyle> QgsFieldConditionalFormatWidget::defaultPresets() const
{
QList<QgsConditionalStyle> styles;
QgsConditionalStyle style = QgsConditionalStyle();
style.setBackgroundColor( QColor( 154, 216, 113 ) );
styles.append( style );
style = QgsConditionalStyle();
style.setBackgroundColor( QColor( 251, 193, 78 ) );
styles.append( style );
style = QgsConditionalStyle();
style.setBackgroundColor( QColor( 251, 154, 153 ) );
styles.append( style );
style = QgsConditionalStyle();
style.setTextColor( QColor( 154, 216, 113 ) );
styles.append( style );
style = QgsConditionalStyle();
style.setTextColor( QColor( 251, 193, 78 ) );
styles.append( style );
style = QgsConditionalStyle();
style.setTextColor( QColor( 251, 154, 153 ) );
styles.append( style );
return styles;
}

void QgsFieldConditionalFormatWidget::saveRule()
{
QList<QgsConditionalStyle> styles;
@@ -204,6 +266,7 @@ void QgsFieldConditionalFormatWidget::saveRule()
QgsConditionalStyle style = QgsConditionalStyle();

style.setRule( mRuleEdit->text() );
style.setName( mNameEdit->text() );

QColor backColor = btnBackgroundColor->color();
QColor fontColor = btnTextColor->color();
@@ -254,7 +317,7 @@ void QgsFieldConditionalFormatWidget::reloadStyles()

foreach ( QgsConditionalStyle style, getStyles() )
{
QStandardItem* item = new QStandardItem( style.rule() );
QStandardItem* item = new QStandardItem( style.displayText() );
item->setIcon( QIcon( style.renderPreview() ) );
mModel->appendRow( item );
}
@@ -56,10 +56,19 @@ class GUI_EXPORT QgsFieldConditionalFormatWidget : public QWidget, private Ui::Q
*/
void editStyle( int index, QgsConditionalStyle style );

/**
* @param style initial conditional styling options
*/
void loadStyle( QgsConditionalStyle style );

/** Resets the formatting options to their default state.
*/
void reset();

void setPresets( QList<QgsConditionalStyle> styles );

QList<QgsConditionalStyle> defaultPresets() const;

signals:

/** Emitted when the conditional styling rules are updated.
@@ -74,14 +83,18 @@ class GUI_EXPORT QgsFieldConditionalFormatWidget : public QWidget, private Ui::Q
int mEditIndex;
bool mEditing;
QStandardItemModel* mModel;
QStandardItemModel* mPresetsModel;
QgsSymbolV2* mSymbol;
QList<QgsConditionalStyle> mPresets;

QList<QgsConditionalStyle> getStyles();

void setFormattingFromStyle( QgsConditionalStyle style );

private slots:
void setExpression();
void updateIcon();
void defaultPressed( QAbstractButton*button );
void presetSet( int index );
bool isCustomSet();
void ruleClicked( QModelIndex index );
void reloadStyles();

0 comments on commit d753626

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