void QgsComposerColumnWidthDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const { int value = index.model()->data( index, Qt::EditRole ).toInt(); QgsDoubleSpinBox *spinBox = static_cast<QgsDoubleSpinBox*>( editor ); spinBox->setValue( value ); }
void TestQgsDoubleSpinBox::clear() { QgsDoubleSpinBox* spinBox = new QgsDoubleSpinBox(); spinBox->setMaximum( 10.0 ); spinBox->setMinimum( 1.0 ); spinBox->setValue( 5.0 ); spinBox->setClearValueMode( QgsDoubleSpinBox::MinimumValue ); spinBox->clear(); QCOMPARE( spinBox->value(), 1.0 ); QCOMPARE( spinBox->clearValue(), 1.0 ); spinBox->setClearValueMode( QgsDoubleSpinBox::MaximumValue ); spinBox->clear(); QCOMPARE( spinBox->value(), 10.0 ); QCOMPARE( spinBox->clearValue(), 10.0 ); spinBox->setClearValue( 7.0 ); spinBox->clear(); QCOMPARE( spinBox->value(), 7.0 ); QCOMPARE( spinBox->clearValue(), 7.0 ); delete spinBox; }
void TestQgsDoubleSpinBox::expression() { QgsDoubleSpinBox* spinBox = new QgsDoubleSpinBox(); spinBox->setMinimum( -10.0 ); spinBox->setMaximum( 10.0 ); spinBox->setValue( 1.0 ); spinBox->setExpressionsEnabled( false ); QCOMPARE( spinBox->valueFromText( QString( "5" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "5+2" ) ), -10.0 ); spinBox->setExpressionsEnabled( true ); QCOMPARE( spinBox->valueFromText( QString( "5" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "5+2" ) ), 7.0 ); spinBox->setClearValue( 3.0 ); spinBox->setShowClearButton( true ); QCOMPARE( spinBox->valueFromText( QString( "" ) ), 3.0 ); //clearing should set to clearValue spinBox->setShowClearButton( false ); spinBox->setValue( 8.0 ); QCOMPARE( spinBox->valueFromText( QString( "" ) ), 8.0 ); //if no clear button, clearing should set to previous value spinBox->setShowClearButton( true ); spinBox->setValue( 4.0 ); QCOMPARE( spinBox->valueFromText( QString( "5/" ) ), 4.0 ); //invalid expression should reset to previous value //suffix tests spinBox->setSuffix( QString( "mm" ) ); spinBox->setExpressionsEnabled( false ); QCOMPARE( spinBox->valueFromText( QString( "5mm" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "5+2mm" ) ), -10.0 ); spinBox->setExpressionsEnabled( true ); QCOMPARE( spinBox->valueFromText( QString( "5 mm" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "5+2 mm" ) ), 7.0 ); QCOMPARE( spinBox->valueFromText( QString( "5mm" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "5+2mm" ) ), 7.0 ); spinBox->setClearValue( 3.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm" ) ), 3.0 ); //clearing should set to clearValue QCOMPARE( spinBox->valueFromText( QString( "" ) ), 3.0 ); spinBox->setValue( 4.0 ); QCOMPARE( spinBox->valueFromText( QString( "5/mm" ) ), 4.0 ); //invalid expression should reset to previous value //prefix tests spinBox->setSuffix( QString() ); spinBox->setPrefix( QString( "mm" ) ); spinBox->setExpressionsEnabled( false ); QCOMPARE( spinBox->valueFromText( QString( "mm5" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm5+2" ) ), -10.0 ); spinBox->setExpressionsEnabled( true ); QCOMPARE( spinBox->valueFromText( QString( "mm 5" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm 5+2" ) ), 7.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm5" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm5+2" ) ), 7.0 ); spinBox->setClearValue( 3.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm" ) ), 3.0 ); //clearing should set to clearValue QCOMPARE( spinBox->valueFromText( QString( "" ) ), 3.0 ); spinBox->setValue( 4.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm5/" ) ), 4.0 ); //invalid expression should reset to previous value //both suffix and prefix spinBox->setSuffix( QString( "ll" ) ); spinBox->setPrefix( QString( "mm" ) ); spinBox->setExpressionsEnabled( true ); QCOMPARE( spinBox->valueFromText( QString( "mm 5 ll" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm 5+2 ll" ) ), 7.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm5ll" ) ), 5.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm5+2ll" ) ), 7.0 ); spinBox->setClearValue( 3.0 ); QCOMPARE( spinBox->valueFromText( QString( "mmll" ) ), 3.0 ); //clearing should set to clearValue QCOMPARE( spinBox->valueFromText( QString( "" ) ), 3.0 ); spinBox->setValue( 4.0 ); QCOMPARE( spinBox->valueFromText( QString( "mm5/ll" ) ), 4.0 ); //invalid expression should reset to previous value delete spinBox; }
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 ); }