示例#1
0
    def select_ncloth_outliner_item_from_dock_widget(self, dock_widget):
        """
        Selects nodes in the nclothOutliner that are selected in the Maya scene
        """
        # Get the selection model of the TreeView and initiate an empty item selection
        selection_model = self.selectionModel()
        selection = QtCore.QItemSelection()

        # print "dock_widget_tree_view:", type(dock_widget_tree_view), dock_widget_tree_view
        # dock_widget = dock_widget_tree_view.parent()
        print "dock_widget:", type(dock_widget), dock_widget
        dock_widget_mobj = dock_widget.mobj
        print "dock_widget_mobj:", type(dock_widget_mobj), dock_widget_mobj

        ncloth_outliner_item = self.find_item_by_mobj_with_path(
            dock_widget_mobj)
        if ncloth_outliner_item is None:
            return False

        # Otherwise: Find the index of the found item...
        index = self.model().indexFromItem(ncloth_outliner_item)
        # ...and append the item to the selection model (must be via ItemSelectionRange)
        selected_item = QtCore.QItemSelectionRange(index)
        selection.append(selected_item)

        # Finally: Clear the current selection and select the new itemSelection
        selection_model.select(selection,
                               QtCore.QItemSelectionModel.ClearAndSelect)
    def getSelectedIndexs(self, proxy):
        #pyside and pyside 2 compatible
        try:
            out = QtGui.QItemSelection()
        except:
            out = QtCore.QItemSelection()

        chld = [
            self.index(i, 0, None) for i in xrange(len(self._data.children))
        ]
        first = None
        last = None

        while len(chld):
            grandChild = []
            for c in chld:
                node = c.internalPointer()
                for i in xrange(len(node.children)):
                    grandChild.append(self.index(i, 0, c))

                if node.isSelected:
                    mI = proxy.mapFromSource(c)
                    out.select(mI, mI)  #,QtGui.QItemSelectionModel.Select)

            chld = grandChild
        return out
示例#3
0
    def select_ncloth_outliner_items_from_scene_selection(self):
        """
        Selects nodes in the nclothOutliner that are selected in the Maya scene
        """
        # Get the selection model of the TreeView and initiate an empty item selection
        selection_model = self.selectionModel()
        selection = QtCore.QItemSelection()

        # Go through all mobjs that are currently selected in the Maya scene
        for selected_mobj in MUtil._selected_nodes_in_scene_as_mobjs():
            # Try to match the selected mobj to any outliner item (using their long name)
            ncloth_outliner_item = self.find_item_by_mobj_with_path(
                selected_mobj)
            if ncloth_outliner_item is None:
                # If the mobj itself couldn't be found: try its shape!
                shape_mobj = MUtil._get_shape_mobj(selected_mobj)
                ncloth_outliner_item = self.find_item_by_mobj_with_path(
                    shape_mobj)

            # If no outliner-item was matched: continue
            if ncloth_outliner_item is None:
                continue

            # Otherwise: Find the index of the found item...
            index = self.model().indexFromItem(ncloth_outliner_item)
            # ...and append the item to the selection model (must be via ItemSelectionRange)
            selected_item = QtCore.QItemSelectionRange(index)
            selection.append(selected_item)

        # Finally: Clear the current selection and select the new itemSelection
        selection_model.select(selection,
                               QtCore.QItemSelectionModel.ClearAndSelect)
