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

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 #include <KChartLegend>
0011 #include <KChartCartesianAxis>
0012 #include <QColorDialog>
0013 
0014 using namespace KChart;
0015 
0016 MainWindow::MainWindow( QWidget *parent )
0017     : QWidget( parent )
0018     , m_chart( new Chart() )
0019     , m_diagram( m_chart )
0020 {
0021     setupUi( this );
0022 
0023     m_HLCModel.loadFromCSV( ":/HLC" );
0024     m_OHLCModel.loadFromCSV( ":/OHLC" );
0025 
0026     m_diagram.setType( StockDiagram::HighLowClose );
0027     m_diagram.setModel( &m_HLCModel );
0028     m_chart->coordinatePlane()->replaceDiagram( &m_diagram );
0029     KChart::Legend* legend  = new KChart::Legend( &m_diagram, m_chart );
0030     m_chart->addLegend( legend );
0031 
0032     QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
0033     chartLayout->addWidget( m_chart );
0034 
0035     // Abscissa
0036     CartesianAxis *leftAxis = new CartesianAxis( &m_diagram );
0037     // Ordinate
0038     CartesianAxis *bottomAxis = new CartesianAxis( &m_diagram );
0039 
0040     leftAxis->setPosition( CartesianAxis::Left );
0041 
0042     TextAttributes attributes = bottomAxis->textAttributes();
0043     attributes.setRotation( 90 );
0044     attributes.setFontSize( Measure( 7.0, KChartEnums::MeasureCalculationModeAbsolute ) );
0045     bottomAxis->setTextAttributes( attributes );
0046     bottomAxis->setPosition( CartesianAxis::Bottom );
0047     m_diagram.addAxis( leftAxis );
0048     m_diagram.addAxis( bottomAxis );
0049     m_diagram.addAxis( bottomAxis );
0050     applyColor( QColor( "chartreuse" ) );
0051     const bool connected = connect( colorChooser, SIGNAL(clicked()), SLOT(chooseColor()) );
0052     Q_ASSERT( connected );
0053     Q_UNUSED( connected );
0054 
0055     // Initialize all values for the stock chart to sane defaults
0056     initValues();
0057 }
0058 
0059 void MainWindow::chooseColor()
0060 {
0061     applyColor( QColorDialog::getColor( m_diagram.brush().color(), this ) );
0062 }
0063 
0064 void MainWindow::applyColor(const QColor &color)
0065 {
0066     if ( color.isValid() ) {
0067         m_diagram.setPen( 0, QPen( color.darker( 130 ) ) );
0068         m_diagram.setBrush( 0, QBrush( color ) );
0069         QColor inverse( 255 - color.red(), 255 - color.green(), 255 - color.blue() );
0070         m_diagram.setPen( 1, QPen( inverse.darker( 130 ) ) );
0071         m_diagram.setBrush( 1, QBrush( inverse ) );
0072         QPalette pal = colorChooser->palette();
0073         pal.setBrush( QPalette::Button, QBrush( color ) );
0074         colorChooser->setPalette( pal );
0075     }
0076 }
0077 
0078 void MainWindow::initValues()
0079 {
0080     m_threeDBarAttributes = m_diagram.threeDBarAttributes();
0081     m_threeDBarAttributes.setDepth( 10.0 );
0082     m_threeDBarAttributes.setUseShadowColors( false );
0083     threeDProperties->setChecked( m_threeDBarAttributes.isEnabled() );
0084     perspectiveAngle->setValue( m_threeDBarAttributes.angle() );
0085     perspectiveDepth->setValue( (int)m_threeDBarAttributes.depth() );
0086     useShadowColors->setChecked( m_threeDBarAttributes.useShadowColors() );
0087     m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
0088 }
0089 
0090 void MainWindow::on_threeDProperties_toggled( bool checked )
0091 {
0092     m_threeDBarAttributes.setEnabled( checked );
0093     m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
0094     m_chart->update();
0095 }
0096 
0097 void MainWindow::on_perspectiveAngle_valueChanged( int value )
0098 {
0099     m_threeDBarAttributes.setAngle( value );
0100     m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
0101     m_chart->update();
0102 }
0103 
0104 void MainWindow::on_perspectiveDepth_valueChanged( int value )
0105 {
0106     m_threeDBarAttributes.setDepth( value );
0107     m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
0108     m_chart->update();
0109 }
0110 
0111 void MainWindow::on_useShadowColors_toggled( bool checked )
0112 {
0113     m_threeDBarAttributes.setUseShadowColors( checked );
0114     m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
0115     m_chart->update();
0116 }
0117 
0118 void MainWindow::on_stockTypeCB_currentIndexChanged( const QString &text )
0119 {
0120     // FIXME: Workaround for disappearing diagram when setting new model
0121     m_chart->coordinatePlane()->takeDiagram( &m_diagram );
0122 
0123     if ( text == "High-Low-Close" ) {
0124         m_diagram.setType( StockDiagram::HighLowClose );
0125         m_diagram.setModel( &m_HLCModel );
0126     } else if ( text == "Open-High-Low-Close" ) {
0127         m_diagram.setType( StockDiagram::OpenHighLowClose );
0128         m_diagram.setModel( &m_OHLCModel );
0129     } else if ( text == "Candlestick" ) {
0130         m_diagram.setType( StockDiagram::Candlestick );
0131         m_diagram.setModel( &m_OHLCModel );
0132     }
0133 
0134     m_chart->coordinatePlane()->replaceDiagram( &m_diagram );
0135 }
0136