File size: 6,805 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
 *   Copyright (c) 2012 Jan Rheinländer jrheinlaender@users.sourceforge.net *
 *   Copyright (c) 2025 AstoCAD                  <hello@astocad.com>        *
 *                                                                          *
 *   This file is part of FreeCAD.                                          *
 *                                                                          *
 *   FreeCAD is free software: you can redistribute it and/or modify it     *
 *   under the terms of the GNU Lesser General Public License as            *
 *   published by the Free Software Foundation, either version 2.1 of the   *
 *   License, or (at your option) any later version.                        *
 *                                                                          *
 *   FreeCAD 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       *
 *   Lesser General Public License for more details.                        *
 *                                                                          *
 *   You should have received a copy of the GNU Lesser General Public       *
 *   License along with FreeCAD. If not, see                                *
 *   <https://www.gnu.org/licenses/>.                                       *
 *                                                                          *
 ***************************************************************************/

#include "ComboLinks.h"

#include <QComboBox>
#include <QVariant>
#include <QString>

#include <App/Document.h>
#include <App/DocumentObject.h>

namespace Gui
{

ComboLinks::ComboLinks(QComboBox* combo)
    : combo(combo)
{
    if (combo) {
        combo->clear();
    }
}

ComboLinks::~ComboLinks()
{
    clear();  // Deletes owned pointers in linksInList
}

void ComboLinks::setCombo(QComboBox* combobox)
{
    clear();  // Clear old state if any
    combo = combobox;
    if (combo) {
        combo->clear();
    }
}

int ComboLinks::addLink(const App::PropertyLinkSub& lnk, const QString& itemText, int userData)
{
    if (!combo) {
        return -1;
    }
    int newIndex = combo->count();
    int finalUserData = (userData == -1) ? newIndex : userData;

    // Store link internally (create a copy)
    auto* newLink = new App::PropertyLinkSub();
    newLink->Paste(lnk);
    linksInList.push_back(newLink);

    // Add to combo box
    combo->addItem(itemText, QVariant(finalUserData));

    // Track document context from the first valid object link
    if (!doc && newLink->getValue()) {
        doc = newLink->getValue()->getDocument();
    }
    return newIndex;
}

int ComboLinks::addLink(
    App::DocumentObject* linkObj,
    const std::string& linkSubname,
    const QString& itemText,
    int userData
)
{
    App::PropertyLinkSub lnk;
    std::vector<std::string> sub = {linkSubname};
    lnk.setValue(linkObj, sub);
    return addLink(lnk, itemText, userData);
}

int ComboLinks::addLinkBefore(
    const App::PropertyLinkSub& lnk,
    const QString& itemText,
    int targetUserData,
    int userData
)
{
    if (!combo) {
        return -1;
    }

    int insertPos = -1;
    for (int i = 0; i < combo->count(); ++i) {
        if (combo->itemData(i).toInt() == targetUserData) {
            insertPos = i;
            break;
        }
    }

    // Store link internally (create a copy)
    auto* newLink = new App::PropertyLinkSub();
    newLink->Paste(lnk);

    int finalUserData = (userData == -1) ? ((insertPos == -1) ? count() : insertPos) : userData;

    if (insertPos != -1) {
        linksInList.insert(linksInList.begin() + insertPos, newLink);
        combo->insertItem(insertPos, itemText, QVariant(finalUserData));
    }
    else {
        // Target not found, append to end
        insertPos = combo->count();
        linksInList.push_back(newLink);
        combo->addItem(itemText, QVariant(finalUserData));
    }

    // Update user data for subsequent items if default (-1) was used and inserting
    if (userData == -1 && insertPos != -1 && insertPos < count() - 1) {
        for (int i = insertPos + 1; i < count(); ++i) {
            if (combo->itemData(i).toInt() == i - 1) {  // Check if it was using default index
                combo->setItemData(i, QVariant(i));
            }
        }
    }


    // Track document context
    if (!doc && newLink->getValue()) {
        doc = newLink->getValue()->getDocument();
    }
    return insertPos;
}


void ComboLinks::clear()
{
    if (combo) {
        // Block signals while clearing to prevent issues if connected elsewhere
        QSignalBlocker blocker(combo);
        combo->clear();
    }
    for (App::PropertyLinkSub* linkPtr : linksInList) {
        delete linkPtr;  // Delete the objects pointed to
    }
    linksInList.clear();  // Clear the vector itself
    doc = nullptr;        // Reset document context
}

App::PropertyLinkSub& ComboLinks::getLink(int index) const
{
    if (index < 0 || static_cast<size_t>(index) >= linksInList.size()) {
        throw Base::IndexError("ComboLinks::getLink: Index out of range");
    }
    App::PropertyLinkSub* linkPtr = linksInList[static_cast<size_t>(index)];
    // Perform validity check only if we have a document context and a linked object
    if (doc && linkPtr->getValue() && !(doc->isIn(linkPtr->getValue()))) {
        throw Base::ValueError("Linked object is not in the document; it may have been deleted");
    }
    return *linkPtr;
}

App::PropertyLinkSub& ComboLinks::getCurrentLink() const
{
    assert(combo);
    return getLink(combo->currentIndex());
}

int ComboLinks::getUserData(int index) const
{
    if (!combo || index < 0 || index >= combo->count()) {
        return -1;  // Indicate invalid index or no combo
    }
    return combo->itemData(index).toInt();
}

int ComboLinks::getCurrentUserData() const
{
    if (!combo) {
        return -1;
    }
    return combo->currentData().toInt();
}


int ComboLinks::setCurrentLink(const App::PropertyLinkSub& lnk)
{
    if (!combo) {
        return -1;
    }
    for (size_t i = 0; i < linksInList.size(); ++i) {
        const App::PropertyLinkSub* it = linksInList[i];
        // Compare object pointer and sub-values vector
        if (lnk.getValue() == it->getValue() && lnk.getSubValues() == it->getSubValues()) {
            QSignalBlocker blocker(combo);
            combo->setCurrentIndex(static_cast<int>(i));
            return static_cast<int>(i);
        }
    }
    return -1;  // Not found
}

int ComboLinks::count() const
{
    return combo ? combo->count() : 0;
}

}  // namespace Gui