Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assembly: BOM follow up #14790

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 157 additions & 26 deletions src/Mod/Assembly/CommandCreateBom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

Check warning on line 1 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

would reformat src/Mod/Assembly/CommandCreateBom.py

Check warning on line 1 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Missing module docstring (missing-module-docstring)
# /**************************************************************************
# *
# Copyright (c) 2023 Ondsel <development@ondsel.com> *
Expand Down Expand Up @@ -32,10 +32,9 @@
from PySide import QtCore, QtGui, QtWidgets
from PySide.QtWidgets import QPushButton, QMenu

import UtilsAssembly

Check warning on line 35 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import "import UtilsAssembly" should be placed at the top of the module (wrong-import-position)
import Preferences

# translate = App.Qt.translate
from functools import partial

__title__ = "Assembly Command Create Bill of Materials"
__author__ = "Ondsel"
Expand All @@ -44,11 +43,11 @@
translate = App.Qt.translate

TranslatedColumnNames = [
translate("Assembly", "Index"),
translate("Assembly", "Name"),
translate("Assembly", "Index (auto)"),
translate("Assembly", "Name (auto)"),
translate("Assembly", "Description"),
translate("Assembly", "File Name"),
translate("Assembly", "Quantity"),
translate("Assembly", "File Name (auto)"),
translate("Assembly", "Quantity (auto)"),
]

