File indexing completed on 2025-02-16 04:03:08

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 <KChartLineDiagram>
0014 #include <KChartLegend>
0015 
0016 #include <QDebug>
0017 #include <QPainter>
0018 
0019 using namespace KChart;
0020 
0021 MainWindow::MainWindow( QWidget* parent ) :
0022     QWidget( parent )
0023 {
0024     setupUi( this );
0025 
0026     QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
0027     m_chart = new Chart();
0028     m_chart->setGlobalLeadingTop( 10 );
0029     m_chart->setGlobalLeadingRight( 10 );
0030     chartLayout->addWidget( m_chart );
0031     hSBar->setVisible( false );
0032     vSBar->setVisible( false );
0033 
0034     m_model.loadFromCSV( ":/data" );
0035 
0036     // Set up the diagram
0037     m_lines = new LineDiagram();
0038     m_lines->setModel( &m_model );
0039 
0040     CartesianAxis *xAxis = new CartesianAxis( m_lines );
0041     CartesianAxis *yAxis = new CartesianAxis ( m_lines );
0042     xAxis->setPosition ( KChart::CartesianAxis::Bottom );
0043     yAxis->setPosition ( KChart::CartesianAxis::Left );
0044 
0045     xAxis->setTitleText ( "Abscissa axis at the bottom" );
0046     yAxis->setTitleText ( "Ordinate axis at the left side" );
0047 
0048     m_lines->addAxis( xAxis );
0049     m_lines->addAxis( yAxis );
0050     m_chart->coordinatePlane()->replaceDiagram( m_lines );
0051 
0052     connect( m_chart, SIGNAL(propertiesChanged()), SLOT(applyNewZoomParameters()) );
0053 
0054     // Set up the legend
0055     m_legend = new Legend( m_lines, m_chart );
0056     m_chart->addLegend( m_legend );
0057     m_legend->setPosition( KChartEnums::PositionEast );
0058     m_legend->setAlignment( Qt::AlignTop );
0059 }
0060 
0061 
0062 void MainWindow::on_zoomFactorSB_valueChanged( double factor )
0063 {
0064     if ( factor > 1 ) {
0065         hSBar->setVisible( true );
0066         vSBar->setVisible( true );
0067     } else {
0068         hSBar->setValue( 500 );
0069         vSBar->setValue( 500 );
0070         hSBar->setVisible( false );
0071         vSBar->setVisible( false );
0072     }
0073     m_chart->coordinatePlane()->setZoomFactorX( factor );
0074     m_chart->coordinatePlane()->setZoomFactorY( factor );
0075     m_chart->update();
0076 }
0077 
0078 void MainWindow::on_adjustGridCB_toggled( bool checked )
0079 {
0080     static_cast <CartesianCoordinatePlane*>( m_chart->coordinatePlane() )
0081             ->setAutoAdjustGridToZoom( checked );
0082     m_chart->update();
0083 }
0084 
0085 void MainWindow::on_rubberBandZoomCB_toggled( bool checked )
0086 {
0087     m_chart->coordinatePlane()->setRubberBandZoomingEnabled( checked );
0088 }
0089 
0090 void MainWindow::on_hSBar_valueChanged( int hPos )
0091 {
0092     m_chart->coordinatePlane()->setZoomCenter( QPointF(hPos/1000.0, vSBar->value()/1000.0) );
0093     m_chart->update();
0094 }
0095 
0096 void MainWindow::on_vSBar_valueChanged( int vPos )
0097 {
0098     m_chart->coordinatePlane()->setZoomCenter( QPointF( hSBar->value()/1000.0, vPos/1000.0) );
0099     m_chart->update();
0100 }
0101 
0102 void MainWindow::applyNewZoomParameters()
0103 {
0104     hSBar->blockSignals( true );
0105     vSBar->blockSignals( true );
0106 
0107     hSBar->setValue( qRound( m_chart->coordinatePlane()->zoomCenter().x() * 1000 ) );
0108     vSBar->setValue( qRound( m_chart->coordinatePlane()->zoomCenter().y() * 1000 ) );
0109     zoomFactorSB->setValue( m_chart->coordinatePlane()->zoomFactorX() );
0110 
0111     hSBar->blockSignals( false );
0112     vSBar->blockSignals( false );
0113 }