source: trunk/tools/shared/qtgradienteditor/qtgradienteditor.cpp@ 360

Last change on this file since 360 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 33.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*
43TRANSLATOR qdesigner_internal::QtGradientEditor
44*/
45
46#include "qtgradienteditor.h"
47#include "qtgradientstopscontroller.h"
48#include "ui_qtgradienteditor.h"
49
50QT_BEGIN_NAMESPACE
51
52class QtGradientEditorPrivate
53{
54 QtGradientEditor *q_ptr;
55 Q_DECLARE_PUBLIC(QtGradientEditor)
56public:
57 QtGradientEditorPrivate() : m_gradient(QLinearGradient()) {}
58
59 void slotGradientStopsChanged(const QGradientStops &stops);
60 void slotTypeChanged(int type);
61 void slotSpreadChanged(int spread);
62 void slotStartLinearXChanged(double value);
63 void slotStartLinearYChanged(double value);
64 void slotEndLinearXChanged(double value);
65 void slotEndLinearYChanged(double value);
66 void slotCentralRadialXChanged(double value);
67 void slotCentralRadialYChanged(double value);
68 void slotFocalRadialXChanged(double value);
69 void slotFocalRadialYChanged(double value);
70 void slotRadiusRadialChanged(double value);
71 void slotCentralConicalXChanged(double value);
72 void slotCentralConicalYChanged(double value);
73 void slotAngleConicalChanged(double value);
74
75 void slotDetailsChanged(bool details);
76
77 void startLinearChanged(const QPointF &point);
78 void endLinearChanged(const QPointF &point);
79 void centralRadialChanged(const QPointF &point);
80 void focalRadialChanged(const QPointF &point);
81 void radiusRadialChanged(qreal radius);
82 void centralConicalChanged(const QPointF &point);
83 void angleConicalChanged(qreal angle);
84
85 void setStartLinear(const QPointF &point);
86 void setEndLinear(const QPointF &point);
87 void setCentralRadial(const QPointF &point);
88 void setFocalRadial(const QPointF &point);
89 void setRadiusRadial(qreal radius);
90 void setCentralConical(const QPointF &point);
91 void setAngleConical(qreal angle);
92
93 void setType(QGradient::Type type);
94 void showDetails(bool details);
95
96 void setSpinBox(QDoubleSpinBox *spinBox, const char *slot, double max = 1.0, double step = 0.01, int decimals = 3);
97 void reset();
98 void setLayout(bool details);
99 void layoutDetails(bool details);
100 bool row4Visible() const;
101 bool row5Visible() const;
102 int extensionWidthHint() const;
103
104 void setCombos(bool combos);
105
106 QGradient gradient() const;
107 void updateGradient(bool emitSignal);
108
109 Ui::QtGradientEditor m_ui;
110 QtGradientStopsController *m_gradientStopsController;
111
112 QDoubleSpinBox *startLinearXSpinBox;
113 QDoubleSpinBox *startLinearYSpinBox;
114 QDoubleSpinBox *endLinearXSpinBox;
115 QDoubleSpinBox *endLinearYSpinBox;
116 QDoubleSpinBox *centralRadialXSpinBox;
117 QDoubleSpinBox *centralRadialYSpinBox;
118 QDoubleSpinBox *focalRadialXSpinBox;
119 QDoubleSpinBox *focalRadialYSpinBox;
120 QDoubleSpinBox *radiusRadialSpinBox;
121 QDoubleSpinBox *centralConicalXSpinBox;
122 QDoubleSpinBox *centralConicalYSpinBox;
123 QDoubleSpinBox *angleConicalSpinBox;
124
125 QButtonGroup *m_typeGroup;
126 QButtonGroup *m_spreadGroup;
127
128 QGradient::Type m_type;
129
130 QGridLayout *m_gridLayout;
131 QWidget *m_hiddenWidget;
132 QGridLayout *m_hiddenLayout;
133 bool m_details;
134 bool m_detailsButtonVisible;
135 bool m_backgroundCheckered;
136
137 QGradient m_gradient;
138
139 bool m_combos;
140};
141
142QGradient QtGradientEditorPrivate::gradient() const
143{
144 QGradient *gradient = 0;
145 switch (m_ui.gradientWidget->gradientType()) {
146 case QGradient::LinearGradient:
147 gradient = new QLinearGradient(m_ui.gradientWidget->startLinear(),
148 m_ui.gradientWidget->endLinear());
149 break;
150 case QGradient::RadialGradient:
151 gradient = new QRadialGradient(m_ui.gradientWidget->centralRadial(),
152 m_ui.gradientWidget->radiusRadial(),
153 m_ui.gradientWidget->focalRadial());
154 break;
155 case QGradient::ConicalGradient:
156 gradient = new QConicalGradient(m_ui.gradientWidget->centralConical(),
157 m_ui.gradientWidget->angleConical());
158 break;
159 default:
160 break;
161 }
162 if (!gradient)
163 return QGradient();
164 gradient->setStops(m_ui.gradientWidget->gradientStops());
165 gradient->setSpread(m_ui.gradientWidget->gradientSpread());
166 gradient->setCoordinateMode(QGradient::StretchToDeviceMode);
167 QGradient gr = *gradient;
168 delete gradient;
169 return gr;
170}
171
172void QtGradientEditorPrivate::updateGradient(bool emitSignal)
173{
174 QGradient grad = gradient();
175 if (m_gradient == grad)
176 return;
177
178 m_gradient = grad;
179 if (emitSignal)
180 emit q_ptr->gradientChanged(m_gradient);
181}
182
183void QtGradientEditorPrivate::setCombos(bool combos)
184{
185 if (m_combos == combos)
186 return;
187
188 m_combos = combos;
189 m_ui.linearButton->setVisible(!m_combos);
190 m_ui.radialButton->setVisible(!m_combos);
191 m_ui.conicalButton->setVisible(!m_combos);
192 m_ui.padButton->setVisible(!m_combos);
193 m_ui.repeatButton->setVisible(!m_combos);
194 m_ui.reflectButton->setVisible(!m_combos);
195 m_ui.typeComboBox->setVisible(m_combos);
196 m_ui.spreadComboBox->setVisible(m_combos);
197}
198
199void QtGradientEditorPrivate::setLayout(bool details)
200{
201 QHBoxLayout *hboxLayout = new QHBoxLayout();
202 hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
203 hboxLayout->addWidget(m_ui.typeComboBox);
204 hboxLayout->addWidget(m_ui.spreadComboBox);
205 QHBoxLayout *typeLayout = new QHBoxLayout();
206 typeLayout->setSpacing(0);
207 typeLayout->addWidget(m_ui.linearButton);
208 typeLayout->addWidget(m_ui.radialButton);
209 typeLayout->addWidget(m_ui.conicalButton);
210 hboxLayout->addLayout(typeLayout);
211 QHBoxLayout *spreadLayout = new QHBoxLayout();
212 spreadLayout->setSpacing(0);
213 spreadLayout->addWidget(m_ui.padButton);
214 spreadLayout->addWidget(m_ui.repeatButton);
215 spreadLayout->addWidget(m_ui.reflectButton);
216 hboxLayout->addLayout(spreadLayout);
217 hboxLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
218 hboxLayout->addWidget(m_ui.detailsButton);
219 m_gridLayout->addLayout(hboxLayout, 0, 0, 1, 2);
220 int span = 1;
221 if (details)
222 span = 7;
223 m_gridLayout->addWidget(m_ui.frame, 1, 0, span, 2);
224 int row = 2;
225 if (details) {
226 row = 8;
227 span = 4;
228 }
229 m_gridLayout->addWidget(m_ui.gradientStopsWidget, row, 0, span, 2);
230 QHBoxLayout *hboxLayout1 = new QHBoxLayout();
231 hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1"));
232 hboxLayout1->addWidget(m_ui.colorLabel);
233 hboxLayout1->addWidget(m_ui.colorButton);
234 hboxLayout1->addWidget(m_ui.hsvRadioButton);
235 hboxLayout1->addWidget(m_ui.rgbRadioButton);
236 hboxLayout1->addItem(new QSpacerItem(16, 23, QSizePolicy::Expanding, QSizePolicy::Minimum));
237 int addRow = 0;
238 if (details)
239 addRow = 9;
240 m_gridLayout->addLayout(hboxLayout1, 3 + addRow, 0, 1, 2);
241 m_gridLayout->addWidget(m_ui.hLabel, 4 + addRow, 0, 1, 1);
242 m_gridLayout->addWidget(m_ui.frame_2, 4 + addRow, 1, 1, 1);
243 m_gridLayout->addWidget(m_ui.sLabel, 5 + addRow, 0, 1, 1);
244 m_gridLayout->addWidget(m_ui.frame_5, 5 + addRow, 1, 1, 1);
245 m_gridLayout->addWidget(m_ui.vLabel, 6 + addRow, 0, 1, 1);
246 m_gridLayout->addWidget(m_ui.frame_3, 6 + addRow, 1, 1, 1);
247 m_gridLayout->addWidget(m_ui.aLabel, 7 + addRow, 0, 1, 1);
248 m_gridLayout->addWidget(m_ui.frame_4, 7 + addRow, 1, 1, 1);
249
250 if (details) {
251 layoutDetails(details);
252 }
253}
254
255void QtGradientEditorPrivate::layoutDetails(bool details)
256{
257 QGridLayout *gridLayout = m_gridLayout;
258 int col = 2;
259 if (!details) {
260 col = 0;
261 if (!m_hiddenWidget) {
262 m_hiddenWidget = new QWidget();
263 m_hiddenLayout = new QGridLayout(m_hiddenWidget);
264 m_hiddenLayout->setContentsMargins(0, 0, 0, 0);
265 m_hiddenLayout->setSizeConstraint(QLayout::SetFixedSize);
266 }
267 gridLayout = m_hiddenLayout;
268 }
269 gridLayout->addWidget(m_ui.label1, 1, col + 0, 1, 1);
270 gridLayout->addWidget(m_ui.spinBox1, 1, col + 1, 1, 1);
271 gridLayout->addWidget(m_ui.label2, 2, col + 0, 1, 1);
272 gridLayout->addWidget(m_ui.spinBox2, 2, col + 1, 1, 1);
273 gridLayout->addWidget(m_ui.label3, 3, col + 0, 1, 1);
274 gridLayout->addWidget(m_ui.spinBox3, 3, col + 1, 1, 1);
275 gridLayout->addWidget(m_ui.label4, 4, col + 0, 1, 1);
276 gridLayout->addWidget(m_ui.spinBox4, 4, col + 1, 1, 1);
277 gridLayout->addWidget(m_ui.label5, 5, col + 0, 1, 1);
278 gridLayout->addWidget(m_ui.spinBox5, 5, col + 1, 1, 1);
279 gridLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 6, col + 0, 1, 1);
280 gridLayout->addWidget(m_ui.line1Widget, 7, col + 0, 1, 2);
281 gridLayout->addWidget(m_ui.zoomLabel, 8, col + 0, 1, 1);
282 gridLayout->addWidget(m_ui.zoomWidget, 8, col + 1, 1, 1);
283 gridLayout->addWidget(m_ui.zoomButtonsWidget, 9, col + 0, 1, 1);
284 gridLayout->addWidget(m_ui.zoomAllButton, 9, col + 1, 1, 1);
285 gridLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Preferred), 10, col + 0, 1, 1);
286 gridLayout->addWidget(m_ui.line2Widget, 11, col + 0, 1, 2);
287 gridLayout->addWidget(m_ui.positionLabel, 12, col + 0, 1, 1);
288 gridLayout->addWidget(m_ui.positionWidget, 12, col + 1, 1, 1);
289 gridLayout->addWidget(m_ui.hueLabel, 13, col + 0, 1, 1);
290 gridLayout->addWidget(m_ui.hueWidget, 13, col + 1, 1, 1);
291 gridLayout->addWidget(m_ui.saturationLabel, 14, col + 0, 1, 1);
292 gridLayout->addWidget(m_ui.saturationWidget, 14, col + 1, 1, 1);
293 gridLayout->addWidget(m_ui.valueLabel, 15, col + 0, 1, 1);
294 gridLayout->addWidget(m_ui.valueWidget, 15, col + 1, 1, 1);
295 gridLayout->addWidget(m_ui.alphaLabel, 16, col + 0, 1, 1);
296 gridLayout->addWidget(m_ui.alphaWidget, 16, col + 1, 1, 1);
297
298 if (details) {
299 if (m_hiddenLayout) {
300 delete m_hiddenLayout;
301 m_hiddenLayout = 0;
302 }
303 if (m_hiddenWidget) {
304 delete m_hiddenWidget;
305 m_hiddenWidget = 0;
306 }
307 }
308}
309
310int QtGradientEditorPrivate::extensionWidthHint() const
311{
312 if (m_details)
313 return q_ptr->size().width() - m_ui.gradientStopsWidget->size().width();
314
315 const int space = m_ui.spinBox1->geometry().left() - m_ui.label1->geometry().right();
316
317 return m_hiddenLayout->minimumSize().width() + space;
318}
319
320void QtGradientEditorPrivate::slotDetailsChanged(bool details)
321{
322 showDetails(details);
323}
324
325bool QtGradientEditorPrivate::row4Visible() const
326{
327 if (m_type == QGradient::ConicalGradient)
328 return false;
329 return true;
330}
331
332bool QtGradientEditorPrivate::row5Visible() const
333{
334 if (m_type == QGradient::RadialGradient)
335 return true;
336 return false;
337}
338
339void QtGradientEditorPrivate::showDetails(bool details)
340{
341 if (m_details == details)
342 return;
343
344 bool blocked = m_ui.detailsButton->signalsBlocked();
345 m_ui.detailsButton->blockSignals(true);
346 m_ui.detailsButton->setChecked(details);
347 m_ui.detailsButton->blockSignals(blocked);
348
349 bool updates = q_ptr->updatesEnabled();
350 q_ptr->setUpdatesEnabled(false);
351
352 if (m_gridLayout) {
353 m_gridLayout->setEnabled(false);
354 delete m_gridLayout;
355 m_gridLayout = 0;
356 }
357
358 if (!details) {
359 layoutDetails(details);
360 }
361
362 emit q_ptr->aboutToShowDetails(details, extensionWidthHint());
363 m_details = details;
364
365 m_gridLayout = new QGridLayout(q_ptr);
366 m_gridLayout->setEnabled(false);
367 m_gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
368 m_gridLayout->setContentsMargins(0, 0, 0, 0);
369
370 m_ui.label4->setVisible(row4Visible());
371 m_ui.label5->setVisible(row5Visible());
372 m_ui.spinBox4->setVisible(row4Visible());
373 m_ui.spinBox5->setVisible(row5Visible());
374
375 setLayout(details);
376 m_gridLayout->setEnabled(true);
377
378 q_ptr->setUpdatesEnabled(updates);
379 q_ptr->update();
380}
381
382void QtGradientEditorPrivate::setSpinBox(QDoubleSpinBox *spinBox, const char *slot, double max, double step, int decimals)
383{
384 bool blocked = spinBox->signalsBlocked();
385 spinBox->blockSignals(true);
386 spinBox->setDecimals(decimals);
387 spinBox->setMaximum(max);
388 spinBox->setSingleStep(step);
389 spinBox->blockSignals(blocked);
390 QObject::connect(spinBox, SIGNAL(valueChanged(double)), q_ptr, slot);
391}
392
393void QtGradientEditorPrivate::reset()
394{
395 startLinearXSpinBox = 0;
396 startLinearYSpinBox = 0;
397 endLinearXSpinBox = 0;
398 endLinearYSpinBox = 0;
399 centralRadialXSpinBox = 0;
400 centralRadialYSpinBox = 0;
401 focalRadialXSpinBox = 0;
402 focalRadialYSpinBox = 0;
403 radiusRadialSpinBox = 0;
404 centralConicalXSpinBox = 0;
405 centralConicalYSpinBox = 0;
406 angleConicalSpinBox = 0;
407}
408
409void QtGradientEditorPrivate::setType(QGradient::Type type)
410{
411 if (m_type == type)
412 return;
413
414 m_type = type;
415 m_ui.spinBox1->disconnect(SIGNAL(valueChanged(double)));
416 m_ui.spinBox2->disconnect(SIGNAL(valueChanged(double)));
417 m_ui.spinBox3->disconnect(SIGNAL(valueChanged(double)));
418 m_ui.spinBox4->disconnect(SIGNAL(valueChanged(double)));
419 m_ui.spinBox5->disconnect(SIGNAL(valueChanged(double)));
420
421 reset();
422
423 bool ena = true;
424
425 if (m_gridLayout) {
426 ena = m_gridLayout->isEnabled();
427 m_gridLayout->setEnabled(false);
428 }
429
430 bool spreadEnabled = true;
431
432 if (type == QGradient::LinearGradient) {
433 startLinearXSpinBox = m_ui.spinBox1;
434 setSpinBox(startLinearXSpinBox, SLOT(slotStartLinearXChanged(double)));
435 m_ui.label1->setText(QApplication::translate("QtGradientEditor", "Start X", 0, QApplication::UnicodeUTF8));
436
437 startLinearYSpinBox = m_ui.spinBox2;
438 setSpinBox(startLinearYSpinBox, SLOT(slotStartLinearYChanged(double)));
439 m_ui.label2->setText(QApplication::translate("QtGradientEditor", "Start Y", 0, QApplication::UnicodeUTF8));
440
441 endLinearXSpinBox = m_ui.spinBox3;
442 setSpinBox(endLinearXSpinBox, SLOT(slotEndLinearXChanged(double)));
443 m_ui.label3->setText(QApplication::translate("QtGradientEditor", "Final X", 0, QApplication::UnicodeUTF8));
444
445 endLinearYSpinBox = m_ui.spinBox4;
446 setSpinBox(endLinearYSpinBox, SLOT(slotEndLinearYChanged(double)));
447 m_ui.label4->setText(QApplication::translate("QtGradientEditor", "Final Y", 0, QApplication::UnicodeUTF8));
448
449 setStartLinear(m_ui.gradientWidget->startLinear());
450 setEndLinear(m_ui.gradientWidget->endLinear());
451 } else if (type == QGradient::RadialGradient) {
452 centralRadialXSpinBox = m_ui.spinBox1;
453 setSpinBox(centralRadialXSpinBox, SLOT(slotCentralRadialXChanged(double)));
454 m_ui.label1->setText(QApplication::translate("QtGradientEditor", "Central X", 0, QApplication::UnicodeUTF8));
455
456 centralRadialYSpinBox = m_ui.spinBox2;
457 setSpinBox(centralRadialYSpinBox, SLOT(slotCentralRadialYChanged(double)));
458 m_ui.label2->setText(QApplication::translate("QtGradientEditor", "Central Y", 0, QApplication::UnicodeUTF8));
459
460 focalRadialXSpinBox = m_ui.spinBox3;
461 setSpinBox(focalRadialXSpinBox, SLOT(slotFocalRadialXChanged(double)));
462 m_ui.label3->setText(QApplication::translate("QtGradientEditor", "Focal X", 0, QApplication::UnicodeUTF8));
463
464 focalRadialYSpinBox = m_ui.spinBox4;
465 setSpinBox(focalRadialYSpinBox, SLOT(slotFocalRadialYChanged(double)));
466 m_ui.label4->setText(QApplication::translate("QtGradientEditor", "Focal Y", 0, QApplication::UnicodeUTF8));
467
468 radiusRadialSpinBox = m_ui.spinBox5;
469 setSpinBox(radiusRadialSpinBox, SLOT(slotRadiusRadialChanged(double)), 2.0);
470 m_ui.label5->setText(QApplication::translate("QtGradientEditor", "Radius", 0, QApplication::UnicodeUTF8));
471
472 setCentralRadial(m_ui.gradientWidget->centralRadial());
473 setFocalRadial(m_ui.gradientWidget->focalRadial());
474 setRadiusRadial(m_ui.gradientWidget->radiusRadial());
475 } else if (type == QGradient::ConicalGradient) {
476 centralConicalXSpinBox = m_ui.spinBox1;
477 setSpinBox(centralConicalXSpinBox, SLOT(slotCentralConicalXChanged(double)));
478 m_ui.label1->setText(QApplication::translate("QtGradientEditor", "Central X", 0, QApplication::UnicodeUTF8));
479
480 centralConicalYSpinBox = m_ui.spinBox2;
481 setSpinBox(centralConicalYSpinBox, SLOT(slotCentralConicalYChanged(double)));
482 m_ui.label2->setText(QApplication::translate("QtGradientEditor", "Central Y", 0, QApplication::UnicodeUTF8));
483
484 angleConicalSpinBox = m_ui.spinBox3;
485 setSpinBox(angleConicalSpinBox, SLOT(slotAngleConicalChanged(double)), 360.0, 1.0, 1);
486 m_ui.label3->setText(QApplication::translate("QtGradientEditor", "Angle", 0, QApplication::UnicodeUTF8));
487
488 setCentralConical(m_ui.gradientWidget->centralConical());
489 setAngleConical(m_ui.gradientWidget->angleConical());
490
491 spreadEnabled = false;
492 }
493 m_ui.spreadComboBox->setEnabled(spreadEnabled);
494 m_ui.padButton->setEnabled(spreadEnabled);
495 m_ui.repeatButton->setEnabled(spreadEnabled);
496 m_ui.reflectButton->setEnabled(spreadEnabled);
497
498 m_ui.label4->setVisible(row4Visible());
499 m_ui.spinBox4->setVisible(row4Visible());
500 m_ui.label5->setVisible(row5Visible());
501 m_ui.spinBox5->setVisible(row5Visible());
502
503 if (m_gridLayout) {
504 m_gridLayout->setEnabled(ena);
505 }
506}
507
508void QtGradientEditorPrivate::slotGradientStopsChanged(const QGradientStops &stops)
509{
510 m_ui.gradientWidget->setGradientStops(stops);
511 updateGradient(true);
512}
513
514void QtGradientEditorPrivate::slotTypeChanged(int idx)
515{
516 QGradient::Type type = QGradient::NoGradient;
517 if (idx == 0)
518 type = QGradient::LinearGradient;
519 else if (idx == 1)
520 type = QGradient::RadialGradient;
521 else if (idx == 2)
522 type = QGradient::ConicalGradient;
523 setType(type);
524 m_ui.typeComboBox->setCurrentIndex(idx);
525 m_typeGroup->button(idx)->setChecked(true);
526 m_ui.gradientWidget->setGradientType(type);
527 updateGradient(true);
528}
529
530void QtGradientEditorPrivate::slotSpreadChanged(int spread)
531{
532 if (spread == 0) {
533 m_ui.gradientWidget->setGradientSpread(QGradient::PadSpread);
534 } else if (spread == 1) {
535 m_ui.gradientWidget->setGradientSpread(QGradient::RepeatSpread);
536 } else if (spread == 2) {
537 m_ui.gradientWidget->setGradientSpread(QGradient::ReflectSpread);
538 }
539 m_ui.spreadComboBox->setCurrentIndex(spread);
540 updateGradient(true);
541}
542
543void QtGradientEditorPrivate::slotStartLinearXChanged(double value)
544{
545 QPointF point = m_ui.gradientWidget->startLinear();
546 point.setX(value);
547 m_ui.gradientWidget->setStartLinear(point);
548 updateGradient(true);
549}
550
551void QtGradientEditorPrivate::slotStartLinearYChanged(double value)
552{
553 QPointF point = m_ui.gradientWidget->startLinear();
554 point.setY(value);
555 m_ui.gradientWidget->setStartLinear(point);
556 updateGradient(true);
557}
558
559void QtGradientEditorPrivate::slotEndLinearXChanged(double value)
560{
561 QPointF point = m_ui.gradientWidget->endLinear();
562 point.setX(value);
563 m_ui.gradientWidget->setEndLinear(point);
564 updateGradient(true);
565}
566
567void QtGradientEditorPrivate::slotEndLinearYChanged(double value)
568{
569 QPointF point = m_ui.gradientWidget->endLinear();
570 point.setY(value);
571 m_ui.gradientWidget->setEndLinear(point);
572 updateGradient(true);
573}
574
575void QtGradientEditorPrivate::slotCentralRadialXChanged(double value)
576{
577 QPointF point = m_ui.gradientWidget->centralRadial();
578 point.setX(value);
579 m_ui.gradientWidget->setCentralRadial(point);
580 updateGradient(true);
581}
582
583void QtGradientEditorPrivate::slotCentralRadialYChanged(double value)
584{
585 QPointF point = m_ui.gradientWidget->centralRadial();
586 point.setY(value);
587 m_ui.gradientWidget->setCentralRadial(point);
588 updateGradient(true);
589}
590
591void QtGradientEditorPrivate::slotFocalRadialXChanged(double value)
592{
593 QPointF point = m_ui.gradientWidget->focalRadial();
594 point.setX(value);
595 m_ui.gradientWidget->setFocalRadial(point);
596 updateGradient(true);
597}
598
599void QtGradientEditorPrivate::slotFocalRadialYChanged(double value)
600{
601 QPointF point = m_ui.gradientWidget->focalRadial();
602 point.setY(value);
603 m_ui.gradientWidget->setFocalRadial(point);
604 updateGradient(true);
605}
606
607void QtGradientEditorPrivate::slotRadiusRadialChanged(double value)
608{
609 m_ui.gradientWidget->setRadiusRadial(value);
610 updateGradient(true);
611}
612
613void QtGradientEditorPrivate::slotCentralConicalXChanged(double value)
614{
615 QPointF point = m_ui.gradientWidget->centralConical();
616 point.setX(value);
617 m_ui.gradientWidget->setCentralConical(point);
618 updateGradient(true);
619}
620
621void QtGradientEditorPrivate::slotCentralConicalYChanged(double value)
622{
623 QPointF point = m_ui.gradientWidget->centralConical();
624 point.setY(value);
625 m_ui.gradientWidget->setCentralConical(point);
626 updateGradient(true);
627}
628
629void QtGradientEditorPrivate::slotAngleConicalChanged(double value)
630{
631 m_ui.gradientWidget->setAngleConical(value);
632 updateGradient(true);
633}
634
635void QtGradientEditorPrivate::startLinearChanged(const QPointF &point)
636{
637 setStartLinear(point);
638 updateGradient(true);
639}
640
641void QtGradientEditorPrivate::endLinearChanged(const QPointF &point)
642{
643 setEndLinear(point);
644 updateGradient(true);
645}
646
647void QtGradientEditorPrivate::centralRadialChanged(const QPointF &point)
648{
649 setCentralRadial(point);
650 updateGradient(true);
651}
652
653void QtGradientEditorPrivate::focalRadialChanged(const QPointF &point)
654{
655 setFocalRadial(point);
656 updateGradient(true);
657}
658
659void QtGradientEditorPrivate::radiusRadialChanged(qreal radius)
660{
661 setRadiusRadial(radius);
662 updateGradient(true);
663}
664
665void QtGradientEditorPrivate::centralConicalChanged(const QPointF &point)
666{
667 setCentralConical(point);
668 updateGradient(true);
669}
670
671void QtGradientEditorPrivate::angleConicalChanged(qreal angle)
672{
673 setAngleConical(angle);
674 updateGradient(true);
675}
676
677void QtGradientEditorPrivate::setStartLinear(const QPointF &point)
678{
679 if (startLinearXSpinBox)
680 startLinearXSpinBox->setValue(point.x());
681 if (startLinearYSpinBox)
682 startLinearYSpinBox->setValue(point.y());
683}
684
685void QtGradientEditorPrivate::setEndLinear(const QPointF &point)
686{
687 if (endLinearXSpinBox)
688 endLinearXSpinBox->setValue(point.x());
689 if (endLinearYSpinBox)
690 endLinearYSpinBox->setValue(point.y());
691}
692
693void QtGradientEditorPrivate::setCentralRadial(const QPointF &point)
694{
695 if (centralRadialXSpinBox)
696 centralRadialXSpinBox->setValue(point.x());
697 if (centralRadialYSpinBox)
698 centralRadialYSpinBox->setValue(point.y());
699}
700
701void QtGradientEditorPrivate::setFocalRadial(const QPointF &point)
702{
703 if (focalRadialXSpinBox)
704 focalRadialXSpinBox->setValue(point.x());
705 if (focalRadialYSpinBox)
706 focalRadialYSpinBox->setValue(point.y());
707}
708
709void QtGradientEditorPrivate::setRadiusRadial(qreal radius)
710{
711 if (radiusRadialSpinBox)
712 radiusRadialSpinBox->setValue(radius);
713}
714
715void QtGradientEditorPrivate::setCentralConical(const QPointF &point)
716{
717 if (centralConicalXSpinBox)
718 centralConicalXSpinBox->setValue(point.x());
719 if (centralConicalYSpinBox)
720 centralConicalYSpinBox->setValue(point.y());
721}
722
723void QtGradientEditorPrivate::setAngleConical(qreal angle)
724{
725 if (angleConicalSpinBox)
726 angleConicalSpinBox->setValue(angle);
727}
728
729QtGradientEditor::QtGradientEditor(QWidget *parent)
730 : QWidget(parent)
731{
732 d_ptr = new QtGradientEditorPrivate();
733 d_ptr->q_ptr = this;
734 d_ptr->m_type = QGradient::RadialGradient;
735 d_ptr->m_ui.setupUi(this);
736 d_ptr->m_gridLayout = 0;
737 d_ptr->m_hiddenLayout = 0;
738 d_ptr->m_hiddenWidget = 0;
739 bool detailsDefault = false;
740 d_ptr->m_details = !detailsDefault;
741 d_ptr->m_detailsButtonVisible = true;
742 bool checkeredDefault = true;
743 d_ptr->m_backgroundCheckered = !checkeredDefault;
744 d_ptr->m_gradientStopsController = new QtGradientStopsController(this);
745 d_ptr->m_gradientStopsController->setUi(&d_ptr->m_ui);
746 d_ptr->reset();
747 d_ptr->setType(QGradient::LinearGradient);
748 d_ptr->m_combos = true;
749 d_ptr->setCombos(!d_ptr->m_combos);
750
751 d_ptr->showDetails(detailsDefault);
752 setBackgroundCheckered(checkeredDefault);
753
754 d_ptr->setStartLinear(QPointF(0, 0));
755 d_ptr->setEndLinear(QPointF(1, 1));
756 d_ptr->setCentralRadial(QPointF(0.5, 0.5));
757 d_ptr->setFocalRadial(QPointF(0.5, 0.5));
758 d_ptr->setRadiusRadial(0.5);
759 d_ptr->setCentralConical(QPointF(0.5, 0.5));
760 d_ptr->setAngleConical(0);
761
762 QIcon icon;
763 icon.addPixmap(style()->standardPixmap(QStyle::SP_ArrowRight), QIcon::Normal, QIcon::Off);
764 icon.addPixmap(style()->standardPixmap(QStyle::SP_ArrowLeft), QIcon::Normal, QIcon::On);
765 d_ptr->m_ui.detailsButton->setIcon(icon);
766
767 connect(d_ptr->m_ui.detailsButton, SIGNAL(clicked(bool)), this, SLOT(slotDetailsChanged(bool)));
768 connect(d_ptr->m_gradientStopsController, SIGNAL(gradientStopsChanged(const QGradientStops &)),
769 this, SLOT(slotGradientStopsChanged(const QGradientStops &)));
770
771 QIcon iconLinear(QLatin1String(":/trolltech/qtgradienteditor/images/typelinear.png"));
772 QIcon iconRadial(QLatin1String(":/trolltech/qtgradienteditor/images/typeradial.png"));
773 QIcon iconConical(QLatin1String(":/trolltech/qtgradienteditor/images/typeconical.png"));
774
775 d_ptr->m_ui.typeComboBox->addItem(iconLinear, tr("Linear"));
776 d_ptr->m_ui.typeComboBox->addItem(iconRadial, tr("Radial"));
777 d_ptr->m_ui.typeComboBox->addItem(iconConical, tr("Conical"));
778
779 d_ptr->m_ui.linearButton->setIcon(iconLinear);
780 d_ptr->m_ui.radialButton->setIcon(iconRadial);
781 d_ptr->m_ui.conicalButton->setIcon(iconConical);
782
783 d_ptr->m_typeGroup = new QButtonGroup(this);
784 d_ptr->m_typeGroup->addButton(d_ptr->m_ui.linearButton, 0);
785 d_ptr->m_typeGroup->addButton(d_ptr->m_ui.radialButton, 1);
786 d_ptr->m_typeGroup->addButton(d_ptr->m_ui.conicalButton, 2);
787
788 connect(d_ptr->m_typeGroup, SIGNAL(buttonClicked(int)),
789 this, SLOT(slotTypeChanged(int)));
790 connect(d_ptr->m_ui.typeComboBox, SIGNAL(activated(int)),
791 this, SLOT(slotTypeChanged(int)));
792
793 QIcon iconPad(QLatin1String(":/trolltech/qtgradienteditor/images/spreadpad.png"));
794 QIcon iconRepeat(QLatin1String(":/trolltech/qtgradienteditor/images/spreadrepeat.png"));
795 QIcon iconReflect(QLatin1String(":/trolltech/qtgradienteditor/images/spreadreflect.png"));
796
797 d_ptr->m_ui.spreadComboBox->addItem(iconPad, tr("Pad"));
798 d_ptr->m_ui.spreadComboBox->addItem(iconRepeat, tr("Repeat"));
799 d_ptr->m_ui.spreadComboBox->addItem(iconReflect, tr("Reflect"));
800
801 d_ptr->m_ui.padButton->setIcon(iconPad);
802 d_ptr->m_ui.repeatButton->setIcon(iconRepeat);
803 d_ptr->m_ui.reflectButton->setIcon(iconReflect);
804
805 d_ptr->m_spreadGroup = new QButtonGroup(this);
806 d_ptr->m_spreadGroup->addButton(d_ptr->m_ui.padButton, 0);
807 d_ptr->m_spreadGroup->addButton(d_ptr->m_ui.repeatButton, 1);
808 d_ptr->m_spreadGroup->addButton(d_ptr->m_ui.reflectButton, 2);
809 connect(d_ptr->m_spreadGroup, SIGNAL(buttonClicked(int)),
810 this, SLOT(slotSpreadChanged(int)));
811 connect(d_ptr->m_ui.spreadComboBox, SIGNAL(activated(int)),
812 this, SLOT(slotSpreadChanged(int)));
813
814 connect(d_ptr->m_ui.gradientWidget, SIGNAL(startLinearChanged(const QPointF &)),
815 this, SLOT(startLinearChanged(const QPointF &)));
816 connect(d_ptr->m_ui.gradientWidget, SIGNAL(endLinearChanged(const QPointF &)),
817 this, SLOT(endLinearChanged(const QPointF &)));
818 connect(d_ptr->m_ui.gradientWidget, SIGNAL(centralRadialChanged(const QPointF &)),
819 this, SLOT(centralRadialChanged(const QPointF &)));
820 connect(d_ptr->m_ui.gradientWidget, SIGNAL(focalRadialChanged(const QPointF &)),
821 this, SLOT(focalRadialChanged(const QPointF &)));
822 connect(d_ptr->m_ui.gradientWidget, SIGNAL(radiusRadialChanged(qreal)),
823 this, SLOT(radiusRadialChanged(qreal)));
824 connect(d_ptr->m_ui.gradientWidget, SIGNAL(centralConicalChanged(const QPointF &)),
825 this, SLOT(centralConicalChanged(const QPointF &)));
826 connect(d_ptr->m_ui.gradientWidget, SIGNAL(angleConicalChanged(qreal)),
827 this, SLOT(angleConicalChanged(qreal)));
828
829 QGradientStops stops = gradient().stops();
830 d_ptr->m_gradientStopsController->setGradientStops(stops);
831 d_ptr->m_ui.gradientWidget->setGradientStops(stops);
832}
833
834QtGradientEditor::~QtGradientEditor()
835{
836 if (d_ptr->m_hiddenWidget)
837 delete d_ptr->m_hiddenWidget;
838 delete d_ptr;
839}
840
841void QtGradientEditor::setGradient(const QGradient &grad)
842{
843 if (grad == gradient())
844 return;
845
846 QGradient::Type type = grad.type();
847 int idx = 0;
848 switch (type) {
849 case QGradient::LinearGradient: idx = 0; break;
850 case QGradient::RadialGradient: idx = 1; break;
851 case QGradient::ConicalGradient: idx = 2; break;
852 default: return;
853 }
854 d_ptr->setType(type);
855 d_ptr->m_ui.typeComboBox->setCurrentIndex(idx);
856 d_ptr->m_ui.gradientWidget->setGradientType(type);
857 d_ptr->m_typeGroup->button(idx)->setChecked(true);
858
859 QGradient::Spread spread = grad.spread();
860 switch (spread) {
861 case QGradient::PadSpread: idx = 0; break;
862 case QGradient::RepeatSpread: idx = 1; break;
863 case QGradient::ReflectSpread: idx = 2; break;
864 default: idx = 0; break;
865 }
866 d_ptr->m_ui.spreadComboBox->setCurrentIndex(idx);
867 d_ptr->m_ui.gradientWidget->setGradientSpread(spread);
868 d_ptr->m_spreadGroup->button(idx)->setChecked(true);
869
870 if (type == QGradient::LinearGradient) {
871 QLinearGradient *gr = (QLinearGradient *)(&grad);
872 d_ptr->setStartLinear(gr->start());
873 d_ptr->setEndLinear(gr->finalStop());
874 d_ptr->m_ui.gradientWidget->setStartLinear(gr->start());
875 d_ptr->m_ui.gradientWidget->setEndLinear(gr->finalStop());
876 } else if (type == QGradient::RadialGradient) {
877 QRadialGradient *gr = (QRadialGradient *)(&grad);
878 d_ptr->setCentralRadial(gr->center());
879 d_ptr->setFocalRadial(gr->focalPoint());
880 d_ptr->setRadiusRadial(gr->radius());
881 d_ptr->m_ui.gradientWidget->setCentralRadial(gr->center());
882 d_ptr->m_ui.gradientWidget->setFocalRadial(gr->focalPoint());
883 d_ptr->m_ui.gradientWidget->setRadiusRadial(gr->radius());
884 } else if (type == QGradient::ConicalGradient) {
885 QConicalGradient *gr = (QConicalGradient *)(&grad);
886 d_ptr->setCentralConical(gr->center());
887 d_ptr->setAngleConical(gr->angle());
888 d_ptr->m_ui.gradientWidget->setCentralConical(gr->center());
889 d_ptr->m_ui.gradientWidget->setAngleConical(gr->angle());
890 }
891
892 d_ptr->m_gradientStopsController->setGradientStops(grad.stops());
893 d_ptr->m_ui.gradientWidget->setGradientStops(grad.stops());
894 d_ptr->updateGradient(false);
895}
896
897QGradient QtGradientEditor::gradient() const
898{
899 return d_ptr->m_gradient;
900}
901
902bool QtGradientEditor::isBackgroundCheckered() const
903{
904 return d_ptr->m_backgroundCheckered;
905}
906
907void QtGradientEditor::setBackgroundCheckered(bool checkered)
908{
909 if (d_ptr->m_backgroundCheckered == checkered)
910 return;
911
912 d_ptr->m_backgroundCheckered = checkered;
913 d_ptr->m_ui.hueColorLine->setBackgroundCheckered(checkered);
914 d_ptr->m_ui.saturationColorLine->setBackgroundCheckered(checkered);
915 d_ptr->m_ui.valueColorLine->setBackgroundCheckered(checkered);
916 d_ptr->m_ui.alphaColorLine->setBackgroundCheckered(checkered);
917 d_ptr->m_ui.gradientWidget->setBackgroundCheckered(checkered);
918 d_ptr->m_ui.gradientStopsWidget->setBackgroundCheckered(checkered);
919 d_ptr->m_ui.colorButton->setBackgroundCheckered(checkered);
920}
921
922bool QtGradientEditor::detailsVisible() const
923{
924 return d_ptr->m_details;
925}
926
927void QtGradientEditor::setDetailsVisible(bool visible)
928{
929 d_ptr->showDetails(visible);
930}
931
932bool QtGradientEditor::isDetailsButtonVisible() const
933{
934 return d_ptr->m_detailsButtonVisible;
935}
936
937void QtGradientEditor::setDetailsButtonVisible(bool visible)
938{
939 if (d_ptr->m_detailsButtonVisible == visible)
940 return;
941
942 d_ptr->m_detailsButtonVisible = visible;
943 d_ptr->m_ui.detailsButton->setVisible(visible);
944}
945
946QColor::Spec QtGradientEditor::spec() const
947{
948 return d_ptr->m_gradientStopsController->spec();
949}
950
951void QtGradientEditor::setSpec(QColor::Spec spec)
952{
953 d_ptr->m_gradientStopsController->setSpec(spec);
954}
955
956QT_END_NAMESPACE
957
958#include "moc_qtgradienteditor.cpp"
Note: See TracBrowser for help on using the repository browser.