使用 JSON 序列化 Gtk TreeStore / ListStore

Posted

技术标签:

【中文标题】使用 JSON 序列化 Gtk TreeStore / ListStore【英文标题】:Serialize Gtk TreeStore / ListStore using JSON 【发布时间】:2015-07-04 19:47:45 【问题描述】:

我做了一个新的例子,它更好地展示了我正在尝试做的事情。新示例给出以下输出。有没有办法让数据进入相应的存储键( 括号)?


    "copy": [
        [
            [
                5.0,
                8.0,
                9.0
            ]
        ],
        [
            [
                4.0,
                0.0,
                1.0
            ]
        ]
    ],
    "name": "dataset1",
    "sets": [
        
            "store": ,
            "type": "vector"
        ,
        
            "store": ,
            "type": "vector"
        
    ]

新示例

from gi.repository import Gtk
import json
import random

class Vector(object):

    def __init__(self, data):
        self.store = Gtk.ListStore(float, float, float)
        self.store.append([data[0], data[1], data[2]])
        self.type = "vector"

    def return_data(self):
        store_data = []

        def iterate_over_data(model, path, itr):
            row = model[path]
            store_data.append([row[0], row[1], row[2]])

        self.store.foreach(iterate_over_data)

        return store_data

class DataSet(object):

    def __init__(self, name):
        self.name = name
        self.sets = []

    def add_vector(self):
        data = [random.randint(0,9) for x in range(3)]
        self.sets.append(Vector(data))

    def to_json(self):
        self.copy = []
        for s in self.sets:
            self.copy.append(s.return_data())

        return json.dumps(self, default=lambda o: o.__dict__, 
            sort_keys=True, indent=4)

obj1 = DataSet("dataset1")
for x in range(2):
    obj1.add_vector()

print(obj1.to_json())

老例子

我目前正在研究如何序列化嵌套在 Gtk TreeStore 中的 Gtk ListStore。我有一个小例子可以工作,但不确定这种方法是否适用于附加更多数据的程序(例如,图层对象可以保存颜色或创建日期)。有没有其他方法可以解决这个问题?

我目前的方法是自己收集列表和字典形式的数据,然后创建 JSON 转储。我觉得如果我需要为每个图层对象附加 25 个值,这将很难维护。

from gi.repository import Gtk, Gdk
import json
import random


class LayerTreeView(Gtk.TreeView):

    def __init__(self, store):
        Gtk.TreeView.__init__(self, store)
        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn("Name", renderer, text=0)
        self.append_column(column)


class DataTreeView(Gtk.TreeView):

    def __init__(self, store):
        Gtk.TreeView.__init__(self, store)
        self.store = store
        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn("Data", renderer, text=0)
        self.append_column(column)


class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="TreeView Serialize")
        self.connect("delete-event", Gtk.main_quit)
        self.set_border_width(10)
        self.set_default_size(400, 300)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6, expand=True)
        self.add(vbox)

        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        button = Gtk.Button("Cut")
        button.connect("clicked", self.on_cut_clicked)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button(stock=Gtk.STOCK_COPY)
        button.connect("clicked", self.on_copy_clicked)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button(stock=Gtk.STOCK_PASTE)
        button.connect("clicked", self.on_paste_clicked)
        hbox.pack_start(button, True, True, 0)
        vbox.add(hbox)

        self.layer_store = Gtk.TreeStore(str, object, object)
        self.layer_view = LayerTreeView(self.layer_store)

        self.layer_sw = Gtk.ScrolledWindow()
        self.data_sw = Gtk.ScrolledWindow()
        self.layer_sw.add(self.layer_view)

        treebox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6, expand=True)
        treebox.pack_start(self.layer_sw, True, True, 0)
        treebox.pack_start(self.data_sw, True, True, 0)
        vbox.add(treebox)

        self.select = self.layer_view.get_selection()
        self.select.connect("changed", self.on_selection_changed)

        self.add_test_data()

    def add_test_data(self):
        for x in range(3):
            data_store = Gtk.ListStore(str)
            data_view = DataTreeView(data_store)
            for y in range(5):
                data_store.append([str(y+x)])
            self.layer_store.append(None, ["Data ".format(x), data_store, data_view])

    def on_selection_changed(self, selection):
        """
        When layer is switched load respective data
        """
        model, treeiter = selection.get_selected()
        if treeiter != None:
            data_view = model[treeiter][2]
            child = self.data_sw.get_child()
            if child != None:
                self.data_sw.remove(self.data_sw.get_child())
            self.data_sw.add(data_view)
            self.show_all()

    def on_cut_clicked(self, button):
        pass

    def on_copy_clicked(self, button):
        copy_list = ["safe-to-paste"]
        data_dict = 
        for row in self.layer_store:
            name = row[0]
            data_obj = row[1]
            value_list = []
            for datarow in data_obj:
                value = datarow[0]
                value_list.append(value)
            data_dict[name] = value_list

        copy_list.append(data_dict)
        data = json.dumps(copy_list)
        self.clipboard.set_text(data, -1)

    def on_paste_clicked(self, button):
        paste_str = self.clipboard.wait_for_text()
        try:
            parse = json.loads(paste_str)
            json_str = True
        except:
            json_str = False

        if json_str is False:
            return

        keyword = parse[0]
        if keyword != "safe-to-paste":
            return

        data_dict = parse[1]
        for x in data_dict:
            data_list = data_dict[x]
            data_store = Gtk.ListStore(str)
            data_view = DataTreeView(data_store)
            for y in data_list:
                data_store.append([str(y)])
            self.layer_store.append(None, [x, data_store, data_view])

