Debian Codegraph @

src/Gui/ComboLinks.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * Copyright (c) 2012 Jan Rheinländer [email protected] *
4 * Copyright (c) 2025 AstoCAD <[email protected]> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 #include "ComboLinks.h"
25
26 #include <QComboBox>
27 #include <QVariant>
28 #include <QString>
29
30 #include <App/Document.h>
31 #include <App/DocumentObject.h>
32
33 namespace Gui
34 {
35
36 ComboLinks::ComboLinks(QComboBox* combo)
37 : combo(combo)
38 {
39 if (combo) {
40 combo->clear();
41 }
42 }
43
44 ComboLinks::~ComboLinks()
45 {
46 clear(); // Deletes owned pointers in linksInList
47 }
48
49 void ComboLinks::setCombo(QComboBox* combobox)
50 {
51 clear(); // Clear old state if any
52 combo = combobox;
53 if (combo) {
54 combo->clear();
55 }
56 }
57
58 int ComboLinks::addLink(const App::PropertyLinkSub& lnk, const QString& itemText, int userData)
59 {
60 if (!combo) {
61 return -1;
62 }
63 int newIndex = combo->count();
64 int finalUserData = (userData == -1) ? newIndex : userData;
65
66 // Store link internally (create a copy)
67 auto* newLink = new App::PropertyLinkSub();
68 newLink->Paste(lnk);
69 linksInList.push_back(newLink);
70
71 // Add to combo box
72 combo->addItem(itemText, QVariant(finalUserData));
73
74 // Track document context from the first valid object link
75 if (!doc && newLink->getValue()) {
76 doc = newLink->getValue()->getDocument();
77 }
78 return newIndex;
79 }
80
81 int ComboLinks::addLink(
82 App::DocumentObject* linkObj,
83 const std::string& linkSubname,
84 const QString& itemText,
85 int userData
86 )
87 {
88 App::PropertyLinkSub lnk;
89 std::vector<std::string> sub = {linkSubname};
90 lnk.setValue(linkObj, sub);
91 return addLink(lnk, itemText, userData);
92 }
93
94 int ComboLinks::addLinkBefore(
95 const App::PropertyLinkSub& lnk,
96 const QString& itemText,
97 int targetUserData,
98 int userData
99 )
100 {
101 if (!combo) {
102 return -1;
103 }
104
105 int insertPos = -1;
106 for (int i = 0; i < combo->count(); ++i) {
107 if (combo->itemData(i).toInt() == targetUserData) {
108 insertPos = i;
109 break;
110 }
111 }
112
113 // Store link internally (create a copy)
114 auto* newLink = new App::PropertyLinkSub();
115 newLink->Paste(lnk);
116
117 int finalUserData = (userData == -1) ? ((insertPos == -1) ? count() : insertPos) : userData;
118
119 if (insertPos != -1) {
120 linksInList.insert(linksInList.begin() + insertPos, newLink);
121 combo->insertItem(insertPos, itemText, QVariant(finalUserData));
122 }
123 else {
124 // Target not found, append to end
125 insertPos = combo->count();
126 linksInList.push_back(newLink);
127 combo->addItem(itemText, QVariant(finalUserData));
128 }
129
130 // Update user data for subsequent items if default (-1) was used and inserting
131 if (userData == -1 && insertPos != -1 && insertPos < count() - 1) {
132 for (int i = insertPos + 1; i < count(); ++i) {
133 if (combo->itemData(i).toInt() == i - 1) { // Check if it was using default index
134 combo->setItemData(i, QVariant(i));
135 }
136 }
137 }
138
139
140 // Track document context
141 if (!doc && newLink->getValue()) {
142 doc = newLink->getValue()->getDocument();
143 }
144 return insertPos;
145 }
146
147
148 void ComboLinks::clear()
149 {
150 if (combo) {
151 // Block signals while clearing to prevent issues if connected elsewhere
152 QSignalBlocker blocker(combo);
153 combo->clear();
154 }
155 for (App::PropertyLinkSub* linkPtr : linksInList) {
156 delete linkPtr; // Delete the objects pointed to
157 }
158 linksInList.clear(); // Clear the vector itself
159 doc = nullptr; // Reset document context
160 }
161
162 App::PropertyLinkSub& ComboLinks::getLink(int index) const
163 {
164 if (index < 0 || static_cast<size_t>(index) >= linksInList.size()) {
165 throw Base::IndexError("ComboLinks::getLink: Index out of range");
166 }
167 App::PropertyLinkSub* linkPtr = linksInList[static_cast<size_t>(index)];
168 // Perform validity check only if we have a document context and a linked object
169 if (doc && linkPtr->getValue() && !(doc->isIn(linkPtr->getValue()))) {
170 throw Base::ValueError("Linked object is not in the document; it may have been deleted");
171 }
172 return *linkPtr;
173 }
174
175 App::PropertyLinkSub& ComboLinks::getCurrentLink() const
176 {
177 assert(combo);
178 return getLink(combo->currentIndex());
179 }
180
181 int ComboLinks::getUserData(int index) const
182 {
183 if (!combo || index < 0 || index >= combo->count()) {
184 return -1; // Indicate invalid index or no combo
185 }
186 return combo->itemData(index).toInt();
187 }
188
189 int ComboLinks::getCurrentUserData() const
190 {
191 if (!combo) {
192 return -1;
193 }
194 return combo->currentData().toInt();
195 }
196
197
198 int ComboLinks::setCurrentLink(const App::PropertyLinkSub& lnk)
199 {
200 if (!combo) {
201 return -1;
202 }
203 for (size_t i = 0; i < linksInList.size(); ++i) {
204 const App::PropertyLinkSub* it = linksInList[i];
205 // Compare object pointer and sub-values vector
206 if (lnk.getValue() == it->getValue() && lnk.getSubValues() == it->getSubValues()) {
207 QSignalBlocker blocker(combo);
208 combo->setCurrentIndex(static_cast<int>(i));
209 return static_cast<int>(i);
210 }
211 }
212 return -1; // Not found
213 }
214
215 int ComboLinks::count() const
216 {
217 return combo ? combo->count() : 0;
218 }
219
220 } // namespace Gui