1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "customwidgetsinfo.h"
30#include "driver.h"
31#include "ui4.h"
32#include "utils.h"
33
34#include <utility>
35
36QT_BEGIN_NAMESPACE
37
38CustomWidgetsInfo::CustomWidgetsInfo() = default;
39
40void CustomWidgetsInfo::acceptUI(DomUI *node)
41{
42 m_customWidgets.clear();
43
44 if (node->elementCustomWidgets())
45 acceptCustomWidgets(node->elementCustomWidgets());
46}
47
48void CustomWidgetsInfo::acceptCustomWidgets(DomCustomWidgets *node)
49{
50 TreeWalker::acceptCustomWidgets(node);
51}
52
53void CustomWidgetsInfo::acceptCustomWidget(DomCustomWidget *node)
54{
55 if (node->elementClass().isEmpty())
56 return;
57
58 m_customWidgets.insert(node->elementClass(), node);
59}
60
61bool CustomWidgetsInfo::extends(const QString &classNameIn, QLatin1String baseClassName) const
62{
63 if (classNameIn == baseClassName)
64 return true;
65
66 QString className = classNameIn;
67 while (const DomCustomWidget *c = customWidget(className)) {
68 const QString extends = c->elementExtends();
69 if (className == extends) // Faulty legacy custom widget entries exist.
70 return false;
71 if (extends == baseClassName)
72 return true;
73 className = extends;
74 }
75 return false;
76}
77
78bool CustomWidgetsInfo::extendsOneOf(const QString &classNameIn,
79 const QStringList &baseClassNames) const
80{
81 if (baseClassNames.contains(classNameIn))
82 return true;
83
84 QString className = classNameIn;
85 while (const DomCustomWidget *c = customWidget(className)) {
86 const QString extends = c->elementExtends();
87 if (className == extends) // Faulty legacy custom widget entries exist.
88 return false;
89 if (baseClassNames.contains(extends))
90 return true;
91 className = extends;
92 }
93 return false;
94}
95
96bool CustomWidgetsInfo::isCustomWidgetContainer(const QString &className) const
97{
98 if (const DomCustomWidget *dcw = m_customWidgets.value(className, nullptr))
99 if (dcw->hasElementContainer())
100 return dcw->elementContainer() != 0;
101 return false;
102}
103
104QString CustomWidgetsInfo::realClassName(const QString &className) const
105{
106 if (className == QLatin1String("Line"))
107 return QLatin1String("QFrame");
108
109 return className;
110}
111
112QString CustomWidgetsInfo::customWidgetAddPageMethod(const QString &name) const
113{
114 if (DomCustomWidget *dcw = m_customWidgets.value(name, nullptr))
115 return dcw->elementAddPageMethod();
116 return QString();
117}
118
119// add page methods for simple containers taking only the widget parameter
120QString CustomWidgetsInfo::simpleContainerAddPageMethod(const QString &name) const
121{
122 using AddPageMethod = std::pair<const char *, const char *>;
123
124 static AddPageMethod addPageMethods[] = {
125 {"QStackedWidget", "addWidget"},
126 {"QToolBar", "addWidget"},
127 {"QDockWidget", "setWidget"},
128 {"QScrollArea", "setWidget"},
129 {"QSplitter", "addWidget"},
130 {"QMdiArea", "addSubWindow"}
131 };
132 for (const auto &m : addPageMethods) {
133 if (extends(name, QLatin1String(m.first)))
134 return QLatin1String(m.second);
135 }
136 return QString();
137}
138
139QT_END_NAMESPACE
140