Exemple #1
0
 def test_data(self):
     """test data and setData"""
     index = QModelIndex()
     index.sibling(0, 0)
     self.assertEqual(index.isValid(), False)
     self.assertEqual(
         self.model_user.data(index, Qt.DisplayRole).value(), None)
     self.session.add(model.User(name='John', lastname='Dow'))
     self.model_user.refresh()
     index = self.model_user.createIndex(0, 1)
     self.assertEqual(index.isValid(), True)
     self.assertEqual(self.model_user.data(index, Qt.DisplayRole), 'Dow')
     index = self.model_user.createIndex(0, 2)
     self.assertEqual(self.model_user.data(index, Qt.DisplayRole), 'John')
     self.assertEqual(self.model_user.setData(index, 'Jonny'), True)
     self.assertEqual(self.model_user.data(index, Qt.DisplayRole), 'Jonny')
Exemple #2
0
    def flags(self, index: Qt.QModelIndex) -> Qt.Qt.ItemFlags:
        # debug.print_func_name()
        if not index.isValid():
            return cast(Qt.Qt.ItemFlags, Qt.Qt.ItemIsEnabled)

        return cast(Qt.Qt.ItemFlags,
                    super().flags(index) | Qt.Qt.ItemIsEditable)
Exemple #3
0
    def _is_index_valid(self, index: QModelIndex) -> bool:
        """

        :param index:
        :return:
        """
        if index.row() < 0 or index.row() >= self.rowCount() or not index.isValid():
            return False
        return True
Exemple #4
0
    def data(self, index: Qt.QModelIndex, role: int = Qt.Qt.UserRole) -> Any:
        if (not index.isValid() or index.row() >= len(self.levels)):
            return None

        if role == Qt.Qt.DisplayRole:
            return '{}%'.format(round(self.levels[index.row()] * 100))
        if role == Qt.Qt.UserRole:
            return self.levels[index.row()]
        return None
Exemple #5
0
    def data(self, index: QModelIndex, role: int):
        if not index.isValid():
            return None

        row = index.row()

        if role == Qt.DisplayRole:
            return self._data[row].name
        elif role == Qt.EditRole:
            return self._data[row]
Exemple #6
0
    def data(self, index: QModelIndex, role=Qt.DisplayRole):
        if not index.isValid():
            return None
        if role == Qt.DisplayRole:
            if index.column() == 0:
                return self.image_names[index.row()].name
            return None

        if role == Qt.DecorationRole and index.column() == 1:
            average_snow_height = self.camera.average_snow_height(
                self.image_names[index.row()].name)
            if average_snow_height > 0:
                if average_snow_height == 1:
                    return self.snow_level1
                elif average_snow_height == 2:
                    return self.snow_level2
                elif average_snow_height >= 3:
                    return self.snow_level3
            return None

        if role == Qt.DecorationRole and index.column() == 0:
            return self.thumbnails.get_thumbnail(index.row(), self.dragging)

        if role == Qt.BackgroundRole:
            photo_state = self.camera.photo_state(index.row())
            if photo_state == PhotoState.Processed:
                quality = self.camera.image_quality(
                    self.image_names[index.row()].name)
                if quality < 0.33:
                    color = self.quality_colors['BAD']
                elif quality < 0.66:
                    color = self.quality_colors['OK']
                else:
                    color = self.quality_colors['GOOD']
                return QBrush(color)
            elif photo_state == PhotoState.Skipped:
                return QBrush(QColor(150, 150, 150, 255))
            else:
                QBrush(QColor(0, 0, 0, 200))

        if role == Qt.UserRole:
            return self.image_names[index.row()]

        if role == Qt.UserRole + 1:
            return self.camera.is_snowy(self.image_names[index.row()].name)

        if role == Qt.UserRole + 2:
            return 'snow' if self.camera.is_snowy(
                self.image_names[index.row()].name) else 'ground'

        if role == Qt.ForegroundRole and index.row(
        ) < self.processed_images_count:
            return QBrush(QColor(0, 150, 0))
        return None
Exemple #7
0
    def setData(self, index: Qt.QModelIndex, value: Any, role: int = Qt.Qt.EditRole) -> bool:
        if not index.isValid():
            return False
        if not role == Qt.Qt.EditRole:
            return False
        if not isinstance(value, str):
            return False

        self.items[index.row()].name = value
        self.dataChanged.emit(index, index, [role])
        return True
Exemple #8
0
    def data(self, index: Qt.QModelIndex, role: int = Qt.Qt.UserRole) -> Any:
        if not index.isValid():
            return None
        if index.row() >= len(self.items):
            return None

        if role in (Qt.Qt.DisplayRole, Qt.Qt.EditRole):
            return self.items[index.row()].name
        if role == Qt.Qt.UserRole:
            return self.items[index.row()]
        return None
