1 /***************************************************************************
2 * Copyright (C) 2000-2019 by Johan Maes *
3 * on4qz@telenet.be *
4 * http://users.telenet.be/on4qz *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (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, write to the *
18 * Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21 #include "gradientdialog.h"
22 //#include "appdefs.h"
23 #include <QColorDialog>
24
25
gradientDialog(QWidget * parent)26 gradientDialog::gradientDialog(QWidget *parent):QDialog(parent), Ui::gradientForm()
27 {
28 setupUi(this);
29 readSettings();
30 connect(color1Button,SIGNAL(clicked()),SLOT(slotColorDialog()));
31 connect(color2Button,SIGNAL(clicked()),SLOT(slotColorDialog()));
32 connect(color3Button,SIGNAL(clicked()),SLOT(slotColorDialog()));
33 connect(color4Button,SIGNAL(clicked()),SLOT(slotColorDialog()));
34
35 previewLabel->setBackgroundRole(QPalette::Base);
36 g=NULL;
37 slotUpdate();
38 connect(pos1SpinBox,SIGNAL(valueChanged(int)),SLOT(slotUpdate()));
39 connect(pos2SpinBox,SIGNAL(valueChanged(int)),SLOT(slotUpdate()));
40 connect(pos3SpinBox,SIGNAL(valueChanged(int)),SLOT(slotUpdate()));
41 connect(pos4SpinBox,SIGNAL(valueChanged(int)),SLOT(slotUpdate()));
42 connect(directionDial,SIGNAL(valueChanged(int)),SLOT(slotUpdate()));
43 connect(noGradientButton,SIGNAL(clicked()),SLOT(slotUpdate()));
44 connect(linearGradientButton,SIGNAL(clicked()),SLOT(slotUpdate()));
45 connect(radialGradientButton,SIGNAL(clicked()),SLOT(slotUpdate()));
46 connect(conicalGradientButton,SIGNAL(clicked()),SLOT(slotUpdate()));
47
48 }
49
~gradientDialog()50 gradientDialog::~gradientDialog()
51 {
52 writeSettings();
53 if(g==NULL) delete g;
54 }
55
readSettings()56 void gradientDialog::readSettings()
57 {
58 QSettings qSettings;
59 qSettings.beginGroup ("Editor");
60 gParam.color1 = qSettings.value("gradcolor1", QColor(Qt::red)).value<QColor>();
61 gParam.color2 = qSettings.value("gradcolor2", QColor(Qt::green )).value<QColor>();
62 gParam.color3 = qSettings.value("gradcolor3", QColor(Qt::yellow)).value<QColor>();
63 gParam.color4 = qSettings.value("gradcolor4", QColor(Qt::blue )).value<QColor>();
64 gParam.pos1=qSettings.value("gradpos1", 0 ).toInt();
65 gParam.pos2=qSettings.value("gradpos2", 0 ).toInt();
66 gParam.pos3=qSettings.value("gradpos3", 0 ).toInt();
67 gParam.pos4=qSettings.value("gradpos4", 0 ).toInt();
68 pos1SpinBox->setValue(gParam.pos1);
69 pos2SpinBox->setValue(gParam.pos2);
70 pos3SpinBox->setValue(gParam.pos3);
71 pos4SpinBox->setValue(gParam.pos4);
72 gParam.direction=qSettings.value("graddirection", 0 ).toInt();
73 directionDial->setValue((gParam.direction+90)%360); // 0 degrees is East not North
74 directionLCD->display(gParam.direction);
75 if(qSettings.value("nogradbutton", 1 ).toBool())
76 {
77 noGradientButton->setChecked(true);
78 gParam.type=sgradientParam::NONE;
79 }
80 else if(qSettings.value("lineargradbutton", 0 ).toBool())
81 {
82 linearGradientButton->setChecked(true);
83 gParam.type=sgradientParam::LINEAR;;
84 }
85 else if(qSettings.value("radialgradbutton", 0 ).toBool())
86 {
87 radialGradientButton->setChecked(true);
88 gParam.type=sgradientParam::RADIAL;
89 }
90 else if(qSettings.value("conicalgradbutton", 0 ).toBool())
91 {
92 conicalGradientButton->setChecked(true);
93 gParam.type=sgradientParam::CONICAL;
94 }
95 qSettings.endGroup();
96 }
97
writeSettings()98 void gradientDialog::writeSettings()
99 {
100 QSettings qSettings;
101 qSettings.beginGroup ("Editor" );
102
103 qSettings.setValue ("gradcolor1", gParam.color1);
104 qSettings.setValue ("gradcolor2", gParam.color2);
105 qSettings.setValue ("gradcolor3", gParam.color3);
106 qSettings.setValue ("gradcolor4", gParam.color4);
107 qSettings.setValue ("gradpos1", gParam.pos1);
108 qSettings.setValue ("gradpos2", gParam.pos2);
109 qSettings.setValue ("gradpos3", gParam.pos3);
110 qSettings.setValue ("gradpos4", gParam.pos4);
111 qSettings.setValue ("graddirection", gParam.direction);
112 qSettings.setValue ("nogradbutton",noGradientButton->isChecked());
113 qSettings.setValue ("lineargradbutton",linearGradientButton->isChecked());
114 qSettings.setValue ("radialgradbutton",radialGradientButton->isChecked());
115 qSettings.setValue ("conicalgradbutton",conicalGradientButton->isChecked());
116 qSettings.endGroup();
117 }
118
load(QDataStream & str)119 void sgradientParam::load(QDataStream &str)
120 {
121 int t;
122 str >> color1;
123 str >> color2;
124 str >> color3;
125 str >> color4;
126 str >> pos1;
127 str >> pos2;
128 str >> pos3;
129 str >>pos4;
130 str >> t;
131 type=(gType)t;
132 str >> direction;
133 }
save(QDataStream & str)134 void sgradientParam::save(QDataStream &str)
135 {
136 str << color1;
137 str << color2;
138 str << color3;
139 str << color4;
140 str << pos1;
141 str << pos2;
142 str << pos3;
143 str << pos4;
144 str << (int)type;
145 str << direction;
146 }
147
148
slotColorDialog()149 void gradientDialog::slotColorDialog()
150 {
151 QColor c;
152 QPushButton *act=qobject_cast<QPushButton *>(sender());
153 if (act==color1Button)
154 {
155 c=QColorDialog::getColor(gParam.color1,this,"",QColorDialog::ShowAlphaChannel);
156 if (c.isValid()) gParam.color1=c;
157 }
158 else if (act==color2Button)
159 {
160 c=QColorDialog::getColor(gParam.color2,this,"",QColorDialog::ShowAlphaChannel);
161 if (c.isValid()) gParam.color2=c;
162 }
163 else if (act==color3Button)
164 {
165 c=QColorDialog::getColor(gParam.color3,this,"",QColorDialog::ShowAlphaChannel);
166 if (c.isValid()) gParam.color3=c;
167 }
168 else if (act==color4Button)
169 {
170 c=QColorDialog::getColor(gParam.color4,this,"",QColorDialog::ShowAlphaChannel);
171 if (c.isValid()) gParam.color4=c;
172 }
173 slotUpdate();
174 }
175
176 /*! \todo split param update from graphic creation
177 */
slotUpdate()178 void gradientDialog::slotUpdate()
179 {
180 QString s;
181 QPalette palette;
182 QBrush brush;
183 gParam.direction=(270+directionDial->value())%360; // 0 degrees is East not North
184 directionLCD->display(gParam.direction);
185 gParam.pos1=pos1SpinBox->value();
186 gParam.pos2=pos2SpinBox->value();
187 gParam.pos3=pos3SpinBox->value();
188 gParam.pos4=pos4SpinBox->value();
189
190 if(noGradientButton->isChecked()) gParam.type=sgradientParam::NONE;
191 else if (linearGradientButton->isChecked()) gParam.type=sgradientParam::LINEAR;
192 else if (radialGradientButton->isChecked()) gParam.type=sgradientParam::RADIAL;
193 else if (conicalGradientButton->isChecked()) gParam.type=sgradientParam::CONICAL;
194
195 s=gParam.color1.name();
196 color1Button->setStyleSheet("background-color: "+s+"; border-style: outset; border-width: 2px;border-radius: 10px; border-color: beige; padding: 6px");
197 s=gParam.color2.name();
198 color2Button->setStyleSheet("background-color: "+s+"; border-style: outset; border-width: 2px;border-radius: 10px; border-color: beige; padding: 6px");
199 s=gParam.color3.name();
200 color3Button->setStyleSheet("background-color: "+s+"; border-style: outset; border-width: 2px;border-radius: 10px; border-color: beige; padding: 6px");
201 s=gParam.color4.name();
202 color4Button->setStyleSheet("background-color: "+s+"; border-style: outset; border-width: 2px;border-radius: 10px; border-color: beige; padding: 6px");
203
204 brush.setStyle(Qt::SolidPattern);
205 if(gParam.type!=sgradientParam::NONE)
206 {
207 QBrush br(buildGradient(gParam,previewLabel->rect()));
208 palette.setBrush(QPalette::Active, QPalette::Base, br);
209 }
210 else
211 {
212 QBrush br(gParam.color1);
213 palette.setBrush(QPalette::Active, QPalette::Base, br);
214 }
215 previewLabel->setPalette(palette);
216 }
217
218
selectGradient()219 void gradientDialog::selectGradient()
220 {
221 exec();
222 }
223
grSetup(sgradientParam prm,QGradient & g)224 void grSetup (sgradientParam prm,QGradient &g)
225 {
226 g.setColorAt(prm.pos1/100.,prm.color1);
227 if(prm.pos2<=prm.pos1) return ;
228 g.setColorAt(prm.pos2/100.,prm.color2);
229 if(prm.pos3<=prm.pos2) return ;
230 g.setColorAt(prm.pos3/100.,prm.color3);
231 if(prm.pos4<=prm.pos3) return ;
232 g.setColorAt(prm.pos4/100.,prm.color4);
233 }
234
buildGradient(sgradientParam prm,QRectF f)235 QGradient buildGradient(sgradientParam prm, QRectF f)
236 {
237 qreal w=f.width();
238 qreal h=f.height();
239 qreal d=(double)prm.direction;
240 qreal x1,y1,x2,y2;
241 qreal temp;
242 if(prm.type==sgradientParam::NONE)
243 {
244 QLinearGradient g(0,0,0,0);
245 grSetup(prm,g);
246 return g;
247 }
248 if(prm.type==sgradientParam::LINEAR)
249 {
250 if(fabs(w/2*tan(M_PI/2-d*M_PI/180))<=(w/2))
251 {
252 x1=f.x()+w/2-(w/2*tan(M_PI/2-d*M_PI/180.));
253 y1=f.y()+h;
254 x2=f.x()+w/2+(w/2*tan(M_PI/2-d*M_PI/180.));
255 y2=f.y();
256 if ((prm.direction>180) && (prm.direction<=359))
257 {
258 temp=x1; x1=x2;x2=temp;
259 temp=y1; y1=y2;y2=temp;
260 }
261 }
262 else
263 {
264 x1=f.x();
265 y1=f.y()+h/2+(h/2*tan(d*M_PI/180.));
266 x2=f.x()+w;
267 y2=f.y()+h/2-(h/2*tan(d*M_PI/180.));
268 if ((prm.direction>90) && (prm.direction<=270))
269 {
270 temp=x1; x1=x2;x2=temp;
271 temp=y1; y1=y2;y2=temp;
272 }
273 }
274 QLinearGradient g(x1,y1,x2,y2);
275
276 grSetup(prm,g);
277 return g;
278 }
279 else if(prm.type==sgradientParam::RADIAL)
280 {
281 QRadialGradient g(QPointF(f.x()+f.width()/2,f.y()+f.height()/2),f.width()/2);
282 grSetup(prm,g);
283 return g;
284 }
285 else
286 {
287 QConicalGradient g(QPointF(f.x()+f.width()/2,f.y()+f.height()/2),prm.direction);
288 grSetup(prm,g);
289 return g;
290 }
291 }
292