//! Update the widget that represents the curve on the legend // this was adapted from QwtPlotCurve::updateLegend() void HistogramItem::updateLegend( QwtLegend *legend ) const { if ( !legend ) return; QwtPlotItem::updateLegend( legend ); QWidget *widget = legend->find( this ); if ( !widget || !widget->inherits( "QwtLegendItem" ) ) return; QwtLegendItem *legendItem = ( QwtLegendItem * )widget; #if QT_VERSION < 0x040000 const bool doUpdate = legendItem->isUpdatesEnabled(); #else const bool doUpdate = legendItem->updatesEnabled(); #endif legendItem->setUpdatesEnabled( false ); const int policy = legend->displayPolicy(); if ( policy == QwtLegend::FixedIdentifier ) { int mode = legend->identifierMode(); legendItem->setCurvePen( QPen( color() ) ); if ( mode & QwtLegendItem::ShowText ) legendItem->setText( title() ); else legendItem->setText( QwtText() ); legendItem->setIdentifierMode( mode ); } else if ( policy == QwtLegend::AutoIdentifier ) { int mode = 0; legendItem->setCurvePen( QPen( color() ) ); mode |= QwtLegendItem::ShowLine; if ( !title().isEmpty() ) { legendItem->setText( title() ); mode |= QwtLegendItem::ShowText; } else { legendItem->setText( QwtText() ); } legendItem->setIdentifierMode( mode ); } legendItem->setUpdatesEnabled( doUpdate ); legendItem->update(); }
void VectorPlot::addDotPlot(const MarkupQwtAdapter&adapter, QColor color, const QString &name, bool addToLegend) { QwtPlotCurve *curve = new QwtPlotCurve(name); QwtSymbol sym; sym.setPen(QColor(color)); sym.setBrush(QColor(color)); sym.setStyle(QwtSymbol::Rect); sym.setSize( QSize(1, 20) ); curve->setSymbol(sym); curve->setStyle(QwtPlotCurve::NoCurve); if(adapter.size() > 0) { setupPlot(adapter.boundingRect()); curve->setData(adapter); curve->attach(plot); } if(!legend) { return; } // Добавляем кривую в легенду QwtLegendItem *item = new QwtLegendItem(0); item->setText(curve->title()); item->setSymbol(sym); item->setIdentifierMode(QwtLegendItem::ShowLine | QwtLegendItem::ShowSymbol | QwtLegendItem::ShowText); legend->insert(curve, item); }
/*! \brief Update the widget that represents the item on the legend updateLegend() is called from itemChanged() to adopt the widget representing the item on the legend to its new configuration. The default implementation is made for QwtPolarCurve and updates a QwtLegendItem(), but an item could be represented by any type of widget, by overloading legendItem() and updateLegend(). \sa legendItem(), itemChanged(), QwtLegend() */ void QwtPolarItem::updateLegend( QwtLegend *legend ) const { if ( legend == NULL ) return; QWidget *lgdItem = legend->find( this ); if ( testItemAttribute( QwtPolarItem::Legend ) ) { if ( lgdItem == NULL ) { lgdItem = legendItem(); if ( lgdItem ) legend->insert( this, lgdItem ); } QwtLegendItem* label = qobject_cast<QwtLegendItem *>( lgdItem ); if ( label ) { // paint the identifier const QSize sz = label->identifierSize(); QPixmap identifier( sz.width(), sz.height() ); identifier.fill( Qt::transparent ); QPainter painter( &identifier ); painter.setRenderHint( QPainter::Antialiasing, testRenderHint( QwtPolarItem::RenderAntialiased ) ); drawLegendIdentifier( &painter, QRect( 0, 0, sz.width(), sz.height() ) ); painter.end(); const bool doUpdate = label->updatesEnabled(); if ( doUpdate ) label->setUpdatesEnabled( false ); label->setText( title() ); label->setIdentifier( identifier ); label->setItemMode( legend->itemMode() ); if ( doUpdate ) label->setUpdatesEnabled( true ); label->update(); } } else { if ( lgdItem ) { lgdItem->hide(); lgdItem->deleteLater(); } } }
//! Update the widget that represents the curve on the legend void QwtPolarCurve::updateLegend( QwtLegend *legend ) const { if ( !legend ) return; QwtPolarItem::updateLegend( legend ); QWidget *widget = legend->find( this ); if ( !widget || !widget->inherits( "QwtLegendItem" ) ) return; QwtLegendItem *legendItem = ( QwtLegendItem * )widget; #if QT_VERSION < 0x040000 const bool doUpdate = legendItem->isUpdatesEnabled(); #else const bool doUpdate = legendItem->updatesEnabled(); #endif legendItem->setUpdatesEnabled( false ); const int policy = legend->displayPolicy(); if ( policy == QwtLegend::FixedIdentifier ) { int mode = legend->identifierMode(); if ( mode & QwtLegendItem::ShowLine ) legendItem->setCurvePen( pen() ); if ( mode & QwtLegendItem::ShowSymbol ) legendItem->setSymbol( symbol() ); if ( mode & QwtLegendItem::ShowText ) legendItem->setText( title() ); else legendItem->setText( QwtText() ); legendItem->setIdentifierMode( mode ); } else if ( policy == QwtLegend::AutoIdentifier ) { int mode = 0; if ( QwtPolarCurve::NoCurve != style() ) { legendItem->setCurvePen( pen() ); mode |= QwtLegendItem::ShowLine; } if ( QwtSymbol::NoSymbol != symbol().style() ) { legendItem->setSymbol( symbol() ); mode |= QwtLegendItem::ShowSymbol; } if ( !title().isEmpty() ) { legendItem->setText( title() ); mode |= QwtLegendItem::ShowText; } else { legendItem->setText( QwtText() ); } legendItem->setIdentifierMode( mode ); } legendItem->setUpdatesEnabled( doUpdate ); legendItem->update(); }