summaryrefslogtreecommitdiff
path: root/src/qtconfig/paletteeditoradvanced.cpp
blob: 168890eb28e4b84f198475b53a1d6f5ad6659190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "ui_paletteeditoradvanced.h"
#include "paletteeditoradvanced.h"
#include "colorbutton.h"

QT_BEGIN_NAMESPACE

PaletteEditorAdvanced::PaletteEditorAdvanced(QWidget *parent)
    : QDialog(parent), ui(new Ui::PaletteEditorAdvanced), selectedPalette(0)
{
    ui->setupUi(this);

    // create a ColorButton's
    buttonCentral = new ColorButton(ui->groupCentral);
    buttonCentral->setToolTip(tr("Choose a color"));
    buttonCentral->setWhatsThis(tr("Choose a color for the selected central color role."));
    ui->layoutCentral->addWidget(buttonCentral);
    ui->labelCentral->setBuddy(buttonCentral);

    buttonEffect = new ColorButton(ui->groupEffect);
    buttonEffect->setToolTip(tr("Choose a color"));
    buttonEffect->setWhatsThis(tr("Choose a color for the selected effect color role."));
    buttonEffect->setEnabled(false);
    ui->layoutEffect->addWidget(buttonEffect);
    ui->labelEffect->setBuddy(buttonEffect);

    // signals and slots connections
    connect(ui->paletteCombo, SIGNAL(activated(int)), SLOT(paletteSelected(int)));
    connect(ui->comboCentral, SIGNAL(activated(int)), SLOT(onCentral(int)));
    connect(buttonCentral, SIGNAL(clicked()), SLOT(onChooseCentralColor()));
    connect(buttonEffect, SIGNAL(clicked()), SLOT(onChooseEffectColor()));
    connect(ui->comboEffect, SIGNAL(activated(int)), SLOT(onEffect(int)));
    connect(ui->checkBuildEffect, SIGNAL(toggled(bool)), SLOT(onToggleBuildEffects(bool)));
    connect(ui->checkBuildEffect, SIGNAL(toggled(bool)), buttonEffect, SLOT(setDisabled(bool)));
    connect(ui->checkBuildInactive, SIGNAL(toggled(bool)), SLOT(onToggleBuildInactive(bool)));
    connect(ui->checkBuildDisabled, SIGNAL(toggled(bool)), SLOT(onToggleBuildDisabled(bool)));

    onToggleBuildEffects(true);

    editPalette = QApplication::palette();
}

PaletteEditorAdvanced::~PaletteEditorAdvanced()
{
    delete ui;
}

void PaletteEditorAdvanced::onToggleBuildInactive(bool v)
{
    if (selectedPalette == 1) {
        ui->groupCentral->setDisabled(v);
        ui->groupEffect->setDisabled(v);
    }

    if (v) {
        build(QPalette::Inactive);
        updateColorButtons();
    }
}

void PaletteEditorAdvanced::onToggleBuildDisabled(bool v)
{
    if (selectedPalette == 2) {
        ui->groupCentral->setDisabled(v);
        ui->groupEffect->setDisabled(v);
    }

    if (v) {
        build(QPalette::Disabled);
        updateColorButtons();
    }
}

void PaletteEditorAdvanced::paletteSelected(int p)
{
    selectedPalette = p;

    if(p == 1) { // inactive
        ui->groupCentral->setDisabled(ui->checkBuildInactive->isChecked());
        ui->groupEffect->setDisabled(ui->checkBuildInactive->isChecked());
    } else if (p == 2) { // disabled
        ui->groupCentral->setDisabled(ui->checkBuildDisabled->isChecked());
        ui->groupEffect->setDisabled(ui->checkBuildDisabled->isChecked());
    } else {
        ui->groupCentral->setEnabled(true);
        ui->groupEffect->setEnabled(true);
    }
    updateColorButtons();
}

