File size: 28,307 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# SPDX-License-Identifier: LGPL-2.1-or-later

# flake8: noqa E731
# ***************************************************************************
# *   Copyright (c) 2025 Samuel Abels <knipknap@gmail.com>                  *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

"""Widget for browsing Tool Library assets with filtering and sorting."""

import yaml
from typing import cast, List, Optional
from PySide import QtCore, QtGui
from PySide.QtGui import QMenu, QAction, QKeySequence
import FreeCAD
import Path
from ...assets import AssetManager, AssetUri
from ...toolbit import ToolBit
from ...toolbit.ui import ToolBitEditor
from ...toolbit.ui.util import natural_sort_key
from ...toolbit.ui.browser import ToolBitBrowserWidget, ToolBitUriRole
from ...toolbit.serializers import YamlToolBitSerializer
from ..models.library import Library


Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())
Path.Log.trackModule(Path.Log.thisModule())


class LibraryBrowserWidget(ToolBitBrowserWidget):
    """
    A widget to browse, filter, and select Tool Library assets from the
    AssetManager, with sorting and batch insertion, using a current library.
    """

    current_library_changed = QtCore.Signal()

    def __init__(
        self,
        asset_manager: AssetManager,
        store: str = "local",
        parent=None,
        compact=True,
    ):
        super().__init__(
            asset_manager=asset_manager,
            store=store,
            parent=parent,
            tool_no_factory=self.get_tool_no_from_current_library,
            compact=compact,
        )
        self.current_library: Optional[Library] = None
        self._selected_tool_type: Optional[str] = None
        self.layout().setContentsMargins(0, 0, 0, 0)

        # Add tool type filter combo box to the base widget
        self._tool_type_combo = QtGui.QComboBox()
        self._tool_type_combo.setSizePolicy(
            QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred
        )
        self._top_layout.insertWidget(0, self._tool_type_combo, 1)
        self._tool_type_combo.currentTextChanged.connect(self._on_tool_type_combo_changed)

        self.restore_last_sort_order()
        self.load_last_library()

    def setDragEnabled(self, enabled: bool = True):
        """Enable or disable drag-and-drop support for the tool list."""
        self._tool_list_widget.setDragEnabled(enabled)

    def load_last_library(self):
        """Loads the last selected library from preferences."""
        library_uri = Path.Preferences.getLastToolLibrary()
        if library_uri:
            try:
                library = self._asset_manager.get(library_uri, store="local", depth=1)
                self.set_current_library(library)
            except Exception as e:
                Path.Log.warning(f"Failed to load last tool library: {e}")

    def restore_last_sort_order(self):
        """Sets the sort mode and updates the tool list."""
        last_sort_key = Path.Preferences.getLastToolLibrarySortKey()
        if last_sort_key:
            self.set_sort_order(last_sort_key)

    def set_sort_order(self, key: str):
        super().set_sort_order(key)
        Path.Preferences.setLastToolLibrarySortKey(self._sort_key)

    def _get_state(self):
        """Gets the current library URI, selected toolbit URI, and scroll
        position."""
        current_library_uri_str = (
            str(self.current_library.get_uri()) if self.current_library else None
        )

        selected_toolbit_uris = []
        selected_items = self._tool_list_widget.selectedItems()
        if selected_items:
            selected_toolbit_uris = [item.data(ToolBitUriRole) for item in selected_items]

        scroll_pos = self._tool_list_widget.verticalScrollBar().value()

        return {
            "library_uri": current_library_uri_str,
            "toolbit_uris": selected_toolbit_uris,
            "scroll_pos": scroll_pos,
        }

    def _set_state(self, selection_data):
        """Restores the library selection, toolbit selection, and scroll
        position."""
        library_uri_str = selection_data.get("library_uri")
        toolbit_uris = selection_data.get("toolbit_uris", [])
        scroll_pos = selection_data.get("scroll_pos", 0)

        # Restore library selection
        if library_uri_str:
            try:
                library_uri = AssetUri(library_uri_str)
                library = self._asset_manager.get(library_uri, store=self._store_name, depth=1)
                self.set_current_library(library)
            except FileNotFoundError:
                Path.Log.error(f"Library {library_uri_str} not found.")
                self.set_current_library(None)
        else:
            self.set_current_library(None)

        # Restore toolbit selection
        if toolbit_uris:
            for uri in toolbit_uris:
                for i in range(self._tool_list_widget.count()):
                    item = self._tool_list_widget.item(i)
                    if item.data(ToolBitUriRole) == uri:
                        item.setSelected(True)

        # Restore scroll position
        self._tool_list_widget.verticalScrollBar().setValue(scroll_pos)

    def refresh(self):
        """Refreshes the toolbits for the current library from disk."""
        Path.Log.debug("refresh(): Fetching and populating toolbits.")
        if self.current_library:
            library_uri = self.current_library.get_uri()
            try:
                self.current_library = cast(
                    Library, self._asset_manager.get(library_uri, store=self._store_name, depth=1)
                )
            except FileNotFoundError:
                Path.Log.error(f"Library {library_uri} not found.")
                self.current_library = None
        self._update_tool_list()

    def get_tool_no_from_current_library(self, toolbit):
        """
        Retrieves the tool number for a toolbit based on the current library.
        """
        if not self.current_library:
            return None
        tool_no = self.current_library.get_bit_no_from_bit(toolbit)
        return tool_no

    def set_current_library(self, library):
        """Sets the current library and updates the tool list."""
        self.current_library = library
        self._update_tool_list()
        self.current_library_changed.emit()

        # Save the selected library to preferences
        if library:
            Path.Preferences.setLastToolLibrary(str(library.get_uri()))

    def _get_available_tool_types(self):
        """Get all available tool types from the current assets."""
        tool_types = set()
        # Make sure we have assets to work with
        if not hasattr(self, "_all_assets") or not self._all_assets:
            return []

        for asset in self._all_assets:
            # Use get_shape_name() method to get the tool type
            if hasattr(asset, "get_shape_name"):
                tool_type = asset.get_shape_name()
                if tool_type:
                    tool_types.add(tool_type)

        return sorted(tool_types)

    def _get_filtered_assets(self):
        """Get assets filtered by tool type if a specific type is selected."""
        if self._tool_type_combo.currentIndex() == 0:  # "All Toolbit Types"
            return self._all_assets

        filtered_assets = []
        for asset in self._all_assets:
            if hasattr(asset, "get_shape_name"):
                tool_type = asset.get_shape_name()
                if tool_type == self._selected_tool_type:
                    filtered_assets.append(asset)
        return filtered_assets

    def _update_tool_list(self):
        """Updates the tool list based on the current library."""
        if self.current_library:
            self._all_assets = [t for t in self.current_library]
        else:
            # Fetch all toolbits
            all_toolbits = self._asset_manager.fetch(asset_type="toolbit", depth=0)
            self._all_assets = cast(List[ToolBit], all_toolbits)
        self._sort_assets()
        self._tool_list_widget.clear_list()
        # Update tool type combo after assets are loaded
        if hasattr(self, "_tool_type_combo"):
            self._update_tool_type_combo()
        self._update_list()

    def _update_list(self):
        """Updates the list widget with filtered assets."""
        self._tool_list_widget.clear_list()
        filtered_assets = self._get_filtered_assets()

        # Apply search filter if there is one
        search_term = self._search_edit.text().lower()
        if search_term:
            search_filtered = []
            for asset in filtered_assets:
                if search_term in asset.label.lower():
                    search_filtered.append(asset)
                    continue
                # Also search in tool type
                if hasattr(asset, "get_shape_name"):
                    tool_type = asset.get_shape_name()
                    if tool_type and search_term in tool_type.lower():
                        search_filtered.append(asset)
            filtered_assets = search_filtered

        for asset in filtered_assets:
            self._tool_list_widget.add_toolbit(asset)

    def _add_shortcuts(self):
        """Adds keyboard shortcuts for common actions."""
        Path.Log.debug("LibraryBrowserWidget._add_shortcuts: Called.")
        super()._add_shortcuts()

        cut_action = QAction(self)
        cut_action.setShortcuts(QKeySequence.Cut)
        cut_action.triggered.connect(self._on_cut_requested)
        self.addAction(cut_action)

        duplicate_action = QAction(self)
        duplicate_action.setShortcut(QKeySequence("Ctrl+D"))
        duplicate_action.triggered.connect(self._on_duplicate_requested)
        self.addAction(duplicate_action)

        remove_action = QAction(self)
        remove_action.setShortcut(QKeySequence.Delete)
        remove_action.triggered.connect(self._on_remove_from_library_requested)
        self.addAction(remove_action)

        paste_action = QAction(self)
        paste_action.setShortcuts(QKeySequence.Paste)
        paste_action.triggered.connect(self._on_paste_requested)
        self.addAction(paste_action)

    def _show_context_menu(self, position):
        """Shows the context menu at the given position."""
        context_menu = QMenu(self)

        selected_items = self._tool_list_widget.selectedItems()
        has_selection = bool(selected_items)
        has_library = self.current_library is not None

        # Add actions in the desired order
        edit_action = context_menu.addAction("Edit", self._on_edit_requested)
        edit_action.setEnabled(has_selection)

        context_menu.addSeparator()

        action = context_menu.addAction("Copy", self._on_copy_requested)
        action.setShortcut(QtGui.QKeySequence("Ctrl+C"))

        action = context_menu.addAction("Cut", self._on_cut_requested)
        action.setShortcut(QtGui.QKeySequence("Ctrl+X"))

        action = context_menu.addAction("Paste", self._on_paste_requested)
        action.setShortcut(QtGui.QKeySequence("Ctrl+V"))

        # Paste is enabled if there is data in the clipboard
        clipboard = QtGui.QApplication.clipboard()
        mime_type = "application/x-freecad-toolbit-list-yaml"
        action.setEnabled(clipboard.mimeData().hasFormat(mime_type))

        action = context_menu.addAction("Duplicate", self._on_duplicate_requested)
        action.setShortcut(QtGui.QKeySequence("Ctrl+D"))

        context_menu.addSeparator()

        # Only show "Remove from Library" when viewing a specific library
        if has_library:
            action = context_menu.addAction(
                "Remove from Library", self._on_remove_from_library_requested
            )
            action.setShortcut(QtGui.QKeySequence.Delete)

        # Only show "Delete from disk" when viewing 'all tools' (no library selected)
        if not has_library:
            action = context_menu.addAction("Delete from disk", self._on_delete_requested)
            action.setShortcut(QtGui.QKeySequence("Shift+Delete"))

        # Execute the menu
        context_menu.exec_(self._tool_list_widget.mapToGlobal(position))

    def get_current_library(self) -> Library | None:
        """Helper to get the current library."""
        return self.current_library

    def _on_edit_requested(self):
        """Opens the ToolBitEditor for the selected toolbit."""
        toolbit = self._get_first_selected_bit()
        if not toolbit:
            return

        # Open the editor for the selected toolbit
        tool_no = self.get_tool_no_from_current_library(toolbit)
        editor = ToolBitEditor(toolbit, tool_no, parent=self)
        result = editor.show()
        if result != QtGui.QDialog.Accepted:
            return

        # If the editor was closed with "OK", save the changes
        self._asset_manager.add(toolbit)
        Path.Log.info(f"Toolbit {toolbit.get_id()} saved.")
        editor._restore_original_schema()

        # Also save the library because the tool number may have changed.
        if self.current_library and tool_no != editor.tool_no:
            self.current_library.assign_new_bit_no(toolbit, editor.tool_no)
            self._asset_manager.add(self.current_library)

        state = self._get_state()
        self.refresh()
        self._update_list()
        self._set_state(state)

    def _on_cut_requested(self):
        """Handles cut request by copying and marking for removal from library."""
        uris = self.get_selected_bit_uris()
        library = self.get_current_library()
        if not library or not uris:
            return

        # Copy to clipboard (handled by base class _to_clipboard)
        extra_data = {"source_library_uri": str(library.get_uri())}
        self._to_clipboard(uris, mode="cut", extra_data=extra_data)

    def _on_duplicate_requested(self):
        """Handles duplicate request by duplicating and adding to library."""
        Path.Log.debug("LibraryBrowserWidget._on_duplicate_requested: Called.\n")
        uris = self.get_selected_bit_uris()
        library = self.get_current_library()
        if not library or not uris:
            Path.Log.debug(
                "LibraryBrowserWidget._on_duplicate_requested: No library or URIs selected. Returning."
            )
            return

        new_uris = set()
        for uri_string in uris:
            toolbit = cast(ToolBit, self._asset_manager.get(AssetUri(uri_string), depth=0))
            if not toolbit:
                Path.Log.warning(f"Toolbit {uri_string} not found.\n")
                continue

            # Change the ID of the toolbit and save it to disk
            toolbit.set_id()  # Generate a new ID
            toolbit.label = toolbit.label + " (copy)"
            added_uri = self._asset_manager.add(toolbit)
            if added_uri:
                new_uris.add(str(toolbit.get_uri()))

            # Add the bit to the current library
            library.add_bit(toolbit)

        self._asset_manager.add(library)  # Save the modified library
        self.refresh()

        self.select_by_uri(list(new_uris))

    def _on_paste_requested(self):
        """Handles paste request by adding toolbits to the current library."""
        current_library = self.get_current_library()
        if not current_library:
            return

        clipboard = QtGui.QApplication.clipboard()
        mime_type = "application/x-freecad-toolbit-list-yaml"
        mime_data = clipboard.mimeData()

        if not mime_data.hasFormat(mime_type):
            return

        try:
            clipboard_content_yaml = mime_data.data(mime_type).data().decode("utf-8")
            clipboard_data_dict = yaml.safe_load(clipboard_content_yaml)

            if (
                not isinstance(clipboard_data_dict, dict)
                or "toolbits" not in clipboard_data_dict
                or not isinstance(clipboard_data_dict["toolbits"], list)
            ):
                return

            serialized_toolbits_data = clipboard_data_dict["toolbits"]
            mode = clipboard_data_dict.get("operation", "copy")
            source_library_uri_str = clipboard_data_dict.get("source_library_uri")

            if mode == "copy":
                self._on_copy_paste(current_library, serialized_toolbits_data)
            elif mode == "cut" and source_library_uri_str:
                self._on_cut_paste(
                    current_library, serialized_toolbits_data, source_library_uri_str
                )

        except Exception as e:
            Path.Log.warning(f"An unexpected error occurred during paste: {e}")

    def _on_copy_paste(self, current_library: Library, serialized_toolbits_data: list):
        """Handles pasting toolbits that were copied."""
        new_uris = set()
        for toolbit_yaml_str in serialized_toolbits_data:
            if not isinstance(toolbit_yaml_str, str) or not toolbit_yaml_str.strip():
                continue

            toolbit_data_bytes = toolbit_yaml_str.encode("utf-8")
            toolbit = YamlToolBitSerializer.deserialize(toolbit_data_bytes, dependencies=None)

            # Get the original toolbit ID from the deserialized data
            original_id = toolbit.id
            Path.Log.info(f"COPY PASTE: Attempting to paste toolbit with original_id={original_id}")

            # Check if toolbit already exists in asset manager
            toolbit_uri = toolbit.get_uri()
            existing_toolbit = None
            try:
                existing_toolbit = self._asset_manager.get(
                    toolbit_uri, store=["local", "builtin"], depth=0
                )
                Path.Log.info(f"COPY PASTE: Found existing toolbit {original_id}, using reference")
            except FileNotFoundError:
                # Toolbit doesn't exist, save it as new
                Path.Log.info(f"COPY PASTE: Toolbit {original_id} not found, creating new one")
                self._asset_manager.add(toolbit)
                existing_toolbit = toolbit

            # Add the existing or new toolbit to the current library
            added_toolbit = current_library.add_bit(existing_toolbit)
            if added_toolbit:
                new_uris.add(str(existing_toolbit.get_uri()))

        if new_uris:
            self._asset_manager.add(current_library)  # Save the modified library
            self.refresh()
            self.select_by_uri(list(new_uris))

    def _on_cut_paste(
        self,
        current_library: Library,
        serialized_toolbits_data: list,
        source_library_uri_str: str,
    ):
        """Handles pasting toolbits that were cut."""
        source_library_uri = AssetUri(source_library_uri_str)
        if source_library_uri == current_library.get_uri():
            # Cut from the same library, do nothing
            return

        try:
            source_library = cast(
                Library,
                self._asset_manager.get(source_library_uri, store=self._store_name, depth=1),
            )
        except FileNotFoundError:
            Path.Log.warning(f"Source library {source_library_uri_str} not found.\n")
            return

        new_uris = set()
        for toolbit_yaml_str in serialized_toolbits_data:
            if not isinstance(toolbit_yaml_str, str) or not toolbit_yaml_str.strip():
                continue

            toolbit_data_bytes = toolbit_yaml_str.encode("utf-8")
            toolbit = YamlToolBitSerializer.deserialize(toolbit_data_bytes, dependencies=None)

            # Get the original toolbit ID and find the existing toolbit
            original_id = toolbit.id
            Path.Log.info(f"CUT PASTE: Moving toolbit with original_id={original_id}")

            toolbit_uri = toolbit.get_uri()
            try:
                existing_toolbit = self._asset_manager.get(
                    toolbit_uri, store=["local", "builtin"], depth=0
                )
                Path.Log.info(f"CUT PASTE: Found existing toolbit {original_id}, using reference")

                # Remove from source library, add to target library
                source_library.remove_bit(existing_toolbit)
                added_toolbit = current_library.add_bit(existing_toolbit)
                if added_toolbit:
                    new_uris.add(str(existing_toolbit.get_uri()))
            except FileNotFoundError:
                Path.Log.warning(f"CUT PASTE: Toolbit {original_id} not found in asset manager")

        if new_uris:
            # Save the modified libraries
            self._asset_manager.add(current_library)
            self._asset_manager.add(source_library)
            self.refresh()
            self.select_by_uri(list(new_uris))

    def _on_remove_from_library_requested(self):
        """Handles request to remove selected toolbits from the current library."""
        Path.Log.debug("_on_remove_from_library_requested: Called.")
        uris = self.get_selected_bit_uris()
        library = self.get_current_library()
        if not library or not uris:
            return

        # Ask for confirmation
        reply = QtGui.QMessageBox.question(
            self,
            FreeCAD.Qt.translate("CAM", "Confirm Removal"),
            FreeCAD.Qt.translate(
                "CAM", "Are you sure you want to remove the selected toolbit(s) from the library?"
            ),
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
            QtGui.QMessageBox.No,
        )

        if reply == QtGui.QMessageBox.Yes:
            self._remove_toolbits_from_library(library, uris)

    def _remove_toolbits_from_library(self, library: Library, uris: List[str]):
        """Removes toolbits with the given URIs from the specified library."""
        removed_count = 0
        for uri_string in uris:
            try:
                # Remove the toolbit from the library
                library.remove_bit_by_uri(uri_string)
                removed_count += 1
            except Exception as e:
                Path.Log.error(f"Failed to remove toolbit {uri_string} from library: {e}\n")

        if removed_count > 0:
            self._asset_manager.add(library)
            self.refresh()

    def _update_tool_type_combo(self):
        """Update the tool type combo box with available types."""
        current_selection = self._tool_type_combo.currentText()
        self._tool_type_combo.blockSignals(True)
        try:
            self._tool_type_combo.clear()
            self._tool_type_combo.addItem(FreeCAD.Qt.translate("CAM", "All Toolbit Types"))

            for tool_type in self._get_available_tool_types():
                self._tool_type_combo.addItem(tool_type)

            # Restore selection if it still exists
            index = self._tool_type_combo.findText(current_selection)
            if index >= 0:
                self._tool_type_combo.setCurrentIndex(index)
            else:
                self._tool_type_combo.setCurrentIndex(0)
        finally:
            self._tool_type_combo.blockSignals(False)

    def _on_tool_type_combo_changed(self, tool_type):
        """Handle tool type filter selection change."""
        self._selected_tool_type = tool_type
        self._update_list()


