KDDockWidgets API Documentation 2.1
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::Group:
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 switch (t) {
213 case Core::ViewType::Group:
214 return qobject_cast<QtWidgets::Group *>(m_widget);
215 case Core::ViewType::TitleBar:
216 return qobject_cast<QtWidgets::TitleBar *>(m_widget);
217 case Core::ViewType::TabBar:
218 return qobject_cast<QtWidgets::TabBar *>(m_widget);
219 case Core::ViewType::Stack:
220 return qobject_cast<QtWidgets::Stack *>(m_widget);
221 case Core::ViewType::FloatingWindow:
222 return qobject_cast<QtWidgets::FloatingWindow *>(m_widget);
223 case Core::ViewType::Separator:
224 return qobject_cast<QtWidgets::Separator *>(m_widget);
225 case Core::ViewType::DockWidget:
226 return qobject_cast<QtWidgets::DockWidget *>(m_widget);
227 case Core::ViewType::SideBar:
228 return qobject_cast<QtWidgets::SideBar *>(m_widget);
229 case Core::ViewType::MainWindow:
230 return qobject_cast<QtWidgets::MainWindow *>(m_widget);
231 case Core::ViewType::DropArea:
232 return qobject_cast<QtWidgets::DropArea *>(m_widget);
233 case Core::ViewType::MDILayout:
234 return qobject_cast<MDILayout *>(m_widget);
235 case Core::ViewType::RubberBand:
236 return qobject_cast<RubberBand *>(m_widget);
237 case Core::ViewType::LayoutItem:
238 case Core::ViewType::None:
239 case Core::ViewType::DropAreaIndicatorOverlay:
240 qWarning() << Q_FUNC_INFO << "These are framework internals that are not wrapped";
241 return false;
242 case Core::ViewType::ViewWrapper:
243 return true;
244 }
245
246 qWarning() << Q_FUNC_INFO << "Unknown type" << static_cast<int>(t);
247 return false;
248}
249
250std::shared_ptr<Core::View> ViewWrapper::rootView() const
251{
252 if (auto w = m_widget->window())
253 return std::shared_ptr<Core::View>(new ViewWrapper(w));
254
255 return {};
256}
257
258std::shared_ptr<Core::View> ViewWrapper::parentView() const
259{
260 if (auto p = m_widget->parentWidget())
261 return std::shared_ptr<Core::View>(new ViewWrapper(p));
262
263 return {};
264}
265
266std::shared_ptr<Core::View> ViewWrapper::childViewAt(QPoint localPos) const
267{
268 if (QWidget *child = m_widget->childAt(localPos))
269 return std::shared_ptr<Core::View>(new ViewWrapper(child));
270
271 return {};
272}
273
274void ViewWrapper::grabMouse()
275{
276 m_widget->grabMouse();
277}
278
279void ViewWrapper::releaseMouse()
280{
281 m_widget->releaseMouse();
282}
283
284void ViewWrapper::setFocus(Qt::FocusReason reason)
285{
286 m_widget->setFocus(reason);
287}
288
289QString ViewWrapper::viewName() const
290{
291 return m_widget->QWidget::objectName();
292}
293
294bool ViewWrapper::isNull() const
295{
296 return m_widget.data() == nullptr;
297}
298
299QWidget *ViewWrapper::widget() const
300{
301 return m_widget;
302}
303
304void ViewWrapper::setWindowTitle(const QString &title)
305{
306 m_widget->setWindowTitle(title);
307}
308
309QPoint ViewWrapper::mapTo(Core::View *someAncestor, QPoint pos) const
310{
311 return m_widget->mapTo(View_qt::asQWidget(someAncestor), pos);
312}
313
314bool ViewWrapper::hasAttribute(Qt::WidgetAttribute attr) const
315{
316 return m_widget->testAttribute(attr);
317}
318
319void ViewWrapper::setCursor(Qt::CursorShape cursor)
320{
321 m_widget->setCursor(cursor);
322}
323
324QSize ViewWrapper::minSize() const
325{
326 const int minW = m_widget->minimumWidth() > 0 ? m_widget->minimumWidth()
327 : m_widget->minimumSizeHint().width();
328
329 const int minH = m_widget->minimumHeight() > 0 ? m_widget->minimumHeight()
330 : m_widget->minimumSizeHint().height();
331
332 return QSize(minW, minH).expandedTo(View::hardcodedMinimumSize());
333}
334
335QVector<std::shared_ptr<Core::View>> ViewWrapper::childViews() const
336{
338}
339
340void ViewWrapper::setParent(Core::View *parent)
341{
343}
344
345bool ViewWrapper::close()
346{
347 return m_widget->close();
348}
349
350Qt::FocusPolicy ViewWrapper::focusPolicy() const
351{
352 return m_widget->focusPolicy();
353}
354
355void ViewWrapper::setFocusPolicy(Qt::FocusPolicy policy)
356{
357 m_widget->setFocusPolicy(policy);
358}
359
360bool ViewWrapper::hasFocus() const
361{
362 return m_widget->hasFocus();
363}
364
365QSize ViewWrapper::maxSizeHint() const
366{
367 return m_widget->maximumSize();
368}
369
370void ViewWrapper::raiseAndActivate()
371{
373}
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