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
5#include <QtDesigner/abstractformwindowtool.h>
6
7#include <QtWidgets/qstackedlayout.h>
8#include <QtWidgets/qboxlayout.h>
9#include <QtWidgets/qwidget.h>
10
11#include <QtGui/qevent.h>
12#include <QtGui/qaction.h>
13
14#include <QtCore/qdebug.h>
15
17
18using namespace Qt::StringLiterals;
19
20namespace qdesigner_internal {
21
22FormWindowWidgetStack::FormWindowWidgetStack(QObject *parent) :
23 QObject(parent),
24 m_formContainer(new QWidget),
25 m_formContainerLayout(new QStackedLayout),
26 m_layout(new QStackedLayout)
27{
28 m_layout->setContentsMargins(QMargins());
29 m_layout->setSpacing(0);
30 m_layout->setStackingMode(QStackedLayout::StackAll);
31
32 // We choose a QStackedLayout as immediate layout for
33 // the form windows as it ignores the sizePolicy of
34 // its child (for example, Fixed would cause undesired side effects).
35 m_formContainerLayout->setContentsMargins(QMargins());
36 m_formContainer->setObjectName(u"formContainer"_s);
37 m_formContainer->setLayout(m_formContainerLayout);
38 m_formContainerLayout->setStackingMode(QStackedLayout::StackAll);
39 // System settings might have different background colors, autofill them
40 // (affects for example mainwindow status bars)
41 m_formContainer->setAutoFillBackground(true);
42}
43
45
47{
48 return m_tools.size();
49}
50
51QDesignerFormWindowToolInterface *FormWindowWidgetStack::currentTool() const
52{
54}
55
57{
58 const int cnt = count();
59 if (index < 0 || index >= cnt) {
60 qDebug("FormWindowWidgetStack::setCurrentTool(): invalid index: %d", index);
61 return;
62 }
63
64 const int cur = currentIndex();
65 if (index == cur)
66 return;
67
68 if (cur != -1)
69 m_tools.at(cur)->deactivated();
70
71
72 m_layout->setCurrentIndex(index);
73 // Show the widget editor and the current tool
74 for (int i = 0; i < cnt; i++)
75 m_tools.at(i)->editor()->setVisible(i == 0 || i == index);
76
77 QDesignerFormWindowToolInterface *tool = m_tools.at(index);
78 tool->activated();
79
80 emit currentToolChanged(index);
81}
82
84{
85 QDesignerFormWindowToolInterface *tool = nullptr;
86 QAction *action = qobject_cast<QAction*>(sender());
87 if (action == nullptr) {
88 qDebug("FormWindowWidgetStack::setSenderAsCurrentTool(): sender is not a QAction");
89 return;
90 }
91
92 for (QDesignerFormWindowToolInterface *t : std::as_const(m_tools)) {
93 if (action == t->action()) {
94 tool = t;
95 break;
96 }
97 }
98
99 if (tool == nullptr) {
100 qDebug("FormWindowWidgetStack::setSenderAsCurrentTool(): unknown tool");
101 return;
102 }
103
105}
106
107int FormWindowWidgetStack::indexOf(QDesignerFormWindowToolInterface *tool) const
108{
109 return m_tools.indexOf(tool);
110}
111
112void FormWindowWidgetStack::setCurrentTool(QDesignerFormWindowToolInterface *tool)
113{
114 int index = indexOf(tool);
115 if (index == -1) {
116 qDebug("FormWindowWidgetStack::setCurrentTool(): unknown tool");
117 return;
118 }
119
120 setCurrentTool(index);
121}
122
124{
125 // This code is triggered once by the formwindow and
126 // by integrations doing "revert to saved". Anything changing?
127 const int previousCount = m_formContainerLayout->count();
128 QWidget *previousMainContainer = previousCount
129 ? m_formContainerLayout->itemAt(0)->widget() : nullptr;
130 if (previousMainContainer == w)
131 return;
132 // Swap
133 if (previousCount)
134 delete m_formContainerLayout->takeAt(0);
135 if (w)
136 m_formContainerLayout->addWidget(w);
137}
138
139void FormWindowWidgetStack::addTool(QDesignerFormWindowToolInterface *tool)
140{
141 if (QWidget *w = tool->editor()) {
142 w->setVisible(m_layout->count() == 0); // Initially only form editor is visible
143 m_layout->addWidget(w);
144 } else {
145 // The form editor might not have a tool initially, use dummy. Assert on anything else
146 Q_ASSERT(m_tools.isEmpty());
147 m_layout->addWidget(m_formContainer);
148 }
149
150 m_tools.append(tool);
151
152 connect(tool->action(), &QAction::triggered,
153 this, &FormWindowWidgetStack::setSenderAsCurrentTool);
154}
155
156QDesignerFormWindowToolInterface *FormWindowWidgetStack::tool(int index) const
157{
158 if (index < 0 || index >= count())
159 return nullptr;
160
161 return m_tools.at(index);
162}
163
165{
166 return m_layout->currentIndex();
167}
168
170{
171 if (m_tools.isEmpty())
172 return nullptr;
173
174 return m_tools.at(0)->editor();
175}
176
178{
179 return m_layout;
180}
181
182} // namespace qdesigner_internal
183
184QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:421
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.