/*!
  Minimum size hint needed to display an entry

  \param data Attributes of the legend entry
  \return Minimum size
 */
QSize QwtPlotLegendItem::minimumSize( const QwtLegendData &data ) const
{
    QSize size( 2 * d_data->itemMargin, 2 * d_data->itemMargin );

    if ( !data.isValid() )
        return size;

    const QwtGraphic graphic = data.icon();
    const QwtText text = data.title();

    int w = 0;
    int h = 0;

    if ( !graphic.isNull() )
    {
        w = graphic.width();
        h = graphic.height();
    }

    if ( !text.isEmpty() )
    {
        const QSizeF sz = text.textSize( font() );

        w += qCeil( sz.width() );
        h = qMax( h, qCeil( sz.height() ) );
    }

    if ( graphic.width() > 0 && !text.isEmpty() )
        w += d_data->itemSpacing;

    size += QSize( w, h );
    return size;
}
/*!
  \return Information to be displayed on the legend

  The chart is represented by a list of entries - one for each bar title.
  Each element contains a bar title and an icon showing its corresponding bar.

  \sa barTitles(), legendIcon(), legendIconSize()
*/
QList<QwtLegendData> QwtPlotMultiBarChart::legendData() const
{
    QList<QwtLegendData> list;

    for ( int i = 0; i < d_data->barTitles.size(); i++ )
    {
        QwtLegendData data;

        QVariant titleValue;
        qVariantSetValue( titleValue, d_data->barTitles[i] );
        data.setValue( QwtLegendData::TitleRole, titleValue );

        if ( !legendIconSize().isEmpty() )
        {
            QVariant iconValue;
            qVariantSetValue( iconValue, 
                legendIcon( i, legendIconSize() ) );

            data.setValue( QwtLegendData::IconRole, iconValue );
        }

        list += data;
    }

    return list;
}
Esempio n. 3
0
/*!
   \brief Return all information, that is needed to represent
          the item on the legend

   In case of LegendBarTitles an entry for each bar is returned,
   otherwise the chart is represented like any other plot item
   from its title() and the legendIcon().

   \sa title(), setLegendMode(), barTitle(), QwtLegend, QwtPlotLegendItem
 */
QList<QwtLegendData> QwtPlotBarChart::legendData() const
{
    QList<QwtLegendData> list;

    if ( d_data->legendMode == LegendBarTitles )
    {
        const size_t numSamples = dataSize();
        for ( size_t i = 0; i < numSamples; i++ )
        {
            QwtLegendData data;

            QVariant titleValue;
            qVariantSetValue( titleValue, barTitle( i ) );
            data.setValue( QwtLegendData::TitleRole, titleValue );

            if ( !legendIconSize().isEmpty() )
            {
                QVariant iconValue;
                qVariantSetValue( iconValue,
                    legendIcon( i, legendIconSize() ) );

                data.setValue( QwtLegendData::IconRole, iconValue );
            }

            list += data;
        }
    }
    else
    {
        return QwtPlotAbstractBarChart::legendData();
    }

    return list;
}
/*!
  \return The preferred height, for a width.
  \param data Attributes of the legend entry
  \param width Width
*/
int QwtPlotLegendItem::heightForWidth( 
    const QwtLegendData &data, int width ) const
{
    width -= 2 * d_data->itemMargin;

    const QwtGraphic graphic = data.icon();
    const QwtText text = data.title();

    if ( text.isEmpty() )
        return graphic.height();

    if ( graphic.width() > 0 )
        width -= graphic.width() + d_data->itemSpacing;

    int h = text.heightForWidth( width, font() );
    h += 2 * d_data->itemMargin;

    return qMax( graphic.height(), h );
}
Esempio n. 5
0
/*!
  Set the attributes of the legend label

  \param legendData Attributes of the label
  \sa data()
 */
void QwtLegendLabel::setData( const QwtLegendData &legendData )
{
    d_data->legendData = legendData;

    const bool doUpdate = updatesEnabled();
    setUpdatesEnabled( false );

    setText( legendData.title() );
    setIcon( legendData.icon().toPixmap() );

    if ( legendData.hasRole( QwtLegendData::ModeRole ) )
        setItemMode( legendData.mode() );

    if ( doUpdate )
    {
        setUpdatesEnabled( true );
        update();
    }
}
Esempio n. 6
0
/*!
  \brief Update the widget 

  \param widget Usually a QwtLegendLabel
  \param data Attributes to be displayed

  \sa createWidget()
  \note When widget is no QwtLegendLabel updateWidget() does nothing.
 */
void QwtLegend::updateWidget( QWidget *widget, const QwtLegendData &data )
{
    QwtLegendLabel *label = qobject_cast<QwtLegendLabel *>( widget );
    if ( label )
    {
        label->setData( data );
        if ( !data.value( QwtLegendData::ModeRole ).isValid() )
        {
            // use the default mode, when there is no specific
            // hint from the legend data

            label->setItemMode( defaultItemMode() );
        }
    }
}
Esempio n. 7
0
/*!
   \brief Return all information, that is needed to represent
          the item on the legend

   Most items are represented by one entry on the legend
   showing an icon and a text.

   QwtLegendData is basically a list of QVariants that makes it
   possible to overload and reimplement legendData() to 
   return almost any type of information, that is understood
   by the receiver that acts as the legend.

   The default implementation returns one entry with 
   the title() of the item and the legendIcon().

   \sa title(), legendIcon(), QwtLegend
 */
QList<QwtLegendData> QwtPolarItem::legendData() const
{
    QwtLegendData data;

    QwtText label = title();
    label.setRenderFlags( label.renderFlags() & Qt::AlignLeft );

    QVariant titleValue;
    qVariantSetValue( titleValue, label );
    data.setValue( QwtLegendData::TitleRole, titleValue );

    const QwtGraphic graphic = legendIcon( 0, legendIconSize() );
    if ( !graphic.isNull() )
    {
        QVariant iconValue;
        qVariantSetValue( iconValue, graphic );
        data.setValue( QwtLegendData::IconRole, iconValue );
    }

    QList<QwtLegendData> list;
    list += data;

    return list;
}
/*!
  Draw an entry on the legend

  \param painter Qt Painter
  \param plotItem Plot item, represented by the entry
  \param data Attributes of the legend entry
  \param rect Bounding rectangle for the entry
 */
void QwtPlotLegendItem::drawLegendData( QPainter *painter,
    const QwtPlotItem *plotItem, const QwtLegendData &data, 
    const QRectF &rect ) const
{
    Q_UNUSED( plotItem );

    const int m = d_data->itemMargin;
    const QRectF r = rect.toRect().adjusted( m, m, -m, -m );

    painter->setClipRect( r, Qt::IntersectClip );

    int titleOff = 0;

    const QwtGraphic graphic = data.icon();
    if ( !graphic.isEmpty() )
    {
        QRectF iconRect( r.topLeft(), graphic.defaultSize() );

        iconRect.moveCenter( 
            QPoint( iconRect.center().x(), rect.center().y() ) );

        graphic.render( painter, iconRect, Qt::KeepAspectRatio );

        titleOff += iconRect.width() + d_data->itemSpacing;
    }

    const QwtText text = data.title();
    if ( !text.isEmpty() )
    {
        painter->setPen( textPen() );
        painter->setFont( font() );

        const QRectF textRect = r.adjusted( titleOff, 0, 0, 0 );
        text.draw( painter, textRect );
    }
}
bool QwtLegendLayoutItem::hasHeightForWidth() const
{
    return !d_data.title().isEmpty();
}