def __init__(self, parent=None): super().__init__(parent) logo = QSvgWidget() logo.load(":/icons/apps/22/tabulator.svg") name = QLabel( self.tr( "<strong style=\"font-size:large;\">{0}</strong> v{1}").format( QApplication.applicationName(), QApplication.applicationVersion())) description = QLabel( self. tr("An editor tool for documents with character-separated values")) labels = QVBoxLayout() labels.addWidget(name) labels.addWidget(description) # Main layout self._layout = QHBoxLayout(self) self._layout.addWidget(logo) self._layout.addLayout(labels) # Set logo size height = name.sizeHint().height() + labels.layout().spacing( ) + description.sizeHint().height() logo.setFixedSize(height, height)
def _draw_icon(self, id, svg_filename): svg = QSvgWidget(str(RESOURCES_PATH / svg_filename)) height = self.cellWidget(id, ICON_COLUMN).size().height() svg.setFixedSize(height, height) container = QWidget() layout = QHBoxLayout(container) layout.addWidget(svg) layout.setAlignment(Qt.AlignCenter) layout.setContentsMargins(0, 0, 0, 0) container.setLayout(layout) self.setCellWidget(id, ICON_COLUMN, container)
class Form(QMainWindow): def __init__(self, parent=None): super(Form, self).__init__(parent) self.svg_style = SvgStyle() self.temp_svg_file = QTemporaryFile() if not self.temp_svg_file.open(): # need to obtain temp file name raise RuntimeError('Cannot create temporary file for svg object') self.temp_svg_file.close() self.setWindowTitle(u'Круговая диаграмма') self.model = CustomTableModel() self.table_view = QTableView() self.table_view.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) self.table_view.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) self.widget_svg = QSvgWidget() self.widget_style = StyleWidget() self.widget_style.style_changed.connect(self.style_updated) # central widget layout layout = QHBoxLayout() # size = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) # size.setHorizontalStretch(1) # self.table_view.setSizePolicy(size) layout.addWidget(self.table_view) layout.addWidget(self.widget_svg) layout.addWidget(self.widget_style) self.widget_svg.setFixedSize(500, 500) widget_central = QWidget() widget_central.setLayout(layout) self.setCentralWidget(widget_central) # main menu self.menu = self.menuBar() self.menu_file = self.menu.addMenu("File") # load QAction action_load = QAction('Open', self) action_load.setShortcut(QKeySequence.Open) action_load.triggered.connect(self.load_data) self.menu_file.addAction(action_load) self.statusBar() # exit QAction action_exit = QAction('Exit', self) action_exit.setShortcut(QKeySequence.Quit) action_exit.triggered.connect(self.close) self.menu_file.addAction(action_exit) # window dimensions # geometry = qApp.desktop().availableGeometry(self) # self.setFixedSize(geometry.width() * 0.5, geometry.height() * 0.5) def load_data(self) -> None: filename, _ = QFileDialog.getOpenFileName(self, "Load data", dir="./tests", filter="Text files (*.txt);;Excel data (*csv)") if filename: country_data: CountryData = read_data(filename) self.model = CustomTableModel(country_data) self.table_view.setModel(self.model) self.statusBar().showMessage("Data loaded and plotted") self.draw_diagram() def load_svg(self, filename) -> None: self.widget_svg.load(filename) def draw_diagram(self) -> None: n_countries: int = self.model.rowCount() if n_countries > 0: style: SvgStyle = self.svg_style delta_angle: float = 2.0*math.pi/n_countries max_value: float = max(self.model.values) dwg = Drawing(self.temp_svg_file.fileName(), profile='tiny', viewBox='-250 -250 500 500') for idx, v in enumerate(self.model.values): x: float = style.line_length * v/max_value * math.sin(idx * delta_angle) y: float = -style.line_length * v/max_value * math.cos(idx * delta_angle) dwg.add(shapes.Line(start=(0, 0), end=(x, y), stroke=style.line_color, stroke_width=style.line_width)) radius: float = style.circle_rad if style.circle_rad_normalization: radius *= v/max_value dwg.add(shapes.Circle(center=(x, y), r=radius, stroke=style.circle_stroke_color, stroke_width=style.circle_stroke_width, fill=style.circle_fill_color)) dwg.save(pretty=True) self.load_svg(self.temp_svg_file.fileName()) @Slot() def style_updated(self, style: SvgStyle): self.svg_style = style self.draw_diagram()
class Header(CellBase): def __init__(self, track, subject): super().__init__(subject, track.index) self.track = track self.setAttribute(Qt.WA_StyledBackground) self.setStyleSheet(Theme.track.header.style) self.setFixedHeight(Theme.track.header.height) self._initLabels() self.updateStyle() self.add_responder(events.view.main.TrackEditName, self.editNameResponder) def _initLabels(self): self.backendName = QLabel(self.track.template.backend_name) self.backendName.setFont(Theme.track.header.backendNameFont) self.backendName.setStyleSheet(Theme.track.header.backendNameStyle) self.nameLabel.setFont(Theme.track.header.userNameFont) self.nameLabel.setStyleSheet(Theme.track.header.userNameStyle) self.nameLabel.setMaxLength(15) vlayout = QVBoxLayout() vlayout.setSpacing(0) vlayout.setContentsMargins(0, 0, 0, 0) vlayout.addWidget(self.backendName) vlayout.addWidget(self.nameLabel) vlayout.setAlignment(Qt.AlignLeft) layout = QHBoxLayout() layout.setSpacing(18) layout.setContentsMargins(18, 9, 18, 18) self.icon = QSvgWidget(self.track.template.icon_path()) self.icon.setFixedSize(36, 36) layout.addWidget(self.icon) layout.addLayout(vlayout) self.setLayout(layout) def onEditingNameFinished(self): self.notify(events.view.track.NameChanged(self.index, self.name())) return super().onEditingNameFinished() def mouseDoubleClickEvent(self, event): self.track.edit() return super().mouseDoubleClickEvent(event) def updateStyle(self): if self.selected: self.setSelectedStyle() else: self.setNormalStyle() def setSelectedStyle(self): self.setStyleSheet(Theme.track.header.styleSelected) def setNormalStyle(self): self.setStyleSheet(Theme.track.header.style)
class Item(Observation, QWidget): def __init__(self, template, subject): Observation.__init__(self, subject) QWidget.__init__(self) self.maxNameLen = 13 self.maxDescLen = 27 self.maxCommandLen = 13 self.template = template self.selected = False name = self.shortenString(template.backend_name, self.maxNameLen) description = self.shortenString(template.description, self.maxDescLen) runCommand = self.shortenString(template.run_command, self.maxCommandLen) self.setFixedSize(Theme.browser.item.size) layout = QVBoxLayout() layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) layout.setAlignment(Qt.AlignTop) self.setLayout(layout) self.__initHeader__(name, runCommand) self.__initDescription__(description) def __initHeader__(self, name, command): hlayout = QHBoxLayout() hlayout.setSpacing(0) hlayout.setContentsMargins(18, 18, 18, 18) self.iconLight = QSvgWidget(self.template.icon_path()) self.iconLight.setFixedSize(36, 36) self.iconDark = QSvgWidget(self.template.icon_path(False)) self.iconDark.setFixedSize(36, 36) self.iconDark.setHidden(True) hlayout.addWidget(self.iconLight) hlayout.addWidget(self.iconDark) vlayout = QVBoxLayout() vlayout.setSpacing(0) vlayout.setContentsMargins(0, 0, 0, 0) self.name = QLabel(name, wordWrap=True) self.name.setFont(Theme.browser.item.headerFont) self.name.setStyleSheet(Theme.browser.item.headerStyle) vlayout.addWidget(self.name) self.command = QLabel(command, wordWrap=True) self.command.setFont(Theme.browser.item.commandFont) self.command.setStyleSheet(Theme.browser.item.commandStyle) vlayout.addWidget(self.command) hlayout.addLayout(vlayout) self.layout().addLayout(hlayout) def __initDescription__(self, description): self.description = QLabel(description, wordWrap=True) self.description.setFont(Theme.browser.item.descriptionFont) self.description.setStyleSheet(Theme.browser.item.descriptionStyle) self.layout().addWidget(self.description) def shortenString(self, value, length): lines = value.splitlines() if len(lines) < 1: return value firstLine = lines[0] if len(firstLine) <= length: return firstLine return firstLine[:length] + "..." def setSelected(self, selected): self.selected = selected if self.selected: self.name.setStyleSheet(Theme.browser.item.headerStyleSelected) self.command.setStyleSheet(Theme.browser.item.commandStyleSelected) self.description.setStyleSheet( Theme.browser.item.descriptionStyleSelected) self.iconLight.setHidden(True) self.iconDark.setHidden(False) return self.name.setStyleSheet(Theme.browser.item.headerStyle) self.command.setStyleSheet(Theme.browser.item.commandStyle) self.description.setStyleSheet(Theme.browser.item.descriptionStyle) self.iconLight.setHidden(False) self.iconDark.setHidden(True) def deserialize(self, template): self.name.setText( self.shortenString(template.backend_name, self.maxNameLen)) self.command.setText( self.shortenString(template.run_command, self.maxCommandLen)) self.description.setText( self.shortenString(template.description, self.maxDescLen)) def sizeHint(self): return Theme.browser.item.size