swift
Loading...
Searching...
No Matches
dockwidgetinfoarea.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QMenu>
7#include <QString>
8#include <QWidget>
9#include <QtGlobal>
10
12#include "gui/infoarea.h"
13
14using namespace swift::gui::components;
15
16namespace swift::gui
17{
22
24 {
25 const auto *ia = qobject_cast<const CInfoArea *>(this->parent());
26 Q_ASSERT(ia);
27 return ia;
28 }
29
31 {
32 auto *ia = qobject_cast<CInfoArea *>(this->parent());
33 Q_ASSERT(ia);
34 return ia;
35 }
36
38 {
39 const CInfoArea *ia = getParentInfoArea();
40 if (!ia)
41 {
42 return false;
43 }
44 return ia->isSelectedDockWidgetInfoArea(this);
45 }
46
48 {
49 // if the widget is invisible we are done
50 // but if it is visible, there is no guarantee it can be seen by the user
51 if (!this->isVisible())
52 {
53 return false;
54 }
55
56 // further checks
57 if (this->isFloating())
58 {
59 return !this->isMinimized();
60 }
61 else
62 {
63 return isSelectedDockWidget();
64 }
65 }
66
68 {
69 QList<const CInfoArea *> parentInfoAreas = this->findParentInfoAreas();
70 Q_ASSERT(!parentInfoAreas.isEmpty());
71 if (parentInfoAreas.isEmpty()) return;
72
73 // Dockable widget's context menu
75 if (!contextMenu->isEmpty())
76 {
77 contextMenu->addSeparator();
78 }
79
80 // first info area, myself's direct parent info area
81 parentInfoAreas.first()->addToContextMenu(contextMenu);
82
83 // top info areas other than direct parent
84 // (parent's parent when nested info areas are used)
85 if (parentInfoAreas.size() < 2)
86 {
87 return;
88 }
89 contextMenu->addSeparator();
90 for (int i = 1; i < parentInfoAreas.size(); i++)
91 {
92 const CInfoArea *infoArea = parentInfoAreas.at(i);
93 QString title(infoArea->windowTitle());
94 if (title.isEmpty())
95 {
96 title = infoArea->objectName();
97 }
98 QMenu *m = contextMenu->addMenu(title);
99 infoArea->addToContextMenu(m);
100 }
101 }
102
104 {
105 CDockWidget::initialFloating(); // initial floating to init position & size
106
107 // set the top level dock widget area to all children
108 QList<CEnableForDockWidgetInfoArea *> infoAreaDockWidgets = this->findEmbeddedDockWidgetInfoAreaComponents();
109 for (CEnableForDockWidgetInfoArea *dwia : infoAreaDockWidgets)
110 {
111 Q_ASSERT_X(dwia, Q_FUNC_INFO, "Missing info area");
112 dwia->setParentDockWidgetInfoArea(this);
113 }
114 }
115
116 QList<CEnableForDockWidgetInfoArea *> CDockWidgetInfoArea::findEmbeddedDockWidgetInfoAreaComponents()
117 {
118 QList<QWidget *> widgets = this->findChildren<QWidget *>(); // must not use Qt::FindDirectChildrenOnly here
119 QList<CEnableForDockWidgetInfoArea *> widgetsWithDockWidgetInfoAreaComponent;
120 for (QWidget *w : widgets)
121 {
122 Q_ASSERT(w);
123
124 // CEnableForDockWidgetInfoArea is no QObject, so we use dynamic_cast
125 auto *dwc = dynamic_cast<CEnableForDockWidgetInfoArea *>(w);
126 if (dwc)
127 {
128 widgetsWithDockWidgetInfoAreaComponent.append(dwc);
129 }
130 }
131 QList<CDockWidgetInfoArea *> nestedInfoAreas = this->findNestedInfoAreas();
132 if (nestedInfoAreas.isEmpty()) return widgetsWithDockWidgetInfoAreaComponent;
133
134 // we have to exclude the nested embedded areas
135 for (CDockWidgetInfoArea *ia : nestedInfoAreas)
136 {
137 QList<CEnableForDockWidgetInfoArea *> nestedInfoAreaComponents =
138 ia->findEmbeddedDockWidgetInfoAreaComponents();
139 if (nestedInfoAreaComponents.isEmpty())
140 {
141 continue;
142 }
143 for (CEnableForDockWidgetInfoArea *iac : nestedInfoAreaComponents)
144 {
145 const bool r = widgetsWithDockWidgetInfoAreaComponent.removeOne(iac);
146 Q_ASSERT(r); // why is the nested component not in the child list?
147 Q_UNUSED(r)
148 }
149 }
150 return widgetsWithDockWidgetInfoAreaComponent;
151 }
152
153 QList<CDockWidgetInfoArea *> CDockWidgetInfoArea::findNestedInfoAreas()
154 {
155 // must not use Qt::FindDirectChildrenOnly here
156 QList<CDockWidgetInfoArea *> nestedInfoAreas = this->findChildren<CDockWidgetInfoArea *>();
157 return nestedInfoAreas;
158 }
159
160 QList<const CInfoArea *> CDockWidgetInfoArea::findParentInfoAreas() const
161 {
162 QList<const CInfoArea *> parents;
163 QWidget *currentWidget = this->parentWidget();
164 while (currentWidget)
165 {
166 const CInfoArea *ia = qobject_cast<CInfoArea *>(currentWidget);
167 if (ia)
168 {
169 parents.append(ia);
170 }
171 currentWidget = currentWidget->parentWidget();
172 }
173 return parents;
174 }
175} // namespace swift::gui
CDockWidget(QWidget *parent=nullptr)
Constructor.
virtual void addToContextMenu(QMenu *contextMenu) const
Contribute to menu.
virtual void initialFloating()
Widget is initialized by being a floating window for a shot period.
Specialized class for dock widgets serving as info area.
const CInfoArea * getParentInfoArea() const
The parent info area.
CDockWidgetInfoArea(QWidget *parent=nullptr)
Constructor.
void initialFloating()
Widget is initialized by being a floating window for a shot period.
bool isVisibleWidget() const
Visible widget.
bool isSelectedDockWidget() const
Is this the selected widget, means it is not floating, and it is the one selected.
void addToContextMenu(QMenu *contextMenu) const
Contribute to menu.
Helper class: If a component is residing in an dockable widget. This class provides access to its inf...
Info area, hosting dockable widgets.
Definition infoarea.h:41
void addToContextMenu(QMenu *menu) const
Add items to context menu.
Definition infoarea.cpp:87
bool isSelectedDockWidgetInfoArea(const CDockWidgetInfoArea *infoArea) const
Is given widget selected. Means it is not floating, and the one selected.
Definition infoarea.cpp:186
High level reusable GUI components.
GUI related classes.
bool isFloating() const const
void append(QList< T > &&value)
QList< T >::const_reference at(qsizetype i) const const
T & first()
bool isEmpty() const const
bool removeOne(const AT &t)
qsizetype size() const const
QAction * addMenu(QMenu *menu)
QAction * addSeparator()
bool isEmpty() const const
QList< T > findChildren(QAnyStringView name, Qt::FindChildOptions options) const const
QObject * parent() const const
T qobject_cast(QObject *object)
bool isEmpty() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
bool isMinimized() const const
QWidget * parentWidget() const const
bool isVisible() const const