Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / util / qdeclarativesystempalette.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 QtDeclarative module 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 "private/qdeclarativesystempalette_p.h"
43
44 #include <QApplication>
45
46 #include <private/qobject_p.h>
47
48 QT_BEGIN_NAMESPACE
49
50 class QDeclarativeSystemPalettePrivate : public QObjectPrivate
51 {
52 public:
53     QPalette palette;
54     QPalette::ColorGroup group;
55 };
56
57
58
59 /*!
60     \qmlclass SystemPalette QDeclarativeSystemPalette
61     \ingroup qml-utility-elements
62     \since 4.7
63     \brief The SystemPalette element provides access to the Qt palettes.
64
65     The SystemPalette element provides access to the Qt application
66     palettes. This provides information about the standard colors used 
67     for application windows, buttons and other features. These colors
68     are grouped into three \e {color groups}: \c Active, \c Inactive,
69     and \c Disabled.  See the QPalette documentation for details about
70     color groups and the properties provided by SystemPalette.
71
72     This can be used to color items in a way that provides a more
73     native look and feel.
74
75     The following example creates a palette from the \c Active color
76     group and uses this to color the window and text items
77     appropriately:
78
79     \snippet doc/src/snippets/declarative/systempalette.qml 0
80
81     \sa QPalette
82 */
83 QDeclarativeSystemPalette::QDeclarativeSystemPalette(QObject *parent)
84     : QObject(*(new QDeclarativeSystemPalettePrivate), parent)
85 {
86     Q_D(QDeclarativeSystemPalette);
87     d->palette = QApplication::palette();
88     d->group = QPalette::Active;
89     qApp->installEventFilter(this);
90 }
91
92 QDeclarativeSystemPalette::~QDeclarativeSystemPalette()
93 {
94 }
95
96 /*!
97     \qmlproperty color SystemPalette::window
98     The window (general background) color of the current color group.
99
100     \sa QPalette::ColorRole
101 */
102 QColor QDeclarativeSystemPalette::window() const
103 {
104     Q_D(const QDeclarativeSystemPalette);
105     return d->palette.color(d->group, QPalette::Window);
106 }
107
108 /*!
109     \qmlproperty color SystemPalette::windowText
110     The window text (general foreground) color of the current color group.
111
112     \sa QPalette::ColorRole
113 */
114 QColor QDeclarativeSystemPalette::windowText() const
115 {
116     Q_D(const QDeclarativeSystemPalette);
117     return d->palette.color(d->group, QPalette::WindowText);
118 }
119
120 /*!
121     \qmlproperty color SystemPalette::base
122     The base color of the current color group.
123
124     \sa QPalette::ColorRole
125 */
126 QColor QDeclarativeSystemPalette::base() const
127 {
128     Q_D(const QDeclarativeSystemPalette);
129     return d->palette.color(d->group, QPalette::Base);
130 }
131
132 /*!
133     \qmlproperty color SystemPalette::text
134     The text color of the current color group.
135
136     \sa QPalette::ColorRole
137 */
138 QColor QDeclarativeSystemPalette::text() const
139 {
140     Q_D(const QDeclarativeSystemPalette);
141     return d->palette.color(d->group, QPalette::Text);
142 }
143
144 /*!
145     \qmlproperty color SystemPalette::alternateBase
146     The alternate base color of the current color group.
147
148     \sa QPalette::ColorRole
149 */
150 QColor QDeclarativeSystemPalette::alternateBase() const
151 {
152     Q_D(const QDeclarativeSystemPalette);
153     return d->palette.color(d->group, QPalette::AlternateBase);
154 }
155
156 /*!
157     \qmlproperty color SystemPalette::button
158     The button color of the current color group.
159
160     \sa QPalette::ColorRole
161 */
162 QColor QDeclarativeSystemPalette::button() const
163 {
164     Q_D(const QDeclarativeSystemPalette);
165     return d->palette.color(d->group, QPalette::Button);
166 }
167
168 /*!
169     \qmlproperty color SystemPalette::buttonText
170     The button text foreground color of the current color group.
171
172     \sa QPalette::ColorRole
173 */
174 QColor QDeclarativeSystemPalette::buttonText() const
175 {
176     Q_D(const QDeclarativeSystemPalette);
177     return d->palette.color(d->group, QPalette::ButtonText);
178 }
179
180 /*!
181     \qmlproperty color SystemPalette::light
182     The light color of the current color group.
183
184     \sa QPalette::ColorRole
185 */
186 QColor QDeclarativeSystemPalette::light() const
187 {
188     Q_D(const QDeclarativeSystemPalette);
189     return d->palette.color(d->group, QPalette::Light);
190 }
191
192 /*!
193     \qmlproperty color SystemPalette::midlight
194     The midlight color of the current color group.
195
196     \sa QPalette::ColorRole
197 */
198 QColor QDeclarativeSystemPalette::midlight() const
199 {
200     Q_D(const QDeclarativeSystemPalette);
201     return d->palette.color(d->group, QPalette::Midlight);
202 }
203
204 /*!
205     \qmlproperty color SystemPalette::dark
206     The dark color of the current color group.
207
208     \sa QPalette::ColorRole
209 */
210 QColor QDeclarativeSystemPalette::dark() const
211 {
212     Q_D(const QDeclarativeSystemPalette);
213     return d->palette.color(d->group, QPalette::Dark);
214 }
215
216 /*!
217     \qmlproperty color SystemPalette::mid
218     The mid color of the current color group.
219
220     \sa QPalette::ColorRole
221 */
222 QColor QDeclarativeSystemPalette::mid() const
223 {
224     Q_D(const QDeclarativeSystemPalette);
225     return d->palette.color(d->group, QPalette::Mid);
226 }
227
228 /*!
229     \qmlproperty color SystemPalette::shadow
230     The shadow color of the current color group.
231
232     \sa QPalette::ColorRole
233 */
234 QColor QDeclarativeSystemPalette::shadow() const
235 {
236     Q_D(const QDeclarativeSystemPalette);
237     return d->palette.color(d->group, QPalette::Shadow);
238 }
239
240 /*!
241     \qmlproperty color SystemPalette::highlight
242     The highlight color of the current color group.
243
244     \sa QPalette::ColorRole
245 */
246 QColor QDeclarativeSystemPalette::highlight() const
247 {
248     Q_D(const QDeclarativeSystemPalette);
249     return d->palette.color(d->group, QPalette::Highlight);
250 }
251
252 /*!
253     \qmlproperty color SystemPalette::highlightedText
254     The highlighted text color of the current color group.
255
256     \sa QPalette::ColorRole
257 */
258 QColor QDeclarativeSystemPalette::highlightedText() const
259 {
260     Q_D(const QDeclarativeSystemPalette);
261     return d->palette.color(d->group, QPalette::HighlightedText);
262 }
263
264 /*!
265     \qmlproperty enumeration SystemPalette::colorGroup
266
267     The color group of the palette. This can be one of:
268
269     \list
270     \o SystemPalette.Active (default)
271     \o SystemPalette.Inactive
272     \o SystemPalette.Disabled
273     \endlist
274
275     \sa QPalette::ColorGroup
276 */
277 QDeclarativeSystemPalette::ColorGroup QDeclarativeSystemPalette::colorGroup() const
278 {
279     Q_D(const QDeclarativeSystemPalette);
280     return (QDeclarativeSystemPalette::ColorGroup)d->group;
281 }
282
283 void QDeclarativeSystemPalette::setColorGroup(QDeclarativeSystemPalette::ColorGroup colorGroup)
284 {
285     Q_D(QDeclarativeSystemPalette);
286     d->group = (QPalette::ColorGroup)colorGroup;
287     emit paletteChanged();
288 }
289
290 bool QDeclarativeSystemPalette::eventFilter(QObject *watched, QEvent *event)
291 {
292     if (watched == qApp) {
293         if (event->type() == QEvent::ApplicationPaletteChange) {
294             QApplication::postEvent(this, new QEvent(QEvent::ApplicationPaletteChange));
295             return false;
296         }
297     }
298     return QObject::eventFilter(watched, event);
299 }
300
301 bool QDeclarativeSystemPalette::event(QEvent *event)
302 {
303     Q_D(QDeclarativeSystemPalette);
304     if (event->type() == QEvent::ApplicationPaletteChange) {
305         d->palette = QApplication::palette();
306         emit paletteChanged();
307         return true;
308     }
309     return QObject::event(event);
310 }
311
312 QT_END_NAMESPACE