src/declarative/util/qdeclarativesystempalette.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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     \since 4.7
       
    62     \brief The SystemPalette item gives access to the Qt palettes.
       
    63     \sa QPalette
       
    64 
       
    65     Example:
       
    66     \qml
       
    67     SystemPalette { id: myPalette; colorGroup: Qt.Active }
       
    68 
       
    69     Rectangle {
       
    70         width: 640; height: 480
       
    71         color: myPalette.window
       
    72         Text {
       
    73             anchors.fill: parent
       
    74             text: "Hello!"; color: myPalette.windowText
       
    75         }
       
    76     }
       
    77     \endqml
       
    78 */
       
    79 QDeclarativeSystemPalette::QDeclarativeSystemPalette(QObject *parent)
       
    80     : QObject(*(new QDeclarativeSystemPalettePrivate), parent)
       
    81 {
       
    82     Q_D(QDeclarativeSystemPalette);
       
    83     d->palette = QApplication::palette();
       
    84     d->group = QPalette::Active;
       
    85     qApp->installEventFilter(this);
       
    86 }
       
    87 
       
    88 QDeclarativeSystemPalette::~QDeclarativeSystemPalette()
       
    89 {
       
    90 }
       
    91 
       
    92 /*!
       
    93     \qmlproperty color SystemPalette::window
       
    94     The window (general background) color of the current color group.
       
    95 
       
    96     \sa QPalette::ColorRole
       
    97 */
       
    98 QColor QDeclarativeSystemPalette::window() const
       
    99 {
       
   100     Q_D(const QDeclarativeSystemPalette);
       
   101     return d->palette.color(d->group, QPalette::Window);
       
   102 }
       
   103 
       
   104 /*!
       
   105     \qmlproperty color SystemPalette::windowText
       
   106     The window text (general foreground) color of the current color group.
       
   107 
       
   108     \sa QPalette::ColorRole
       
   109 */
       
   110 QColor QDeclarativeSystemPalette::windowText() const
       
   111 {
       
   112     Q_D(const QDeclarativeSystemPalette);
       
   113     return d->palette.color(d->group, QPalette::WindowText);
       
   114 }
       
   115 
       
   116 /*!
       
   117     \qmlproperty color SystemPalette::base
       
   118     The base color of the current color group.
       
   119 
       
   120     \sa QPalette::ColorRole
       
   121 */
       
   122 QColor QDeclarativeSystemPalette::base() const
       
   123 {
       
   124     Q_D(const QDeclarativeSystemPalette);
       
   125     return d->palette.color(d->group, QPalette::Base);
       
   126 }
       
   127 
       
   128 /*!
       
   129     \qmlproperty color SystemPalette::text
       
   130     The text color of the current color group.
       
   131 
       
   132     \sa QPalette::ColorRole
       
   133 */
       
   134 QColor QDeclarativeSystemPalette::text() const
       
   135 {
       
   136     Q_D(const QDeclarativeSystemPalette);
       
   137     return d->palette.color(d->group, QPalette::Text);
       
   138 }
       
   139 
       
   140 /*!
       
   141     \qmlproperty color SystemPalette::alternateBase
       
   142     The alternate base color of the current color group.
       
   143 
       
   144     \sa QPalette::ColorRole
       
   145 */
       
   146 QColor QDeclarativeSystemPalette::alternateBase() const
       
   147 {
       
   148     Q_D(const QDeclarativeSystemPalette);
       
   149     return d->palette.color(d->group, QPalette::AlternateBase);
       
   150 }
       
   151 
       
   152 /*!
       
   153     \qmlproperty color SystemPalette::button
       
   154     The button color of the current color group.
       
   155 
       
   156     \sa QPalette::ColorRole
       
   157 */
       
   158 QColor QDeclarativeSystemPalette::button() const
       
   159 {
       
   160     Q_D(const QDeclarativeSystemPalette);
       
   161     return d->palette.color(d->group, QPalette::Button);
       
   162 }
       
   163 
       
   164 /*!
       
   165     \qmlproperty color SystemPalette::buttonText
       
   166     The button text foreground color of the current color group.
       
   167 
       
   168     \sa QPalette::ColorRole
       
   169 */
       
   170 QColor QDeclarativeSystemPalette::buttonText() const
       
   171 {
       
   172     Q_D(const QDeclarativeSystemPalette);
       
   173     return d->palette.color(d->group, QPalette::ButtonText);
       
   174 }
       
   175 
       
   176 /*!
       
   177     \qmlproperty color SystemPalette::light
       
   178     The light color of the current color group.
       
   179 
       
   180     \sa QPalette::ColorRole
       
   181 */
       
   182 QColor QDeclarativeSystemPalette::light() const
       
   183 {
       
   184     Q_D(const QDeclarativeSystemPalette);
       
   185     return d->palette.color(d->group, QPalette::Light);
       
   186 }
       
   187 
       
   188 /*!
       
   189     \qmlproperty color SystemPalette::midlight
       
   190     The midlight color of the current color group.
       
   191 
       
   192     \sa QPalette::ColorRole
       
   193 */
       
   194 QColor QDeclarativeSystemPalette::midlight() const
       
   195 {
       
   196     Q_D(const QDeclarativeSystemPalette);
       
   197     return d->palette.color(d->group, QPalette::Midlight);
       
   198 }
       
   199 
       
   200 /*!
       
   201     \qmlproperty color SystemPalette::dark
       
   202     The dark color of the current color group.
       
   203 
       
   204     \sa QPalette::ColorRole
       
   205 */
       
   206 QColor QDeclarativeSystemPalette::dark() const
       
   207 {
       
   208     Q_D(const QDeclarativeSystemPalette);
       
   209     return d->palette.color(d->group, QPalette::Dark);
       
   210 }
       
   211 
       
   212 /*!
       
   213     \qmlproperty color SystemPalette::mid
       
   214     The mid color of the current color group.
       
   215 
       
   216     \sa QPalette::ColorRole
       
   217 */
       
   218 QColor QDeclarativeSystemPalette::mid() const
       
   219 {
       
   220     Q_D(const QDeclarativeSystemPalette);
       
   221     return d->palette.color(d->group, QPalette::Mid);
       
   222 }
       
   223 
       
   224 /*!
       
   225     \qmlproperty color SystemPalette::shadow
       
   226     The shadow color of the current color group.
       
   227 
       
   228     \sa QPalette::ColorRole
       
   229 */
       
   230 QColor QDeclarativeSystemPalette::shadow() const
       
   231 {
       
   232     Q_D(const QDeclarativeSystemPalette);
       
   233     return d->palette.color(d->group, QPalette::Shadow);
       
   234 }
       
   235 
       
   236 /*!
       
   237     \qmlproperty color SystemPalette::highlight
       
   238     The highlight color of the current color group.
       
   239 
       
   240     \sa QPalette::ColorRole
       
   241 */
       
   242 QColor QDeclarativeSystemPalette::highlight() const
       
   243 {
       
   244     Q_D(const QDeclarativeSystemPalette);
       
   245     return d->palette.color(d->group, QPalette::Highlight);
       
   246 }
       
   247 
       
   248 /*!
       
   249     \qmlproperty color SystemPalette::highlightedText
       
   250     The highlighted text color of the current color group.
       
   251 
       
   252     \sa QPalette::ColorRole
       
   253 */
       
   254 QColor QDeclarativeSystemPalette::highlightedText() const
       
   255 {
       
   256     Q_D(const QDeclarativeSystemPalette);
       
   257     return d->palette.color(d->group, QPalette::HighlightedText);
       
   258 }
       
   259 
       
   260 /*!
       
   261     \qmlproperty QDeclarativeSystemPalette::ColorGroup SystemPalette::colorGroup
       
   262 
       
   263     The color group of the palette. It can be Active, Inactive or Disabled.
       
   264     Active is the default.
       
   265 
       
   266     \sa QPalette::ColorGroup
       
   267 */
       
   268 QDeclarativeSystemPalette::ColorGroup QDeclarativeSystemPalette::colorGroup() const
       
   269 {
       
   270     Q_D(const QDeclarativeSystemPalette);
       
   271     return (QDeclarativeSystemPalette::ColorGroup)d->group;
       
   272 }
       
   273 
       
   274 void QDeclarativeSystemPalette::setColorGroup(QDeclarativeSystemPalette::ColorGroup colorGroup)
       
   275 {
       
   276     Q_D(QDeclarativeSystemPalette);
       
   277     d->group = (QPalette::ColorGroup)colorGroup;
       
   278     emit paletteChanged();
       
   279 }
       
   280 
       
   281 bool QDeclarativeSystemPalette::eventFilter(QObject *watched, QEvent *event)
       
   282 {
       
   283     if (watched == qApp) {
       
   284         if (event->type() == QEvent::ApplicationPaletteChange) {
       
   285             QApplication::postEvent(this, new QEvent(QEvent::ApplicationPaletteChange));
       
   286             return false;
       
   287         }
       
   288     }
       
   289     return QObject::eventFilter(watched, event);
       
   290 }
       
   291 
       
   292 bool QDeclarativeSystemPalette::event(QEvent *event)
       
   293 {
       
   294     Q_D(QDeclarativeSystemPalette);
       
   295     if (event->type() == QEvent::ApplicationPaletteChange) {
       
   296         d->palette = QApplication::palette();
       
   297         emit paletteChanged();
       
   298         return true;
       
   299     }
       
   300     return QObject::event(event);
       
   301 }
       
   302 
       
   303 QT_END_NAMESPACE