File size: 7,060 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * ********************************************************************************
 * This file is part of the LibreCAD project, a 2D CAD program
 *
 * Copyright (C) 2025 LibreCAD.org
 * Copyright (C) 2025 sand1024
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * ********************************************************************************
 */

#include "lc_dimstyleitem.h"

#include "lc_dimstyle.h"

namespace RS2
{
    enum EntityType : unsigned;
}

LC_DimStyleItem::LC_DimStyleItem() : m_dimType{} {
}

LC_DimStyleItem::~LC_DimStyleItem() {
    m_childItems.clear();
}

void LC_DimStyleItem::appendChild(LC_DimStyleItem *item){
    m_childItems.append(item);
    item->setParentItem(this);
}

LC_DimStyleItem *LC_DimStyleItem::child(int row) const {
    return m_childItems.value(row);
}

int LC_DimStyleItem::childCount() const{
    return m_childItems.count();
}

LC_DimStyleItem *LC_DimStyleItem::parentItem() const {
    return m_parentItem;
}

void LC_DimStyleItem::updateNameAndType() {
    QString name = m_dimStyle->getName();
    LC_DimStyle::parseStyleName(name, m_baseName, m_dimType);
    m_displayName = composeDisplayName(m_baseName, m_dimType);
}

QString LC_DimStyleItem::getDisplayDimStyleName(LC_DimStyle* style) {
    QString name = style->getName();
    QString baseName;
    RS2::EntityType entityType;
    LC_DimStyle::parseStyleName(name, baseName, entityType);
    return composeDisplayName(baseName, entityType);
}

bool LC_DimStyleItem::hasUsedChildren() {
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        if (child->usageCount() > 0) {
            return true;
        }
    }
    return false;
}

bool LC_DimStyleItem::isNotUsedInDrawing() {
    return m_usageCount == 0 && !hasUsedChildren();
}

QString LC_DimStyleItem::composeDisplayName(QString baseName, RS2::EntityType entityType) {
    QString result = baseName;
    QString suffix = "";
    switch (entityType) {
        case RS2::EntityDimLinear:
            suffix = tr("Linear");
            result.append(":").append(suffix);
            break;
        case RS2::EntityDimAngular:
            suffix = tr("Angular");
            result.append(":").append(suffix);
            break;
        case RS2::EntityDimDiametric:
            suffix = tr("Diametric");
            result.append(":").append(suffix);
            break;
        case RS2::EntityDimRadial:
            suffix = tr("Radial");
            result.append(":").append(suffix);
            break;
        case RS2::EntityDimOrdinate:
            suffix = tr("Ordinate");
            result.append(":").append(suffix);
            break;
        case RS2::EntityDimLeader:
            suffix = tr("Leader and Tolerance");
            result.append(":").append(suffix);
            break;
        default:
            break;
    }
    return result;
}

int LC_DimStyleItem::row() const {
    if (m_parentItem != nullptr) {
        return m_parentItem->m_childItems.indexOf(const_cast<LC_DimStyleItem*>(this));
    }
    return 0;
}

LC_DimStyleItem* LC_DimStyleItem::findByName(const QString& name) const {
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        if (child->dimStyle()->getName().compare(name, Qt::CaseInsensitive) == 0) {
            return child;
        }
        LC_DimStyleItem* foundChild = child->findByName(name);
        if (foundChild != nullptr) {
            return foundChild;
        }
    }
    return nullptr;
}

LC_DimStyleItem* LC_DimStyleItem::findBaseStyleItem(const QString& baseStyleName) const {
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        if (child->baseName().compare(baseStyleName, Qt::CaseInsensitive) == 0) {
            return child;
        }
    }
    return nullptr;
}

// fixme - sand - rework later, create more uniform generic
// tree model, with search based on acceptors etc..
LC_DimStyleItem* LC_DimStyleItem::findActive() const {
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        if (child->isActive()) {
            return child;
        }
        LC_DimStyleItem* foundChild = child->findActive();
        if (foundChild != nullptr) {
            return foundChild;
        }
    }
    return nullptr;
}

LC_DimStyleItem* LC_DimStyleItem::findEntityStyleItem() const {
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        if (child->isEntityStyleItem()) {
            return child;
        }
        LC_DimStyleItem* foundChild = child->findEntityStyleItem();
        if (foundChild != nullptr) {
            return foundChild;
        }
    }
    return nullptr;
}

void LC_DimStyleItem::cleanup(bool deleteDimStyles) {
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        child->cleanup(deleteDimStyles);
        auto dimStyle = child->dimStyle();
        if (deleteDimStyles) {
            delete dimStyle;
        }
        delete child;
    }
    m_childItems.clear();
}

void LC_DimStyleItem::removeChild(LC_DimStyleItem* item) {
    m_childItems.removeAll(item);
    item->setParentItem(nullptr);
}

void LC_DimStyleItem::collectChildren(QList<LC_DimStyleItem*>& items) {
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        items.push_back(child);
        child->collectChildren(items);
    }
}

void LC_DimStyleItem::setNewBaseName(const QString& newBaseName) {
    if (m_dimType == RS2::EntityUnknown) {
        m_dimStyle->setName(newBaseName);
    }
    else {
        QString newName = LC_DimStyle::getStyleNameForBaseAndType(newBaseName, m_dimType);
        m_dimStyle->setName(newName);
    }
    m_baseName = newBaseName;
    m_displayName = composeDisplayName(m_baseName, m_dimType);
    int childCount = m_childItems.count();
    for (int i = 0; i < childCount; ++i) {
        LC_DimStyleItem* child = m_childItems.at(i);
        child->setNewBaseName(newBaseName);
    }
}