-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdependencies.py
More file actions
202 lines (169 loc) · 6.3 KB
/
Copy pathdependencies.py
File metadata and controls
202 lines (169 loc) · 6.3 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
"""Dependencies widget of the psyplot package
This module defines the :class:`DependenciesWidget` that shows the versions of
of psyplot, psyplot_gui, psyplot plugins and their requirements"""
# SPDX-FileCopyrightText: 2016-2024 University of Lausanne
# SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht
# SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum hereon GmbH
#
# SPDX-License-Identifier: LGPL-3.0-only
from psyplot.docstring import docstrings
from psyplot_gui.compat.qtcompat import (
QAbstractItemView,
QAction,
QApplication,
QDialog,
QDialogButtonBox,
QHBoxLayout,
QLabel,
QMenu,
QMessageBox,
QPushButton,
Qt,
QtCore,
QTreeWidget,
QTreeWidgetItem,
QVBoxLayout,
)
class DependenciesTree(QTreeWidget):
"""A tree widget to display dependencies
This widget uses a dictionary as created through the
:func:`psyplot.get_versions` function to display the requirements and
versions."""
@docstrings.get_sections(base="DependenciesTree")
def __init__(self, versions, *args, **kwargs):
"""
Parameters
----------
versions: dict
The dictionary that contains the version information
See Also
--------
psyplot.get_versions
"""
super(DependenciesTree, self).__init__(*args, **kwargs)
self.resizeColumnToContents(0)
self.setColumnCount(2)
self.setHeaderLabels(["Package", "installed version"])
self.add_dependencies(versions)
self.expandAll()
self.resizeColumnToContents(0)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.open_menu)
@docstrings.dedent
def add_dependencies(self, versions, parent=None):
"""
Add the version informations to the tree
This method creates an QTreeWidgetItem for each package in `versions`
and adds it to this tree.
Parameters
----------
%(DependenciesTree.parameters)s
parent: QTreeWidgetItem
The parent of the newly created items for the packages in
`versions`. If None, the newly created items are inserted as
top level items into the tree
"""
for pkg, pkg_d in versions.items():
new_item = QTreeWidgetItem(0)
new_item.setText(0, pkg)
if isinstance(pkg_d, dict):
new_item.setText(1, pkg_d["version"])
else:
new_item.setText(1, pkg_d)
if parent is None:
self.addTopLevelItem(new_item)
else:
parent.addChild(new_item)
if "requirements" in pkg_d:
self.add_dependencies(pkg_d["requirements"], new_item)
def open_menu(self, position):
"""Open a menu to expand and collapse all items in the tree
Parameters
----------
position: QPosition
The position where to open the menu"""
menu = QMenu()
expand_all_action = QAction("Expand all", self)
expand_all_action.triggered.connect(self.expandAll)
menu.addAction(expand_all_action)
collapse_all_action = QAction("Collapse all", self)
collapse_all_action.triggered.connect(self.collapseAll)
menu.addAction(collapse_all_action)
menu.exec_(self.viewport().mapToGlobal(position))
class DependenciesDialog(QDialog):
"""A dialog for displaying the dependencies"""
#: description label
label = None
#: the QVBoxLayout containing all the widgets
vbox = None
#: The :class:`DependenciesTree` that contains the package infos
tree = None
#: The QPushButton used for copying selected packages to the clipboard
bt_copy = None
#: A simple info label for info messages
info_label = None
#: A QTimer that clears the :attr:`info_label` after some time
timer = None
@docstrings.dedent
def __init__(self, versions, *args, **kwargs):
"""
Parameters
----------
%(DependenciesTree.parameters)s
"""
super(DependenciesDialog, self).__init__(*args, **kwargs)
self.setWindowTitle("Dependencies")
self.versions = versions
self.vbox = layout = QVBoxLayout()
self.label = QLabel(
"""
psyplot and the plugins depend on several python libraries. The
tree widget below lists the versions of the plugins and the
requirements. You can select the items in the tree and copy them to
clipboard.""",
parent=self,
)
layout.addWidget(self.label)
self.tree = DependenciesTree(versions, parent=self)
self.tree.setSelectionMode(QAbstractItemView.MultiSelection)
layout.addWidget(self.tree)
# copy button
self.bt_copy = QPushButton("Copy selection to clipboard")
self.bt_copy.setToolTip(
"Copy the selected packages in the above table to the clipboard."
)
self.bt_copy.clicked.connect(lambda: self.copy_selected())
self.bbox = QDialogButtonBox(QDialogButtonBox.Ok)
self.bbox.accepted.connect(self.accept)
hbox = QHBoxLayout()
hbox.addWidget(self.bt_copy)
hbox.addStretch(1)
hbox.addWidget(self.bbox)
layout.addLayout(hbox)
#: A label for simple status update
self.info_label = QLabel("", self)
layout.addWidget(self.info_label)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.clear_label)
self.setLayout(layout)
def copy_selected(self, label=None):
"""Copy the selected versions and items to the clipboard"""
d = {}
items = self.tree.selectedItems()
if not items:
QMessageBox.warning(
self,
"No packages selected!",
"Please select packages in the tree!",
)
return
for item in items:
d[item.text(0)] = item.text(1)
if label is None:
label = QApplication.clipboard()
label.setText("\n".join("%s: %s" % t for t in d.items()))
self.info_label.setText("Packages copied to clipboard.")
self.timer.start(3000)
def clear_label(self):
"""Clear the info label"""
self.info_label.setText("")