ColumnNames = [
Expand All @@ -67,22 +66,22 @@
def GetResources(self):
return {
"Pixmap": "Assembly_BillOfMaterials",
"MenuText": QT_TRANSLATE_NOOP("Assembly_BillOfMaterials", "Create Bill of Materials"),
"MenuText": QT_TRANSLATE_NOOP("Assembly_CreateBom", "Create Bill of Materials"),
"Accel": "O",
"ToolTip": "<p>"
+ QT_TRANSLATE_NOOP(
"Assembly_CreateView",
"Assembly_CreateBom",
"Create a bill of materials of the current assembly. If an assembly is active, it will be a BOM of this assembly. Else it will be a BOM of the whole document.",

Check warning on line 74 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (176/100) (line-too-long)
)
+ "</p><p>"
+ QT_TRANSLATE_NOOP(
"Assembly_CreateView",
"Assembly_CreateBom",
"The BOM object is a document object that stores the settings of your BOM. It is also a spreadsheet object so you can easily visualize the bom. If you don't need the BOM object to be saved as a document object, you can simply export and cancel the task.",

Check warning on line 79 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (271/100) (line-too-long)
)
+ "</p><p>"
+ QT_TRANSLATE_NOOP(
"Assembly_CreateView",
"The columns 'Index', 'Name', 'File Name' and 'Quantity' are automatically generated on recompute. The 'Description' and custom columns are not overwriten.",
"Assembly_CreateBom",
"The columns 'Index', 'Name', 'File Name' and 'Quantity' are automatically generated on recompute. The 'Description' and custom columns are not overwritten.",

Check warning on line 84 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (174/100) (line-too-long)
)
+ "</p>",
"CmdType": "ForEdit",
Expand All @@ -93,11 +92,11 @@

def Activated(self):
self.panel = TaskAssemblyCreateBom()
Gui.Control.showDialog(self.panel)

Check failure on line 95 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Possibly using variable 'Gui' before assignment (possibly-used-before-assignment)


######### Create Exploded View Task ###########
class TaskAssemblyCreateBom(QtCore.QObject):

Check failure on line 99 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Possibly using variable 'QtCore' before assignment (possibly-used-before-assignment)
def __init__(self, bomObj=None):
super().__init__()

Expand All @@ -105,7 +104,7 @@

# Set the QListWidget properties to support drag and drop
self.form.columnList.setEditTriggers(
QtWidgets.QAbstractItemView.DoubleClicked | QtWidgets.QAbstractItemView.EditKeyPressed

Check failure on line 107 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Possibly using variable 'QtWidgets' before assignment (possibly-used-before-assignment)
)
self.form.columnList.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.form.columnList.setDragEnabled(True)
Expand All @@ -115,22 +114,22 @@

self.form.columnList.installEventFilter(self)

self.form.btnAddColumn.clicked.connect(self.addColumn)
self.form.btnAddColumn.clicked.connect(self.showAddColumnMenu)
self.form.btnExport.clicked.connect(self.export)

self.form.helpButton.clicked.connect(self.showHelpDialog)

pref = Preferences.preferences()

if bomObj:
App.setActiveTransaction("Edit Bill Of Materials")

names = []
for i in range(len(bomObj.columnsNames)):
text = bomObj.columnsNames[i]
if text in ColumnNames:
index = ColumnNames.index(text)
text = TranslatedColumnNames[index]
names.append(text)
self.form.columnList.addItems(names)
for name in bomObj.columnsNames:
if name in ColumnNames:
index = ColumnNames.index(name)
name = TranslatedColumnNames[index]

self.addColItem(name)

self.bomObj = bomObj
self.form.CheckBox_onlyParts.setChecked(bomObj.onlyParts)
Expand All @@ -139,7 +138,10 @@

else:
App.setActiveTransaction("Create Bill Of Materials")
self.form.columnList.addItems(TranslatedColumnNames)

# Add the columns
for name in TranslatedColumnNames:
self.addColItem(name)

self.createBomObject()
self.form.CheckBox_onlyParts.setChecked(pref.GetBool("BOMOnlyParts", False))
Expand Down Expand Up @@ -199,15 +201,67 @@
counter += 1
new_name = f"{new_name}_{counter}"

new_item = QtWidgets.QListWidgetItem(new_name)
new_item.setFlags(new_item.flags() | QtCore.Qt.ItemIsEditable)
self.form.columnList.addItem(new_item)
item = self.addColItem(new_name)

# Ensure the new item is selected and starts editing
self.form.columnList.setCurrentItem(new_item)
self.form.columnList.editItem(new_item)
self.form.columnList.setCurrentItem(item)
self.form.columnList.editItem(item)
self.updateColumnList()

def addColItem(self, name):
item = QtWidgets.QListWidgetItem(name)

isCustomCol = self.isCustomColumn(name)

if isCustomCol:
item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
else:
font = item.font()
font.setBold(True)
item.setFont(font)

self.form.columnList.addItem(item)
return item

def showAddColumnMenu(self):
menu = QtWidgets.QMenu()
# Get the current columns in the list
current_columns = [
self.form.columnList.item(i).text() for i in range(self.form.columnList.count())
]

# Add actions for columns that are not currently in the list
noneAdded = True
for name in TranslatedColumnNames:
if name not in current_columns:
action = QtWidgets.QAction(f"Add '{name}' column", self)
action.triggered.connect(partial(self.addColItem, name))
menu.addAction(action)
noneAdded = False

if noneAdded:
self.addColumn()
return

# Add the action for adding a custom column
action = QtWidgets.QAction("Add custom column", self)
action.triggered.connect(self.addColumn)
menu.addAction(action)

# Show the menu below the button
menu.exec_(
self.form.btnAddColumn.mapToGlobal(QtCore.QPoint(0, self.form.btnAddColumn.height()))
)

def isCustomColumn(self, name):
isCustomCol = True
if name in TranslatedColumnNames:
# Description column is currently not auto generated so it's a custom column
index = TranslatedColumnNames.index(name)
if ColumnNames[index] != "Description":
isCustomCol = False
return isCustomCol

def onItemsReordered(self, parent, start, end, destination, row):
self.updateColumnList()

Expand Down Expand Up @@ -248,8 +302,20 @@
)
item.setText(old_text)
else:
isCustomCol = self.isCustomColumn(new_text)

if not isCustomCol:
font = item.font()
font.setBold(True)
item.setFont(font)
# Use a single-shot timer to defer changing the flags (else FC crashes)
QtCore.QTimer.singleShot(0, lambda: self.makeItemNonEditable(item))

self.updateColumnList()

def makeItemNonEditable(self, item):
item.setFlags(item.flags() & ~QtCore.Qt.ItemIsEditable)

def isNameDuplicate(self, name):
for i in range(self.form.columnList.count()):
if self.form.columnList.item(i).text() == name:
Expand Down Expand Up @@ -289,6 +355,71 @@

return super().eventFilter(watched, event)

def showHelpDialog(self):
help_dialog = QtWidgets.QDialog(self.form)
help_dialog.setWindowFlags(QtCore.Qt.Popup)
help_dialog.setWindowModality(QtCore.Qt.NonModal)
help_dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)

layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(10, 10, 10, 10)

