def mouseReleaseEvent(self, *args, **kwargs): # reset everything back to default state # reset gradient self.__hideRGBACrosshair(False) self.__hideLinearCrosshair(True) self.scene().gradient_type = attrs.RGBA self.scene().drawGradient() # reset picking attrs self._picking = False self._black_select = False # reset cursor self.unsetCursor() QCursor.setPos(self._orig_pos) # disable labels color_gradient_widget = getWidgetAncestor(self, ColorGradientDelegate) color_arg_widgets_dict = color_gradient_widget.header_widget.getWidgetDict() for color_arg in color_arg_widgets_dict: color_arg_widgets_dict[color_arg].setSelected(False) # update rgba crosshair pos self.scene().updateRGBACrosshair() return QGraphicsView.mouseReleaseEvent(self, *args, **kwargs)
def warn(widget, text=None): """Display warning for widget in preferences.""" label = widget.label_information if text: label = PreferencesDialog.update_icon(label, WARNING_ICON) label.setToolTip(str(text)) w = widget.label_information.width() / 2 h = widget.label_information.height() / 2 position = widget.label_information.mapToGlobal(QPoint(w, h)) QCursor.setPos(position) else: label.setPixmap(QPixmap()) label.setToolTip('')
def __placeCursorAtActiveItem(self): """ Gets the current active items position in world space and places the cursor in the center of that. """ try: cursor_display_pos = getGlobalPos(self.activeObject()) except AttributeError: view = self.activeObject().scene().views()[0] view_pos = view.mapFromScene(self.activeObject().scenePos()) cursor_display_pos = view.viewport().mapToGlobal(view_pos) QCursor.setPos(cursor_display_pos)
def warn(widget, text=None): """Display warning for widget in preferences.""" label = widget.label_information if text: pixmap = QPixmap(WARNING_ICON) label.setPixmap( pixmap.scaled(16, 16, Qt.KeepAspectRatio, Qt.SmoothTransformation)) label.setToolTip(str(text)) w = widget.label_information.width() / 2 h = widget.label_information.height() / 2 position = widget.label_information.mapToGlobal(QPoint(w, h)) QCursor.setPos(position) else: label.setPixmap(QPixmap()) label.setToolTip('')
def leaveEvent(self, event, *args, **kwargs): """ When the cursor leaves the invisible display area, this will return it back to the original point that it was set at. It will then update the positions to accomdate this so that one drag seems seemless. """ if not self._drag_STICKY: return current_pos = QCursor.pos() offset = (current_pos - self._cursor_pos) self._calc_pos = self._calc_pos - offset # reset cursor QCursor.setPos(self._cursor_pos) # update value self.__setValue() return QFrame.leaveEvent(self, event, *args, **kwargs)
def _fix_cursor(self, from_index, to_index): """Fix mouse cursor position to adjust for different tab sizes.""" # The direction is +1 (moving to the right) or -1 (moving to the left) direction = abs(to_index - from_index) / (to_index - from_index) tab_width = self.dock_tabbar.tabRect(to_index).width() tab_x_min = self.dock_tabbar.tabRect(to_index).x() tab_x_max = tab_x_min + tab_width previous_width = self.dock_tabbar.tabRect(to_index - direction).width() delta = previous_width - tab_width if delta > 0: delta = delta * direction else: delta = 0 cursor = QCursor() pos = self.dock_tabbar.mapFromGlobal(cursor.pos()) x, y = pos.x(), pos.y() if x < tab_x_min or x > tab_x_max: new_pos = self.dock_tabbar.mapToGlobal(QPoint(x + delta, y)) cursor.setPos(new_pos)
def _fix_cursor(self, from_index, to_index): """Fix mouse cursor position to adjust for different tab sizes.""" # The direction is +1 (moving to the right) or -1 (moving to the left) direction = abs(to_index - from_index)/(to_index - from_index) tab_width = self.dock_tabbar.tabRect(to_index).width() tab_x_min = self.dock_tabbar.tabRect(to_index).x() tab_x_max = tab_x_min + tab_width previous_width = self.dock_tabbar.tabRect(to_index - direction).width() delta = previous_width - tab_width if delta > 0: delta = delta * direction else: delta = 0 cursor = QCursor() pos = self.dock_tabbar.mapFromGlobal(cursor.pos()) x, y = pos.x(), pos.y() if x < tab_x_min or x > tab_x_max: new_pos = self.dock_tabbar.mapToGlobal(QPoint(x + delta, y)) cursor.setPos(new_pos)
def __activateStickyDrag(self, obj): """ This should be run every time the user clicks. Args: obj (QWidget / QItem --> DragWidget): Object to install all of the extra attrs on """ obj._cursor_pos = QCursor.pos() top_left = getTopLeftPos(obj) QCursor.setPos(top_left + QPoint(10, 10)) obj.setFocus() # set up drag time attrs obj.updateOrigValue() obj._calc_pos = QCursor.pos() obj._drag_STICKY = not obj._drag_STICKY obj._num_ticks = 0 obj._pixels_per_tick = self.pixelsPerTick() obj._value_per_tick = self.valuePerTick() # toggle cursor display if obj._drag_STICKY: obj.setCursor(Qt.BlankCursor)
def move_from(self, cur_pos_data: Tuple[float, float]) -> None: """ Calculate a new position based on the given starting position and self.newpixel transform and move the mouse cursor there. :param cur_pos_data: Current cursor position in data coordinates """ new_pos_data = self.clip(self.new_pixel(*cur_pos_data)) xdisp, ydisp = self.data_to_display.transform_point(new_pos_data) canvas = self.canvas dpi_ratio = canvas._dpi_ratio to_int = self.to_int xp = to_int(xdisp / dpi_ratio) yp = to_int((canvas.figure.bbox.height - ydisp) / dpi_ratio) # in the special case of just vertical/horizontal moves # use the original cursor position for the orthogonal coordinate # to avoid rounding errors and mouse jitter new_global_pos = canvas.mapToGlobal(QPoint(xp, yp)) cur_global_pos = QCursor.pos() if new_pos_data[0] == cur_pos_data[0]: new_global_pos.setX(cur_global_pos.x()) if new_pos_data[1] == cur_pos_data[1]: new_global_pos.setY(cur_global_pos.y()) QCursor.setPos(new_global_pos)
def _pickColor(self, pos, constrain_to_picker=True): """ picks the the current color displayed on screen at the current location of the cursor Args: pos (QPoint): Global pos constrain_to_pick (bool): determines whether or not the crosshair should be forced to remain inside of the picker while the user is selecting. """ # preflight check (cursor) # forces the cursor to remain inside of the color picker # TODO Removing bouncing """ display is bouncing when reaching ends as it snaps back to the last position in space. It also cannot get the final 0/1 values = """ if constrain_to_picker is True: cursor_sector_dict = checkMousePos(pos, self) if cursor_sector_dict["INSIDE"] is False: top_left = self.mapToGlobal(self.pos()) top = top_left.y() left = top_left.x() right = left + self.geometry().width() bottom = top + self.geometry().height() if cursor_sector_dict["NORTH"] is True: pos.setY(top + 1) if cursor_sector_dict["EAST"] is True: pos.setX(right - 1) if cursor_sector_dict["SOUTH"] is True: pos.setY(bottom - 1) if cursor_sector_dict["WEST"] is True: pos.setX(left + 1) QCursor.setPos(pos) # self.scene().rgba_crosshair_item.hide() # get pixel data desktop = QApplication.desktop().winId() screen = QApplication.primaryScreen() pixmap = screen.grabWindow( desktop, pos.x(), pos.y(), 1, 1 ) img = pixmap.toImage() color = QColor(img.pixel(0, 0)) # if pure black recurse # if color.valueF() == 0: # TODO """ Grabbing color of picker because fails """ if color.valueF() < .0001: self._black_select = True pos = QPoint(pos.x() + 1, pos.y() + 1) return self._pickColor(pos) return color
def mousePressEvent(self, event, *args, **kwargs): """ Attrs: _picking (bool): tells the widget that the user is now attempting to pick a value _in_gradient_widget (bool) tells the widget that the cursor is in the gradient widget """ modifiers = QApplication.keyboardModifiers() button = event.button() if button in [Qt.LeftButton, Qt.RightButton, Qt.MiddleButton]: # move rgba # HSV self._picking = True self._black_select = False self._in_gradient_widget = True self._orig_pos = QCursor.pos() # setup default crosshair self.__hideRGBACrosshair(True) self.__hideLinearCrosshair(False) # RGB main_widget = getWidgetAncestor(self, ColorGradientDelegate) color = main_widget.color() pos = QPoint(0, 0) # TODO Check hack after upgrading to 5.15 # katana is not registering the alt + mmb? if modifiers in [Qt.AltModifier, Qt.ControlModifier]: # RED if button == Qt.LeftButton: pos = QPoint(color.redF() * self.width(), color.redF() * self.height()) self.scene().gradient_type = attrs.RED # GREEN elif button == Qt.MiddleButton: pos = QPoint(color.greenF() * self.width(), color.greenF() * self.height()) self.scene().gradient_type = attrs.GREEN # BLUE elif button == Qt.RightButton: pos = QPoint(color.blueF() * self.width(), color.blueF() * self.height()) self.scene().gradient_type = attrs.BLUE # HSV else: if button == Qt.LeftButton: self.__hideRGBACrosshair(False) self.__hideLinearCrosshair(True) self.scene().gradient_type = attrs.RGBA # VALUE elif button == Qt.MiddleButton: pos = QPoint(color.valueF() * self.width(), color.valueF() * self.height()) self.scene().gradient_type = attrs.VALUE # SATURATION elif button == Qt.RightButton: pos = QPoint(color.saturationF() * self.width(), color.saturationF() * self.height()) self.scene().gradient_type = attrs.SATURATION # update display label to show selected value color_gradient_widget = getWidgetAncestor(self, ColorGradientDelegate) color_arg_widgets_dict = color_gradient_widget.header_widget.getWidgetDict() if self.scene().gradient_type != attrs.RGBA: color_arg_widgets_dict[self.scene().gradient_type].setSelected(True) # draw gradient / hide cursor self.scene().drawGradient() # set up cursor self.setCursor(Qt.BlankCursor) if pos: self.scene().linear_crosshair_item.setCrosshairPos(pos) QCursor.setPos(self.mapToGlobal(pos)) return QGraphicsView.mousePressEvent(self, event, *args, **kwargs)