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 QgsComposerColumnWidthDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const { QgsDoubleSpinBox *spinBox = static_cast<QgsDoubleSpinBox*>( editor ); spinBox->interpretText(); int value = spinBox->value(); model->setData( index, value, Qt::EditRole ); }
void QgsRangeWidgetWrapper::initWidget( QWidget* editor ) { mDoubleSpinBox = qobject_cast<QDoubleSpinBox*>( editor ); mIntSpinBox = qobject_cast<QSpinBox*>( editor ); mDial = qobject_cast<QDial*>( editor ); mSlider = qobject_cast<QSlider*>( editor ); mQgsDial = qobject_cast<QgsDial*>( editor ); mQgsSlider = qobject_cast<QgsSlider*>( editor ); bool allowNull = config( QStringLiteral( "AllowNull" ), true ).toBool(); QVariant min( config( QStringLiteral( "Min" ) ) ); QVariant max( config( QStringLiteral( "Max" ) ) ); QVariant step( config( QStringLiteral( "Step" ) ) ); if ( mDoubleSpinBox ) { // set the precision if field is integer int precision = layer()->fields().at( fieldIdx() ).precision(); if ( precision > 0 ) { mDoubleSpinBox->setDecimals( layer()->fields().at( fieldIdx() ).precision() ); } double minval = min.toDouble(); double stepval = step.toDouble(); QgsDoubleSpinBox* qgsWidget = dynamic_cast<QgsDoubleSpinBox*>( mDoubleSpinBox ); if ( qgsWidget ) qgsWidget->setShowClearButton( allowNull ); if ( allowNull ) { if ( precision > 0 ) { minval -= 10 ^ -precision; } else { minval -= stepval; } mDoubleSpinBox->setValue( minval ); mDoubleSpinBox->setSpecialValueText( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() ); } mDoubleSpinBox->setMinimum( min.isValid() ? min.toDouble() : std::numeric_limits<double>::min() ); mDoubleSpinBox->setMaximum( max.isValid() ? max.toDouble() : std::numeric_limits<double>::max() ); mDoubleSpinBox->setSingleStep( step.isValid() ? step.toDouble() : 1.0 ); if ( config( QStringLiteral( "Suffix" ) ).isValid() ) mDoubleSpinBox->setSuffix( config( QStringLiteral( "Suffix" ) ).toString() ); connect( mDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( valueChanged( double ) ) ); } else if ( mIntSpinBox )
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; }
void QgsRangeWidgetWrapper::initWidget( QWidget* editor ) { mDoubleSpinBox = qobject_cast<QDoubleSpinBox*>( editor ); mIntSpinBox = qobject_cast<QSpinBox*>( editor ); mDial = qobject_cast<QDial*>( editor ); mSlider = qobject_cast<QSlider*>( editor ); mQgsDial = qobject_cast<QgsDial*>( editor ); mQgsSlider = qobject_cast<QgsSlider*>( editor ); bool allowNull = config( "AllowNull" ).toBool(); QVariant min( config( "Min" ) ); QVariant max( config( "Max" ) ); QVariant step( config( "Step" ) ); if ( mDoubleSpinBox ) { // set the precision if field is integer int precision = layer()->fields()[fieldIdx()].precision(); if ( precision > 0 ) { mDoubleSpinBox->setDecimals( layer()->fields()[fieldIdx()].precision() ); } double minval = min.toDouble(); double stepval = step.toDouble(); QgsDoubleSpinBox* qgsWidget = dynamic_cast<QgsDoubleSpinBox*>( mDoubleSpinBox ); if ( qgsWidget ) qgsWidget->setShowClearButton( allowNull ); if ( allowNull ) { if ( precision > 0 ) { minval -= 10 ^ -precision; } else { minval -= stepval; } mDoubleSpinBox->setValue( minval ); mDoubleSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() ); } if ( min.isValid() ) mDoubleSpinBox->setMinimum( min.toDouble() ); if ( max.isValid() ) mDoubleSpinBox->setMaximum( max.toDouble() ); if ( step.isValid() ) mDoubleSpinBox->setSingleStep( step.toDouble() ); if ( config( "Suffix" ).isValid() ) mDoubleSpinBox->setSuffix( config( "Suffix" ).toString() ); connect( mDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( valueChanged( double ) ) ); } if ( mIntSpinBox ) { int minval = min.toInt(); int stepval = step.toInt(); QgsSpinBox* qgsWidget = dynamic_cast<QgsSpinBox*>( mIntSpinBox ); if ( qgsWidget ) qgsWidget->setShowClearButton( allowNull ); if ( allowNull ) { minval -= stepval; mIntSpinBox->setValue( minval ); mIntSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() ); } if ( min.isValid() ) mIntSpinBox->setMinimum( min.toInt() ); if ( max.isValid() ) mIntSpinBox->setMaximum( max.toInt() ); if ( step.isValid() ) mIntSpinBox->setSingleStep( step.toInt() ); if ( config( "Suffix" ).isValid() ) mIntSpinBox->setSuffix( config( "Suffix" ).toString() ); connect( mIntSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) ); } if ( mQgsDial || mQgsSlider ) { field().convertCompatible( min ); field().convertCompatible( max ); field().convertCompatible( step ); if ( mQgsSlider ) { if ( min.isValid() ) mQgsSlider->setMinimum( min ); if ( max.isValid() ) mQgsSlider->setMaximum( max ); if ( step.isValid() ) mQgsSlider->setSingleStep( step ); } if ( mQgsDial ) { if ( min.isValid() ) mQgsDial->setMinimum( min ); if ( max.isValid() ) mQgsDial->setMaximum( max ); if ( step.isValid() ) mQgsDial->setSingleStep( step ); } connect( editor, SIGNAL( valueChanged( QVariant ) ), this, SLOT( valueChanged( QVariant ) ) ); } else if ( mDial ) { if ( min.isValid() ) mDial->setMinimum( min.toInt() ); if ( max.isValid() ) mDial->setMaximum( max.toInt() ); if ( step.isValid() ) mDial->setSingleStep( step.toInt() ); connect( mDial, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) ); } else if ( mSlider )
void QgsRangeWidgetWrapper::initWidget( QWidget *editor ) { mDoubleSpinBox = qobject_cast<QDoubleSpinBox *>( editor ); mIntSpinBox = qobject_cast<QSpinBox *>( editor ); mDial = qobject_cast<QDial *>( editor ); mSlider = qobject_cast<QSlider *>( editor ); mQgsDial = qobject_cast<QgsDial *>( editor ); mQgsSlider = qobject_cast<QgsSlider *>( editor ); bool allowNull = config( QStringLiteral( "AllowNull" ), true ).toBool(); QVariant min( config( QStringLiteral( "Min" ) ) ); QVariant max( config( QStringLiteral( "Max" ) ) ); QVariant step( config( QStringLiteral( "Step" ) ) ); if ( mDoubleSpinBox ) { // set the precision if field is integer int precision = layer()->fields().at( fieldIdx() ).precision(); if ( precision > 0 ) { mDoubleSpinBox->setDecimals( layer()->fields().at( fieldIdx() ).precision() ); } double minval = min.toDouble(); double stepval = step.toDouble(); QgsDoubleSpinBox *qgsWidget = dynamic_cast<QgsDoubleSpinBox *>( mDoubleSpinBox ); if ( qgsWidget ) qgsWidget->setShowClearButton( allowNull ); if ( allowNull ) { if ( precision > 0 ) { minval -= 10 ^ -precision; } else { minval -= stepval; } mDoubleSpinBox->setValue( minval ); mDoubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() ); } mDoubleSpinBox->setMinimum( min.isValid() ? min.toDouble() : std::numeric_limits<double>::min() ); mDoubleSpinBox->setMaximum( max.isValid() ? max.toDouble() : std::numeric_limits<double>::max() ); mDoubleSpinBox->setSingleStep( step.isValid() ? step.toDouble() : 1.0 ); if ( config( QStringLiteral( "Suffix" ) ).isValid() ) mDoubleSpinBox->setSuffix( config( QStringLiteral( "Suffix" ) ).toString() ); connect( mDoubleSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, static_cast < void ( QgsEditorWidgetWrapper::* )( double ) > ( &QgsEditorWidgetWrapper::valueChanged ) ); } else if ( mIntSpinBox ) { QgsSpinBox *qgsWidget = dynamic_cast<QgsSpinBox *>( mIntSpinBox ); if ( qgsWidget ) qgsWidget->setShowClearButton( allowNull ); if ( allowNull ) { int minval = min.toInt(); int stepval = step.toInt(); minval -= stepval; mIntSpinBox->setValue( minval ); mIntSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() ); } setupIntEditor( min, max, step, mIntSpinBox, this ); if ( config( QStringLiteral( "Suffix" ) ).isValid() ) mIntSpinBox->setSuffix( config( QStringLiteral( "Suffix" ) ).toString() ); } else { field().convertCompatible( min ); field().convertCompatible( max ); field().convertCompatible( step ); if ( mQgsDial ) setupIntEditor( min, max, step, mQgsDial, this ); else if ( mQgsSlider ) setupIntEditor( min, max, step, mQgsSlider, this ); else if ( mDial ) setupIntEditor( min, max, step, mDial, this ); else if ( mSlider ) setupIntEditor( min, max, step, mSlider, this ); } }
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 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 QgsRangeWidgetWrapper::initWidget( QWidget *editor ) { mDoubleSpinBox = qobject_cast<QDoubleSpinBox *>( editor ); mIntSpinBox = qobject_cast<QSpinBox *>( editor ); mDial = qobject_cast<QDial *>( editor ); mSlider = qobject_cast<QSlider *>( editor ); mQgsDial = qobject_cast<QgsDial *>( editor ); mQgsSlider = qobject_cast<QgsSlider *>( editor ); bool allowNull = config( QStringLiteral( "AllowNull" ), true ).toBool(); QVariant min( config( QStringLiteral( "Min" ) ) ); QVariant max( config( QStringLiteral( "Max" ) ) ); QVariant step( config( QStringLiteral( "Step" ) ) ); QVariant precision( config( QStringLiteral( "Precision" ) ) ); if ( mDoubleSpinBox ) { double stepval = step.isValid() ? step.toDouble() : 1.0; double minval = min.isValid() ? min.toDouble() : std::numeric_limits<double>::lowest(); double maxval = max.isValid() ? max.toDouble() : std::numeric_limits<double>::max(); int precisionval = precision.isValid() ? precision.toInt() : layer()->fields().at( fieldIdx() ).precision(); mDoubleSpinBox->setDecimals( precisionval ); QgsDoubleSpinBox *qgsWidget = qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox ); if ( qgsWidget ) qgsWidget->setShowClearButton( allowNull ); // Make room for null value: lower the minimum to allow for NULL special values if ( allowNull ) { double decr; if ( precisionval > 0 ) { decr = std::pow( 10, -precisionval ); } else { decr = stepval; } minval -= decr; // Note: call setMinimum here or setValue won't work mDoubleSpinBox->setMinimum( minval ); mDoubleSpinBox->setValue( minval ); QgsDoubleSpinBox *doubleSpinBox( qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox ) ); if ( doubleSpinBox ) doubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() ); else mDoubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() ); } mDoubleSpinBox->setMinimum( minval ); mDoubleSpinBox->setMaximum( maxval ); mDoubleSpinBox->setSingleStep( stepval ); if ( config( QStringLiteral( "Suffix" ) ).isValid() ) mDoubleSpinBox->setSuffix( config( QStringLiteral( "Suffix" ) ).toString() ); connect( mDoubleSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, [ = ]( double ) { emitValueChanged(); } ); } else if ( mIntSpinBox ) { QgsSpinBox *qgsWidget = qobject_cast<QgsSpinBox *>( mIntSpinBox ); if ( qgsWidget ) qgsWidget->setShowClearButton( allowNull ); int minval = min.toInt(); if ( allowNull ) { int stepval = step.isValid() ? step.toInt() : 1; minval -= stepval; mIntSpinBox->setValue( minval ); QgsSpinBox *intSpinBox( qobject_cast<QgsSpinBox *>( mIntSpinBox ) ); if ( intSpinBox ) intSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() ); else mIntSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() ); } setupIntEditor( minval, max, step, mIntSpinBox, this ); if ( config( QStringLiteral( "Suffix" ) ).isValid() ) mIntSpinBox->setSuffix( config( QStringLiteral( "Suffix" ) ).toString() ); } else { ( void )field().convertCompatible( min ); ( void )field().convertCompatible( max ); ( void )field().convertCompatible( step ); if ( mQgsDial ) setupIntEditor( min, max, step, mQgsDial, this ); else if ( mQgsSlider ) setupIntEditor( min, max, step, mQgsSlider, this ); else if ( mDial ) setupIntEditor( min, max, step, mDial, this ); else if ( mSlider ) setupIntEditor( min, max, step, mSlider, this ); } }
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 ); }