File: main.cpp

package info (click to toggle)
kdiagram2 2.8.0-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,796 kB
  • sloc: cpp: 52,379; makefile: 8; sh: 2
file content (148 lines) | stat: -rw-r--r-- 5,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
 *
 * This file is part of the KD Chart library.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QStandardItemModel>
#include <QTreeView>
#include <QTimer>
#include <QWidget>
#include <QSplitter>
#include <QPushButton>

#include <math.h>

#include <KChartChart>
#include <KChartPlotter>

#include "timeaxis.h"
#include "timechartmodel.h"

class ChartWidget : public QWidget {
    Q_OBJECT
public:
    explicit ChartWidget(QWidget* parent = nullptr) : QWidget(parent), m_counter(0) {
        QSplitter* splitter = new QSplitter(this);
        QHBoxLayout* l = new QHBoxLayout(this);
        setLayout(l);
        l->addWidget( splitter );

        QWidget* leftWidget = new QWidget( splitter );
        QVBoxLayout* leftLayout = new QVBoxLayout(leftWidget);
        leftWidget->setLayout(leftLayout);
        
        QPushButton* button = new QPushButton("Animate", leftWidget);
        leftLayout->addWidget( button );
        button->setCheckable( true );
        connect( button, SIGNAL(toggled(bool)), this, SLOT(buttonToggled(bool)) );

        QTreeView* tv = new QTreeView( leftWidget );
        leftLayout->addWidget( tv );

        m_chart = new KChart::Chart( splitter );
        
        m_model = new QStandardItemModel( 365, 2, this );
        for ( int i = 0; i < 365; ++i ) {
            const QDateTime dt = QDateTime( QDate( 2010, 1, 1 ), QTime() );
            m_model->setData( m_model->index( i, 0 ), dt.addDays( i ) );
            m_model->setData( m_model->index( i, 1 ), sin( i / 10.0 ) * 10 );
        }

        TimeChartModel* proxy = new TimeChartModel( this );
        proxy->setSourceModel( m_model );
        proxy->setVisibleRange( QDateTime( QDate( 2010, 2, 1 ), QTime() ),
                                QDateTime( QDate( 2010, 3, 31 ), QTime() ) );

        KChart::Plotter* plotter = new KChart::Plotter;
        m_chart->coordinatePlane()->replaceDiagram( plotter );

        tv->setModel( proxy );
        tv->show();

        TimeAxis* axis = new TimeAxis( plotter );
        axis->setPosition( TimeAxis::Bottom );
        plotter->addAxis( axis );

        plotter->setModel( proxy );

        connect( proxy, SIGNAL(rowsInserted(QModelIndex,int,int)),
                 m_chart->coordinatePlane(), SLOT(adjustRangesToData()) );
        connect( proxy, SIGNAL(rowsRemoved(QModelIndex,int,int)),
                 m_chart->coordinatePlane(), SLOT(adjustRangesToData()) );

        proxy->setVisibleRange( QDateTime( QDate( 2010, 3, 15 ), QTime() ),
                                QDateTime( QDate( 2010, 5, 18 ), QTime() ) );
        qobject_cast< KChart::CartesianCoordinatePlane* >( m_chart->coordinatePlane() )->adjustRangesToData();

        m_timer = new QTimer(this);
        connect( m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()) );
    }
private Q_SLOTS:
    void slotTimeout() {

        // An ugly hack to prevent the QAbstractItemModel from emitting dataChanged
        // for every single call to setData which would result in a full relayout
        // every time. That is horrible for performance and what we do is to prevent
        // QAbstractItemModel to Q_EMIT dataChanged, collect what data changed and
        // Q_EMIT the signal at the end ourself.
        m_model->blockSignals(true);
        QModelIndexList indexes;

        QVariant v1, v2;
        for ( int i = 0; i < 365; ++i ) {
            QModelIndex idx = m_model->index( i, 1 );
            indexes.append(idx);
            m_model->setData( idx, sin( i / 10.0 + m_counter ) * 10 );
        }

        m_model->blockSignals(false);
        if (!indexes.isEmpty()) {
            m_model->metaObject()->invokeMethod(m_model, "dataChanged", Qt::DirectConnection, Q_ARG(QModelIndex,indexes.first()), Q_ARG(QModelIndex,indexes.last()));
        }

        m_counter += 0.02;
    }
    void buttonToggled(bool checked) {
        if (checked)
            m_timer->start( 200 );
        else
            m_timer->stop();
    }
private:
    KChart::Chart* m_chart;
    QStandardItemModel* m_model;
    QTimer* m_timer;
    qreal m_counter;
};

/**
 * This example demonstrates how to use time-based plots with timestamp-value data points
 * based on seconds and how to use a proxy model for defining the plotted "window" of the
 * measurement data.
 */
int main( int argc, char* argv[] )
{
    QApplication app( argc, argv );
    
    ChartWidget w;
    w.show();

    return app.exec();
}

#include "main.moc"