def paintEvent(self, event): painter = QPainter(self) minSize = min(self.rect().width(), self.rect().height()) myRect = QRect(self.rect().x(), self.rect().y(), minSize, minSize) painter.drawPixmap(myRect, self.backgroundPic, self.backgroundPic.rect()) pen = QPen() pen.setCapStyle(Qt.RoundCap) pen.setWidthF(4.0) pen.setColor(Qt.red) pen.setJoinStyle(Qt.RoundJoin) painter.setPen(pen) spacing = 40 contentRect = myRect.adjusted(spacing, spacing, -spacing, -spacing) valueInPercent = (self.m_value - self.m_minimum) / (self.m_maximum - self.m_minimum) math_pi = 3.14159265358979323846 degree = (self.m_startAngle + 90) - valueInPercent * 2.0 * self.m_startAngle degree = degree * math_pi / 180.0 radius = (contentRect.width() - spacing * 0.5) * 0.5 vec = QPoint(radius * math.cos(degree), radius * -math.sin(degree)) painter.drawLine(contentRect.center(), contentRect.center() + vec)
def createOnlineIcon(self,color): dim = QRect(0,0,32,32) img = QImage(dim.size(),QImage.Format_ARGB32) p = QPainter(img) p.setCompositionMode(QPainter.CompositionMode_Source) p.fillRect(dim,Qt.transparent) p.setCompositionMode(QPainter.CompositionMode_SourceOver) p.setRenderHints(QPainter.Antialiasing) p.setPen(Qt.black) p.setBrush(color) p.drawEllipse(dim.adjusted(1,1,-2,-2)) return img
def createOnlineIcon(self, color): dim = QRect(0, 0, 32, 32) img = QImage(dim.size(), QImage.Format_ARGB32) p = QPainter(img) p.setCompositionMode(QPainter.CompositionMode_Source) p.fillRect(dim, Qt.transparent) p.setCompositionMode(QPainter.CompositionMode_SourceOver) p.setRenderHints(QPainter.Antialiasing) p.setPen(Qt.black) p.setBrush(color) p.drawEllipse(dim.adjusted(1, 1, -2, -2)) return img
def paint(self, painter, option, index): """ Draws the widget Arguments: painter -- a QPainter option -- a QStyleOptionView index -- a QModelIndex """ # Retrieve the data model = index.model() record = model.data(index) text = record.toString() zoteroItem = zoteroCache[unicode(text)] l = zoteroItem.full_format().split(u"\n") if zoteroItem.fulltext == None: pixmap = self.noPdfPixmap else: pixmap = self.pdfPixmap # Choose the colors self.palette = self.qnotero.ui.listWidgetResults.palette() if option.state & QStyle.State_MouseOver: background = self.palette.Highlight foreground = self.palette.HighlightedText _note = zoteroItem.get_note() if _note != None: self.qnotero.showNoteHint() else: self.qnotero.hideNoteHint() elif option.state & QStyle.State_Selected: background = self.palette.Dark foreground = self.palette.WindowText else: background = self.palette.Base foreground = self.palette.WindowText # Draw the frame _rect = option.rect.adjusted(self._margin, self._margin, \ -2*self._margin, -self._margin) pen = painter.pen() pen.setColor(self.palette.color(background)) painter.setPen(pen) painter.setBrush(self.palette.brush(background)) painter.drawRoundedRect(_rect, self.roundness, self.roundness) font = painter.font pen = painter.pen() pen.setColor(self.palette.color(foreground)) painter.setPen(pen) # Draw icon _rect = QRect(option.rect) _rect.moveBottom(_rect.bottom() + 0.5 * self.dy) _rect.moveLeft(_rect.left() + 0.5 * self.dy) _rect.setHeight(self.pixmapSize) _rect.setWidth(self.pixmapSize) painter.drawPixmap(_rect, pixmap) # Draw the text _rect = option.rect.adjusted(self.pixmapSize+self.dy, 0.5*self.dy, \ -self.dy, 0) f = [self.tagFont, self.italicFont, self.regularFont, \ self.boldFont] l.reverse() while len(l) > 0: s = l.pop() if len(f) > 0: painter.setFont(f.pop()) painter.drawText(_rect, Qt.AlignLeft, s) _rect = _rect.adjusted(0, self.dy, 0, 0)
class VispaWidgetOwner(object): """ Interface for classes containing VispaWidgets Only makes sense if implementing class also inherits QWidget or class inheriting QWidget. """ def enableMultiSelect(self, multiSelect=True): self._multiSelectEnabledFlag = multiSelect def multiSelectEnabled(self): return hasattr( self, "_multiSelectEnabledFlag") and self._multiSelectEnabledFlag def selectedWidgets(self): """ Returns a list of all selected widgets. """ if hasattr(self, "_selectedWidgets"): return self._selectedWidgets return [ child for child in self.children() if hasattr(child, "isSelected") and child.isSelected() ] def widgetSelected(self, widget, multiSelect=False): """ Forward selection information to super class if it is a VispaWidgetOwner. """ logging.debug(self.__class__.__name__ + ": widgetSelected()") if isinstance(self, QObject): if not hasattr(self, "_selectedWidgets"): self._selectedWidgets = [] if not multiSelect or not self.multiSelectEnabled(): self.deselectAllWidgets(widget) self._selectedWidgets = [] if widget.parent() == self and not widget in self._selectedWidgets: self._selectedWidgets.append(widget) for widget in [ child for child in self._selectedWidgets if hasattr(child, "isSelected") and not child.isSelected() ]: self._selectedWidgets.remove(widget) if isinstance(self.parent(), VispaWidgetOwner): self.parent().widgetSelected(widget) def widgetDoubleClicked(self, widget): """ Forward selection information to super class if it is a VispaWidgetOwner. """ #logging.debug(self.__class__.__name__ +": widgetDoubleClicked()") if isinstance(self, QObject): if isinstance(self.parent(), VispaWidgetOwner): self.parent().widgetDoubleClicked(widget) def initWidgetMovement(self, widget): self._lastMovedWidgets = [] if self.multiSelectEnabled(): pos = widget.pos() for child in self.children(): if child != widget and hasattr( child, "isSelected") and child.isSelected(): child.setDragReferencePoint(pos - child.pos()) def widgetDragged(self, widget): """ Tell parent widget has moved. Only informs parent if it is a VispaWidgetOwner, too. """ if isinstance(self.parent(), VispaWidgetOwner): self.parent().widgetDragged(widget) if hasattr(self, "_lastMovedWidgets"): self._lastMovedWidgets.append(widget) if self.multiSelectEnabled(): for child in self.children(): if hasattr( child, "dragReferencePoint") and child != widget and hasattr( child, "isSelected") and child.isSelected(): if hasattr(child, "setPreviousDragPosition"): child.setPreviousDragPosition(child.pos()) child.move(widget.pos() - child.dragReferencePoint()) self._lastMovedWidgets.append(child) # apparently unused feature (2010-07-02), remove if really unnecessary # also see self._lastMovedWidget definition above def lastMovedWidgets(self): if hasattr(self, "_lastMovedWidgets"): return self._lastMovedWidgets return None def widgetAboutToDelete(self, widget): """ This function is called from the delete() function of VispaWidget. """ pass def keyPressEvent(self, event): """ Calls delete() method of selected child widgets if multi-select is activated. """ if self.multiSelectEnabled() and (event.key() == Qt.Key_Backspace or event.key() == Qt.Key_Delete): selection = self.selectedWidgets()[:] for widget in selection: widget.delete() def deselectAllWidgets(self, exception=None): """ Deselects all widgets except the widget given as exception. """ #logging.debug(self.__class__.__name__ +": deselectAllWidgets()") self._selectedWidgets = [] for child in self.children(): if child != exception: if hasattr(child, 'select'): child.select(False) else: self._selectedWidgets.append(child) if isinstance(child, VispaWidgetOwner): child.deselectAllWidgets(exception) self.update() def mousePressEvent(self, event): """ Calls deselectAllWidgets. """ multiSelectEnabled = self.multiSelectEnabled() if event.modifiers() != Qt.ControlModifier: self.deselectAllWidgets() if multiSelectEnabled: self._selectionRectStartPos = QPoint(event.pos()) self._selectionRect = None def mouseMoveEvent(self, event): if self.multiSelectEnabled() and self._selectionRectStartPos and ( event.pos() - self._selectionRectStartPos ).manhattanLength() >= QApplication.startDragDistance(): eventX = event.pos().x() eventY = event.pos().y() startX = self._selectionRectStartPos.x() startY = self._selectionRectStartPos.y() oldRect = self._selectionRect self._selectionRect = QRect(min(startX, eventX), min(startY, eventY), abs(eventX - startX), abs(eventY - startY)) if oldRect: self.update( self._selectionRect.united(oldRect).adjusted(-5, -5, 5, 5)) else: self.update(self._selectionRect) # dynamically update selection statur # currently bad performance (2010-07-07) # TODO: improve selection mechanism # for child in self.children(): # if hasattr(child, "select") and hasattr(child, "isSelected"): # child.select(self._selectionRect.contains(child.geometry()), True) # select, mulitSelect def mouseReleaseEvent(self, event): if hasattr(self, "_selectionRect" ) and self._selectionRect and self.multiSelectEnabled(): for child in self.children(): if hasattr(child, "select") and hasattr( child, "isSelected") and self._selectionRect.contains( child.geometry()) and not child.isSelected(): child.select(True, True) # select, mulitSelect self.update(self._selectionRect.adjusted(-5, -5, 5, 5)) self._selectionRect = None self._selectionRectStartPos = None
def paint(self, painter, option, index): """ Draws the widget Arguments: painter -- a QPainter option -- a QStyleOptionView index -- a QModelIndex """ # Retrieve the data model = index.model() record = model.data(index) text = record.toString() zoteroItem = zoteroCache[unicode(text)] l = zoteroItem.full_format().split("\n") if zoteroItem.fulltext == None: pixmap = self.noPdfPixmap else: pixmap = self.pdfPixmap # Choose the colors self.palette = self.qnotero.ui.listWidgetResults.palette() if option.state & QStyle.State_MouseOver: background = self.palette.Highlight foreground = self.palette.HighlightedText _note = zoteroItem.get_note() if _note != None: self.qnotero.showNoteHint() else: self.qnotero.hideNoteHint() elif option.state & QStyle.State_Selected: background = self.palette.Dark foreground = self.palette.WindowText else: background = self.palette.Base foreground = self.palette.WindowText # Draw the frame _rect = option.rect.adjusted(self._margin, self._margin, \ -2*self._margin, -self._margin) pen = painter.pen() pen.setColor(self.palette.color(background)) painter.setPen(pen) painter.setBrush(self.palette.brush(background)) painter.drawRoundedRect(_rect, self.roundness, self.roundness) font = painter.font pen = painter.pen() pen.setColor(self.palette.color(foreground)) painter.setPen(pen) # Draw icon _rect = QRect(option.rect) _rect.moveBottom(_rect.bottom() + 0.5*self.dy) _rect.moveLeft(_rect.left() + 0.5*self.dy) _rect.setHeight(self.pixmapSize) _rect.setWidth(self.pixmapSize) painter.drawPixmap(_rect, pixmap) # Draw the text _rect = option.rect.adjusted(self.pixmapSize+self.dy, 0.5*self.dy, \ -self.dy, 0) f = [self.tagFont, self.italicFont, self.regularFont, \ self.boldFont] l.reverse() while len(l) > 0: s = l.pop() if len(f) > 0: painter.setFont(f.pop()) painter.drawText(_rect, Qt.AlignLeft, s) _rect = _rect.adjusted(0, self.dy, 0, 0)
class VispaWidgetOwner(object): """ Interface for classes containing VispaWidgets Only makes sense if implementing class also inherits QWidget or class inheriting QWidget. """ def enableMultiSelect(self, multiSelect=True): self._multiSelectEnabledFlag = multiSelect def multiSelectEnabled(self): return hasattr(self, "_multiSelectEnabledFlag") and self._multiSelectEnabledFlag def selectedWidgets(self): """ Returns a list of all selected widgets. """ if hasattr(self, "_selectedWidgets"): return self._selectedWidgets return [child for child in self.children() if hasattr(child, "isSelected") and child.isSelected()] def widgetSelected(self, widget, multiSelect=False): """ Forward selection information to super class if it is a VispaWidgetOwner. """ logging.debug(self.__class__.__name__ +": widgetSelected()") if isinstance(self, QObject): if not hasattr(self, "_selectedWidgets"): self._selectedWidgets = [] if not multiSelect or not self.multiSelectEnabled(): self.deselectAllWidgets(widget) self._selectedWidgets = [] if widget.parent() == self and not widget in self._selectedWidgets: self._selectedWidgets.append(widget) for widget in [child for child in self._selectedWidgets if hasattr(child, "isSelected") and not child.isSelected()]: self._selectedWidgets.remove(widget) if isinstance(self.parent(), VispaWidgetOwner): self.parent().widgetSelected(widget) def widgetDoubleClicked(self, widget): """ Forward selection information to super class if it is a VispaWidgetOwner. """ #logging.debug(self.__class__.__name__ +": widgetDoubleClicked()") if isinstance(self, QObject): if isinstance(self.parent(), VispaWidgetOwner): self.parent().widgetDoubleClicked(widget) def initWidgetMovement(self, widget): self._lastMovedWidgets = [] if self.multiSelectEnabled(): pos = widget.pos() for child in self.children(): if child != widget and hasattr(child, "isSelected") and child.isSelected(): child.setDragReferencePoint(pos - child.pos()) def widgetDragged(self, widget): """ Tell parent widget has moved. Only informs parent if it is a VispaWidgetOwner, too. """ if isinstance(self.parent(), VispaWidgetOwner): self.parent().widgetDragged(widget) if hasattr(self, "_lastMovedWidgets"): self._lastMovedWidgets.append(widget) if self.multiSelectEnabled(): for child in self.children(): if hasattr(child, "dragReferencePoint") and child != widget and hasattr(child, "isSelected") and child.isSelected(): if hasattr(child, "setPreviousDragPosition"): child.setPreviousDragPosition(child.pos()) child.move(widget.pos() - child.dragReferencePoint()) self._lastMovedWidgets.append(child) # apparently unused feature (2010-07-02), remove if really unnecessary # also see self._lastMovedWidget definition above def lastMovedWidgets(self): if hasattr(self, "_lastMovedWidgets"): return self._lastMovedWidgets return None def widgetAboutToDelete(self, widget): """ This function is called from the delete() function of VispaWidget. """ pass def keyPressEvent(self, event): """ Calls delete() method of selected child widgets if multi-select is activated. """ if self.multiSelectEnabled() and ( event.key() == Qt.Key_Backspace or event.key() == Qt.Key_Delete ): selection = self.selectedWidgets()[:] for widget in selection: widget.delete() def deselectAllWidgets(self, exception=None): """ Deselects all widgets except the widget given as exception. """ #logging.debug(self.__class__.__name__ +": deselectAllWidgets()") self._selectedWidgets = [] for child in self.children(): if child != exception: if hasattr(child, 'select'): child.select(False) else: self._selectedWidgets.append(child) if isinstance(child, VispaWidgetOwner): child.deselectAllWidgets(exception) self.update() def mousePressEvent(self, event): """ Calls deselectAllWidgets. """ multiSelectEnabled = self.multiSelectEnabled() if event.modifiers() != Qt.ControlModifier: self.deselectAllWidgets() if multiSelectEnabled: self._selectionRectStartPos = QPoint(event.pos()) self._selectionRect = None def mouseMoveEvent(self, event): if self.multiSelectEnabled() and self._selectionRectStartPos and (event.pos() - self._selectionRectStartPos).manhattanLength() >= QApplication.startDragDistance(): eventX = event.pos().x() eventY = event.pos().y() startX = self._selectionRectStartPos.x() startY = self._selectionRectStartPos.y() oldRect = self._selectionRect self._selectionRect = QRect(min(startX, eventX), min(startY, eventY), abs(eventX - startX), abs(eventY - startY)) if oldRect: self.update(self._selectionRect.united(oldRect).adjusted(-5, -5, 5, 5)) else: self.update(self._selectionRect) # dynamically update selection statur # currently bad performance (2010-07-07) # TODO: improve selection mechanism # for child in self.children(): # if hasattr(child, "select") and hasattr(child, "isSelected"): # child.select(self._selectionRect.contains(child.geometry()), True) # select, mulitSelect def mouseReleaseEvent(self, event): if hasattr(self, "_selectionRect") and self._selectionRect and self.multiSelectEnabled(): for child in self.children(): if hasattr(child, "select") and hasattr(child, "isSelected") and self._selectionRect.contains(child.geometry()) and not child.isSelected(): child.select(True, True) # select, mulitSelect self.update(self._selectionRect.adjusted(-5, -5, 5, 5)) self._selectionRect = None self._selectionRectStartPos = None