KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
core/View.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: SĂ©rgio Martins <sergio.martins@kdab.com>
6
7 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
8
9 Contact KDAB at <info@kdab.com> for commercial licensing options.
10*/
11
12#include "core/View.h"
13#include "core/View_p.h"
14#include "core/Utils_p.h"
15#include "core/layouting/Item_p.h"
17#include "core/FloatingWindow.h"
18#include "core/Group.h"
19#include "core/Stack.h"
20#include "core/TitleBar.h"
21#include "core/TabBar.h"
22#include "core/MainWindow.h"
23#include "core/DropArea.h"
24#include "core/MDILayout.h"
25#include "core/Logging_p.h"
26#include "core/Platform.h"
27#include "core/Window_p.h"
28
29#include <iostream>
30#include <cstdlib>
31#include <utility>
32
33using namespace KDDockWidgets;
34using namespace KDDockWidgets::Core;
35
36namespace KDDockWidgets {
37static qint64 s_nextId = 1;
38
40{
41 if (controller)
42 return controller;
43
44 if (type == ViewType::ViewWrapper)
45 return new Controller(ViewType::ViewWrapper, view);
46
47 return new Controller(ViewType::None, view);
48}
49
50}
51
52View::View(Controller *controller, ViewType type)
53 : d(new Private(this, QString::number(KDDockWidgets::s_nextId++), type))
54 , m_controller(maybeCreateController(controller, type, this))
55{
56}
57
59{
60 m_inDtor = true;
61 d->beingDestroyed.emit();
62
64 // Views should be deleted via View::free()!
65 // However some of our deletes are coming from widgets parent destroying their children
66 // But we want the controllers to drive things instead. For now detect the view destruction
67 // and destroy its controller, which was the old behaviour.
68 delete m_controller;
69 }
70
71#ifdef KDDW_FRONTEND_FLUTTER
72 const auto children = m_childViews;
73 for (auto child : children)
74 delete child;
75
76 m_childViews.clear();
77#endif
78
79 delete d;
80}
81
82QString View::Private::id() const
83{
84 return m_id;
85}
86
87ViewType View::Private::type() const
88{
89 return m_type;
90}
91
92void View::Private::free()
93{
94 if (m_freed) {
95 KDDW_ERROR("Free already called");
96 return;
97 }
98
99 m_freed = true;
100 delete q;
101}
102
104{
105}
106
107bool View::Private::freed() const
108{
109 return m_freed;
110}
111
112bool View::inDtor() const
113{
114 return m_inDtor;
115}
116
118{
119}
120
121Size View::size() const
122{
123 return geometry().size();
124}
125
126Point View::pos() const
127{
128 return geometry().topLeft();
129}
130
131Rect View::rect() const
132{
133 return Rect(Point(0, 0), size());
134}
135
136int View::x() const
137{
138 return geometry().x();
139}
140
141int View::y() const
142{
143 return geometry().y();
144}
145
146int View::height() const
147{
148 return geometry().height();
149}
150
151int View::width() const
152{
153 return geometry().width();
154}
155
156void View::move(Point pt)
157{
158 move(pt.x(), pt.y());
159}
160
161void View::resize(Size sz)
162{
163 setSize(sz.width(), sz.height());
164}
165
166void View::setSize(Size sz)
167{
168 setSize(sz.width(), sz.height());
169}
170
171void View::resize(int w, int h)
172{
173 setSize(w, h);
174}
175
177{
178 return minSize().width();
179}
180
182{
183 return minSize().height();
184}
185
187{
188 if (auto screen = d->screen())
189 return screen->size();
190
191 return {};
192}
193
195{
196 return m_controller;
197}
198
201{
202 return Core::Item::hardcodedMinimumSize;
203}
204
205bool View::is(ViewType t) const
206{
207 return int(d->m_type) & int(t);
208}
209
211{
213 return object_cast<Core::FloatingWindow *>(m_controller);
214
215 return nullptr;
216}
217
219{
221 return object_cast<Core::Group *>(m_controller);
222
223 return nullptr;
224}
225
227{
229 return object_cast<Core::TitleBar *>(m_controller);
230
231 return nullptr;
232}
233
235{
237 return object_cast<Core::TabBar *>(m_controller);
238
239 return nullptr;
240}
241
243{
245 return object_cast<Core::Stack *>(m_controller);
246
247 return nullptr;
248}
249
251{
253 return object_cast<Core::DockWidget *>(m_controller);
254
255 return nullptr;
256}
257
259{
261 return object_cast<Core::MainWindow *>(m_controller);
262
263 return nullptr;
264}
265
267{
269 return object_cast<Core::DropArea *>(m_controller);
270 }
271 return nullptr;
272}
273
275{
277 return object_cast<Core::MDILayout *>(m_controller);
278
279 return nullptr;
280}
281
283{
285 return da;
286 } else if (Core::MDILayout *mdi = asMDILayoutController()) {
287 return mdi;
288 }
289
290 return nullptr;
291}
292
293bool View::equals(const View *other) const
294{
295 return other && handle() == other->handle();
296}
297
298bool View::equals(const std::shared_ptr<View> &other) const
299{
300 if (isNull() || !other || other->isNull()) {
301 // We don't care about nullity for identity
302 return false;
303 }
304
305 return handle() == other->handle();
306}
307
308bool View::isNull() const
309{
310 return false;
311}
312
313bool View::Private::isInWindow(std::shared_ptr<Core::Window> window) const
314{
315 if (!window)
316 return false;
317
318 if (auto ourWindow = q->window())
319 return ourWindow->equals(window);
320
321 return false;
322}
323
324Size View::Private::parentSize() const
325{
326 if (auto p = q->parentView())
327 return p->size();
328 return {};
329}
330
331Rect View::Private::windowGeometry() const
332{
333 if (Core::Window::Ptr window = q->window())
334 return window->geometry();
335
336 return {};
337}
338
339void View::Private::closeRootView()
340{
341 if (auto view = q->rootView())
342 view->close();
343}
344
345Screen::Ptr View::Private::screen() const
346{
347 if (Core::Window::Ptr window = q->window())
348 return window->screen();
349
350 return nullptr;
351}
352
353void View::Private::setAboutToBeDestroyed()
354{
355 m_aboutToBeDestroyed = true;
356}
357
358bool View::Private::aboutToBeDestroyed() const
359{
360 return m_aboutToBeDestroyed;
361}
362
364{
365 KDDW_DEBUG("View::dumpDebug: controller={}, type={}, rootController={}\n", ( void * )m_controller, int(d->type()), ( void * )rootView()->controller());
366}
367
370{
371 auto p = view->asWrapper();
372 while (p) {
373 if (p->is(type))
374 return p->controller();
375
376 // Ignore QObject hierarchies spanning though multiple windows
377 if (p->isRootView())
378 return nullptr;
379
380 p = p->parentView();
381 }
382
383 return nullptr;
384}
385
386Controller *View::Private::firstParentOfType(ViewType type) const
387{
388 return View::firstParentOfType(const_cast<View *>(q), type);
389}
390
391void View::Private::requestClose(CloseEvent *e)
392{
393 closeRequested.emit(e);
394}
395
396Rect View::Private::globalGeometry() const
397{
398 Rect geo = q->geometry();
399 if (!q->isRootView())
400 geo.moveTopLeft(q->mapToGlobal(Point(0, 0)));
401 return geo;
402}
403
405{
406 // Only qtwidgets need this
407 KDDW_ERROR("Shouldn't be called on this platform");
408 std::abort();
409}
410
411std::shared_ptr<Core::Window> View::Private::transientWindow() const
412{
413 if (auto w = q->window())
414 return w->transientParent();
415
416 return {};
417}
418
419bool View::onResize(int w, int h)
420{
421 d->resized.emit(Size(w, h));
422 return false;
423}
424
425bool View::onResize(Size sz)
426{
427 return onResize(sz.width(), sz.height());
428}
429
431bool View::equals(const View *one, const View *two)
432{
433 if ((one && !two) || (!one && two))
434 return false;
435
436 if (!one && !two)
437 return true;
438
439 return one->equals(two);
440}
441
443{
444 d->m_viewEventFilters.push_back(filter);
445}
446
448{
449 d->m_viewEventFilters.erase(
450 std::remove(d->m_viewEventFilters.begin(), d->m_viewEventFilters.end(), filter),
451 d->m_viewEventFilters.end());
452}
453
455{
456 for (Core::EventFilterInterface *filter : std::as_const(d->m_viewEventFilters)) {
457 if (ev->type() == Event::Move) {
458 if (filter->onMoveEvent(this))
459 return true;
460 } else if (auto me = mouseEvent(ev)) {
461 if (filter->onMouseEvent(this, me))
462 return true;
463
464 switch (ev->type()) {
465 case Event::MouseButtonPress:
466 if (filter->onMouseButtonPress(this, me))
467 return true;
468 break;
469 case Event::MouseButtonRelease:
470 if (filter->onMouseButtonRelease(this, me))
471 return true;
472 break;
473 case Event::MouseMove:
474 if (filter->onMouseButtonMove(this, me))
475 return true;
476 break;
477 case Event::MouseButtonDblClick:
478 if (filter->onMouseDoubleClick(this, me))
479 return true;
480 break;
481 default:
482 break;
483 }
484 }
485 }
486
487 return false;
488}
bool is(ViewType) const
Returns whether this controller is of the specified type.
The DockWidget base-class. DockWidget and Core::DockWidget are only split in two so we can share some...
The widget (QWidget or QQuickItem) which holds a layout of dock widgets.
Definition Layout.h:57
The MDILayout class implements a layout suitable for MDI style docking. Where dock widgets are free t...
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
View(Controller *controller, ViewType)
Definition core/View.cpp:52
virtual void setSize(int width, int height)=0
bool equals(const View *other) const
Returns whether this view represents the same GUI element as the other.
Controller *const m_controller
Definition core/View.h:253
virtual Size minSize() const =0
Core::TabBar * asTabBarController() const
virtual Rect geometry() const =0
virtual bool isNull() const
Returns whether the gui item represented by this view was already deleted Usually false,...
Core::TitleBar * asTitleBarController() const
void installViewEventFilter(EventFilterInterface *)
Installs an event filter in this view to intercept the event it receives Analogue to QObject::install...
virtual std::shared_ptr< View > rootView() const =0
Returns the top-level gui element which this view is inside It's the root view of the window.
Controller * controller() const
Returns this view's controller.
Size screenSize() const
Returns the size of the screen that this view belongs to.
Core::MDILayout * asMDILayoutController() const
virtual bool onResize(int h, int w)
void dumpDebug()
Prints some debug to stderr.
Core::Stack * asStackController() const
Core::FloatingWindow * asFloatingWindowController() const
asFooController() are deprecated. Use asController<T>() instead
virtual bool is(ViewType) const
Returns whether the view is of the specified type Virtual so it can be overridden by ViewWrapper....
virtual void createPlatformWindow()
static Size hardcodedMinimumSize()
The minimum minimum size a dock widget can have.
Core::Group * asGroupController() const
bool inDtor() const
Returns whether the DTOR is currently running. freed() might be true while inDtor false,...
virtual void move(int x, int y)=0
Core::DropArea * asDropAreaController() const
Core::DockWidget * asDockWidgetController() const
void removeViewEventFilter(EventFilterInterface *)
Removes the event filter.
Core::MainWindow * asMainWindowController() const
virtual std::shared_ptr< View > asWrapper()=0
Returns this view, but as a wrapper.
static Controller * firstParentOfType(View *view, ViewType)
Returns the controller of the first parent view of the specified type Goes up the view hierarchy chai...
Core::Layout * asLayout() const
virtual void setZOrder(int)
Sets the z order Not supported on all platforms.
virtual HANDLE handle() const =0
Returns a handle for the GUI element This value only makes sense to the frontend. For example,...
bool deliverViewEventToFilters(Event *e)
Delivers mouse events and such to event filters.
A MultiSplitter with support for drop indicators when hovering over.
The MainWindow base-class that's shared between QtWidgets and QtQuick stack.
ViewType
Each View type also has a specific Controller associated with, except for ViewType::None.
Definition Controller.h:26
Class to abstract QAction, so code still works with QtQuick and Flutter.
static qint64 s_nextId
Definition core/View.cpp:37
Controller * maybeCreateController(Controller *controller, ViewType type, View *view)
Definition core/View.cpp:39

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDDockWidgets
Advanced Dock Widget Framework for Qt
https://www.kdab.com/development-resources/qt-tools/kddockwidgets/
Generated by doxygen 1.9.8