1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qwsembedwidget.h"
43 
44 #ifndef QT_NO_QWSEMBEDWIDGET
45 
46 #include <qwsdisplay_qws.h>
47 #include <private/qwidget_p.h>
48 #include <private/qwsdisplay_qws_p.h>
49 #include <private/qwscommand_qws_p.h>
50 
51 QT_BEGIN_NAMESPACE
52 
53 // TODO:
54 // Must remove window decorations from the embedded window
55 // Focus In/Out, Keyboard/Mouse...
56 //
57 // BUG: what if my parent change parent?
58 
59 class QWSEmbedWidgetPrivate : public QWidgetPrivate
60 {
61     Q_DECLARE_PUBLIC(QWSEmbedWidget);
62 
63 public:
64     QWSEmbedWidgetPrivate(int winId);
65     void updateWindow();
66     void resize(const QSize &size);
67 
68     QWidget *window;
69     WId windowId;
70     WId embeddedId;
71 };
72 
QWSEmbedWidgetPrivate(int winId)73 QWSEmbedWidgetPrivate::QWSEmbedWidgetPrivate(int winId)
74     : window(0), windowId(0), embeddedId(winId)
75 {
76 }
77 
updateWindow()78 void QWSEmbedWidgetPrivate::updateWindow()
79 {
80     Q_Q(QWSEmbedWidget);
81 
82     QWidget *win = q->window();
83     if (win == window)
84         return;
85 
86     if (window) {
87         window->removeEventFilter(q);
88         QWSEmbedCommand command;
89         command.setData(windowId, embeddedId, QWSEmbedEvent::StopEmbed);
90         QWSDisplay::instance()->d->sendCommand(command);
91     }
92 
93     window = win;
94     if (!window)
95         return;
96     windowId = window->winId();
97 
98     QWSEmbedCommand command;
99     command.setData(windowId, embeddedId, QWSEmbedEvent::StartEmbed);
100     QWSDisplay::instance()->d->sendCommand(command);
101     window->installEventFilter(q);
102     q->installEventFilter(q);
103 }
104 
resize(const QSize & size)105 void QWSEmbedWidgetPrivate::resize(const QSize &size)
106 {
107     if (!window)
108         return;
109 
110     Q_Q(QWSEmbedWidget);
111 
112     QWSEmbedCommand command;
113     command.setData(windowId, embeddedId, QWSEmbedEvent::Region,
114                     QRect(q->mapToGlobal(QPoint(0, 0)), size));
115     QWSDisplay::instance()->d->sendCommand(command);
116 }
117 
118 /*!
119     \class QWSEmbedWidget
120     \since 4.2
121     \ingroup qws
122     \ingroup advanced
123 
124     \brief The QWSEmbedWidget class enables embedded top-level widgets
125     in Qt for Embedded Linux.
126 
127     Note that this class is only available in \l{Qt for Embedded Linux}.
128 
129     QWSEmbedWidget inherits QWidget and acts as any other widget, but
130     in addition it is capable of embedding another top-level widget.
131 
132     An example of use is when painting directly onto the screen using
133     the QDirectPainter class. Then the reserved region can be embedded
134     into an instance of the QWSEmbedWidget class, providing for
135     example event handling and size policies for the reserved region.
136 
137     All that is required to embed a top-level widget is its window ID.
138 
139     \sa {Qt for Embedded Linux Architecture}
140 */
141 
142 /*!
143     Constructs a widget with the given \a parent, embedding the widget
144     identified by the given window \a id.
145 */
QWSEmbedWidget(WId id,QWidget * parent)146 QWSEmbedWidget::QWSEmbedWidget(WId id, QWidget *parent)
147     : QWidget(*new QWSEmbedWidgetPrivate(id), parent, 0)
148 {
149     Q_D(QWSEmbedWidget);
150     d->updateWindow();
151 }
152 
153 /*!
154     Destroys this widget.
155 */
~QWSEmbedWidget()156 QWSEmbedWidget::~QWSEmbedWidget()
157 {
158     Q_D(QWSEmbedWidget);
159     if (!d->window)
160         return;
161 
162     QWSEmbedCommand command;
163     command.setData(d->windowId, d->embeddedId, QWSEmbedEvent::StopEmbed);
164     QWSDisplay::instance()->d->sendCommand(command);
165 }
166 
167 /*!
168     \reimp
169 */
eventFilter(QObject * object,QEvent * event)170 bool QWSEmbedWidget::eventFilter(QObject *object, QEvent *event)
171 {
172     Q_D(QWSEmbedWidget);
173     if (object == d->window && event->type() == QEvent::Move)
174         resizeEvent(0);
175     else if (object == this && event->type() == QEvent::Hide)
176         d->resize(QSize());
177     return QWidget::eventFilter(object, event);
178 }
179 
180 /*!
181     \reimp
182 */
changeEvent(QEvent * event)183 void QWSEmbedWidget::changeEvent(QEvent *event)
184 {
185     Q_D(QWSEmbedWidget);
186     if (event->type() == QEvent::ParentChange)
187         d->updateWindow();
188 }
189 
190 /*!
191     \reimp
192 */
resizeEvent(QResizeEvent *)193 void QWSEmbedWidget::resizeEvent(QResizeEvent*)
194 {
195     Q_D(QWSEmbedWidget);
196     d->resize(rect().size());
197 }
198 
199 /*!
200     \reimp
201 */
moveEvent(QMoveEvent *)202 void QWSEmbedWidget::moveEvent(QMoveEvent*)
203 {
204     resizeEvent(0);
205 }
206 
207 /*!
208     \reimp
209 */
hideEvent(QHideEvent *)210 void QWSEmbedWidget::hideEvent(QHideEvent*)
211 {
212     Q_D(QWSEmbedWidget);
213     d->resize(QSize());
214 }
215 
216 /*!
217     \reimp
218 */
showEvent(QShowEvent *)219 void QWSEmbedWidget::showEvent(QShowEvent*)
220 {
221     Q_D(QWSEmbedWidget);
222     d->resize(rect().size());
223 }
224 
225 QT_END_NAMESPACE
226 
227 #endif // QT_NO_QWSEMBEDWIDGET
228