win = MainWindow()
win.show_all()
Gtk.main()

【问题讨论】:

您是否将剪贴板和 json 用于其他用途?否则,可能有更简单的方法。 我也想用这个来加载/保存和拖放。但我刚开始研究这个,不知道有更好的解决方案。 【参考方案1】:

我有一个改进的代码版本,带有 dict 理解和 @staticmethod,它使信号回调更具可读性和更短。尽管如此,这并不能真正解决您的问题,因为它仍然手动生成 json。如果 ListStore 变得更复杂,最好让DataListStore 类使用相应的方法生成自己的 json。

from gi.repository import Gtk, Gdk
import json


class LayerTreeView(Gtk.TreeView):

    def __init__(self, store):
        Gtk.TreeView.__init__(self, store)
        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn("Name", renderer, text=0)
        self.append_column(column)


class DataTreeView(Gtk.TreeView):

    def __init__(self):
        Gtk.TreeView.__init__(self)
        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn("Data", renderer, text=0)
        self.append_column(column)

class DataListStore(Gtk.ListStore):

    @staticmethod
    def from_json(*args, values=[]):
        store = DataListStore(*args)
        for value in values:
            store.append((value,))
        return store


class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="TreeView Serialize")
        self.connect("delete-event", Gtk.main_quit)
        self.set_border_width(10)
        self.set_default_size(400, 300)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6, expand=True)
        self.add(vbox)

        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        button = Gtk.Button("Cut")
        button.connect("clicked", self.on_cut_clicked)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button(stock=Gtk.STOCK_COPY)
        button.connect("clicked", self.on_copy_clicked)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button(stock=Gtk.STOCK_PASTE)
        button.connect("clicked", self.on_paste_clicked)
        hbox.pack_start(button, True, True, 0)
        vbox.add(hbox)

        self.layer_store = Gtk.TreeStore(str, object)
        self.layer_view = LayerTreeView(self.layer_store)
        self.data_view = DataTreeView()

        layer_sw = Gtk.ScrolledWindow()
        layer_sw.add(self.layer_view)
        data_sw = Gtk.ScrolledWindow()
        data_sw.add(self.data_view)

        treebox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6, expand=True)
        treebox.pack_start(layer_sw, True, True, 0)
        treebox.pack_start(data_sw, True, True, 0)
        vbox.add(treebox)

        select = self.layer_view.get_selection()
        select.connect("changed", self.on_selection_changed)

        self.add_test_data()

    def add_test_data(self):
        for x in range(3):
            data_list = [str(y+x) for y in range(5)]
            self.layer_store.append(None, ["Data ".format(x), data_list])

    def on_selection_changed(self, selection):
        """
        When layer is switched load respective data
        """
        model, treeiter = selection.get_selected()
        if treeiter != None:
            self.data_view.set_model(
                DataListStore.from_json(str, values=model[treeiter][1])
            )

    def on_cut_clicked(self, button):
        pass

    def on_copy_clicked(self, button):
        copy_list = [
            'safe-to-paste',
            row[0]: row[1] for row in self.layer_store,
        ]
        data = json.dumps(copy_list)
        self.clipboard.set_text(data, -1)

    def on_paste_clicked(self, button):
        paste_str = self.clipboard.wait_for_text()
        try:
            parse = json.loads(paste_str)
        except:
            return

        if parse[0] != "safe-to-paste":
            return

        data_dict = parse[1]
        for x in data_dict:
            self.layer_store.append(None, [x, data_dict[x]])

win = MainWindow()
win.show_all()
Gtk.main()

【讨论】:

真的很不错的改进。您认为将数据存储在字典中并从该字典重新填充列表存储会更好吗?我在想,使用原生 Python 数据类型可能比教序列化如何处理 Gtk Liststore 更容易? 如果你想将数据保存到文件中,json 会让事情变得更容易,但现在它不是很有用。

以上是关于使用 JSON 序列化 Gtk TreeStore / ListStore的主要内容,如果未能解决你的问题,请参考以下文章

使用 TreeStore 和 TreePanel 更正 JSON 和模型

使用具有不同子字段的 JSON 加载 TreeStore

将自定义 JSON 解析为 ExtJS 4.1 TreeStore

ExtJS 4.1 TreeStore 中的不同节点类型

EXTJS 4.1 如何在不重新加载 TreeStore 的情况下删除过滤器

GXT 在 TreeStore 中重新排列数据数组索引