class LibraryBrowserWithCombo(LibraryBrowserWidget):
    """
    A widget extending LibraryBrowserWidget with a combo box for library selection.
    """

    def __init__(
        self,
        asset_manager: AssetManager,
        store: str = "local",
        parent=None,
        compact=True,
    ):
        super().__init__(
            asset_manager=asset_manager,
            store=store,
            parent=parent,
            compact=compact,
        )

        # Move search box into dedicated row to make space for the
        # library selection combo box
        layout = self.layout()
        self._top_layout.removeWidget(self._search_edit)
        layout.insertWidget(1, self._search_edit, 20)

        # Library selection combo box
        self._library_combo = QtGui.QComboBox()
        self._library_combo.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred)
        self._top_layout.insertWidget(0, self._library_combo, 1)
        self._library_combo.currentIndexChanged.connect(self._on_library_combo_changed)

        self._top_layout.removeWidget(self._tool_type_combo)
        self._top_layout.insertWidget(1, self._tool_type_combo, 1)

        self.current_library_changed.connect(self._on_current_library_changed)

        self._in_refresh = False
        self.refresh()

    def _on_library_combo_changed(self, index):
        """Handles library selection change from the combo box."""
        if self._in_refresh:
            return

        selected_library = cast(Library, self._library_combo.itemData(index))
        if not selected_library:
            return

        # Have to refetch the non-shallow library.
        uri = selected_library.get_uri()
        library = self._asset_manager.get(uri, store=self._store_name, depth=1)
        self.set_current_library(library)

    def _on_current_library_changed(self):
        """Updates the combo box when the current library changes externally."""
        if self.current_library:
            for i in range(self._library_combo.count()):
                lib = self._library_combo.itemData(i)
                if lib.get_uri() == self.current_library.get_uri():
                    self._library_combo.setCurrentIndex(i)
                    return
            Path.Log.warning(
                f"Current library {self.current_library.get_uri()} not found in combo box."
            )

    def refresh(self):
        """Reads available libraries and refreshes the combo box and toolbits."""
        Path.Log.debug("refresh(): Fetching and populating libraries and toolbits.")
        libraries = self._asset_manager.fetch("toolbitlibrary", store=self._store_name, depth=0)
        self._in_refresh = True
        try:
            self._library_combo.clear()
            for library in sorted(libraries, key=lambda x: natural_sort_key(x.label)):
                self._library_combo.addItem(library.label, userData=library)
        finally:
            self._in_refresh = False

        super().refresh()

        if not libraries:
            return
        if not self.current_library:
            first_library = self._library_combo.itemData(0)
            if first_library:
                uri = first_library.get_uri()
                library = self._asset_manager.get(uri, store=self._store_name, depth=1)
                self.set_current_library(library)
            self._library_combo.setCurrentIndex(0)
            return

        for i in range(self._library_combo.count()):
            lib = self._library_combo.itemData(i)
            if lib.get_uri() == self.current_library.get_uri():
                self._library_combo.setCurrentIndex(i)
                break
        else:
            self._library_combo.setCurrentIndex(0)