API Documentation
Loading...
Searching...
No Matches
WindowContainer.h
Go to the documentation of this file.
1#pragma once
2#include <QWindow>
3#include <QTimer>
4#include <QPainter>
5#include <QHBoxLayout>
6#include <QResizeEvent>
7#include <QFrame>
8#if defined(ANDROID)
9 #define FORCE_HIDE_WINDOW 0
10 #define NDEVR_SPECIAL_GRAPHICS_WINDOW 0
11 #define USE_WINDOW_WIDGET 0
12 //typedef QAbstractScrollArea BaseWindowContainer;
13 typedef QFrame BaseWindowContainer;
14#elif defined(Q_OS_WASM)
15 #include <QFrame>
16 #define FORCE_HIDE_WINDOW 0
17 #define USE_WINDOW_WIDGET 0
18 #define NDEVR_SPECIAL_GRAPHICS_WINDOW 0
19 typedef QFrame BaseWindowContainer;
20#else
21 #include <QFrame>
22 #define FORCE_HIDE_WINDOW 0
23 #define USE_WINDOW_WIDGET 0
24 #define NDEVR_SPECIAL_GRAPHICS_WINDOW 0
25 typedef QFrame BaseWindowContainer;
26#endif
27
28namespace NDEVR
29{
30 /**--------------------------------------------------------------------------------------------------
31 \brief A (hacky) class for storing a window into a container. This is used if the rendering engine
32 for a particular widget is different from the one used by the application. The hackiness comes
33 from Qt's limited support for this functionality which results in the need to hack around the
34 errors in Qt's core library.
35 **/
37 {
38 public:
39 WindowContainer(QWidget* parent = nullptr)
40 : BaseWindowContainer(parent)
41 , m_location_timer(new QTimer(this))
43 , m_window(nullptr)
44 , m_container(nullptr)
45 {
46/*#ifdef ANDROID
47 connect(m_location_timer, &QTimer::timeout, this, [this] {
48 updateSize();
49 });
50 m_location_timer->setInterval(1);
51 m_location_timer->setSingleShot(false);
52 m_location_timer->start();
53#endif*/
54 //setAttribute(Qt::WA_NoSystemBackground);
55 //setAttribute(Qt::WA_OpaquePaintEvent);
56 //setAutoFillBackground(true);
57 }
58 static Qt::WindowFlags windowFlags()
59 {
60 #if USE_WINDOW_WIDGET
61 return Qt::WindowFlags();//Qt::WindowFlags(Qt::Window | Qt::WindowDoesNotAcceptFocus | Qt::WindowTransparentForInput | Qt::WindowStaysOnBottomHint);
62 #else
63 #if defined(ANDROID)
64 return Qt::WindowFlags();//Qt::WindowDoesNotAcceptFocus | Qt::WindowTransparentForInput);
65 #elif defined(Q_OS_WASM)
66 return Qt::WindowFlags();
67 #else
68 return Qt::WindowFlags();// Qt::WindowFlags(Qt::Widget | Qt::WindowDoesNotAcceptFocus | Qt::WindowTransparentForInput);
69 #endif
70 #endif
71 }
72
73 QPoint windowOffset() const
74 {
75#if USE_WINDOW_WIDGET
76 return mapToGlobal(QPoint(0, 0));
77#else
78 return QPoint(0,0);
79#endif
80 }
81 void setWidget(QWidget* window)
82 {
83 if (m_container == window)
84 return;
85 if (m_container)
86 {
87 m_container->deleteLater();
88 m_container = nullptr;
89 }
90 m_window = nullptr;
91 m_container = window;
92 if (m_container)
93 {
94 //m_container->setAttribute(Qt::WA_NoSystemBackground);
95 m_container->setParent(this);
96 m_container->setMouseTracking(true);
97 }
98 }
99 void setWindow(QWindow* window)
100 {
101 if (window != m_window)
102 {
103 if (m_container)
104 {
105 m_container->deleteLater();
106 m_container = nullptr;
107 }
108 m_window = window;
109 if (m_window)
110 {
111#if NDEVR_SPECIAL_GRAPHICS_WINDOW
112 QHBoxLayout* layout = new QHBoxLayout();
113 layout->setContentsMargins(0,0,0,0);
114#endif
115 m_container = QWidget::createWindowContainer(m_window, this, windowFlags());
116 //m_container->setAttribute(Qt::WA_ShowWithoutActivating);
117 //m_container->installEventFilter(this);
118 m_container->setMouseTracking(true);
119#if NDEVR_SPECIAL_GRAPHICS_WINDOW
120 layout->addWidget(m_container);
121 setLayout(layout);
122#endif
123
124 //m_container->setEnabled(false);
125 //m_window->resize(size());
126 }
127 }
128 }
129 void resizeEvent(QResizeEvent* resize_event) override
130 {
131 BaseWindowContainer::resizeEvent(resize_event);
132 m_last_paint_size = resize_event->oldSize();
133#ifndef ANDROID
134 //repaint();
135#endif
136 updateSize();
137 }
139 {
140#if !NDEVR_SPECIAL_GRAPHICS_WINDOW
141 /*QTimer::singleShot(1000, this, [this]
142 {
143 QWidget* widget = new QWidget(this, windowFlags());
144 widget->setAttribute(Qt::WidgetAttribute::WA_PaintOnScreen, false);
145 widget->show();
146 qApp->setActiveWindow(this);
147 widget->deleteLater();
148 });*/
149#endif
150 }
151 void paintEvent(QPaintEvent* event) override
152 {
153#if !NDEVR_SPECIAL_GRAPHICS_WINDOW
154 updateSize();
155#endif
156 /*QSize new_size = size();
157 QSize old_size = QSize(0, 0);// window()->size();
158
159 if (new_size.width() == 0 || new_size.height() == 0)
160 return;
161 if (old_size.width() < new_size.width() || old_size.height() < new_size.height())
162 {
163
164 QRect current_size_x(QPoint(old_size.width(), 0), QSize(new_size.width() - old_size.width(), height()));
165 QRect current_size_y(QPoint(0, old_size.height()), QSize(new_size.width(), new_size.height() - old_size.height()));
166 QPalette pal = palette();
167
168 QPainter painter(this);
169 QRegion region;
170 if (current_size_x.size().width() > 0)
171 region += current_size_x;
172 if (current_size_y.size().height() > 0)
173 region += current_size_y;
174 if (current_size_x.size().width() > 0)
175 painter.fillRect(current_size_x, pal.color(backgroundRole()));
176 if (current_size_y.size().height() > 0)
177 painter.fillRect(current_size_y, pal.color(backgroundRole()));
178 painter.end();
179 m_last_paint_size = size();
180 }*/
181 BaseWindowContainer::paintEvent(event);
182 }
183
184 /*bool eventFilter(QObject* watched, QEvent* e) override
185 {
186 QEvent::Type q_event = e->type();
187 switch (q_event)
188 {
189 case QEvent::MouseMove:
190 case QEvent::MouseButtonPress:
191 case QEvent::MouseButtonRelease:
192 case QEvent::MouseButtonDblClick:
193 if (QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(e))
194 {
195 if (!geometry().contains(mapFromGlobal(mouse_event->globalPos())))
196 {
197 //e->setAccepted(false);
198 return true;
199 }
200 }
201 return false;
202 case QEvent::Resize:
203 return true;
204 default:
205 return false;
206 }
207 }*/
208 virtual void updateSize()
209 {
210#if !NDEVR_SPECIAL_GRAPHICS_WINDOW
211 if(isVisible())
212 {
213 QRect rect = QRect(windowOffset(), size());
214 if (m_container && m_container->geometry() != rect)
215 {
216 m_container->setGeometry(rect);
217 //m_window->resize(size());
218 }
219 }
220 else
221 {
222 if(m_container)
223 {
224 m_container->setVisible(false);
225 m_container->resize(1,1);
226 }
227 }
228#endif
229 }
230 bool event(QEvent* event) override
231 {
232#if !NDEVR_SPECIAL_GRAPHICS_WINDOW
233 QEvent::Type event_type = event->type();
234 switch(event_type)
235 {
236 case QEvent::Type::Show:
237 if (m_container)
238 {
239 m_container->setVisible(true);
240#if FORCE_HIDE_WINDOW
241 if(m_window)
242 m_window->show();
243 visibleHack();
244#endif
245 }
246 break;
247 case QEvent::Type::Hide:
248 if(m_container)
249 {
250 m_container->setVisible(false);
251#if FORCE_HIDE_WINDOW
252 if(m_window)
253 m_window->hide();
254#endif
255 }
256 break;
257 default:
258 break;
259 }
260#endif
263 //return false;
264 return BaseWindowContainer::event(event);
265 }
266 void setEventForwarder(const std::function<bool(QEvent * event)>& forwarder)
267 {
268 m_event_forwarder = forwarder;
269 }
270 protected:
271 std::function<bool(QEvent * event)> m_event_forwarder;
274 QWindow* m_window;
275 QWidget* m_container;
276 };
277}
QFrame BaseWindowContainer
Definition WindowContainer.h:25
A (hacky) class for storing a window into a container. This is used if the rendering engine for a par...
Definition WindowContainer.h:37
std::function< bool(QEvent *event)> m_event_forwarder
Definition WindowContainer.h:271
QSize m_last_paint_size
Definition WindowContainer.h:273
static Qt::WindowFlags windowFlags()
Definition WindowContainer.h:58
QWidget * m_container
Definition WindowContainer.h:275
bool event(QEvent *event) override
Definition WindowContainer.h:230
void setWindow(QWindow *window)
Definition WindowContainer.h:99
void visibleHack()
Definition WindowContainer.h:138
void setWidget(QWidget *window)
Definition WindowContainer.h:81
QWindow * m_window
Definition WindowContainer.h:274
QPoint windowOffset() const
Definition WindowContainer.h:73
void paintEvent(QPaintEvent *event) override
Definition WindowContainer.h:151
virtual void updateSize()
Definition WindowContainer.h:208
WindowContainer(QWidget *parent=nullptr)
Definition WindowContainer.h:39
void resizeEvent(QResizeEvent *resize_event) override
Definition WindowContainer.h:129
QTimer * m_location_timer
Definition WindowContainer.h:272
void setEventForwarder(const std::function< bool(QEvent *event)> &forwarder)
Definition WindowContainer.h:266
Definition ACIColor.h:37