def get_icon(name, default=None, resample=False): """Return image inside a QIcon object. default: default image name or icon resample: if True, manually resample icon pixmaps for usual sizes (16, 24, 32, 48, 96, 128, 256). This is recommended for QMainWindow icons created from SVG images on non-Windows platforms due to a Qt bug (see Issue 1314). """ icon_path = get_image_path(name, default=None) if icon_path is not None: icon = QIcon(icon_path) elif isinstance(default, QIcon): icon = default elif default is None: try: icon = get_std_icon(name[:-4]) except AttributeError: icon = QIcon(get_image_path(name, default)) else: icon = QIcon(get_image_path(name, default)) if resample: icon0 = QIcon() for size in (16, 24, 32, 48, 96, 128, 256, 512): icon0.addPixmap(icon.pixmap(size, size)) return icon0 else: return icon
def set_icon(self, icon: QIcon): ''' Sets the icon to show in title bar Parameters ---------- icon : QIcon ''' layout = self.layout() if not self.d.icon_label and icon.isNull(): return if not self.d.icon_label: self.d.icon_label = QLabel() self.d.icon_label.setAlignment(Qt.AlignVCenter) self.d.icon_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred) self.d.icon_label.setToolTip(self.d.title_label.toolTip()) layout.insertWidget(0, self.d.icon_label, Qt.AlignVCenter) layout.insertSpacing( 1, round(1.5 * layout.contentsMargins().left() / 2.0)) elif icon.isNull(): # Remove icon label and spacer item layout.removeWidget(self.d.icon_label) layout.removeItem(layout.itemAt(0)) self.d.icon_label.deleteLater() self.d.icon_label = None self.d.icon = icon if self.d.icon_label: self.d.icon_label.setPixmap( icon.pixmap(self.windowHandle(), QSize(16, 16))) self.d.icon_label.setVisible(True)
def convert_svg_to_png(svg_path, png_path, height, width): """Convert svg files to png files using Qt.""" size = QSize(height, width) icon = QIcon(svg_path) pixmap = icon.pixmap(size) img = pixmap.toImage() img.save(png_path)
def get_icon(name, default=None, resample=True): """Return image inside a QIcon object default: default image name or icon resample: if True, manually resample icon pixmaps for usual sizes (16, 24, 32, 48, 96, 128, 256). This is recommended for QMainWindow icons created from SVG images on non-Windows platforms due to a Qt bug (see http://code.google.com/p/spyderlib/issues/detail?id=1314).""" icon = icon_cache.get((name, default, resample)) if icon: return icon if default is None: icon = QIcon(get_image_path(name)) elif isinstance(default, QIcon): icon_path = get_image_path(name) icon = default if icon_path is None else QIcon(icon_path) else: icon = QIcon(get_image_path(name)) if resample: icon0 = QIcon() for size in (16, 24, 32, 48, 96, 128, 256, 512): icon0.addPixmap(icon.pixmap(size, size)) icon_cache[(name, default, resample)] = icon0 ret = icon0 else: ret = icon icon_cache[(name, default, resample)] = ret return ret
def get_icon(name, default=None, resample=False): """Return image inside a QIcon object. default: default image name or icon resample: if True, manually resample icon pixmaps for usual sizes (16, 24, 32, 48, 96, 128, 256). This is recommended for QMainWindow icons created from SVG images on non-Windows platforms due to a Qt bug (see Issue 1314). """ icon_path = get_image_path(name, default=None) if icon_path is not None: icon = QIcon(icon_path) elif isinstance(default, QIcon): icon = default elif default is None: try: icon = get_std_icon(name[:-4]) except AttributeError: icon = QIcon(get_image_path(name, default)) else: icon = QIcon(get_image_path(name, default)) if resample: icon0 = QIcon() for size in (16, 24, 32, 48, 96, 128, 256, 512): icon0.addPixmap(icon.pixmap(size, size)) return icon0 else: return icon
def load_icon(self, project_path, project=None, as_pixmap=False): """ Load project icon for project located at `project_path`. """ logger.debug(str((project_path, project['name'], as_pixmap))) from PIL.ImageQt import ImageQt from PIL import Image if project is None: project = self.load_project(project_path) icon_path = os.sep.join([project_path, project.icon]) if os.path.isfile(icon_path): try: icon = qta.icon('fa.gear') image = Image.open(icon_path) image = ImageQt(image) qt_image = QImage(image) pixmap = QPixmap.fromImage(qt_image) icon = QIcon(pixmap) except Exception: icon = qta.icon('fa.gear') else: icon = qta.icon('fa.gear') if as_pixmap: try: icon = icon.pixmap(icon.availableSizes()[0]) except Exception: icon = QPixmap(*IMAGE_ICON_SIZE) return icon
def convert_svg_to_png(svg_path, png_path, height, width): """Convert svg files to png files using Qt.""" size = QSize(height, width) icon = QIcon(svg_path) pixmap = icon.pixmap(size) img = pixmap.toImage() img.save(png_path)
def update(self): cross_icon = QIcon(":/icons/cross.png") cross = cross_icon.pixmap(QSize(32, 32)) check_icon = QIcon(":/icons/check.png") check = check_icon.pixmap(QSize(32, 32)) selected_engine = self.selected_engine.currentText() if selected_engine == "None": qmark_icon = QIcon(":/icons/qmark.png") qmark = qmark_icon.pixmap(QSize(32, 32)) self.cna_label.setPixmap(qmark) else: if self.cna_ok: self.cna_label.setPixmap(check) else: self.cna_label.setPixmap(cross) if self.oeng is not None: # disable button if octave is already working self.oc_label.setPixmap(check) else: self.oc_label.setPixmap(cross) if self.meng is not None: self.ml_label.setPixmap(check) else: self.ml_label.setPixmap(cross) self.selected_engine.currentTextChanged.disconnect(self.update) self.selected_engine.clear() self.selected_engine.addItem("None") if self.meng is not None: self.selected_engine.addItem("Matlab") if self.oeng is not None: self.selected_engine.addItem("Octave") # if selected_engine is "None": self.selected_engine.setCurrentIndex(0) if selected_engine == "Matlab": self.selected_engine.setCurrentIndex(1) if selected_engine == "Octave": if self.selected_engine.count() == 2: self.selected_engine.setCurrentIndex(1) elif self.selected_engine.count() == 3: self.selected_engine.setCurrentIndex(2) self.selected_engine.currentTextChanged.connect(self.update)
def convert_svg_to_png(svg_path, png_path, height, width): """ Convert svg files to png files using Qt. """ size = QSize(height, width) icon = QIcon(svg_path) pixmap = icon.pixmap(size) img = pixmap.toImage() dirname = os.path.dirname(png_path) if not os.path.exists(dirname): os.makedirs(dirname) img.save(os.path.abspath(png_path))
def get_icon(self, name, resample=False): """Return image inside a QIcon object. default: default image name or icon resample: if True, manually resample icon pixmaps for usual sizes (16, 24, 32, 48, 96, 128, 256). This is recommended for QMainWindow icons created from SVG images on non-Windows platforms due to a Qt bug. See spyder-ide/spyder#1314. """ icon_path = get_image_path(name) icon = QIcon(icon_path) if resample: icon0 = QIcon() for size in (16, 24, 32, 48, 96, 128, 256, 512): icon0.addPixmap(icon.pixmap(size, size)) return icon0 else: return icon
def get_icon(self, name, resample=False): """Return image inside a QIcon object. Parameters ---------- name: str Image name or icon resample: bool If True, manually resample icon pixmaps for usual sizes (16, 24, 32, 48, 96, 128, 256). This is recommended for QMainWindow icons created from SVG images on non-Windows platforms due to a Qt bug. See spyder-ide/spyder#1314. """ icon_path = get_image_path(name) if resample: # This only applies to the Spyder 2 icons icon = QIcon(icon_path) icon0 = QIcon() for size in (16, 24, 32, 48, 96, 128, 256, 512): icon0.addPixmap(icon.pixmap(size, size)) return icon0 else: icon = QIcon() # Normal state normal_state = QPixmap(icon_path) icon.addPixmap(normal_state, QIcon.Normal) # This is the color GammaRay reports for icons in disabled # buttons, both for the dark and light themes disabled_color = QColor(150, 150, 150) # Paint icon with the previous color to get the disabled state. # Taken from https://stackoverflow.com/a/65618075/438386 disabled_state = QPixmap(icon_path) qp = QPainter(disabled_state) qp.setCompositionMode(QPainter.CompositionMode_SourceIn) qp.fillRect(disabled_state.rect(), disabled_color) qp.end() icon.addPixmap(disabled_state, QIcon.Disabled) return icon
def setWindowIcon(self, icon: QIcon): self.appLogoLabel.setPixmap(icon.pixmap())
def __init__(self, appdata: CnaData): QDialog.__init__(self) self.setWindowTitle("Configure CNApy") cross_icon = QIcon(":/icons/cross.png") cross = cross_icon.pixmap(QSize(32, 32)) self.appdata = appdata self.oeng = appdata.octave_engine self.meng = appdata.matlab_engine self.layout = QVBoxLayout() self.cna_ok = False descr = QLabel("\ Some functionalities in CNApy need a working CNA installation.\n \ To use CNA you need either Matlab >= R2019 or Octave >= 5 .\n \ Below you can choose a Matlab directory or the Octave executable.\n \ Only if one of the engines is green your CNA directory can be validated." ) self.layout.addWidget(descr) ml = QHBoxLayout() label = QLabel("Matlab") label.setFixedWidth(100) ml.addWidget(label) self.ml_label = QLabel() self.ml_label.setPixmap(cross) self.ml_label.setFixedWidth(100) ml.addWidget(self.ml_label) self.choose_ml_path_btn = QPushButton("Choose Matlab folder") self.choose_ml_path_btn.setFixedWidth(300) ml.addWidget(self.choose_ml_path_btn) label2 = QLabel("") label2.setFixedWidth(30) ml.addWidget(label2) self.matlab_path = QLabel() self.matlab_path.setMinimumWidth(200) self.matlab_path.setText(self.appdata.matlab_path) ml.addWidget(self.matlab_path) self.layout.addItem(ml) oc = QHBoxLayout() label = QLabel("Octave") label.setFixedWidth(100) oc.addWidget(label) self.oc_label = QLabel() self.oc_label.setPixmap(cross) self.oc_label.setFixedWidth(100) oc.addWidget(self.oc_label) self.choose_oc_exe_btn = QPushButton("Choose Octave octave-cli(.exe)") self.choose_oc_exe_btn.setFixedWidth(300) oc.addWidget(self.choose_oc_exe_btn) label2 = QLabel("") label2.setFixedWidth(30) oc.addWidget(label2) self.oc_exe = QLabel() self.oc_exe.setText(self.appdata.octave_executable) self.oc_exe.setMinimumWidth(200) oc.addWidget(self.oc_exe) self.layout.addItem(oc) h9 = QHBoxLayout() label = QLabel("Selected engine:") h9.addWidget(label) label2 = QLabel("") label2.setFixedWidth(30) h9.addWidget(label2) self.selected_engine = QComboBox() self.selected_engine.addItem("None") h9.addWidget(self.selected_engine) label2 = QLabel("") label2.setMinimumWidth(200) h9.addWidget(label2) self.layout.addItem(h9) cna_l = QHBoxLayout() label = QLabel("CNA") label.setFixedWidth(100) cna_l.addWidget(label) self.cna_label = QLabel() self.cna_label.setPixmap(cross) self.cna_label.setFixedWidth(100) cna_l.addWidget(self.cna_label) self.choose_cna_path_btn = QPushButton("Choose CNA directory") self.choose_cna_path_btn.setFixedWidth(300) cna_l.addWidget(self.choose_cna_path_btn) label2 = QLabel("") label2.setFixedWidth(30) cna_l.addWidget(label2) self.cna_path = QLabel() self.cna_path.setMinimumWidth(200) self.cna_path.setText(self.appdata.cna_path) cna_l.addWidget(self.cna_path) self.layout.addItem(cna_l) h2 = QHBoxLayout() label = QLabel("Default color for values in a scenario:") h2.addWidget(label) self.scen_color_btn = QPushButton() self.scen_color_btn.setFixedWidth(100) palette = self.scen_color_btn.palette() palette.setColor(QPalette.Button, self.appdata.scen_color) self.scen_color_btn.setPalette(palette) h2.addWidget(self.scen_color_btn) self.layout.addItem(h2) h3 = QHBoxLayout() label = QLabel( "Default color for computed values not part of the scenario:") h3.addWidget(label) self.comp_color_btn = QPushButton() self.comp_color_btn.setFixedWidth(100) palette = self.comp_color_btn.palette() palette.setColor(QPalette.Button, self.appdata.comp_color) self.comp_color_btn.setPalette(palette) h3.addWidget(self.comp_color_btn) self.layout.addItem(h3) h4 = QHBoxLayout() label = QLabel("Special Color used for non equal flux bounds:") h4.addWidget(label) self.spec1_color_btn = QPushButton() self.spec1_color_btn.setFixedWidth(100) palette = self.spec1_color_btn.palette() palette.setColor(QPalette.Button, self.appdata.special_color_1) self.spec1_color_btn.setPalette(palette) h4.addWidget(self.spec1_color_btn) self.layout.addItem(h4) h5 = QHBoxLayout() label = QLabel( "Special Color 2 used for non equal flux bounds that exclude 0:") h5.addWidget(label) self.spec2_color_btn = QPushButton() self.spec2_color_btn.setFixedWidth(100) palette = self.spec2_color_btn.palette() palette.setColor(QPalette.Button, self.appdata.special_color_2) self.spec2_color_btn.setPalette(palette) h5.addWidget(self.spec2_color_btn) self.layout.addItem(h5) h6 = QHBoxLayout() label = QLabel("Color used for empty reaction boxes:") h6.addWidget(label) self.default_color_btn = QPushButton() self.default_color_btn.setFixedWidth(100) palette = self.default_color_btn.palette() palette.setColor(QPalette.Button, self.appdata.default_color) self.default_color_btn.setPalette(palette) h6.addWidget(self.default_color_btn) self.layout.addItem(h6) h = QHBoxLayout() label = QLabel("Work directory:") h.addWidget(label) self.work_directory = QPushButton() self.work_directory.setText(self.appdata.work_directory) h.addWidget(self.work_directory) self.layout.addItem(h) h7 = QHBoxLayout() label = QLabel("Shown number of digits after the decimal point:") h7.addWidget(label) self.rounding = QLineEdit() self.rounding.setFixedWidth(100) self.rounding.setText(str(self.appdata.rounding)) validator = QIntValidator(0, 20, self) self.rounding.setValidator(validator) h7.addWidget(self.rounding) self.layout.addItem(h7) h8 = QHBoxLayout() label = QLabel( "Absolute tolerance used to compare float values in the UI:") h8.addWidget(label) self.abs_tol = QLineEdit() self.abs_tol.setFixedWidth(100) self.abs_tol.setText(str(self.appdata.abs_tol)) validator = QDoubleValidator(self) validator.setTop(1) self.abs_tol.setValidator(validator) h8.addWidget(self.abs_tol) self.layout.addItem(h8) l2 = QHBoxLayout() self.button = QPushButton("Apply Changes") self.cancel = QPushButton("Close") l2.addWidget(self.button) l2.addWidget(self.cancel) self.layout.addItem(l2) self.setLayout(self.layout) # Connecting the signal self.choose_ml_path_btn.clicked.connect(self.choose_ml_path) self.choose_oc_exe_btn.clicked.connect(self.choose_oc_exe) self.choose_cna_path_btn.clicked.connect(self.choose_cna_path) self.work_directory.clicked.connect(self.choose_work_directory) self.scen_color_btn.clicked.connect(self.choose_scen_color) self.comp_color_btn.clicked.connect(self.choose_comp_color) self.spec1_color_btn.clicked.connect(self.choose_spec1_color) self.spec2_color_btn.clicked.connect(self.choose_spec2_color) self.default_color_btn.clicked.connect(self.choose_default_color) self.selected_engine.currentTextChanged.connect(self.update) self.cancel.clicked.connect(self.reject) self.button.clicked.connect(self.apply) self.check_all() if self.meng is not None: self.selected_engine.insertItem(1, "Matlab") if self.appdata.selected_engine == "matlab": self.selected_engine.setCurrentIndex(1) if self.oeng is not None: self.selected_engine.insertItem(1, "Octave") if self.appdata.selected_engine == "octave": self.selected_engine.setCurrentIndex(1) self.update()
def draw_normal_line(painter, connection, style): if connection.requires_port: return # colors normal_color_out = style.get_normal_color() normal_color_in = normal_color_out selected_color = style.selected_color gradient_color = False if style.use_data_defined_colors: data_type_out = connection.data_type(PortType.output) data_type_in = connection.data_type(PortType.input) gradient_color = data_type_out.id != data_type_in.id normal_color_out = style.get_normal_color(data_type_out.id) normal_color_in = style.get_normal_color(data_type_in.id) selected_color = normal_color_out.darker(200) # geometry geom = connection.geometry line_width = style.line_width # draw normal line p = QPen() p.setWidth(line_width) graphics_object = connection.graphics_object selected = graphics_object.isSelected() cubic = cubic_path(geom) if gradient_color: painter.setBrush(Qt.NoBrush) c = normal_color_out if selected: c = c.darker(200) p.setColor(c) painter.setPen(p) segments = 60 for i in range(segments): ratio_prev = float(i) / segments ratio = float(i + 1) / segments if i == segments / 2: c = normal_color_in if selected: c = c.darker(200) p.setColor(c) painter.setPen(p) painter.drawLine(cubic.pointAtPercent(ratio_prev), cubic.pointAtPercent(ratio)) icon = QIcon(":convert.png") pixmap = icon.pixmap(QSize(22, 22)) painter.drawPixmap( cubic.pointAtPercent(0.50) - QPoint(pixmap.width() / 2, pixmap.height() / 2), pixmap) else: p.setColor(normal_color_out) if selected: p.setColor(selected_color) painter.setPen(p) painter.setBrush(Qt.NoBrush) painter.drawPath(cubic)