FreeCAD
129 строк · 5.6 Кб
1# SPDX-License-Identifier: LGPL-2.1-or-later
2# ***************************************************************************
3# * *
4# * Copyright (c) 2022 FreeCAD Project Association *
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""" Contains a wrapper class for a table listing authors and maintainers """
25
26import os27
28from PySide.QtWidgets import QTableWidgetItem29from PySide.QtGui import QIcon30
31import FreeCAD32import FreeCADGui33
34from addonmanager_devmode_license_selector import LicenseSelector35
36translate = FreeCAD.Qt.translate37
38# pylint: disable=too-few-public-methods
39
40
41class LicensesTable:42"""A QTableWidget and associated buttons for managing the list of authors and maintainers."""43
44def __init__(self):45self.widget = FreeCADGui.PySideUic.loadUi(46os.path.join(os.path.dirname(__file__), "developer_mode_licenses_table.ui")47)48
49self.widget.addButton.setIcon(QIcon.fromTheme("add", QIcon(":/icons/list-add.svg")))50self.widget.removeButton.setIcon(51QIcon.fromTheme("remove", QIcon(":/icons/list-remove.svg"))52)53
54self.widget.addButton.clicked.connect(self._add_clicked)55self.widget.removeButton.clicked.connect(self._remove_clicked)56self.widget.tableWidget.itemSelectionChanged.connect(self._selection_changed)57self.widget.tableWidget.itemDoubleClicked.connect(self._edit)58self.metadata = None59self.path_to_addon = ""60
61def show(self, metadata, path_to_addon):62"""Set up the widget based on incoming metadata"""63self.metadata = metadata64self.path_to_addon = path_to_addon65self._populate_from_metadata()66self.widget.removeButton.setDisabled(True)67self.widget.show()68
69def _populate_from_metadata(self):70"""Use the passed metadata object to populate the maintainers and authors"""71self.widget.tableWidget.setRowCount(0)72row = 073for lic in self.metadata.License:74shortcode = lic["name"]75path = lic["file"]76self._add_row(row, shortcode, path)77row += 178
79def _add_row(self, row, shortcode, path):80"""Add this license to the tableWidget at row given"""81self.widget.tableWidget.insertRow(row)82self.widget.tableWidget.setItem(row, 0, QTableWidgetItem(shortcode))83self.widget.tableWidget.setItem(row, 1, QTableWidgetItem(path))84
85def _add_clicked(self):86"""Callback: the Add License button was clicked"""87dlg = LicenseSelector(self.path_to_addon)88shortcode, path = dlg.exec()89if shortcode and path:90self._add_row(self.widget.tableWidget.rowCount(), shortcode, path)91self.metadata.addLicense(shortcode, path)92
93def _remove_clicked(self):94"""Callback: the Remove License button was clicked"""95items = self.widget.tableWidget.selectedIndexes()96if items:97# We only support single-selection, so can just pull the row # from98# the first entry99row = items[0].row()100shortcode = self.widget.tableWidget.item(row, 0).text()101path = self.widget.tableWidget.item(row, 1).text()102self.widget.tableWidget.removeRow(row)103self.metadata.removeLicense(shortcode, path)104
105def _edit(self, item):106"""Callback: a row in the tableWidget was double-clicked"""107row = item.row()108shortcode = self.widget.tableWidget.item(row, 0).text()109path = self.widget.tableWidget.item(row, 1).text()110
111dlg = LicenseSelector(self.path_to_addon)112new_shortcode, new_path = dlg.exec(shortcode, path)113
114if new_shortcode and new_path:115self.widget.tableWidget.removeRow(row)116self.metadata.removeLicense(new_shortcode, new_path)117
118self._add_row(row, new_shortcode, new_path)119self.metadata.addLicense(new_shortcode, new_path)120
121self.widget.tableWidget.selectRow(row)122
123def _selection_changed(self):124"""Callback: the current selection in the tableWidget changed"""125items = self.widget.tableWidget.selectedItems()126if items:127self.widget.removeButton.setDisabled(False)128else:129self.widget.removeButton.setDisabled(True)130