Skip to content

Commit

Permalink
TableEditor column with progress bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed May 19, 2016
1 parent 536ff2c commit eb161e3
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/demo/Advanced/Table_editor_with_progress_column.py
@@ -0,0 +1,56 @@
import random

from pyface.api import GUI
from traits.api import Button, HasTraits, Instance, Int, List, Str
from traitsui.api import ObjectColumn, TableEditor, UItem, View
from traitsui.extras.progress_column import ProgressColumn


class Job(HasTraits):

name = Str

percent_complete = Int


class JobManager(HasTraits):

jobs = List(Instance(Job))

start = Button

def populate(self):
self.jobs = [
Job(name='job %02d' % i, percent_complete=0)
for i in range(1, 25)
]

def process(self):
for job in self.jobs:
job.percent_complete = min(
job.percent_complete+random.randint(0, 3), 100)

if any(job.percent_complete < 100 for job in self.jobs):
GUI.invoke_after(100, self.process)

def _start_fired(self):
self.populate()
GUI.invoke_after(1000, self.process)

view = View(
UItem('jobs', editor=TableEditor(
columns=[
ObjectColumn(name='name'),
ProgressColumn(name='percent_complete'),
]
)),
UItem('start'),
resizable=True,
)


demo = view = job_manager = JobManager()

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()
63 changes: 63 additions & 0 deletions traitsui/extras/progress_column.py
@@ -0,0 +1,63 @@
#------------------------------------------------------------------------------
#
# Copyright (c) 2016, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: Corran Webster
#
#------------------------------------------------------------------------------

""" A column class for for the TableEditor that displays progress bars. """

from __future__ import absolute_import

from traits.etsconfig.api import ETSConfig
from traits.api import Str

from traitsui.table_column import ObjectColumn


if ETSConfig.toolkit == 'qt4':
from traitsui.qt4.extra.progress_renderer import ProgressRenderer
else:
raise NotImplementedError("No checkbox renderer for backend")


class ProgressColumn(ObjectColumn):

# Format string to apply to column values:
format = Str('%s%%')

def __init__(self, **traits):
super(ProgressColumn, self).__init__(**traits)
# force the renderer to be a progress bar renderer
self.renderer = ProgressRenderer()

def is_editable(self, object):
""" Returns whether the column is editable for a specified object.
"""
# Although a checkbox column is always editable, we return this
# to keep a standard editor from appearing. The editing is handled
# in the renderer's handlers.
return False

def get_minimum(self, object):
return 0

def get_maximum(self, object):
return 100

def get_text_visible(self, object):
""" Whether or not to display text in column.
Note, may not render on some platforms (eg. OS X) due to
the rendering style.
"""
return True
52 changes: 52 additions & 0 deletions traitsui/qt4/extra/progress_renderer.py
@@ -0,0 +1,52 @@
#------------------------------------------------------------------------------
# Copyright (c) 2016, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Corran Webster
#------------------------------------------------------------------------------

""" A renderer which displays a progress bar based on a 0.0 to 1.0 value.
"""

# System library imports
from pyface.qt import QtCore, QtGui

# ETS imports
from traitsui.qt4.table_editor import TableDelegate


class ProgressRenderer(TableDelegate):
""" A renderer which displays a progress bar for a True value and an
unchecked box for a false value.
"""

#---------------------------------------------------------------------------
# QAbstractItemDelegate interface
#---------------------------------------------------------------------------

def paint(self, painter, option, index):
""" Reimplemented to paint the checkbox.
"""
# Get the progress value
column = index.model()._editor.columns[index.column()]
obj = index.data(QtCore.Qt.UserRole)

# set up progress bar options
progress_bar_option = QtGui.QStyleOptionProgressBarV2()
progress_bar_option.rect = option.rect
progress_bar_option.minimum = column.get_minimum(obj)
progress_bar_option.maximum = column.get_maximum(obj)
progress_bar_option.progress = column.get_raw_value(obj)
progress_bar_option.textVisible = column.get_text_visible(obj)
progress_bar_option.text = column.get_value(obj)

# Draw it
style = QtGui.QApplication.instance().style()
style.drawControl(QtGui.QStyle.CE_ProgressBar, progress_bar_option,
painter)

0 comments on commit eb161e3

Please sign in to comment.