Example #1
0
    void testPieDiagram()
    {
      QgsDiagramSettings ds;
      QColor col1 = Qt::red;
      QColor col2 = Qt::yellow;
      col1.setAlphaF( 0.5 );
      col2.setAlphaF( 0.5 );
      ds.categoryColors = QList<QColor>() << col1 << col2;
      ds.categoryAttributes = QList<QString>() << "\"Pilots\"" << "\"Cabin Crew\"";
      ds.maxScaleDenominator = -1;
      ds.minScaleDenominator = -1;
      ds.minimumSize = 0;
      ds.penColor = Qt::green;
      ds.penWidth = .5;
      ds.scaleByArea = true;
      ds.sizeType = QgsDiagramSettings::MM;
      ds.size = QSizeF( 15, 15 );
      ds.angleOffset = 0;


      QgsLinearlyInterpolatedDiagramRenderer *dr = new QgsLinearlyInterpolatedDiagramRenderer();
      dr->setLowerValue( 0.0 );
      dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
      dr->setUpperValue( 10 );
      dr->setUpperSize( QSizeF( 100, 100 ) );
      dr->setClassificationAttribute( 5 ); // Staff
      dr->setDiagram( mPieDiagram );
      dr->setDiagramSettings( ds );
      mPointsLayer->setDiagramRenderer( dr );

      QgsDiagramLayerSettings dls = QgsDiagramLayerSettings();
      dls.placement = QgsDiagramLayerSettings::OverPoint;

      QgsProject::instance()->writeEntry( "PAL", "/ShowingAllLabels", true );

      mPointsLayer->setDiagramLayerSettings( dls );

      mComposerMap->setNewExtent( QgsRectangle( -122, -79, -70, 47 ) );
      QgsCompositionChecker checker( "piediagram", mComposition );

      QVERIFY( checker.testComposition( mReport ) );

      mPointsLayer->setDiagramRenderer( 0 );
    }
Example #2
0
    void testPieDiagramExpression()
    {
      QgsDiagramSettings ds;
      QColor col1 = Qt::red;
      QColor col2 = Qt::yellow;
      col1.setAlphaF( 0.5 );
      col2.setAlphaF( 0.5 );
      ds.categoryColors = QList<QColor>() << col1 << col2;
      ds.categoryAttributes = QList<QString>() << "ln(Pilots + 1)" << "ln(\"Cabin Crew\" + 1)";
      ds.maxScaleDenominator = -1;
      ds.minScaleDenominator = -1;
      ds.minimumSize = 0;
      ds.penColor = Qt::green;
      ds.penWidth = .5;
      ds.scaleByArea = true;
      ds.sizeType = QgsUnitTypes::RenderMillimeters;
      ds.size = QSizeF( 5, 5 );
      ds.angleOffset = 0;

      QgsLinearlyInterpolatedDiagramRenderer *dr = new QgsLinearlyInterpolatedDiagramRenderer();
      dr->setLowerValue( 0.0 );
      dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
      dr->setUpperValue( 10 );
      dr->setUpperSize( QSizeF( 40, 40 ) );
      dr->setClassificationAttributeIsExpression( true );
      dr->setClassificationAttributeExpression( "ln(Staff + 1)" );
      dr->setDiagram( new QgsPieDiagram() );
      dr->setDiagramSettings( ds );

      QgsDiagramLayerSettings dls = QgsDiagramLayerSettings();
      dls.setPlacement( QgsDiagramLayerSettings::OverPoint );
      dls.setShowAllDiagrams( true );
      // dls.setRenderer( dr );

      mPointsLayer->setDiagramRenderer( dr );
      mPointsLayer->setDiagramLayerSettings( dls );

      QVERIFY( imageCheck( "piediagram_expression" ) );

      mPointsLayer->setDiagramRenderer( 0 );
    }
Example #3
0
    void testPieDiagram()
    {
      QgsDiagramSettings ds;
      QColor col1 = Qt::red;
      QColor col2 = Qt::yellow;
      col1.setAlphaF( 0.5 );
      col2.setAlphaF( 0.5 );
      ds.categoryColors = QList<QColor>() << col1 << col2;
      ds.categoryAttributes = QList<QString>() << "\"Pilots\"" << "\"Cabin Crew\"";
      ds.maxScaleDenominator = -1;
      ds.minScaleDenominator = -1;
      ds.minimumSize = 0;
      ds.penColor = Qt::green;
      ds.penWidth = .5;
      ds.scaleByArea = true;
      ds.sizeType = QgsDiagramSettings::MM;
      ds.size = QSizeF( 5, 5 );
      ds.angleOffset = 0;

      QgsLinearlyInterpolatedDiagramRenderer *dr = new QgsLinearlyInterpolatedDiagramRenderer();
      dr->setLowerValue( 0.0 );
      dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
      dr->setUpperValue( 10 );
      dr->setUpperSize( QSizeF( 40, 40 ) );
      dr->setClassificationAttribute( 5 ); // Staff
      dr->setDiagram( new QgsPieDiagram() );
      dr->setDiagramSettings( ds );
      mPointsLayer->setDiagramRenderer( dr );

      QgsDiagramLayerSettings dls = QgsDiagramLayerSettings();
      dls.placement = QgsDiagramLayerSettings::OverPoint;
      dls.showAll = true;
      mPointsLayer->setDiagramLayerSettings( dls );

      QVERIFY( imageCheck( "piediagram" ) );
    }
