KDDockWidgets API Documentation  1.5
FrameQuick.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KDDockWidgets.
3 
4  SPDX-FileCopyrightText: 2019-2022 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 
19 #include "FrameQuick_p.h"
20 #include "Config.h"
21 #include "FrameworkWidgetFactory.h"
22 #include "TabWidgetQuick_p.h"
23 #include "DockWidgetQuick.h"
24 #include "../DockWidgetBase_p.h"
25 #include "../WidgetResizeHandler_p.h"
26 
27 #include <QDebug>
28 
29 using namespace KDDockWidgets;
30 
31 FrameQuick::FrameQuick(QWidgetAdapter *parent, FrameOptions options, int userType)
32  : Frame(parent, options, userType)
33 {
34  connect(m_tabWidget->asWidget(), SIGNAL(countChanged()),
35  this, SLOT(updateConstriants()));
36 
37  connect(m_tabWidget->asWidget(), SIGNAL(currentDockWidgetChanged(KDDockWidgets::DockWidgetBase*)),
38  this, SIGNAL(currentDockWidgetChanged(KDDockWidgets::DockWidgetBase*)));
39 
40  connect(this, &QWidgetAdapter::geometryUpdated, this, &Frame::layoutInvalidated);
41 
42  connect(this, &QWidgetAdapter::itemGeometryChanged, this, [this] {
43  for (auto dw : dockWidgets()) {
44  Q_EMIT static_cast<DockWidgetQuick *>(dw)->frameGeometryChanged(QWidgetAdapter::geometry());
45  }
46  });
47 
48  QQmlComponent component(Config::self().qmlEngine(),
49  Config::self().frameworkWidgetFactory()->frameFilename());
50 
51  m_visualItem = static_cast<QQuickItem *>(component.create());
52 
53  if (!m_visualItem) {
54  qWarning() << Q_FUNC_INFO << "Failed to create item" << component.errorString();
55  return;
56  }
57 
58  m_visualItem->setProperty("frameCpp", QVariant::fromValue(this));
59  m_visualItem->setParentItem(this);
60  m_visualItem->setParent(this);
61 }
62 
63 FrameQuick::~FrameQuick()
64 {
65  {
66  const DockWidgetBase::List docks = dockWidgets();
67 
68  // The QML item must be deleted with deleteLater(), has we might be currently with its mouse
69  // handler in the stack. QML doesn't support it being deleted in that case.
70  // So unparent it and deleteLater().
71  m_visualItem->setParent(nullptr);
72  m_visualItem->deleteLater();
73 
74  qDeleteAll(docks);
75  }
76 }
77 
78 void FrameQuick::updateConstriants()
79 {
80  onDockWidgetCountChanged();
81 
82  // QtQuick doesn't have layouts, so we need to do constraint propagation manually
83 
84  setProperty("kddockwidgets_min_size", minimumSize());
85  setProperty("kddockwidgets_max_size", maximumSize());
86 
87  Q_EMIT layoutInvalidated();
88 }
89 
90 void FrameQuick::removeWidget_impl(DockWidgetBase *dw)
91 {
92  m_tabWidget->removeDockWidget(dw);
93  disconnect(m_connections.take(dw));
94 }
95 
96 int FrameQuick::indexOfDockWidget_impl(const DockWidgetBase *dw)
97 {
98  return m_tabWidget->indexOfDockWidget(dw);
99 }
100 
101 int FrameQuick::currentIndex_impl() const
102 {
103  return m_tabWidget->currentIndex();
104 }
105 
106 void FrameQuick::setCurrentTabIndex_impl(int index)
107 {
108  setCurrentDockWidget_impl(dockWidgetAt(index));
109 }
110 
111 void FrameQuick::setCurrentDockWidget_impl(DockWidgetBase *dw)
112 {
113  m_tabWidget->TabWidget::setCurrentDockWidget(dw);
114 }
115 
116 void FrameQuick::insertDockWidget_impl(DockWidgetBase *dw, int index)
117 {
118  QPointer<Frame> oldFrame = dw->d->frame();
119  if (m_tabWidget->insertDockWidget(index, dw, {}, {})) {
120  dw->setParent(m_stackLayout);
121 
122  QMetaObject::Connection conn = connect(dw, &DockWidgetBase::parentChanged, this, [dw, this] {
123  if (dw->parent() != m_stackLayout)
124  removeWidget_impl(dw);
125  });
126 
127  m_connections[dw] = conn;
128  setCurrentDockWidget_impl(dw);
129 
130  if (oldFrame && oldFrame->beingDeletedLater()) {
131  // give it a push and delete it immediately.
132  // Having too many deleteLater() puts us in an inconsistent state. For example if LayoutSaver::saveState()
133  // would to be called while the Frame hadn't been deleted yet it would count with that frame unless hacks.
134  // Also the unit-tests are full of waitForDeleted() due to deleteLater.
135 
136  // Ideally we would just remove the deleteLater from frame.cpp, but QTabWidget::insertTab()
137  // would crash, as it accesses the old tab-widget we're stealing from
138 
139  delete oldFrame;
140  }
141  }
142 }
143 
144 DockWidgetBase *FrameQuick::dockWidgetAt_impl(int index) const
145 {
146  return m_tabWidget->dockwidgetAt(index);
147 }
148 
149 DockWidgetBase *FrameQuick::currentDockWidget_impl() const
150 {
151  return m_tabWidget->currentDockWidget();
152 }
153 
154 void FrameQuick::renameTab(int, const QString &)
155 {
156  // Not needed for QtQuick. Our model reacts to titleChanged()
157 }
158 
159 
160 void FrameQuick::changeTabIcon(int index, const QIcon &)
161 {
162  Q_UNUSED(index);
163  qDebug() << Q_FUNC_INFO << "Not implemented";
164 }
165 
166 void FrameQuick::setStackLayout(QQuickItem *stackLayout)
167 {
168  if (m_stackLayout || !stackLayout) {
169  qWarning() << Q_FUNC_INFO << "Shouldn't happen";
170  return;
171  }
172 
173  m_stackLayout = stackLayout;
174 }
175 
176 QSize FrameQuick::minimumSize() const
177 {
178  const QSize contentsSize = dockWidgetsMinSize();
179  return contentsSize + QSize(0, nonContentsHeight());
180 }
181 
182 QSize FrameQuick::maximumSize() const
183 {
184  return Frame::maximumSize();
185 }
186 
187 QObject *FrameQuick::tabWidgetObj() const
188 {
189  return m_tabWidget->asWidget();
190 }
191 
192 TabWidget *FrameQuick::tabWidget() const
193 {
194  return m_tabWidget;
195 }
196 
197 QQuickItem *FrameQuick::visualItem() const
198 {
199  return m_visualItem;
200 }
201 
202 int FrameQuick::nonContentsHeight() const
203 {
204  return m_visualItem->property("nonContentsHeight").toInt();
205 }
QPointer
DockWidgetQuick.h
Represents a dock widget.
QVariant::fromValue
QVariant fromValue(const T &value)
QMetaObject::Connection
QSize
KDDockWidgets::DockWidgetQuick
Represents a dock widget.
Definition: DockWidgetQuick.h:40
QObject
QString
QVariant::toInt
int toInt(bool *ok) const const
QIcon
Config.h
Application-wide config to tune certain behaviours of the framework.
KDDockWidgets::DockWidgetBase
The DockWidget base-class. DockWidget and DockWidgetBase are only split in two so we can share some c...
Definition: DockWidgetBase.h:61
KDDockWidgets
Definition: Config.cpp:36
QVector
KDDockWidgets::Config::self
static Config & self()
returns the singleton Config instance
Definition: Config.cpp:82
FrameworkWidgetFactory.h
A factory class for allowing the user to customize some internal widgets.
QObject::property
QVariant property(const char *name) const const

© 2019-2022 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 on Mon Mar 7 2022 02:01:20 for KDDockWidgets API Documentation by doxygen 1.8.20