Qwt User's Guide: qwt_date_scale_draw.cpp Source File

Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_date_scale_draw.cpp
1/******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_date_scale_draw.h"
11#include "qwt_text.h"
12
13class QwtDateScaleDraw::PrivateData
14{
15 public:
16 explicit PrivateData( Qt::TimeSpec spec )
17 : timeSpec( spec )
18 , utcOffset( 0 )
19 , week0Type( QwtDate::FirstThursday )
20 {
21 dateFormats[ QwtDate::Millisecond ] = "hh:mm:ss:zzz\nddd dd MMM yyyy";
22 dateFormats[ QwtDate::Second ] = "hh:mm:ss\nddd dd MMM yyyy";
23 dateFormats[ QwtDate::Minute ] = "hh:mm\nddd dd MMM yyyy";
24 dateFormats[ QwtDate::Hour ] = "hh:mm\nddd dd MMM yyyy";
25 dateFormats[ QwtDate::Day ] = "ddd dd MMM yyyy";
26 dateFormats[ QwtDate::Week ] = "Www yyyy";
27 dateFormats[ QwtDate::Month ] = "MMM yyyy";
28 dateFormats[ QwtDate::Year ] = "yyyy";
29 }
30
31 Qt::TimeSpec timeSpec;
32 int utcOffset;
33 QwtDate::Week0Type week0Type;
34 QString dateFormats[ QwtDate::Year + 1 ];
35};
36
48QwtDateScaleDraw::QwtDateScaleDraw( Qt::TimeSpec timeSpec )
49{
50 m_data = new PrivateData( timeSpec );
51}
52
54QwtDateScaleDraw::~QwtDateScaleDraw()
55{
56 delete m_data;
57}
58
65 void QwtDateScaleDraw::setTimeSpec( Qt::TimeSpec timeSpec )
66{
67 m_data->timeSpec = timeSpec;
68}
69
74Qt::TimeSpec QwtDateScaleDraw::timeSpec() const
75{
76 return m_data->timeSpec;
77}
78
89 void QwtDateScaleDraw::setUtcOffset( int seconds )
90{
91 m_data->utcOffset = seconds;
92}
93
101 int QwtDateScaleDraw::utcOffset() const
102{
103 return m_data->utcOffset;
104}
105
115 void QwtDateScaleDraw::setWeek0Type( QwtDate::Week0Type week0Type )
116{
117 m_data->week0Type = week0Type;
118}
119
124 QwtDate::Week0Type QwtDateScaleDraw::week0Type() const
125{
126 return m_data->week0Type;
127}
128
137 void QwtDateScaleDraw::setDateFormat(
138 QwtDate::IntervalType intervalType, const QString& format )
139{
140 if ( intervalType >= QwtDate::Millisecond &&
141 intervalType <= QwtDate::Year )
142 {
143 m_data->dateFormats[ intervalType ] = format;
144 }
145}
146
152QString QwtDateScaleDraw::dateFormat(
153 QwtDate::IntervalType intervalType ) const
154{
155 if ( intervalType >= QwtDate::Millisecond &&
156 intervalType <= QwtDate::Year )
157 {
158 return m_data->dateFormats[ intervalType ];
159 }
160
161 return QString();
162}
163
180QString QwtDateScaleDraw::dateFormatOfDate( const QDateTime& dateTime,
181 QwtDate::IntervalType intervalType ) const
182{
183 Q_UNUSED( dateTime )
184
185 if ( intervalType >= QwtDate::Millisecond &&
186 intervalType <= QwtDate::Year )
187 {
188 return m_data->dateFormats[ intervalType ];
189 }
190
191 return m_data->dateFormats[ QwtDate::Second ];
192}
193
205 QwtText QwtDateScaleDraw::label( double value ) const
206{
207 const QDateTime dt = toDateTime( value );
208 const QString fmt = dateFormatOfDate(
209 dt, intervalType( scaleDiv() ) );
210
211 return QwtDate::toString( dt, fmt, m_data->week0Type );
212}
213
223 QwtDate::IntervalType QwtDateScaleDraw::intervalType(
224 const QwtScaleDiv& scaleDiv ) const
225{
226 int intvType = QwtDate::Year;
227
228 bool alignedToWeeks = true;
229
230 const QList< double > ticks = scaleDiv.ticks( QwtScaleDiv::MajorTick );
231 for ( int i = 0; i < ticks.size(); i++ )
232 {
233 const QDateTime dt = toDateTime( ticks[i] );
234 for ( int j = QwtDate::Second; j <= intvType; j++ )
235 {
236 const QDateTime dt0 = QwtDate::floor( dt,
237 static_cast< QwtDate::IntervalType >( j ) );
238
239 if ( dt0 != dt )
240 {
241 if ( j == QwtDate::Week )
242 {
243 alignedToWeeks = false;
244 }
245 else
246 {
247 intvType = j - 1;
248 break;
249 }
250 }
251 }
252
253 if ( intvType == QwtDate::Millisecond )
254 break;
255 }
256
257 if ( intvType == QwtDate::Week && !alignedToWeeks )
258 intvType = QwtDate::Day;
259
260 return static_cast< QwtDate::IntervalType >( intvType );
261}
262
269QDateTime QwtDateScaleDraw::toDateTime( double value ) const
270{
271 QDateTime dt = QwtDate::toDateTime( value, m_data->timeSpec );
272 if ( m_data->timeSpec == Qt::OffsetFromUTC )
273 {
274 dt = dt.addSecs( m_data->utcOffset );
275#if QT_VERSION >= 0x050200
276 dt.setOffsetFromUtc( m_data->utcOffset );
277#else
278 dt.setUtcOffset( m_data->utcOffset );
279#endif
280 }
281
282 return dt;
283}
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