Skip to content
chart_view.py 6.06 KiB
Newer Older
import gtk

class ChartView(gtk.HPaned):

    def create_new_chart(self, field, allocation):
        import tempfile
        substrings = []
        strings = []
        if field is not None:
            for r in self.results:
                strings.append(r[field])
        for sub in self.treeViewChartValues.store:
            if not sub[0] == self.treeViewChartValues.inital_value:
                substrings.append(sub[0])
        data = chart.count_substrings(substrings, strings)
        with tempfile.NamedTemporaryFile(delete=True) as file:
            chart.create(file,data,(allocation.width,allocation.height),len(self.results), len(self.treeViewChartValues.store)-1)
            file.flush()
            self.image.set_from_file(file.name)

    def update_chart(self, data,root_type):
        self.combobox.show_data(root_type, data)
        self.combobox.set_active(0)
        type_ = self.combobox.get_selected_item()
        self.create_new_chart(type_,self.image.get_allocation())

    def __init__(self, *args, **kwargs):
        super(ChartView, self).__init__(*args, **kwargs)

Franziska Koehn's avatar
Franziska Koehn committed
        self.set_position(220)

        v_box = gtk.VBox()
        self.add1(v_box)

        sw_hist_values = gtk.ScrolledWindow()
        sw_hist_values.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw_hist_values.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
Franziska Koehn's avatar
Franziska Koehn committed
        v_box.pack_start(sw_hist_values, True, True, 0)
            selected_type = self.combobox.get_selected_item()
            self.create_new_chart(selected_type, self.image.get_allocation())

        self.treeViewChartValues = treeViewChartValues()
        self.treeViewChartValues.connect("values-changed", callback_columns_changed)
        sw_hist_values.add(self.treeViewChartValues)

Franziska Koehn's avatar
Franziska Koehn committed
        hBox_type = gtk.HBox()
        v_box.pack_start(hBox_type, False, True, 0)

        label_type = gtk.Label()
        label_type.set_text("Type: ")
        hBox_type.pack_start(label_type, False, True, 0)

        def changed_cb(combobox):
            selected_type =  combobox.get_selected_item()
            self.create_new_chart(selected_type, self.image.get_allocation())

        self.combobox = ComboBoxRootType()
        self.combobox.connect('changed', changed_cb)
        cell = gtk.CellRendererText()
        self.combobox.pack_start(cell)
        self.combobox.add_attribute(cell, 'text', 0)
        hBox_type.pack_start(self.combobox, True, True,0)
Franziska Koehn's avatar
Franziska Koehn committed

        # chart

        frame_image = gtk.Frame()
        self.add2(frame_image)
        frame_image.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        self.image = gtk.Image()
        frame_image.add(self.image)

        from pydash import throttle

        def callback_char_size_allocate(widget, allocation, *_):
            if callback_char_size_allocate.old_allocation.width != allocation.width or callback_char_size_allocate.old_allocation.height != allocation.height:
                selected_type =  self.combobox.get_selected_item()
                self.create_new_chart(selected_type, allocation)
                callback_char_size_allocate.old_allocation = allocation

        callback_char_size_allocate = throttle(callback_char_size_allocate, 50)
        callback_char_size_allocate.old_allocation = gtk.gdk.Rectangle()

        self.image.connect("size-allocate",callback_char_size_allocate)

class ComboBoxRootType(gtk.ComboBox):

    store = []

    def __init__(self, *args, **kwargs):
        super(ComboBoxRootType, self).__init__(*args, **kwargs)
        self.store = gtk.ListStore(str, str)
        self.set_model(self.store)


    def get_selected_item(self):
        model = self.get_model()
        index = self.get_active()
        if index > -1:
            return  model[index][1] # return key
        else:
            return None

    def show_data(self, root_type, data):
        import xsalogic.datatypereader as type_reader
        self.store.clear()
        for key in data.headers():
            label = type_reader.get_field_label(root_type, key)
            self.store.append([label, key])


class treeViewChartValues(gtk.TreeView):

    inital_value = "..."
    tooltip = 'Delete by using right click'

    def __init__(self):
        super(treeViewChartValues, self).__init__()
        self.create_model()
        self.create_column()

        import gobject
        gobject.signal_new("values-changed", treeViewChartValues, gobject.SIGNAL_RUN_FIRST,
        gobject.TYPE_NONE, ())


        def on_treeview_button_press_event(treeview, event):
            if event.button == 3:
                x = int(event.x)
                y = int(event.y)
                path_info = treeview.get_path_at_pos(x, y)
                if path_info is not None:
                    path, col, cellx, celly = path_info
                    iter = self.store.get_iter(path)
                    if self.store.get_value(iter,0) != self.inital_value:
                        self.store.remove(iter)

        self.connect('button_press_event', on_treeview_button_press_event)
        self.set_tooltip_column(1)


    def create_column(self):

        def cell_edited_callback(cellrenderertext, path_string, new_text, *_):
            it = self.store.get_iter_from_string(path_string)
            it_last = self.store.get_iter(len(self.store)-1)
            self.store.set(it, 0, new_text)
            if not (it_last is None) and (self.store.get_value(it_last, 0) != self.inital_value):
                self.store.append([self.inital_value, self.tooltip])


        rendererText = gtk.CellRendererText()
        rendererText.set_property('editable', True)
        rendererText.connect('edited', cell_edited_callback, 2)

Franziska Koehn's avatar
Franziska Koehn committed
        column = gtk.TreeViewColumn("Chart Values (Type Substrings)", rendererText, text=0)
        column.set_sort_column_id(0)
        self.append_column(column)

    def create_model(self):
        self.store = gtk.ListStore(str, str)
        self.store.append([self.inital_value, self.tooltip])
        self.set_model(self.store)