00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- ***************************** 00002 * Qwt Widget Library 00003 * Copyright (C) 1997 Josef Wilgen 00004 * Copyright (C) 2002 Uwe Rathmann 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the Qwt License, Version 1.0 00008 *****************************************************************************/ 00009 00010 #include "qwt_date_scale_draw.h" 00011 00012 class QwtDateScaleDraw::PrivateData 00013 { 00014 public: 00015 explicit PrivateData( Qt::TimeSpec spec ): 00016 timeSpec( spec ), 00017 utcOffset( 0 ), 00018 week0Type( QwtDate::FirstThursday ) 00019 { 00020 dateFormats[ QwtDate::Millisecond ] = "hh:mm:ss:zzz\nddd dd MMM yyyy"; 00021 dateFormats[ QwtDate::Second ] = "hh:mm:ss\nddd dd MMM yyyy"; 00022 dateFormats[ QwtDate::Minute ] = "hh:mm\nddd dd MMM yyyy"; 00023 dateFormats[ QwtDate::Hour ] = "hh:mm\nddd dd MMM yyyy"; 00024 dateFormats[ QwtDate::Day ] = "ddd dd MMM yyyy"; 00025 dateFormats[ QwtDate::Week ] = "Www yyyy"; 00026 dateFormats[ QwtDate::Month ] = "MMM yyyy"; 00027 dateFormats[ QwtDate::Year ] = "yyyy"; 00028 } 00029 00030 Qt::TimeSpec timeSpec; 00031 int utcOffset; 00032 QwtDate::Week0Type week0Type; 00033 QString dateFormats[ QwtDate::Year + 1 ]; 00034 }; 00035 00047 QwtDateScaleDraw::QwtDateScaleDraw( Qt::TimeSpec timeSpec ) 00048 { 00049 d_data = new PrivateData( timeSpec ); 00050 } 00051 00053 QwtDateScaleDraw::~QwtDateScaleDraw() 00054 { 00055 delete d_data; 00056 } 00057 00064 void QwtDateScaleDraw::setTimeSpec( Qt::TimeSpec timeSpec ) 00065 { 00066 d_data->timeSpec = timeSpec; 00067 } 00068 00073 Qt::TimeSpec QwtDateScaleDraw::timeSpec() const 00074 { 00075 return d_data->timeSpec; 00076 } 00077 00088 void QwtDateScaleDraw::setUtcOffset( int seconds ) 00089 { 00090 d_data->utcOffset = seconds; 00091 } 00092 00100 int QwtDateScaleDraw::utcOffset() const 00101 { 00102 return d_data->utcOffset; 00103 } 00104 00114 void QwtDateScaleDraw::setWeek0Type( QwtDate::Week0Type week0Type ) 00115 { 00116 d_data->week0Type = week0Type; 00117 } 00118 00123 QwtDate::Week0Type QwtDateScaleDraw::week0Type() const 00124 { 00125 return d_data->week0Type; 00126 } 00127 00136 void QwtDateScaleDraw::setDateFormat( 00137 QwtDate::IntervalType intervalType, const QString &format ) 00138 { 00139 if ( intervalType >= QwtDate::Millisecond && 00140 intervalType <= QwtDate::Year ) 00141 { 00142 d_data->dateFormats[ intervalType ] = format; 00143 } 00144 } 00145 00151 QString QwtDateScaleDraw::dateFormat( 00152 QwtDate::IntervalType intervalType ) const 00153 { 00154 if ( intervalType >= QwtDate::Millisecond && 00155 intervalType <= QwtDate::Year ) 00156 { 00157 return d_data->dateFormats[ intervalType ]; 00158 } 00159 00160 return QString::null; 00161 } 00162 00179 QString QwtDateScaleDraw::dateFormatOfDate( const QDateTime &dateTime, 00180 QwtDate::IntervalType intervalType ) const 00181 { 00182 Q_UNUSED( dateTime ) 00183 00184 if ( intervalType >= QwtDate::Millisecond && 00185 intervalType <= QwtDate::Year ) 00186 { 00187 return d_data->dateFormats[ intervalType ]; 00188 } 00189 00190 return d_data->dateFormats[ QwtDate::Second ]; 00191 } 00192 00204 QwtText QwtDateScaleDraw::label( double value ) const 00205 { 00206 const QDateTime dt = toDateTime( value ); 00207 const QString fmt = dateFormatOfDate( 00208 dt, intervalType( scaleDiv() ) ); 00209 00210 return QwtDate::toString( dt, fmt, d_data->week0Type ); 00211 } 00212 00222 QwtDate::IntervalType QwtDateScaleDraw::intervalType( 00223 const QwtScaleDiv &scaleDiv ) const 00224 { 00225 int intvType = QwtDate::Year; 00226 00227 bool alignedToWeeks = true; 00228 00229 const QList<double> ticks = scaleDiv.ticks( QwtScaleDiv::MajorTick ); 00230 for ( int i = 0; i < ticks.size(); i++ ) 00231 { 00232 const QDateTime dt = toDateTime( ticks[i] ); 00233 for ( int j = QwtDate::Second; j <= intvType; j++ ) 00234 { 00235 const QDateTime dt0 = QwtDate::floor( dt, 00236 static_cast<QwtDate::IntervalType>( j ) ); 00237 00238 if ( dt0 != dt ) 00239 { 00240 if ( j == QwtDate::Week ) 00241 { 00242 alignedToWeeks = false; 00243 } 00244 else 00245 { 00246 intvType = j - 1; 00247 break; 00248 } 00249 } 00250 } 00251 00252 if ( intvType == QwtDate::Millisecond ) 00253 break; 00254 } 00255 00256 if ( intvType == QwtDate::Week && !alignedToWeeks ) 00257 intvType = QwtDate::Day; 00258 00259 return static_cast<QwtDate::IntervalType>( intvType ); 00260 } 00261 00268 QDateTime QwtDateScaleDraw::toDateTime( double value ) const 00269 { 00270 QDateTime dt = QwtDate::toDateTime( value, d_data->timeSpec ); 00271 if ( d_data->timeSpec == Qt::OffsetFromUTC ) 00272 { 00273 dt = dt.addSecs( d_data->utcOffset ); 00274 dt.setUtcOffset( d_data->utcOffset ); 00275 } 00276 00277 return dt; 00278 }