7fd8dc9e6e44b501f6864c7ead0c82c9e6b6c385
[profile/ivi/qtdeclarative.git] / src / qtquick1 / util / qdeclarativesystempalette.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "QtQuick1/private/qdeclarativesystempalette_p.h"
43
44 #include <QApplication>
45
46 #include <private/qobject_p.h>
47
48 QT_BEGIN_NAMESPACE
49
50
51
52 class QDeclarative1SystemPalettePrivate : public QObjectPrivate
53 {
54 public:
55     QPalette palette;
56     QPalette::ColorGroup group;
57 };
58
59
60
61 /*!
62     \qmlclass SystemPalette QDeclarative1SystemPalette
63     \inqmlmodule QtQuick 1
64     \ingroup qml-utility-elements
65     \since QtQuick 1.0
66     \brief The SystemPalette element provides access to the Qt palettes.
67
68     The SystemPalette element provides access to the Qt application
69     palettes. This provides information about the standard colors used 
70     for application windows, buttons and other features. These colors
71     are grouped into three \e {color groups}: \c Active, \c Inactive,
72     and \c Disabled.  See the QPalette documentation for details about
73     color groups and the properties provided by SystemPalette.
74
75     This can be used to color items in a way that provides a more
76     native look and feel.
77
78     The following example creates a palette from the \c Active color
79     group and uses this to color the window and text items
80     appropriately:
81
82     \snippet doc/src/snippets/qtquick1/systempalette.qml 0
83
84     \sa QPalette
85 */
86 QDeclarative1SystemPalette::QDeclarative1SystemPalette(QObject *parent)
87     : QObject(*(new QDeclarative1SystemPalettePrivate), parent)
88 {
89     Q_D(QDeclarative1SystemPalette);
90     d->palette = QApplication::palette();
91     d->group = QPalette::Active;
92     qApp->installEventFilter(this);
93 }
94
95 QDeclarative1SystemPalette::~QDeclarative1SystemPalette()
96 {
97 }
98
99 /*!
100     \qmlproperty color QtQuick1::SystemPalette::window
101     The window (general background) color of the current color group.
102
103     \sa QPalette::ColorRole
104 */
105 QColor QDeclarative1SystemPalette::window() const
106 {
107     Q_D(const QDeclarative1SystemPalette);
108     return d->palette.color(d->group, QPalette::Window);
109 }
110
111 /*!
112     \qmlproperty color QtQuick1::SystemPalette::windowText
113     The window text (general foreground) color of the current color group.
114
115     \sa QPalette::ColorRole
116 */
117 QColor QDeclarative1SystemPalette::windowText() const
118 {
119     Q_D(const QDeclarative1SystemPalette);
120     return d->palette.color(d->group, QPalette::WindowText);
121 }
122
123 /*!
124     \qmlproperty color QtQuick1::SystemPalette::base
125     The base color of the current color group.
126
127     \sa QPalette::ColorRole
128 */
129 QColor QDeclarative1SystemPalette::base() const
130 {
131     Q_D(const QDeclarative1SystemPalette);
132     return d->palette.color(d->group, QPalette::Base);
133 }
134
135 /*!
136     \qmlproperty color QtQuick1::SystemPalette::text
137     The text color of the current color group.
138
139     \sa QPalette::ColorRole
140 */
141 QColor QDeclarative1SystemPalette::text() const
142 {
143     Q_D(const QDeclarative1SystemPalette);
144     return d->palette.color(d->group, QPalette::Text);
145 }
146
147 /*!
148     \qmlproperty color QtQuick1::SystemPalette::alternateBase
149     The alternate base color of the current color group.
150
151     \sa QPalette::ColorRole
152 */
153 QColor QDeclarative1SystemPalette::alternateBase() const
154 {
155     Q_D(const QDeclarative1SystemPalette);
156     return d->palette.color(d->group, QPalette::AlternateBase);
157 }
158
159 /*!
160     \qmlproperty color QtQuick1::SystemPalette::button
161     The button color of the current color group.
162
163     \sa QPalette::ColorRole
164 */
165 QColor QDeclarative1SystemPalette::button() const
166 {
167     Q_D(const QDeclarative1SystemPalette);
168     return d->palette.color(d->group, QPalette::Button);
169 }
170
171 /*!
172     \qmlproperty color QtQuick1::SystemPalette::buttonText
173     The button text foreground color of the current color group.
174
175     \sa QPalette::ColorRole
176 */
177 QColor QDeclarative1SystemPalette::buttonText() const
178 {
179     Q_D(const QDeclarative1SystemPalette);
180     return d->palette.color(d->group, QPalette::ButtonText);
181 }
182
183 /*!
184     \qmlproperty color QtQuick1::SystemPalette::light
185     The light color of the current color group.
186
187     \sa QPalette::ColorRole
188 */
189 QColor QDeclarative1SystemPalette::light() const
190 {
191     Q_D(const QDeclarative1SystemPalette);
192     return d->palette.color(d->group, QPalette::Light);
193 }
194
195 /*!
196     \qmlproperty color QtQuick1::SystemPalette::midlight
197     The midlight color of the current color group.
198
199     \sa QPalette::ColorRole
200 */
201 QColor QDeclarative1SystemPalette::midlight() const
202 {
203     Q_D(const QDeclarative1SystemPalette);
204     return d->palette.color(d->group, QPalette::Midlight);
205 }
206
207 /*!
208     \qmlproperty color QtQuick1::SystemPalette::dark
209     The dark color of the current color group.
210
211     \sa QPalette::ColorRole
212 */
213 QColor QDeclarative1SystemPalette::dark() const
214 {
215     Q_D(const QDeclarative1SystemPalette);
216     return d->palette.color(d->group, QPalette::Dark);
217 }
218
219 /*!
220     \qmlproperty color QtQuick1::SystemPalette::mid
221     The mid color of the current color group.
222
223     \sa QPalette::ColorRole
224 */
225 QColor QDeclarative1SystemPalette::mid() const
226 {
227     Q_D(const QDeclarative1SystemPalette);
228     return d->palette.color(d->group, QPalette::Mid);
229 }
230
231 /*!
232     \qmlproperty color QtQuick1::SystemPalette::shadow
233     The shadow color of the current color group.
234
235     \sa QPalette::ColorRole
236 */
237 QColor QDeclarative1SystemPalette::shadow() const
238 {
239     Q_D(const QDeclarative1SystemPalette);
240     return d->palette.color(d->group, QPalette::Shadow);
241 }
242
243 /*!
244     \qmlproperty color QtQuick1::SystemPalette::highlight
245     The highlight color of the current color group.
246
247     \sa QPalette::ColorRole
248 */
249 QColor QDeclarative1SystemPalette::highlight() const
250 {
251     Q_D(const QDeclarative1SystemPalette);
252     return d->palette.color(d->group, QPalette::Highlight);
253 }
254
255 /*!
256     \qmlproperty color QtQuick1::SystemPalette::highlightedText
257     The highlighted text color of the current color group.
258
259     \sa QPalette::ColorRole
260 */
261 QColor QDeclarative1SystemPalette::highlightedText() const
262 {
263     Q_D(const QDeclarative1SystemPalette);
264     return d->palette.color(d->group, QPalette::HighlightedText);
265 }
266
267 /*!
268     \qmlproperty enumeration QtQuick1::SystemPalette::colorGroup
269
270     The color group of the palette. This can be one of:
271
272     \list
273     \o SystemPalette.Active (default)
274     \o SystemPalette.Inactive
275     \o SystemPalette.Disabled
276     \endlist
277
278     \sa QPalette::ColorGroup
279 */
280 QDeclarative1SystemPalette::ColorGroup QDeclarative1SystemPalette::colorGroup() const
281 {
282     Q_D(const QDeclarative1SystemPalette);
283     return (QDeclarative1SystemPalette::ColorGroup)d->group;
284 }
285
286 void QDeclarative1SystemPalette::setColorGroup(QDeclarative1SystemPalette::ColorGroup colorGroup)
287 {
288     Q_D(QDeclarative1SystemPalette);
289     d->group = (QPalette::ColorGroup)colorGroup;
290     emit paletteChanged();
291 }
292
293 bool QDeclarative1SystemPalette::eventFilter(QObject *watched, QEvent *event)
294 {
295     if (watched == qApp) {
296         if (event->type() == QEvent::ApplicationPaletteChange) {
297             QApplication::postEvent(this, new QEvent(QEvent::ApplicationPaletteChange));
298             return false;
299         }
300     }
301     return QObject::eventFilter(watched, event);
302 }
303
304 bool QDeclarative1SystemPalette::event(QEvent *event)
305 {
306     Q_D(QDeclarative1SystemPalette);
307     if (event->type() == QEvent::ApplicationPaletteChange) {
308         d->palette = QApplication::palette();
309         emit paletteChanged();
310         return true;
311     }
312     return QObject::event(event);
313 }
314
315
316
317 QT_END_NAMESPACE