Loading...
Searching...
No Matches
qwt_date_scale_draw.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_date_scale_draw.h"
#include "qwt_text.h"
class QwtDateScaleDraw::PrivateData
{
public:
explicit PrivateData( Qt::TimeSpec spec )
: timeSpec( spec )
, utcOffset( 0 )
, week0Type( QwtDate::FirstThursday )
{
dateFormats[ QwtDate::Millisecond ] = "hh:mm:ss:zzz\nddd dd MMM yyyy";
dateFormats[ QwtDate::Second ] = "hh:mm:ss\nddd dd MMM yyyy";
dateFormats[ QwtDate::Minute ] = "hh:mm\nddd dd MMM yyyy";
dateFormats[ QwtDate::Hour ] = "hh:mm\nddd dd MMM yyyy";
dateFormats[ QwtDate::Day ] = "ddd dd MMM yyyy";
dateFormats[ QwtDate::Week ] = "Www yyyy";
dateFormats[ QwtDate::Month ] = "MMM yyyy";
dateFormats[ QwtDate::Year ] = "yyyy";
}
Qt::TimeSpec timeSpec;
int utcOffset;
QwtDate::Week0Type week0Type;
QString dateFormats[ QwtDate::Year + 1 ];
};
QwtDateScaleDraw::QwtDateScaleDraw( Qt::TimeSpec timeSpec )
{
m_data = new PrivateData( timeSpec );
}
QwtDateScaleDraw::~QwtDateScaleDraw()
{
delete m_data;
}
void QwtDateScaleDraw::setTimeSpec( Qt::TimeSpec timeSpec )
{
m_data->timeSpec = timeSpec;
}
Qt::TimeSpec QwtDateScaleDraw::timeSpec() const
{
return m_data->timeSpec;
}
void QwtDateScaleDraw::setUtcOffset( int seconds )
{
m_data->utcOffset = seconds;
}
int QwtDateScaleDraw::utcOffset() const
{
return m_data->utcOffset;
}
void QwtDateScaleDraw::setWeek0Type( QwtDate::Week0Type week0Type )
{
m_data->week0Type = week0Type;
}
QwtDate::Week0Type QwtDateScaleDraw::week0Type() const
{
return m_data->week0Type;
}
void QwtDateScaleDraw::setDateFormat(
QwtDate::IntervalType intervalType, const QString& format )
{
if ( intervalType >= QwtDate::Millisecond &&
intervalType <= QwtDate::Year )
{
m_data->dateFormats[ intervalType ] = format;
}
}
QString QwtDateScaleDraw::dateFormat(
QwtDate::IntervalType intervalType ) const
{
if ( intervalType >= QwtDate::Millisecond &&
intervalType <= QwtDate::Year )
{
return m_data->dateFormats[ intervalType ];
}
return QString();
}
QString QwtDateScaleDraw::dateFormatOfDate( const QDateTime& dateTime,
QwtDate::IntervalType intervalType ) const
{
Q_UNUSED( dateTime )
if ( intervalType >= QwtDate::Millisecond &&
intervalType <= QwtDate::Year )
{
return m_data->dateFormats[ intervalType ];
}
return m_data->dateFormats[ QwtDate::Second ];
}
QwtText QwtDateScaleDraw::label( double value ) const
{
const QDateTime dt = toDateTime( value );
const QString fmt = dateFormatOfDate(
dt, intervalType( scaleDiv() ) );
return QwtDate::toString( dt, fmt, m_data->week0Type );
}
QwtDate::IntervalType QwtDateScaleDraw::intervalType(
const QwtScaleDiv& scaleDiv ) const
{
int intvType = QwtDate::Year;
bool alignedToWeeks = true;
const QList< double > ticks = scaleDiv.ticks( QwtScaleDiv::MajorTick );
for ( int i = 0; i < ticks.size(); i++ )
{
const QDateTime dt = toDateTime( ticks[i] );
for ( int j = QwtDate::Second; j <= intvType; j++ )
{
const QDateTime dt0 = QwtDate::floor( dt,
static_cast< QwtDate::IntervalType >( j ) );
if ( dt0 != dt )
{
if ( j == QwtDate::Week )
{
alignedToWeeks = false;
}
else
{
intvType = j - 1;
break;
}
}
}
if ( intvType == QwtDate::Millisecond )
break;
}
if ( intvType == QwtDate::Week && !alignedToWeeks )
intvType = QwtDate::Day;
return static_cast< QwtDate::IntervalType >( intvType );
}
QDateTime QwtDateScaleDraw::toDateTime( double value ) const
{
QDateTime dt = QwtDate::toDateTime( value, m_data->timeSpec );
if ( m_data->timeSpec == Qt::OffsetFromUTC )
{
dt = dt.addSecs( m_data->utcOffset );
#if QT_VERSION >= 0x050200
dt.setOffsetFromUtc( m_data->utcOffset );
#else
dt.setUtcOffset( m_data->utcOffset );
#endif
}
return dt;
}
QList< double >
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition qwt_abstract_scale_draw.cpp:133
QwtDate
A collection of methods around date/time values.
Definition qwt_date.h:43
QwtDate::floor
static QDateTime floor(const QDateTime &, IntervalType)
Definition qwt_date.cpp:425
QwtDate::toString
static QString toString(const QDateTime &, const QString &format, Week0Type)
Definition qwt_date.cpp:686
QwtDate::toDateTime
static QDateTime toDateTime(double value, Qt::TimeSpec=Qt::UTC)
Definition qwt_date.cpp:261
QwtDate::Week0Type
Week0Type
Definition qwt_date.h:50
QwtDate::IntervalType
IntervalType
Definition qwt_date.h:76
QwtDate::Month
@ Month
The interval is related to months.
Definition qwt_date.h:96
QwtDate::Day
@ Day
The interval is related to days.
Definition qwt_date.h:90
QwtDate::Millisecond
@ Millisecond
The interval is related to milliseconds.
Definition qwt_date.h:78
QwtDate::Minute
@ Minute
The interval is related to minutes.
Definition qwt_date.h:84
QwtDate::Hour
@ Hour
The interval is related to hours.
Definition qwt_date.h:87
QwtDate::Second
@ Second
The interval is related to seconds.
Definition qwt_date.h:81
QwtDate::Week
@ Week
The interval is related to weeks.
Definition qwt_date.h:93
QwtDate::Year
@ Year
The interval is related to years.
Definition qwt_date.h:99
QwtDateScaleDraw::toDateTime
QDateTime toDateTime(double) const
Definition qwt_date_scale_draw.cpp:269
QwtDateScaleDraw::intervalType
virtual QwtDate::IntervalType intervalType(const QwtScaleDiv &) const
Definition qwt_date_scale_draw.cpp:223
QwtDateScaleDraw::setTimeSpec
void setTimeSpec(Qt::TimeSpec)
Definition qwt_date_scale_draw.cpp:65
QwtDateScaleDraw::utcOffset
int utcOffset() const
Definition qwt_date_scale_draw.cpp:101
QwtDateScaleDraw::dateFormat
QString dateFormat(QwtDate::IntervalType) const
Definition qwt_date_scale_draw.cpp:152
QwtDateScaleDraw::setUtcOffset
void setUtcOffset(int seconds)
Definition qwt_date_scale_draw.cpp:89
QwtDateScaleDraw::dateFormatOfDate
virtual QString dateFormatOfDate(const QDateTime &, QwtDate::IntervalType) const
Definition qwt_date_scale_draw.cpp:180
QwtDateScaleDraw::setWeek0Type
void setWeek0Type(QwtDate::Week0Type)
Definition qwt_date_scale_draw.cpp:115
QwtDateScaleDraw::label
virtual QwtText label(double) const override
Convert a value into its representing label.
Definition qwt_date_scale_draw.cpp:205
QwtDateScaleDraw::QwtDateScaleDraw
QwtDateScaleDraw(Qt::TimeSpec=Qt::LocalTime)
Constructor.
Definition qwt_date_scale_draw.cpp:48
QwtDateScaleDraw::setDateFormat
void setDateFormat(QwtDate::IntervalType, const QString &)
Definition qwt_date_scale_draw.cpp:137
QwtDateScaleDraw::~QwtDateScaleDraw
virtual ~QwtDateScaleDraw()
Destructor.
Definition qwt_date_scale_draw.cpp:54
QwtDateScaleDraw::week0Type
QwtDate::Week0Type week0Type() const
Definition qwt_date_scale_draw.cpp:124
QwtDateScaleDraw::timeSpec
Qt::TimeSpec timeSpec() const
Definition qwt_date_scale_draw.cpp:74
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition qwt_scale_div.cpp:309
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition qwt_scale_div.h:49
QwtText
A class representing a text.
Definition qwt_text.h:52