Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
formwindow_widgetstack.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
6#include <QtDesigner/abstractformwindowtool.h>
7
8#include <QtWidgets/qstackedlayout.h>
9#include <QtWidgets/qboxlayout.h>
10#include <QtWidgets/qwidget.h>
11
12#include <QtGui/qevent.h>
13#include <QtGui/qaction.h>
14
15#include <QtCore/qdebug.h>
16
18
19using namespace Qt::StringLiterals;
20
21namespace qdesigner_internal {
22
23FormWindowWidgetStack::FormWindowWidgetStack(QObject *parent) :
24 QObject(parent),
25 m_formContainer(new QWidget),
26 m_formContainerLayout(new QStackedLayout),
27 m_layout(new QStackedLayout)
28{
29 m_layout->setContentsMargins(QMargins());
30 m_layout->setSpacing(0);
31 m_layout->setStackingMode(QStackedLayout::StackAll);
32
33 // We choose a QStackedLayout as immediate layout for
34 // the form windows as it ignores the sizePolicy of
35 // its child (for example, Fixed would cause undesired side effects).
36 m_formContainerLayout->setContentsMargins(QMargins());
37 m_formContainer->setObjectName(u"formContainer"_s);
38 m_formContainer->setLayout(m_formContainerLayout);
39 m_formContainerLayout->setStackingMode(QStackedLayout::StackAll);
40 // System settings might have different background colors, autofill them
41 // (affects for example mainwindow status bars)
42 m_formContainer->setAutoFillBackground(true);
43}
44
46
48{
49 return m_tools.size();
50}
51
52QDesignerFormWindowToolInterface *FormWindowWidgetStack::currentTool() const
53{
55}
56
58{
59 const int cnt = count();
60 if (index < 0 || index >= cnt) {
61 qDebug("FormWindowWidgetStack::setCurrentTool(): invalid index: %d", index);
62 return;
63 }
64
65 const int cur = currentIndex();
66 if (index == cur)
67 return;
68
69 if (cur != -1)
70 m_tools.at(cur)->deactivated();
71
72
73 m_layout->setCurrentIndex(index);
74 // Show the widget editor and the current tool
75 for (int i = 0; i < cnt; i++)
76 m_tools.at(i)->editor()->setVisible(i == 0 || i == index);
77
78 QDesignerFormWindowToolInterface *tool = m_tools.at(index);
79 tool->activated();
80
81 emit currentToolChanged(index);
82}
83
85{
86 QDesignerFormWindowToolInterface *tool = nullptr;
87 QAction *action = qobject_cast<QAction*>(sender());
88 if (action == nullptr) {
89 qDebug("FormWindowWidgetStack::setSenderAsCurrentTool(): sender is not a QAction");
90 return;
91 }
92
93 for (QDesignerFormWindowToolInterface *t : std::as_const(m_tools)) {
94 if (action == t->action()) {
95 tool = t;
96 break;
97 }
98 }
99
100 if (tool == nullptr) {
101 qDebug("FormWindowWidgetStack::setSenderAsCurrentTool(): unknown tool");
102 return;
103 }
104
106}
107
108int FormWindowWidgetStack::indexOf(QDesignerFormWindowToolInterface *tool) const
109{
110 return m_tools.indexOf(tool);
111}
112
113void FormWindowWidgetStack::setCurrentTool(QDesignerFormWindowToolInterface *tool)
114{
115 int index = indexOf(tool);
116 if (index == -1) {
117 qDebug("FormWindowWidgetStack::setCurrentTool(): unknown tool");
118 return;
119 }
120
121 setCurrentTool(index);
122}
123
125{
126 // This code is triggered once by the formwindow and
127 // by integrations doing "revert to saved". Anything changing?
128 const int previousCount = m_formContainerLayout->count();
129 QWidget *previousMainContainer = previousCount
130 ? m_formContainerLayout->itemAt(0)->widget() : nullptr;
131 if (previousMainContainer == w)
132 return;
133 // Swap
134 if (previousCount)
135 delete m_formContainerLayout->takeAt(0);
136 if (w)
137 m_formContainerLayout->addWidget(w);
138}
139
140void FormWindowWidgetStack::addTool(QDesignerFormWindowToolInterface *tool)
141{
142 if (QWidget *w = tool->editor()) {
143 w->setVisible(m_layout->count() == 0); // Initially only form editor is visible
144 m_layout->addWidget(w);
145 } else {
146 // The form editor might not have a tool initially, use dummy. Assert on anything else
147 Q_ASSERT(m_tools.isEmpty());
148 m_layout->addWidget(m_formContainer);
149 }
150
151 m_tools.append(tool);
152
153 connect(tool->action(), &QAction::triggered,
155}
156
157QDesignerFormWindowToolInterface *FormWindowWidgetStack::tool(int index) const
158{
159 if (index < 0 || index >= count())
160 return nullptr;
161
162 return m_tools.at(index);
163}
164
166{
167 return m_layout->currentIndex();
168}
169
171{
172 if (m_tools.isEmpty())
173 return nullptr;
174
175 return m_tools.at(0)->editor();
176}
177
179{
180 return m_layout;
181}
182
183} // namespace qdesigner_internal
184
185QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:432
QDesignerFormWindowToolInterface * currentTool() const
QDesignerFormWindowToolInterface * tool(int index) const
void setCurrentTool(QDesignerFormWindowToolInterface *tool)
int indexOf(QDesignerFormWindowToolInterface *tool) const
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.