Debian Codegraph @

src/Gui/ComboLinks.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
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 #ifndef GUI_COMBOLINKS_H
25 #define GUI_COMBOLINKS_H
26
27 #include <vector>
28 #include <string>
29
30 #include <App/PropertyLinks.h>
31 #include <Base/Exception.h>
32
33 // Forward declarations
34 class QComboBox;
35 class QString;
36 namespace App
37 {
38 class Document;
39 class DocumentObject;
40 } // namespace App
41
42 namespace Gui
43 {
44
45 /**
46 * @brief The ComboLinks class is a helper class that binds to a QComboBox and
47 * provides an interface to add App::PropertyLinkSub items, retrieve links,
48 * and select items by link value.
49 */
50 class GuiExport ComboLinks
51 {
52 public:
53 /**
54 * @brief Constructor. Binds to an existing QComboBox.
55 * @param combo The combo box to manage. It will be cleared upon binding.
56 * Do not add/remove items directly to/from the combo after binding.
57 */
58 explicit ComboLinks(QComboBox* combo);
59
60 /**
61 * @brief Default constructor. Use setCombo() later.
62 */
63 ComboLinks() = default;
64
65 /**
66 * @brief Destructor. Clears internal resources.
67 */
68 ~ComboLinks();
69
70 FC_DISABLE_COPY_MOVE(ComboLinks)
71
72 /**
73 * @brief Binds the helper to a QComboBox. Clears the combo box.
74 * @param combo The combo box to manage.
75 */
76 void setCombo(QComboBox* combo);
77
78 /**
79 * @brief Adds an item to the combo box associated with a PropertyLinkSub.
80 * @param lnk The link data. Can point to nullptr (e.g., for "Select reference...").
81 * @param itemText The text to display in the combo box.
82 * @param userData Optional integer data associated with the item (default: item index).
83 * @return The index of the added item.
84 */
85 int addLink(const App::PropertyLinkSub& lnk, const QString& itemText, int userData = -1);
86
87 /**
88 * @brief Adds an item to the combo box associated with specific object and subname.
89 * @param linkObj The document object (can be nullptr).
90 * @param linkSubname The sub-element name (e.g., "Edge1", "Face2", "X_Axis").
91 * @param itemText The text to display in the combo box.
92 * @param userData Optional integer data associated with the item (default: item index).
93 * @return The index of the added item.
94 */
95 int addLink(
96 App::DocumentObject* linkObj,
97 const std::string& linkSubname,
98 const QString& itemText,
99 int userData = -1
100 );
101
102
103 /**
104 * @brief Adds an item *before* an item identified by its user data.
105 * Useful for inserting custom items before a standard item like "Select reference...".
106 * If targetUserData is not found, appends to the end.
107 *
108 * @param lnk The link data.
109 * @param itemText The text to display.
110 * @param targetUserData The user data of the item to insert before.
111 * @param userData Optional integer data for the new item (default: item index).
112 * @return The index of the inserted item.
113 */
114 int addLinkBefore(
115 const App::PropertyLinkSub& lnk,
116 const QString& itemText,
117 int targetUserData,
118 int userData = -1
119 );
120
121
122 /**
123 * @brief Clears all items from the combo box and the internal list.
124 */
125 void clear();
126
127 /**
128 * @brief Gets the PropertyLinkSub associated with the item at the given index.
129 * @param index The index of the item.
130 * @return A reference to the stored PropertyLinkSub.
131 * @throws Base::IndexError if index is out of range.
132 * @throws Base::ValueError if the linked object exists but is not in the tracked document.
133 */
134 App::PropertyLinkSub& getLink(int index) const;
135
136 /**
137 * @brief Gets the PropertyLinkSub associated with the currently selected item.
138 * @return A reference to the stored PropertyLinkSub.
139 * @throws Base::IndexError if no item is selected or combo is invalid.
140 * @throws Base::ValueError if linked object validity check fails.
141 */
142 App::PropertyLinkSub& getCurrentLink() const;
143
144 /**
145 * @brief Gets the user data associated with the item at the given index.
146 * @param index The index of the item.
147 * @return The user data. Returns -1 if index is invalid.
148 */
149 int getUserData(int index) const;
150
151 /**
152 * @brief Gets the user data associated with the currently selected item.
153 * @return The user data. Returns -1 if no item is selected or combo is invalid.
154 */
155 int getCurrentUserData() const;
156
157 /**
158 * @brief Selects the item whose PropertyLinkSub matches the provided link.
159 * Blocks combo box signals during the operation.
160 * @param lnk The link to match.
161 * @return The index of the selected item, or -1 if no match is found.
162 */
163 int setCurrentLink(const App::PropertyLinkSub& lnk);
164
165 /**
166 * @brief Gets the number of items in the combo box.
167 * @return Item count.
168 */
169 int count() const;
170
171
172 private:
173 QComboBox* combo = nullptr;
174 App::Document* doc = nullptr; // Document context for validation
175 std::vector<App::PropertyLinkSub*> linksInList; // Owned pointers
176 };
177
178 } // namespace Gui
179
180 #endif // GUI_COMBOLINKS_H