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 test suite 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 <QList> |
30 | #include <QPointer> |
31 | #include <QVariant> |
32 | |
33 | #include "ASTItem.h" |
34 | |
35 | using namespace QPatternistSDK; |
36 | |
37 | /** |
38 | * This is what the AST rendering is indented with. |
39 | */ |
40 | static const QLatin1String astIndent(" " ); |
41 | // STATIC DATA |
42 | |
43 | ASTItem::ASTItem(ASTItem *p, |
44 | const QString &name, |
45 | const QString &details, |
46 | const QString &staticType, |
47 | const QString &reqType) : m_name(name), |
48 | m_details(details), |
49 | m_reqType(reqType), |
50 | m_staticType(staticType), |
51 | m_parent(p) |
52 | { |
53 | } |
54 | |
55 | ASTItem::~ASTItem() |
56 | { |
57 | qDeleteAll(c: m_children); |
58 | } |
59 | |
60 | QString ASTItem::toString() const |
61 | { |
62 | /* The first ASTItem* is a virtual root node, so skip "this". */ |
63 | Q_ASSERT(m_children.count() == 1); |
64 | TreeItem *treeChild = m_children.first(); |
65 | Q_ASSERT(treeChild); |
66 | |
67 | ASTItem *astChild = static_cast<ASTItem *>(treeChild); |
68 | |
69 | return astChild->toString(indent: QString()); |
70 | } |
71 | |
72 | QString ASTItem::toString(const QString &indent) const |
73 | { |
74 | QString retval; |
75 | |
76 | retval += indent; |
77 | retval += m_name; |
78 | retval += QLatin1Char('('); |
79 | retval += m_details; |
80 | retval += QLatin1String(")\n" ); |
81 | |
82 | const TreeItem::List::const_iterator end(m_children.constEnd()); |
83 | |
84 | for(TreeItem::List::const_iterator it(m_children.constBegin()); it != end; ++it) |
85 | { |
86 | TreeItem *treeChild = *it; /* Cast away the QPointer with its casting operator. */ |
87 | ASTItem *astChild = static_cast<ASTItem *>(treeChild); |
88 | |
89 | retval += astChild->toString(indent: indent + astIndent); |
90 | } |
91 | |
92 | return retval; |
93 | } |
94 | |
95 | QVariant ASTItem::data(const Qt::ItemDataRole role, int column) const |
96 | { |
97 | if(role != Qt::DisplayRole) |
98 | return QVariant(); |
99 | |
100 | switch(column) |
101 | { |
102 | case 0: |
103 | return m_name; |
104 | case 1: |
105 | return m_details; |
106 | case 2: |
107 | return m_staticType; |
108 | case 3: |
109 | return m_reqType; |
110 | default: |
111 | { |
112 | Q_ASSERT(false); |
113 | return QVariant(); |
114 | } |
115 | } |
116 | } |
117 | |
118 | int ASTItem::columnCount() const |
119 | { |
120 | return 4; |
121 | } |
122 | |
123 | TreeItem::List ASTItem::children() const |
124 | { |
125 | return m_children; |
126 | } |
127 | |
128 | void ASTItem::appendChild(TreeItem *item) |
129 | { |
130 | m_children.append(t: item); |
131 | } |
132 | |
133 | TreeItem *ASTItem::child(const unsigned int rowP) const |
134 | { |
135 | return m_children.value(i: rowP); |
136 | } |
137 | |
138 | unsigned int ASTItem::childCount() const |
139 | { |
140 | return m_children.count(); |
141 | } |
142 | |
143 | TreeItem *ASTItem::parent() const |
144 | { |
145 | return m_parent; |
146 | } |
147 | |
148 | // vim: et:ts=4:sw=4:sts=4 |
149 | |