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
121int View::zOrder() const
122{
123 return 0;
124}
125
126Size View::size() const
127{
128 return geometry().size();
129}
130
131Point View::pos() const
132{
133 return geometry().topLeft();
134}
135
136Rect View::rect() const
137{
138 return Rect(Point(0, 0), size());
139}
140
141int View::x() const
142{
143 return geometry().x();
144}
145
146int View::y() const
147{
148 return geometry().y();
149}
150
151int View::height() const
152{
153 return geometry().height();
154}
155
156int View::width() const
157{
158 return geometry().width();
159}
160
161void View::move(Point pt)
162{
163 move(pt.x(), pt.y());
164}
165
166void View::resize(Size sz)
167{
168 setSize(sz.width(), sz.height());
169}
170
171void View::setSize(Size sz)
172{
173 setSize(sz.width(), sz.height());
174}
175
176void View::resize(int w, int h)
177{
178 setSize(w, h);
179}
180
182{
183 return minSize().width();
184}
185
187{
188 return minSize().height();
189}
190
192{
193 if (auto screen = d->screen())
194 return screen->size();
195
196 return {};
197}
198
200{
201 return m_controller;
202}
203
206{
207 return Core::Item::hardcodedMinimumSize;
208}
209
210bool View::is(ViewType t) const
211{
212 return int(d->m_type) & int(t);
213}
214
216{
218 return object_cast<Core::FloatingWindow *>(m_controller);
219
220 return nullptr;
221}
222
224{
226 return object_cast<Core::Group *>(m_controller);
227
228 return nullptr;
229}
230
232{
234 return object_cast<Core::TitleBar *>(m_controller);
235
236 return nullptr;
237}
238
240{
242 return object_cast<Core::TabBar *>(m_controller);
243
244 return nullptr;
245}
246
248{
250 return object_cast<Core::Stack *>(m_controller);
251
252 return nullptr;
253}
254
256{
258 return object_cast<Core::DockWidget *>(m_controller);
259
260 return nullptr;
261}
262
264{
266 return object_cast<Core::MainWindow *>(m_controller);
267
268 return nullptr;
269}
270
272{
274 return object_cast<Core::DropArea *>(m_controller);
275 }
276 return nullptr;
277}
278
280{
282 return object_cast<Core::MDILayout *>(m_controller);
283
284 return nullptr;
285}
286
288{
290 return da;
291 } else if (Core::MDILayout *mdi = asMDILayoutController()) {
292 return mdi;
293 }
294
295 return nullptr;
296}
297
298bool View::equals(const View *other) const
299{
300 return other && handle() == other->handle();
301}
302
303bool View::equals(const std::shared_ptr<View> &other) const
304{
305 if (isNull() || !other || other->isNull()) {
306 // We don't care about nullity for identity
307 return false;
308 }
309
310 return handle() == other->handle();
311}
312
313bool View::isNull() const
314{
315 return false;
316}
317
318bool View::Private::isInWindow(std::shared_ptr<Core::Window> window) const
319{
320 if (!window)
321 return false;
322
323 if (auto ourWindow = q->window())
324 return ourWindow->equals(window);
325
326 return false;
327}
328
329Size View::Private::parentSize() const
330{
331 if (auto p = q->parentView())
332 return p->size();
333 return {};
334}
335
336Rect View::Private::windowGeometry() const
337{
338 if (Core::Window::Ptr window = q->window())
339 return window->geometry();
340
341 return {};
342}
343
344void View::Private::closeRootView()
345{
346 if (auto view = q->rootView())
347 view->close();
348}
349
350Screen::Ptr View::Private::screen() const
351{
352 if (Core::Window::Ptr window = q->window())
353 return window->screen();
354
355 return nullptr;
356}
357
358void View::Private::setAboutToBeDestroyed()
359{
360 m_aboutToBeDestroyed = true;
361}
362
363bool View::Private::aboutToBeDestroyed() const
364{
365 return m_aboutToBeDestroyed;
366}
367
369{
370 KDDW_DEBUG("View::dumpDebug: controller={}, type={}, rootController={}\n", ( void * )m_controller, int(d->type()), ( void * )rootView()->controller());
371}
372
374{
375 return !m_inDtor && minSize().width() == maxSizeHint().width();
376}
377
379{
380 return !m_inDtor && minSize().height() == maxSizeHint().height();
381}
382
385{
386 auto p = view->asWrapper();
387 while (p) {
388 if (p->is(type))
389 return p->controller();
390
391 // Ignore QObject hierarchies spanning though multiple windows
392 if (p->isRootView())
393 return nullptr;
394
395 p = p->parentView();
396 }
397
398 return nullptr;
399}
400
401Controller *View::Private::firstParentOfType(ViewType type) const
402{
403 return View::firstParentOfType(const_cast<View *>(q), type);
404}
405
406void View::Private::requestClose(CloseEvent *e)
407{
408 closeRequested.emit(e);
409}
410
411Rect View::Private::globalGeometry() const
412{
413 Rect geo = q->geometry();
414 if (!q->isRootView())
415 geo.moveTopLeft(q->mapToGlobal(Point(0, 0)));
416 return geo;
417}
418
420{
421 // Only qtwidgets need this
422 KDDW_ERROR("Shouldn't be called on this platform");
423 std::abort();
424}
425
426std::shared_ptr<Core::Window> View::Private::transientWindow() const
427{
428 if (auto w = q->window())
429 return w->transientParent();
430
431 return {};
432}
433
434bool View::onResize(int w, int h)
435{
436 d->resized.emit(Size(w, h));
437 return false;
438}
439
440bool View::onResize(Size sz)
441{
442 return onResize(sz.width(), sz.height());
443}
444
446bool View::equals(const View *one, const View *two)
447{
448 if ((one && !two) || (!one && two))
449 return false;
450
451 if (!one && !two)
452 return true;
453
454 return one->equals(two);
455}
456
458{
459 d->m_viewEventFilters.push_back(filter);
460}
461
463{
464 d->m_viewEventFilters.erase(
465 std::remove(d->m_viewEventFilters.begin(), d->m_viewEventFilters.end(), filter),
466 d->m_viewEventFilters.end());
467}
468
470{
471 for (Core::EventFilterInterface *filter : std::as_const(d->m_viewEventFilters)) {
472 if (ev->type() == Event::Move) {
473 if (filter->onMoveEvent(this))
474 return true;
475 } else if (auto me = mouseEvent(ev)) {
476 if (filter->onMouseEvent(this, me))
477 return true;
478
479 switch (ev->type()) {
480 case Event::MouseButtonPress:
481 if (filter->onMouseButtonPress(this, me))
482 return true;
483 break;
484 case Event::MouseButtonRelease:
485 if (filter->onMouseButtonRelease(this, me))
486 return true;
487 break;
488 case Event::MouseMove:
489 if (filter->onMouseButtonMove(this, me))
490 return true;
491 break;
492 case Event::MouseButtonDblClick:
493 if (filter->onMouseDoubleClick(this, me))
494 return true;
495 break;
496 default:
497 break;
498 }
499 }
500 }
501
502 return false;
503}
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:260
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)
virtual Size maxSizeHint() const =0
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 int zOrder() const
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 and only relevant for MDI mode.
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