///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2016- Statoil ASA // // ResInsight is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU General Public License at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "RiuSummaryQwtPlot.h" #include "RiaApplication.h" #include "RimSummaryCurve.h" #include "RimSummaryPlot.h" #include "RiuMainPlotWindow.h" #include "qwt_date_scale_draw.h" #include "qwt_date_scale_engine.h" #include "qwt_legend.h" #include "qwt_plot_curve.h" #include "qwt_plot_grid.h" #include "qwt_plot_layout.h" #include "qwt_plot_panner.h" #include "qwt_plot_zoomer.h" #include "qwt_scale_engine.h" #include #include #include //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RiuSummaryQwtPlot::RiuSummaryQwtPlot(RimSummaryPlot* plotDefinition, QWidget* parent) : QwtPlot(parent) { Q_ASSERT(plotDefinition); m_plotDefinition = plotDefinition; m_grid = new QwtPlotGrid; m_grid->attach(this); setDefaults(); // LeftButton for the zooming zoomer = new QwtPlotZoomer(canvas()); zoomer->setRubberBandPen(QColor(Qt::black)); zoomer->setTrackerMode(QwtPicker::AlwaysOff); zoomer->setTrackerPen(QColor(Qt::black)); zoomer->initMousePattern(1); // MidButton for the panning QwtPlotPanner* panner = new QwtPlotPanner(canvas()); panner->setMouseButton(Qt::MidButton); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RiuSummaryQwtPlot::~RiuSummaryQwtPlot() { m_grid->detach(); delete m_grid; if (m_plotDefinition) { m_plotDefinition->handleViewerDeletion(); } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimSummaryPlot* RiuSummaryQwtPlot::ownerPlotDefinition() { return m_plotDefinition; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RiuSummaryQwtPlot::setYAxisTitle(const QString& title) { QwtText axisTitleY = axisTitle(QwtPlot::yLeft); axisTitleY.setText(title); setAxisTitle(QwtPlot::yLeft, axisTitleY); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RiuSummaryQwtPlot::zoomAll() { setAxisAutoScale(yLeft, true); setAxisAutoScale(xBottom, true); zoomer->setZoomBase(true); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QRectF RiuSummaryQwtPlot::currentVisibleWindow() const { QRectF scaleRect; scaleRect.setLeft(axisScaleDiv(xBottom).lowerBound()); scaleRect.setRight(axisScaleDiv(xBottom).upperBound()); scaleRect.setBottom(axisScaleDiv(yLeft).upperBound()); scaleRect.setTop(axisScaleDiv(yLeft).lowerBound()); return scaleRect; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RiuSummaryQwtPlot::setZoomWindow(const QRectF& zoomWindow) { zoomer->zoom(zoomWindow); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RiuSummaryQwtPlot::setDefaults() { QPalette newPalette(palette()); newPalette.setColor(QPalette::Background, Qt::white); setPalette(newPalette); setAutoFillBackground(true); setCanvasBackground(Qt::white); QFrame* canvasFrame = dynamic_cast(canvas()); if (canvasFrame) { canvasFrame->setFrameShape(QFrame::NoFrame); } canvas()->setMouseTracking(true); canvas()->installEventFilter(this); QPen gridPen(Qt::SolidLine); gridPen.setColor(Qt::lightGray); m_grid->setPen(gridPen); enableAxis(QwtPlot::xBottom, true); enableAxis(QwtPlot::yLeft, true); enableAxis(QwtPlot::xTop, false); enableAxis(QwtPlot::yRight, false); plotLayout()->setAlignCanvasToScales(true); QwtDateScaleDraw* scaleDraw = new QwtDateScaleDraw(Qt::UTC); scaleDraw->setDateFormat(QwtDate::Year, QString("dd-MM-yyyy")); QwtDateScaleEngine* scaleEngine = new QwtDateScaleEngine(Qt::UTC); setAxisScaleEngine(QwtPlot::xBottom, scaleEngine); setAxisScaleDraw(QwtPlot::xBottom, scaleDraw); QFont xAxisFont = axisFont(QwtPlot::xBottom); xAxisFont.setPixelSize(9); setAxisFont(QwtPlot::xBottom, xAxisFont); QFont yAxisFont = axisFont(QwtPlot::yLeft); yAxisFont.setPixelSize(9); setAxisFont(QwtPlot::yLeft, yAxisFont); QwtText axisTitleY = axisTitle(QwtPlot::yLeft); QFont yAxisTitleFont = axisTitleY.font(); yAxisTitleFont.setPixelSize(9); yAxisTitleFont.setBold(false); axisTitleY.setFont(yAxisTitleFont); axisTitleY.setRenderFlags(Qt::AlignRight); setAxisTitle(QwtPlot::yLeft, axisTitleY); QwtLegend* legend = new QwtLegend(this); // The legend will be deleted in the destructor of the plot or when // another legend is inserted. this->insertLegend(legend, BottomLegend); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool RiuSummaryQwtPlot::eventFilter(QObject* watched, QEvent* event) { if(watched == canvas()) { QWheelEvent* wheelEvent = dynamic_cast(event); if(wheelEvent) { #if 0 if(!m_plotDefinition) { return QwtPlot::eventFilter(watched, event); } if(wheelEvent->modifiers() & Qt::ControlModifier) { QwtScaleMap scaleMap = canvasMap(QwtPlot::yLeft); double zoomCenter = scaleMap.invTransform(wheelEvent->pos().y()); if(wheelEvent->delta() > 0) { plotDefinition->setDepthZoomByFactorAndCenter(RIU_SCROLLWHEEL_ZOOMFACTOR, zoomCenter); } else { plotDefinition->setDepthZoomByFactorAndCenter(1.0/RIU_SCROLLWHEEL_ZOOMFACTOR, zoomCenter); } } else { plotDefinition->panDepth(wheelEvent->delta() < 0 ? RIU_SCROLLWHEEL_PANFACTOR : -RIU_SCROLLWHEEL_PANFACTOR); } event->accept(); return true; #endif } else { QMouseEvent* mouseEvent = dynamic_cast(event); if(mouseEvent) { if(mouseEvent->button() == Qt::LeftButton && mouseEvent->type() == QMouseEvent::MouseButtonRelease) { selectClosestCurve(mouseEvent->pos()); } } } } return QwtPlot::eventFilter(watched, event); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RiuSummaryQwtPlot::selectClosestCurve(const QPoint& pos) { QwtPlotCurve* closestCurve = NULL; double distMin = DBL_MAX; const QwtPlotItemList& itmList = itemList(); for(QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); it++) { if((*it)->rtti() == QwtPlotItem::Rtti_PlotCurve) { QwtPlotCurve* candidateCurve = static_cast(*it); double dist = DBL_MAX; candidateCurve->closestPoint(pos, &dist); if(dist < distMin) { closestCurve = candidateCurve; distMin = dist; } } } if(closestCurve && distMin < 20) { RimSummaryCurve* selectedCurve = m_plotDefinition->findRimCurveFromQwtCurve(closestCurve); if(selectedCurve) { RiaApplication::instance()->getOrCreateAndShowMainPlotWindow()->selectAsCurrentItem(selectedCurve); } } }