Skip to content

Commit

Permalink
Color: Annotatedsettings
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Mar 12, 2021
1 parent c2fc3da commit c1f0db7
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions Orange/widgets/data/owcolor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from itertools import chain
import json
from typing import List

import numpy as np

Expand All @@ -10,7 +11,7 @@
from AnyQt.QtWidgets import QHeaderView, QColorDialog, QTableView, QComboBox, \
QFileDialog, QMessageBox

from orangewidget.settings import IncompatibleContext
from orangewidget.settings import IncompatibleContext, TypeSupport

import Orange
from Orange.preprocess.transformation import Identity
Expand Down Expand Up @@ -232,6 +233,48 @@ def from_dict(cls, var, data):
return obj, warnings


class AttrDescTypeSupport(TypeSupport):
@classmethod
def pack_value(cls, value, _=None):
packed = {k: list(tuple(x) for x in v) if k == "new_colors" else v
for k, v in value.__dict__.items()
if k.startswith("new_") and v is not None}
packed["var"] = value.var.name
return packed

@classmethod
def unpack_value(cls, value, _, domain, *_a):
var = domain[value["var"]]
desc = cls.supported_types[0](var)
if "new_name" in value:
desc.name = value["new_name"]
return desc


class DiscAttrDescTypeSupport(AttrDescTypeSupport):
supported_types = (DiscAttrDesc, )

@classmethod
def unpack_value(cls, value, tp, domain, *_):
desc = super().unpack_value(value, tp, domain)
for i, color in enumerate(value.get("new_colors", ())):
desc.set_color(i, color)
for i, color in enumerate(value.get("new_values", ())):
desc.set_value(i, color)
return desc


class ContAttrDescTypeSupport(AttrDescTypeSupport):
supported_types = (ContAttrDesc, )

@classmethod
def unpack_value(cls, value, tp, domain, *_):
desc = super().unpack_value(value, tp, domain)
if "new_palette_name" in value:
desc.palette_name = value["new_palette_name"]
return desc


class ColorTableModel(QAbstractTableModel):
"""
Base color model for discrete and continuous variables. The model handles:
Expand Down Expand Up @@ -547,12 +590,12 @@ class Outputs:

settingsHandler = settings.PerfectDomainContextHandler(
match_values=settings.PerfectDomainContextHandler.MATCH_VALUES_ALL)
disc_descs = settings.ContextSetting([])
cont_descs = settings.ContextSetting([])
disc_descs: List[DiscAttrDesc] = settings.ContextSetting([])
cont_descs: List[ContAttrDesc] = settings.ContextSetting([])
selected_schema_index = settings.Setting(0)
auto_apply = settings.Setting(True)

settings_version = 2
settings_version = 3

want_main_area = False

Expand Down Expand Up @@ -801,9 +844,17 @@ def was(n, o):
self.report_raw(f"<table>{table}</table>")

@classmethod
def migrate_context(cls, _, version):
def migrate_context(cls, context, version):
if not version or version < 2:
raise IncompatibleContext
if version < 3:
values = context.values
values["disc_descs"] = [
DiscAttrDescTypeSupport.pack_value(desc)
for desc in values["disc_descs"]]
values["cont_descs"] = [
ContAttrDescTypeSupport.pack_value(desc)
for desc in values["cont_descs"]]


if __name__ == "__main__": # pragma: no cover
Expand Down

0 comments on commit c1f0db7

Please sign in to comment.