def _create_animation(self, path): if path in self._gif_list.values(): # We added it already. return movie = WObjectCache().open(QtGui.QMovie, path) self._gif_list[movie] = path movie.frameChanged.connect(self.drawAnimate) movie.start()
def _create_animation(self, path): movie = WObjectCache().open(WMovie, path) try: movie.frameChanged.connect(self.drawAnimate, QtCore.Qt.UniqueConnection) except TypeError: # connected already pass movie.start()
def __init__(self, parent=None): super(WAsyncLabel, self).__init__(parent) self._url = "" self._image = None self.fetcher = AsyncFetcher("".join((down_path, str(WeRuntimeInfo()["uid"])))) busyIconPixmap = WObjectCache().open(QtGui.QPixmap, ":/IMG/img/busy.gif") self.minimumImageHeight = busyIconPixmap.height() self.minimumImageWidth = busyIconPixmap.width() self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.contextMenu)
def __init__(self, parent=None): super(WAsyncLabel, self).__init__(parent) self._url = "" self._image = None self.fetcher = AsyncFetcher("".join( (down_path, str(WeRuntimeInfo()["uid"])))) busyIconPixmap = WObjectCache().open(QtGui.QPixmap, ":/IMG/img/busy.gif") self.minimumImageHeight = busyIconPixmap.height() self.minimumImageWidth = busyIconPixmap.width() self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.contextMenu)
def setBusy(self, busy): if busy: # Hack: #74. # What's wrong with the busyMovie()? To save the memory, # We use a single busyMovie() in the whole program. # If the image downloaded here, we'll stop the movie and the # busyIcon will disappear. But it may start from somewhere else. # The the busyIcon appear again unexpectedly. # The quick fix is disconnecting the signal/slot connection # when we stop the movie. self.animation = WObjectCache().open(QtGui.QMovie, ":/IMG/img/busy.gif") self.animation.start() self.animation.frameChanged.connect(self.drawBusyIcon) else: self.clearBusyIcon()
def setPixmap(self, url): super(WAsyncLabel, self).setMovie(WObjectCache().open(QtGui.QMovie, ":/IMG/img/busy.gif")) self.start() if not ("http" in url): self._setPixmap(url) return self._url = url self._fetch()
def _setupTopicTab(self, topic, switch=True): index = self._getSameTab("topic", topic) if index: if switch: self.tabWidget.setCurrentIndex(index) return view = TweetListWidget() timeline = TweetTopicModel(self.client.api("search/topics"), topic, view) tab = self._setupCommonTab(timeline, view, switch, protect=False) self._setTabIcon( tab, WObjectCache().open(QtGui.QPixmap, ":/IMG/img/topic.jpg"))
class WIconLabel(QtGui.QWidget): clicked = QtCore.pyqtSignal() def __init__(self, parent=None): super().__init__(parent) self._icon = None self._text = None def setIcon(self, icon): self._icon = WObjectCache().open(QtGui.QPixmap, icon) self.update() def setText(self, text): if self._text is None: self._text = QtGui.QStaticText() self._text.setText(text) self.update() def paintEvent(self, e): painter = QtGui.QPainter(self) if self._icon: painter.drawPixmap(0, 0, self._icon) if self._text: painter.drawStaticText(self._icon.width(), 0, self._text) def sizeHint(self): if self._text: return QtCore.QSize(self._icon.width() + self._text.size().width(), self._icon.height()) elif self._icon: return self._icon.size() else: return QtCore.QSize(0, 0) def mouseReleaseEvent(self, e): self.clicked.emit()
def setIcon(self, icon): self._icon = WObjectCache().open(QtGui.QPixmap, icon) self.update()
class WAsyncLabel(WImageLabel): clicked = QtCore.pyqtSignal(int) def __init__(self, parent=None): super(WAsyncLabel, self).__init__(parent) self._url = "" self._image = None self.fetcher = AsyncFetcher("".join((down_path, str(WeRuntimeInfo()["uid"])))) busyIconPixmap = WObjectCache().open(QtGui.QPixmap, ":/IMG/img/busy.gif") self.minimumImageHeight = busyIconPixmap.height() self.minimumImageWidth = busyIconPixmap.width() self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.contextMenu) def url(self): return self._url def setBusy(self, busy): if busy: # Hack: #74. # What's wrong with the busyMovie()? To save the memory, # We use a single busyMovie() in the whole program. # If the image downloaded here, we'll stop the movie and the # busyIcon will disappear. But it may start from somewhere else. # The the busyIcon appear again unexpectedly. # The quick fix is disconnecting the signal/slot connection # when we stop the movie. self.animation = WObjectCache().open(QtGui.QMovie, ":/IMG/img/busy.gif") self.animation.start() self.animation.frameChanged.connect(self.drawBusyIcon) else: self.clearBusyIcon() @QtCore.pyqtSlot() def drawBusyIcon(self): image = QtGui.QPixmap(self._image) icon = self.animation.currentPixmap() height = (image.height() - icon.height()) / 2 width = (image.width() - icon.width()) / 2 painter = QtGui.QPainter(image) painter.drawPixmap(width, height, icon) painter.end() super(WAsyncLabel, self).setPixmap(image) def clearBusyIcon(self): self.animation.stop() self.animation.frameChanged.disconnect(self.drawBusyIcon) super(WAsyncLabel, self).setPixmap(self._image) def _setPixmap(self, path): _image = QtGui.QPixmap(path) minimalHeight = self.minimumImageHeight minimalWidth = self.minimumImageWidth if _image.height() < minimalHeight or _image.width() < minimalWidth: if _image.height() > minimalHeight: height = _image.height() else: height = minimalHeight if _image.width() > minimalWidth: width = _image.width() else: width = minimalWidth image = QtGui.QPixmap(width, height) painter = QtGui.QPainter(image) path = QtGui.QPainterPath() path.addRect(0, 0, width, height) painter.fillPath(path, QtGui.QBrush(QtCore.Qt.gray)) painter.drawPixmap((width - _image.width()) / 2, (height - _image.height()) / 2, _image) painter.end() else: image = _image self._image = image super(WAsyncLabel, self).setPixmap(image) def setPixmap(self, url): super(WAsyncLabel, self).setMovie( WObjectCache().open(QtGui.QMovie, ":/IMG/img/busy.gif") ) self.start() if not ("http" in url): self._setPixmap(url) return self._url = url self._fetch() def _fetch(self): self.fetcher.addTask(self._url, self.setPixmap) def mouseReleaseEvent(self, e): if e.button() == QtCore.Qt.LeftButton and self._image: self.clicked.emit(QtCore.Qt.LeftButton) elif e.button() == QtCore.Qt.MiddleButton and self._image: self.clicked.emit(QtCore.Qt.MiddleButton) def contextMenu(self, pos): if not self._image: return saveAction = QtGui.QAction(self) saveAction.setText(self.tr("&Save")) saveAction.triggered.connect(self.save) menu = QtGui.QMenu() menu.addAction(saveAction) menu.exec(self.mapToGlobal(pos)) def save(self): file = QtGui.QFileDialog.getOpenFileName(self, self.tr("Choose a path")) self._image.save(file)
def _create_animation(self, path): movie = WObjectCache().open(WMovie, path) movie.frameChanged.connect(self.drawAnimate, QtCore.Qt.UniqueConnection) movie.start()
def setAvatar(f): self._setTabIcon(tab, WObjectCache().open(QtGui.QPixmap, f))
def setupUi(self): self.layout = QtGui.QVBoxLayout(self) self.setLayout(self.layout) self.busyMovie = WObjectCache().open(QtGui.QMovie, ":/IMG/img/busy.gif")
class WAsyncLabel(WImageLabel): clicked = QtCore.pyqtSignal(int) def __init__(self, parent=None): super(WAsyncLabel, self).__init__(parent) self._url = "" self._image = None self.fetcher = AsyncFetcher("".join( (down_path, str(WeRuntimeInfo()["uid"])))) busyIconPixmap = WObjectCache().open(QtGui.QPixmap, ":/IMG/img/busy.gif") self.minimumImageHeight = busyIconPixmap.height() self.minimumImageWidth = busyIconPixmap.width() self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.contextMenu) def url(self): return self._url def setBusy(self, busy): if busy: # Hack: #74. # What's wrong with the busyMovie()? To save the memory, # We use a single busyMovie() in the whole program. # If the image downloaded here, we'll stop the movie and the # busyIcon will disappear. But it may start from somewhere else. # The the busyIcon appear again unexpectedly. # The quick fix is disconnecting the signal/slot connection # when we stop the movie. self.animation = WObjectCache().open(QtGui.QMovie, ":/IMG/img/busy.gif") self.animation.start() self.animation.frameChanged.connect(self.drawBusyIcon) else: self.clearBusyIcon() @QtCore.pyqtSlot() def drawBusyIcon(self): image = QtGui.QPixmap(self._image) icon = self.animation.currentPixmap() height = (image.height() - icon.height()) / 2 width = (image.width() - icon.width()) / 2 painter = QtGui.QPainter(image) painter.drawPixmap(width, height, icon) painter.end() super(WAsyncLabel, self).setPixmap(image) def clearBusyIcon(self): self.animation.stop() self.animation.frameChanged.disconnect(self.drawBusyIcon) super(WAsyncLabel, self).setPixmap(self._image) def _setPixmap(self, path): _image = QtGui.QPixmap(path) minimalHeight = self.minimumImageHeight minimalWidth = self.minimumImageWidth if _image.height() < minimalHeight or _image.width() < minimalWidth: if _image.height() > minimalHeight: height = _image.height() else: height = minimalHeight if _image.width() > minimalWidth: width = _image.width() else: width = minimalWidth image = QtGui.QPixmap(width, height) painter = QtGui.QPainter(image) path = QtGui.QPainterPath() path.addRect(0, 0, width, height) painter.fillPath(path, QtGui.QBrush(QtCore.Qt.gray)) painter.drawPixmap((width - _image.width()) / 2, (height - _image.height()) / 2, _image) painter.end() else: image = _image self._image = image super(WAsyncLabel, self).setPixmap(image) def setPixmap(self, url): super(WAsyncLabel, self).setMovie(WObjectCache().open(QtGui.QMovie, ":/IMG/img/busy.gif")) self.start() if not ("http" in url): self._setPixmap(url) return self._url = url self._fetch() def _fetch(self): self.fetcher.addTask(self._url, self.setPixmap) def mouseReleaseEvent(self, e): if e.button() == QtCore.Qt.LeftButton and self._image: self.clicked.emit(QtCore.Qt.LeftButton) elif e.button() == QtCore.Qt.MiddleButton and self._image: self.clicked.emit(QtCore.Qt.MiddleButton) def contextMenu(self, pos): if not self._image: return saveAction = QtGui.QAction(self) saveAction.setText(self.tr("&Save")) saveAction.triggered.connect(self.save) menu = QtGui.QMenu() menu.addAction(saveAction) menu.exec(self.mapToGlobal(pos)) def save(self): file = QtGui.QFileDialog.getOpenFileName(self, self.tr("Choose a path")) self._image.save(file)