void QgsDataDefinedButton::updateDataDefined( QgsDataDefined *dd ) const { if ( !dd ) return; dd->setActive( isActive() ); dd->setExpressionString( getExpression() ); dd->setField( getField() ); dd->setUseExpression( useExpression() ); }
void QgsDataDefinedButton::updateGui() { QString oldDef = mCurrentDefinition; QString newDef( "" ); bool hasExp = !getExpression().isEmpty(); bool hasField = !getField().isEmpty(); if ( useExpression() && !hasExp ) { setActive( false ); setUseExpression( false ); } else if ( !useExpression() && !hasField ) { setActive( false ); } QIcon icon = mIconDataDefine; QString deftip = tr( "undefined" ); if ( useExpression() && hasExp ) { icon = isActive() ? mIconDataDefineExpressionOn : mIconDataDefineExpression; newDef = deftip = getExpression(); QgsExpression exp( getExpression() ); if ( exp.hasParserError() ) { setActive( false ); icon = mIconDataDefineExpressionError; deftip = tr( "Parse error: %1" ).arg( exp.parserErrorString() ); newDef = ""; } } else if ( !useExpression() && hasField ) { icon = isActive() ? mIconDataDefineOn : mIconDataDefine; newDef = deftip = getField(); if ( !mFieldNameList.contains( getField() ) ) { setActive( false ); icon = mIconDataDefineError; deftip = tr( "'%1' field missing" ).arg( getField() ); newDef = ""; } } setIcon( icon ); // update and emit current definition if ( newDef != oldDef ) { mCurrentDefinition = newDef; emit dataDefinedChanged( mCurrentDefinition ); } // build full description for tool tip and popup dialog mFullDescription = tr( "<b><u>Data defined override</u></b><br>" ); mFullDescription += tr( "<b>Active: </b>%1 <i>(ctrl|right-click toggles)</i><br>" ).arg( isActive() ? tr( "yes" ) : tr( "no" ) ); if ( !mUsageInfo.isEmpty() ) { mFullDescription += tr( "<b>Usage:</b><br>%1<br>" ).arg( mUsageInfo ); } if ( !mInputDescription.isEmpty() ) { mFullDescription += tr( "<b>Expected input:</b><br>%1<br>" ).arg( mInputDescription ); } if ( !mDataTypesString.isEmpty() ) { mFullDescription += tr( "<b>Valid input types:</b><br>%1<br>" ).arg( mDataTypesString ); } QString deftype( "" ); if ( deftip != tr( "undefined" ) ) { deftype = QString( " (%1)" ).arg( useExpression() ? tr( "expression" ) : tr( "field" ) ); } // truncate long expressions, or tool tip may be too wide for screen if ( deftip.length() > 75 ) { deftip.truncate( 75 ); deftip.append( "..." ); } mFullDescription += tr( "<b>Current definition %1:</b><br>%2" ).arg( deftype ).arg( deftip ); setToolTip( mFullDescription ); }
void QgsDataDefinedButton::menuActionTriggered( QAction* action ) { if ( action == mActionActive ) { setActive( mActionActive->data().toBool() ); updateGui(); } else if ( action == mActionDescription ) { showDescriptionDialog(); } else if ( action == mActionExpDialog ) { showExpressionDialog(); } else if ( action == mActionExpression ) { setUseExpression( true ); setActive( true ); updateGui(); } else if ( action == mActionCopyExpr ) { QApplication::clipboard()->setText( getExpression() ); } else if ( action == mActionPasteExpr ) { QString exprString = QApplication::clipboard()->text(); if ( !exprString.isEmpty() ) { setExpression( exprString ); setUseExpression( true ); setActive( true ); updateGui(); } } else if ( action == mActionClearExpr ) { // only deactivate if defined expression is being used if ( isActive() && useExpression() ) { setUseExpression( false ); setActive( false ); } setExpression( QString( "" ) ); updateGui(); } else if ( mFieldsMenu->actions().contains( action ) ) // a field name clicked { if ( action->isEnabled() ) { if ( getField() != action->text() ) { setField( action->data().toString() ); } setUseExpression( false ); setActive( true ); updateGui(); } } }
void QgsDataDefinedButton::aboutToShowMenu() { mDefineMenu->clear(); bool hasExp = !getExpression().isEmpty(); bool hasField = !getField().isEmpty(); QString ddTitle = tr( "Data defined override" ); QAction* ddTitleAct = mDefineMenu->addAction( ddTitle ); QFont titlefont = ddTitleAct->font(); titlefont.setItalic( true ); ddTitleAct->setFont( titlefont ); ddTitleAct->setEnabled( false ); bool addActiveAction = false; if ( useExpression() && hasExp ) { QgsExpression exp( getExpression() ); // whether expression is parse-able addActiveAction = !exp.hasParserError(); } else if ( !useExpression() && hasField ) { // whether field exists addActiveAction = mFieldNameList.contains( getField() ); } if ( addActiveAction ) { ddTitleAct->setText( ddTitle + " (" + ( useExpression() ? tr( "expression" ) : tr( "field" ) ) + ")" ); mDefineMenu->addAction( mActionActive ); mActionActive->setText( isActive() ? tr( "Deactivate" ) : tr( "Activate" ) ); mActionActive->setData( QVariant( isActive() ? false : true ) ); } if ( !mFullDescription.isEmpty() ) { mDefineMenu->addAction( mActionDescription ); } mDefineMenu->addSeparator(); if ( !mDataTypesString.isEmpty() ) { QAction* fieldTitleAct = mDefineMenu->addAction( tr( "Attribute field" ) ); fieldTitleAct->setFont( titlefont ); fieldTitleAct->setEnabled( false ); mDefineMenu->addAction( mActionDataTypes ); mFieldsMenu->clear(); if ( mFieldNameList.size() > 0 ) { for ( int j = 0; j < mFieldNameList.count(); ++j ) { QString fldname = mFieldNameList.at( j ); QAction* act = mFieldsMenu->addAction( fldname + " (" + mFieldTypeList.at( j ) + ")" ); act->setData( QVariant( fldname ) ); if ( getField() == fldname ) { act->setCheckable( true ); act->setChecked( !useExpression() ); } } } else { QAction* act = mFieldsMenu->addAction( tr( "No matching field types found" ) ); act->setEnabled( false ); } mDefineMenu->addSeparator(); } QAction* exprTitleAct = mDefineMenu->addAction( tr( "Expression" ) ); exprTitleAct->setFont( titlefont ); exprTitleAct->setEnabled( false ); if ( hasExp ) { QString expString = getExpression(); if ( expString.length() > 35 ) { expString.truncate( 35 ); expString.append( "..." ); } expString.prepend( tr( "Current: " ) ); if ( !mActionExpression ) { mActionExpression = new QAction( expString, this ); mActionExpression->setCheckable( true ); } else { mActionExpression->setText( expString ); } mDefineMenu->addAction( mActionExpression ); mActionExpression->setChecked( useExpression() ); mDefineMenu->addAction( mActionExpDialog ); mDefineMenu->addAction( mActionCopyExpr ); mDefineMenu->addAction( mActionPasteExpr ); mDefineMenu->addAction( mActionClearExpr ); } else { mDefineMenu->addAction( mActionExpDialog ); mDefineMenu->addAction( mActionPasteExpr ); } }
void QgsDataDefinedButton::aboutToShowMenu() { mDefineMenu->clear(); bool hasExp = !getExpression().isEmpty(); bool hasField = !getField().isEmpty(); QString ddTitle = tr( "Data defined override" ); QAction* ddTitleAct = mDefineMenu->addAction( ddTitle ); QFont titlefont = ddTitleAct->font(); titlefont.setItalic( true ); ddTitleAct->setFont( titlefont ); ddTitleAct->setEnabled( false ); bool addActiveAction = false; if ( useExpression() && hasExp ) { QgsExpression exp( getExpression() ); // whether expression is parse-able addActiveAction = !exp.hasParserError(); } else if ( !useExpression() && hasField ) { // whether field exists addActiveAction = mFieldNameList.contains( getField() ); } if ( addActiveAction ) { ddTitleAct->setText( ddTitle + " (" + ( useExpression() ? tr( "expression" ) : tr( "field" ) ) + ')' ); mDefineMenu->addAction( mActionActive ); mActionActive->setText( isActive() ? tr( "Deactivate" ) : tr( "Activate" ) ); mActionActive->setData( QVariant( isActive() ? false : true ) ); } if ( !mFullDescription.isEmpty() ) { mDefineMenu->addAction( mActionDescription ); } mDefineMenu->addSeparator(); bool fieldActive = false; if ( !mDataTypesString.isEmpty() ) { QAction* fieldTitleAct = mDefineMenu->addAction( tr( "Attribute field" ) ); fieldTitleAct->setFont( titlefont ); fieldTitleAct->setEnabled( false ); mDefineMenu->addAction( mActionDataTypes ); mFieldsMenu->clear(); if ( mFieldNameList.size() > 0 ) { for ( int j = 0; j < mFieldNameList.count(); ++j ) { QString fldname = mFieldNameList.at( j ); QAction* act = mFieldsMenu->addAction( fldname + " (" + mFieldTypeList.at( j ) + ')' ); act->setData( QVariant( fldname ) ); if ( getField() == fldname ) { act->setCheckable( true ); act->setChecked( !useExpression() ); fieldActive = !useExpression(); } } } else { QAction* act = mFieldsMenu->addAction( tr( "No matching field types found" ) ); act->setEnabled( false ); } mDefineMenu->addSeparator(); } mFieldsMenu->menuAction()->setCheckable( true ); mFieldsMenu->menuAction()->setChecked( fieldActive ); QAction* exprTitleAct = mDefineMenu->addAction( tr( "Expression" ) ); exprTitleAct->setFont( titlefont ); exprTitleAct->setEnabled( false ); mVariablesMenu->clear(); bool variableActive = false; if ( mExpressionContextCallback ) { QgsExpressionContext context = mExpressionContextCallback( mExpressionContextCallbackContext ); QStringList variables = context.variableNames(); Q_FOREACH ( const QString& variable, variables ) { if ( context.isReadOnly( variable ) ) //only want to show user-set variables continue; if ( variable.startsWith( '_' ) ) //no hidden variables continue; QAction* act = mVariablesMenu->addAction( variable ); act->setData( QVariant( variable ) ); if ( useExpression() && hasExp && getExpression() == '@' + variable ) { act->setCheckable( true ); act->setChecked( true ); variableActive = true; } } } if ( mVariablesMenu->actions().isEmpty() ) { QAction* act = mVariablesMenu->addAction( tr( "No variables set" ) ); act->setEnabled( false ); } mDefineMenu->addAction( mActionVariables ); mVariablesMenu->menuAction()->setCheckable( true ); mVariablesMenu->menuAction()->setChecked( variableActive ); if ( hasExp ) { QString expString = getExpression(); if ( expString.length() > 35 ) { expString.truncate( 35 ); expString.append( "..." ); } expString.prepend( tr( "Current: " ) ); if ( !mActionExpression ) { mActionExpression = new QAction( expString, this ); mActionExpression->setCheckable( true ); } else { mActionExpression->setText( expString ); } mDefineMenu->addAction( mActionExpression ); mActionExpression->setChecked( useExpression() && !variableActive ); mDefineMenu->addAction( mActionExpDialog ); mDefineMenu->addAction( mActionCopyExpr ); mDefineMenu->addAction( mActionPasteExpr ); mDefineMenu->addAction( mActionClearExpr ); } else { mDefineMenu->addAction( mActionExpDialog ); mDefineMenu->addAction( mActionPasteExpr ); } if ( mAssistant.data() ) { mDefineMenu->addSeparator(); mDefineMenu->addAction( mActionAssistant ); } }