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