def make_image(path, src, fail_ok=False): """relative to path (if needed), make a QGraphicsPixmapItem for the image named in src, returning None if not available, or an 'No Image' image if fail_ok == False""" if '//' not in src or src.startswith('file://'): testpath = src if '//' in testpath: testpath = testpath.split('//', 1)[-1] # # file on local file system testpath = g.os_path_finalize_join(path, testpath) if g.os_path_exists(testpath): return QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(testpath)) # # explicit file://, but no such file exists if src.startswith('file://'): return None if fail_ok else GetImage._no_image() # # no explict file://, so try other protocols testpath = src if '//' in src else 'http://%s' % (src) data = GetImage.get_url(testpath) if data: img = QtGui.QPixmap() if img.loadFromData(data): return QtWidgets.QGraphicsPixmapItem(img) return None if fail_ok else GetImage._no_image()
def image(self, pane, fn, center=None, height=None, width=None): '''Put an image in the indicated pane.''' m = self parent = m.pane_widget(pane) if not parent: g.trace('bad pane: %s' % (pane)) return None w = QtWidgets.QLabel('label', parent) fn = m.resolve_icon_fn(fn) if not fn: return None pixmap = QtGui.QPixmap(fn) if not pixmap: return g.trace('Not a pixmap: %s' % (fn)) if height: pixmap = pixmap.scaledToHeight(height) if width: pixmap = pixmap.scaledToWidth(width) w.setPixmap(pixmap) if center: g_w = w.geometry() g_p = parent.geometry() dx = (g_p.width() - g_w.width()) / 2 w.move(g_w.x() + dx, g_w.y() + 10) w.show() m.widgets.append(w) return w
def init_image(self, fn, magnification, position, size): '''Init the image whose file name fn is given.''' demo, widget = g.app.demo, self fn = demo.get_icon_fn(fn) if not fn: g.trace('can not resolve', fn) return pixmap = QtGui.QPixmap(fn) if not pixmap: g.trace('Not a pixmap:', fn) return if magnification: if size: h, w = size else: r = pixmap.size() h, w = r.height(), r.width() size = h*magnification, w*magnification if size: try: h, w = size h = demo.get_int(h) w = demo.get_int(w) if h is not None: pixmap = pixmap.scaledToHeight(h) if w is not None: pixmap = pixmap.scaledToWidth(w) except ValueError: g.trace('invalid size', repr(size)) if position: demo.set_position(widget, position) widget.setPixmap(pixmap)
def create_frame(self, filename, filenames, window): QLabel = QtWidgets.QLabel # Create the frame. frame = QtWidgets.QFrame(parent=window) # Create the vertical layout. layout = QtWidgets.QVBoxLayout() frame.setLayout(layout) # Set the font. font = QtGui.QFont() font.setBold(True) font.setPointSize(12) # Create the labels.. ctime = time.ctime(os.path.getctime(filename)) struct_time = time.strptime(ctime) creation_time = time.strftime('%Y %m %d', struct_time) file_label = QLabel(text=filename, parent=frame) file_label.setFont(font) layout.addWidget(file_label) size = os.path.getsize(filename) / 1000 info_label = QLabel(text=f"size: {size} KB date: {creation_time}") info_label.setFont(font) layout.addWidget(info_label) # Create the delete button, centered. button_layout = QtWidgets.QHBoxLayout() button_layout.addStretch() delete_button = QtWidgets.QPushButton(text='Delete', parent=frame) button_layout.addWidget(delete_button) button_layout.addStretch() layout.addLayout(button_layout) # Set the button action. def delete_action(arg): self.delete_file(filename) delete_button.clicked.connect(delete_action) # Create the picture area. picture = QtWidgets.QLabel('picture', parent=frame) layout.addWidget(picture) # Display the picture. pixmap = QtGui.QPixmap(filename) try: TransformationMode = QtCore.Qt if isQt5 else QtCore.Qt.TransformationMode image = pixmap.scaledToHeight( self.window_height, TransformationMode.SmoothTransformation) # pylint: disable=no-member picture.setPixmap(image) picture.adjustSize() return frame except Exception: g.trace('Bad image') g.es_exception() return None
def showColorNames(event=None): """Put up a dialog showing color names.""" c = event.get('c') template = ''' QComboBox { background-color: %s; selection-background-color: %s; selection-color: black; }''' ivar = 'leo_settings_color_picker' if getattr(c, ivar, None): g.es('The color picker already exists in the icon bar.') else: color_list: List[str] = [] box = QtWidgets.QComboBox() def onActivated(n, *args, **keys): color = color_list[n] sheet = template % (color, color) box.setStyleSheet(sheet) g.es("copied to clipboard:", color) QtWidgets.QApplication.clipboard().setText(color) box.activated.connect(onActivated) color_db = leoColor.leo_color_database for key in sorted(color_db): if not key.startswith('grey'): # Use gray, not grey. val = color_db.get(key) color = QtGui.QColor(val) color_list.append(val) pixmap = QtGui.QPixmap(40, 40) pixmap.fill(color) icon = QtGui.QIcon(pixmap) box.addItem(icon, key) c.frame.iconBar.addWidget(box) setattr(c, ivar, True) # Do this last, so errors don't prevent re-execution. g.es('created color picker in icon area')
def show_slide(self): # Reset the timer. self.timer.stop() self.timer.start(int(self.delay * 1000.0), self) if not self.files_list: self.quit() # Get the file name. file_name = self.files_list[self.slide_number] if self.verbose: print(file_name) # Change the title. self.setWindowTitle(file_name) # Display the picture. pixmap = QtGui.QPixmap(file_name) try: TransformationMode = QtCore.Qt if isQt5 else QtCore.Qt.TransformationMode image = pixmap.scaledToHeight( int(self.height() * self.scale), TransformationMode.SmoothTransformation, # pylint: disable=no-member ) self.picture.setPixmap(image) self.picture.adjustSize() except Exception: self.next_slide()
def make_pointer(self): """Return a pixmap for a pointer.""" path = g.computeLeoDir() path = g.os_path_join(path, 'Icons', 'recorder', 'pointer.png') return QtGui.QPixmap(path)
def _no_image(): """return QGraphicsPixmapItem with "No Image" image loaded""" testpath = g.os_path_abspath( g.os_path_join(g.app.loadDir, '../plugins/GraphCanvas/no_image.png')) return QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(testpath))