From 922f02b292925da6a5920777f6decce40e53c07a Mon Sep 17 00:00:00 2001 From: Agustin Zubiaga Date: Tue, 24 Jan 2012 20:35:14 +0000 Subject: Save as image option, delete value option, toolbar redesign --- diff --git a/activity.py b/activity.py index 77b17c7..f23380c 100644 --- a/activity.py +++ b/activity.py @@ -22,20 +22,34 @@ import gtk import gobject +import os + from sugar.activity import activity from sugar.activity.widgets import ActivityToolbarButton from sugar.activity.widgets import StopButton from sugar.graphics.toolbarbox import ToolbarBox from sugar.graphics.toolbutton import ToolButton +from sugar.datastore import datastore + from pycha.color import basicColors as basic_colors -from charts import Chart +from charts import Chart, CHART_IMAGE COLOR1 = gtk.gdk.Color("#224565") COLOR2 = gtk.gdk.Color("#C0C0C0") COLOR3 = gtk.gdk.Color("#D1E5EC") +ACTIVITY_DIR = os.path.join(activity.get_activity_root(), "data/") +CHART_FILE = os.path.join(ACTIVITY_DIR, "chart-1.png") +num = 0 + +while os.path.exists(CHART_FILE): + num += 1 + CHART_FILE = os.path.join(ACTIVITY_DIR, "chart-" + str(num) + ".png") + +del num + class SimpleGraph(activity.Activity): @@ -60,8 +74,23 @@ class SimpleGraph(activity.Activity): self.toolbarbox = ToolbarBox() self.activity_button = ActivityToolbarButton(self) + activity_btn_toolbar = self.activity_button.page + + save_as_image = ToolButton("image") + save_as_image.connect("clicked", self.save_as_image) + save_as_image.set_tooltip("Save as image") + activity_btn_toolbar.insert(save_as_image, -1) + + save_as_image.show() + activity_btn_toolbar.keep.hide() + self.toolbarbox.toolbar.insert(self.activity_button, 0) + separator = gtk.SeparatorToolItem() + separator.set_draw(False) + separator.set_expand(False) + self.toolbarbox.toolbar.insert(separator, -1) + self.add_v = ToolButton("gtk-add") self.add_v.connect("clicked", self.add_value) self.add_v.set_tooltip("Add a value") @@ -72,11 +101,11 @@ class SimpleGraph(activity.Activity): self.remove_v.connect("clicked", self.remove_value) self.remove_v.set_tooltip("Remove the selected value") - #self.toolbarbox.toolbar.insert(self.remove_v, -1) + self.toolbarbox.toolbar.insert(self.remove_v, -1) separator = gtk.SeparatorToolItem() - separator.set_draw(False) - separator.set_expand(True) + separator.set_draw(True) + separator.set_expand(False) self.toolbarbox.toolbar.insert(separator, -1) self.add_vbar_chart = ToolButton("vbar") @@ -89,21 +118,11 @@ class SimpleGraph(activity.Activity): self.add_hbar_chart.set_tooltip("Create a horizontal bar chart") self.toolbarbox.toolbar.insert(self.add_hbar_chart, -1) - separator = gtk.SeparatorToolItem() - separator.set_draw(True) - separator.set_expand(False) - self.toolbarbox.toolbar.insert(separator, -1) - self.add_line_chart = ToolButton("line") self.add_line_chart.connect("clicked", self.add_chart_cb, "line") self.add_line_chart.set_tooltip("Create a line chart") self.toolbarbox.toolbar.insert(self.add_line_chart, -1) - separator = gtk.SeparatorToolItem() - separator.set_draw(True) - separator.set_expand(False) - self.toolbarbox.toolbar.insert(separator, -1) - self.add_pie_chart = ToolButton("pie") self.add_pie_chart.connect("clicked", self.add_chart_cb, "pie") self.add_pie_chart.set_tooltip("Create a pie chart") @@ -159,7 +178,8 @@ class SimpleGraph(activity.Activity): self.chart_data.append(("Unknown", "0")) def remove_value(self, widget): - self.remove_selected_value() + path = self.labels_and_values.remove_selected_value() + del self.chart_data[path] def add_chart_cb(self, widget, type="vbar"): self.current_chart = Chart(type) @@ -172,15 +192,20 @@ class SimpleGraph(activity.Activity): self.current_chart.set_title(self.chart_title) self.current_chart.set_x_label(self.x_label) self.current_chart.set_y_label(self.y_label) - self.current_chart.set_bg_color(self.bg_color) + if self.bg_color != "#C0C0C0": + self.current_chart.set_bg_color(self.bg_color) + + else: + self.current_chart.set_bg_color(None) self.current_chart.set_color_scheme(color=self.chart_color) self.current_chart.set_line_color(self.chart_line_color) self.current_chart.connect("ready", lambda w, f: - self.charts_area.set_from_file(f)) + self.charts_area.set_from_file(f)) if self.current_chart.type == "pie": self.current_chart.render(self) - else: self.current_chart.render() + else: + self.current_chart.render() def label_changed(self, tw, path, new_label): path = int(path) @@ -188,7 +213,7 @@ class SimpleGraph(activity.Activity): def value_changed(self, tw, path, new_value): path = int(path) - self.chart_data[path] = (self.chart_data[path][0], float(new_value)) + self.chart_data[path] = (self.chart_data[path][0], float(new_value)) def set_h_label(self, options, label): self.x_label = label @@ -208,6 +233,49 @@ class SimpleGraph(activity.Activity): def set_chart_line_color(self, options, color): self.chart_line_color = color + def save_as_image(self, widget): + if self.current_chart: + jobject = datastore.create() + + jobject.metadata['title'] = self.chart_title + jobject.metadata['mime_type'] = "image/png" + + image = open(CHART_IMAGE, "r") + jfile = open(CHART_FILE, "w") + + jfile.write(image.read()) + + jfile.close() + image.close() + + jobject.set_file_path(CHART_FILE) + + datastore.write(jobject) + + def write_file(self, file_path): + if self.current_chart: + jfile = open(file_path, "w") + + jfile.write(self.chart_title + "\n") + jfile.write(self.x_label + "\n") + jfile.write(self.y_label + "\n") + jfile.write(self.bg_color + "\n") + jfile.write(self.chart_color + "\n") + jfile.write(self.chart_line_color + "\n") + jfile.write(self.current_chart.type + "\n") + + string = "" + for item in self.chart_data: + print "Saving: %s : %s" % (item[0], str(item[-1])) + string += item[0] + ":" + str(item[1]) + "," + + jfile.write(string) + + jfile.close() + + def read_file(self, file_path): + print file_path + class TreeView(gtk.TreeView): @@ -252,7 +320,13 @@ class TreeView(gtk.TreeView): print "Added: %s, Value: %s" % (label, value) def remove_selected_value(self): - self.remove(self.get_selection()) + path, column = self.get_cursor() + path = path[0] + + model, iter = self.get_selection().get_selected() + self.model.remove(iter) + + return path def label_changed(self, cell, path, new_text, model): print "Change '%s' to '%s'" % (model[path][0], new_text) @@ -376,6 +450,8 @@ class Options(gtk.VBox): self.pack_start(hbox, False, True, 3) + self.set_size_request(400, 300) + self.show_all() def color_selector(self, widget): @@ -384,24 +460,27 @@ class Options(gtk.VBox): if widget.id == 3: box = gtk.HBox() - + for color in basic_colors.keys(): btn = gtk.Button() btn.set_size_request(40, 40) btn.set_property("has-tooltip", True) btn.set_property("tooltip-text", str(color).capitalize()) btn.color = gtk.gdk.Color(basic_colors[color]) - btn.connect("clicked", lambda w: selector.colorsel.set_current_color(w.color)) + btn.connect("clicked", lambda w: + selector.colorsel.set_current_color(w.color)) btn.modify_bg(gtk.STATE_NORMAL, btn.color) btn.modify_bg(gtk.STATE_PRELIGHT, btn.color) box.pack_start(btn, False, True, 1) - + selector.vbox.pack_end(box, False, True, 0) selector.colorsel.set_current_color(COLOR1) - elif widget.id == 2: selector.colorsel.set_current_color(COLOR3) - - elif widget.id == 1: selector.colorsel.set_current_color(COLOR2) + elif widget.id == 2: + selector.colorsel.set_current_color(COLOR3) + + elif widget.id == 1: + selector.colorsel.set_current_color(COLOR2) selector.get_color_selection().connect("color-changed", self.color_changed, widget) diff --git a/charts.py b/charts.py index cf3d0da..dc31ad5 100644 --- a/charts.py +++ b/charts.py @@ -35,7 +35,7 @@ class Chart(gobject.GObject): __gsignals__ = { 'ready': (gobject.SIGNAL_RUN_FIRST, None, [str])} - def __init__(self, type="vertical", width=800, height=600): + def __init__(self, type="vertical", width=600, height=460): gobject.GObject.__init__(self) self.dataSet = None @@ -112,7 +112,7 @@ class Chart(gobject.GObject): chart = pycha.line.LineChart(self.surface, self.options) elif self.type == "pie": - self.options["legend"] = {"hide" : "False"} + self.options["legend"] = {"hide": "False"} chart = pycha.pie.PieChart(self.surface, self.options) print sg.chart_data self.dataSet = [(data[0], [[0, data[1]]]) for data in sg.chart_data] @@ -122,4 +122,3 @@ class Chart(gobject.GObject): self.surface.write_to_png(CHART_IMAGE) self.emit("ready", CHART_IMAGE) - diff --git a/icons/hbar.svg b/icons/hbar.svg index 1d2c0b9..67815a5 100644 --- a/icons/hbar.svg +++ b/icons/hbar.svg @@ -15,7 +15,7 @@ viewBox="0 0 62 60" id="svg2" xml:space="preserve" - inkscape:version="0.47 r22583" + inkscape:version="0.48.2 r9819" sodipodi:docname="hbar.svg"> \ No newline at end of file diff --git a/icons/line.svg b/icons/line.svg index 1c32b34..30361c0 100755 --- a/icons/line.svg +++ b/icons/line.svg @@ -62,7 +62,7 @@ \ No newline at end of file diff --git a/icons/vbar.svg b/icons/vbar.svg index 0b74b4c..6601d02 100644 --- a/icons/vbar.svg +++ b/icons/vbar.svg @@ -16,7 +16,7 @@ id="svg2" xml:space="preserve" inkscape:version="0.48.2 r9819" - sodipodi:docname="bar.svg"> + style="fill:none;stroke:#ffffff;stroke-width:33.73588181000000219;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" /> \ No newline at end of file diff --git a/pycha/__init__.pyo b/pycha/__init__.pyo index 7e9aa3c..5f9a458 100644 --- a/pycha/__init__.pyo +++ b/pycha/__init__.pyo Binary files differ diff --git a/pycha/bar.pyo b/pycha/bar.pyo index ff60b2e..3c9afea 100644 --- a/pycha/bar.pyo +++ b/pycha/bar.pyo Binary files differ diff --git a/pycha/chart.pyo b/pycha/chart.pyo index 60be0fc..f6c5b2b 100644 --- a/pycha/chart.pyo +++ b/pycha/chart.pyo Binary files differ diff --git a/pycha/color.pyo b/pycha/color.pyo index 2914c8c..78225ea 100644 --- a/pycha/color.pyo +++ b/pycha/color.pyo Binary files differ diff --git a/pycha/pie.pyo b/pycha/pie.pyo index 77daa2a..6f7c2be 100644 --- a/pycha/pie.pyo +++ b/pycha/pie.pyo Binary files differ diff --git a/pycha/utils.pyo b/pycha/utils.pyo index 84606ff..6a9a614 100644 --- a/pycha/utils.pyo +++ b/pycha/utils.pyo Binary files differ -- cgit v0.9.1