def itemsRect(self): """ Return a QRect of the specific boundary of the items in the layout """ count = self._itemLayout.count() if not count: return QtCore.QRect(0,0,0,0) first = self._itemLayout.itemAt(0).widget() if count == 1: rect = first.geometry() return QtCore.QRect(rect.topLeft(), rect.bottomRight()) last = self._itemLayout.itemAt(count-1).widget() return QtCore.QRect(first.geometry().topLeft(), last.geometry().bottomRight())
def paintEvent(self, event): total_width = self.width() total_height = self.height() painter = QtGui.QPainter() painter.begin(self) painter.setRenderHints(painter.HighQualityAntialiasing | painter.SmoothPixmapTransform | painter.Antialiasing) if self.__hasErrors: painter.setBrush(constants.RED) else: painter.setBrush(constants.COLOR_JOB_STATE[self.__state]) painter.setPen(painter.brush().color().darker()) rect = QtCore.QRect(0, 0, total_width, total_height) painter.drawRoundedRect(rect, 5, 5) painter.setPen(QtCore.Qt.black) painter.drawText(rect, QtCore.Qt.AlignCenter, constants.JOB_STATES[self.__state]) painter.end()
def dropEvent(self, event): layout = self._itemLayout parent = layout.parentWidget() or self sourceItem = event.source() sourceIndex = layout.indexOf(sourceItem) dropPos = event.pos() dropItem = parent.childAt(dropPos) insertIndex = None # The item was dropped directly on another item # so figure out if it was a bit above, or a bit below if dropItem: dropIndex = layout.indexOf(dropItem) middle = dropItem.geometry().center().y() if dropPos.y() < middle: # print "Drop source", sourceIndex, "before", dropIndex insertIndex = dropIndex else: # print "Drop source", sourceIndex, "after", dropIndex insertIndex = dropIndex else: itemsRect = self.itemsRect() # The item was dropped somewhere inside the items layout if itemsRect.contains(dropPos): droppedY = dropPos.y() # Figure out which two items it was dropped between for child1, child2 in util.pairwise(self): r1 = child1.geometry() r2 = child2.geometry() r1_bottom = r1.bottomLeft() r2_top = r2.topRight() testRect = QtCore.QRect(r1_bottom, r2_top) if testRect.contains(dropPos): if child1 is sourceItem or child2 is sourceItem: # print "Dropped near original. Ignored." event.ignore() return # print "Dropped between", child1, child2 insertIndex = layout.indexOf(child2) break # This item was dropped above the first item # or below the last one else: if dropPos.y() < itemsRect.y(): # print "Dropped above layout" insertIndex = 0 else: # print "Dropped below layout" insertIndex = -1 if insertIndex is not None: if insertIndex > sourceIndex: insertIndex -= 1 # print "Final insert index is", insertIndex, "(from original %d)" % sourceIndex layout.insertWidget(insertIndex, sourceItem) event.acceptProposedAction() else: event.ignore()