Some error banner delays fixed.
[jenirok] / src / daemon / informationbox.cpp
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 QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial Usage
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.
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 ** 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.
35 **
36 ** If you have questions regarding the use of this file, please contact
37 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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>
51
52 #include <QtGui/QX11Info>
53 #include <X11/Xlib.h>
54 #include <X11/Xatom.h>
55
56 #include "informationbox.h"
57
58 class InformationBoxPrivate
59 {
60     Q_DECLARE_PUBLIC(InformationBox)
61
62 public:
63     InformationBoxPrivate()
64         : m_timeout(InformationBox::DefaultTimeout),
65           m_timer_id(0), m_layout(0), m_widget(0), q_ptr(0)
66     { }
67
68     void enforceInformationType();
69
70     int m_timeout;
71     int m_timer_id;
72     QBoxLayout *m_layout;
73     QWidget *m_widget;
74     InformationBox *q_ptr;
75 };
76
77 /*!
78     Constructs an information box with no text, a default timeout
79     (\c DefaultTimeout) and the given \a parent widget.
80 */
81 InformationBox::InformationBox(QWidget *parent)
82     : QDialog(parent, Qt::CustomizeWindowHint), d_ptr(new InformationBoxPrivate())
83 {
84     Q_D(InformationBox);
85     d_ptr->q_ptr = this;
86
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*/);
94 }
95
96
97 /*!
98     Destroys the information box.
99 */
100 InformationBox::~InformationBox()
101 { }
102
103 /*! \internal
104  */
105 void InformationBoxPrivate::enforceInformationType()
106 {
107     Q_Q(InformationBox);
108
109     const char *type = "_HILDON_NOTIFICATION_TYPE_BANNER";
110
111     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_NOTIFICATION_TYPE", False);
112
113     XChangeProperty(QX11Info::display(), q->winId(), atom, XA_STRING, 8,
114                     PropModeReplace, (unsigned char *) type, qstrlen(type));
115 }
116
117
118 /*!
119   \property InformationBox::timeout
120   \brief the timeout after which the informaton box should automatically be
121   hidden.
122
123   Setting this value while the box is already visible will reset its timer.
124
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).
128
129   The default value of this property is \c DefaultTimeout (3 seconds).
130 */
131
132 int InformationBox::timeout() const
133 {
134     Q_D(const InformationBox);
135     return d->m_timeout;
136 }
137
138 void InformationBox::setTimeout(unsigned int ms)
139 {
140     Q_D(InformationBox);
141     d->m_timeout = ms;
142
143     // restart the timer
144     if (d->m_timeout != InformationBox::NoTimeout
145             && d->m_timer_id) {
146         killTimer(d->m_timer_id);
147         d->m_timer_id = startTimer(d->m_timeout);
148     }
149 }
150
151 /*!
152     Returns the central widget of the information box.
153 */
154 QWidget *InformationBox::widget() const
155 {
156     Q_D(const InformationBox);
157     return d->m_widget;
158 }
159
160 /*!
161     Sets the central widget of the information box to \a widget.
162
163     InformationBox will take ownership of \a widget by reparenting
164     it.  Any previously set widget() will be destroyed.
165 */
166 void InformationBox::setWidget(QWidget *widget)
167 {
168     Q_D(InformationBox);
169     delete d->m_widget;
170     d->m_widget = widget;
171     if (widget) {
172         d->m_layout->addWidget(widget);
173         widget->show();
174     }
175 }
176
177 /*! \reimp
178 */
179 void InformationBox::showEvent(QShowEvent *)
180 {
181     Q_D(InformationBox);
182
183     d->enforceInformationType();
184     if (d->m_timeout != InformationBox::NoTimeout)
185         d->m_timer_id = startTimer(d->m_timeout);
186 }
187
188 /*! \reimp
189 */
190 void InformationBox::timerEvent(QTimerEvent *te)
191 {
192     Q_D(InformationBox);
193
194     if (te->timerId() == d->m_timer_id) {
195         killTimer(d->m_timer_id);
196         hide();
197     }
198 }
199
200 /*! \reimp
201 */
202 void InformationBox::mousePressEvent(QMouseEvent *me)
203 {
204     if (me->button() == Qt::LeftButton) {
205         hide();
206         emit clicked();
207     }
208 }
209
210 /*! \reimp
211 */
212 void InformationBox::paintEvent(QPaintEvent *)
213 {
214     if (qobject_cast<QMaemo5Style*>(style())) {
215         QPainter painter(this);
216         QStyleOption option;
217         option.initFrom(this);
218         style()->drawPrimitive(static_cast<QStyle::PrimitiveElement>(QMaemo5Style::PE_Maemo5InformationBox),
219                                &option, &painter, this);
220     }
221 }
222