plotlegend.cpp
Go to the documentation of this file.
00001 #include "plotlegend.h"
00002 #include <QEvent>
00003 #include <QMouseEvent>
00004 #include <QWheelEvent>
00005 
00006 
00007 QRectF PlotLegend::hideButtonRect() const
00008 {
00009     auto canvas_rect = _parent_plot->canvas()->rect();
00010     if( alignment() & Qt::AlignRight)
00011     {
00012         return QRectF( geometry(canvas_rect).topRight(), QSize(8,-8) );
00013     }
00014     return QRectF( geometry(canvas_rect).topLeft(), QSize(-8,-8) );
00015 }
00016 
00017 void PlotLegend::draw(QPainter *painter, const QwtScaleMap &xMap,
00018                       const QwtScaleMap &yMap,
00019                       const QRectF &rect) const
00020 {
00021     if( !_collapsed )
00022     {
00023         QwtPlotLegendItem::draw(painter, xMap, yMap, rect);
00024     }
00025 
00026     QRectF iconRect = hideButtonRect();
00027 
00028     if( isVisible() && plotItems().size() > 0)
00029     {
00030         painter->save();
00031 
00032         auto col = _collapsed ? Qt::black : Qt::white;
00033         painter->setPen( col );
00034         painter->setBrush( QBrush(col, Qt::SolidPattern) );
00035         painter->drawRect( iconRect );
00036 
00037         QPen black_pen (Qt::black);
00038         black_pen.setWidth(2);
00039         painter->setPen( black_pen );
00040 //        painter->drawLine( iconRect.topLeft(), iconRect.bottomRight() );
00041 //        painter->drawLine( iconRect.topRight(), iconRect.bottomLeft() );
00042         painter->drawEllipse( iconRect );
00043 
00044         painter->restore();
00045     }
00046 }
00047 
00048 void PlotLegend::drawLegendData( QPainter *painter,
00049     const QwtPlotItem *plotItem, const QwtLegendData &data,
00050     const QRectF &rect ) const
00051 {
00052     Q_UNUSED( plotItem );
00053 
00054     const int m = margin();
00055     const QRectF r = rect.toRect().adjusted( m, m, -m, -m );
00056 
00057     painter->setClipRect( r, Qt::IntersectClip );
00058 
00059     int titleOff = 0;
00060 
00061     const QwtGraphic graphic = data.icon();
00062     if ( !graphic.isEmpty() )
00063     {
00064         QRectF iconRect( r.topLeft(), graphic.defaultSize() );
00065 
00066         iconRect.moveCenter(
00067             QPoint( iconRect.center().x(), rect.center().y() ) );
00068 
00069         if(  plotItem->isVisible() )
00070         {
00071             graphic.render( painter, iconRect, Qt::KeepAspectRatio );
00072         }
00073 
00074         titleOff += iconRect.width() + spacing();
00075     }
00076 
00077     const QwtText text = data.title();
00078     if ( !text.isEmpty() )
00079     {
00080         auto pen = textPen();
00081         if( !plotItem->isVisible() )
00082         {
00083             pen.setColor( Qt::gray );
00084         }
00085         painter->setPen( pen );
00086         painter->setFont( font() );
00087 
00088         const QRectF textRect = r.adjusted( titleOff, 0, 0, 0 );
00089         text.draw( painter, textRect );
00090     }
00091 }
00092 
00093 
00094 bool PlotLegend::processWheelEvent(QWheelEvent* mouse_event)
00095 {
00096     if ( mouse_event->modifiers() == Qt::ControlModifier && isVisible() )
00097     {
00098         auto canvas_rect = _parent_plot->canvas()->rect();
00099         auto legend_rect = geometry( canvas_rect );
00100         if( legend_rect.contains( mouse_event->pos()) )
00101         {
00102             int point_size = font().pointSize();
00103             if( mouse_event->delta() > 0 && point_size < 14)
00104             {
00105                 emit legendSizeChanged(point_size+1);
00106             }
00107             if( mouse_event->delta() < 0 && point_size > 6)
00108             {
00109                 emit legendSizeChanged(point_size-1);
00110             }
00111             return true;
00112         }
00113     }
00114     return false;
00115 }
00116 
00117 const QwtPlotItem* PlotLegend::processMousePressEvent(QMouseEvent* mouse_event)
00118 {
00119     auto canvas_rect = _parent_plot->canvas()->rect();
00120     const QPoint press_point = mouse_event->pos();
00121 
00122     if( isVisible() &&  mouse_event->modifiers() == Qt::NoModifier)
00123     {
00124         if( !_collapsed && geometry( canvas_rect ).contains(press_point) )
00125         {
00126             for(auto item: plotItems())
00127             {
00128                 auto item_rect = legendGeometries(item).first();
00129                 if( item_rect.contains( press_point ))
00130                 {
00131                     return item;
00132                 }
00133             }
00134         }
00135         else if( hideButtonRect().contains(press_point) )
00136         {
00137             _collapsed = !_collapsed;
00138             _parent_plot->replot();
00139             return nullptr;
00140         }
00141     }
00142     return nullptr;
00143 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:04