()
return m
Return a list model of text encodings.

    The items are checkable and initialized based on current stored user
    preferences. Any change in check state is stored and writen back
    immediately.

    The normalized encoding (codec) names are accessible using `Qt.UserRole`

    Returns
    -------
    model : QAbstractItemModel
Return a list model of text encodings.
Return a list model of text encodings .
def encodings_model():
    # type: () -> QAbstractItemModel
    """
    Return a list model of text encodings.

    The items are checkable and initialized based on current stored user
    preferences. Any change in check state is stored and writen back
    immediately.

    The normalized encoding (codec) names are accessible using `Qt.UserRole`

    Returns
    -------
    model : QAbstractItemModel
    """
    m = QStandardItemModel()
    items = []
    settings = QSettings()
    settings.beginGroup(SettingsGroup)

    def is_selected(co):
        # type: (codecs.CodecInfo) -> bool
        return settings.value(
            co.name, defaultValue=co.name in DEFAULT_ENCODINGS, type=bool)

    def store_selected(index):
        # type: (QModelIndex) -> None
        # write back the selected state for index
        co = index.data(CodecInfoRole)
        state = index.data(Qt.CheckStateRole)
        if isinstance(co, codecs.CodecInfo):
            settings.setValue(co.name, state == Qt.Checked)

    for encoding, name in ENCODING_DISPLAY_NAME:
        try:
            co = codecs.lookup(encoding)
        except LookupError:
            continue

        item = QStandardItem(name)
        item.setData(co.name, EncodingNameRole)
        item.setData(co, CodecInfoRole)
        item.setToolTip(name + "; " + encoding)
        item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled |
                      Qt.ItemIsSelectable)
        item.setCheckState(Qt.Checked if is_selected(co) else Qt.Unchecked)
        items.append(item)

    def on_data_changed(first, last, roles=()):
        # type: (QModelIndex, QModelIndex, Iterable[int]) -> None
        if roles and Qt.CheckStateRole not in roles:
            return
        assert first.column() == last.column()
        for i in range(first.row(), last.row() + 1):
            index = first.sibling(i, first.column())
            store_selected(index)

    m.invisibleRootItem().appendRows(items)
    m.dataChanged.connect(on_data_changed)
    return m
def encodings_model ( ) : # type: () -> QAbstractItemModel m = QStandardItemModel ( ) items = [ ] settings = QSettings ( ) settings . beginGroup ( SettingsGroup ) def is_selected ( co ) : # type: (codecs.CodecInfo) -> bool return settings . value ( co . name , defaultValue = co . name in DEFAULT_ENCODINGS , type = bool ) def store_selected ( index ) : # type: (QModelIndex) -> None # write back the selected state for index co = index . data ( CodecInfoRole ) state = index . data ( Qt . CheckStateRole ) if isinstance ( co , codecs . CodecInfo ) : settings . setValue ( co . name , state == Qt . Checked ) for encoding , name in ENCODING_DISPLAY_NAME : try : co = codecs . lookup ( encoding ) except LookupError : continue item = QStandardItem ( name ) item . setData ( co . name , EncodingNameRole ) item . setData ( co , CodecInfoRole ) item . setToolTip ( name + "; " + encoding ) item . setFlags ( Qt . ItemIsUserCheckable | Qt . ItemIsEnabled | Qt . ItemIsSelectable ) item . setCheckState ( Qt . Checked if is_selected ( co ) else Qt . Unchecked ) items . append ( item ) def on_data_changed ( first , last , roles = ( ) ) : # type: (QModelIndex, QModelIndex, Iterable[int]) -> None if roles and Qt . CheckStateRole not in roles : return assert first . column ( ) == last . column ( ) for i in range ( first . row ( ) , last . row ( ) + 1 ) : index = first . sibling ( i , first . column ( ) ) store_selected ( index ) m . invisibleRootItem ( ) . appendRows ( items ) m . dataChanged . connect ( on_data_changed ) return m