void PaletteEditorAdvanced::onChooseCentralColor()
{
    QPalette::ColorGroup group = groupFromIndex(selectedPalette);
    editPalette.setColor(group, centralFromIndex(ui->comboCentral->currentIndex()),
                         buttonCentral->color());

    buildEffect(group);
    if (group == QPalette::Active) {
        if(ui->checkBuildInactive->isChecked())
            build(QPalette::Inactive);
        if(ui->checkBuildDisabled->isChecked())
            build(QPalette::Disabled);
    }

    updateColorButtons();
}

void PaletteEditorAdvanced::onChooseEffectColor()
{
    QPalette::ColorGroup group = groupFromIndex(selectedPalette);
    editPalette.setColor(group, effectFromIndex(ui->comboEffect->currentIndex()),
                         buttonEffect->color());

    if (group == QPalette::Active) {
        if(ui->checkBuildInactive->isChecked())
            build(QPalette::Inactive);
        if(ui->checkBuildDisabled->isChecked())
            build(QPalette::Disabled);
    }

    updateColorButtons();
}

void PaletteEditorAdvanced::onToggleBuildEffects(bool on)
{
    if (on) {
        for (int i = 0; i < QPalette::NColorGroups; i++)
            buildEffect(QPalette::ColorGroup(i));
    }
}

QPalette::ColorGroup PaletteEditorAdvanced::groupFromIndex(int item)
{
    switch (item) {
    case 0:
    default:
        return QPalette::Active;
    case 1:
        return QPalette::Inactive;
    case 2:
        return QPalette::Disabled;
    }
}

QPalette::ColorRole PaletteEditorAdvanced::centralFromIndex(int item)
{
    switch (item) {
    case 0:
        return QPalette::Window;
    case 1:
        return QPalette::WindowText;
    case 2:
        return QPalette::Base;
    case 3:
        return QPalette::AlternateBase;
    case 4:
        return QPalette::ToolTipBase;
    case 5:
        return QPalette::ToolTipText;
    case 6:
        return QPalette::Text;
    case 7:
        return QPalette::Button;
    case 8:
        return QPalette::ButtonText;
    case 9:
        return QPalette::BrightText;
    case 10:
        return QPalette::Highlight;
    case 11:
        return QPalette::HighlightedText;
    case 12:
        return QPalette::Link;
    case 13:
        return QPalette::LinkVisited;
    default:
        return QPalette::NoRole;
    }
}

QPalette::ColorRole PaletteEditorAdvanced::effectFromIndex(int item)
{
    switch (item) {
    case 0:
        return QPalette::Light;
    case 1:
        return QPalette::Midlight;
    case 2:
        return QPalette::Mid;
    case 3:
        return QPalette::Dark;
    case 4:
        return QPalette::Shadow;
    default:
        return QPalette::NoRole;
    }
}

void PaletteEditorAdvanced::onCentral(int item)
{
    QColor c = editPalette.color(groupFromIndex(selectedPalette), centralFromIndex(item));
    buttonCentral->setColor(c);
}

void PaletteEditorAdvanced::onEffect(int item)
{
    QColor c = editPalette.color(groupFromIndex(selectedPalette), effectFromIndex(item));
    buttonEffect->setColor(c);
}

QPalette PaletteEditorAdvanced::buildEffect(QPalette::ColorGroup colorGroup,
                                            const QPalette &basePalette)
{
    QPalette result(basePalette);

    if (colorGroup == QPalette::Active) {
        QPalette calculatedPalette(basePalette.color(colorGroup, QPalette::Button),
                                   basePalette.color(colorGroup, QPalette::Window));

        for (int i = 0; i < 5; i++) {
            QPalette::ColorRole effectRole = effectFromIndex(i);
            result.setColor(colorGroup, effectRole,
                            calculatedPalette.color(colorGroup, effectRole));
        }
    } else {
        QColor btn = basePalette.color(colorGroup, QPalette::Button);

        result.setColor(colorGroup, QPalette::Light, btn.lighter());
        result.setColor(colorGroup, QPalette::Midlight, btn.lighter(115));
        result.setColor(colorGroup, QPalette::Mid, btn.darker(150));
        result.setColor(colorGroup, QPalette::Dark, btn.darker());
        result.setColor(colorGroup, QPalette::Shadow, Qt::black);
    }

    return result;
}