void qgis_dev_layerPropDialog::apply()
{
    QSettings().setValue( "/Windows/VectorLayerProperties/diagram/tab",
                          mDiagramPropertiesTabWidget->currentIndex() );
    if ( !mDisplayDiagramsGroupBox->isChecked() ) {m_layer->setDiagramRenderer( 0 ); return;}

    QgsDiagram* diagram = 0;
    int index = diagramType_comboBox->currentIndex();
    QString diagramType = diagramType_comboBox->itemData( index ).toString();

    if ( 0 == mDiagramAttributesTreeWidget->topLevelItemCount() )
    {
        qgis_dev::instance()->messageBar()->pushMessage(
            tr( "Diagrams: No attributes added." ),
            tr( "You did not add any attributes to this diagram layer. Please specify the attributes to visualize on the diagrams or disable diagrams." ),
            QgsMessageBar::WARNING );
    }

    bool scaleAttributeValueOk = false;
    // Check if a (usable) scale attribute value is inserted
    mValueLineEdit->text().toDouble( &scaleAttributeValueOk );

    if ( !mFixedSizeCheckBox->isChecked() && !scaleAttributeValueOk )
    {
        double maxVal = DBL_MIN;
        QgsVectorDataProvider* provider = m_layer->dataProvider();

        if ( provider )
        {
            if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
            {
                // Find maximum value
                for ( int i = 0; i < mDiagramAttributesTreeWidget->topLevelItemCount(); ++i )
                {
                    QString fldName = mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, Qt::UserRole ).toString();
                    if ( fldName.count() >= 2 && fldName.at( 0 ) == '"' && fldName.at( fldName.count() - 1 ) == '"' )
                    {
                        fldName = fldName.mid( 1, fldName.count() - 2 );    // remove enclosing double quotes
                    }
                    int fld = provider->fieldNameIndex( fldName );
                    if ( fld != -1 )
                    {
                        bool ok = false;
                        double val = provider->maximumValue( fld ).toDouble( &ok );
                        if ( ok )
                        {
                            maxVal = qMax( maxVal, val );
                        }
                    }
                }
            }
            else
            {
                maxVal = provider->maximumValue( mSizeAttributeComboBox->itemData( mSizeAttributeComboBox->currentIndex() ).toInt() ).toDouble();
            }
        }

        if ( maxVal != DBL_MIN )
        {
            qgis_dev::instance()->messageBar()->pushMessage(
                tr( "Interpolation value" ),
                tr( "You did not specify an interpolation value. A default value of %1 has been set." ).arg( QString::number( maxVal ) ),
                QgsMessageBar::INFO,
                5 );

            mValueLineEdit->setText( QString::number( maxVal ) );
        }
    }

    if ( diagramType == DIAGRAM_NAME_TEXT )
    {
        diagram = new QgsTextDiagram();
    }
    else if ( diagramType == DIAGRAM_NAME_PIE )
    {
        diagram = new QgsPieDiagram();
    }
    else // if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
    {
        diagram = new QgsHistogramDiagram();
    }

    QgsDiagramSettings ds;
    ds.font = font_comboBox->currentFont(); // 字体
    ds.transparency = mTransparencySlider->value(); // 透明度

    QList<QColor> categoryColors;
    QList<QString> categoryAttributes;
    for ( int i = 0; i < mDiagramAttributesTreeWidget->topLevelItemCount(); ++i )
    {
        QColor color = mDiagramAttributesTreeWidget->topLevelItem( i )->background( 1 ).color();
        color.setAlpha( 255 - ds.transparency );
        categoryColors.append( color );
        categoryAttributes.append( mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, Qt::UserRole ).toString() );
    }
    ds.categoryColors = categoryColors;
    ds.categoryAttributes = categoryAttributes;
    //ds.size = QSizeF( mDiagramSizeSpinBox->value(), mDiagramSizeSpinBox->value() );
    ds.size = QSizeF( 50, 50 );
    //ds.sizeType = static_cast<QgsDiagramSettings::SizeType>( mDiagramUnitComboBox->itemData( mDiagramUnitComboBox->currentIndex() ).toInt() );
    ds.sizeType = static_cast<QgsDiagramSettings::SizeType>( 0 );
    ds.labelPlacementMethod = static_cast<QgsDiagramSettings::LabelPlacementMethod>( mLabelPlacementComboBox->itemData( mLabelPlacementComboBox->currentIndex() ).toInt() );
    //ds.scaleByArea = mScaleDependencyComboBox->itemData( mScaleDependencyComboBox->currentIndex() ).toBool();
    ds.scaleByArea = false;

    if ( mIncreaseSmallDiagramsGroupBox->isChecked() )
    {
        ds.minimumSize = mIncreaseMinimumSizeSpinBox->value();
    }
    else
    {
        ds.minimumSize = 0;
    }

    /*ds.backgroundColor = mBackgroundColorButton->backgroundColor();
    ds.penColor = mDiagramPenColorButton->backgroundColor();*/

    ds.backgroundColor = QColor( 255, 0, 0 );
    ds.penColor = QColor( 255, 255, 0 );
    ds.penWidth = mPenWidthSpinBox->value();
    if ( mVisibilityGroupBox->isChecked() )
    {
        ds.minScaleDenominator = mMinimumDiagramScaleLineEdit->text().toDouble();
        ds.maxScaleDenominator = mMaximumDiagramScaleLineEdit->text().toDouble();
    }
    else
    {
        ds.minScaleDenominator = -1;
        ds.maxScaleDenominator = -1;
    }

    // Diagram angle offset (pie)
    ds.angleOffset = mAngleOffsetComboBox->itemData( mAngleOffsetComboBox->currentIndex() ).toInt();

    // Diagram orientation (histogram)
    //ds.diagramOrientation = static_cast<QgsDiagramSettings::DiagramOrientation>( mOrientationButtonGroup->checkedButton()->property( "direction" ).toInt() );
    ds.diagramOrientation = static_cast<QgsDiagramSettings::DiagramOrientation>( 0 );
    ds.barWidth = mBarWidthSpinBox->value();

    if ( mFixedSizeCheckBox->isChecked() )
    {
        QgsSingleCategoryDiagramRenderer* dr = new QgsSingleCategoryDiagramRenderer();
        dr->setDiagram( diagram );
        dr->setDiagramSettings( ds );
        m_layer->setDiagramRenderer( dr );
    }
    else
    {
        QgsLinearlyInterpolatedDiagramRenderer* dr = new QgsLinearlyInterpolatedDiagramRenderer();
        dr->setLowerValue( 0.0 );
        dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
        dr->setUpperValue( mValueLineEdit->text().toDouble() );
        dr->setUpperSize( QSizeF( mSizeSpinBox->value(), mSizeSpinBox->value() ) );
        bool isExpression = mSizeAttributeComboBox->currentIndex() >= m_availableAttributeCount;
        dr->setClassificationAttributeIsExpression( isExpression );
        if ( isExpression )
        {
            dr->setClassificationAttributeExpression( mSizeAttributeComboBox->currentText() );
        }
        else
        {
            dr->setClassificationAttribute( mSizeAttributeComboBox->itemData( mSizeAttributeComboBox->currentIndex() ).toInt() );
        }
        dr->setDiagram( diagram );
        dr->setDiagramSettings( ds );
        m_layer->setDiagramRenderer( dr );
    }

    QgsDiagramLayerSettings dls;
    dls.dist = mDiagramDistanceSpinBox->value();
    dls.priority = mPrioritySlider->value();
    if ( mDataDefinedPositionGroupBox->isChecked() )
    {
        //dls.xPosColumn = mDataDefinedXComboBox->itemData( mDataDefinedXComboBox->currentIndex() ).toInt();
        //dls.yPosColumn = mDataDefinedYComboBox->itemData( mDataDefinedYComboBox->currentIndex() ).toInt();
        dls.xPosColumn = -1;
        dls.yPosColumn = -1;
    }
    else
    {
        dls.xPosColumn = -1;
        dls.yPosColumn = -1;
    }
    dls.placement = ( QgsDiagramLayerSettings::Placement )mPlacementComboBox->itemData( mPlacementComboBox->currentIndex() ).toInt();
    if ( mLineOptionsComboBox->isEnabled() )
    {
        dls.placementFlags = static_cast<QgsDiagramLayerSettings::LinePlacementFlags>( mLineOptionsComboBox->itemData( mLineOptionsComboBox->currentIndex() ).toInt() );
    }
    m_layer->setDiagramLayerSettings( dls );
}