示例#4
0
    def __onActionReceived__(self, action, phase):
        super(OQWidgetList, self).__onActionReceived__(action, phase)

        if action.sender is self.__model.obj() and phase is POST:

            # Wait for the model to receive it first.
            self.__model.objToken().wait()

            # Open persistent editors when items are inserted.
            if isinstance(action.change, ListInsert):
                self.clearSelection()
                indexes = []
                for i in x_range(action.change.index,
                                 action.change.last_index + 1):
                    index = self.__model.index(i, 0, QtCore.QModelIndex())
                    self.openPersistentEditor(index)
                    indexes.append(index)

                if indexes:
                    if len(indexes) > 1:
                        self.select(
                            QtCore.QItemSelection(indexes[0], indexes[-1]),
                            QtCore.QItemSelectionModel.Select,
                            indexes[-1],
                        )
                    else:
                        self.select(indexes[0],
                                    QtCore.QItemSelectionModel.Select,
                                    indexes[0])

            # Select moved items.
            elif isinstance(action.change, ListMove):
                first = self.__model.index(action.change.post_index, 0,
                                           QtCore.QModelIndex())
                if action.change.post_index != action.change.post_last_index:
                    last = self.__model.index(action.change.post_last_index, 0,
                                              QtCore.QModelIndex())
                    selection = QtCore.QItemSelection(first, last)
                    current = last
                else:
                    selection = first
                    current = first
                self.select(selection,
                            QtCore.QItemSelectionModel.ClearAndSelect, current)

            # Update layout.
            self.__updateLayout__()
示例#5
0
	def __selectIndexes( self, indexes ) :

		selectionModel = self._qtWidget().selectionModel()

		if set( selectionModel.selectedIndexes() ) != set( indexes ) :
			selection = QtCore.QItemSelection()
			for index in indexes :
				selection.select( index, index )
			selectionModel.select( selection, QtCore.QItemSelectionModel.ClearAndSelect )

		if not selectionModel.isSelected( selectionModel.currentIndex() ) :
			selectionModel.setCurrentIndex( indexes[ -1 ], QtCore.QItemSelectionModel.ClearAndSelect )
示例#6
0
	def __addRows( self, button ) :

		if self.__model is None :
			return

		# Get the data we want to append.

		newData = self._createRows()
		if not newData :
			return

		# Extend our current data with the new data,
		# and call setData() to update the table view.

		data = self.getData()
		assert( len( data ) == len( newData ) )
		originalLength = len( data[0] )
		for i in range( 0, len( data ) ) :
			data[i].extend( newData[i] )

		self.setData( data )

		# Select the newly created rows, making the last one
		# the current selection (the one used as the endpoint
		# for shift-click region selects).

		lastIndex = self.__model.index( len( data[0] ) - 1, 0 )

		self.__tableView.setCurrentIndex(
			lastIndex
		)

		selection = QtCore.QItemSelection(
			self.__model.index( originalLength, 0 ),
			lastIndex
		)

		self.__tableView.selectionModel().select(
			selection,
			QtCore.QItemSelectionModel.ClearAndSelect | QtCore.QItemSelectionModel.Rows
		)

		# Scroll so the newly added item is visible, and
		# move the focus to the table view, so the new item can
		# be edited with the keyboard immediately.

		self.__tableView.scrollToBottom()
		self.__tableView.setFocus( QtCore.Qt.OtherFocusReason )

		# Let everyone know about this wondrous event.

		self.__emitDataChangedSignal()
示例#7
0
	def __selectDisplayIndex( self, index ) :

		if not index.isValid() :
			return

		selectionMode = QtCore.QItemSelectionModel.ClearAndSelect | QtCore.QItemSelectionModel.Rows
		selection = self._qtWidget().selectionModel()

		if self.__displayTransform is not None :
			# Expand the selection to make sure we have the whole row as the transform may map columns to rows
			row = index.row()
			lastColumn = index.model().columnCount() - 1
			rowSelection = QtCore.QItemSelection( index.sibling( row, 0 ), index.sibling( row, lastColumn ) )
			selection.select( self.__displayTransform.mapSelectionFromSource( rowSelection ), selectionMode )
		else :
			selection.select( index, selectionMode )
