void QgsPropertyOverrideButton::showAssistant()
{
  //first step - try to convert any existing expression to a transformer if one doesn't
  //already exist
  if ( !mProperty.transformer() )
  {
    ( void )mProperty.convertToTransformer();
  }

  QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
  QgsPropertyAssistantWidget *widget = new QgsPropertyAssistantWidget( panel, mDefinition, mProperty, mVectorLayer );
  widget->registerExpressionContextGenerator( mExpressionContextGenerator );
  widget->setSymbol( mSymbol ); // we only show legend preview in dialog version

  if ( panel && panel->dockMode() )
  {
    connect( widget, &QgsPropertyAssistantWidget::widgetChanged, this, [this, widget]
    {
      widget->updateProperty( this->mProperty );
      mExpressionString = this->mProperty.asExpression();
      mFieldName = this->mProperty.field();
      updateSiblingWidgets( isActive() );
      this->emit changed();
    } );

    connect( widget, &QgsPropertyAssistantWidget::panelAccepted, this, [ = ] { updateGui(); } );

    panel->openPanel( widget );
    return;
  }
  else
  {
    // Show the dialog version if not in a panel
    QDialog *dlg = new QDialog( this );
    QString key = QStringLiteral( "/UI/paneldialog/%1" ).arg( widget->panelTitle() );
    QgsSettings settings;
    dlg->restoreGeometry( settings.value( key ).toByteArray() );
    dlg->setWindowTitle( widget->panelTitle() );
    dlg->setLayout( new QVBoxLayout() );
    dlg->layout()->addWidget( widget );
    QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
    connect( buttonBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept );
    connect( buttonBox, &QDialogButtonBox::rejected, dlg, &QDialog::reject );
    connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsPropertyOverrideButton::showHelp );
    dlg->layout()->addWidget( buttonBox );

    if ( dlg->exec() == QDialog::Accepted )
    {
      widget->updateProperty( mProperty );
      mExpressionString = mProperty.asExpression();
      mFieldName = mProperty.field();
      widget->acceptPanel();
      updateSiblingWidgets( isActive() );
      updateGui();

      emit changed();
    }
    settings.setValue( key, dlg->saveGeometry() );
  }
}
Пример #2
0
void QgsPropertyOverrideButton::setToProperty( const QgsProperty &property )
{
  if ( property )
  {
    switch ( property.propertyType() )
    {
      case QgsProperty::StaticProperty:
      case QgsProperty::InvalidProperty:
        break;
      case QgsProperty::FieldBasedProperty:
      {
        mFieldName = property.field();
        break;
      }
      case QgsProperty::ExpressionBasedProperty:
      {
        mExpressionString = property.expressionString();
        break;
      }
    }
  }
  else
  {
    mFieldName.clear();
    mExpressionString.clear();
  }
  mProperty = property;
  setActive( mProperty && mProperty.isActive() );
  updateSiblingWidgets( isActive() );
  updateGui();
}
Пример #3
0
void QgsPropertyOverrideButton::registerExpressionWidget( QWidget *widget )
{
  Q_FOREACH ( const SiblingWidget &sw, mSiblingWidgets )
  {
    if ( widget == sw.mWidgetPointer.data() && sw.mSiblingType == SiblingExpressionText )
      return;
  }
  mSiblingWidgets.append( SiblingWidget( QPointer<QWidget>( widget ), SiblingExpressionText ) );
  updateSiblingWidgets( isActive() );
}
Пример #4
0
void QgsPropertyOverrideButton::registerVisibleWidget( QWidget *widget, bool natural )
{
  Q_FOREACH ( const SiblingWidget &sw, mSiblingWidgets )
  {
    if ( widget == sw.mWidgetPointer.data() && sw.mSiblingType == SiblingVisibility )
      return;
  }
  mSiblingWidgets.append( SiblingWidget( QPointer<QWidget>( widget ), SiblingVisibility, natural ) );
  updateSiblingWidgets( isActive() );
}
void QgsPropertyOverrideButton::init( int propertyKey, const QgsProperty &property, const QgsPropertyDefinition &definition, const QgsVectorLayer *layer, bool auxiliaryStorageEnabled )
{
  mVectorLayer = layer;
  mAuxiliaryStorageEnabled = auxiliaryStorageEnabled;
  setToProperty( property );
  mPropertyKey = propertyKey;

  mDefinition = definition;
  mDataTypes = mDefinition.dataType();

  mInputDescription = mDefinition.helpText();
  mFullDescription.clear();
  mUsageInfo.clear();

  // set up data types string
  mDataTypesString.clear();

  QStringList ts;
  switch ( mDataTypes )
  {
    case QgsPropertyDefinition::DataTypeBoolean:
      ts << tr( "boolean" );
      FALLTHROUGH

    case QgsPropertyDefinition::DataTypeNumeric:
      ts << tr( "int" );
      ts << tr( "double" );
      FALLTHROUGH

    case QgsPropertyDefinition::DataTypeString:
      ts << tr( "string" );
      break;
  }

  if ( !ts.isEmpty() )
  {
    mDataTypesString = ts.join( QStringLiteral( ", " ) );
    mActionDataTypes->setText( tr( "Field type: " ) + mDataTypesString );
  }

  updateFieldLists();
  updateGui();
  updateSiblingWidgets( isActive() );
}
void QgsPropertyOverrideButton::registerLinkedWidget( QWidget *widget )
{
  for ( const SiblingWidget &sw : qgis::as_const( mSiblingWidgets ) )
  {
    if ( widget == sw.mWidgetPointer.data() && sw.mSiblingType == SiblingLinkedWidget )
      return;
  }
  mSiblingWidgets.append( SiblingWidget( QPointer<QWidget>( widget ), SiblingLinkedWidget ) );

  if ( QgsColorButton *cb = qobject_cast< QgsColorButton * >( widget ) )
  {
    connect( cb, &QgsColorButton::unlinked, this, [ = ]
    {
      setActive( false );
      updateGui();
    } );
  }

  updateSiblingWidgets( isActive() );
}
void QgsPropertyOverrideButton::showExpressionDialog()
{
  QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : QgsExpressionContext();

  // build sensible initial expression text - see https://issues.qgis.org/issues/18638
  QString currentExpression = ( mProperty.propertyType() == QgsProperty::StaticProperty && !mProperty.staticValue().isValid() ) ? QString()
                              : mProperty.asExpression();

  QgsExpressionBuilderDialog d( const_cast<QgsVectorLayer *>( mVectorLayer ), currentExpression, this, QStringLiteral( "generic" ), context );
  d.setExpectedOutputFormat( mInputDescription );
  if ( d.exec() == QDialog::Accepted )
  {
    mExpressionString = d.expressionText().trimmed();
    mProperty.setExpressionString( mExpressionString );
    mProperty.setTransformer( nullptr );
    setActivePrivate( !mExpressionString.isEmpty() );
    updateSiblingWidgets( isActive() );
    updateGui();
    emit changed();
  }
  activateWindow(); // reset focus to parent window
}
Пример #8
0
void QgsPropertyOverrideButton::menuActionTriggered( QAction *action )
{
  if ( action == mActionActive )
  {
    setActivePrivate( mActionActive->data().toBool() );
    updateGui();
    emit changed();
  }
  else if ( action == mActionDescription )
  {
    showDescriptionDialog();
  }
  else if ( action == mActionExpDialog )
  {
    showExpressionDialog();
  }
  else if ( action == mActionExpression )
  {
    mProperty.setExpressionString( mExpressionString );
    mProperty.setTransformer( nullptr );
    setActivePrivate( true );
    updateSiblingWidgets( isActive() );
    updateGui();
    emit changed();
  }
  else if ( action == mActionCopyExpr )
  {
    QApplication::clipboard()->setText( mExpressionString );
  }
  else if ( action == mActionPasteExpr )
  {
    QString exprString = QApplication::clipboard()->text();
    if ( !exprString.isEmpty() )
    {
      mExpressionString = exprString;
      mProperty.setExpressionString( mExpressionString );
      mProperty.setTransformer( nullptr );
      setActivePrivate( true );
      updateSiblingWidgets( isActive() );
      updateGui();
      emit changed();
    }
  }
  else if ( action == mActionClearExpr )
  {
    setActivePrivate( false );
    mProperty.setStaticValue( QVariant() );
    mProperty.setTransformer( nullptr );
    mExpressionString.clear();
    updateSiblingWidgets( isActive() );
    updateGui();
    emit changed();
  }
  else if ( action == mActionAssistant )
  {
    showAssistant();
  }
  else if ( action == mActionCreateAuxiliaryField )
  {
    emit createAuxiliaryField();
  }
  else if ( mFieldsMenu->actions().contains( action ) )  // a field name clicked
  {
    if ( action->isEnabled() )
    {
      if ( mFieldName != action->text() )
      {
        mFieldName = action->data().toString();
      }
      mProperty.setField( mFieldName );
      mProperty.setTransformer( nullptr );
      setActivePrivate( true );
      updateSiblingWidgets( isActive() );
      updateGui();
      emit changed();
    }
  }
  else if ( mVariablesMenu->actions().contains( action ) )  // a variable name clicked
  {
    if ( mExpressionString != action->text().prepend( "@" ) )
    {
      mExpressionString = action->data().toString().prepend( "@" );
    }
    mProperty.setExpressionString( mExpressionString );
    mProperty.setTransformer( nullptr );
    setActivePrivate( true );
    updateSiblingWidgets( isActive() );
    updateGui();
    emit changed();
  }
}