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

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 "MainWidget.h"
0010 
0011 #include "KChartAbstractDiagram_p.h"
0012 #include "KChartChart.h"
0013 #include "KChartPlotter.h"
0014 
0015 #include <QHBoxLayout>
0016 
0017 #include <cmath>
0018 
0019 MainWidget::MainWidget()
0020    : m_controlsContainer( new QWidget( this ) )
0021 {
0022     // set up the chart
0023 
0024     QHBoxLayout* topLayout = new QHBoxLayout( this );
0025     m_controls.setupUi( m_controlsContainer );
0026     topLayout->addWidget( m_controlsContainer );
0027 
0028     KChart::Chart* chart = new KChart::Chart;
0029     topLayout->addWidget( chart );
0030 
0031     m_plotter = new KChart::Plotter;
0032     m_plotter->setModel( &m_model );
0033     KChart::AbstractDiagram::Private::get( m_plotter )->doDumpPaintTime = true;
0034     chart->coordinatePlane()->replaceDiagram( m_plotter );
0035 
0036     KChart::CartesianCoordinatePlane* cPlane
0037         = qobject_cast< KChart::CartesianCoordinatePlane* >( chart->coordinatePlane() );
0038     Q_ASSERT( cPlane );
0039     cPlane->setVerticalRange( QPair< qreal, qreal >( -2, 2 ) );
0040 
0041     KChart::CartesianAxis* xAxis = new KChart::CartesianAxis( m_plotter );
0042     xAxis->setPosition( KChart::CartesianAxis::Bottom );
0043     xAxis->setTitleText("X");
0044     m_plotter->addAxis( xAxis );
0045 
0046     KChart::CartesianAxis* yAxis = new KChart::CartesianAxis( m_plotter );
0047     yAxis->setPosition( KChart::CartesianAxis::Left );
0048     yAxis->setTitleText("Y");
0049     m_plotter->addAxis( yAxis );
0050 
0051     // wire up the UI
0052 
0053     // index of elements in vector must match corresponding Model::Function enum values
0054     m_functionSelector << m_controls.sineRadio << m_controls.triangleRadio << m_controls.squareRadio
0055                        << m_controls.noiseRadio << m_controls.oneDivSineRadio
0056                        << m_controls.sineOneDivRadio;
0057     for ( QRadioButton* r : qAsConst(m_functionSelector) ) {
0058         connect( r, SIGNAL(toggled(bool)), SLOT(functionToggled(bool)) );
0059     }
0060 
0061     connect( m_controls.runButton, SIGNAL(toggled(bool)),
0062              &m_model, SLOT(setRunning(bool)) );
0063 
0064     // order matters again
0065     m_addPointsButtons << m_controls.add1kButton << m_controls.add10kButton << m_controls.add100kButton;
0066     for ( QPushButton* b : qAsConst(m_addPointsButtons) ) {
0067         connect( b, SIGNAL(clicked(bool)), SLOT(addPointsButtonClicked()) );
0068     }
0069 }
0070 
0071 // slot
0072 void MainWidget::functionToggled( bool checked )
0073 {
0074     if ( !checked ) {
0075         return;
0076     }
0077     int idx = m_functionSelector.indexOf( qobject_cast< QRadioButton* >( sender() ) );
0078     Q_ASSERT( idx >= 0 );
0079     m_model.setFunction( static_cast< Model::Function >( idx ) );
0080 }
0081 
0082 // slot
0083 void MainWidget::addPointsButtonClicked()
0084 {
0085     int idx = m_addPointsButtons.indexOf( qobject_cast< QPushButton* >( sender() ) );
0086     Q_ASSERT( idx >= 0 );
0087     m_model.appendPoints( pow( qreal( 10 ), qreal( idx + 3 ) ) );
0088 }