示例#8
0
    def onModelItemsReordered(self):
        new_selection = QtCore.QItemSelection()
        new_index = QtCore.QModelIndex()
        for item in self.model().lastDroppedItems:
            row = self.model().rowForItem(item)
            if row is None:
                continue
            new_index = self.model().index(row, 0, QtCore.QModelIndex())
            new_selection.select(new_index, new_index)

        self.clearSelection()
        flags = (
            QtCore.QItemSelectionModel.ClearAndSelect
            | QtCore.QItemSelectionModel.Rows
            | QtCore.QItemSelectionModel.Current
        )
        self.select(new_selection, flags)
        self.setCurrentIndex(new_index, flags)
示例#9
0
    def select(self, selection, flags):

        assert (isinstance(selection,
                           (QtCore.QItemSelection, QtCore.QModelIndex)))

        if isinstance(selection, QtCore.QModelIndex):
            # The overload of this just wraps the selection and calls back here
            # which results in double evaluation
            selection = QtCore.QItemSelection(selection, selection)

        QtCore.QItemSelectionModel.select(self, selection, flags)

        # Forward to source model
        self.__settingSelection = True
        try:
            sourceSelection = self.mapSelectionToSource(selection)
            self.__sourceSelectionModel.select(sourceSelection, flags)
        finally:
            self.__settingSelection = False
示例#10
0
    def startDrag(self, _):
        """Start drag."""

        # Can we drag?
        model = self.model()
        if not self.dragEnabled() or model is None:
            return
        model_obj = model.obj()
        if model_obj is None:
            return

        # Get selected indexes.
        selected_indexes = self.selectedIndexes()
        if not selected_indexes:
            return

        # In a write context.
        with model_obj.app.write_context():

            # Get indexes.
            sorted_indexes = sorted(selected_indexes, key=lambda i: i.row())
            first_index, last_index = sorted_indexes[0], sorted_indexes[-1]
            selection = QtCore.QItemSelection(first_index, last_index)

            # Get mime data.
            mime_data = model.mimeData(selected_indexes)
            if mime_data is None:
                return

            # Get drag actions from the model.
            drag_actions = model.supportedDragActions()

            # Start drag.
            viewport = self.viewport()
            drag = QtGui.QDrag(viewport)
            drag.setMimeData(mime_data)

            # Prepare pixmap.
            pixmap = QtGui.QPixmap(viewport.visibleRegion().boundingRect().size())
            pixmap.fill(QtCore.Qt.transparent)
            painter = QtGui.QPainter(pixmap)
            visual_rect = self.visualRegionForSelection(selection).boundingRect()
            if hasattr(viewport, "grab"):
                painter.drawPixmap(visual_rect, viewport.grab(visual_rect))
            else:
                painter.drawPixmap(
                    visual_rect,
                    QtGui.QPixmap.grabWidget(viewport, visual_rect),
                )
            painter.end()
            drag.setPixmap(pixmap)  # TODO: gradient fade if overflowing
            drag.setHotSpot(self.viewport().mapFromGlobal(QtGui.QCursor.pos()))

            # Prepare cursor.
            move_cursor = QtGui.QCursor(QtCore.Qt.DragMoveCursor)
            copy_cursor = QtGui.QCursor(QtCore.Qt.DragCopyCursor)
            drag.setDragCursor(move_cursor.pixmap(), QtCore.Qt.MoveAction)
            drag.setDragCursor(copy_cursor.pixmap(), QtCore.Qt.CopyAction)

            # Get state before.
            state_before = model_obj._state

            # Execute drag.
            try:
                action = drag.exec_(drag_actions)
            finally:
                moved_mimed_data_id = _internal_move_cache.pop(model_obj, None)

            # Move action was performed and state did not change, so item was moved
            # somewhere else. Delete the items from the list in this case.
            if moved_mimed_data_id != id(mime_data):
                if action == QtCore.Qt.MoveAction:
                    state_after = model_obj._state
                    if state_before is state_after:
                        model_obj.delete(slice(first_index.row(), last_index.row() + 1))
示例#11
0
def QItemSelection():
    try:
        return QtGui.QItemSelection()
    except Exception:
        return QtCore.QItemSelection()