2019-02-21 05:52:23 -06:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2019- Equinor 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 <http://www.gnu.org/licenses/gpl.html>
|
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "RiuQwtPlotTools.h"
|
|
|
|
|
2020-09-04 10:10:55 -05:00
|
|
|
#include "RiuGuiTheme.h"
|
|
|
|
|
2019-08-19 02:37:42 -05:00
|
|
|
#include "RiaApplication.h"
|
|
|
|
#include "RiaPreferences.h"
|
2022-03-14 03:18:48 -05:00
|
|
|
#include "RiaQDateTimeTools.h"
|
2019-08-19 02:37:42 -05:00
|
|
|
|
2019-02-25 07:54:36 -06:00
|
|
|
#include "qwt_date_scale_draw.h"
|
|
|
|
#include "qwt_date_scale_engine.h"
|
2019-02-21 05:52:23 -06:00
|
|
|
#include "qwt_plot.h"
|
|
|
|
#include "qwt_plot_grid.h"
|
|
|
|
#include "qwt_plot_layout.h"
|
2020-04-20 02:35:59 -05:00
|
|
|
#include "qwt_plot_shapeitem.h"
|
2019-10-11 08:54:19 -05:00
|
|
|
#include "qwt_scale_widget.h"
|
2019-02-21 05:52:23 -06:00
|
|
|
|
2019-08-19 02:37:42 -05:00
|
|
|
#include <QRegExp>
|
2019-02-21 05:52:23 -06:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RiuQwtPlotTools::setCommonPlotBehaviour( QwtPlot* plot )
|
2019-02-21 05:52:23 -06:00
|
|
|
{
|
|
|
|
// Plot background and frame look
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QPalette newPalette( plot->palette() );
|
2019-12-04 02:46:00 -06:00
|
|
|
newPalette.setColor( QPalette::Window, Qt::white );
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->setPalette( newPalette );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->setAutoFillBackground( true );
|
|
|
|
plot->setCanvasBackground( Qt::white );
|
2020-01-17 01:11:01 -06:00
|
|
|
plot->plotLayout()->setCanvasMargin( 0, -1 );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QFrame* canvasFrame = dynamic_cast<QFrame*>( plot->canvas() );
|
2019-10-11 08:54:19 -05:00
|
|
|
canvasFrame->setFrameShape( QFrame::Box );
|
|
|
|
|
2019-02-21 05:52:23 -06:00
|
|
|
// Grid
|
|
|
|
QwtPlotGrid* grid = new QwtPlotGrid;
|
2019-09-06 03:40:57 -05:00
|
|
|
grid->attach( plot );
|
|
|
|
QPen gridPen( Qt::SolidLine );
|
|
|
|
grid->setPen( gridPen );
|
2020-09-04 10:10:55 -05:00
|
|
|
RiuGuiTheme::styleQwtItem( grid );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
|
|
|
// Axis number font
|
2020-05-09 04:23:58 -05:00
|
|
|
int axisFontSize = caf::FontTools::absolutePointSize( RiaPreferences::current()->defaultPlotFontSize(),
|
|
|
|
caf::FontTools::RelativeSize::Medium );
|
|
|
|
QFont axisFont = plot->axisFont( QwtPlot::xBottom );
|
|
|
|
axisFont.setPixelSize( caf::FontTools::pointSizeToPixelSize( axisFontSize ) );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->setAxisFont( QwtPlot::xBottom, axisFont );
|
|
|
|
plot->setAxisFont( QwtPlot::xTop, axisFont );
|
|
|
|
plot->setAxisFont( QwtPlot::yLeft, axisFont );
|
|
|
|
plot->setAxisFont( QwtPlot::yRight, axisFont );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
|
|
|
// Axis title font
|
2020-11-06 03:46:38 -06:00
|
|
|
std::vector<QwtPlot::Axis> axes = { QwtPlot::xBottom, QwtPlot::xTop, QwtPlot::yLeft, QwtPlot::yRight };
|
2019-08-19 02:37:42 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
for ( QwtPlot::Axis axis : axes )
|
2019-02-21 05:52:23 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QwtText axisTitle = plot->axisTitle( axis );
|
2019-02-21 05:52:23 -06:00
|
|
|
QFont axisTitleFont = axisTitle.font();
|
2020-05-09 04:23:58 -05:00
|
|
|
axisTitleFont.setPixelSize( caf::FontTools::pointSizeToPixelSize( axisFontSize ) );
|
2019-09-06 03:40:57 -05:00
|
|
|
axisTitleFont.setBold( false );
|
|
|
|
axisTitle.setFont( axisTitleFont );
|
|
|
|
axisTitle.setRenderFlags( Qt::AlignRight );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->setAxisTitle( axis, axisTitle );
|
2019-02-21 05:52:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set a focus policy to allow it taking key press events.
|
|
|
|
// This is not strictly necessary since this widget inherit QwtPlot
|
|
|
|
// which already has a focus policy.
|
|
|
|
// However, for completeness we still do it here.
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->setFocusPolicy( Qt::WheelFocus );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
|
|
|
// Enable mousetracking and event filter
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->canvas()->setMouseTracking( true );
|
|
|
|
plot->plotLayout()->setAlignCanvasToScales( true );
|
2019-04-15 02:58:08 -05:00
|
|
|
|
2020-01-17 01:11:01 -06:00
|
|
|
plot->setContentsMargins( 1, 1, 1, 1 );
|
2019-10-11 08:54:19 -05:00
|
|
|
|
|
|
|
// Store the pointer address as an object name. This way each plot can be identified uniquely for CSS-stylesheets
|
|
|
|
QString objectName = QString( "%1" ).arg( reinterpret_cast<uint64_t>( plot ) );
|
|
|
|
plot->setObjectName( objectName );
|
|
|
|
|
|
|
|
QString canvasName = QString( "%1" ).arg( reinterpret_cast<uint64_t>( plot->canvas() ) );
|
|
|
|
plot->canvas()->setObjectName( canvasName );
|
2019-02-21 05:52:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RiuQwtPlotTools::setDefaultAxes( QwtPlot* plot )
|
2019-02-21 05:52:23 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->enableAxis( QwtPlot::xBottom, true );
|
|
|
|
plot->enableAxis( QwtPlot::yLeft, true );
|
|
|
|
plot->enableAxis( QwtPlot::xTop, false );
|
|
|
|
plot->enableAxis( QwtPlot::yRight, false );
|
2019-02-21 05:52:23 -06:00
|
|
|
|
2019-10-11 08:54:19 -05:00
|
|
|
plot->axisWidget( QwtPlot::xBottom )->setMargin( 0 );
|
|
|
|
plot->axisWidget( QwtPlot::yLeft )->setMargin( 0 );
|
2020-01-17 01:11:01 -06:00
|
|
|
plot->axisWidget( QwtPlot::xTop )->setMargin( 0 );
|
|
|
|
plot->axisWidget( QwtPlot::yRight )->setMargin( 0 );
|
2019-10-11 08:54:19 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
plot->setAxisMaxMinor( QwtPlot::xBottom, 2 );
|
|
|
|
plot->setAxisMaxMinor( QwtPlot::yLeft, 3 );
|
2019-02-21 05:52:23 -06:00
|
|
|
}
|
2019-02-25 07:54:36 -06:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
void RiuQwtPlotTools::enableDateBasedBottomXAxis( QwtPlot* plot,
|
|
|
|
const QString& dateFormat,
|
|
|
|
const QString& timeFormat,
|
|
|
|
RiaDefines::DateFormatComponents dateComponents,
|
|
|
|
RiaDefines::TimeFormatComponents timeComponents )
|
2019-02-25 07:54:36 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QwtDateScaleDraw* scaleDraw = new QwtDateScaleDraw( Qt::UTC );
|
2019-08-19 02:37:42 -05:00
|
|
|
|
2020-11-06 03:46:38 -06:00
|
|
|
std::set<QwtDate::IntervalType> intervals = { QwtDate::Year,
|
|
|
|
QwtDate::Month,
|
|
|
|
QwtDate::Week,
|
|
|
|
QwtDate::Day,
|
|
|
|
QwtDate::Hour,
|
|
|
|
QwtDate::Minute,
|
|
|
|
QwtDate::Second,
|
|
|
|
QwtDate::Millisecond };
|
2019-08-19 02:37:42 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
for ( QwtDate::IntervalType interval : intervals )
|
2019-08-19 02:37:42 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
scaleDraw->setDateFormat( interval,
|
2020-02-12 04:43:15 -06:00
|
|
|
dateTimeFormatForInterval( interval, dateFormat, timeFormat, dateComponents, timeComponents ) );
|
2019-08-19 02:37:42 -05:00
|
|
|
}
|
2019-02-25 07:54:36 -06:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QwtDateScaleEngine* scaleEngine = new QwtDateScaleEngine( Qt::UTC );
|
|
|
|
plot->setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
|
|
|
|
plot->setAxisScaleDraw( QwtPlot::xBottom, scaleDraw );
|
2019-08-19 02:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
QString RiuQwtPlotTools::dateTimeFormatForInterval( QwtDate::IntervalType interval,
|
|
|
|
const QString& dateFormat,
|
|
|
|
const QString& timeFormat,
|
|
|
|
RiaDefines::DateFormatComponents dateComponents,
|
|
|
|
RiaDefines::TimeFormatComponents timeComponents )
|
2019-08-19 02:37:42 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
if ( dateComponents != RiaDefines::DateFormatComponents::DATE_FORMAT_UNSPECIFIED &&
|
|
|
|
timeComponents != RiaDefines::TimeFormatComponents::TIME_FORMAT_UNSPECIFIED )
|
2019-09-06 03:40:57 -05:00
|
|
|
{
|
|
|
|
return RiaQDateTimeTools::timeFormatString( timeFormat, timeComponents ) + "\n" +
|
|
|
|
RiaQDateTimeTools::dateFormatString( dateFormat, dateComponents );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch ( interval )
|
|
|
|
{
|
|
|
|
case QwtDate::Millisecond:
|
|
|
|
return RiaQDateTimeTools::timeFormatString( timeFormat,
|
2022-03-14 03:18:48 -05:00
|
|
|
RiaDefines::TimeFormatComponents::TIME_FORMAT_HOUR_MINUTE_SECOND_MILLISECOND );
|
2019-09-06 03:40:57 -05:00
|
|
|
case QwtDate::Second:
|
2020-04-24 01:10:48 -05:00
|
|
|
return RiaQDateTimeTools::timeFormatString( timeFormat,
|
2022-03-14 03:18:48 -05:00
|
|
|
RiaDefines::TimeFormatComponents::TIME_FORMAT_HOUR_MINUTE_SECOND );
|
2019-09-06 03:40:57 -05:00
|
|
|
case QwtDate::Minute:
|
|
|
|
{
|
2020-02-12 04:43:15 -06:00
|
|
|
QString fullFormat =
|
2020-04-24 01:10:48 -05:00
|
|
|
RiaQDateTimeTools::timeFormatString( timeFormat,
|
2022-03-14 03:18:48 -05:00
|
|
|
RiaDefines::TimeFormatComponents::TIME_FORMAT_HOUR_MINUTE );
|
2019-09-06 03:40:57 -05:00
|
|
|
fullFormat += "\n";
|
2020-02-12 04:43:15 -06:00
|
|
|
fullFormat +=
|
2022-03-14 03:18:48 -05:00
|
|
|
RiaQDateTimeTools::dateFormatString( dateFormat,
|
|
|
|
RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY );
|
2019-09-06 03:40:57 -05:00
|
|
|
return fullFormat;
|
|
|
|
}
|
|
|
|
case QwtDate::Hour:
|
|
|
|
{
|
2020-04-24 01:10:48 -05:00
|
|
|
QString fullFormat =
|
2022-03-14 03:18:48 -05:00
|
|
|
RiaQDateTimeTools::timeFormatString( timeFormat, RiaDefines::TimeFormatComponents::TIME_FORMAT_HOUR );
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( !fullFormat.endsWith( "AP" ) )
|
|
|
|
{
|
|
|
|
fullFormat += ":00";
|
|
|
|
}
|
|
|
|
fullFormat += "\n";
|
2020-02-12 04:43:15 -06:00
|
|
|
fullFormat +=
|
2022-03-14 03:18:48 -05:00
|
|
|
RiaQDateTimeTools::dateFormatString( dateFormat,
|
|
|
|
RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY );
|
2019-09-06 03:40:57 -05:00
|
|
|
return fullFormat;
|
|
|
|
}
|
|
|
|
case QwtDate::Day:
|
2022-03-14 03:18:48 -05:00
|
|
|
return RiaQDateTimeTools::dateFormatString( dateFormat,
|
|
|
|
RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY );
|
2019-09-06 03:40:57 -05:00
|
|
|
case QwtDate::Week:
|
2022-03-14 03:18:48 -05:00
|
|
|
return RiaQDateTimeTools::dateFormatString( dateFormat,
|
|
|
|
RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH );
|
2019-09-06 03:40:57 -05:00
|
|
|
case QwtDate::Month:
|
2022-03-14 03:18:48 -05:00
|
|
|
return RiaQDateTimeTools::dateFormatString( dateFormat,
|
|
|
|
RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH );
|
2019-09-06 03:40:57 -05:00
|
|
|
case QwtDate::Year:
|
2022-03-14 03:18:48 -05:00
|
|
|
return RiaQDateTimeTools::dateFormatString( dateFormat, RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR );
|
2019-09-06 03:40:57 -05:00
|
|
|
default:
|
2022-03-14 03:18:48 -05:00
|
|
|
return RiaQDateTimeTools::dateFormatString( dateFormat,
|
|
|
|
RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY );
|
2019-09-06 03:40:57 -05:00
|
|
|
}
|
2019-08-23 09:23:07 -05:00
|
|
|
}
|
2019-08-19 02:37:42 -05:00
|
|
|
}
|
2020-04-20 02:35:59 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-05-04 12:47:32 -05:00
|
|
|
QwtPlotShapeItem* RiuQwtPlotTools::createBoxShape( const QString& label,
|
|
|
|
double startX,
|
|
|
|
double endX,
|
|
|
|
double startY,
|
|
|
|
double endY,
|
|
|
|
QColor color,
|
|
|
|
Qt::BrushStyle brushStyle )
|
2020-04-20 02:35:59 -05:00
|
|
|
{
|
2020-05-04 12:47:32 -05:00
|
|
|
return createBoxShapeT<QwtPlotShapeItem>( label, startX, endX, startY, endY, color, brushStyle );
|
2020-04-20 02:35:59 -05:00
|
|
|
}
|
2022-01-17 06:14:21 -06:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QwtPlot::Axis RiuQwtPlotTools::toQwtPlotAxis( RiaDefines::PlotAxis axis )
|
|
|
|
{
|
|
|
|
if ( axis == RiaDefines::PlotAxis::PLOT_AXIS_LEFT )
|
|
|
|
return QwtPlot::yLeft;
|
|
|
|
else if ( axis == RiaDefines::PlotAxis::PLOT_AXIS_RIGHT )
|
|
|
|
return QwtPlot::yRight;
|
|
|
|
else if ( axis == RiaDefines::PlotAxis::PLOT_AXIS_BOTTOM )
|
|
|
|
return QwtPlot::xBottom;
|
|
|
|
|
|
|
|
return QwtPlot::xTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RiaDefines::PlotAxis RiuQwtPlotTools::fromQwtPlotAxis( QwtPlot::Axis axis )
|
|
|
|
{
|
|
|
|
if ( axis == QwtPlot::yLeft )
|
|
|
|
return RiaDefines::PlotAxis::PLOT_AXIS_LEFT;
|
|
|
|
else if ( axis == QwtPlot::yRight )
|
|
|
|
return RiaDefines::PlotAxis::PLOT_AXIS_RIGHT;
|
|
|
|
else if ( axis == QwtPlot::xBottom )
|
|
|
|
return RiaDefines::PlotAxis::PLOT_AXIS_BOTTOM;
|
|
|
|
|
|
|
|
return RiaDefines::PlotAxis::PLOT_AXIS_TOP;
|
|
|
|
}
|