e0795a38bfdf6df9f5e2d1b5437320dc4593c268
[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     Destroys the information box.
98 */
99 InformationBox::~InformationBox()
100 { }
101
102 /*! \internal
103  */
104 void InformationBoxPrivate::enforceInformationType()
105 {
106     Q_Q(InformationBox);
107
108     const char *type = "_HILDON_NOTIFICATION_TYPE_BANNER";
109
110     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_NOTIFICATION_TYPE", False);
111
112     XChangeProperty(QX11Info::display(), q->winId(), atom, XA_STRING, 8,
113                     PropModeReplace, (unsigned char *) type, qstrlen(type));
114 }
115
116 /*!
117   \property InformationBox::timeout
118   \brief the timeout after which the informaton box should automatically be
119   hidden.
120
121   Setting this value while the box is already visible will reset its timer.
122
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).
126
127   The default value of this property is \c DefaultTimeout (3 seconds).
128 */
129
130 int InformationBox::timeout() const
131 {
132     Q_D(const InformationBox);
133     return d->m_timeout;
134 }
135
136 void InformationBox::setTimeout(unsigned int ms)
137 {
138     Q_D(InformationBox);
139     d->m_timeout = ms;
140
141     // restart the timer
142     if (d->m_timeout != InformationBox::NoTimeout
143             && d->m_timer_id) {
144         killTimer(d->m_timer_id);
145         d->m_timer_id = startTimer(d->m_timeout);
146     }
147 }
148
149 /*!
150     Returns the central widget of the information box.
151 */
152 QWidget *InformationBox::widget() const
153 {
154     Q_D(const InformationBox);
155     return d->m_widget;
156 }
157
158 /*!
159     Sets the central widget of the information box to \a widget.
160
161     InformationBox will take ownership of \a widget by reparenting
162     it.  Any previously set widget() will be destroyed.
163 */
164 void InformationBox::setWidget(QWidget *widget)
165 {
166     Q_D(InformationBox);
167     delete d->m_widget;
168     d->m_widget = widget;
169     if (widget) {
170         d->m_layout->addWidget(widget);
171         widget->show();
172     }
173 }
174
175 /*! \reimp
176 */
177 void InformationBox::showEvent(QShowEvent *)
178 {
179     Q_D(InformationBox);
180
181     d->enforceInformationType();
182     if (d->m_timeout != InformationBox::NoTimeout)
183         d->m_timer_id = startTimer(d->m_timeout);
184 }
185
186 /*! \reimp
187 */
188 void InformationBox::timerEvent(QTimerEvent *te)
189 {
190     Q_D(InformationBox);
191
192     if (te->timerId() == d->m_timer_id) {
193         killTimer(d->m_timer_id);
194         hide();
195     }
196 }
197
198 /*! \reimp
199 */
200 void InformationBox::mousePressEvent(QMouseEvent *me)
201 {
202     if (me->button() == Qt::LeftButton) {
203         hide();
204         emit clicked();
205     }
206 }
207
208 /*! \reimp
209 */
210 void InformationBox::paintEvent(QPaintEvent *)
211 {
212     if (qobject_cast<QMaemo5Style*>(style())) {
213         QPainter painter(this);
214         QStyleOption option;
215         option.initFrom(this);
216         style()->drawPrimitive(static_cast<QStyle::PrimitiveElement>(QMaemo5Style::PE_Maemo5InformationBox),
217                                &option, &painter, this);
218     }
219 }
220