File indexing completed on 2024-05-19 15:26:57

0001 /**
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "mainwindow.h"
0021 
0022 #include <KChartChart>
0023 #include <KChartAbstractCoordinatePlane>
0024 #include <KChartCartesianAxis>
0025 #include <KChartLineDiagram>
0026 #include <KChartLegend>
0027 
0028 
0029 #include <QDebug>
0030 #include <QPen>
0031 #include <QHBoxLayout>
0032 #include <QStandardItemModel>
0033 
0034 using namespace KChart;
0035 
0036 class EmptyModel : public QAbstractItemModel
0037 {
0038 public:
0039     EmptyModel( QObject* parent = nullptr )
0040         : QAbstractItemModel( parent )
0041     {
0042         //qDebug() << "EmptyModel::EmptyModel()";
0043     }
0044 
0045     ~EmptyModel()
0046     {
0047         //qDebug() << "EmptyModel::~EmptyModel()";
0048     }
0049 
0050     int columnCount( const QModelIndex& parent = QModelIndex() ) const override
0051     {
0052         Q_UNUSED( parent );
0053         //qDebug() << "EmptyModel::columnCount(...)";
0054         return 0;
0055     }
0056 
0057     int rowCount( const QModelIndex& parent = QModelIndex() ) const override
0058     {
0059         Q_UNUSED( parent );
0060         //qDebug() << "EmptyModel::rowCount(...)";
0061         return 0;
0062     }
0063 
0064 
0065     // NOTE: The following method will not be called by KD Chart,
0066     //       because the model is returning 0 for columnCount() / rowCount().
0067     QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override
0068     {
0069         Q_UNUSED( role );
0070         qDebug() << "EmptyModel::data(" << index.row() << index.column() << ")";
0071         Q_ASSERT_X( false, "EmptyModel::data", "We should not end here..." );
0072         return QVariant();
0073     }
0074 
0075     QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const override
0076     {
0077         Q_UNUSED( row );
0078         Q_UNUSED( column );
0079         Q_UNUSED( parent );
0080         //qDebug() << "EmptyModel::index(" << row << column << ")";
0081         return QModelIndex();
0082     }
0083 
0084     QModelIndex parent( const QModelIndex& parent ) const override
0085     {
0086         Q_UNUSED( parent );
0087         //qDebug() << "EmptyModel::parent(...)";
0088         return QModelIndex();
0089     }
0090 };
0091 
0092 MainWindow::MainWindow( QWidget* parent ) :
0093     QWidget( parent )
0094 {
0095     QHBoxLayout* chartLayout = new QHBoxLayout( this );
0096     m_chart = new Chart();
0097     m_chart->setGlobalLeading( 5, 5, 5, 5 );
0098     chartLayout->addWidget( m_chart );
0099 
0100     m_model = new EmptyModel( this ); // model contains no data at all
0101 
0102     // Set up the diagram
0103     m_bars = new LineDiagram();
0104     m_bars->setModel( m_model );
0105     CartesianAxis *xAxis = new CartesianAxis( m_bars );
0106     CartesianAxis *yAxis = new CartesianAxis ( m_bars );
0107     xAxis->setPosition ( KChart::CartesianAxis::Bottom );
0108     yAxis->setPosition ( KChart::CartesianAxis::Left );
0109     xAxis->setTitleText ( "Abscissa axis at the bottom" );
0110     yAxis->setTitleText ( "Ordinate axis at the left side" );
0111     m_bars->addAxis( xAxis );
0112     m_bars->addAxis( yAxis );
0113 
0114     m_chart->coordinatePlane()->replaceDiagram( m_bars );
0115 
0116     Legend* legend = new Legend( m_bars, m_chart );
0117     legend->setPosition( Position::South );
0118     legend->setAlignment( Qt::AlignCenter );
0119     legend->setShowLines( true );
0120     legend->setTitleText("This is the legend - showing no data either");
0121     legend->setOrientation( Qt::Horizontal );
0122     legend->addDiagram( m_bars );
0123     m_chart->addLegend( legend );
0124 }
0125