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

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