File indexing completed on 2024-05-12 04:20:11

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 "mainwindow.h"
0010 
0011 #include <KChartChart>
0012 #include <KChartAbstractCoordinatePlane>
0013 #include <KChartCartesianAxis>
0014 #include <KChartLineDiagram>
0015 #include <KChartLegend>
0016 
0017 
0018 #include <QDebug>
0019 #include <QPen>
0020 #include <QHBoxLayout>
0021 #include <QStandardItemModel>
0022 
0023 using namespace KChart;
0024 
0025 class EmptyModel : public QAbstractItemModel
0026 {
0027 public:
0028     EmptyModel( QObject* parent = nullptr )
0029         : QAbstractItemModel( parent )
0030     {
0031         //qDebug() << "EmptyModel::EmptyModel()";
0032     }
0033 
0034     ~EmptyModel() override
0035     {
0036         //qDebug() << "EmptyModel::~EmptyModel()";
0037     }
0038 
0039     int columnCount( const QModelIndex& parent = QModelIndex() ) const override
0040     {
0041         Q_UNUSED( parent );
0042         //qDebug() << "EmptyModel::columnCount(...)";
0043         return 0;
0044     }
0045 
0046     int rowCount( const QModelIndex& parent = QModelIndex() ) const override
0047     {
0048         Q_UNUSED( parent );
0049         //qDebug() << "EmptyModel::rowCount(...)";
0050         return 0;
0051     }
0052 
0053 
0054     // NOTE: The following method will not be called by KD Chart,
0055     //       because the model is returning 0 for columnCount() / rowCount().
0056     QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override
0057     {
0058         Q_UNUSED( role );
0059         qDebug() << "EmptyModel::data(" << index.row() << index.column() << ")";
0060         Q_ASSERT_X( false, "EmptyModel::data", "We should not end here..." );
0061         return QVariant();
0062     }
0063 
0064     QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const override
0065     {
0066         Q_UNUSED( row );
0067         Q_UNUSED( column );
0068         Q_UNUSED( parent );
0069         //qDebug() << "EmptyModel::index(" << row << column << ")";
0070         return QModelIndex();
0071     }
0072 
0073     QModelIndex parent( const QModelIndex& parent ) const override
0074     {
0075         Q_UNUSED( parent );
0076         //qDebug() << "EmptyModel::parent(...)";
0077         return QModelIndex();
0078     }
0079 };
0080 
0081 MainWindow::MainWindow( QWidget* parent ) :
0082     QWidget( parent )
0083 {
0084     QHBoxLayout* chartLayout = new QHBoxLayout( this );
0085     m_chart = new Chart();
0086     m_chart->setGlobalLeading( 5, 5, 5, 5 );
0087     chartLayout->addWidget( m_chart );
0088 
0089     m_model = new EmptyModel( this ); // model contains no data at all
0090 
0091     // Set up the diagram
0092     m_bars = new LineDiagram();
0093     m_bars->setModel( m_model );
0094     CartesianAxis *xAxis = new CartesianAxis( m_bars );
0095     CartesianAxis *yAxis = new CartesianAxis ( m_bars );
0096     xAxis->setPosition ( KChart::CartesianAxis::Bottom );
0097     yAxis->setPosition ( KChart::CartesianAxis::Left );
0098     xAxis->setTitleText ( "Abscissa axis at the bottom" );
0099     yAxis->setTitleText ( "Ordinate axis at the left side" );
0100     m_bars->addAxis( xAxis );
0101     m_bars->addAxis( yAxis );
0102 
0103     m_chart->coordinatePlane()->replaceDiagram( m_bars );
0104 
0105     Legend* legend = new Legend( m_bars, m_chart );
0106     legend->setPosition( Position::South );
0107     legend->setAlignment( Qt::AlignCenter );
0108     legend->setShowLines( true );
0109     legend->setTitleText("This is the legend - showing no data either");
0110     legend->setOrientation( Qt::Horizontal );
0111     legend->addDiagram( m_bars );
0112     m_chart->addLegend( legend );
0113 }
0114