From 7a803d5bacf352abce4001b4dc451838831b1eae Mon Sep 17 00:00:00 2001 From: sbh Date: Thu, 8 Aug 2013 07:19:36 +0000 Subject: [PATCH] refs #3238 Ability to place Plot2D legend on canvas --- src/Plot2d/Plot2d_Legend.cxx | 334 ++++++++++++++++++++++++++ src/Plot2d/Plot2d_Legend.h | 61 +++++ src/Plot2d/Plot2d_SetupViewDlg.cxx | 75 ++++-- src/Plot2d/Plot2d_SetupViewDlg.h | 4 + src/Plot2d/Plot2d_ViewFrame.cxx | 84 ++++--- src/Plot2d/Plot2d_ViewFrame.h | 16 +- src/Plot2d/resources/Plot2d_msg_en.ts | 24 ++ 7 files changed, 540 insertions(+), 58 deletions(-) create mode 100644 src/Plot2d/Plot2d_Legend.cxx create mode 100644 src/Plot2d/Plot2d_Legend.h diff --git a/src/Plot2d/Plot2d_Legend.cxx b/src/Plot2d/Plot2d_Legend.cxx new file mode 100644 index 000000000..aa66ee8c9 --- /dev/null +++ b/src/Plot2d/Plot2d_Legend.cxx @@ -0,0 +1,334 @@ + +#include "Plot2d_Legend.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +/** + Constructor + @param thePlot the plot that the axis should be assigned to +*/ +Plot2d_Legend::Plot2d_Legend( QwtPlot* thePlot ) +: QwtLegend( thePlot ), + myIsEnabled( false ), + myIsOnCanvas( false ), + myPosition( Bottom ) +{ + init(); + setPlot( thePlot ); + QwtPlotCanvas* aCanvas = myPlot->canvas(); + if ( aCanvas ) + aCanvas->installEventFilter( this ); +} + +/** + Destructor +*/ +Plot2d_Legend::~Plot2d_Legend() +{ +} + +/** + Fill the initial state of the legend +*/ +void Plot2d_Legend::init() +{ + setFrameStyle( QFrame::Box ); + setWindowTitle( tr( "LEGEND_TLT" ) ); +} + +/** + Return the legend position type + @return the legend position type +*/ +Plot2d_Legend::LegendPosition Plot2d_Legend::getPositionType() const +{ + return myPosition; +} + +/** + Set the legend position type + @param thePositionType legend position type +*/ +void Plot2d_Legend::setPositionType( const LegendPosition& thePositionType ) +{ + myPosition = thePositionType; +} + +/** + Set the legend position type (Embedded or external) + @param thePositionType legend position type +*/ +void Plot2d_Legend::setOnCanvas( bool isOnCanvas ) +{ + myIsOnCanvas = isOnCanvas; +} + +/** + Return true if the legend is shown on plot2d + @return true if the legend shown +*/ +bool Plot2d_Legend::isEnabled() const +{ + return myIsEnabled; +} + +/** + Show or hide legend + @param isEnable if true the legend will be shown + @param isUpdate if true the plot2d will be updated +*/ +void Plot2d_Legend::setEnabled( bool isEnable ) +{ + myIsEnabled = isEnable; + if( myPlot ) + { + if ( myIsEnabled ) + insertLegend(); + else if ( myIsOnCanvas ) { + hide(); + } else { + myPlot->insertLegend( 0 ); + } + } +} + + +void Plot2d_Legend::setPlot( QwtPlot* thePlot ) +{ + if( myPlot ) + disconnect( myPlot ); + if( thePlot != NULL ) + connect( thePlot, SIGNAL(destroyed(QObject*)), this, SLOT(onPlotDestroyed() ) ); + myPlot = thePlot; +} + +/** + SLOT: called when registered plot is destroyed +*/ +void Plot2d_Legend::onPlotDestroyed() +{ + setPlot(NULL); + deleteLater(); +} + +/*! + Adjust contents widget and item layout to the size of the viewport(). +*/ +void Plot2d_Legend::layoutContents() +{ + QWidget* aContents = contentsWidget(); + if ( !aContents ) + return; + + // the qwt functionality of calculation the content of the legend use visible size and the legend could not be + // less than the visible size, so the legend's size is grown but can not be reduced. The behaviour is corrected here. + QSize aPrevSize = size(); + const QLayout* aLay = aContents->layout(); + if ( aLay && aLay->inherits( "QwtDynGridLayout" ) ) { + const QwtDynGridLayout* aDynLay = ( const QwtDynGridLayout* )aLay; + int aWidth = int( aDynLay->maxItemWidth() ) + 2*aDynLay->margin(); + QSize aSize = QSize( aWidth, aDynLay->heightForWidth( aWidth ) ); + aContents->resize( aSize ); + + if ( !aSize.isEmpty() ) + aSize = correctContentSize( aSize ); + resize( aSize ); + updateLegendPosition(); + } +} + +/** + Return if the given point belongs to axis + @param thePoint the point to be checked + @return if point belongs to axis +*/ +bool Plot2d_Legend::pick( const QPoint& thePoint ) const +{ + if( !myPlot || !isEnabled() ) + return false; + + return frameRect().contains( thePoint ); +} + +/** + Return the parent class for the legend to be attached to in the standard position type + @return legend font +*/ +QWidget* Plot2d_Legend::parentToAttach() const +{ + return myPlot ? myPlot->canvas() : NULL; +} + +/** + Insert legend in the plot widget +*/ +void Plot2d_Legend::insertLegend() +{ + if( !myPlot ) + return; + + //It's workaround - the legend should totaly rebuilded + //But if the plot is the same the legend doesn't rebuilded + if( myIsOnCanvas ) { + myPlot->insertLegend( this, QwtPlot::ExternalLegend ); + setParent( parentToAttach() ); + layoutContents(); + updateLegendPosition(); + show(); + } else { + setParent( myPlot ); + myPlot->insertLegend( this, qwtLegendPosition() ); + } +} + +/** + Update legend position taking into account the position type +*/ +void Plot2d_Legend::updateLegendPosition() +{ + if ( !parentWidget() ) + return; + + QRect aRect = parentWidget()->contentsRect(); + int aX = aRect.x(); + int anY = aRect.y(); + switch( myPosition ) { + case TopLeft: + break; + case BottomLeft: + anY = anY + aRect.height() - height(); + break; + case Top: + case Bottom: { + aX = aX + aRect.width()/2 - width()/2; + if ( myPosition == Bottom ) + anY = anY + aRect.height() - height(); + } + break; + case TopRight: + case BottomRight: { + aX = aX + aRect.width() - width(); + if ( myPosition == BottomRight ) + anY = anY + aRect.height() - height(); + } + break; + case Left: + case Right: { + anY = anY + aRect.height()/2 - height()/2; + if ( myPosition == Right ) + aX = aX + aRect.width() - width(); + } + break; + default: + break; + } + move( aX, anY ); +} + +/** + Correct the content size according to the parent widget size + @param theSize the size to be corrected + @return the rectangle +*/ +QSize Plot2d_Legend::correctContentSize( const QSize& theSize ) const +{ + if ( !myPlot || theSize.width() <=0 || theSize.height() <= 0 ) + return theSize; + + QWidget* aCont = const_cast( contentsWidget() ); + if ( !myPlot || !aCont ) + return theSize; + + int aWidth = theSize.width(); + int aHeight = theSize.height(); + + if ( parentWidget() ) { + int aCw = theSize.width(); + int aCh = theSize.height(); + + QRect aBoundRect = parentWidget()->contentsRect(); + int aPw = aBoundRect.width(); + int aPh = aBoundRect.height(); + + aWidth = qMin( aPw, aCw ); + aHeight = qMin( aPh, aCh ); + + // correction the legend width + if ( aPw > aCw ) { + aWidth = aCw; + if ( aCh > aPh ) { + QScrollBar* aScroll = new QScrollBar( Qt::Horizontal ); + aWidth += aScroll->sizeHint().height(); + aHeight = aPh; + delete aScroll; + } + } + // correction the legend height + if ( aPh > aCh ) { + aHeight = aCh; + if ( aCw > aPw ) { + QScrollBar* aScroll = new QScrollBar( Qt::Vertical ); + aHeight += aScroll->sizeHint().width(); + aWidth = aPw; + delete aScroll; + } + } + } + int aFrameW = 2*frameWidth(); + aWidth += aFrameW; + aHeight += aFrameW; + + return QSize( aWidth, aHeight ); +} + +QwtPlot::LegendPosition Plot2d_Legend::qwtLegendPosition() +{ + QwtPlot::LegendPosition result; + switch ( myPosition ) { + case Plot2d_Legend::Left: + result = QwtPlot::LeftLegend; + break; + case Plot2d_Legend::Right: + result = QwtPlot::RightLegend; + break; + case Plot2d_Legend::Top: + result = QwtPlot::TopLegend; + break; + case Plot2d_Legend::Bottom: + result = QwtPlot::BottomLegend; + break; + default: + result = QwtPlot::BottomLegend; + break; + } + return result; +} + +/** + Reimplementation of event filter (cavas actions process to calculate position of the legend) + @param theObj the object whose events are handled + @param theEvent the event instance + @return if event if handled by this event filter +*/ +bool Plot2d_Legend::eventFilter( QObject *theObj, QEvent *theEvent ) +{ + bool isProcessed = QwtLegend::eventFilter( theObj, theEvent ); + if ( myPlot && myIsOnCanvas && theObj == parentToAttach() ) { + if ( theEvent->type() == QEvent::Resize ) { + layoutContents(); + updateLegendPosition(); + } + } + return isProcessed; +} diff --git a/src/Plot2d/Plot2d_Legend.h b/src/Plot2d/Plot2d_Legend.h new file mode 100644 index 000000000..bf89f987d --- /dev/null +++ b/src/Plot2d/Plot2d_Legend.h @@ -0,0 +1,61 @@ + +#ifndef PLOT2D_LEGEND_H +#define PLOT2D_LEGEND_H + +#include "Plot2d.h" +#include +#include + +class QAction; + +class PLOT2D_EXPORT Plot2d_Legend : public QwtLegend +{ + Q_OBJECT + +public: + /** The legend position relative to the canvas */ + enum LegendPosition { Bottom = 0, Top = 1, Right = 2, Left = 3, + TopLeft = 4, TopRight = 5, BottomLeft = 6, BottomRight = 7 }; + + Plot2d_Legend( QwtPlot* thePlot ); + virtual ~Plot2d_Legend(); + + void init(); + + LegendPosition getPositionType() const; + void setPositionType( const LegendPosition& thePosition ); + + bool isEnabled() const; + void setEnabled( bool isEnable ); + + void setOnCanvas( bool ); + + virtual void setPlot( QwtPlot* thePlot ); + + virtual bool eventFilter( QObject* theObj, QEvent* theEvent ); + +protected slots: + void onPlotDestroyed(); + +protected: + virtual void layoutContents(); + + virtual bool pick( const QPoint& thePoint ) const; + + QWidget* parentToAttach() const; + + void insertLegend(); + void updateLegendPosition(); + QSize correctContentSize( const QSize& theSize ) const; + +private: + QwtPlot::LegendPosition qwtLegendPosition(); + +private: + bool myIsEnabled; + bool myIsOnCanvas; + LegendPosition myPosition; + QwtPlot* myPlot; +}; + +#endif diff --git a/src/Plot2d/Plot2d_SetupViewDlg.cxx b/src/Plot2d/Plot2d_SetupViewDlg.cxx index badc6a333..a5e9f9e4e 100755 --- a/src/Plot2d/Plot2d_SetupViewDlg.cxx +++ b/src/Plot2d/Plot2d_SetupViewDlg.cxx @@ -85,13 +85,15 @@ Plot2d_SetupViewDlg::Plot2d_SetupViewDlg( QWidget* parent, // legend myLegendCheck = new QCheckBox( tr( "PLOT2D_ENABLE_LEGEND" ), this ); + myLegendTypeCombo = new QComboBox( this ); + myLegendTypeCombo->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); + myLegendTypeCombo->setMinimumWidth( MIN_COMBO_WIDTH ); + myLegendTypeCombo->addItem( tr("PLOT2D_EXTERNAL") ); + myLegendTypeCombo->addItem( tr("PLOT2D_EMBEDDED") ); myLegendCombo = new QComboBox( this ); myLegendCombo->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); myLegendCombo->setMinimumWidth( MIN_COMBO_WIDTH ); - myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_LEFT" ) ); - myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_RIGHT" ) ); - myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_TOP" ) ); - myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_BOTTOM" ) ); + onLegendTypeChanged(); // marker size QLabel* aMarkerLab = new QLabel( tr( "PLOT2D_MARKER_SIZE_LBL" ), this ); @@ -320,20 +322,22 @@ Plot2d_SetupViewDlg::Plot2d_SetupViewDlg( QWidget* parent, topLayout->addWidget( myTitleEdit, 0, 1, 1, 3 ); topLayout->addWidget( aCurveLab, 1, 0 ); topLayout->addWidget( myCurveCombo, 1, 1 ); - topLayout->addWidget( myLegendCheck, 1, 2 ); - topLayout->addWidget( myLegendCombo, 1, 3 ); - topLayout->addWidget( aMarkerLab, 2, 0 ); - topLayout->addWidget( myMarkerSpin, 2, 1 ); + topLayout->addWidget( myLegendCheck, 2, 0 ); + topLayout->addWidget( myLegendTypeCombo, 2, 1 ); + topLayout->addWidget( myLegendCombo, 2, 2, 1, 1 ); + topLayout->addWidget( aMarkerLab, 3, 0 ); + topLayout->addWidget( myMarkerSpin, 3, 1 ); QHBoxLayout* bgLayout = new QHBoxLayout; - bgLayout->addWidget( myBackgroundBtn ); bgLayout->addStretch(); - topLayout->addWidget( aBGLab, 2, 2 ); - topLayout->addLayout( bgLayout, 2, 3 ); - topLayout->addWidget( aScaleGrp, 3, 0, 1, 4 ); - topLayout->addWidget( aTabWidget, 4, 0, 1, 4 ); - topLayout->addWidget( myDefCheck, 5, 0, 1, 4 ); + bgLayout->addWidget( myBackgroundBtn ); + bgLayout->addStretch(); + topLayout->addWidget( aBGLab, 3, 2 ); + topLayout->addLayout( bgLayout, 3, 3 ); + topLayout->addWidget( aScaleGrp, 4, 0, 1, 4 ); + topLayout->addWidget( aTabWidget, 5, 0, 1, 4 ); + topLayout->addWidget( myDefCheck, 6, 0, 1, 4 ); topLayout->setRowStretch( 5, 5 ); - topLayout->addLayout( btnLayout, 6, 0, 1, 4 ); + topLayout->addLayout( btnLayout, 7, 0, 1, 4 ); if ( !showDefCheck ) myDefCheck->hide(); @@ -356,6 +360,8 @@ Plot2d_SetupViewDlg::Plot2d_SetupViewDlg( QWidget* parent, connect( myY2GridCheck, SIGNAL( clicked() ), this, SLOT( onY2GridMajorChecked() ) ); connect( myY2MinGridCheck, SIGNAL( clicked() ), this, SLOT( onY2GridMinorChecked() ) ); } + connect( myLegendTypeCombo, SIGNAL( activated ( int ) ), this, SLOT( onLegendTypeChanged() ) ); + // init fields setBackgroundColor( Qt::gray ); @@ -631,6 +637,23 @@ void Plot2d_SetupViewDlg::setLegend( bool enable, int pos ) onLegendChecked(); } +/*! + \brief Set legend on canvas. + \param if \c true legend is on canvas (embedded) + \sa isLegendEnabled(), getLegendPos() +*/ +void Plot2d_SetupViewDlg::setLegendOnCanvas( bool isOn ) +{ + myLegendTypeCombo->setCurrentIndex( isOn ? 1 /*Embedded*/ : 0 /*External*/ ); + onLegendTypeChanged(); +} + +bool Plot2d_SetupViewDlg::isLegendOnCanvas() +{ + return myLegendTypeCombo->currentIndex() == 1; +} + + /*! \brief Check if legend is enabled. \return \c true if legend is enabled @@ -871,6 +894,7 @@ void Plot2d_SetupViewDlg::onY2TitleChecked() void Plot2d_SetupViewDlg::onLegendChecked() { myLegendCombo->setEnabled( myLegendCheck->isChecked() ); + myLegendTypeCombo->setEnabled( myLegendCheck->isChecked() ); } /*! @@ -918,6 +942,27 @@ void Plot2d_SetupViewDlg::onY2GridMinorChecked() { } +void Plot2d_SetupViewDlg::onLegendTypeChanged() +{ + if( !myLegendTypeCombo ) + return; + int idx = myLegendCombo->currentIndex(); + myLegendCombo->clear(); + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_BOTTOM" ) ); + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_TOP" ) ); + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_RIGHT" ) ); + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_LEFT" ) ); + if( myLegendTypeCombo->currentIndex() == 1 ) { //Embedded + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_TOPLEFT" ) ); + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_TOPRIGHT" ) ); + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_BOTTOMLEFT" ) ); + myLegendCombo->addItem( tr( "PLOT2D_LEGEND_POSITION_BOTTOMRIGHT" ) ); + } + if( idx >= myLegendCombo->count() ) + idx = 0; + myLegendCombo->setCurrentIndex( idx ); +} + /*! \brief Get "Set settings as default" check box value. \return \c true if "Set settings as default" check box is on diff --git a/src/Plot2d/Plot2d_SetupViewDlg.h b/src/Plot2d/Plot2d_SetupViewDlg.h index 342a64f04..2c581e15c 100755 --- a/src/Plot2d/Plot2d_SetupViewDlg.h +++ b/src/Plot2d/Plot2d_SetupViewDlg.h @@ -71,6 +71,8 @@ public: void setLegend( bool, int ); bool isLegendEnabled(); + void setLegendOnCanvas( bool ); + bool isLegendOnCanvas(); int getLegendPos(); void setMarkerSize( const int ); @@ -103,6 +105,7 @@ protected slots: void onYGridMinorChecked(); void onY2GridMinorChecked(); void onHelp(); + void onLegendTypeChanged(); private: QCheckBox* myTitleCheck; @@ -128,6 +131,7 @@ private: QSpinBox* myY2MinGridSpin; QComboBox* myCurveCombo; QCheckBox* myLegendCheck; + QComboBox* myLegendTypeCombo; QComboBox* myLegendCombo; QSpinBox* myMarkerSpin; QComboBox* myXModeCombo; diff --git a/src/Plot2d/Plot2d_ViewFrame.cxx b/src/Plot2d/Plot2d_ViewFrame.cxx index 3a38103bb..5f97c97f3 100755 --- a/src/Plot2d/Plot2d_ViewFrame.cxx +++ b/src/Plot2d/Plot2d_ViewFrame.cxx @@ -24,6 +24,7 @@ #include "Plot2d_ViewWindow.h" #include "Plot2d_SetupViewDlg.h" #include "Plot2d_ToolTip.h" +#include "Plot2d_Legend.h" #include "SUIT_Tools.h" #include "SUIT_Session.h" @@ -154,7 +155,7 @@ Plot2d_ViewFrame::Plot2d_ViewFrame( QWidget* parent, const QString& title ) : QWidget (parent, 0), myOperation( NoOpId ), myCurveType( 1 ), - myShowLegend( true ), myLegendPos( 1 ), + myShowLegend( true ), myLegendPos( 1 ), myIsLegendOnCanvas( false ), myMarkerSize( DEFAULT_MARKER_SIZE ), myBackground( Qt::white ), myTitle( "" ), myXTitle( "" ), myYTitle( "" ), myY2Title( "" ), @@ -197,7 +198,7 @@ Plot2d_ViewFrame::Plot2d_ViewFrame( QWidget* parent, const QString& title ) setVerScaleMode( myYMode, false ); setBackgroundColor( myBackground ); setLegendPos( myLegendPos ); - showLegend( myShowLegend, false ); + showLegend( myShowLegend, true ); myPlot->replot(); if ( parent ) { @@ -380,6 +381,7 @@ void Plot2d_ViewFrame::readPreferences() setCurveType( resMgr->integerValue( "Plot2d", "CurveType", myCurveType ) ); myShowLegend = resMgr->booleanValue( "Plot2d", "ShowLegend", myShowLegend ); + myIsLegendOnCanvas = resMgr->booleanValue( "Plot2d", "IsLegendOnCanvas", myIsLegendOnCanvas ); myLegendPos = resMgr->integerValue( "Plot2d", "LegendPos", myLegendPos ); myMarkerSize = resMgr->integerValue( "Plot2d", "MarkerSize", myMarkerSize ); myBackground = resMgr->colorValue( "Plot2d", "Background", myBackground ); @@ -423,6 +425,7 @@ void Plot2d_ViewFrame::writePreferences() resMgr->setValue( "Plot2d", "CurveType", myCurveType ); resMgr->setValue( "Plot2d", "ShowLegend", myShowLegend ); + resMgr->setValue( "Plot2d", "IsLegendOnCanvas", myIsLegendOnCanvas ); resMgr->setValue( "Plot2d", "LegendPos", myLegendPos ); resMgr->setValue( "Plot2d", "MarkerSize", myMarkerSize ); resMgr->setValue( "Plot2d", "Background", myBackground ); @@ -995,6 +998,7 @@ void Plot2d_ViewFrame::onSettings() if (mySecondY) dlg->setY2Title( myY2TitleEnabled, myY2Title ); dlg->setCurveType( myCurveType ); + dlg->setLegendOnCanvas( myIsLegendOnCanvas ); dlg->setLegend( myShowLegend, myLegendPos ); dlg->setMarkerSize( myMarkerSize ); dlg->setBackgroundColor( myBackground ); @@ -1007,6 +1011,7 @@ void Plot2d_ViewFrame::onSettings() myYGridMinorEnabled, myPlot->axisMaxMinor( QwtPlot::yLeft ), myY2GridMinorEnabled, myPlot->axisMaxMinor( QwtPlot::yRight ) ); if ( dlg->exec() == QDialog::Accepted ) { + // horizontal axis title setTitle( dlg->isXTitleEnabled(), dlg->getXTitle(), XTitle, false ); // vertical left axis title @@ -1022,13 +1027,13 @@ void Plot2d_ViewFrame::onSettings() if ( myCurveType != dlg->getCurveType() ) { setCurveType( dlg->getCurveType(), false ); } - // legend - if ( myShowLegend != dlg->isLegendEnabled() ) { - showLegend( dlg->isLegendEnabled(), false ); - } - if ( myLegendPos != dlg->getLegendPos() ) { - setLegendPos( dlg->getLegendPos() ); - } + + // Set legend props + myIsLegendOnCanvas = dlg->isLegendOnCanvas(); + setLegendPos( dlg->getLegendPos() ); + //Show/Hide or just update it + showLegend( dlg->isLegendEnabled(), true ); + // marker size if ( myMarkerSize != dlg->getMarkerSize() ) { setMarkerSize( dlg->getMarkerSize(), false ); @@ -1137,43 +1142,37 @@ void Plot2d_ViewFrame::setObjectTitle( Plot2d_Object* object, const QString& tit void Plot2d_ViewFrame::showLegend( bool show, bool update ) { myShowLegend = show; - if ( myShowLegend ) { - QwtLegend* legend = myPlot->legend(); - if ( !legend ) { - legend = new QwtLegend( myPlot ); - legend->setFrameStyle( QFrame::Box | QFrame::Sunken ); + Plot2d_Legend* aLegend = myPlot->getLegend(); + if( !aLegend ) { + QwtLegend* anOldLegend = myPlot->legend(); + if( anOldLegend ){ + myPlot->insertLegend(0); + anOldLegend->deleteLater(); } - legend->setItemMode( QwtLegend::ClickableItem ); - myPlot->insertLegend( legend ); - setLegendPos( myLegendPos ); + aLegend = new Plot2d_Legend( myPlot ); + aLegend->setItemMode( Plot2d_Legend::ClickableItem ); } - else - myPlot->insertLegend( 0 ); + if( !myIsLegendOnCanvas ) + myPlot->insertLegend( aLegend ); + setLegendPos( myLegendPos ); + aLegend->setEnabled( show ); + if ( update ) myPlot->replot(); } /*! - Sets legend position : 0 - left, 1 - right, 2 - top, 3 - bottom + Sets legend position : Bottom 0, Top = 1, Right = 2, Left = 3, + TopLeft = 4, TopRight = 5, BottomLeft = 6, BottomRight = 7 */ void Plot2d_ViewFrame::setLegendPos( int pos ) { myLegendPos = pos; - QwtLegend* legend = myPlot->legend(); - switch( pos ) { - case 0: - myPlot->insertLegend( legend, QwtPlot::LeftLegend ); - break; - case 1: - myPlot->insertLegend( legend, QwtPlot::RightLegend ); - break; - case 2: - myPlot->insertLegend( legend, QwtPlot::TopLegend ); - break; - case 3: - myPlot->insertLegend( legend, QwtPlot::BottomLegend ); - break; - } + Plot2d_Legend* legend = myPlot->getLegend(); + if( !legend ) + return; + legend->setOnCanvas( myIsLegendOnCanvas ); + legend->setPositionType( static_cast( pos ) ); } /*! @@ -1798,6 +1797,18 @@ void Plot2d_Plot2d::replot() QwtPlot::replot(); } +/* + * Returns legend. + */ +Plot2d_Legend* Plot2d_Plot2d::getLegend() +{ +#if QWT_VERSION < 0x040200 + return d_legend; +#else + return dynamic_cast( legend() ); +#endif +} + /*! \return the default layout behavior of the widget */ @@ -1869,6 +1880,7 @@ void Plot2d_ViewFrame::copyPreferences( Plot2d_ViewFrame* vf ) myCurveType = vf->myCurveType; myShowLegend = vf->myShowLegend; + myIsLegendOnCanvas = vf->myIsLegendOnCanvas; myLegendPos = vf->myLegendPos; myMarkerSize = vf->myMarkerSize; myBackground = vf->myBackground; @@ -2193,3 +2205,5 @@ void Plot2d_ViewFrame::customEvent( QEvent* ce ) if ( ce->type() == FITALL_EVENT ) fitAll(); } + + diff --git a/src/Plot2d/Plot2d_ViewFrame.h b/src/Plot2d/Plot2d_ViewFrame.h index 350dbdc60..3ab865724 100755 --- a/src/Plot2d/Plot2d_ViewFrame.h +++ b/src/Plot2d/Plot2d_ViewFrame.h @@ -28,6 +28,7 @@ class Plot2d_Plot2d; class Plot2d_Prs; class Plot2d_SetupViewDlg; +class Plot2d_Legend; class QCustomEvent; class QwtPlotCurve; class QwtPlotGrid; @@ -201,6 +202,9 @@ signals: void vpCurveChanged(); void contextMenuRequested( QContextMenuEvent *e ); +private: + void setExternalLegendPos( int ); + protected: Plot2d_Plot2d* myPlot; int myOperation; @@ -208,6 +212,7 @@ protected: int myCurveType; bool myShowLegend; + bool myIsLegendOnCanvas; int myLegendPos; int myMarkerSize; QColor myBackground; @@ -233,14 +238,9 @@ public: void setLogScale( int axisId, bool log10 ); - void replot(); - QwtLegend* getLegend() { -#if QWT_VERSION < 0x040200 - return d_legend; -#else - return legend(); /* mpv: porting to the Qwt 4.2.0 */ -#endif - } + void replot(); + Plot2d_Legend* getLegend(); + virtual QSize sizeHint() const; virtual QSizePolicy sizePolicy() const; virtual QSize minimumSizeHint() const; diff --git a/src/Plot2d/resources/Plot2d_msg_en.ts b/src/Plot2d/resources/Plot2d_msg_en.ts index c75674898..7cd9d4a29 100644 --- a/src/Plot2d/resources/Plot2d_msg_en.ts +++ b/src/Plot2d/resources/Plot2d_msg_en.ts @@ -323,6 +323,30 @@ Logarithmic scale for ordinate axis is not allowed. PLOT2D_LEGEND_POSITION_RIGHT Right + + PLOT2D_LEGEND_POSITION_TOPLEFT + Top Left + + + PLOT2D_LEGEND_POSITION_TOPRIGHT + Top Right + + + PLOT2D_LEGEND_POSITION_BOTTOMLEFT + Bottom Left + + + PLOT2D_LEGEND_POSITION_BOTTOMRIGHT + Bottom Right + + + PLOT2D_EXTERNAL + External + + + PLOT2D_EMBEDDED + Embedded + DSC_DUMP_VIEW Saves the active view in the image file -- 2.39.2