void PaletteEditorAdvanced::buildEffect(QPalette::ColorGroup colorGroup)
{
    editPalette = buildEffect(colorGroup, editPalette);
    updateColorButtons();
}

void PaletteEditorAdvanced::build(QPalette::ColorGroup colorGroup)
{
    if (colorGroup != QPalette::Active) {
        for (int i = 0; i < QPalette::NColorRoles; i++)
            editPalette.setColor(colorGroup, QPalette::ColorRole(i),
                                 editPalette.color(QPalette::Active, QPalette::ColorRole(i)));

        if (colorGroup == QPalette::Disabled) {
            editPalette.setColor(colorGroup, QPalette::ButtonText, Qt::darkGray);
            editPalette.setColor(colorGroup, QPalette::WindowText, Qt::darkGray);
            editPalette.setColor(colorGroup, QPalette::Text, Qt::darkGray);
            editPalette.setColor(colorGroup, QPalette::HighlightedText, Qt::darkGray);
        }

        if (ui->checkBuildEffect->isChecked())
            buildEffect(colorGroup);
        else
            updateColorButtons();
    }
}

void PaletteEditorAdvanced::updateColorButtons()
{
    QPalette::ColorGroup colorGroup = groupFromIndex(selectedPalette);
    buttonCentral->setColor(editPalette.color(colorGroup,
                                              centralFromIndex(ui->comboCentral->currentIndex())));
    buttonEffect->setColor(editPalette.color(colorGroup,
                                             effectFromIndex(ui->comboEffect->currentIndex())));
}

void PaletteEditorAdvanced::setPal(const QPalette &pal)
{
    editPalette = pal;
    updateColorButtons();
}

QPalette PaletteEditorAdvanced::pal() const
{
    return editPalette;
}

void PaletteEditorAdvanced::setupBackgroundRole(QPalette::ColorRole role)
{
    int initRole = 0;

    switch (role) {
    case QPalette::Window:
        initRole = 0;
        break;
    case QPalette::WindowText:
        initRole = 1;
        break;
    case QPalette::Base:
        initRole = 2;
        break;
    case QPalette::AlternateBase:
        initRole = 3;
        break;
    case QPalette::ToolTipBase:
        initRole = 4;
        break;
    case QPalette::ToolTipText:
        initRole = 5;
        break;
    case QPalette::Text:
        initRole = 6;
        break;
    case QPalette::Button:
        initRole = 7;
        break;
    case QPalette::ButtonText:
        initRole = 8;
        break;
    case QPalette::BrightText:
        initRole = 9;
        break;
    case QPalette::Highlight:
        initRole = 10;
        break;
    case QPalette::HighlightedText:
        initRole = 11;
        break;
    case QPalette::Link:
        initRole = 12;
        break;
    case QPalette::LinkVisited:
        initRole = 13;
        break;
    default:
        initRole = -1;
        break;
    }

    if (initRole != -1)
        ui->comboCentral->setCurrentIndex(initRole);
}

QPalette PaletteEditorAdvanced::getPalette(bool *ok, const QPalette &init,
                                           QPalette::ColorRole backgroundRole, QWidget *parent)
{
    PaletteEditorAdvanced *dlg = new PaletteEditorAdvanced(parent);
    dlg->setupBackgroundRole(backgroundRole);

    if (init != QPalette())
        dlg->setPal(init);
    int resultCode = dlg->exec();

    QPalette result = init;
    if (resultCode == QDialog::Accepted)
        result = dlg->pal();

    if (ok)
        *ok = resultCode;

    delete dlg;
    return result;
}

QT_END_NAMESPACE