1 /**
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "datasetsettings.h"
21 #include "ui_datasetsettings.h"
22 
23 #include <KChartAbstractCoordinatePlane>
24 #include <KChartChart>
25 
26 #include <QColorDialog>
27 #include <QFileDialog>
28 
29 #include <QStyleFactory>
30 
31 #include <QImage>
32 
33 #include <QObject>
34 
35 #include <QDebug>
36 
37 #include "gradientdialog.h"
38 
39 using namespace KChart;
40 
41 class DatasetSettings::Private : public QObject
42 {
43     Q_OBJECT
44 public:
45     Private( Chart *chart, DatasetSettings *q, QObject *parent );
46     ~Private();
47 
48     Ui::DatasetSettings *ui;
49     int m_dataset;
50     int m_datasetCount;
51     KChart::Chart *m_chart;
52     DatasetSettings *qq;
53 public Q_SLOTS:
54     void changeColor();
55     void changeOutline();
56 };
57 
Private(Chart * chart,DatasetSettings * q,QObject * parent)58 DatasetSettings::Private::Private( Chart *chart, DatasetSettings *q, QObject *parent )
59     : QObject( parent )
60     , ui( new Ui::DatasetSettings )
61     , m_dataset( 0 )
62     , m_datasetCount( 0 )
63     , m_chart( chart )
64     , qq( q )
65 {
66 
67 }
68 
~Private()69 DatasetSettings::Private::~Private()
70 {
71     delete ui;
72 }
73 
changeColor()74 void DatasetSettings::Private::changeColor()
75 {
76     const int index = ui->datasetSelector->currentIndex();
77     if ( ui->Color->isChecked() )
78     {
79         QBrush setBrush = m_chart->coordinatePlane()->diagram()->brush( index );
80         const QColor color = QColorDialog::getColor( setBrush.color(), qq, tr( "Choose new color" ) );
81         if ( !color.isValid() )
82             return;
83         m_chart->coordinatePlane()->diagram()->setBrush( index, color );
84         QPalette palette = ui->colorDisplay->palette();
85         palette.setBrush( QPalette::Button, color );
86         ui->colorDisplay->setPalette( palette );
87     }
88     else if ( ui->textureBtn->isChecked() )
89     {
90         //QBrush setBrush = m_chart->coordinatePlane()->diagram()->brush( index );
91         QImage texture;
92 
93         const QString filename = QFileDialog::getOpenFileName( qq, tr( "Choose Texture" ), QString(), tr( "Images (*.png *.xpm *.jpg)" ) );
94         if ( filename.isEmpty() )
95             return;
96         texture = QImage( filename );
97         m_chart->coordinatePlane()->diagram()->setBrush( index, texture );
98         QPalette palette = ui->colorDisplay->palette();
99         palette.setBrush( QPalette::Button, QBrush( texture ) );
100         ui->colorDisplay->setPalette( palette );
101     }
102     else
103     {
104         QBrush setBrush = m_chart->coordinatePlane()->diagram()->brush( index );
105         QGradient grad;
106         QLinearGradient lGrad;
107         lGrad.setColorAt( 0, Qt::black );
108         lGrad.setColorAt( 1, setBrush.color() );
109         grad = lGrad;
110 
111         if ( setBrush.gradient() )
112             grad = *setBrush.gradient();
113         const QGradient &color = GradientDialog::getGradient( grad, qq, tr( "Choose new color" ) );
114         m_chart->coordinatePlane()->diagram()->setBrush( index, color );
115         QPalette palette = ui->colorDisplay->palette();
116         palette.setBrush( QPalette::Button, QBrush( color ) );
117         ui->colorDisplay->setPalette( palette );
118     }
119 }
120 
changeOutline()121 void DatasetSettings::Private::changeOutline()
122 {
123     const int index = ui->datasetSelector->currentIndex();
124     if ( ui->Color->isChecked() )
125     {
126         QPen pen = m_chart->coordinatePlane()->diagram()->pen( index );
127         const QColor color = QColorDialog::getColor( pen.color(), qq, tr( "Choose new color" ) );
128         if ( !color.isValid() )
129             return;
130         pen.setColor( color );
131         m_chart->coordinatePlane()->diagram()->setPen( index, pen );
132         QPalette palette = ui->outlineBtn->palette();
133         palette.setBrush( QPalette::Button, color );
134         ui->outlineBtn->setPalette( palette );
135     }
136 }
137 
DatasetSettings(Chart * chart,QWidget * parent)138 DatasetSettings::DatasetSettings( Chart *chart, QWidget *parent )
139     : QWidget( parent )
140     , d( new Private( chart, this, this ) )
141 {
142     d->ui->setupUi(this);
143 #ifdef Q_OS_LINUX
144     d->ui->colorDisplay->setStyle( QStyleFactory::create( QStringLiteral( "cleanlooks" ) ) );
145     d->ui->outlineBtn->setStyle( QStyleFactory::create( QStringLiteral( "cleanlooks" ) ) );
146 #endif
147     connect( d->ui->datasetSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(indexChanged(int)) );
148     connect( d->ui->colorDisplay, SIGNAL(clicked()), d, SLOT(changeColor()) );
149     connect( d->ui->outlineBtn, SIGNAL(clicked()), d, SLOT(changeOutline()) );
150 }
151 
~DatasetSettings()152 DatasetSettings::~DatasetSettings()
153 {
154     delete d;
155 }
156 
datasetCount() const157 int DatasetSettings::datasetCount() const
158 {
159     return d->m_datasetCount;
160 }
setDatasetCount(int value)161 void DatasetSettings::setDatasetCount( int value )
162 {
163     if ( d->m_datasetCount != value )
164     {
165         d->m_datasetCount = value;
166         QStringList list;
167         for ( int i = 0; i < value; ++i )
168         {
169             list << tr( "Dataset %1" ).arg( i );
170         }
171         d->ui->datasetSelector->clear();
172         d->ui->datasetSelector->addItems( list );
173         Q_EMIT datasetCountChanged();
174     }
175 }
176 
indexChanged(int index)177 void DatasetSettings::indexChanged( int index )
178 {
179     if ( d->m_chart && index >= 0 )
180     {
181         const QBrush setBrush = d->m_chart->coordinatePlane()->diagram()->brush( index );
182         QPalette palette = d->ui->colorDisplay->palette();
183         if ( setBrush.gradient() )
184             d->ui->radioButton_2->setChecked( true );
185         else if ( !setBrush.textureImage().isNull() )
186             d->ui->textureBtn->setChecked( true );
187         else
188             d->ui->Color->setChecked( true );
189         palette.setBrush( QPalette::Button, setBrush );
190         d->ui->colorDisplay->setPalette( palette );
191         const QPen pen = d->m_chart->coordinatePlane()->diagram()->pen( index );
192         QPalette penPalette = d->ui->outlineBtn->palette();
193         penPalette.setBrush( QPalette::Button, pen.color() );
194         d->ui->outlineBtn->setPalette( penPalette );
195     }
196 }
197 
diagramTypeChanged()198 void DatasetSettings::diagramTypeChanged()
199 {
200 
201 }
202 
203 #include "datasetsettings.moc"
204