FreeCAD

Форк
0
/
addonmanager_devmode_licenses_table.py 
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

26
import os
27

28
from PySide.QtWidgets import QTableWidgetItem
29
from PySide.QtGui import QIcon
30

31
import FreeCAD
32
import FreeCADGui
33

34
from addonmanager_devmode_license_selector import LicenseSelector
35

36
translate = FreeCAD.Qt.translate
37

38
# pylint: disable=too-few-public-methods
39

40

41
class LicensesTable:
42
    """A QTableWidget and associated buttons for managing the list of authors and maintainers."""
43

44
    def __init__(self):
45
        self.widget = FreeCADGui.PySideUic.loadUi(
46
            os.path.join(os.path.dirname(__file__), "developer_mode_licenses_table.ui")
47
        )
48

49
        self.widget.addButton.setIcon(QIcon.fromTheme("add", QIcon(":/icons/list-add.svg")))
50
        self.widget.removeButton.setIcon(
51
            QIcon.fromTheme("remove", QIcon(":/icons/list-remove.svg"))
52
        )
53

54
        self.widget.addButton.clicked.connect(self._add_clicked)
55
        self.widget.removeButton.clicked.connect(self._remove_clicked)
56
        self.widget.tableWidget.itemSelectionChanged.connect(self._selection_changed)
57
        self.widget.tableWidget.itemDoubleClicked.connect(self._edit)
58
        self.metadata = None
59
        self.path_to_addon = ""
60

61
    def show(self, metadata, path_to_addon):
62
        """Set up the widget based on incoming metadata"""
63
        self.metadata = metadata
64
        self.path_to_addon = path_to_addon
65
        self._populate_from_metadata()
66
        self.widget.removeButton.setDisabled(True)
67
        self.widget.show()
68

69
    def _populate_from_metadata(self):
70
        """Use the passed metadata object to populate the maintainers and authors"""
71
        self.widget.tableWidget.setRowCount(0)
72
        row = 0
73
        for lic in self.metadata.License:
74
            shortcode = lic["name"]
75
            path = lic["file"]
76
            self._add_row(row, shortcode, path)
77
            row += 1
78

79
    def _add_row(self, row, shortcode, path):
80
        """Add this license to the tableWidget at row given"""
81
        self.widget.tableWidget.insertRow(row)
82
        self.widget.tableWidget.setItem(row, 0, QTableWidgetItem(shortcode))
83
        self.widget.tableWidget.setItem(row, 1, QTableWidgetItem(path))
84

85
    def _add_clicked(self):
86
        """Callback: the Add License button was clicked"""
87
        dlg = LicenseSelector(self.path_to_addon)
88
        shortcode, path = dlg.exec()
89
        if shortcode and path:
90
            self._add_row(self.widget.tableWidget.rowCount(), shortcode, path)
91
            self.metadata.addLicense(shortcode, path)
92

93
    def _remove_clicked(self):
94
        """Callback: the Remove License button was clicked"""
95
        items = self.widget.tableWidget.selectedIndexes()
96
        if items:
97
            # We only support single-selection, so can just pull the row # from
98
            # the first entry
99
            row = items[0].row()
100
            shortcode = self.widget.tableWidget.item(row, 0).text()
101
            path = self.widget.tableWidget.item(row, 1).text()
102
            self.widget.tableWidget.removeRow(row)
103
            self.metadata.removeLicense(shortcode, path)
104

105
    def _edit(self, item):
106
        """Callback: a row in the tableWidget was double-clicked"""
107
        row = item.row()
108
        shortcode = self.widget.tableWidget.item(row, 0).text()
109
        path = self.widget.tableWidget.item(row, 1).text()
110

111
        dlg = LicenseSelector(self.path_to_addon)
112
        new_shortcode, new_path = dlg.exec(shortcode, path)
113

114
        if new_shortcode and new_path:
115
            self.widget.tableWidget.removeRow(row)
116
            self.metadata.removeLicense(new_shortcode, new_path)
117

118
            self._add_row(row, new_shortcode, new_path)
119
            self.metadata.addLicense(new_shortcode, new_path)
120

121
            self.widget.tableWidget.selectRow(row)
122

123
    def _selection_changed(self):
124
        """Callback: the current selection in the tableWidget changed"""
125
        items = self.widget.tableWidget.selectedItems()
126
        if items:
127
            self.widget.removeButton.setDisabled(False)
128
        else:
129
            self.widget.removeButton.setDisabled(True)
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.