Skip to content

Commit f699110

Browse files
authored
Fix cell/material tab in color options dialog (#143)
* Fix outdated use of Qt API * Fix cell/material table in color options dialog
1 parent e93a120 commit f699110

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

openmc_plotter/plotmodel.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import itertools
88
import pickle
99
import threading
10-
from typing import Literal, Tuple, Optional
10+
from typing import Literal, Tuple, Optional, Dict
1111

1212
from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
1313
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
@@ -1025,7 +1025,7 @@ def __hash__(self):
10251025
return hash(self.__dict__.__str__() + self.__str__())
10261026

10271027
@staticmethod
1028-
def getDomains(domain_type, rng):
1028+
def getDomains(domain_type, rng) -> DomainViewDict:
10291029
""" Return dictionary of domain settings.
10301030
10311031
Retrieve cell or material ID numbers and names from .xml files
@@ -1109,7 +1109,7 @@ class DomainViewDict(dict):
11091109
the defaults dictionary.
11101110
11111111
"""
1112-
def __init__(self, defaults: dict):
1112+
def __init__(self, defaults: Dict[int, DomainView]):
11131113
self.defaults = defaults
11141114

11151115
def __getitem__(self, key) -> DomainView:
@@ -1129,6 +1129,10 @@ def __deepcopy__(self, memo):
11291129
obj.defaults = self.defaults
11301130
return obj
11311131

1132+
def set_name(self, key: int, name: Optional[str]):
1133+
domain = self[key]
1134+
self[key] = DomainView(domain.id, name, domain.color, domain.masked, domain.highlight)
1135+
11321136
def set_color(self, key: int, color):
11331137
domain = self[key]
11341138
self[key] = DomainView(domain.id, domain.name, color, domain.masked, domain.highlight)
@@ -1186,22 +1190,24 @@ def __eq__(self, other):
11861190
class DomainTableModel(QAbstractTableModel):
11871191
""" Abstract Table Model of cell/material view attributes """
11881192

1189-
def __init__(self, domains):
1193+
def __init__(self, domains: DomainViewDict):
11901194
super().__init__()
1191-
self.domains = [dom for dom in domains.values()]
1195+
self.domains = domains
1196+
self.keys = dict(enumerate(self.domains.defaults))
11921197

11931198
def rowCount(self, index=QModelIndex()):
1194-
return len(self.domains)
1199+
return len(self.keys)
11951200

11961201
def columnCount(self, index=QModelIndex()):
11971202
return 6
11981203

11991204
def data(self, index, role=Qt.DisplayRole):
12001205

1201-
if not index.isValid() or not (0 <= index.row() < len(self.domains)):
1206+
if not index.isValid() or not (0 <= index.row() < self.rowCount()):
12021207
return None
12031208

1204-
domain = self.domains[index.row()]
1209+
key = self.keys[index.row()]
1210+
domain = self.domains[key]
12051211
column = index.column()
12061212

12071213
if role == Qt.DisplayRole:
@@ -1212,7 +1218,7 @@ def data(self, index, role=Qt.DisplayRole):
12121218
elif column == COLOR:
12131219
return '' if domain.color is not None else '+'
12141220
elif column == COLORLABEL:
1215-
return str(domain.color) if domain.color is not None else '--'
1221+
return str(tuple(domain.color)) if domain.color is not None else '--'
12161222
elif column == MASK:
12171223
return None
12181224
elif column == HIGHLIGHT:
@@ -1235,10 +1241,10 @@ def data(self, index, role=Qt.DisplayRole):
12351241
elif role == Qt.BackgroundRole:
12361242
color = domain.color
12371243
if column == COLOR:
1238-
if isinstance(color, tuple):
1239-
return QColor.fromRgb(*color)
1240-
elif isinstance(color, str):
1244+
if isinstance(color, str):
12411245
return QColor.fromRgb(*openmc.plots._SVG_COLORS[color])
1246+
else:
1247+
return QColor.fromRgb(*color)
12421248

12431249
elif role == Qt.CheckStateRole:
12441250
if column == MASK:
@@ -1282,24 +1288,24 @@ def flags(self, index):
12821288

12831289
def setData(self, index, value, role=Qt.EditRole):
12841290

1285-
if not index.isValid() or not (0 <= index.row() < len(self.domains)):
1291+
if not index.isValid() or not (0 <= index.row() < self.rowCount()):
12861292
return False
12871293

1288-
domain = self.domains[index.row()]
1294+
key = self.keys[index.row()]
12891295
column = index.column()
12901296

12911297
if column == NAME:
1292-
domain.name = value if value else None
1298+
self.domains.set_name(key, value if value else None)
12931299
elif column == COLOR:
1294-
domain.color = value
1300+
self.domains.set_color(key, value)
12951301
elif column == COLORLABEL:
1296-
domain.color = value
1302+
self.domains.set_color(key, value)
12971303
elif column == MASK:
12981304
if role == Qt.CheckStateRole:
1299-
domain.masked = True if value == Qt.Checked else False
1305+
self.domains.set_masked(key, value == Qt.Checked)
13001306
elif column == HIGHLIGHT:
13011307
if role == Qt.CheckStateRole:
1302-
domain.highlight = True if value == Qt.Checked else False
1308+
self.domains.set_highlight(value == Qt.Checked)
13031309

13041310
self.dataChanged.emit(index, index)
13051311
return True
@@ -1338,7 +1344,7 @@ def createEditor(self, parent, option, index):
13381344
def setEditorData(self, editor, index):
13391345

13401346
if index.column() == COLOR:
1341-
color = index.data(Qt.BackgroundColorRole)
1347+
color = index.data(Qt.BackgroundRole)
13421348
color = 'white' if color is None else color
13431349
editor.setCurrentColor(color)
13441350
elif index.column() in (NAME, COLORLABEL):
@@ -1349,7 +1355,7 @@ def setEditorData(self, editor, index):
13491355
def editorEvent(self, event, model, option, index):
13501356

13511357
if index.column() in (COLOR, COLORLABEL):
1352-
if not int(index.flags() & Qt.ItemIsEditable) > 0:
1358+
if not (index.flags() & Qt.ItemIsEditable).value > 0:
13531359
return False
13541360
if event.type() == QEvent.MouseButtonRelease \
13551361
and event.button() == Qt.RightButton:
@@ -1365,28 +1371,28 @@ def setModelData(self, editor, model, index):
13651371
column = index.column()
13661372

13671373
if column == COLOR and editor is None:
1368-
model.setData(index, None, Qt.BackgroundColorRole)
1374+
model.setData(index, None, Qt.BackgroundRole)
13691375
model.setData(model.index(row, column+1), None, Qt.DisplayRole)
13701376
elif column == COLOR:
13711377
color = editor.currentColor()
13721378
if color != QColor():
13731379
color = color.getRgb()[:3]
1374-
model.setData(index, color, Qt.BackgroundColorRole)
1380+
model.setData(index, color, Qt.BackgroundRole)
13751381
model.setData(model.index(row, column+1),
13761382
color,
13771383
Qt.DisplayRole)
13781384
elif column == COLORLABEL:
13791385
if editor is None:
13801386
model.setData(model.index(row, column-1),
13811387
None,
1382-
Qt.BackgroundColorRole)
1388+
Qt.BackgroundRole)
13831389
model.setData(index, None, Qt.DisplayRole)
13841390
elif editor.text().lower() in openmc.plots._SVG_COLORS:
13851391
svg = editor.text().lower()
13861392
color = openmc.plots._SVG_COLORS[svg]
13871393
model.setData(model.index(row, column-1),
13881394
color,
1389-
Qt.BackgroundColorRole)
1395+
Qt.BackgroundRole)
13901396
model.setData(index, svg, Qt.DisplayRole)
13911397
else:
13921398
try:
@@ -1400,7 +1406,7 @@ def setModelData(self, editor, model, index):
14001406
return None
14011407
model.setData(model.index(row, column-1),
14021408
input,
1403-
Qt.BackgroundColorRole)
1409+
Qt.BackgroundRole)
14041410
model.setData(index, input, Qt.DisplayRole)
14051411
else:
14061412
QItemDelegate.setModelData(self, editor, model, index)

0 commit comments

Comments
 (0)