|
1 | 1 | import os |
2 | 2 | from itertools import chain |
3 | 3 | import json |
| 4 | +from typing import List |
4 | 5 |
|
5 | 6 | import numpy as np |
6 | 7 |
|
|
10 | 11 | from AnyQt.QtWidgets import QHeaderView, QColorDialog, QTableView, QComboBox, \ |
11 | 12 | QFileDialog, QMessageBox |
12 | 13 |
|
13 | | -from orangewidget.settings import IncompatibleContext |
| 14 | +from orangewidget.settings import IncompatibleContext, TypeSupport |
14 | 15 |
|
15 | 16 | import Orange |
16 | 17 | from Orange.preprocess.transformation import Identity |
@@ -232,6 +233,48 @@ def from_dict(cls, var, data): |
232 | 233 | return obj, warnings |
233 | 234 |
|
234 | 235 |
|
| 236 | +class AttrDescTypeSupport(TypeSupport): |
| 237 | + @classmethod |
| 238 | + def pack_value(cls, value, _=None): |
| 239 | + packed = {k: list(tuple(x) for x in v) if k == "new_colors" else v |
| 240 | + for k, v in value.__dict__.items() |
| 241 | + if k.startswith("new_") and v is not None} |
| 242 | + packed["var"] = value.var.name |
| 243 | + return packed |
| 244 | + |
| 245 | + @classmethod |
| 246 | + def unpack_value(cls, value, tp, domain, *ctx_arg): |
| 247 | + var = domain[value["var"]] |
| 248 | + desc = cls.supported_types[0](var) |
| 249 | + if "new_name" in value: |
| 250 | + desc.name = value["new_name"] |
| 251 | + return desc |
| 252 | + |
| 253 | + |
| 254 | +class DiscAttrDescTypeSupport(AttrDescTypeSupport): |
| 255 | + supported_types = (DiscAttrDesc, ) |
| 256 | + |
| 257 | + @classmethod |
| 258 | + def unpack_value(cls, value, tp, domain, *ctx_args): |
| 259 | + desc = super().unpack_value(value, tp, domain) |
| 260 | + for i, color in enumerate(value.get("new_colors", ())): |
| 261 | + desc.set_color(i, color) |
| 262 | + for i, color in enumerate(value.get("new_values", ())): |
| 263 | + desc.set_value(i, color) |
| 264 | + return desc |
| 265 | + |
| 266 | + |
| 267 | +class ContAttrDescTypeSupport(AttrDescTypeSupport): |
| 268 | + supported_types = (ContAttrDesc, ) |
| 269 | + |
| 270 | + @classmethod |
| 271 | + def unpack_value(cls, value, tp, domain, *ctx_args): |
| 272 | + desc = super().unpack_value(value, tp, domain) |
| 273 | + if "new_palette_name" in value: |
| 274 | + desc.palette_name = value["new_palette_name"] |
| 275 | + return desc |
| 276 | + |
| 277 | + |
235 | 278 | class ColorTableModel(QAbstractTableModel): |
236 | 279 | """ |
237 | 280 | Base color model for discrete and continuous variables. The model handles: |
@@ -547,12 +590,12 @@ class Outputs: |
547 | 590 |
|
548 | 591 | settingsHandler = settings.PerfectDomainContextHandler( |
549 | 592 | match_values=settings.PerfectDomainContextHandler.MATCH_VALUES_ALL) |
550 | | - disc_descs = settings.ContextSetting([]) |
551 | | - cont_descs = settings.ContextSetting([]) |
| 593 | + disc_descs: List[DiscAttrDesc] = settings.ContextSetting([]) |
| 594 | + cont_descs: List[ContAttrDesc] = settings.ContextSetting([]) |
552 | 595 | selected_schema_index = settings.Setting(0) |
553 | 596 | auto_apply = settings.Setting(True) |
554 | 597 |
|
555 | | - settings_version = 2 |
| 598 | + settings_version = 3 |
556 | 599 |
|
557 | 600 | want_main_area = False |
558 | 601 |
|
@@ -804,9 +847,17 @@ def was(n, o): |
804 | 847 | self.report_raw(f"<table>{table}</table>") |
805 | 848 |
|
806 | 849 | @classmethod |
807 | | - def migrate_context(cls, _, version): |
| 850 | + def migrate_context(cls, context, version): |
808 | 851 | if not version or version < 2: |
809 | 852 | raise IncompatibleContext |
| 853 | + if version < 3: |
| 854 | + values = context.values |
| 855 | + values["disc_descs"] = [ |
| 856 | + DiscAttrDescTypeSupport.pack_value(desc) |
| 857 | + for desc in values["disc_descs"]] |
| 858 | + values["cont_descs"] = [ |
| 859 | + ContAttrDescTypeSupport.pack_value(desc) |
| 860 | + for desc in values["cont_descs"]] |
810 | 861 |
|
811 | 862 |
|
812 | 863 | if __name__ == "__main__": # pragma: no cover |
|
0 commit comments