1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
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.
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.
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ** If you have questions regarding the use of this file, please contact
37 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include <QtGui/QApplication>
43 #include <QtGui/QDesktopWidget>
44 #include <QtGui/QLabel>
45 #include <QtGui/QLayout>
46 #include <QtGui/QMaemo5Style>
47 #include <QtGui/QStyleOption>
48 #include <QtGui/QDesktopWidget>
49 #include <QtGui/QMouseEvent>
50 #include <QtGui/QPainter>
52 #include <QtGui/QX11Info>
54 #include <X11/Xatom.h>
56 #include "informationbox.h"
58 class InformationBoxPrivate
60 Q_DECLARE_PUBLIC(InformationBox)
63 InformationBoxPrivate()
64 : m_timeout(InformationBox::DefaultTimeout),
65 m_timer_id(0), m_layout(0), m_widget(0), q_ptr(0)
68 void enforceInformationType();
74 InformationBox *q_ptr;
78 Constructs an information box with no text, a default timeout
79 (\c DefaultTimeout) and the given \a parent widget.
81 InformationBox::InformationBox(QWidget *parent)
82 : QDialog(parent, Qt::CustomizeWindowHint), d_ptr(new InformationBoxPrivate())
87 setAttribute(Qt::WA_X11NetWmWindowTypeNotification);
88 setAttribute(Qt::WA_ShowWithoutActivating);
89 setFocusPolicy(Qt::NoFocus);
90 d->m_layout = new QHBoxLayout(this);
91 d->m_layout->setContentsMargins(8, 0, 8, 0); // 8 == HILDON_MARGIN_DEFAULT
92 setFixedWidth(QApplication::desktop()->screenGeometry().width());
93 //move(0, 56 /*HILDON_WINDOW_TITLEBAR_HEIGHT*/);
97 Destroys the information box.
99 InformationBox::~InformationBox()
104 void InformationBoxPrivate::enforceInformationType()
108 const char *type = "_HILDON_NOTIFICATION_TYPE_BANNER";
110 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_NOTIFICATION_TYPE", False);
112 XChangeProperty(QX11Info::display(), q->winId(), atom, XA_STRING, 8,
113 PropModeReplace, (unsigned char *) type, qstrlen(type));
117 \property InformationBox::timeout
118 \brief the timeout after which the informaton box should automatically be
121 Setting this value while the box is already visible will reset its timer.
123 The timeout value can be specified as an arbitrary millisecond value,
124 although it is recommended to use the predefined values \c NoTimeout (for
125 Hildon Notes) and \c DefaultTimeout (for style guide compliant Banners).
127 The default value of this property is \c DefaultTimeout (3 seconds).
130 int InformationBox::timeout() const
132 Q_D(const InformationBox);
136 void InformationBox::setTimeout(unsigned int ms)
142 if (d->m_timeout != InformationBox::NoTimeout
144 killTimer(d->m_timer_id);
145 d->m_timer_id = startTimer(d->m_timeout);
150 Returns the central widget of the information box.
152 QWidget *InformationBox::widget() const
154 Q_D(const InformationBox);
159 Sets the central widget of the information box to \a widget.
161 InformationBox will take ownership of \a widget by reparenting
162 it. Any previously set widget() will be destroyed.
164 void InformationBox::setWidget(QWidget *widget)
168 d->m_widget = widget;
170 d->m_layout->addWidget(widget);
177 void InformationBox::showEvent(QShowEvent *)
181 d->enforceInformationType();
182 if (d->m_timeout != InformationBox::NoTimeout)
183 d->m_timer_id = startTimer(d->m_timeout);
188 void InformationBox::timerEvent(QTimerEvent *te)
192 if (te->timerId() == d->m_timer_id) {
193 killTimer(d->m_timer_id);
200 void InformationBox::mousePressEvent(QMouseEvent *me)
202 if (me->button() == Qt::LeftButton) {
210 void InformationBox::paintEvent(QPaintEvent *)
212 if (qobject_cast<QMaemo5Style*>(style())) {
213 QPainter painter(this);
215 option.initFrom(this);
216 style()->drawPrimitive(static_cast<QStyle::PrimitiveElement>(QMaemo5Style::PE_Maemo5InformationBox),
217 &option, &painter, this);