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
55{
56 delete m_data;
57}
58
65void 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
90{
91 m_data->utcOffset = seconds;
92}
93
102{
103 return m_data->utcOffset;
104}
105
116{
117 m_data->week0Type = week0Type;
118}
119
125{
126 return m_data->week0Type;
127}
128
138 QwtDate::IntervalType intervalType, const QString& format )
139{
142 {
143 m_data->dateFormats[ intervalType ] = format;
144 }
145}
146
153 QwtDate::IntervalType intervalType ) const
154{
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
187 {
188 return m_data->dateFormats[ intervalType ];
189 }
190
191 return m_data->dateFormats[ QwtDate::Second ];
192}
193
205QwtText 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
224 const QwtScaleDiv& scaleDiv ) const
225{
226 int intvType = QwtDate::Year;
227
228 bool alignedToWeeks = true;
229
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}
const QwtScaleDiv & scaleDiv() const
A collection of methods around date/time values.
Definition qwt_date.h:43
static QDateTime floor(const QDateTime &, IntervalType)
Definition qwt_date.cpp:425
static QString toString(const QDateTime &, const QString &format, Week0Type)
Definition qwt_date.cpp:686
static QDateTime toDateTime(double value, Qt::TimeSpec=Qt::UTC)
Definition qwt_date.cpp:261
IntervalType
Definition qwt_date.h:76
@ Month
The interval is related to months.
Definition qwt_date.h:96
@ Day
The interval is related to days.
Definition qwt_date.h:90
@ Millisecond
The interval is related to milliseconds.
Definition qwt_date.h:78
@ Minute
The interval is related to minutes.
Definition qwt_date.h:84
@ Hour
The interval is related to hours.
Definition qwt_date.h:87
@ Second
The interval is related to seconds.
Definition qwt_date.h:81
@ Week
The interval is related to weeks.
Definition qwt_date.h:93
@ Year
The interval is related to years.
Definition qwt_date.h:99
QDateTime toDateTime(double) const
virtual QwtDate::IntervalType intervalType(const QwtScaleDiv &) const
void setTimeSpec(Qt::TimeSpec)
QString dateFormat(QwtDate::IntervalType) const
void setUtcOffset(int seconds)
virtual QString dateFormatOfDate(const QDateTime &, QwtDate::IntervalType) const
void setWeek0Type(QwtDate::Week0Type)
virtual QwtText label(double) const override
Convert a value into its representing label.
QwtDateScaleDraw(Qt::TimeSpec=Qt::LocalTime)
Constructor.
void setDateFormat(QwtDate::IntervalType, const QString &)
virtual ~QwtDateScaleDraw()
Destructor.
QwtDate::Week0Type week0Type() const
Qt::TimeSpec timeSpec() const
A class representing a scale division.
QList< double > ticks(int tickType) const
@ MajorTick
Major ticks.
A class representing a text.
Definition qwt_text.h:52