options_title = QtWidgets.QLabel("<b>" + translate("Assembly", "Options:") + "</b>")
options_text = QtWidgets.QLabel(
" - "
+ translate(
"Assembly",
"Sub-assemblies children : If checked, Sub assemblies children will be added to the bill of materials.",

Check warning on line 372 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (120/100) (line-too-long)
)
+ "\n"
" - "
+ translate(
"Assembly",
"Parts children : If checked, Parts children will be added to the bill of materials.",

Check warning on line 378 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (102/100) (line-too-long)
)
+ "\n"
" - "
+ translate(
"Assembly",
"Only parts : If checked, only Part containers and sub-assemblies will be added to the bill of materials. Solids like PartDesign Bodies, fasteners or Part workbench primitives will be ignored.",

Check warning on line 384 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (210/100) (line-too-long)
)
+ "\n"
)
columns_title = QtWidgets.QLabel("<b>" + translate("Assembly", "Columns:") + "</b>")
columns_text = QtWidgets.QLabel(
" - "
+ translate(
"Assembly",
"Auto columns : (Index, Quantity, Name...) are populated automatically. Any modification you make will be overridden. These columns cannot be renamed.",

Check warning on line 393 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (169/100) (line-too-long)
)
+ "\n"
" - "
+ translate(
"Assembly",
"Custom columns : 'Description' and other custom columns you add by clicking on 'Add column' will not have their data overwritten. These columns can be renamed by double-clicking or pressing F2 (Renaming a column will currently lose its data).",

Check warning on line 399 in src/Mod/Assembly/CommandCreateBom.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (261/100) (line-too-long)
)
+ "\n"
"\n"
+ translate(
"Assembly",
"Any column (custom or not) can be deleted by pressing Del.",
)
+ "\n"
)

options_text.setWordWrap(True)
columns_text.setWordWrap(True)

layout.addWidget(options_title)
layout.addWidget(options_text)
layout.addWidget(columns_title)
layout.addWidget(columns_text)

help_dialog.setLayout(layout)
help_dialog.setFixedWidth(500)

help_dialog.show()


if App.GuiUp:
Gui.addCommand("Assembly_CreateBom", CommandCreateBom())
41 changes: 29 additions & 12 deletions src/Mod/Assembly/Gui/Resources/panels/TaskAssemblyCreateBom.ui
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<string>Create Bill Of Materials</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<item row="0" column="0" colspan="2">
<widget class="Gui::PrefCheckBox" name="CheckBox_detailSubAssemblies">
<property name="toolTip">
<string>If checked, Sub assemblies sub-components will be added to the bill of materials.</string>
<string>If checked, Sub assemblies children will be added to the bill of materials.</string>
</property>
<property name="text">
<string>Detail sub-assemblies</string>
<string>Sub-assemblies children</string>
</property>
<property name="checked">
<bool>true</bool>
Expand All @@ -33,13 +33,13 @@
</property>
</widget>
</item>
<item row="1" column="0">
<item row="1" column="0" colspan="2">
<widget class="Gui::PrefCheckBox" name="CheckBox_detailParts">
<property name="toolTip">
<string>If checked, Parts sub-components will be added to the bill of materials.</string>
<string>If checked, Parts children will be added to the bill of materials.</string>
</property>
<property name="text">
<string>Detail parts</string>
<string>Parts children</string>
</property>
<property name="checked">
<bool>true</bool>
Expand All @@ -52,10 +52,10 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="2" column="0" colspan="2">
<widget class="Gui::PrefCheckBox" name="CheckBox_onlyParts">
<property name="toolTip">
<string>If checked, only Part containers will be added to the bill of materials. Solids like PartDesign Bodies will be ignored.</string>
<string>If checked, only Part containers and sub-assemblies will be added to the bill of materials. Solids like PartDesign Bodies, fasteners or Part workbench primitives will be ignored.</string>
</property>
<property name="text">
<string>Only parts</string>
Expand All @@ -71,11 +71,8 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="3" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_1">
<property name="toolTip">
<string>Columns of the bill of materials</string>
</property>
<property name="title">
<string>Columns</string>
</property>
Expand All @@ -100,6 +97,26 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QToolButton" name="helpButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Help</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="Icons/resource.qrc">
<normaloff>:/icons/help-browser.svg</normaloff>:/icons/help-browser.svg</iconset>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
Expand Down
Loading
Loading