|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the tools applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "paletteeditoradvanced.h" |
|
43 #include "colorbutton.h" |
|
44 |
|
45 #include <QCheckBox> |
|
46 #include <QComboBox> |
|
47 #include <QApplication> |
|
48 #include <QPushButton> |
|
49 #include <QPainter> |
|
50 #include <QGroupBox> |
|
51 |
|
52 QT_BEGIN_NAMESPACE |
|
53 |
|
54 PaletteEditorAdvanced::PaletteEditorAdvanced( QWidget * parent, |
|
55 const char * name, bool modal, Qt::WindowFlags f ) |
|
56 : PaletteEditorAdvancedBase( parent, name, modal, f ), selectedPalette(0) |
|
57 { |
|
58 // work around buggy UI file |
|
59 comboEffect->setEnabled(false); |
|
60 buttonEffect->setEnabled(false); |
|
61 onToggleBuildEffects(true); |
|
62 |
|
63 editPalette = QApplication::palette(); |
|
64 setPreviewPalette( editPalette ); |
|
65 } |
|
66 |
|
67 PaletteEditorAdvanced::~PaletteEditorAdvanced() |
|
68 { |
|
69 } |
|
70 |
|
71 void PaletteEditorAdvanced::onToggleBuildInactive( bool v ) |
|
72 { |
|
73 if (selectedPalette == 1) { |
|
74 groupCentral->setDisabled(v); |
|
75 groupEffect->setDisabled(v); |
|
76 } |
|
77 |
|
78 if (v) { |
|
79 buildInactive(); |
|
80 updateColorButtons(); |
|
81 } |
|
82 } |
|
83 |
|
84 void PaletteEditorAdvanced::onToggleBuildDisabled( bool v ) |
|
85 { |
|
86 if (selectedPalette == 2) { |
|
87 groupCentral->setDisabled(v); |
|
88 groupEffect->setDisabled(v); |
|
89 } |
|
90 |
|
91 if (v) { |
|
92 buildDisabled(); |
|
93 updateColorButtons(); |
|
94 } |
|
95 } |
|
96 |
|
97 void PaletteEditorAdvanced::paletteSelected(int p) |
|
98 { |
|
99 selectedPalette = p; |
|
100 |
|
101 if(p == 1) { // inactive |
|
102 groupCentral->setDisabled(checkBuildInactive->isChecked()); |
|
103 groupEffect->setDisabled(checkBuildInactive->isChecked()); |
|
104 } |
|
105 else if (p == 2) { // disabled |
|
106 groupCentral->setDisabled(checkBuildDisabled->isChecked()); |
|
107 groupEffect->setDisabled(checkBuildDisabled->isChecked()); |
|
108 } |
|
109 else { |
|
110 groupCentral->setEnabled(true); |
|
111 groupEffect->setEnabled(true); |
|
112 } |
|
113 updateColorButtons(); |
|
114 } |
|
115 |
|
116 void PaletteEditorAdvanced::onChooseCentralColor() |
|
117 { |
|
118 switch(selectedPalette) { |
|
119 case 0: |
|
120 default: |
|
121 mapToActiveCentralRole( buttonCentral->color() ); |
|
122 break; |
|
123 case 1: |
|
124 mapToInactiveCentralRole( buttonCentral->color() ); |
|
125 break; |
|
126 case 2: |
|
127 mapToDisabledCentralRole( buttonCentral->color() ); |
|
128 break; |
|
129 } |
|
130 updateColorButtons(); |
|
131 } |
|
132 |
|
133 void PaletteEditorAdvanced::onChooseEffectColor() |
|
134 { |
|
135 switch(selectedPalette) { |
|
136 case 0: |
|
137 default: |
|
138 mapToActiveEffectRole( buttonEffect->color() ); |
|
139 break; |
|
140 case 1: |
|
141 mapToInactiveEffectRole( buttonEffect->color() ); |
|
142 break; |
|
143 case 2: |
|
144 mapToDisabledEffectRole( buttonEffect->color() ); |
|
145 break; |
|
146 } |
|
147 updateColorButtons(); |
|
148 } |
|
149 |
|
150 void PaletteEditorAdvanced::onToggleBuildEffects( bool on ) |
|
151 { |
|
152 if (!on) return; |
|
153 buildActiveEffect(); |
|
154 buildInactiveEffect(); |
|
155 buildDisabledEffect(); |
|
156 } |
|
157 |
|
158 QColorGroup::ColorRole PaletteEditorAdvanced::centralFromItem( int item ) |
|
159 { |
|
160 switch( item ) { |
|
161 case 0: |
|
162 return QColorGroup::Window; |
|
163 case 1: |
|
164 return QColorGroup::WindowText; |
|
165 case 2: |
|
166 return QColorGroup::Button; |
|
167 case 3: |
|
168 return QColorGroup::Base; |
|
169 case 4: |
|
170 return QColorGroup::Text; |
|
171 case 5: |
|
172 return QColorGroup::BrightText; |
|
173 case 6: |
|
174 return QColorGroup::ButtonText; |
|
175 case 7: |
|
176 return QColorGroup::Highlight; |
|
177 case 8: |
|
178 return QColorGroup::HighlightedText; |
|
179 default: |
|
180 return QColorGroup::NColorRoles; |
|
181 } |
|
182 } |
|
183 |
|
184 QColorGroup::ColorRole PaletteEditorAdvanced::effectFromItem( int item ) |
|
185 { |
|
186 switch( item ) { |
|
187 case 0: |
|
188 return QColorGroup::Light; |
|
189 case 1: |
|
190 return QColorGroup::Midlight; |
|
191 case 2: |
|
192 return QColorGroup::Mid; |
|
193 case 3: |
|
194 return QColorGroup::Dark; |
|
195 case 4: |
|
196 return QColorGroup::Shadow; |
|
197 default: |
|
198 return QColorGroup::NColorRoles; |
|
199 } |
|
200 } |
|
201 |
|
202 void PaletteEditorAdvanced::onCentral( int item ) |
|
203 { |
|
204 QColor c; |
|
205 |
|
206 switch(selectedPalette) { |
|
207 case 0: |
|
208 default: |
|
209 c = editPalette.active().color( centralFromItem(item) ); |
|
210 break; |
|
211 case 1: |
|
212 c = editPalette.inactive().color( centralFromItem(item) ); |
|
213 break; |
|
214 case 2: |
|
215 c = editPalette.disabled().color( centralFromItem(item) ); |
|
216 break; |
|
217 } |
|
218 |
|
219 buttonCentral->setColor(c); |
|
220 } |
|
221 |
|
222 void PaletteEditorAdvanced::onEffect( int item ) |
|
223 { |
|
224 QColor c; |
|
225 switch(selectedPalette) { |
|
226 case 0: |
|
227 default: |
|
228 c = editPalette.active().color( effectFromItem(item) ); |
|
229 break; |
|
230 case 1: |
|
231 editPalette.inactive().color( effectFromItem(item) ); |
|
232 break; |
|
233 case 2: |
|
234 editPalette.disabled().color( effectFromItem(item) ); |
|
235 break; |
|
236 } |
|
237 buttonEffect->setColor(c); |
|
238 } |
|
239 |
|
240 void PaletteEditorAdvanced::mapToActiveCentralRole( const QColor& c ) |
|
241 { |
|
242 QColorGroup cg = editPalette.active(); |
|
243 cg.setColor( centralFromItem(comboCentral->currentItem()), c ); |
|
244 editPalette.setActive( cg ); |
|
245 |
|
246 buildActiveEffect(); |
|
247 if(checkBuildInactive->isChecked()) |
|
248 buildInactive(); |
|
249 if(checkBuildDisabled->isChecked()) |
|
250 buildDisabled(); |
|
251 |
|
252 setPreviewPalette( editPalette ); |
|
253 } |
|
254 |
|
255 void PaletteEditorAdvanced::mapToActiveEffectRole( const QColor& c ) |
|
256 { |
|
257 QColorGroup cg = editPalette.active(); |
|
258 cg.setColor( effectFromItem(comboEffect->currentItem()), c ); |
|
259 editPalette.setActive( cg ); |
|
260 |
|
261 if(checkBuildInactive->isChecked()) |
|
262 buildInactive(); |
|
263 if(checkBuildDisabled->isChecked()) |
|
264 buildDisabled(); |
|
265 |
|
266 setPreviewPalette( editPalette ); |
|
267 } |
|
268 |
|
269 void PaletteEditorAdvanced::mapToActivePixmapRole( const QPixmap& pm ) |
|
270 { |
|
271 QColorGroup::ColorRole role = centralFromItem(comboCentral->currentItem()); |
|
272 QColorGroup cg = editPalette.active(); |
|
273 if ( !pm.isNull() ) |
|
274 cg.setBrush( role, QBrush( cg.color( role ), pm ) ); |
|
275 else |
|
276 cg.setBrush( role, QBrush( cg.color( role ) ) ); |
|
277 editPalette.setActive( cg ); |
|
278 |
|
279 |
|
280 buildActiveEffect(); |
|
281 if(checkBuildInactive->isChecked()) |
|
282 buildInactive(); |
|
283 if(checkBuildDisabled->isChecked()) |
|
284 buildDisabled(); |
|
285 |
|
286 setPreviewPalette( editPalette ); |
|
287 } |
|
288 |
|
289 void PaletteEditorAdvanced::mapToInactiveCentralRole( const QColor& c ) |
|
290 { |
|
291 QColorGroup cg = editPalette.inactive(); |
|
292 cg.setColor( centralFromItem(comboCentral->currentItem()), c ); |
|
293 editPalette.setInactive( cg ); |
|
294 |
|
295 buildInactiveEffect(); |
|
296 |
|
297 setPreviewPalette( editPalette ); |
|
298 } |
|
299 |
|
300 void PaletteEditorAdvanced::mapToInactiveEffectRole( const QColor& c ) |
|
301 { |
|
302 QColorGroup cg = editPalette.inactive(); |
|
303 cg.setColor( effectFromItem(comboEffect->currentItem()), c ); |
|
304 editPalette.setInactive( cg ); |
|
305 |
|
306 setPreviewPalette( editPalette ); |
|
307 } |
|
308 |
|
309 void PaletteEditorAdvanced::mapToInactivePixmapRole( const QPixmap& pm ) |
|
310 { |
|
311 QColorGroup::ColorRole role = centralFromItem(comboCentral->currentItem()); |
|
312 QColorGroup cg = editPalette.inactive(); |
|
313 if ( !pm.isNull() ) |
|
314 cg.setBrush( role, QBrush( cg.color( role ), pm ) ); |
|
315 else |
|
316 cg.setBrush( role, QBrush( cg.color( role ) ) ); |
|
317 editPalette.setInactive( cg ); |
|
318 |
|
319 setPreviewPalette( editPalette ); |
|
320 } |
|
321 |
|
322 void PaletteEditorAdvanced::mapToDisabledCentralRole( const QColor& c ) |
|
323 { |
|
324 QColorGroup cg = editPalette.disabled(); |
|
325 cg.setColor( centralFromItem(comboCentral->currentItem()), c ); |
|
326 editPalette.setDisabled( cg ); |
|
327 |
|
328 buildDisabledEffect(); |
|
329 |
|
330 setPreviewPalette( editPalette ); |
|
331 } |
|
332 |
|
333 void PaletteEditorAdvanced::mapToDisabledEffectRole( const QColor& c ) |
|
334 { |
|
335 QColorGroup cg = editPalette.disabled(); |
|
336 cg.setColor( effectFromItem(comboEffect->currentItem()), c ); |
|
337 editPalette.setDisabled( cg ); |
|
338 |
|
339 setPreviewPalette( editPalette ); |
|
340 } |
|
341 |
|
342 void PaletteEditorAdvanced::mapToDisabledPixmapRole( const QPixmap& pm ) |
|
343 { |
|
344 QColorGroup::ColorRole role = centralFromItem(comboCentral->currentItem()); |
|
345 QColorGroup cg = editPalette.disabled(); |
|
346 if ( !pm.isNull() ) |
|
347 cg.setBrush( role, QBrush( cg.color( role ), pm ) ); |
|
348 else |
|
349 cg.setBrush( role, QBrush( cg.color( role ) ) ); |
|
350 |
|
351 editPalette.setDisabled( cg ); |
|
352 |
|
353 setPreviewPalette( editPalette ); |
|
354 } |
|
355 |
|
356 void PaletteEditorAdvanced::buildActiveEffect() |
|
357 { |
|
358 QColorGroup cg = editPalette.active(); |
|
359 QColor btn = cg.color( QColorGroup::Button ); |
|
360 |
|
361 QPalette temp( btn, btn ); |
|
362 |
|
363 for (int i = 0; i<5; i++) |
|
364 cg.setColor( effectFromItem(i), temp.active().color( effectFromItem(i) ) ); |
|
365 |
|
366 editPalette.setActive( cg ); |
|
367 setPreviewPalette( editPalette ); |
|
368 |
|
369 updateColorButtons(); |
|
370 } |
|
371 |
|
372 void PaletteEditorAdvanced::buildInactive() |
|
373 { |
|
374 editPalette.setInactive( editPalette.active() ); |
|
375 if ( checkBuildEffect->isChecked() ) |
|
376 buildInactiveEffect(); |
|
377 else { |
|
378 setPreviewPalette( editPalette ); |
|
379 updateColorButtons(); |
|
380 } |
|
381 |
|
382 } |
|
383 |
|
384 void PaletteEditorAdvanced::buildInactiveEffect() |
|
385 { |
|
386 QColorGroup cg = editPalette.inactive(); |
|
387 |
|
388 QColor light, midlight, mid, dark, shadow; |
|
389 QColor btn = cg.color( QColorGroup::Button ); |
|
390 |
|
391 light = btn.light(150); |
|
392 midlight = btn.light(115); |
|
393 mid = btn.dark(150); |
|
394 dark = btn.dark(); |
|
395 shadow = Qt::black; |
|
396 |
|
397 cg.setColor( QColorGroup::Light, light ); |
|
398 cg.setColor( QColorGroup::Midlight, midlight ); |
|
399 cg.setColor( QColorGroup::Mid, mid ); |
|
400 cg.setColor( QColorGroup::Dark, dark ); |
|
401 cg.setColor( QColorGroup::Shadow, shadow ); |
|
402 |
|
403 editPalette.setInactive( cg ); |
|
404 setPreviewPalette( editPalette ); |
|
405 updateColorButtons(); |
|
406 } |
|
407 |
|
408 void PaletteEditorAdvanced::buildDisabled() |
|
409 { |
|
410 QColorGroup cg = editPalette.active(); |
|
411 cg.setColor( QColorGroup::ButtonText, Qt::darkGray ); |
|
412 cg.setColor( QColorGroup::WindowText, Qt::darkGray ); |
|
413 cg.setColor( QColorGroup::Text, Qt::darkGray ); |
|
414 cg.setColor( QColorGroup::HighlightedText, Qt::darkGray ); |
|
415 editPalette.setDisabled( cg ); |
|
416 |
|
417 if ( checkBuildEffect->isChecked() ) |
|
418 buildDisabledEffect(); |
|
419 else { |
|
420 setPreviewPalette( editPalette ); |
|
421 updateColorButtons(); |
|
422 } |
|
423 } |
|
424 |
|
425 void PaletteEditorAdvanced::buildDisabledEffect() |
|
426 { |
|
427 QColorGroup cg = editPalette.disabled(); |
|
428 |
|
429 QColor light, midlight, mid, dark, shadow; |
|
430 QColor btn = cg.color( QColorGroup::Button ); |
|
431 |
|
432 light = btn.light(150); |
|
433 midlight = btn.light(115); |
|
434 mid = btn.dark(150); |
|
435 dark = btn.dark(); |
|
436 shadow = Qt::black; |
|
437 |
|
438 cg.setColor( QColorGroup::Light, light ); |
|
439 cg.setColor( QColorGroup::Midlight, midlight ); |
|
440 cg.setColor( QColorGroup::Mid, mid ); |
|
441 cg.setColor( QColorGroup::Dark, dark ); |
|
442 cg.setColor( QColorGroup::Shadow, shadow ); |
|
443 |
|
444 editPalette.setDisabled( cg ); |
|
445 setPreviewPalette( editPalette ); |
|
446 updateColorButtons(); |
|
447 } |
|
448 |
|
449 void PaletteEditorAdvanced::setPreviewPalette( const QPalette& pal ) |
|
450 { |
|
451 QColorGroup cg; |
|
452 |
|
453 switch (selectedPalette) { |
|
454 case 0: |
|
455 default: |
|
456 cg = pal.active(); |
|
457 break; |
|
458 case 1: |
|
459 cg = pal.inactive(); |
|
460 break; |
|
461 case 2: |
|
462 cg = pal.disabled(); |
|
463 break; |
|
464 } |
|
465 previewPalette.setActive( cg ); |
|
466 previewPalette.setInactive( cg ); |
|
467 previewPalette.setDisabled( cg ); |
|
468 } |
|
469 |
|
470 void PaletteEditorAdvanced::updateColorButtons() |
|
471 { |
|
472 QColor central, effect; |
|
473 switch (selectedPalette) { |
|
474 case 0: |
|
475 default: |
|
476 central = editPalette.active().color( centralFromItem( comboCentral->currentItem() ) ); |
|
477 effect = editPalette.active().color( effectFromItem( comboEffect->currentItem() ) ); |
|
478 break; |
|
479 case 1: |
|
480 central = editPalette.inactive().color( centralFromItem( comboCentral->currentItem() ) ); |
|
481 effect = editPalette.inactive().color( effectFromItem( comboEffect->currentItem() ) ); |
|
482 break; |
|
483 case 2: |
|
484 central = editPalette.disabled().color( centralFromItem( comboCentral->currentItem() ) ); |
|
485 effect = editPalette.disabled().color( effectFromItem( comboEffect->currentItem() ) ); |
|
486 break; |
|
487 } |
|
488 |
|
489 buttonCentral->setColor(central); |
|
490 buttonEffect->setColor(effect); |
|
491 } |
|
492 |
|
493 void PaletteEditorAdvanced::setPal( const QPalette& pal ) |
|
494 { |
|
495 editPalette = pal; |
|
496 setPreviewPalette( pal ); |
|
497 updateColorButtons(); |
|
498 } |
|
499 |
|
500 QPalette PaletteEditorAdvanced::pal() const |
|
501 { |
|
502 return editPalette; |
|
503 } |
|
504 |
|
505 void PaletteEditorAdvanced::setupBackgroundMode( Qt::BackgroundMode mode ) |
|
506 { |
|
507 int initRole = 0; |
|
508 |
|
509 switch( mode ) { |
|
510 case Qt::PaletteBackground: |
|
511 initRole = 0; |
|
512 break; |
|
513 case Qt::PaletteForeground: |
|
514 initRole = 1; |
|
515 break; |
|
516 case Qt::PaletteButton: |
|
517 initRole = 2; |
|
518 break; |
|
519 case Qt::PaletteBase: |
|
520 initRole = 3; |
|
521 break; |
|
522 case Qt::PaletteText: |
|
523 initRole = 4; |
|
524 break; |
|
525 case Qt::PaletteBrightText: |
|
526 initRole = 5; |
|
527 break; |
|
528 case Qt::PaletteButtonText: |
|
529 initRole = 6; |
|
530 break; |
|
531 case Qt::PaletteHighlight: |
|
532 initRole = 7; |
|
533 break; |
|
534 case Qt::PaletteHighlightedText: |
|
535 initRole = 8; |
|
536 break; |
|
537 case Qt::PaletteLight: |
|
538 initRole = 9; |
|
539 break; |
|
540 case Qt::PaletteMidlight: |
|
541 initRole = 10; |
|
542 break; |
|
543 case Qt::PaletteDark: |
|
544 initRole = 11; |
|
545 break; |
|
546 case Qt::PaletteMid: |
|
547 initRole = 12; |
|
548 break; |
|
549 case Qt::PaletteShadow: |
|
550 initRole = 13; |
|
551 break; |
|
552 default: |
|
553 initRole = -1; |
|
554 break; |
|
555 } |
|
556 |
|
557 if ( initRole <= -1 ) return; |
|
558 |
|
559 if (initRole > 8 ) { |
|
560 comboEffect->setCurrentItem( initRole - 9 ); |
|
561 } |
|
562 else { |
|
563 comboCentral->setCurrentItem( initRole ); |
|
564 } |
|
565 } |
|
566 |
|
567 QPalette PaletteEditorAdvanced::getPalette( bool *ok, const QPalette &init, |
|
568 Qt::BackgroundMode mode, QWidget* parent, |
|
569 const char* name ) |
|
570 { |
|
571 PaletteEditorAdvanced* dlg = new PaletteEditorAdvanced( parent, name, true ); |
|
572 dlg->setupBackgroundMode( mode ); |
|
573 |
|
574 if ( init != QPalette() ) |
|
575 dlg->setPal( init ); |
|
576 int resultCode = dlg->exec(); |
|
577 |
|
578 QPalette result = init; |
|
579 if ( resultCode == QDialog::Accepted ) { |
|
580 if ( ok ) |
|
581 *ok = true; |
|
582 result = dlg->pal(); |
|
583 } else { |
|
584 if ( ok ) |
|
585 *ok = false; |
|
586 } |
|
587 delete dlg; |
|
588 return result; |
|
589 } |
|
590 |
|
591 QT_END_NAMESPACE |