Skip to content

Commit

Permalink
oweditdomain: Indicate variables in error state
Browse files Browse the repository at this point in the history
(have duplicated names)
  • Loading branch information
ales-erjavec committed Dec 10, 2021
1 parent 0515af8 commit bce90fc
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions Orange/widgets/data/oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
QStyledItemDelegate, QStyleOptionViewItem, QStyle, QSizePolicy,
QDialogButtonBox, QPushButton, QCheckBox, QComboBox, QStackedLayout,
QDialog, QRadioButton, QGridLayout, QLabel, QSpinBox, QDoubleSpinBox,
QAbstractItemView, QMenu
QAbstractItemView, QMenu, QToolTip
)
from AnyQt.QtGui import (
QStandardItemModel, QStandardItem, QKeySequence, QIcon, QBrush, QPalette,
QHelpEvent
)
from AnyQt.QtGui import QStandardItemModel, QStandardItem, QKeySequence, QIcon
from AnyQt.QtCore import (
Qt, QSize, QModelIndex, QAbstractItemModel, QPersistentModelIndex, QRect,
QPoint,
Expand Down Expand Up @@ -1588,11 +1591,30 @@ def initStyleOption(self, option, index):
# mark as changed (maybe also change color, add text, ...)
option.font.setItalic(True)

multiplicity = index.data(MultiplicityRole)
if isinstance(multiplicity, int) and multiplicity > 1:
option.palette.setBrush(QPalette.Text, QBrush(Qt.red))
option.palette.setBrush(QPalette.HighlightedText, QBrush(Qt.red))

def helpEvent(self, event: QHelpEvent, view: QAbstractItemView,
option: QStyleOptionViewItem, index: QModelIndex) -> bool:
multiplicity = index.data(MultiplicityRole)
if isinstance(multiplicity, int) and multiplicity > 1:
name = VariableListModel.effective_name(index)
QToolTip.showText(
event.globalPos(),
self.tr("Name `{name}` is duplicated".format(name=name)),
view.viewport()
)
return True
else:
return super().helpEvent(event, view, option, index)


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

def key(self, index):
return VariableListModel.effective_name(index)

def keyRoles(self): # type: () -> FrozenSet[int]
return frozenset((Qt.DisplayRole, Qt.EditRole, TransformRole))

@staticmethod
def effective_name(index) -> Optional[str]:
item = index.data(Qt.EditRole)
if isinstance(item, DataVectorTypes):
var = item.vtype
elif isinstance(item, VariableTypes):
var = item
else:
return None
tr = index.data(TransformRole)
if var is not None:
return effective_name(var, tr or [])
else:
return None


def effective_name(var: Variable, tr: Sequence[Transform]) -> str:
name = var.name
for t in tr:
if isinstance(t, Rename):
name = t.name
return name


class ReinterpretVariableEditor(VariableEditor):
"""
Expand Down

0 comments on commit bce90fc

Please sign in to comment.