-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathaddonmanager_python_deps_gui.py
More file actions
116 lines (96 loc) · 5.14 KB
/
Copy pathaddonmanager_python_deps_gui.py
File metadata and controls
116 lines (96 loc) · 5.14 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
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 FreeCAD Project Association
# SPDX-FileNotice: Part of the AddonManager.
################################################################################
# #
# This addon is free software: you can redistribute it and/or modify #
# it under the terms of the GNU Lesser General Public License as #
# published by the Free Software Foundation, either version 2.1 #
# of the License, or (at your option) any later version. #
# #
# This addon 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 Lesser General Public License for more details. #
# #
# You should have received a copy of the GNU Lesser General Public #
# License along with this addon. If not, see https://www.gnu.org/licenses #
# #
################################################################################
"""GUI for python dependency management."""
from pathlib import Path
import addonmanager_freecad_interface as fci
from addonmanager_python_deps import PythonPackageListModel
from PySideWrapper import QtCore, QtWidgets
translate = fci.translate
base_path = Path(__file__).parent
class PythonPackageManagerGui:
"""GUI for managing Python packages"""
_ui = base_path / "PythonDependencyUpdateDialog.ui"
_icons = base_path / "Resources" / "icons"
def __init__(self, addons):
self.dlg = fci.loadUi(str(self._ui))
self.dlg.setObjectName("AddonManager_PythonDependencyUpdateDialog")
self.model = PythonPackageListModel(addons)
self.dlg.tableView.setModel(self.model)
self.dlg.setMinimumHeight(400)
header = self.dlg.tableView.horizontalHeader()
header.setStretchLastSection(True)
resizeMode = QtWidgets.QHeaderView.ResizeMode.ResizeToContents
for col in range(4):
header.setSectionResizeMode(col, resizeMode)
self.dlg.buttonInstallPkgs.clicked.connect(self._install_button_clicked)
self.dlg.buttonUpdateAll.clicked.connect(self._update_button_clicked)
self.dlg.buttonCancel.clicked.connect(self._cancel_button_clicked)
self.dlg.rejected.connect(self._dialog_rejected)
self.model.modelReset.connect(self._model_was_reset)
self.model.update_complete.connect(self._update_complete)
self.model.progress_message.connect(self._show_progress_message)
def show(self):
self._working(True)
self.model.reset_package_list()
self.dlg.labelInstallationPath.setText(self.model.vendor_path)
self.dlg.exec()
def _working(self, working: bool) -> None:
"""Show or hide the progress display, and enable the buttons that make sense while pip is
running, or while it is not."""
self.dlg.buttonInstallPkgs.setEnabled(not working)
self.dlg.buttonUpdateAll.setEnabled(not working and self.model.updates_are_available())
self.dlg.buttonCancel.setEnabled(working)
if working:
self.dlg.updateInProgressLabel.show()
self.dlg.progressDetailsLabel.show()
else:
self.dlg.updateInProgressLabel.hide()
self.dlg.progressDetailsLabel.hide()
self.dlg.progressDetailsLabel.setText("")
def _show_progress_message(self, message: str) -> None:
"""Display the most recent line of pip output, shortened to fit the available width."""
label = self.dlg.progressDetailsLabel
elided = label.fontMetrics().elidedText(
message, QtCore.Qt.TextElideMode.ElideRight, label.width()
)
label.setText(elided)
def _cancel_button_clicked(self):
"""Ask the running pip call to stop, without waiting for it to do so."""
self.dlg.buttonCancel.setEnabled(False)
self._show_progress_message(translate("AddonsInstaller", "Stopping pip…"))
self.model.cancel_update()
def _dialog_rejected(self):
"""Stop any running pip call before this dialog and its model are destroyed."""
self.model.cancel_update(wait_for_completion=True)
def _install_button_clicked(self):
title = translate("AddonsInstaller", "Install")
prompt = translate("AddonsInstaller", "Packages:")
packages, ok = QtWidgets.QInputDialog.getText(self.dlg, title, prompt)
if packages and ok:
self._working(True)
self.model.install_packages(packages.split())
def _update_button_clicked(self):
self._working(True)
self.model.update_all_packages()
def _model_was_reset(self):
self._working(False)
def _update_complete(self):
self._working(True)
self.model.reset_package_list()