~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/wallpapers/color/color.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2008 by Petri Damsten <damu@iki.fi>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License as
 
6
 *   published by the Free Software Foundation; either version 2, or
 
7
 *   (at your option) any later version.
 
8
 *
 
9
 *   This program is distributed in the hope that it will be useful,
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *   GNU General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "color.h"
 
21
 
 
22
#include <QPainter>
 
23
#include <QLinearGradient>
 
24
#include <QRadialGradient>
 
25
#include <KDebug>
 
26
 
 
27
enum BackgroundMode {
 
28
    SOLID,
 
29
    HORIZONTAL,
 
30
    VERTICAL,
 
31
    RECTANGULAR,
 
32
    RADIAL,
 
33
    TOP_LEFT_DIAGONAL,
 
34
    TOP_RIGHT_DIAGONAL
 
35
};
 
36
 
 
37
Color::Color(QObject *parent, const QVariantList &args)
 
38
    : Plasma::Wallpaper(parent, args)
 
39
{
 
40
}
 
41
 
 
42
void Color::init(const KConfigGroup &config)
 
43
{
 
44
    m_color1 = config.readEntry("color1", QColor(Qt::white));
 
45
    m_color2 = config.readEntry("color2", QColor(Qt::black));
 
46
    m_backgroundMode = config.readEntry("backgroundMode", (int)SOLID);
 
47
    emit update(boundingRect());
 
48
}
 
49
 
 
50
void Color::paint(QPainter *painter, const QRectF& exposedRect)
 
51
{
 
52
    switch (m_backgroundMode) {
 
53
    case SOLID: {
 
54
            painter->fillRect(exposedRect, m_color1);
 
55
            break;
 
56
        }
 
57
 
 
58
    case HORIZONTAL: {
 
59
            QLinearGradient gradient = QLinearGradient(boundingRect().topLeft(),
 
60
                                                       boundingRect().topRight());
 
61
            gradient.setColorAt(0, m_color1);
 
62
            gradient.setColorAt(1, m_color2);
 
63
            painter->fillRect(exposedRect, gradient);
 
64
            break;
 
65
        }
 
66
 
 
67
    case VERTICAL: {
 
68
            QLinearGradient gradient = QLinearGradient(boundingRect().topLeft(),
 
69
                                                       boundingRect().bottomLeft());
 
70
            gradient.setColorAt(0, m_color1);
 
71
            gradient.setColorAt(1, m_color2);
 
72
            painter->fillRect(exposedRect, gradient);
 
73
            break;
 
74
        }
 
75
 
 
76
    case RECTANGULAR: {
 
77
            // First draw a horizontal gradient covering the whole view/screen
 
78
            QLinearGradient horizontalGradient = QLinearGradient(boundingRect().topLeft(),
 
79
                                                                 boundingRect().topRight());
 
80
            horizontalGradient.setColorAt(0, m_color2);
 
81
            horizontalGradient.setColorAt(0.5, m_color1);
 
82
            horizontalGradient.setColorAt(1, m_color2);
 
83
 
 
84
            painter->fillRect(exposedRect, horizontalGradient);
 
85
 
 
86
            // Then draw two triangles with vertical gradient
 
87
            QLinearGradient verticalGradient = QLinearGradient(boundingRect().topLeft(),
 
88
                                                               boundingRect().bottomLeft());
 
89
            verticalGradient.setColorAt(0, m_color2);
 
90
            verticalGradient.setColorAt(0.5, m_color1);
 
91
            verticalGradient.setColorAt(1, m_color2);
 
92
            painter->setBrush(verticalGradient);
 
93
            painter->setPen(Qt::NoPen);
 
94
 
 
95
            QPolygon triangle = QPolygon(3);
 
96
 
 
97
            // Draw a triangle which starts from the top edge to the center
 
98
            triangle.append(boundingRect().topLeft().toPoint());
 
99
            triangle.append(boundingRect().topRight().toPoint());
 
100
            triangle.append(boundingRect().center().toPoint());
 
101
            painter->drawPolygon(triangle);
 
102
 
 
103
            triangle.clear();
 
104
 
 
105
            // Draw a triangle which starts from the bottom edge to the center
 
106
            triangle.append(boundingRect().bottomLeft().toPoint());
 
107
            triangle.append(boundingRect().bottomRight().toPoint());
 
108
            triangle.append(boundingRect().center().toPoint());
 
109
            painter->drawPolygon(triangle);
 
110
 
 
111
            break;
 
112
        }
 
113
 
 
114
    case RADIAL: {
 
115
            // The diameter of the gradient will be the max screen dimension
 
116
            int maxDimension = qMax(boundingRect().height(), boundingRect().width());
 
117
 
 
118
            QRadialGradient gradient = QRadialGradient(boundingRect().center(),
 
119
                                                       maxDimension / 2,
 
120
                                                       boundingRect().center());
 
121
            gradient.setColorAt(0, m_color1);
 
122
            gradient.setColorAt(1, m_color2);
 
123
            painter->fillRect(exposedRect, gradient);
 
124
            break;
 
125
        }
 
126
 
 
127
    case TOP_LEFT_DIAGONAL: {
 
128
            QLinearGradient gradient = QLinearGradient(boundingRect().topLeft(),
 
129
                                                       boundingRect().bottomRight());
 
130
            gradient.setColorAt(0, m_color1);
 
131
            gradient.setColorAt(1, m_color2);
 
132
            painter->fillRect(exposedRect, gradient);
 
133
            break;
 
134
        }
 
135
 
 
136
    case TOP_RIGHT_DIAGONAL: {
 
137
            QLinearGradient gradient = QLinearGradient(boundingRect().topRight(),
 
138
                                                       boundingRect().bottomLeft());
 
139
            gradient.setColorAt(0, m_color1);
 
140
            gradient.setColorAt(1, m_color2);
 
141
            painter->fillRect(exposedRect, gradient);
 
142
            break;
 
143
        }
 
144
 
 
145
    }
 
146
}
 
