~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/qtconfig/paletteeditoradvanced.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

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