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*/);
98 Destroys the information box.
100 InformationBox::~InformationBox()
105 void InformationBoxPrivate::enforceInformationType()
109 const char *type = "_HILDON_NOTIFICATION_TYPE_BANNER";
111 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_NOTIFICATION_TYPE", False);
113 XChangeProperty(QX11Info::display(), q->winId(), atom, XA_STRING, 8,
114 PropModeReplace, (unsigned char *) type, qstrlen(type));
119 \property InformationBox::timeout
120 \brief the timeout after which the informaton box should automatically be
123 Setting this value while the box is already visible will reset its timer.
125 The timeout value can be specified as an arbitrary millisecond value,
126 although it is recommended to use the predefined values \c NoTimeout (for
127 Hildon Notes) and \c DefaultTimeout (for style guide compliant Banners).
129 The default value of this property is \c DefaultTimeout (3 seconds).
132 int InformationBox::timeout() const
134 Q_D(const InformationBox);
138 void InformationBox::setTimeout(unsigned int ms)
144 if (d->m_timeout != InformationBox::NoTimeout
146 killTimer(d->m_timer_id);
147 d->m_timer_id = startTimer(d->m_timeout);
152 Returns the central widget of the information box.
154 QWidget *InformationBox::widget() const
156 Q_D(const InformationBox);
161 Sets the central widget of the information box to \a widget.
163 InformationBox will take ownership of \a widget by reparenting
164 it. Any previously set widget() will be destroyed.
166 void InformationBox::setWidget(QWidget *widget)
170 d->m_widget = widget;
172 d->m_layout->addWidget(widget);
179 void InformationBox::showEvent(QShowEvent *)
183 d->enforceInformationType();
184 if (d->m_timeout != InformationBox::NoTimeout)
185 d->m_timer_id = startTimer(d->m_timeout);
190 void InformationBox::timerEvent(QTimerEvent *te)
194 if (te->timerId() == d->m_timer_id) {
195 killTimer(d->m_timer_id);
202 void InformationBox::mousePressEvent(QMouseEvent *me)
204 if (me->button() == Qt::LeftButton) {
212 void InformationBox::paintEvent(QPaintEvent *)
214 if (qobject_cast<QMaemo5Style*>(style())) {
215 QPainter painter(this);
217 option.initFrom(this);
218 style()->drawPrimitive(static_cast<QStyle::PrimitiveElement>(QMaemo5Style::PE_Maemo5InformationBox),
219 &option, &painter, this);