147
 
 
148
QWidget* Color::createConfigurationInterface(QWidget* parent)
 
149
{
 
150
    QWidget *widget = new QWidget(parent);
 
151
    m_ui.setupUi(widget);
 
152
 
 
153
    m_ui.m_color1->setColor(m_color1);
 
154
    m_ui.m_color2->setColor(m_color2);
 
155
    m_ui.m_backgroundMode->setCurrentIndex(m_backgroundMode);
 
156
 
 
157
    if (m_backgroundMode == SOLID) {
 
158
        m_ui.m_color2->setEnabled(false);
 
159
    } else {
 
160
        m_ui.m_color2->setEnabled(true);
 
161
    }
 
162
 
 
163
    connect(m_ui.m_color1, SIGNAL(changed(const QColor&)), this, SLOT(settingsModified()));
 
164
    connect(m_ui.m_color2, SIGNAL(changed(const QColor&)), this, SLOT(settingsModified()));
 
165
    connect(m_ui.m_backgroundMode, SIGNAL(currentIndexChanged(int)), this, SLOT(settingsModified()));
 
166
 
 
167
    connect(this, SIGNAL(settingsChanged(bool)), parent, SLOT(settingsChanged(bool)));
 
168
 
 
169
    return widget;
 
170
}
 
171
 
 
172
void Color::save(KConfigGroup &config)
 
173
{
 
174
    config.writeEntry("color1", m_color1);
 
175
    config.writeEntry("color2", m_color2);
 
176
    config.writeEntry("backgroundMode", m_backgroundMode);
 
177
}
 
178
 
 
179
void Color::settingsModified()
 
180
{
 
181
    m_color1 = m_ui.m_color1->color();
 
182
    m_color2 = m_ui.m_color2->color();
 
183
    m_backgroundMode = m_ui.m_backgroundMode->currentIndex();
 
184
 
 
185
    if (m_backgroundMode == SOLID) {
 
186
        m_ui.m_color2->setEnabled(false);
 
187
    } else {
 
188
        m_ui.m_color2->setEnabled(true);
 
189
    }
 
190
 
 
191
    emit settingsChanged(true);
 
192
    emit update(boundingRect());
 
193
}
 
194
 
 
195
#include "color.moc"