QWidget *QgsComposerColumnWidthDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
  Q_UNUSED( index );
  Q_UNUSED( option );
  QgsDoubleSpinBox *editor = new QgsDoubleSpinBox( parent );
  editor->setMinimum( 0 );
  editor->setMaximum( 1000 );
  editor->setDecimals( 2 );
  editor->setSuffix( tr( " mm" ) );
  editor->setSpecialValueText( tr( "Automatic" ) );
  editor->setShowClearButton( true );
  return editor;
}
Esempio n. 2
0
void QgsFontButton::prepareMenu()
{
  //we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
  //QgsColorSwatchGridAction is not recalculated by Qt and the swatch grid may not be the correct size
  //for the number of colors shown in the grid. Note that we MUST refresh color swatch grids every time this
  //menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
  mMenu->clear();


  QWidgetAction *sizeAction = new QWidgetAction( mMenu );
  QWidget *sizeWidget = new QWidget();
  QVBoxLayout *sizeLayout = new QVBoxLayout();
  sizeLayout->setMargin( 0 );
  sizeLayout->setContentsMargins( 0, 0, 0, 3 );
  sizeLayout->setSpacing( 2 );

  QString fontHeaderLabel;
  switch ( mMode )
  {
    case ModeTextRenderer:
      fontHeaderLabel = tr( "Font size (%1)" ).arg( QgsUnitTypes::toString( mFormat.sizeUnit() ) );
      break;

    case ModeQFont:
      fontHeaderLabel = tr( "Font size (pt)" );
      break;
  }

  QgsMenuHeader *sizeLabel = new QgsMenuHeader( fontHeaderLabel );
  sizeLayout->addWidget( sizeLabel );

  QgsDoubleSpinBox *sizeSpin = new QgsDoubleSpinBox( nullptr );
  sizeSpin->setDecimals( 4 );
  sizeSpin->setMaximum( 1e+9 );
  sizeSpin->setShowClearButton( false );
  sizeSpin->setValue( mMode == ModeTextRenderer ? mFormat.size() : mFont.pointSizeF() );
  connect( sizeSpin, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ),
           this, [ = ]( double value )
  {
    switch ( mMode )
    {
      case ModeTextRenderer:
        mFormat.setSize( value );
        break;
      case ModeQFont:
        mFont.setPointSizeF( value );
        break;
    }
    updatePreview();
    emit changed();
  } );
  QHBoxLayout *spinLayout = new QHBoxLayout();
  spinLayout->setMargin( 0 );
  spinLayout->setContentsMargins( 4, 0, 4, 0 );
  spinLayout->addWidget( sizeSpin );
  sizeLayout->addLayout( spinLayout );
  sizeWidget->setLayout( sizeLayout );
  sizeAction->setDefaultWidget( sizeWidget );
  sizeWidget->setFocusProxy( sizeSpin );
  sizeWidget->setFocusPolicy( Qt::StrongFocus );
  mMenu->addAction( sizeAction );

  QMenu *recentFontMenu = new QMenu( tr( "Recent fonts" ), mMenu );
  Q_FOREACH ( const QString &family, QgsFontUtils::recentFontFamilies() )
  {
    QAction *fontAction = new QAction( family, recentFontMenu );
    QFont f = fontAction->font();
    f.setFamily( family );
    fontAction->setFont( f );
    fontAction->setToolTip( family );
    recentFontMenu->addAction( fontAction );
    if ( ( mMode == ModeTextRenderer && family == mFormat.font().family() )
         || ( mMode == ModeQFont && family == mFont.family() ) )
    {
      fontAction->setCheckable( true );
      fontAction->setChecked( true );
    }
    auto setFont = [this, family]
    {
      switch ( mMode )
      {
        case ModeTextRenderer:
        {
          QgsTextFormat newFormat = mFormat;
          QFont f = newFormat.font();
          f.setFamily( family );
          newFormat.setFont( f );
          setTextFormat( newFormat );
          QgsFontUtils::addRecentFontFamily( mFormat.font().family() );
          break;
        }
        case ModeQFont:
        {
          QFont font = mFont;
          font.setFamily( family );
          setCurrentFont( font );
          QgsFontUtils::addRecentFontFamily( family );
          break;
        }
      }
    };
    connect( fontAction, &QAction::triggered, this, setFont );
  }