File indexing completed on 2024-11-24 03:57:52

0001 /**
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "timechartmodel.h"
0010 
0011 TimeChartModel::TimeChartModel( QObject* parent )
0012     : QSortFilterProxyModel( parent )
0013 {
0014 }
0015 
0016 QPair< QDateTime, QDateTime > TimeChartModel::visibleRange() const
0017 {
0018     return range;
0019 }
0020 
0021 void TimeChartModel::setVisibleRange( const QDateTime& start, const QDateTime& end )
0022 {
0023     const QPair< QDateTime, QDateTime > r = qMakePair( start, end );
0024     if ( r == range )
0025         return;
0026 
0027     range = r;
0028     invalidateFilter();
0029 }
0030 
0031 void TimeChartModel::setVisibleStart( const QDateTime& start )
0032 {
0033     setVisibleRange( start, range.second );
0034 }
0035 
0036 void TimeChartModel::setVisibleEnd( const QDateTime& end )
0037 {
0038     setVisibleRange( range.first, end );
0039 }
0040 
0041 /*!
0042  \reimp
0043  */
0044 QVariant TimeChartModel::data( const QModelIndex& index, int role ) const
0045 {
0046     const QVariant v = QSortFilterProxyModel::data( index, role );
0047     if ( index.column() % 2 != 0 || role != Qt::DisplayRole )
0048         return v;
0049     else
0050         return QDateTime::fromSecsSinceEpoch( 0 ).secsTo( v.toDateTime() ) / 3600.0;
0051 }
0052 
0053 /*!
0054  \reimp
0055  */
0056 bool TimeChartModel::filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const
0057 {
0058     const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
0059     const QDateTime date = index.data().toDateTime();
0060     return ( date >= range.first || range.first.isNull() ) &&
0061            ( date <= range.second || range.second.isNull() );
0062 }