class AlgorithmProgressWidget(QWidget): """ Widget consisting of a progress bar and a button. """ def __init__(self, parent=None): super(AlgorithmProgressWidget, self).__init__(parent) self.progress_bar = None self.details_button = QPushButton('Details') self.details_button.clicked.connect(self.show_dialog) self.layout = QHBoxLayout() self.layout.addStretch() self.layout.addWidget(self.details_button) self.setLayout(self.layout) self.presenter = AlgorithmProgressPresenter(self) def show_progress_bar(self): if self.progress_bar is None: self.progress_bar = QProgressBar() self.progress_bar.setAlignment(Qt.AlignHCenter) self.layout.insertWidget(0, self.progress_bar) self.layout.removeItem(self.layout.takeAt(1)) def hide_progress_bar(self): if self.progress_bar is not None: self.layout.insertStretch(0) self.layout.removeWidget(self.progress_bar) self.progress_bar.close() self.progress_bar = None def show_dialog(self): dialog = AlgorithmMonitorDialog(self, self.presenter.model) dialog.show()
def createScaleBar(self, barlen): pb = QProgressBar(self) pb.setAlignment(Qt.AlignCenter) pb.setStyleSheet("QProgressBar {border: solid grey; border-radius: 0px; color: black; } \ QProgressBar::chunk {background-color: #00AAFF; border-radius:0px;}") pb.setValue(100) pb.setFixedSize(barlen, 6) pb.setTextVisible(False) return pb
def __on_reporter_created(reporter): initial_geometry = self.geometry() self.progress_bar_container_widget.setVisible(True) self.progress_bar_divider_line.setVisible(True) progress_bar = QProgressBar(self.progress_bar_container_widget) progress_bar.setAlignment(Qt.AlignCenter) reporter.progress_bar = progress_bar self.__reporter_map[reporter] = progress_bar self.progress_bar_container_layout.addWidget(progress_bar) progress_bar.show() current_geometry = self.geometry() current_geometry.moveCenter(initial_geometry.center()) self.setGeometry(current_geometry)
class MemoryView(QWidget): set_value = Signal(int, float, float) """ Initializes and updates the view of memory(progress) bar. """ def __init__(self, parent): super(MemoryView, self).__init__(parent) self.critical = CRITICAL_PERCENTAGE self.memory_bar = QProgressBar(self) self.memory_bar.setAlignment(Qt.AlignCenter) self.set_value.connect(self._set_value) def set_bar_color(self, current_value: int, new_value: int): """ Updates the memory(progress) bar style if needed :param current_value: Used system memory in percentage from previous update :param new_value: Latest used system memory in percentage """ if from_normal_to_critical(self.critical, current_value, new_value): self.memory_bar.setStyleSheet(CRITICAL_STYLE) elif from_critical_to_normal(self.critical, current_value, new_value): self.memory_bar.setStyleSheet(NORMAL_STYLE) else: pass def invoke_set_value(self, new_value: int, mem_used: float, mem_avail: float): self.set_value.emit(new_value, mem_used, mem_avail) @Slot(int, float, float) def _set_value(self, new_value, mem_used, mem_avail): """ Receives memory usage information passed by memory presenter and updates the displayed content as well as the style if needed :param new_value: Latest used system memory in percentage :param mem_used: Used system memory in Gigabytes(GB) :param mem_avail: Available system memory in GB """ # newValue is the latest mem_used_percent current_value = self.memory_bar.value() if current_value == new_value: return self.set_bar_color(current_value, new_value) self.memory_bar.setValue(new_value) display_str = f"{mem_used:3.1f}/{mem_avail:3.1f} GB ({new_value:d}%)" self.memory_bar.setFormat(display_str)
def update(self, data): """ Update the gui elements. :param data: Data in format of AlgorithmProgressModel.get_running_algorithm_data() """ self.tree.clear() for alg_data in data: name, id, properties = alg_data item = QTreeWidgetItem([name]) self.tree.addTopLevelItem(item) progress_bar = QProgressBar() progress_bar.setAlignment(Qt.AlignHCenter) self.presenter.add_progress_bar(id, progress_bar) cancel_button = CancelButton(self.presenter, id) self.tree.setItemWidget(item, 1, progress_bar) self.tree.setItemWidget(item, 2, cancel_button) for prop in properties: item.addChild(QTreeWidgetItem(prop))