def data(self, index, role): target = self.targets[index.row()] if index.isValid() and (0 <= index.row() < len( self.targets)) and target: column = index.column() if role == Qt.DecorationRole: if column == 1: picturePath = os.path.join(GeneralUtilities.getTempDir(), target['targetPicture']) if picturePath and os.path.exists(picturePath): pixmap = QPixmap(picturePath) return QIcon( pixmap.scaled(30, 30, Qt.IgnoreAspectRatio, Qt.FastTransformation)) else: pixmap = QPixmap(':/creepy/user') pixmap.scaled(20, 20, Qt.IgnoreAspectRatio) return QIcon(pixmap) if role == Qt.DisplayRole: if column == 0: return QVariant(target['pluginName']) elif column == 1: return QVariant() elif column == 2: return QVariant(target['targetUsername']) elif column == 3: return QVariant(target['targetFullname']) elif column == 4: return QVariant(target['targetUserid']) else: return QVariant()
def data(self, index, role): target = self.targets[index.row()] if index.isValid() and (0 <= index.row() < len(self.targets)) and target: column = index.column() if role == Qt.DecorationRole: if column == 1: picturePath = os.path.join(os.getcwdu(), 'temp', target['targetPicture']) if picturePath and os.path.exists(picturePath): pixmap = QPixmap(picturePath) return QIcon(pixmap.scaled(30, 30, Qt.IgnoreAspectRatio, Qt.FastTransformation)) else: pixmap = QPixmap(os.path.join(os.getcwdu(), 'include', 'generic_user.png')) pixmap.scaled(20, 20, Qt.IgnoreAspectRatio) return QIcon(pixmap) if role == Qt.DisplayRole: if column == 0: return QVariant(target['pluginName']) elif column == 1: return QVariant() elif column == 2: return QVariant(target['targetUsername']) elif column == 3: return QVariant(target['targetFullname']) elif column == 4: return QVariant(target['targetUserid']) else: return QVariant()
def data(self, index, role): plugin = self.plugins[index.row()][0] if index.isValid(): if role == Qt.DisplayRole: return QVariant(plugin.name) if role == Qt.DecorationRole: picturePath = os.path.join(os.getcwdu(), 'plugins', plugin.plugin_object.name, 'logo.png') if picturePath and os.path.exists(picturePath): pixmap = QPixmap(picturePath) return QIcon( pixmap.scaled(30, 30, Qt.IgnoreAspectRatio, Qt.FastTransformation)) else: pixmap = QPixmap( os.path.join(os.getcwdu(), 'include', 'generic_plugin.png')) pixmap.scaled(30, 30, Qt.IgnoreAspectRatio) return QIcon(pixmap) if role == Qt.CheckStateRole: if plugin: return (Qt.Checked if plugin.name in self.checkedPlugins else Qt.Unchecked) else: return QVariant()
def _addToTab(self, submission, submissionURL, lst): """ Add a submission and its representative image to the lst under its tab. :type submission: praw.objects.Submission :type submissionURL: str :type lst: QListWidget """ imagePath = submission.representativeImage if imagePath is not None and imagePath.exists(): item = QListWidgetItem(submissionURL, lst) item.setTextColor(Qt.transparent) labelWidget = QLabel() labelWidget.setOpenExternalLinks(True) labelWidget.setTextFormat(Qt.RichText) size = QSize(128, 158) item.setSizeHint(size) size = QSize(128, 128) if imagePath.suffix == ".webm": imagePath = pathlib.Path("RedditDataExtractor", "images", "videoImage.png").resolve() pixmap = QPixmap(str(imagePath)) pixmap = pixmap.scaled(size, Qt.KeepAspectRatio) height = pixmap.height() width = pixmap.width() submissionTitle = submissionURL[submissionURL[0:-1].rfind("/") + 1:-1] labelWidget.setText( '<a href="' + submissionURL + '"><img src="' + str(imagePath) + '" height="' + str( height) + '" width="' + str(width) + '"><p>' + submissionTitle) lst.setItemWidget(item, labelWidget)
def _addToTab(self, submission, submissionURL, lst): """ Add a submission and its representative image to the lst under its tab. :type submission: praw.objects.Submission :type submissionURL: str :type lst: QListWidget """ imagePath = submission.representativeImage if imagePath is not None and imagePath.exists(): item = QListWidgetItem(submissionURL, lst) item.setTextColor(Qt.transparent) labelWidget = QLabel() labelWidget.setOpenExternalLinks(True) labelWidget.setTextFormat(Qt.RichText) size = QSize(128, 158) item.setSizeHint(size) size = QSize(128, 128) if imagePath.suffix == ".webm": imagePath = pathlib.Path("RedditDataExtractor", "images", "videoImage.png").resolve() pixmap = QPixmap(str(imagePath)) pixmap = pixmap.scaled(size, Qt.KeepAspectRatio) height = pixmap.height() width = pixmap.width() submissionTitle = submissionURL[submissionURL[0:-1].rfind("/") + 1:-1] labelWidget.setText('<a href="' + submissionURL + '"><img src="' + str(imagePath) + '" height="' + str(height) + '" width="' + str(width) + '"><p>' + submissionTitle) lst.setItemWidget(item, labelWidget)
def data(self, index, role): plugin = self.plugins[index.row()][0] if index.isValid(): if role == Qt.DisplayRole: return QVariant(plugin.name) if role == Qt.DecorationRole: for dir in GeneralUtilities.getPluginDirs(): picturePath = os.path.join(dir, plugin.plugin_object.name, 'logo.png') if picturePath and os.path.exists(picturePath): pixmap = QPixmap(picturePath) return QIcon(pixmap.scaled(30, 30, Qt.IgnoreAspectRatio, Qt.FastTransformation)) pixmap = QPixmap(os.path.join(GeneralUtilities.getIncludeDir(), 'generic_plugin.png')) pixmap.scaled(30, 30, Qt.IgnoreAspectRatio) return QIcon(pixmap) if role == Qt.CheckStateRole: if plugin: return (Qt.Checked if plugin.name in self.checkedPlugins else Qt.Unchecked) else: return QVariant()
def set_color(self): r, g, b = gprefs['cover_grid_color'] pal = QPalette() col = QColor(r, g, b) pal.setColor(pal.Base, col) tex = gprefs['cover_grid_texture'] if tex: from calibre.gui2.preferences.texture_chooser import texture_path path = texture_path(tex) if path: pm = QPixmap(path) if not pm.isNull(): val = pm.scaled(1, 1).toImage().pixel(0, 0) r, g, b = qRed(val), qGreen(val), qBlue(val) pal.setBrush(pal.Base, QBrush(pm)) dark = (r + g + b) / 3.0 < 128 pal.setColor(pal.Text, QColor(Qt.white if dark else Qt.black)) self.setPalette(pal) self.delegate.highlight_color = pal.color(pal.Text)
def set_color(self): r, g, b = gprefs['cover_grid_color'] pal = QPalette() col = QColor(r, g, b) pal.setColor(pal.Base, col) tex = gprefs['cover_grid_texture'] if tex: from calibre.gui2.preferences.texture_chooser import texture_path path = texture_path(tex) if path: pm = QPixmap(path) if not pm.isNull(): val = pm.scaled(1, 1).toImage().pixel(0, 0) r, g, b = qRed(val), qGreen(val), qBlue(val) pal.setBrush(pal.Base, QBrush(pm)) dark = (r + g + b)/3.0 < 128 pal.setColor(pal.Text, QColor(Qt.white if dark else Qt.black)) self.setPalette(pal) self.delegate.highlight_color = pal.color(pal.Text)
class CoverView(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.current_pixmap_size = QSize(0, 0) self.pixmap = QPixmap() self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) def set_pixmap(self, data): self.pixmap.loadFromData(data) self.current_pixmap_size = self.pixmap.size() self.update() def paintEvent(self, event): if self.pixmap.isNull(): return canvas_size = self.rect() width = self.current_pixmap_size.width() extrax = canvas_size.width() - width if extrax < 0: extrax = 0 x = int(extrax / 2.) height = self.current_pixmap_size.height() extray = canvas_size.height() - height if extray < 0: extray = 0 y = int(extray / 2.) target = QRect(x, y, min(canvas_size.width(), width), min(canvas_size.height(), height)) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap( target, self.pixmap.scaled(target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) p.end() def sizeHint(self): return QSize(300, 400)
class CoverView(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.current_pixmap_size = QSize(0, 0) self.pixmap = QPixmap() self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) def set_pixmap(self, data): self.pixmap.loadFromData(data) self.current_pixmap_size = self.pixmap.size() self.update() def paintEvent(self, event): if self.pixmap.isNull(): return canvas_size = self.rect() width = self.current_pixmap_size.width() extrax = canvas_size.width() - width if extrax < 0: extrax = 0 x = int(extrax/2.) height = self.current_pixmap_size.height() extray = canvas_size.height() - height if extray < 0: extray = 0 y = int(extray/2.) target = QRect(x, y, min(canvas_size.width(), width), min(canvas_size.height(), height)) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, self.pixmap.scaled(target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) p.end() def sizeHint(self): return QSize(300, 400)
def _fetch_marvin_cover(border_width=0): ''' Retrieve LargeCoverJpg from cache ''' #self._log_location('border_width: {0}'.format(border_width)) con = sqlite3.connect(self.marvin_db_path) with con: con.row_factory = sqlite3.Row # Fetch Hash from mainDb cover_cur = con.cursor() cover_cur.execute('''SELECT Hash FROM Books WHERE ID = '{0}' '''.format(self.book_id)) row = cover_cur.fetchone() book_hash = row[b'Hash'] large_covers_subpath = self.connected_device._cover_subpath(size="large") cover_path = '/'.join([large_covers_subpath, '%s.jpg' % book_hash]) stats = self.parent.ios.exists(cover_path) if stats: self._log("fetching large cover from cache") #self._log("cover size: {:,} bytes".format(int(stats['st_size']))) cover_bytes = self.parent.ios.read(cover_path, mode='rb') m_image = QImage() m_image.loadFromData(cover_bytes) if border_width: # Construct a QPixmap with oversized yellow background m_image = m_image.scaledToHeight( self.COVER_ICON_SIZE - border_width * 2, Qt.SmoothTransformation) self.m_pixmap = QPixmap( QSize(m_image.width() + border_width * 2, m_image.height() + border_width * 2)) m_painter = QPainter(self.m_pixmap) m_painter.setRenderHints(m_painter.Antialiasing) m_painter.fillRect(self.m_pixmap.rect(), self.MISMATCH_COLOR) m_painter.drawImage(border_width, border_width, m_image) else: m_image = m_image.scaledToHeight( self.COVER_ICON_SIZE, Qt.SmoothTransformation) self.m_pixmap = QPixmap( QSize(m_image.width(), m_image.height())) m_painter = QPainter(self.m_pixmap) m_painter.setRenderHints(m_painter.Antialiasing) m_painter.drawImage(0, 0, m_image) self.marvin_cover.setPixmap(self.m_pixmap) else: # No cover available, use generic self._log("No cached cover, using generic") pixmap = QPixmap() pixmap.load(I('book.png')) pixmap = pixmap.scaled(self.COVER_ICON_SIZE, self.COVER_ICON_SIZE, aspectRatioMode=Qt.KeepAspectRatio, transformMode=Qt.SmoothTransformation) self.marvin_cover.setPixmap(pixmap)
def _fetch_marvin_cover(with_border=False): ''' Retrieve LargeCoverJpg from cache ''' self._log_location() con = sqlite3.connect(self.marvin_db_path) with con: con.row_factory = sqlite3.Row # Fetch Hash from mainDb cover_cur = con.cursor() cover_cur.execute('''SELECT Hash FROM Books WHERE ID = '{0}' '''.format(self.book_id)) row = cover_cur.fetchone() book_hash = row[b'Hash'] large_covers_subpath = self.connected_device._cover_subpath( size="large") cover_path = '/'.join([large_covers_subpath, '%s.jpg' % book_hash]) stats = self.parent.ios.exists(cover_path) if stats: self._log("fetching large cover from cache") #self._log("cover size: {:,} bytes".format(int(stats['st_size']))) cover_bytes = self.parent.ios.read(cover_path, mode='rb') m_image = QImage() m_image.loadFromData(cover_bytes) if with_border: m_image = m_image.scaledToHeight( self.COVER_ICON_SIZE - self.BORDER_WIDTH * 2, Qt.SmoothTransformation) # Construct a QPixmap with yellow background self.m_pixmap = QPixmap( QSize(m_image.width() + self.BORDER_WIDTH * 2, m_image.height() + self.BORDER_WIDTH * 2)) m_painter = QPainter(self.m_pixmap) m_painter.setRenderHints(m_painter.Antialiasing) m_painter.fillRect(self.m_pixmap.rect(), QColor(0xFD, 0xFF, 0x99)) m_painter.drawImage(self.BORDER_WIDTH, self.BORDER_WIDTH, m_image) else: m_image = m_image.scaledToHeight(self.COVER_ICON_SIZE, Qt.SmoothTransformation) self.m_pixmap = QPixmap( QSize(m_image.width(), m_image.height())) m_painter = QPainter(self.m_pixmap) m_painter.setRenderHints(m_painter.Antialiasing) m_painter.drawImage(0, 0, m_image) self.marvin_cover.setPixmap(self.m_pixmap) else: # No cover available, use generic self._log("No cached cover, using generic") pixmap = QPixmap() pixmap.load(I('book.png')) pixmap = pixmap.scaled(self.COVER_ICON_SIZE, self.COVER_ICON_SIZE, aspectRatioMode=Qt.KeepAspectRatio, transformMode=Qt.SmoothTransformation) self.marvin_cover.setPixmap(pixmap)