Skip to content

Commit d8992f1

Browse files
committed
oweditdomain: Indicate variables in error state
(have duplicated names)
1 parent 848fc18 commit d8992f1

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

Orange/widgets/data/oweditdomain.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
QStyledItemDelegate, QStyleOptionViewItem, QStyle, QSizePolicy,
2525
QDialogButtonBox, QPushButton, QCheckBox, QComboBox, QStackedLayout,
2626
QDialog, QRadioButton, QGridLayout, QLabel, QSpinBox, QDoubleSpinBox,
27-
QAbstractItemView, QMenu
27+
QAbstractItemView, QMenu, QToolTip
28+
)
29+
from AnyQt.QtGui import (
30+
QStandardItemModel, QStandardItem, QKeySequence, QIcon, QBrush, QPalette,
31+
QHelpEvent
2832
)
29-
from AnyQt.QtGui import QStandardItemModel, QStandardItem, QKeySequence, QIcon
3033
from AnyQt.QtCore import (
3134
Qt, QSize, QModelIndex, QAbstractItemModel, QPersistentModelIndex, QRect,
3235
QPoint,
@@ -1588,11 +1591,30 @@ def initStyleOption(self, option, index):
15881591
# mark as changed (maybe also change color, add text, ...)
15891592
option.font.setItalic(True)
15901593

1594+
multiplicity = index.data(MultiplicityRole)
1595+
if isinstance(multiplicity, int) and multiplicity > 1:
1596+
option.palette.setBrush(QPalette.Text, QBrush(Qt.red))
1597+
option.palette.setBrush(QPalette.HighlightedText, QBrush(Qt.red))
1598+
1599+
def helpEvent(self, event: QHelpEvent, view: QAbstractItemView,
1600+
option: QStyleOptionViewItem, index: QModelIndex) -> bool:
1601+
multiplicity = index.data(MultiplicityRole)
1602+
name = VariableListModel.effective_name(index)
1603+
if isinstance(multiplicity, int) and multiplicity > 1 \
1604+
and name is not None:
1605+
QToolTip.showText(
1606+
event.globalPos(), f"Name `{name}` is duplicated",
1607+
view.viewport()
1608+
)
1609+
return True
1610+
else: # pragma: no cover
1611+
return super().helpEvent(event, view, option, index)
1612+
15911613

15921614
# Item model for edited variables (Variable). Define a display role to be the
15931615
# source variable name. This is used only in keyboard search. The display is
15941616
# otherwise completely handled by a delegate.
1595-
class VariableListModel(itemmodels.PyListModel):
1617+
class VariableListModel(CountedListModel):
15961618
def data(self, index, role=Qt.DisplayRole):
15971619
# type: (QModelIndex, Qt.ItemDataRole) -> Any
15981620
row = index.row()
@@ -1606,6 +1628,32 @@ def data(self, index, role=Qt.DisplayRole):
16061628
return item.vtype.name
16071629
return super().data(index, role)
16081630

1631+
def key(self, index):
1632+
return VariableListModel.effective_name(index)
1633+
1634+
def keyRoles(self): # type: () -> FrozenSet[int]
1635+
return frozenset((Qt.DisplayRole, Qt.EditRole, TransformRole))
1636+
1637+
@staticmethod
1638+
def effective_name(index) -> Optional[str]:
1639+
item = index.data(Qt.EditRole)
1640+
if isinstance(item, DataVectorTypes):
1641+
var = item.vtype
1642+
elif isinstance(item, VariableTypes):
1643+
var = item
1644+
else:
1645+
return None
1646+
tr = index.data(TransformRole)
1647+
return effective_name(var, tr or [])
1648+
1649+
1650+
def effective_name(var: Variable, tr: Sequence[Transform]) -> str:
1651+
name = var.name
1652+
for t in tr:
1653+
if isinstance(t, Rename):
1654+
name = t.name
1655+
return name
1656+
16091657

16101658
class ReinterpretVariableEditor(VariableEditor):
16111659
"""

0 commit comments

Comments
 (0)