KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtwidgets/views/ViewWrapper.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 "ViewWrapper_p.h"
13#include "core/View_p.h"
26#include "qtwidgets/Window_p.h"
28
29#include "kddockwidgets/core/MDILayout.h"
30#include "kddockwidgets/core/DropArea.h"
31#include "kddockwidgets/core/SideBar.h"
32
33#include <QWindow>
34#include <QDebug>
35
36using namespace KDDockWidgets;
37using namespace KDDockWidgets::QtWidgets;
38
40{
41 // KDDW deals in views, but sometimes we might get a native type like QWidget, for example if
42 // you call someview->window(). This function let's us retrieve the actual controller of the
43 // stray QWidget.
44
45 for (int i = int(Core::ViewType::FIRST); i <= int(Core::ViewType::LAST); i *= 2) {
46 // Using a for+switch pattern so that compiler reminds us if new enumerators are added to
47 // enum
48 switch (Core::ViewType(i)) {
49 case Core::ViewType::Frame:
50 if (auto view = qobject_cast<Group *>(widget))
51 return view->controller();
52 break;
53 case Core::ViewType::TitleBar:
54 if (auto view = qobject_cast<TitleBar *>(widget))
55 return view->controller();
56 break;
57 case Core::ViewType::TabBar:
58 if (auto view = qobject_cast<TabBar *>(widget))
59 return view->controller();
60 break;
61 case Core::ViewType::Stack:
62 if (auto view = qobject_cast<Stack *>(widget))
63 return view->controller();
64 break;
65 case Core::ViewType::FloatingWindow:
66 if (auto view = qobject_cast<FloatingWindow *>(widget))
67 return view->controller();
68 break;
69 case Core::ViewType::Separator:
70 if (auto view = qobject_cast<Separator *>(widget))
71 return view->controller();
72 break;
73 case Core::ViewType::DockWidget:
74 if (auto view = qobject_cast<DockWidget *>(widget))
75 return view->controller();
76 break;
77 case Core::ViewType::DropArea:
78 if (auto view = qobject_cast<DropArea *>(widget))
79 return view->controller();
80 break;
81 case Core::ViewType::MDILayout:
82 if (auto view = qobject_cast<MDILayout *>(widget))
83 return view->controller();
84 break;
85 case Core::ViewType::SideBar:
86 if (auto view = qobject_cast<SideBar *>(widget))
87 return view->controller();
88 break;
89 case Core::ViewType::MainWindow:
90 if (auto view = qobject_cast<MainWindow *>(widget))
91 return view->controller();
92 break;
93 case Core::ViewType::RubberBand:
94 case Core::ViewType::LayoutItem:
95 case Core::ViewType::ViewWrapper:
96 case Core::ViewType::DropAreaIndicatorOverlay:
97 case Core::ViewType::None:
98 // skip internal types
99 continue;
100 break;
101 }
102 }
103
104 return nullptr;
105}
106
107/*static*/
108std::shared_ptr<Core::View> ViewWrapper::create(QObject *widget)
109{
110 return create(qobject_cast<QWidget *>(widget));
111}
112
113/*static*/
114std::shared_ptr<Core::View> ViewWrapper::create(QWidget *widget)
115{
116 if (!widget)
117 return {};
118
119 auto wrapper = new ViewWrapper(widget);
120 auto sharedptr = std::shared_ptr<View>(wrapper);
121 wrapper->d->m_thisWeakPtr = sharedptr;
122
123 return sharedptr;
124}
125
126ViewWrapper::ViewWrapper(QObject *widget)
127 : ViewWrapper(qobject_cast<QWidget *>(widget))
128{
129}
130
131ViewWrapper::ViewWrapper(QWidget *widget)
132 : QtCommon::ViewWrapper(controllerForWidget(widget), widget)
133 , m_widget(widget)
134{
135}
136
137QRect ViewWrapper::geometry() const
138{
139 return m_widget->geometry();
140}
141
142QPoint ViewWrapper::mapToGlobal(QPoint localPt) const
143{
144 return m_widget->mapToGlobal(localPt);
145}
146
147QPoint ViewWrapper::mapFromGlobal(QPoint globalPt) const
148{
149 return m_widget->mapFromGlobal(globalPt);
150}
151
152void ViewWrapper::setGeometry(QRect rect)
153{
154 m_widget->setGeometry(rect);
155}
156
157std::shared_ptr<Core::Window> ViewWrapper::window() const
158{
159 if (m_widget->window()->windowHandle())
160 return std::shared_ptr<Core::Window>(new Window(m_widget->window()));
161
162 return nullptr;
163}
164
165bool ViewWrapper::isRootView() const
166{
167 return m_widget->isWindow();
168}
169
170void ViewWrapper::setVisible(bool is)
171{
172 m_widget->setVisible(is);
173}
174
175bool ViewWrapper::isVisible() const
176{
177 return m_widget->isVisible();
178}
179
180bool ViewWrapper::isExplicitlyHidden() const
181{
182 return m_widget->isHidden();
183}
184
185void ViewWrapper::move(int x, int y)
186{
187 m_widget->move(x, y);
188}
189
190void ViewWrapper::activateWindow()
191{
192 m_widget->activateWindow();
193}
194
195bool ViewWrapper::isMaximized() const
196{
197 return m_widget->isMaximized();
198}
199
200bool ViewWrapper::isMinimized() const
201{
202 return m_widget->isMinimized();
203}
204
205void ViewWrapper::setSize(int x, int y)
206{
207 m_widget->resize(x, y);
208}
209
210bool ViewWrapper::is(Core::ViewType t) const
211{
212 if (t == Core::ViewType::ViewWrapper)
213 return true;
214
215 switch (t) {
216
217 case Core::ViewType::Frame:
218 return qobject_cast<QtWidgets::Group *>(m_widget);
219 case Core::ViewType::TitleBar:
220 return qobject_cast<QtWidgets::TitleBar *>(m_widget);
221 case Core::ViewType::TabBar:
222 return qobject_cast<QtWidgets::TabBar *>(m_widget);
223 case Core::ViewType::Stack:
224 return qobject_cast<QtWidgets::Stack *>(m_widget);
225 case Core::ViewType::FloatingWindow:
226 return qobject_cast<QtWidgets::FloatingWindow *>(m_widget);
227 case Core::ViewType::Separator:
228 return qobject_cast<QtWidgets::Separator *>(m_widget);
229 case Core::ViewType::DockWidget:
230 return qobject_cast<QtWidgets::DockWidget *>(m_widget);
231 case Core::ViewType::SideBar:
232 return qobject_cast<QtWidgets::SideBar *>(m_widget);
233 case Core::ViewType::MainWindow:
234 return qobject_cast<QtWidgets::MainWindow *>(m_widget);
235 case Core::ViewType::DropArea:
236 return qobject_cast<QtWidgets::DropArea *>(m_widget);
237 case Core::ViewType::MDILayout:
238 return qobject_cast<MDILayout *>(m_widget);
239 case Core::ViewType::RubberBand:
240 return qobject_cast<RubberBand *>(m_widget);
241 case Core::ViewType::LayoutItem:
242 case Core::ViewType::None:
243 case Core::ViewType::DropAreaIndicatorOverlay:
244 qWarning() << Q_FUNC_INFO << "These are framework internals that are not wrapped";
245 return false;
246 case Core::ViewType::ViewWrapper:
247 return true;
248 }
249
250 qWarning() << Q_FUNC_INFO << "Unknown type" << static_cast<int>(t);
251 return false;
252}
253
254std::shared_ptr<Core::View> ViewWrapper::rootView() const
255{
256 if (auto w = m_widget->window())
257 return std::shared_ptr<Core::View>(new ViewWrapper(w));
258
259 return {};
260}
261
262std::shared_ptr<Core::View> ViewWrapper::parentView() const
263{
264 if (auto p = m_widget->parentWidget())
265 return std::shared_ptr<Core::View>(new ViewWrapper(p));
266
267 return {};
268}
269
270std::shared_ptr<Core::View> ViewWrapper::childViewAt(QPoint localPos) const
271{
272 if (QWidget *child = m_widget->childAt(localPos))
273 return std::shared_ptr<Core::View>(new ViewWrapper(child));
274
275 return {};
276}
277
278void ViewWrapper::grabMouse()
279{
280 m_widget->grabMouse();
281}
282
283void ViewWrapper::releaseMouse()
284{
285 m_widget->releaseMouse();
286}
287
288void ViewWrapper::setFocus(Qt::FocusReason reason)
289{
290 m_widget->setFocus(reason);
291}
292
293QString ViewWrapper::viewName() const
294{
295 return m_widget->QWidget::objectName();
296}
297
298bool ViewWrapper::isNull() const
299{
300 return m_widget.data() == nullptr;
301}
302
303QWidget *ViewWrapper::widget() const
304{
305 return m_widget;
306}
307
308void ViewWrapper::setWindowTitle(const QString &title)
309{
310 m_widget->setWindowTitle(title);
311}
312
313QPoint ViewWrapper::mapTo(Core::View *someAncestor, QPoint pos) const
314{
315 return m_widget->mapTo(View_qt::asQWidget(someAncestor), pos);
316}
317
318bool ViewWrapper::hasAttribute(Qt::WidgetAttribute attr) const
319{
320 return m_widget->testAttribute(attr);
321}
322
323void ViewWrapper::setCursor(Qt::CursorShape cursor)
324{
325 m_widget->setCursor(cursor);
326}
327
328QSize ViewWrapper::minSize() const
329{
330 const int minW = m_widget->minimumWidth() > 0 ? m_widget->minimumWidth()
331 : m_widget->minimumSizeHint().width();
332
333 const int minH = m_widget->minimumHeight() > 0 ? m_widget->minimumHeight()
334 : m_widget->minimumSizeHint().height();
335
336 return QSize(minW, minH).expandedTo(View::hardcodedMinimumSize());
337}
338
339QVector<std::shared_ptr<Core::View>> ViewWrapper::childViews() const
340{
342}
343
344void ViewWrapper::setParent(Core::View *parent)
345{
347}
348
349bool ViewWrapper::close()
350{
351 return m_widget->close();
352}
353
354Qt::FocusPolicy ViewWrapper::focusPolicy() const
355{
356 return m_widget->focusPolicy();
357}
358
359void ViewWrapper::setFocusPolicy(Qt::FocusPolicy policy)
360{
361 m_widget->setFocusPolicy(policy);
362}
363
364bool ViewWrapper::hasFocus() const
365{
366 return m_widget->hasFocus();
367}
368
369QSize ViewWrapper::maxSizeHint() const
370{
371 return m_widget->maximumSize();
372}
373
374void ViewWrapper::raiseAndActivate()
375{
377}
static QVector< std::shared_ptr< Core::View > > childViewsFor(const QWidget *parent)
static void setParentFor(QWidget *widget, Core::View *parent)
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.
QSize expandedTo(const QSize &otherSize) const const
int width() const const
QChar * data()
CursorShape
FocusPolicy
FocusReason
WidgetAttribute
Represents a dock widget.
A MultiSplitter with support for drop indicators when hovering over.
QMainWindow sub-class to enable KDDockWidgets support.
static Core::Controller * controllerForWidget(QWidget *widget)
QWidget * childAt(int x, int y) const const
void setWindowTitle(const QString &)

© 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