/usr/share/pyshared/chavier/gui.py is in python-pycha 0.7.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | # Copyright(c) 2007-2010 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
#
# This file is part of Chavier.
#
# Chavier is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Chavier is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Chavier. If not, see <http://www.gnu.org/licenses/>.
import pygtk
pygtk.require('2.0')
import gtk
from chavier.dialogs import (
TextInputDialog, PointDialog, OptionDialog, RandomGeneratorDialog,
AboutDialog, warning,
)
class GUI(object):
def __init__(self, app):
self.app = app
self.chart = None
self.surface = None
self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.main_window.connect('delete_event', self.delete_event)
self.main_window.connect('destroy', self.destroy)
self.main_window.set_default_size(640, 480)
self.main_window.set_title(u'Chavier')
vbox = gtk.VBox()
self.main_window.add(vbox)
vbox.show()
menubar, toolbar = self._create_ui_manager()
vbox.pack_start(menubar, False, False)
menubar.show()
vbox.pack_start(toolbar, False, False)
toolbar.show()
hpaned = gtk.HPaned()
vbox.pack_start(hpaned, True, True)
hpaned.show()
vpaned = gtk.VPaned()
hpaned.add1(vpaned)
vpaned.show()
block1 = self._create_sidebar_block(u'Data sets',
self._datasets_notebook_creator)
self._create_dataset("Dataset 1")
block1.set_size_request(-1, 200)
vpaned.add1(block1)
block1.show()
block2 = self._create_sidebar_block(u'Options',
self._options_treeview_creator)
vpaned.add2(block2)
block2.show()
self.drawing_area = gtk.DrawingArea()
self.drawing_area.connect('expose_event',
self.drawing_area_expose_event)
self.drawing_area.connect('size_allocate',
self.drawing_area_size_allocate_event)
hpaned.add2(self.drawing_area)
self.drawing_area.show()
self.main_window.show()
def _create_ui_manager(self):
self.uimanager = gtk.UIManager()
accel_group = self.uimanager.get_accel_group()
self.main_window.add_accel_group(accel_group)
action_group = gtk.ActionGroup('default')
action_group.add_actions([
('file', None, '_File', None, 'File', None),
('quit', gtk.STOCK_QUIT, None, None, 'Quit the program',
self.quit),
('edit', None, '_Edit', None, 'Edit', None),
('add_dataset', gtk.STOCK_ADD, '_Add dataset',
'<ctrl><alt>plus', 'Add another dataset', self.add_dataset),
('remove_dataset', gtk.STOCK_REMOVE, '_Remove dataset',
'<ctrl><alt>minus', 'Remove the current dataset',
self.remove_dataset),
('edit_dataset', gtk.STOCK_EDIT, '_Edit dataset name',
'<ctrl><alt>e', 'Edit the name of the current dataset',
self.edit_dataset),
('add_point', gtk.STOCK_ADD, 'Add _point', '<ctrl>plus',
'Add another point to the current dataset', self.add_point),
('remove_point', gtk.STOCK_REMOVE, 'Remove p_oint',
'<ctrl>minus',
'Remove the current point of the current dataset',
self.remove_point),
('edit_point', gtk.STOCK_EDIT, 'Edit po_int', '<ctrl>e',
'Edit the current point of the current dataset',
self.edit_point),
('edit_option', gtk.STOCK_EDIT, 'Edit op_tion', None,
'Edit the current option',
self.edit_option),
('view', None, '_View', None, 'View', None),
('refresh', gtk.STOCK_REFRESH, None, '<ctrl>r',
'Update the chart', self.refresh),
('tools', None, '_Tools', None, 'Tools', None),
('random-points', gtk.STOCK_EXECUTE, '_Generate random points',
'<ctrl>g', 'Generate random points',
self.generate_random_points),
('dump-chart-state', gtk.STOCK_CONVERT, '_Dump chart state',
'<ctrl>d', 'Dump internal chart variables',
self.dump_chart_state),
('help', None, '_Help', None, 'Help', None),
('about', gtk.STOCK_ABOUT, None, None, 'About this program',
self.about),
])
action_group.add_radio_actions([
('verticalbar', None, '_Vertical bars', None,
'Use vertical bars chart', self.app.VERTICAL_BAR_TYPE),
('horizontalbar', None, '_Horizontal bars', None,
'Use horizontal bars chart', self.app.HORIZONTAL_BAR_TYPE),
('line', None, '_Line', None,
'Use lines chart', self.app.LINE_TYPE),
('pie', None, '_Pie', None,
'Use pie chart', self.app.PIE_TYPE),
('radial', None, '_Radial', None,
'Use radial chart', self.app.RADIAL_TYPE),
('polygonal', None, '_Polygonal', None,
'Use polygonal chart', self.app.POLYGONAL_TYPE),
('scatter', None, '_Scatter', None,
'Use scatter chart', self.app.SCATTER_TYPE),
('stackedverticalbar', None, '_Stacked Vertical bars', None,
'Use stacked vertical bars chart',
self.app.STACKED_VERTICAL_BAR_TYPE),
('stackedhorizontalbar', None, '_Stacked Horizontal bars', None,
'Use stacked horizontal bars chart',
self.app.STACKED_HORIZONTAL_BAR_TYPE),
], self.app.VERTICAL_BAR_TYPE, self.on_chart_type_change)
self.uimanager.insert_action_group(action_group, -1)
ui = """<ui>
<menubar name="MenuBar">
<menu action="file">
<menuitem action="quit"/>
</menu>
<menu action="edit">
<menuitem action="add_dataset"/>
<menuitem action="remove_dataset"/>
<menuitem action="edit_dataset"/>
<separator />
<menuitem action="add_point"/>
<menuitem action="remove_point"/>
<menuitem action="edit_point"/>
<separator />
<menuitem action="edit_option"/>
</menu>
<menu action="view">
<menuitem action="refresh"/>
<separator />
<menuitem action="verticalbar"/>
<menuitem action="horizontalbar"/>
<menuitem action="stackedverticalbar"/>
<menuitem action="stackedhorizontalbar"/>
<menuitem action="line"/>
<menuitem action="pie"/>
<menuitem action="radial"/>
<menuitem action="polygonal"/>
<menuitem action="scatter"/>
</menu>
<menu action="tools">
<menuitem action="random-points"/>
<menuitem action="dump-chart-state"/>
</menu>
<menu action="help">
<menuitem action="about"/>
</menu>
</menubar>
<toolbar name="ToolBar">
<toolitem action="quit"/>
<separator />
<toolitem action="add_dataset"/>
<toolitem action="remove_dataset"/>
<separator />
<toolitem action="add_point"/>
<toolitem action="remove_point"/>
<separator />
<toolitem action="refresh"/>
</toolbar>
</ui>
"""
self.uimanager.add_ui_from_string(ui)
self.uimanager.ensure_update()
menubar = self.uimanager.get_widget('/MenuBar')
toolbar = self.uimanager.get_widget('/ToolBar')
return menubar, toolbar
def _create_sidebar_block(self, title, child_widget_creator):
box = gtk.VBox(spacing=6)
box.set_border_width(6)
label = gtk.Label()
label.set_markup(u'<span size="large" weight="bold">%s</span>' % title)
label.set_alignment(0.0, 0.5)
box.pack_start(label, False, False)
label.show()
child_widget = child_widget_creator()
box.pack_start(child_widget, True, True)
child_widget.show()
return box
def _datasets_notebook_creator(self):
self.datasets_notebook = gtk.Notebook()
self.datasets_notebook.set_scrollable(True)
return self.datasets_notebook
def _dataset_treeview_creator(self):
store = gtk.ListStore(float, float)
treeview = gtk.TreeView(store)
column1 = gtk.TreeViewColumn('x', gtk.CellRendererText(), text=0)
treeview.append_column(column1)
column2 = gtk.TreeViewColumn('y', gtk.CellRendererText(), text=1)
treeview.append_column(column2)
treeview.connect('row-activated', self.dataset_treeview_row_activated)
scrolled_window = gtk.ScrolledWindow()
scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
scrolled_window.add(treeview)
treeview.show()
return scrolled_window
def _options_treeview_creator(self):
self.options_store = gtk.TreeStore(str, str, object)
options = self.app.get_default_options()
self._fill_options_store(options, None, self.app.OPTIONS_TYPES)
self.options_treeview = gtk.TreeView(self.options_store)
column1 = gtk.TreeViewColumn('Name', gtk.CellRendererText(), text=0)
self.options_treeview.append_column(column1)
column2 = gtk.TreeViewColumn('Value', gtk.CellRendererText(), text=1)
self.options_treeview.append_column(column2)
self.options_treeview.expand_all()
self.options_treeview.connect('row-activated',
self.options_treeview_row_activated)
scrolled_window = gtk.ScrolledWindow()
scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
scrolled_window.add(self.options_treeview)
self.options_treeview.show()
return scrolled_window
def _fill_options_store(self, options, parent_node, types):
for name, value in options.items():
value_type = types[name]
if isinstance(value, dict):
current_parent = self.options_store.append(parent_node,
(name, None, None))
self._fill_options_store(value, current_parent, value_type)
else:
if value is not None:
value = str(value)
self.options_store.append(parent_node,
(name, value, value_type))
def _get_current_dataset_tab(self):
current_tab = self.datasets_notebook.get_current_page()
if current_tab != -1:
return self.datasets_notebook.get_nth_page(current_tab)
def _create_dataset(self, name):
scrolled_window = self._dataset_treeview_creator()
scrolled_window.show()
label = gtk.Label(name)
self.datasets_notebook.append_page(scrolled_window, label)
def _get_datasets(self):
datasets = []
n_pages = self.datasets_notebook.get_n_pages()
for i in range(n_pages):
tab = self.datasets_notebook.get_nth_page(i)
label = self.datasets_notebook.get_tab_label(tab)
name = label.get_label()
treeview = tab.get_children()[0]
model = treeview.get_model()
points = [(x, y) for x, y in model]
if len(points) > 0:
datasets.append((name, points))
return datasets
def _get_chart_type(self):
action_group = self.uimanager.get_action_groups()[0]
action = action_group.get_action('verticalbar')
return action.get_current_value()
def _get_options(self, iter):
options = {}
while iter is not None:
name, value, value_type = self.options_store.get(iter, 0, 1, 2)
if value_type is None:
child = self.options_store.iter_children(iter)
options[name] = self._get_options(child)
else:
if value is not None:
converter = str_converters[value_type]
value = converter(value)
options[name] = value
iter = self.options_store.iter_next(iter)
return options
def _edit_point_internal(self, model, iter):
x, y = model.get(iter, 0, 1)
dialog = PointDialog(self.main_window, x, y)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
x, y = dialog.get_point()
model.set(iter, 0, x, 1, y)
self.refresh()
dialog.destroy()
def _edit_option_internal(self, model, iter):
name, value, value_type = model.get(iter, 0, 1, 2)
parents = []
parent = model.iter_parent(iter)
while parent is not None:
parents.append(model.get_value(parent, 0))
parent = model.iter_parent(parent)
parents.reverse()
parents.append(name)
label = u'.'.join(parents)
dialog = OptionDialog(self.main_window, label, value, value_type)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
new_value = dialog.get_value()
if new_value == "":
new_value = None
model.set_value(iter, 1, new_value)
self.refresh()
dialog.destroy()
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def drawing_area_expose_event(self, widget, event, data=None):
if self.chart is None:
return
cr = widget.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
cr.set_source_surface(self.chart.surface, 0, 0)
cr.paint()
def drawing_area_size_allocate_event(self, widget, event, data=None):
if self.chart is not None:
self.refresh()
def on_chart_type_change(self, action, current, data=None):
if self.chart is not None:
self.refresh()
def dataset_treeview_row_activated(self, treeview, path, view_column):
model = treeview.get_model()
iter = model.get_iter(path)
self._edit_point_internal(model, iter)
def options_treeview_row_activated(self, treeview, path, view_column):
model = treeview.get_model()
iter = model.get_iter(path)
self._edit_option_internal(model, iter)
def quit(self, action):
self.main_window.destroy()
def add_dataset(self, action):
n_pages = self.datasets_notebook.get_n_pages()
suggested_name = u'Dataset %d' % (n_pages + 1)
dialog = TextInputDialog(self.main_window, suggested_name)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
name = dialog.get_name()
self._create_dataset(name)
self.datasets_notebook.set_current_page(n_pages)
dialog.destroy()
def remove_dataset(self, action):
current_tab = self.datasets_notebook.get_current_page()
assert current_tab != -1
self.datasets_notebook.remove_page(current_tab)
def edit_dataset(self, action):
tab = self._get_current_dataset_tab()
assert tab is not None
label = self.datasets_notebook.get_tab_label(tab)
name = label.get_label()
dialog = TextInputDialog(self.main_window, name)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
name = dialog.get_name()
label.set_label(name)
dialog.destroy()
def add_point(self, action):
tab = self._get_current_dataset_tab()
assert tab is not None
treeview = tab.get_children()[0]
model = treeview.get_model()
dialog = PointDialog(self.main_window, len(model) * 1.0, 0.0)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
x, y = dialog.get_point()
model.append((x, y))
self.refresh()
dialog.destroy()
def remove_point(self, action):
tab = self._get_current_dataset_tab()
assert tab is not None
treeview = tab.get_children()[0]
selection = treeview.get_selection()
model, selected = selection.get_selected()
if selected is None:
warning(self.main_window, "You must select the point to remove")
return
model.remove(selected)
self.refresh()
def edit_point(self, action):
tab = self._get_current_dataset_tab()
assert tab is not None
treeview = tab.get_children()[0]
selection = treeview.get_selection()
model, selected = selection.get_selected()
if selected is None:
warning(self.main_window, "You must select the point to edit")
return
self._edit_point_internal(model, selected)
def edit_option(self, action):
selection = self.options_treeview.get_selection()
model, selected = selection.get_selected()
if selected is None:
warning(self.main_window, "You must select the option to edit")
return
self._edit_option_internal(model, selected)
def refresh(self, action=None):
datasets = self._get_datasets()
if datasets:
root = self.options_store.get_iter_first()
options = self._get_options(root)
chart_type = self._get_chart_type()
alloc = self.drawing_area.get_allocation()
self.chart = self.app.get_chart(datasets, options, chart_type,
alloc.width, alloc.height)
self.drawing_area.queue_draw()
else:
self.chart = None
def generate_random_points(self, action=None):
tab = self._get_current_dataset_tab()
assert tab is not None
treeview = tab.get_children()[0]
model = treeview.get_model()
dialog = RandomGeneratorDialog(self.main_window)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
points = dialog.generate_points()
for point in points:
model.append(point)
self.refresh()
dialog.destroy()
def dump_chart_state(self, action=None):
if self.chart is None:
return
alloc = self.drawing_area.get_allocation()
print 'CHART STATE'
print '-' * 70
print 'surface: %d x %d' % (alloc.width, alloc.height)
print 'area :', self.chart.area
print
print 'minxval:', self.chart.minxval
print 'maxxval:', self.chart.maxxval
print 'xrange :', self.chart.xrange
print
print 'minyval:', self.chart.minyval
print 'maxyval:', self.chart.maxyval
print 'yrange :', self.chart.yrange
def about(self, action=None):
dialog = AboutDialog(self.main_window)
dialog.run()
dialog.destroy()
def run(self):
gtk.main()
def str2bool(str):
if str.lower() == "true":
return True
else:
return False
str_converters = {
str: str,
int: int,
float: float,
unicode: unicode,
bool: str2bool,
}
|