-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathFSSearchCmd.py
More file actions
356 lines (314 loc) · 13.6 KB
/
Copy pathFSSearchCmd.py
File metadata and controls
356 lines (314 loc) · 13.6 KB
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
# -*- coding: utf-8 -*-
###############################################################################
#
# FastenerSearch.py
#
# Copyright 2025 Shai Seger <shaise at gmail dot com>
# BSP modifications (c) 2025-2026 Andrey Bekhterev <info at bekhterev dot in>
#
# 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.
#
#
###############################################################################
import os
import FreeCADGui
import FreeCAD
from FreeCAD import Gui
from FSAliases import FSGetIconAlias
import FSutils
import FastenerBase
from FastenersCmd import FSScrewCommandTable
from FSutils import iconPath
try:
from PySide import QtCore, QtGui, QtWidgets
except ImportError:
from PySide2 import QtCore, QtGui, QtWidgets
translate = FreeCAD.Qt.translate
# Locate the icons directory shipped with the workbench
_FW_DIR = os.path.dirname(__file__)
_ICONS_DIR = os.path.join(_FW_DIR, "Icons")
class _FastenerRow(QtWidgets.QWidget):
"""A row widget that properly reports its own size to QListWidget."""
def __init__(self, icon_pixmap, desc_text, icon_size=48, parent=None):
super().__init__(parent)
self._icon_size = icon_size
lay = QtWidgets.QHBoxLayout(self)
lay.setContentsMargins(0, 0, 0, 0)
lay.setSpacing(14)
lay.setAlignment(QtCore.Qt.AlignVCenter)
# Icon
lbl_icon = QtWidgets.QLabel()
lbl_icon.setFixedSize(icon_size, icon_size)
lbl_icon.setAlignment(QtCore.Qt.AlignCenter)
lbl_icon.setScaledContents(True)
if icon_pixmap and not icon_pixmap.isNull():
lbl_icon.setPixmap(icon_pixmap)
else:
lbl_icon.setText("\U0001F527")
lbl_icon.setStyleSheet(
"font-size: 28px; background: #333; border-radius: 2px;"
)
lay.addWidget(lbl_icon, 0, QtCore.Qt.AlignVCenter)
# Description
lbl_desc = QtWidgets.QLabel(desc_text)
lbl_desc.setWordWrap(True)
lbl_desc.setTextFormat(QtCore.Qt.PlainText)
lbl_desc.setAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignLeft)
font = lbl_desc.font()
font.setPointSize(font.pointSize() + 1)
lbl_desc.setFont(font)
lbl_desc.setStyleSheet("color: #1a1a1a; padding: 2px 0;")
lay.addWidget(lbl_desc, 1)
def sizeHint(self):
# Force the layout to compute its geometry
self.layout().activate()
sh = self.layout().totalSizeHint()
# Ensure minimum height = icon + vertical padding
min_h = self._icon_size + 16
return QtCore.QSize(sh.width(), max(sh.height(), min_h))
# ═══════════════════════════════════════════════════════════════════════════
# Dialog
# ═══════════════════════════════════════════════════════════════════════════
class FastenerSearchDialog(QtWidgets.QDialog):
"""Pop-up that lets the user search every fastener by description and
insert one into the active document with a single click."""
ICON_SIZE = 48
MAX_DESC_LINES = 3
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Search Fasteners")
self.setMinimumSize(520, 620)
self.resize(560, 700)
self.setWindowFlags(
self.windowFlags()
| QtCore.Qt.WindowMinMaxButtonsHint
| QtCore.Qt.WindowCloseButtonHint
)
self._build_ui()
self._apply_filter("") # show everything at first
# ── UI construction ─────────────────────────────────────────────────
def _build_ui(self):
root = QtWidgets.QVBoxLayout(self)
root.setContentsMargins(8, 8, 8, 6)
root.setSpacing(4)
# Title
title = QtWidgets.QLabel(translate("FastenerSearch", "Search Fasteners"))
title.setStyleSheet(
"font-size: 18px; font-weight: bold; color: #848484;"
)
root.addWidget(title)
# Search bar
self.search_edit = QtWidgets.QLineEdit()
self.search_edit.setPlaceholderText(
translate("FastenerSearch", "Type to search by description") + "\u2026"
)
self.search_edit.setClearButtonEnabled(True)
self.search_edit.setStyleSheet(
"QLineEdit {"
" padding: 4px 6px;"
" border: 2px solid #444;"
" border-radius: 2px;"
" background: #bababa;"
" color: #1a1a1a;"
" font-size: 14px;"
" selection-background-color: #5a5a5a;"
"}"
"QLineEdit:focus { border-color: #6a9fd8; }"
)
root.addWidget(self.search_edit)
# Result count
self.count_label = QtWidgets.QLabel("")
self.count_label.setStyleSheet(
"color: #777; font-size: 11px; padding-left: 2px;"
)
root.addWidget(self.count_label)
# List
self.list_widget = QtWidgets.QListWidget()
self.list_widget.setSpacing(3)
self.list_widget.setUniformItemSizes(False)
self.list_widget.setIconSize(QtCore.QSize(self.ICON_SIZE, self.ICON_SIZE))
self.list_widget.setResizeMode(QtWidgets.QListWidget.Adjust)
self.list_widget.setStyleSheet(
"QListWidget {"
" border: 1px solid #3a3a3a;"
" border-radius: 2px;"
" background: #bababa;"
" padding: 4px;"
" outline: none;"
"}"
"QListWidget::item {"
" border-radius: 2px;"
" padding: 2px;"
" height: 48px;"
" margin: 1px 2px;"
" color: #1a1a1a;"
"}"
"QListWidget::item:selected {"
" background: #909090;"
" border: 1px solid #5a7a9a;"
" color: #d0d0d0;"
"}"
"QListWidget::item:hover {"
" background: #a0a0a0;"
" color: #d0d0d0;"
"}"
)
root.addWidget(self.list_widget, 1)
# Buttons
btn_row = QtWidgets.QHBoxLayout()
btn_row.addStretch()
self.insert_btn = QtWidgets.QPushButton(translate("FastenerSearch", "Insert Fastener"))
self.insert_btn.setEnabled(False)
self.insert_btn.setFixedHeight(36)
self.insert_btn.setStyleSheet(
"QPushButton {"
" padding: 6px 24px;"
" background: #3d6b99;"
" color: #fff;"
" border: none;"
" border-radius: 4px;"
" font-weight: bold;"
" font-size: 13px;"
"}"
"QPushButton:hover { background: #4a7fb3; }"
"QPushButton:pressed { background: #2d5580; }"
"QPushButton:disabled { background: #bababa; color: #aaa; }"
)
cancel_btn = QtWidgets.QPushButton(translate("FastenerSearch", "Close"))
cancel_btn.setFixedHeight(36)
cancel_btn.setStyleSheet(
"QPushButton {"
" padding: 6px 24px;"
" background: #bababa;"
" color: #111;"
" border: none;"
" border-radius: 2px;"
" font-size: 13px;"
"}"
"QPushButton:hover { background: #aaa; }"
)
btn_row.addWidget(self.insert_btn)
btn_row.addWidget(cancel_btn)
root.addLayout(btn_row)
# Connections
self.search_edit.textChanged.connect(self._apply_filter)
self.list_widget.itemDoubleClicked.connect(self._on_insert)
self.insert_btn.clicked.connect(self._on_insert)
cancel_btn.clicked.connect(self.reject)
self.list_widget.itemSelectionChanged.connect(
lambda: self.insert_btn.setEnabled(
self.list_widget.currentItem() is not None
)
)
# ── Filtering ───────────────────────────────────────────────────────
def _apply_filter(self, text):
self.list_widget.clear()
self.insert_btn.setEnabled(False)
text_lower = text.strip().lower()
if len(text_lower) < 2:
self.count_label.setText(translate("FastenerSearch", "Type at least 2 characters to search"))
return
matched = []
for ftype, info in FSScrewCommandTable.items():
desc = info[0]
group = info[1]
cmd_name = 'FS' + ftype
icon = os.path.join(iconPath, FSGetIconAlias(ftype) + '.svg')
# Search against both the menu text, group and the description
searchable = (ftype + " " + desc + " " + group).lower()
text_words = text_lower.split()
if all(word in searchable for word in text_words):
matched.append((cmd_name, ftype, desc, group, icon))
found_txt = translate("FastenerSearch", "found")
fastener_txt = translate("FastenerSearch", "fastener")
fasteners_txt = translate("FastenerSearch", "fasteners")
self.count_label.setText(
f"{len(matched)} {fastener_txt if len(matched) == 1 else fasteners_txt} {found_txt}"
)
for cmd_name, ftype, desc, group, icon in matched:
self._add_item(cmd_name, ftype, desc, group, icon)
def _add_item(self, cmd_name, ftype, desc, group, pixmap):
icon = self._load_icon(pixmap)
item = QtWidgets.QListWidgetItem()
item.setData(QtCore.Qt.UserRole, cmd_name)
# item.setToolTip(desc)
pixmap_obj = None
if icon and not icon.isNull():
pixmap_obj = icon.pixmap(self.ICON_SIZE, self.ICON_SIZE)
desc_text = ftype + " (" + group + ")" + "\n" + desc if desc else ftype
row = _FastenerRow(pixmap_obj, desc_text, self.ICON_SIZE)
self.list_widget.addItem(item)
self.list_widget.setItemWidget(item, row)
# ── Icon loader ─────────────────────────────────────────────────────
def _load_icon(self, pixmap_path):
if not pixmap_path:
return QtGui.QIcon()
icon = QtGui.QIcon(pixmap_path)
if not icon.isNull():
return icon
return QtGui.QIcon()
# ── Insert action ───────────────────────────────────────────────────
def _on_insert(self):
item = self.list_widget.currentItem()
if item is None:
return
cmd_name = item.data(QtCore.Qt.UserRole)
if not cmd_name:
return
if FreeCAD.ActiveDocument is None:
FreeCAD.newDocument("Fasteners")
try:
FreeCADGui.runCommand(str(cmd_name))
except Exception as exc:
FreeCAD.Console.PrintError(
f"[FastenerSearch] Failed to run '{cmd_name}': {exc}\n"
)
# ═══════════════════════════════════════════════════════════════════════════
# FreeCAD Command wrapper
# ═══════════════════════════════════════════════════════════════════════════
class FSSearchCommand:
"""FreeCAD command that opens the fastener search dialog."""
_dialog = None
def GetResources(self):
icon = os.path.join(FSutils.iconPath, "IconSearch.svg")
return {
"Pixmap": icon,
"MenuText": translate("FastenerSearch", "Search Fasteners"),
"ToolTip": (translate(
"FastenerSearch",
"Search fasteners by name or description."
)),
}
def Activated(self):
# Re-use a single dialog instance; bring it forward if already open
if FSSearchCommand._dialog is None:
FSSearchCommand._dialog = FastenerSearchDialog(
FreeCADGui.getMainWindow()
)
FSSearchCommand._dialog.setAttribute(
QtCore.Qt.WA_DeleteOnClose
)
FSSearchCommand._dialog.destroyed.connect(
lambda: setattr(FSSearchCommand, "_dialog", None)
)
FSSearchCommand._dialog.show()
FSSearchCommand._dialog.raise_()
FSSearchCommand._dialog.activateWindow()
FSSearchCommand._dialog.search_edit.setFocus()
def IsActive(self):
return True
Gui.addCommand("Fasteners_Search", FSSearchCommand())
FastenerBase.FSCommands.append("Fasteners_Search", "command")