Skip to content

Commit

Permalink
setData method / types for data role
Browse files Browse the repository at this point in the history
  • Loading branch information
bimac committed Oct 1, 2024
1 parent 3e02ef5 commit 90b91e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]

### Added

- core.DataFrameTableModel: setData method

### Fixed

- core.ColoredDataFrameTableModel: types for data roles


## [0.1.0] - 2024-10-01

_First release._
Expand Down
26 changes: 23 additions & 3 deletions iblqt/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Non-GUI functionality, including event handling, data types, and data management."""

import logging
from typing import Any

from pyqtgraph import ColorMap, colormap # type: ignore
from qtpy.QtCore import (
Expand Down Expand Up @@ -175,6 +176,25 @@ def data(
return QVariant(str(data))
return QVariant()

def setData(
self, index: QModelIndex, value: Any, role: int = Qt.ItemDataRole.DisplayRole
):
"""
Set data at the specified index with the given value.
Parameters
----------
index : QModelIndex
The index where the data will be set.
value : Any
The new value to be set at the specified index.
role : int, optional
The role of the data.
"""
if index.isValid():
self._dataFrame.iloc[index.row(), index.column()] = value
self.dataChanged.emit(index, index, [role])

def sort(self, column: int, order: Qt.SortOrder = Qt.SortOrder.AscendingOrder):
"""
Sort the data based on the specified column and order.
Expand Down Expand Up @@ -367,13 +387,13 @@ def data(
"""
if not index.isValid():
return QVariant()
if role in (Qt.BackgroundRole, Qt.ForegroundRole):
if role in (Qt.ItemDataRole.BackgroundRole, Qt.ItemDataRole.ForegroundRole):
row = self._dataFrame.index[index.row()]
col = index.column()
if role == Qt.BackgroundRole:
if role == Qt.ItemDataRole.BackgroundRole:
r, g, b = self._background[row][col]
return QVariant(QColor.fromRgb(r, g, b, self._alpha))
if role == Qt.ForegroundRole:
if role == Qt.ItemDataRole.ForegroundRole:
lum = self._foreground[row][col]
color = QColor('black' if (lum * self._alpha) < 32512 else 'white')
return QVariant(color)
Expand Down

0 comments on commit 90b91e3

Please sign in to comment.