Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
moves most of the historyview code to the glade file.
Browse files Browse the repository at this point in the history
related and preparing #251.
  • Loading branch information
mfrasca committed Dec 31, 2015
1 parent ef2cd20 commit b205d3c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 52 deletions.
91 changes: 91 additions & 0 deletions bauble/bauble.glade
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,97 @@
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy toplevel-contextual -->
<object class="GtkListStore" id="history_ls">
<columns>
<!-- column-name timestamp -->
<column type="gchararray"/>
<!-- column-name operation -->
<column type="gchararray"/>
<!-- column-name user -->
<column type="gchararray"/>
<!-- column-name table -->
<column type="gchararray"/>
<!-- column-name user_friendly -->
<column type="gchararray"/>
<!-- column-name dict -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="history_window">
<property name="can_focus">False</property>
<child>
<object class="GtkScrolledWindow" id="history_sv">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<object class="GtkTreeView" id="history_tv">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">history_ls</property>
<property name="reorderable">True</property>
<property name="rules_hint">True</property>
<child>
<object class="GtkTreeViewColumn" id="history_treeviewcolumn0">
<property name="title" translatable="yes">Timestamp</property>
<child>
<object class="GtkCellRendererText" id="history_cellrenderertext0"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="history_treeviewcolumn1">
<property name="title" translatable="yes">Operation</property>
<child>
<object class="GtkCellRendererText" id="history_cellrenderertext1"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="history_treeviewcolumn2">
<property name="title" translatable="yes">User</property>
<child>
<object class="GtkCellRendererText" id="history_cellrenderertext2"/>
<attributes>
<attribute name="text">2</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="history_treeviewcolumn3">
<property name="title" translatable="yes">Table</property>
<child>
<object class="GtkCellRendererText" id="history_cellrenderertext3"/>
<attributes>
<attribute name="text">3</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="history_treeviewcolumn4">
<property name="title" translatable="yes">Values</property>
<child>
<object class="GtkCellRendererText" id="history_cellrenderertext4"/>
<attributes>
<attribute name="text">4</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<object class="GtkWindow" id="main_window">
<property name="can_focus">True</property>
<property name="title">Bauble</property>
Expand Down
87 changes: 35 additions & 52 deletions bauble/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,81 +1260,64 @@ def attached_to(cls, obj):
return []


class StringColumn(gtk.TreeViewColumn):

"""
A generic StringColumn for use in a gtk.TreeView.
This code partially based on the StringColumn from the Quidgets
project (http://launchpad.net/quidgets)
"""
def __init__(self, title, format_func=None, **kwargs):
self.renderer = gtk.CellRendererText()
super(StringColumn, self).__init__(title, self.renderer, **kwargs)
self.renderer.set_property('ellipsize', pango.ELLIPSIZE_END)
if format_func:
self.set_cell_data_func(self.renderer, self.cell_data_func,
format_func)

def cell_data_func(self, column, cell, model, treeiter, format):
value = format(model[treeiter])
cell.set_property('text', value)


class HistoryView(pluginmgr.View):
"""Show the tables row in the order they were last updated
"""

def __init__(self):
super(HistoryView, self).__init__()
self.init_gui()

def init_gui(self):
self.treeview = gtk.TreeView()
#self.treeview.set_fixed_height_mode(True)
columns = [(_('Timestamp'), 0), (_('Operation'), 1),
(_('User'), 2), (_('Table'), 3), (_('Values'), 4)]
for name, index in columns:
column = StringColumn(name, text=index)
column.set_sort_column_id(index)
column.set_expand(False)
column.props.sizing = gtk.TREE_VIEW_COLUMN_AUTOSIZE
column.set_resizable(True)
column.renderer.set_fixed_height_from_font(1)
self.treeview.append_column(column)
sw = gtk.ScrolledWindow()
sw.add(self.treeview)
self.pack_start(sw)

def populate_history(self, arg):
logger.debug('PrefsView::__init__')
filename = os.path.join(paths.lib_dir(), 'bauble.glade')
from bauble import utils, editor
self.widgets = utils.load_widgets(filename)
self.view = editor.GenericEditorView(
filename, root_widget_name='history_window')
super(HistoryView, self
).__init__(root_widget=self.view.widgets.history_sv)
self.view.connect_signals(self)
self.liststore = self.view.widgets.history_ls
self.update()

def update(self):
"""
Add the history items to the view.
"""
session = db.Session()
utils.clear_model(self.treeview)
model = gtk.ListStore(str, str, str, str, str)
self.liststore.clear()
for item in session.query(db.History).\
order_by(db.History.timestamp.desc()).all():
model.append([item.timestamp, item.operation, item.user,
item.table_name, item.values])
self.treeview.set_model(model)
d = eval(item.values)
del d['_created']
del d['_last_updated']
friendly = ', '.join(u"%s: %s" % (k, self.show_typed_value(v))
for k, v in sorted(d.items())
)
self.liststore.append([item.timestamp, item.operation, item.user,
item.table_name, friendly, item.values])
session.close()

@staticmethod
def show_typed_value(v):
try:
eval(v)
return v
except:
return u"»%s«" % v

class HistoryCommandHandler(pluginmgr.CommandHandler):

command = 'history'
view = None

def __init__(self):
super(HistoryCommandHandler, self).__init__()
self.view = None

command = 'history'

def get_view(self):
if not self.view:
self.view = HistoryView()
self.__class__.view = HistoryView()
return self.view

def __call__(self, cmd, arg):
self.view.populate_history(arg)
self.view.update()


pluginmgr.register_command(HistoryCommandHandler)
Expand Down

0 comments on commit b205d3c

Please sign in to comment.