Exemple #9
0
 def data(self, index: QModelIndex, role=Qt.DisplayRole) -> QVariant:
     """возвращает отображаемое значение"""
     if not index.isValid():
         return QVariant()
     row, col = index.row(), index.column()
     data = self._data[row]
     if role == Qt.UserRole:
         return QVariant(data)
     if role == Qt.DisplayRole:
         key = self._display[col]
         return data[key] if key in data.keys() else QVariant()
     return QVariant()
    def data(self, index: QModelIndex, role: int = ...) -> typing.Any:
        if not index.isValid():
            return None
        row = index.row()
        column = index.column()

        if role == Qt.DisplayRole:
            if column == HeaderEnum.ADDRESS:
                return hex(self.values[row].address)
            if column == HeaderEnum.CURRENT_VALUE:
                return self.values[row].value
            if column == HeaderEnum.PREVIOUS_VALUE:
                return self.values[row].previous_value
        return None
    def setData(self, index: QModelIndex, value: typing.Any, role: int = Qt.EditRole) -> bool:
        if not index.isValid():
            return False

        if role == Qt.EditRole:
            row = index.row()
            if index.column() == HeaderEnum.CURRENT_VALUE:
                self.values[row].value = value
            elif index.column() == HeaderEnum.PREVIOUS_VALUE:
                self.values[row].previous_value = value
            elif index.column() == HeaderEnum.ADDRESS:
                self.values[row].address = value

            self.dataChanged.emit(index, index)
            return True

        return False
Exemple #12
0
    def data(self, index: Qt.QModelIndex, role: int = Qt.Qt.UserRole) -> Any:
        if not index.isValid():
            return None
        row = index.row()
        if row >= len(self.items):
            return None
        column = index.column()
        if column >= self.COLUMN_COUNT:
            return None

        if role in (Qt.Qt.DisplayRole, Qt.Qt.EditRole):
            if column == self.START_FRAME_COLUMN:
                return str(self.items[row].start)
            if column == self.END_FRAME_COLUMN:
                if self.items[row].end != self.items[row].start:
                    return str(self.items[row].end)
                else:
                    return ''
            if column == self.START_TIME_COLUMN:
                return str(Time(self.items[row].start))
            if column == self.END_TIME_COLUMN:
                if self.items[row].end != self.items[row].start:
                    return str(Time(self.items[row].end))
                else:
                    return ''
            if column == self.LABEL_COLUMN:
                return str(self.items[row].label)

        if role == Qt.Qt.UserRole:
            if column == self.START_FRAME_COLUMN:
                return self.items[row].start
            if column == self.END_FRAME_COLUMN:
                return self.items[row].end
            if column == self.START_TIME_COLUMN:
                return Time(self.items[row].start)
            if column == self.END_TIME_COLUMN:
                return Time(self.items[row].end)
            if column == self.LABEL_COLUMN:
                return self.items[row].label

        return None
Exemple #13
0
    def flags(self, index: Qt.QModelIndex) -> Qt.Qt.ItemFlags:
        if not index.isValid():
            return cast(Qt.Qt.ItemFlags, Qt.Qt.ItemIsEnabled)

        return cast(Qt.Qt.ItemFlags,
                    super().flags(index) | Qt.Qt.ItemIsEditable)
Exemple #14
0
    def setData(self,
                index: Qt.QModelIndex,
                value: Any,
                role: int = Qt.Qt.EditRole) -> bool:
        from copy import deepcopy

        if not index.isValid():
            return False
        if role not in (Qt.Qt.EditRole, Qt.Qt.UserRole):
            return False

        row = index.row()
        column = index.column()
        scene = deepcopy(self.items[row])

        if column == self.START_FRAME_COLUMN:
            if not isinstance(value, Frame):
                raise TypeError
            if scene.start != scene.end:
                if value > scene.end:
                    return False
                scene.start = value
            else:
                scene.start = value
                scene.end = value
            proper_update = True
        elif column == self.END_FRAME_COLUMN:
            if not isinstance(value, Frame):
                raise TypeError
            if scene.start != scene.end:
                if value < scene.start:
                    return False
                scene.end = value
            else:
                scene.start = value
                scene.end = value
            proper_update = True
        elif column == self.START_TIME_COLUMN:
            if not isinstance(value, Time):
                raise TypeError
            frame = Frame(value)
            if scene.start != scene.end:
                if frame > scene.end:
                    return False
                scene.start = frame
            else:
                scene.start = frame
                scene.end = frame
            proper_update = True
        elif column == self.END_TIME_COLUMN:
            if not isinstance(value, Time):
                raise TypeError
            frame = Frame(value)
            if scene.start != scene.end:
                if frame < scene.start:
                    return False
                scene.end = frame
            else:
                scene.start = frame
                scene.end = frame
            proper_update = True
        elif column == self.LABEL_COLUMN:
            if not isinstance(value, str):
                raise TypeError
            scene.label = value
            proper_update = False

        if proper_update is True:
            i = bisect_right(self.items, scene)
            if i > row:
                i -= 1
            if i != row:
                self.beginMoveRows(self.createIndex(row, 0), row, row,
                                   self.createIndex(i, 0), i)
                del self.items[row]
                self.items.insert(i, scene)
                self.endMoveRows()
            else:
                self.items[index.row()] = scene
                self.dataChanged.emit(index, index)
        else:
            self.items[index.row()] = scene
            self.dataChanged.emit(index, index)
        return True