def drag_icon(self, cover, multiple): cover = cover.scaledToHeight(120, Qt.SmoothTransformation) if multiple: base_width = cover.width() base_height = cover.height() base = QImage(base_width+21, base_height+21, QImage.Format_ARGB32_Premultiplied) base.fill(QColor(255, 255, 255, 0).rgba()) p = QPainter(base) rect = QRect(20, 0, base_width, base_height) p.fillRect(rect, QColor('white')) p.drawRect(rect) rect.moveLeft(10) rect.moveTop(10) p.fillRect(rect, QColor('white')) p.drawRect(rect) rect.moveLeft(0) rect.moveTop(20) p.fillRect(rect, QColor('white')) p.save() p.setCompositionMode(p.CompositionMode_SourceAtop) p.drawImage(rect.topLeft(), cover) p.restore() p.drawRect(rect) p.end() cover = base return QPixmap.fromImage(cover)
def __call__(self, image, duration=0.5): ''' Start the animation. You must have called :meth:`initialize` first. :param duration: Animation duration in seconds. ''' if self.running: return self.after_image = QPixmap.fromImage(image) if self.flip_forwards: self.animation.setStartValue(image.width()) self.animation.setEndValue(0) t = self.before_image self.before_image = self.after_image self.after_image = t self.animation.setEasingCurve(QEasingCurve(QEasingCurve.InExpo)) else: self.animation.setStartValue(0) self.animation.setEndValue(image.width()) self.animation.setEasingCurve(QEasingCurve(QEasingCurve.OutExpo)) self.animation.setDuration(duration * 1000) self.animation.start()
def __init__(self, parent=None): QStackedWidget.__init__(self, parent) self.welcome = w = QLabel('<p>'+_( 'Double click a file in the left panel to start editing' ' it.')) self.addWidget(w) w.setWordWrap(True) w.setAlignment(Qt.AlignTop | Qt.AlignHCenter) self.container = c = QWidget(self) self.addWidget(c) l = c.l = QVBoxLayout(c) c.setLayout(l) l.setContentsMargins(0, 0, 0, 0) self.editor_tabs = t = QTabWidget(c) l.addWidget(t) t.setDocumentMode(True) t.setTabsClosable(True) t.setMovable(True) pal = self.palette() if pal.color(pal.WindowText).lightness() > 128: i = QImage(I('modified.png')) i.invertPixels() self.modified_icon = QIcon(QPixmap.fromImage(i)) else: self.modified_icon = QIcon(I('modified.png')) self.editor_tabs.currentChanged.connect(self.current_editor_changed) self.editor_tabs.tabCloseRequested.connect(self._close_requested) self.search_panel = SearchPanel(self) l.addWidget(self.search_panel) self.restore_state() self.editor_tabs.tabBar().installEventFilter(self)
def refresh(self, row): if isinstance(row, QModelIndex): row = row.row() if row == self.current_row: return mi = self.view.model().get_book_display_info(row) if mi is None: # Indicates books was deleted from library, or row numbers have # changed return self.previous_button.setEnabled(False if row == 0 else True) self.next_button.setEnabled(False if row == self.view.model().rowCount(QModelIndex()) - 1 else True) self.current_row = row self.setWindowTitle(mi.title) self.cover_pixmap = QPixmap.fromImage(mi.cover_data[1]) self.resize_cover() html = render_html(mi, self.css, True, self, all_fields=True) self.details.setHtml(html) self.marked = mi.marked self.cover.setBackgroundBrush( self.marked_brush if mi.marked else self.normal_brush) self.update_cover_tooltip()
def drag_icon(self, cover, multiple): cover = cover.scaledToHeight(120, Qt.SmoothTransformation) if multiple: base_width = cover.width() base_height = cover.height() base = QImage(base_width + 21, base_height + 21, QImage.Format_ARGB32_Premultiplied) base.fill(QColor(255, 255, 255, 0).rgba()) p = QPainter(base) rect = QRect(20, 0, base_width, base_height) p.fillRect(rect, QColor('white')) p.drawRect(rect) rect.moveLeft(10) rect.moveTop(10) p.fillRect(rect, QColor('white')) p.drawRect(rect) rect.moveLeft(0) rect.moveTop(20) p.fillRect(rect, QColor('white')) p.save() p.setCompositionMode(p.CompositionMode_SourceAtop) p.drawImage(rect.topLeft(), cover) p.restore() p.drawRect(rect) p.end() cover = base return QPixmap.fromImage(cover)
def configureSponsorButton(self, btn): """ Load the image in the Sponsor button I{btn} that is displayed at the top of the Property Manager. @param btn: The sponsor button. @type btn: QToolButton """ qimg = QImage(self.imgfile) pixmap = QPixmap.fromImage(qimg) size = QSize(pixmap.width(), pixmap.height()) btn.setIconSize(size) btn.setIcon(QIcon(pixmap))
def __getitem__(self, key): ' Must be called in the GUI thread ' with self.lock: self.clear_staging() ans = self.items.pop(key, False) # pop() so that item is moved to the top if ans is not False: if type(ans) is QImage: # Convert to QPixmap, since rendering QPixmap is much # faster ans = QPixmap.fromImage(ans) self.items[key] = ans return ans
def __getitem__(self, key): ' Must be called in the GUI thread ' with self.lock: self.clear_staging() ans = self.items.pop( key, False) # pop() so that item is moved to the top if ans is not False: if type(ans) is QImage: # Convert to QPixmap, since rendering QPixmap is much # faster ans = QPixmap.fromImage(ans) self.items[key] = ans return ans
def create_icon(text, palette=None, sz=32, divider=2): if palette is None: palette = QApplication.palette() img = QImage(sz, sz, QImage.Format_ARGB32) img.fill(Qt.transparent) p = QPainter(img) p.setRenderHints(p.TextAntialiasing | p.Antialiasing) qDrawShadeRect(p, img.rect(), palette, fill=QColor('#ffffff'), lineWidth=1, midLineWidth=1) f = p.font() f.setFamily('Liberation Sans'), f.setPixelSize(sz // divider), f.setBold(True) p.setFont(f), p.setPen(Qt.black) p.drawText(img.rect().adjusted(2, 2, -2, -2), Qt.AlignCenter, text) p.end() return QIcon(QPixmap.fromImage(img))
def show_data(self, data): self.animation.stop() same_item = getattr(data, "id", True) == self.data.get("id", False) self.data = {"id": data.get("id", None)} if data.cover_data[1]: self.pixmap = QPixmap.fromImage(data.cover_data[1]) if self.pixmap.isNull() or self.pixmap.width() < 5 or self.pixmap.height() < 5: self.pixmap = self.default_pixmap else: self.pixmap = self.default_pixmap self.do_layout() self.update() if not same_item and not config["disable_animations"] and self.isVisible(): self.animation.start()
def initialize(self, image, forwards=True): ''' Initialize the flipper, causes the flipper to show itself displaying the full `image`. :param image: The image to display as background :param forwards: If True flipper will flip forwards, otherwise backwards ''' self.flip_forwards = forwards self.before_image = QPixmap.fromImage(image) self.after_image = None self.setGeometry(0, 0, image.width(), image.height()) self.setVisible(True)
def show_data(self, data): self.animation.stop() same_item = getattr(data, 'id', True) == self.data.get('id', False) self.data = {'id': data.get('id', None)} if data.cover_data[1]: self.pixmap = QPixmap.fromImage(data.cover_data[1]) if self.pixmap.isNull() or self.pixmap.width() < 5 or \ self.pixmap.height() < 5: self.pixmap = self.default_pixmap else: self.pixmap = self.default_pixmap self.do_layout() self.update() if not same_item and not config['disable_animations']: self.animation.start()
def nextImage(self): ''' take image from list and scale it to height of parent widget (which should be QMainWindow). Then generate a pixmap from the scaled image and display it on a QLabel''' if len(self.imgList) > 0: image = self.imgList[self.slideIterator%len(self.imgList)] else: return #print("IMAGE SIZE AFTER: " + str(image.size())) currImage = QPixmap.fromImage(image.scaledToHeight(self.parentWidget().height(), Qt.SmoothTransformation)) self.imageLabel.setPixmap(currImage) self.slideIterator += 1
def load_pixmap(self): canvas_size = self.rect().width(), self.rect().height() if self.last_canvas_size != canvas_size: if self.last_canvas_size is not None and self.selection_state.rect is not None: self.selection_state.reset() # TODO: Migrate the selection rect self.last_canvas_size = canvas_size self.current_scaled_pixmap = None if self.current_scaled_pixmap is None: pwidth, pheight = self.last_canvas_size i = self.current_image width, height = i.width(), i.height() scaled, width, height = fit_image(width, height, pwidth, pheight) if scaled: i = self.current_image.scaled(width, height, transformMode=Qt.SmoothTransformation) self.current_scaled_pixmap = QPixmap.fromImage(i)
def get_service_icon(self, id, name, url): icon, st = None, self.app.preferences.settings if not st.contains('icon/'+name): icon = get_favicon(url) if icon: icon = QIcon(QPixmap.fromImage(QImage.fromData(icon))) print name, "icon loaded?", not icon.isNull() if not icon.isNull(): st.setValue('icon/'+name, icon) else: print "error while loading", name, "icon" else: icon = st.value('icon/'+name, None) if icon: icon = QIcon(icon) if icon: self.app.preferences.ui.accountsTabWidget.setTabIcon(id, icon) return icon
def nextImage(self): ''' take image from list and scale it to height of parent widget (which should be QMainWindow). Then generate a pixmap from the scaled image and display it on a QLabel''' image = self.imgList[(self.currIndex+1)%len(self.imgList)] currImage = QPixmap.fromImage(image.scaledToHeight(self.parentWidget().height(), Qt.SmoothTransformation)) self.imageLabel.setPixmap(currImage) if self.validityChecker.load(self.fileList[self.currIndex%len(self.fileList)]): self.imgList[self.currIndex%len(self.imgList)] = self.validityChecker print("Preloading next image") self.currIndex += 1
def failed_img(self): if self._failed_img is None: i = QImage(200, 150, QImage.Format_ARGB32) i.fill(Qt.white) p = QPainter(i) r = i.rect().adjusted(10, 10, -10, -10) n = QPen(Qt.DashLine) n.setColor(Qt.black) p.setPen(n) p.drawRect(r) p.setPen(Qt.black) f = self.font() f.setPixelSize(20) p.setFont(f) p.drawText(r.adjusted(10, 0, -10, 0), Qt.AlignCenter | Qt.TextWordWrap, _('Image could not be rendered')) p.end() self._failed_img = QPixmap.fromImage(i) return self._failed_img
def get_service_icon(self, name, url): key = "service/" + name if key in self.icons: return self.icons[key] icon, st = None, self.app.accounts.settings if not st.contains('icon/'+name): icon = get_favicon(url) if icon: icon = QIcon(QPixmap.fromImage(QImage.fromData(icon))) #print name, "icon loaded?", not icon.isNull() if not icon.isNull(): st.setValue('icon/'+name, icon) else: print "[ERROR] while loading", name, "icon" else: icon = st.value('icon/'+name, None) if icon: icon = QIcon(icon) if icon: self.icons[key] = icon return icon
def refresh(self, row): if isinstance(row, QModelIndex): row = row.row() if row == self.current_row: return mi = self.view.model().get_book_display_info(row) if mi is None: # Indicates books was deleted from library, or row numbers have # changed return self.previous_button.setEnabled(False if row == 0 else True) self.next_button.setEnabled(False if row == self.view.model().rowCount(QModelIndex())-1 else True) self.current_row = row self.setWindowTitle(mi.title) self.title.setText('<b>'+mi.title) mi.title = _('Unknown') self.cover_pixmap = QPixmap.fromImage(mi.cover_data[1]) self.resize_cover() html = render_html(mi, self.css, True, self, all_fields=True) self.details.setHtml(html)
def refresh(self, row, mi=None): if isinstance(row, QModelIndex): row = row.row() if row == self.current_row and mi is None: return mi = self.view.model().get_book_display_info(row) if mi is None else mi if mi is None: # Indicates books was deleted from library, or row numbers have # changed return self.previous_button.setEnabled(False if row == 0 else True) self.next_button.setEnabled(False if row == self.view.model().rowCount(QModelIndex())-1 else True) self.current_row = row self.setWindowTitle(mi.title) self.cover_pixmap = QPixmap.fromImage(mi.cover_data[1]) self.resize_cover() html = render_html(mi, self.css, True, self, all_fields=True) self.details.setHtml(html) self.marked = mi.marked self.cover.setBackgroundBrush(self.marked_brush if mi.marked else self.normal_brush) self.update_cover_tooltip()
def configureSponsorButton(self, btn): qimg = QImage(self.imgfile) pixmap = QPixmap.fromImage(qimg) size = QSize(pixmap.width(), pixmap.height()) btn.setIconSize(size) btn.setIcon(QIcon(pixmap))