def sortDocuments(self): """Sort documents list according to current sort mode""" newDocuments = copy.copy(self._workspace.sortedDocuments) if self._sortMode == self.OpeningOrder: newDocuments.sort(lambda a, b: cmp(self._workspace.sortedDocuments.index(a), \ self._workspace.sortedDocuments.index(b))) elif self._sortMode == self.FileName: newDocuments.sort(lambda a, b: cmp(a.fileName(), b.fileName())) elif self._sortMode == self.URL: newDocuments.sort(lambda a, b: cmp(a.filePath(), b.filePath())) elif self._sortMode == self.Suffixes: def sorter(a, b): # pylint: disable=C0103 """ Compare 2 file pathes""" aSuffix = os.path.splitext(a.filePath())[1] bSuffix = os.path.splitext(b.filePath())[1] return cmp(aSuffix, bSuffix) newDocuments.sort(sorter) elif self._sortMode == self.Custom: pass else: assert(0) self.rebuildMapping( self._workspace.sortedDocuments, newDocuments ) # scroll the view selected = QObject.parent(self).tvFiles.selectionModel().selectedIndexes() if selected: QObject.parent(self).tvFiles.scrollTo( selected[0] )
def sortDocuments(self): """Sort documents list according to current sort mode""" newDocuments = copy.copy(self._workspace.sortedDocuments) if self._sortMode == self.OpeningOrder: newDocuments.sort(lambda a, b: cmp(self._workspace.sortedDocuments.index(a), \ self._workspace.sortedDocuments.index(b))) elif self._sortMode == self.FileName: newDocuments.sort(lambda a, b: cmp(a.fileName(), b.fileName())) elif self._sortMode == self.URL: newDocuments.sort(lambda a, b: cmp(a.filePath(), b.filePath())) elif self._sortMode == self.Suffixes: def sorter(a, b): # pylint: disable=C0103 """ Compare 2 file pathes""" aSuffix = os.path.splitext(a.filePath())[1] bSuffix = os.path.splitext(b.filePath())[1] return cmp(aSuffix, bSuffix) newDocuments.sort(sorter) elif self._sortMode == self.Custom: pass else: assert (0) self.rebuildMapping(self._workspace.sortedDocuments, newDocuments) # scroll the view selected = QObject.parent( self).tvFiles.selectionModel().selectedIndexes() if selected: QObject.parent(self).tvFiles.scrollTo(selected[0])
def dropMimeData(self, data, action, row, column, parent ): # pylint: disable=R0913 """See QAbstractItemModel documentation""" if parent.isValid() or \ ( row == -1 and column == -1 ) or \ action != Qt.MoveAction or \ not data or \ not data.hasFormat( self.mimeTypes()[0] ) : return False fromRow = data.data( self.mimeTypes()[0] ).toInt()[0] if row >= len(self._workspace.sortedDocuments): row -= 1 elif fromRow < row : row -= 1 newDocuments = copy.copy(self._workspace.sortedDocuments) item = newDocuments.pop(fromRow) #if row > fromRow: # row -= 1 newDocuments.insert(row, item) self.rebuildMapping( self._workspace.sortedDocuments, newDocuments ) if self._sortMode != _OpenedFileModel.Custom : self.setSortMode( _OpenedFileModel.Custom ) QObject.parent(self).tvFiles.setCurrentIndex(self.documentIndex(item)) return True
def dropMimeData(self, data, action, row, column, parent): # pylint: disable=R0913 """See QAbstractItemModel documentation""" if parent.isValid() or \ ( row == -1 and column == -1 ) or \ action != Qt.MoveAction or \ not data or \ not data.hasFormat( self.mimeTypes()[0] ) : return False fromRow = data.data(self.mimeTypes()[0]).toInt()[0] if row >= len(self._workspace.sortedDocuments): row -= 1 elif fromRow < row: row -= 1 newDocuments = copy.copy(self._workspace.sortedDocuments) item = newDocuments.pop(fromRow) #if row > fromRow: # row -= 1 newDocuments.insert(row, item) self.rebuildMapping(self._workspace.sortedDocuments, newDocuments) if self._sortMode != _OpenedFileModel.Custom: self.setSortMode(_OpenedFileModel.Custom) QObject.parent(self).tvFiles.setCurrentIndex(self.documentIndex(item)) return True
def sortDocuments(self): """Sort documents list according to current sort mode""" sortedDocuments = self._workspace.sortedDocuments if not self._manuallySorted: sortedDocuments = sorted( sortedDocuments, lambda a, b: cmp(a.filePath(), b.filePath())) self.rebuildMapping(self._workspace.sortedDocuments, sortedDocuments) # scroll the view selected = QObject.parent( self).tvFiles.selectionModel().selectedIndexes() if selected: QObject.parent(self).tvFiles.scrollTo(selected[0])
def _onDocumentClosed(self, document): """Document has been closed. Unhandle it """ index = self._workspace.sortedDocuments.index(document) if index == -1: return # scroll the view QObject.parent(self).startModifyModel() self.beginRemoveRows(QModelIndex(), index, index) self._workspace.sortedDocuments.remove(document) self.endRemoveRows() QObject.parent(self).finishModifyModel()
def _onDocumentClosed(self, document ): """Document has been closed. Unhandle it """ index = self._workspace.sortedDocuments.index( document ) if index == -1 : return # scroll the view QObject.parent(self).startModifyModel() self.beginRemoveRows( QModelIndex(), index, index ) self._workspace.sortedDocuments.remove( document ) self.endRemoveRows() QObject.parent(self).finishModifyModel()
def has_ancestor(obj, object_type): parent = QObject.parent(obj) if parent is None: return False if isinstance(parent, object_type): return True return has_ancestor(parent, object_type)
def _assign_default_object_name( obj ): # Ensure that this object and its siblings have a child index assign_unique_child_index(obj) prop = obj.property('unique_child_index') if type(obj) == QPushButton and prop.isValid() and prop.toInt() == 0: assign_unique_child_index(obj) # Find all siblings (including this object) that appear to have auto-defined names parent = QObject.parent(obj) # Find all siblings of matching type if parent is not None: siblings = filter( lambda c:type(c) == type(obj), parent.children() ) siblings = filter(lambda w: not sip.isdeleted(w), siblings) else: siblings = filter( lambda c:type(c) == type(obj), get_toplevel_widgets() ) if obj not in siblings: # Special case for top-level widgets, since not all of its 'siblings' are included included in get_toplevel_widgets() index_among_default_names = obj.property('unique_child_index').toInt() newname = '{}_{}'.format( obj.__class__.__name__, index_among_default_names ) obj.setObjectName( newname ) else: siblings = filter( lambda c: str(c.objectName()) == "" or str(c.objectName()).startswith( '{}_'.format( obj.__class__.__name__ ) ), siblings ) if obj not in siblings: siblings.append(obj) siblings = sorted( siblings, key=lambda c: c.property('unique_child_index').toInt() ) # Rename everything we might have touched for obj in siblings: index_among_default_names = siblings.index( obj ) newname = '{}_{}'.format( obj.__class__.__name__, index_among_default_names ) obj.setObjectName( newname )
def assign_unique_child_index( child ): """ Assign a unique 'child index' to this child AND all its siblings of the same type. """ # Must call QObject.parent this way because obj.parent() is *shadowed* in # some subclasses (e.g. QModelIndex), which really is very ugly on Qt's part. parent = QObject.parent(child) # Find all siblings of matching type if parent is not None: matching_siblings = filter( lambda c:type(c) == type(child), parent.children() ) else: matching_siblings = filter( lambda c:type(c) == type(child), get_toplevel_widgets() ) matching_siblings = filter( lambda w: w is not None, matching_siblings) matching_siblings = filter(lambda w: not sip.isdeleted(w), matching_siblings) existing_indexes = set() for sibling in matching_siblings: prop = sibling.property('unique_child_index') if prop.isValid(): existing_indexes.add( prop.toInt() ) next_available_index = 0 for sibling in matching_siblings: while next_available_index in existing_indexes: next_available_index += 1 prop = sibling.property('unique_child_index') if not prop.isValid(): sibling.setProperty( 'unique_child_index', next_available_index ) existing_indexes.add( next_available_index )
def get_fully_qualified_name(obj): """ Return a fully qualified object name of the form: someobject.somechild.somegrandchild.etc Before returning, this function **renames** any children that don't have unique names within their parent. Note: The name uniqueness check and renaming algorithm are terribly inefficient, but it doesn't seem to slow things down much. We could improve this later if it becomes a problem. """ # Must call QObject.parent this way because obj.parent() is *shadowed* in # some subclasses (e.g. QModelIndex), which really is very ugly on Qt's part. parent = QObject.parent(obj) objName = obj.objectName() if objName == "": _assign_default_object_name(obj) if not _has_unique_name(obj): _normalize_child_names(parent) objName = str(obj.objectName()) # We combine object names using periods, which means they better not have periods themselves... assert objName.find( '.' ) == -1, "Objects names must not use periods! Found an object named: {}".format( objName) if parent is None: return objName fullname = "{}.".format(get_fully_qualified_name(parent)) + objName # Make sure no siblings have the same name! assert _has_unique_name( obj), "Detected multiple objects with full name: {}".format(fullname) return fullname
def setModel(self, model): """ Reimplemented. Parameters ---------- model : QAbstractItemModel """ if model is self.__completerModel: return if self.__completerModel is not None: self.__completerModel.dataChanged.disconnect( self.__initDynamicModel) self.__completerModel.rowsInserted.disconnect( self.__initDynamicModel) self.__completerModel.rowsRemoved.disconnect( self.__initDynamicModel) if QObject.parent(self.__completerModel) is self: self.__completerModel.deleteLater() self.__completerModel = None self.__completerModel = model if self.__completerModel is not None: self.__completerModel.dataChanged.connect( self.__initDynamicModel) self.__completerModel.rowsInserted.connect( self.__initDynamicModel) self.__completerModel.rowsRemoved.connect( self.__initDynamicModel) self.__initDynamicModel()
def has_ancestor(obj, object_type): parent = QObject.parent( obj ) if parent is None: return False if isinstance( parent, object_type ): return True return has_ancestor( parent, object_type )
def get_fully_qualified_name(obj): """ Return a fully qualified object name of the form: someobject.somechild.somegrandchild.etc Before returning, this function **renames** any children that don't have unique names within their parent. Note: The name uniqueness check and renaming algorithm are terribly inefficient, but it doesn't seem to slow things down much. We could improve this later if it becomes a problem. """ # Must call QObject.parent this way because obj.parent() is *shadowed* in # some subclasses (e.g. QModelIndex), which really is very ugly on Qt's part. parent = QObject.parent(obj) objName = obj.objectName() if objName == "": _assign_default_object_name(obj) if not _has_unique_name(obj): _normalize_child_names(parent) objName = str(obj.objectName()) # We combine object names using periods, which means they better not have periods themselves... assert objName.find('.') == -1, "Objects names must not use periods! Found an object named: {}".format( objName ) if parent is None: return objName fullname = "{}.".format( get_fully_qualified_name(parent) ) + objName # Make sure no siblings have the same name! for sibling in parent.children(): if sibling != obj: assert sibling.objectName() != objName, "Detected multiple objects with full name: {}".format( fullname ) return fullname
def setModel(self, model): """ Reimplemented. Parameters ---------- model : QAbstractItemModel """ if model is self.__completerModel: return if self.__completerModel is not None: self.__completerModel.dataChanged.disconnect( self.__initDynamicModel) self.__completerModel.rowsInserted.disconnect( self.__initDynamicModel) self.__completerModel.rowsRemoved.disconnect( self.__initDynamicModel) if QObject.parent(self.__completerModel) is self: self.__completerModel.deleteLater() self.__completerModel = None self.__completerModel = model if self.__completerModel is not None: self.__completerModel.dataChanged.connect(self.__initDynamicModel) self.__completerModel.rowsInserted.connect(self.__initDynamicModel) self.__completerModel.rowsRemoved.connect(self.__initDynamicModel) self.__initDynamicModel()
def assign_unique_child_index(child): """ Assign a unique 'child index' to this child AND all its siblings of the same type. """ # Must call QObject.parent this way because obj.parent() is *shadowed* in # some subclasses (e.g. QModelIndex), which really is very ugly on Qt's part. parent = QObject.parent(child) # Find all siblings of matching type if parent is not None: matching_siblings = filter(lambda c: type(c) == type(child), parent.children()) else: matching_siblings = filter(lambda c: type(c) == type(child), get_toplevel_widgets()) matching_siblings = filter(lambda w: w is not None, matching_siblings) matching_siblings = filter(lambda w: not sip.isdeleted(w), matching_siblings) existing_indexes = set() for sibling in matching_siblings: prop = sibling.property('unique_child_index') if prop.isValid(): existing_indexes.add(prop.toInt()) next_available_index = 0 for sibling in matching_siblings: while next_available_index in existing_indexes: next_available_index += 1 prop = sibling.property('unique_child_index') if not prop.isValid(): sibling.setProperty('unique_child_index', next_available_index) existing_indexes.add(next_available_index)
def _get_renamed_font(self, original_file, renamed_file, index): if renamed_file is not None and renamed_file.is_override: table = QObject.parent(self) font = table.font() return QFont(font.family(), font.pointSize(), QFont.Bold) return None
def _has_unique_name(obj): parent = QObject.parent(obj) if parent is None: return True # We assume that top-level widgets have unique names, which should usually be true. obj_name = obj.objectName() for child in parent.children(): if child is not obj and child.objectName() == obj_name: return False return True
def has_ancestor(obj, object_type): # Must call QObject.parent this way because obj.parent() is *shadowed* in # some subclasses (e.g. QModelIndex), which really is very ugly on Qt's part. parent = QObject.parent( obj ) if parent is None: return False if isinstance( parent, object_type ): return True return has_ancestor( parent, object_type )
def _assign_default_object_name( obj ): parent = QObject.parent(obj) if parent is None: # We just name the object after it's type and hope for the best. obj.setObjectName( obj.__class__.__name__ ) else: index = parent.children().index( obj ) newname = "child_{}_{}".format( index, obj.__class__.__name__ ) existing_names = map( QObject.objectName, parent.children() ) assert newname not in existing_names, "Children were not accessed in the expected order, so renaming is not consistent! Parent widget: {} already has a child with name: {}".format( get_fully_qualified_name(parent), newname ) obj.setObjectName( newname )
def _has_unique_name(obj): parent = QObject.parent(obj) if parent is None: return True # We assume that top-level widgets are uniquely named # Note that 'garbage' widgets may have parent=None as well. # In that case, we don't care about their names, AS LONG AS THEY AREN"T TOP-LEVEL. obj_name = obj.objectName() for child in parent.children(): if child is not obj and child.objectName() == obj_name: return False return True
def parent(self, index=None): """ Returns the parent of the model item with the given index. If the item has no parent, an invalid QModelIndex is returned. """ if index is None: # Overload with QObject.parent() return QObject.parent(self) if not index.isValid(): return QModelIndex() nodePref = self.index2_tableitem(index) parentPref = nodePref.qt_get_parent() if parentPref == self.rootPref: return QModelIndex() return self.createIndex(parentPref.qt_parents_index_of_me(), 0, parentPref)
def _assign_default_object_name(obj): parent = QObject.parent(obj) if parent is None: # We just name the object after it's type and hope for the best. obj.setObjectName(obj.__class__.__name__) else: index = parent.children().index(obj) newname = "child_{}_{}".format(index, obj.__class__.__name__) existing_names = map(QObject.objectName, parent.children()) assert newname not in existing_names, "Children were not accessed in the expected order, so renaming is not consistent! Parent widget: {} already has a child with name: {}".format( get_fully_qualified_name(parent), newname) obj.setObjectName(newname)
def _has_unique_name(obj): parent = QObject.parent(obj) if parent is None: return True # We assume that top-level widgets are uniquely named # Note that 'garbage' widgets may have parent=None as well. # In that case, we don't care about their names, AS LONG AS THEY AREN"T TOP-LEVEL. obj_name = obj.objectName() for child in parent.children(): if child is not obj and child.objectName() == obj_name: # If the conflicting child is hidden, it doesn't count as a real conflict. if isinstance(child, QWidget) and child.isVisible(): return False return True
def _has_unique_name(obj): parent = QObject.parent(obj) if parent is None: return True # We assume that top-level widgets are uniquely named # Note that 'garbage' widgets may have parent=None as well. # In that case, we don't care about their names, AS LONG AS THEY AREN"T TOP-LEVEL. obj_name = obj.objectName() for child in parent.children(): if child is not obj and child.objectName() == obj_name: # If the conflicting child is hidden, it doesn't count as a real conflict. if isinstance(child, QWidget) and child.isVisible(): return False return True
def parent(self, index=None): """ Returns the parent of the model item with the given index. If the item has no parent, an invalid QModelIndex is returned. """ if index is None: # Overload with QObject.parent() return QObject.parent(self) if not index.isValid(): return QModelIndex() nodePref = self.index2_tableitem(index) parentPref = nodePref.qt_get_parent() if parentPref == self.rootPref: return QModelIndex() return self.createIndex(parentPref.qt_parents_index_of_me(), 0, parentPref)
def _has_unique_name(obj): if obj.objectName() == '': return False # Must call QObject.parent this way because obj.parent() is *shadowed* in # some subclasses (e.g. QModelIndex), which really is very ugly on Qt's part. parent = QObject.parent(obj) if parent is None: siblings = get_toplevel_widgets() else: siblings = parent.children() siblings = filter(lambda w: w is not None, siblings) siblings = filter(lambda w: not sip.isdeleted(w), siblings) obj_name = obj.objectName() for child in siblings: if child is not obj and child.objectName() == obj_name: return False return True
def _has_unique_name(obj): if obj.objectName() == '': return False # Must call QObject.parent this way because obj.parent() is *shadowed* in # some subclasses (e.g. QModelIndex), which really is very ugly on Qt's part. parent = QObject.parent(obj) if parent is None: siblings = get_toplevel_widgets() else: siblings = parent.children() siblings = filter( lambda w: w is not None, siblings) siblings = filter(lambda w: not sip.isdeleted(w), siblings) obj_name = obj.objectName() for child in siblings: if child is not obj and child.objectName() == obj_name: return False return True
def _assign_default_object_name(obj): # Ensure that this object and its siblings have a child index assign_unique_child_index(obj) prop = obj.property('unique_child_index') if type(obj) == QPushButton and prop.isValid() and prop.toInt() == 0: assign_unique_child_index(obj) # Find all siblings (including this object) that appear to have auto-defined names parent = QObject.parent(obj) # Find all siblings of matching type if parent is not None: siblings = filter(lambda c: type(c) == type(obj), parent.children()) siblings = filter(lambda w: not sip.isdeleted(w), siblings) else: siblings = filter(lambda c: type(c) == type(obj), get_toplevel_widgets()) if obj not in siblings: # Special case for top-level widgets, since not all of its 'siblings' are included included in get_toplevel_widgets() index_among_default_names = obj.property('unique_child_index').toInt() newname = '{}_{}'.format(obj.__class__.__name__, index_among_default_names) obj.setObjectName(newname) else: siblings = filter( lambda c: str(c.objectName()) == "" or str(c.objectName()). startswith('{}_'.format(obj.__class__.__name__)), siblings) if obj not in siblings: siblings.append(obj) siblings = sorted( siblings, key=lambda c: c.property('unique_child_index').toInt()) # Rename everything we might have touched for obj in siblings: index_among_default_names = siblings.index(obj) newname = '{}_{}'.format(obj.__class__.__name__, index_among_default_names) obj.setObjectName(newname)
def parent(self): return QObject.parent(self)