import gtk
import gtk.glade

__all__ = ["ui_tree", "window_main", "store_plot", "tree_plot",
    "draw_graph", "status_main", "trace"]

def police_graphs(model, path, row_iter):
    row = model[path]
    if row.next is not None:
        if row[0] == "":
            model.remove(row_iter)
    elif row[0] != "":
        model.append(None, ["", False])

def onToggle(toggle, path, model):
	model[path][1] = not model[path][1]

def onEdited(cell, path, text, model):
	model[path][0] = text
	model[path][1] = True
	
	# Auto create/delete functions as needed
	model.foreach(police_graphs)

glade_ui = "ui.glade"
ui_tree = gtk.glade.XML(glade_ui)

signals = {
    "gtk_main_quit": gtk.main_quit,
}

ui_tree.signal_autoconnect(signals)

# function, draw, line color, line width, line type
store_plot = gtk.TreeStore(str, bool)
store_plot.append(None, ["((x**3 + 2*x**2 + 3*x + 4) / x) / 8", True])
store_plot.append(None, ["(x**2 + 2*x + 3) / 8", True])
store_plot.append(None, ["", False])

# Setup columns
# COLUMN str -- Function
col_function = gtk.TreeViewColumn("Function")
col_function_cell = gtk.CellRendererText()
col_function_cell.set_property("editable", True)
col_function_cell.connect("edited", onEdited, store_plot)
col_function.pack_start(col_function_cell)
col_function.add_attribute(col_function_cell, "text", 0)

# COLUMN bool -- Plot the function?
col_draw = gtk.TreeViewColumn()
col_draw_cell = gtk.CellRendererToggle()
col_draw_cell.set_property("activatable", True)
col_draw_cell.connect("toggled", onToggle, store_plot)
col_draw.pack_start(col_draw_cell)
col_draw.add_attribute(col_draw_cell, "active", 1)

# COLUMN gtk.gdk.Color -- Line color
# TODO: line color

# COLUMN int -- Line width
# TODO: line width

# COLUMN int -- Line style
# TODO: line style

tree_plot = ui_tree.get_widget("tree_plot")
tree_plot.set_model(store_plot)
tree_plot.append_column(col_draw)
tree_plot.append_column(col_function)

draw_graph = ui_tree.get_widget("draw_graph")
window_main = ui_tree.get_widget("window_main")
status_main = ui_tree.get_widget("status_main")

# Prepare graph resolution
xmin = ui_tree.get_widget("spinner_graphwindow_x_min")
xmax = ui_tree.get_widget("spinner_graphwindow_x_max")
ymin = ui_tree.get_widget("spinner_graphwindow_y_min")
ymax = ui_tree.get_widget("spinner_graphwindow_y_max")

def trace():
    x_value = ui_tree.get_widget("spin_trace").get_value()
    b_trace = ui_tree.get_widget("chk_trace").get_active()
    return (b_trace, x_value)

def resolution():
    return (xmin.get_value(), xmax.get_value(),
        ymin.get_value(), ymax.get_value())

# Set default windowing values
xmin.set_value(-5.0)
xmax.set_value(5.0)
ymin.set_value(-5.0)
ymax.set_value(5.0)

@@ -338,24 +338,30 @@
 , %5B%22%22, False
+, None
 %5D)%0A%0Adef onTo
@@ -752,41 +752,13 @@
 aw, 
-line color, line width, line type
+error
 %0Asto
@@ -790,16 +790,32 @@
 tr, bool
+, gtk.gdk.Pixbuf
 )%0Astore_
@@ -868,32 +868,38 @@
  / x) / 8%22, True
+, None
 %5D)%0Astore_plot.ap
@@ -938,16 +938,22 @@
 8%22, True
+, None
 %5D)%0Astore
@@ -981,16 +981,22 @@
 %22, False
+, None
 %5D)%0A%0A# Se
@@ -1078,16 +1078,114 @@
 ction%22)%0A
+%0Acol_function_err_cell = gtk.CellRendererPixbuf()%0Acol_function.pack_start(col_function_err_cell)%0A%0A
 col_func
@@ -1365,24 +1365,25 @@
 ction_cell)%0A
+%0A
 col_function
@@ -1426,16 +1426,79 @@
 ext%22, 0)
+%0Acol_function.add_attribute(col_function_err_cell, %22pixbuf%22, 2)
 %0A%0A# COLU
@@ -1794,159 +1794,8 @@
 1)%0A%0A
-# COLUMN gtk.gdk.Color -- Line color%0A# TODO: line color%0A%0A# COLUMN int -- Line width%0A# TODO: line width%0A%0A# COLUMN int -- Line style%0A# TODO: line style%0A%0A
 tree

Add error column