[8127] | 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
| 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 |
|
---|
| 12 | class QwtDateScaleDraw::PrivateData
|
---|
| 13 | {
|
---|
| 14 | public:
|
---|
| 15 | PrivateData( Qt::TimeSpec spec ):
|
---|
| 16 | timeSpec( spec ),
|
---|
| 17 | utcOffset( 0 ),
|
---|
| 18 | week0Type( QwtDate::FirstThursday )
|
---|
| 19 | {
|
---|
| 20 | dateFormats[ QwtDate::Millisecond ] = "hh:mm:ss:zzz\nddd dd MMM yyyy";
|
---|
| 21 | dateFormats[ QwtDate::Second ] = "hh:mm:ss\nddd dd MMM yyyy";
|
---|
| 22 | dateFormats[ QwtDate::Minute ] = "hh:mm\nddd dd MMM yyyy";
|
---|
| 23 | dateFormats[ QwtDate::Hour ] = "hh:mm\nddd dd MMM yyyy";
|
---|
| 24 | dateFormats[ QwtDate::Day ] = "ddd dd MMM yyyy";
|
---|
| 25 | dateFormats[ QwtDate::Week ] = "Www yyyy";
|
---|
| 26 | dateFormats[ QwtDate::Month ] = "MMM yyyy";
|
---|
| 27 | dateFormats[ QwtDate::Year ] = "yyyy";
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | Qt::TimeSpec timeSpec;
|
---|
| 31 | int utcOffset;
|
---|
| 32 | QwtDate::Week0Type week0Type;
|
---|
| 33 | QString dateFormats[ QwtDate::Year + 1 ];
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | /*!
|
---|
| 37 | \brief Constructor
|
---|
| 38 |
|
---|
| 39 | The default setting is to display tick labels for the
|
---|
| 40 | given time specification. The first week of a year is defined like
|
---|
| 41 | for QwtDate::FirstThursday.
|
---|
| 42 |
|
---|
| 43 | \param timeSpec Time specification
|
---|
| 44 |
|
---|
| 45 | \sa setTimeSpec(), setWeek0Type()
|
---|
| 46 | */
|
---|
| 47 | QwtDateScaleDraw::QwtDateScaleDraw( Qt::TimeSpec timeSpec )
|
---|
| 48 | {
|
---|
| 49 | d_data = new PrivateData( timeSpec );
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | //! Destructor
|
---|
| 53 | QwtDateScaleDraw::~QwtDateScaleDraw()
|
---|
| 54 | {
|
---|
| 55 | delete d_data;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /*!
|
---|
| 59 | Set the time specification used for the tick labels
|
---|
| 60 |
|
---|
| 61 | \param timeSpec Time specification
|
---|
| 62 | \sa timeSpec(), setUtcOffset(), toDateTime()
|
---|
| 63 | */
|
---|
| 64 | void QwtDateScaleDraw::setTimeSpec( Qt::TimeSpec timeSpec )
|
---|
| 65 | {
|
---|
| 66 | d_data->timeSpec = timeSpec;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /*!
|
---|
| 70 | \return Time specification used for the tick labels
|
---|
| 71 | \sa setTimeSpec(), utcOffset(), toDateTime()
|
---|
| 72 | */
|
---|
| 73 | Qt::TimeSpec QwtDateScaleDraw::timeSpec() const
|
---|
| 74 | {
|
---|
| 75 | return d_data->timeSpec;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /*!
|
---|
| 79 | Set the offset in seconds from Coordinated Universal Time
|
---|
| 80 |
|
---|
| 81 | \param seconds Offset in seconds
|
---|
| 82 |
|
---|
| 83 | \note The offset has no effect beside for the time specification
|
---|
| 84 | Qt::OffsetFromUTC.
|
---|
| 85 |
|
---|
| 86 | \sa QDate::utcOffset(), setTimeSpec(), toDateTime()
|
---|
| 87 | */
|
---|
| 88 | void QwtDateScaleDraw::setUtcOffset( int seconds )
|
---|
| 89 | {
|
---|
| 90 | d_data->utcOffset = seconds;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /*!
|
---|
| 94 | \return Offset in seconds from Coordinated Universal Time
|
---|
| 95 | \note The offset has no effect beside for the time specification
|
---|
| 96 | Qt::OffsetFromUTC.
|
---|
| 97 |
|
---|
| 98 | \sa QDate::setUtcOffset(), setTimeSpec(), toDateTime()
|
---|
| 99 | */
|
---|
| 100 | int QwtDateScaleDraw::utcOffset() const
|
---|
| 101 | {
|
---|
| 102 | return d_data->utcOffset;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /*!
|
---|
| 106 | Sets how to identify the first week of a year.
|
---|
| 107 |
|
---|
| 108 | \param week0Type Mode how to identify the first week of a year
|
---|
| 109 |
|
---|
| 110 | \sa week0Type().
|
---|
| 111 | \note week0Type has no effect beside for intervals classified as
|
---|
| 112 | QwtDate::Week.
|
---|
| 113 | */
|
---|
| 114 | void QwtDateScaleDraw::setWeek0Type( QwtDate::Week0Type week0Type )
|
---|
| 115 | {
|
---|
| 116 | d_data->week0Type = week0Type;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /*!
|
---|
| 120 | \return Setting how to identify the first week of a year.
|
---|
| 121 | \sa setWeek0Type()
|
---|
| 122 | */
|
---|
| 123 | QwtDate::Week0Type QwtDateScaleDraw::week0Type() const
|
---|
| 124 | {
|
---|
| 125 | return d_data->week0Type;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /*!
|
---|
| 129 | Set the default format string for an datetime interval type
|
---|
| 130 |
|
---|
| 131 | \param intervalType Interval type
|
---|
| 132 | \param format Default format string
|
---|
| 133 |
|
---|
| 134 | \sa dateFormat(), dateFormatOfDate(), QwtDate::toString()
|
---|
| 135 | */
|
---|
| 136 | void QwtDateScaleDraw::setDateFormat(
|
---|
| 137 | QwtDate::IntervalType intervalType, const QString &format )
|
---|
| 138 | {
|
---|
| 139 | if ( intervalType >= QwtDate::Millisecond &&
|
---|
| 140 | intervalType <= QwtDate::Year )
|
---|
| 141 | {
|
---|
| 142 | d_data->dateFormats[ intervalType ] = format;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /*!
|
---|
| 147 | \param intervalType Interval type
|
---|
| 148 | \return Default format string for an datetime interval type
|
---|
| 149 | \sa setDateFormat(), dateFormatOfDate()
|
---|
| 150 | */
|
---|
| 151 | QString QwtDateScaleDraw::dateFormat(
|
---|
| 152 | QwtDate::IntervalType intervalType ) const
|
---|
| 153 | {
|
---|
| 154 | if ( intervalType >= QwtDate::Millisecond &&
|
---|
| 155 | intervalType <= QwtDate::Year )
|
---|
| 156 | {
|
---|
| 157 | return d_data->dateFormats[ intervalType ];
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return QString::null;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /*!
|
---|
| 164 | Format string for the representation of a datetime
|
---|
| 165 |
|
---|
| 166 | dateFormatOfDate() is intended to be overloaded for
|
---|
| 167 | situations, where formats are individual for specific
|
---|
| 168 | datetime values.
|
---|
| 169 |
|
---|
| 170 | The default setting ignores dateTime and return
|
---|
| 171 | the default format for the interval type.
|
---|
| 172 |
|
---|
| 173 | \param dateTime Datetime value
|
---|
| 174 | \param intervalType Interval type
|
---|
| 175 | \return Format string
|
---|
| 176 |
|
---|
| 177 | \sa setDateFormat(), QwtDate::toString()
|
---|
| 178 | */
|
---|
| 179 | QString QwtDateScaleDraw::dateFormatOfDate( const QDateTime &dateTime,
|
---|
| 180 | QwtDate::IntervalType intervalType ) const
|
---|
| 181 | {
|
---|
| 182 | Q_UNUSED( dateTime )
|
---|
| 183 |
|
---|
| 184 | if ( intervalType >= QwtDate::Millisecond &&
|
---|
| 185 | intervalType <= QwtDate::Year )
|
---|
| 186 | {
|
---|
| 187 | return d_data->dateFormats[ intervalType ];
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | return d_data->dateFormats[ QwtDate::Second ];
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /*!
|
---|
| 194 | \brief Convert a value into its representing label
|
---|
| 195 |
|
---|
| 196 | The value is converted to a datetime value using toDateTime()
|
---|
| 197 | and converted to a plain text using QwtDate::toString().
|
---|
| 198 |
|
---|
| 199 | \param value Value
|
---|
| 200 | \return Label string.
|
---|
| 201 |
|
---|
| 202 | \sa dateFormatOfDate()
|
---|
| 203 | */
|
---|
| 204 | QwtText QwtDateScaleDraw::label( double value ) const
|
---|
| 205 | {
|
---|
| 206 | const QDateTime dt = toDateTime( value );
|
---|
| 207 | const QString fmt = dateFormatOfDate(
|
---|
| 208 | dt, intervalType( scaleDiv() ) );
|
---|
| 209 |
|
---|
| 210 | return QwtDate::toString( dt, fmt, d_data->week0Type );
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /*!
|
---|
| 214 | Find the less detailed datetime unit, where no rounding
|
---|
| 215 | errors happen.
|
---|
| 216 |
|
---|
| 217 | \param scaleDiv Scale division
|
---|
| 218 | \return Interval type
|
---|
| 219 |
|
---|
| 220 | \sa dateFormatOfDate()
|
---|
| 221 | */
|
---|
| 222 | QwtDate::IntervalType QwtDateScaleDraw::intervalType(
|
---|
| 223 | const QwtScaleDiv &scaleDiv ) const
|
---|
| 224 | {
|
---|
| 225 | int intvType = QwtDate::Year;
|
---|
| 226 |
|
---|
| 227 | bool alignedToWeeks = true;
|
---|
| 228 |
|
---|
| 229 | const QList<double> ticks = scaleDiv.ticks( QwtScaleDiv::MajorTick );
|
---|
| 230 | for ( int i = 0; i < ticks.size(); i++ )
|
---|
| 231 | {
|
---|
| 232 | const QDateTime dt = toDateTime( ticks[i] );
|
---|
| 233 | for ( int j = QwtDate::Second; j <= intvType; j++ )
|
---|
| 234 | {
|
---|
| 235 | const QDateTime dt0 = QwtDate::floor( dt,
|
---|
| 236 | static_cast<QwtDate::IntervalType>( j ) );
|
---|
| 237 |
|
---|
| 238 | if ( dt0 != dt )
|
---|
| 239 | {
|
---|
| 240 | if ( j == QwtDate::Week )
|
---|
| 241 | {
|
---|
| 242 | alignedToWeeks = false;
|
---|
| 243 | }
|
---|
| 244 | else
|
---|
| 245 | {
|
---|
| 246 | intvType = j - 1;
|
---|
| 247 | break;
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | if ( intvType == QwtDate::Millisecond )
|
---|
| 253 | break;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | if ( intvType == QwtDate::Week && !alignedToWeeks )
|
---|
| 257 | intvType = QwtDate::Day;
|
---|
| 258 |
|
---|
| 259 | return static_cast<QwtDate::IntervalType>( intvType );
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /*!
|
---|
| 263 | Translate a double value into a QDateTime object.
|
---|
| 264 |
|
---|
| 265 | \return QDateTime object initialized with timeSpec() and utcOffset().
|
---|
| 266 | \sa timeSpec(), utcOffset(), QwtDate::toDateTime()
|
---|
| 267 | */
|
---|
| 268 | QDateTime QwtDateScaleDraw::toDateTime( double value ) const
|
---|
| 269 | {
|
---|
| 270 | QDateTime dt = QwtDate::toDateTime( value, d_data->timeSpec );
|
---|
| 271 | if ( d_data->timeSpec == Qt::OffsetFromUTC )
|
---|
| 272 | {
|
---|
| 273 | dt = dt.addSecs( d_data->utcOffset );
|
---|
| 274 | dt.setUtcOffset( d_data->utcOffset );
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | return dt;
|
---|
| 278 | }
|
---|