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

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 "diagramsettings.h"
0010 #include "ui_diagramsettings.h"
0011 
0012 #include "gradientdialog.h"
0013 
0014 #include <QColorDialog>
0015 
0016 #include <QStyleFactory>
0017 
0018 #include <QFileDialog>
0019 
0020 #include <KChartThreeDBarAttributes>
0021 #include <KChartThreeDLineAttributes>
0022 #include <KChartThreeDPieAttributes>
0023 
0024 #include <KChartChart>
0025 #include <KChartBarDiagram>
0026 #include <KChartLineDiagram>
0027 #include <KChartPieDiagram>
0028 #include <KChartCartesianCoordinatePlane>
0029 
0030 #include <KChartBackgroundAttributes>
0031 
0032 using namespace KChart;
0033 
0034 class DiagramSettings::Private : public QObject
0035 {
0036     Q_OBJECT
0037 public:
0038     Private( Chart *chart = nullptr, DiagramSettings *q = nullptr );
0039     ~Private() override;
0040     void init();
0041     void setThreeDData( const AbstractThreeDAttributes &attr );
0042 
0043     Chart *m_chart;
0044     Ui::DiagramSettings *ui;
0045     DiagramSettings *qq;
0046 public Q_SLOTS:
0047     void changeThreeD();
0048     void changeBackgroundColor();
0049     void changeBackgroundVisibility();
0050     void changeAutoGradient();
0051 };
0052 
0053 DiagramSettings::Private::Private( Chart *chart, DiagramSettings *q )
0054     : QObject( q )
0055     , m_chart( chart )
0056     , ui(new Ui::DiagramSettings)
0057     , qq( q )
0058 {
0059 
0060 }
0061 
0062 DiagramSettings::Private::~Private()
0063 {
0064 
0065 }
0066 
0067 void DiagramSettings::Private::changeAutoGradient()
0068 {
0069     changeThreeD();
0070 }
0071 
0072 void DiagramSettings::Private::changeThreeD()
0073 {
0074     if ( m_chart && m_chart->coordinatePlane() && m_chart->coordinatePlane()->diagram() )
0075     {
0076         BarDiagram *bars = qobject_cast< BarDiagram* >( m_chart->coordinatePlane()->diagram() );
0077         LineDiagram *lines = qobject_cast< LineDiagram* >( m_chart->coordinatePlane()->diagram() );
0078         PieDiagram *pie = qobject_cast< PieDiagram* >( m_chart->coordinatePlane()->diagram() );
0079         if ( bars )
0080         {
0081             ThreeDBarAttributes td( bars->threeDBarAttributes() );
0082             td.setEnabled( ui->threeDSelector->checkState() == Qt::Checked );
0083             td.setUseShadowColors( true );
0084             td.setDepth( ui->barHeightInput->value() );
0085             td.setThreeDBrushEnabled( ui->autoGradient->checkState() == Qt::Checked );
0086             bars->setThreeDBarAttributes( td );
0087             m_chart->update();
0088         }
0089         else if ( lines )
0090         {
0091             ThreeDLineAttributes td( lines->threeDLineAttributes() );
0092             td.setEnabled( ui->threeDSelector->checkState() == Qt::Checked );
0093             td.setDepth( ui->barHeightInput->value() );
0094             td.setThreeDBrushEnabled( ui->autoGradient->checkState() == Qt::Checked );
0095             lines->setThreeDLineAttributes( td );
0096             m_chart->update();
0097         }
0098         else if ( pie )
0099         {
0100             ThreeDPieAttributes td( pie->threeDPieAttributes() );
0101             td.setEnabled( ui->threeDSelector->checkState() == Qt::Checked );
0102             td.setDepth( ui->barHeightInput->value() );
0103             td.setThreeDBrushEnabled( ui->autoGradient->checkState() == Qt::Checked );
0104             pie->setThreeDPieAttributes( td );
0105             m_chart->update();
0106         }
0107     }
0108 }
0109 void DiagramSettings::Private::changeBackgroundColor()
0110 {
0111     if ( m_chart && m_chart->coordinatePlane() && m_chart->coordinatePlane()->diagram() )
0112     {
0113         BackgroundAttributes bat = m_chart->coordinatePlane()->backgroundAttributes();
0114         bat.setVisible( true );
0115         ui->visibleBtn->setChecked( true );
0116 
0117         if ( ui->color->isChecked() )
0118         {
0119 
0120             QBrush setBrush = bat.brush();
0121             const QColor color = QColorDialog::getColor( setBrush.color(), qq, tr( "Choose new color" ) );
0122             if ( !color.isValid() )
0123                 return;
0124             bat.setBrush( color );
0125             QPalette palette = ui->diagramBackground->palette();
0126             palette.setBrush( QPalette::Button, color );
0127             ui->diagramBackground->setPalette( palette );
0128         }
0129         else if ( ui->textureBtn->isChecked() )
0130         {
0131             //QBrush setBrush = m_chart->coordinatePlane()->diagram()->brush( index );
0132             QImage texture;
0133 
0134             const QString filename = QFileDialog::getOpenFileName( qq, tr( "Choose Texture" ), QString(), tr( "Images (*.png *.xpm *.jpg)" ) );
0135             if ( filename.isEmpty() )
0136                 return;
0137             texture = QImage( filename );
0138             bat.setBrush( texture );
0139             QPalette palette = ui->diagramBackground->palette();
0140             palette.setBrush( QPalette::Button, QBrush( texture ) );
0141             ui->diagramBackground->setPalette( palette );
0142         }
0143         else
0144         {
0145             QBrush setBrush = bat.brush();
0146             QGradient grad;
0147             QLinearGradient lGrad;
0148             lGrad.setColorAt( 0, Qt::black );
0149             lGrad.setColorAt( 1, setBrush.color() );
0150             grad = lGrad;
0151 
0152             if ( setBrush.gradient() )
0153                 grad = *setBrush.gradient();
0154             const QGradient &color = GradientDialog::getGradient( grad, qq, tr( "Choose new color" ) );
0155             bat.setBrush( color );
0156             QPalette palette = ui->diagramBackground->palette();
0157             palette.setBrush( QPalette::Button, QBrush( color ) );
0158             ui->diagramBackground->setPalette( palette );
0159         }
0160         bat.setVisible( true );
0161         m_chart->coordinatePlane()->setBackgroundAttributes( bat );
0162         qq->update();
0163     }
0164 }
0165 
0166 void DiagramSettings::Private::changeBackgroundVisibility()
0167 {
0168     if ( m_chart && m_chart->coordinatePlane() && m_chart->coordinatePlane()->diagram() )
0169     {
0170         BackgroundAttributes bat = m_chart->coordinatePlane()->backgroundAttributes();
0171         bat.setVisible( ui->visibleBtn->checkState() == Qt::Checked );
0172         m_chart->coordinatePlane()->setBackgroundAttributes( bat );
0173         m_chart->update();
0174     }
0175 }
0176 
0177 void DiagramSettings::Private::init()
0178 {
0179     ui->setupUi( qq );
0180 #ifdef Q_OS_LINUX
0181     ui->diagramBackground->setStyle( QStyleFactory::create( QStringLiteral( "cleanlooks" ) ) );
0182 #endif
0183 
0184     connect( ui->threeDSelector, SIGNAL(toggled(bool)), this, SLOT(changeThreeD()) );
0185     connect( ui->diagramBackground, SIGNAL(clicked()), this, SLOT(changeBackgroundColor()) );
0186     connect( ui->visibleBtn, SIGNAL(toggled(bool)), this, SLOT(changeBackgroundVisibility()) );
0187     connect( ui->barHeightInput, SIGNAL(valueChanged(int)), this, SLOT(changeThreeD()) );
0188     connect( ui->autoGradient, SIGNAL(toggled(bool)), this, SLOT(changeAutoGradient()) );
0189 
0190     qq->refreshSettings();
0191 }
0192 
0193 void DiagramSettings::Private::setThreeDData( const AbstractThreeDAttributes &attr )
0194 {
0195     ui->threeDSelector->setCheckState( attr.isEnabled() ? Qt::Checked : Qt::Unchecked );
0196     ui->autoGradient->setCheckState( attr.isThreeDBrushEnabled() ? Qt::Checked : Qt::Unchecked );
0197     ui->barHeightInput->setValue( attr.depth() );
0198 }
0199 
0200 DiagramSettings::DiagramSettings( Chart *chart, QWidget *parent )
0201     : QWidget( parent )
0202     , d( new Private( chart, this ) )
0203 {
0204     d->init();
0205 }
0206 
0207 DiagramSettings::~DiagramSettings()
0208 {
0209     delete d;
0210 }
0211 
0212 void DiagramSettings::refreshSettings()
0213 {
0214     if ( d->m_chart && d->m_chart->coordinatePlane() && d->m_chart->coordinatePlane()->diagram() )
0215     {
0216         BarDiagram *bars = qobject_cast< BarDiagram* >( d->m_chart->coordinatePlane()->diagram() );
0217         LineDiagram *lines = qobject_cast< LineDiagram* >( d->m_chart->coordinatePlane()->diagram() );
0218         PieDiagram *pie = qobject_cast< PieDiagram* >( d->m_chart->coordinatePlane()->diagram() );
0219 
0220         if ( bars )
0221         {
0222             const AbstractThreeDAttributes &td = bars->threeDBarAttributes();
0223             d->setThreeDData( td );
0224         }
0225         else if ( lines )
0226         {
0227             const AbstractThreeDAttributes &td = lines->threeDLineAttributes();
0228             d->setThreeDData( td );
0229         }
0230         else if ( pie )
0231         {
0232             const AbstractThreeDAttributes &td = pie->threeDPieAttributes();
0233             d->setThreeDData( td );
0234         }
0235         BackgroundAttributes bat = d->m_chart->coordinatePlane()->backgroundAttributes();
0236         QBrush setBrush = bat.brush();
0237         QPalette palette = d->ui->diagramBackground->palette();
0238         if ( bat.isVisible() )
0239             palette.setBrush( QPalette::Button, setBrush );
0240         else
0241             palette.setBrush( QPalette::Button, this->palette().brush( QPalette::Button ) );
0242         d->ui->diagramBackground->setPalette( palette );
0243     }
0244 }
0245 
0246 #include "diagramsettings.moc"