diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2012-10-24 15:22:57 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2012-10-24 15:22:57 (GMT) |
commit | 5a7acd6fdf1489a999b373f4eff5667c5c9dcbff (patch) | |
tree | d0edb1ea98f69cf1c5de19d03a40587a8760636d | |
parent | b9a67e19b24cf4e067bb93a2eb2a4c07fc151ad5 (diff) |
Fix table creation - SL #3870
This patch replace the abiword widget used for one
with bigger cells to enable the use with touch.
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r-- | gridcreate.py | 106 | ||||
-rw-r--r-- | icons/create-table.svg | 89 | ||||
-rw-r--r-- | toolbar.py | 27 |
3 files changed, 209 insertions, 13 deletions
diff --git a/gridcreate.py b/gridcreate.py new file mode 100644 index 0000000..9115566 --- /dev/null +++ b/gridcreate.py @@ -0,0 +1,106 @@ +#!/usr/bin/python + +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject + +class GridCreateWidget(Gtk.DrawingArea): + + __gsignals__ = { + 'create-table': ( + GObject.SignalFlags.RUN_FIRST, None, [int, int]), + } + + def __init__(self): + super(GridCreateWidget, self).__init__() + self._cell_width = 75 # will be style.GRID_CELL_SIZE + self._cell_height = int(self._cell_width / 2) + self._rows = 0 + self._columns = 0 + self._min_rows = 3 + self._min_columns = 3 + + self._update_size() + self.connect('draw', self.__draw_cb) + self.set_events(Gdk.EventMask.TOUCH_MASK) + self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) + self.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK) + self.add_events(Gdk.EventMask.BUTTON_MOTION_MASK) + self.connect('event', self.__event_cb) + + def __event_cb(self, widget, event): + if event.type in (Gdk.EventType.TOUCH_BEGIN, + Gdk.EventType.TOUCH_CANCEL, Gdk.EventType.TOUCH_END, + Gdk.EventType.TOUCH_UPDATE, Gdk.EventType.BUTTON_PRESS, + Gdk.EventType.BUTTON_RELEASE, Gdk.EventType.MOTION_NOTIFY): + x = event.get_coords()[1] + y = event.get_coords()[2] + seq = str(event.touch.sequence) + + if event.type in (Gdk.EventType.TOUCH_BEGIN, + Gdk.EventType.TOUCH_UPDATE, Gdk.EventType.BUTTON_PRESS, + Gdk.EventType.MOTION_NOTIFY): + # update rows and cols + columns = int(x / self._cell_width) + 1 + rows = int(y / self._cell_height) + 1 + if self._columns != columns or self._rows != rows: + self._columns = columns + self._rows = rows + self._update_size() + + elif event.type in (Gdk.EventType.TOUCH_END, + Gdk.EventType.BUTTON_RELEASE): + self.emit('create-table', self._rows, self._columns) + + def _update_size(self): + self._min_col = max(self._columns + 1, self._min_columns) + self._width = self._min_col * self._cell_width + self._min_rows = max(self._rows + 1, self._min_rows) + self._height = self._min_rows * self._cell_height + self.set_size_request(self._width, self._height) + self.queue_draw() + + def __draw_cb(self, widget, cr): + # background + cr.set_source_rgb(0.0, 0.0, 0.0) + cr.rectangle(0, 0, self._width, self._height) + cr.fill() + # used area + cr.set_source_rgb(0.9, 0.9, 0.9) + width = self._columns * self._cell_width + height = self._rows * self._cell_height + cr.rectangle(0, 0, width, height) + cr.fill() + # draw grid + cr.set_source_rgb(1.0, 1.0, 1.0) + self._draw_grid(cr, self._min_rows, self._min_col, self._width, + self._height) + cr.set_source_rgb(0.0, 0.0, 0.0) + self._draw_grid(cr, self._rows, self._columns, width, height) + + def _draw_grid(self, cr, rows, cols, width, height): + for n in range(rows + 1): + cr.move_to(0, n * self._cell_height) + cr.line_to(width, n * self._cell_height) + for n in range(cols + 1): + cr.move_to(n * self._cell_width, 0) + cr.line_to(n * self._cell_width, height) + cr.stroke() + + +class GridCreateTest(Gtk.Window): + + def __init__(self): + super(GridCreateTest, self).__init__() + self.connect("destroy", Gtk.main_quit) + grid_create = GridCreateWidget() + grid_create.connect('create-table', self.__create_table) + self.add(grid_create) + self.show_all() + + def __create_table(self, grid_creator, rows, columns): + print 'rows %d columns %d' % (rows, columns) + +if __name__ == '__main__': + GridCreateTest() + Gtk.main() diff --git a/icons/create-table.svg b/icons/create-table.svg new file mode 100644 index 0000000..09c7e0b --- /dev/null +++ b/icons/create-table.svg @@ -0,0 +1,89 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + version="1.1" + width="55" + height="54.695999" + viewBox="0 0 55 54.696" + id="svg3072" + xml:space="preserve"><metadata + id="metadata3093"><rdf:RDF><cc:Work + rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /><dc:title></dc:title></cc:Work></rdf:RDF></metadata><defs + id="defs3091" /><g + id="insert-table" + style="display:block"> + <g + id="g3075" + style="display:inline"> + <g + id="g3077"> + <rect + width="43" + height="24" + x="5.875" + y="15.515" + id="rect3079" + style="fill:#808284;stroke:#ffffff;stroke-width:2.25" /> + <line + fill="#808284" + stroke="#FFFFFF" + stroke-width="2.25" + x1="5.875" + x2="48.875" + y1="31.51" + y2="31.51" + id="line3081" + style="fill:#808284;stroke:#ffffff;stroke-width:2.25" /> + <line + fill="#808284" + stroke="#FFFFFF" + stroke-width="2.25" + x1="5.875" + x2="48.875" + y1="23.52" + y2="23.52" + id="line3083" + style="fill:#808284;stroke:#ffffff;stroke-width:2.25" /> + <line + fill="#808284" + stroke="#FFFFFF" + stroke-width="2.25" + x1="34.549" + x2="34.549" + y1="15.515" + y2="39.514999" + id="line3085" + style="fill:#808284;stroke:#ffffff;stroke-width:2.25" /> + <line + fill="#808284" + stroke="#FFFFFF" + stroke-width="2.25" + x1="20.201" + x2="20.201" + y1="15.515" + y2="39.514999" + id="line3087" + style="fill:#808284;stroke:#ffffff;stroke-width:2.25" /> + </g> + </g> +</g><g + id="g3889"><path + d="m 43.135983,37.463013 a 8.188344,8.1348257 0 1 1 -16.376688,0 8.188344,8.1348257 0 1 1 16.376688,0 z" + transform="translate(5.9999996,0)" + id="path3095" + style="fill:#ffffff;fill-opacity:1;stroke:#ffffff;stroke-width:3;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" /><g + transform="translate(53.479536,14.396504)" + id="g3885"><path + d="m -12.520327,18.141649 -0.02102,9.742714 -0.0026,0.107005 0,0" + id="path3865" + style="fill:none;stroke:#000000;stroke-width:3;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" /><path + d="m -7.6070283,23.073278 -9.7427297,-0.01149 -0.107008,-0.0025 0,0" + id="path3865-4" + style="fill:none;stroke:#000000;stroke-width:3;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" /></g></g></svg>
\ No newline at end of file @@ -23,7 +23,6 @@ from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GObject from gi.repository import GdkPixbuf -from gi.repository import Abi import os import tempfile @@ -43,6 +42,7 @@ from sugar3.activity.widgets import RedoButton from widgets import AbiButton from widgets import FontSizeCombo from fontcombobox import FontComboBox +from gridcreate import GridCreateWidget logger = logging.getLogger('write-activity') @@ -218,17 +218,15 @@ class InsertToolbar(Gtk.Toolbar): self._abiword_canvas = abiword_canvas - self._table = Abi.Table() - self._table.set_labels(_('Table'), _('Cancel')) - self._table_id = self._table.connect('selected', self._table_cb) - image = Gtk.Image() - image.set_from_icon_name('insert-table', -1) - self._table.set_image(image) - self._table.set_relief(Gtk.ReliefStyle.NONE) - tool_item = Gtk.ToolItem() - tool_item.add(self._table) - self.insert(tool_item, -1) - tool_item.show_all() + self._table_btn = ToolButton('create-table') + self._table_btn.set_tooltip(_('Create table')) + self.insert(self._table_btn, -1) + self._grid_create = GridCreateWidget() + self._grid_create.show() + self._grid_create.connect('create-table', self._create_table_cb) + palette = self._table_btn.get_palette() + palette.set_content(self._grid_create) + self._table_btn.connect('clicked', self._table_btn_clicked_cb) self._table_rows_after = ToolButton('row-insert') self._table_rows_after.set_tooltip(_('Insert Row')) @@ -260,7 +258,10 @@ class InsertToolbar(Gtk.Toolbar): #self._abiword_canvas.connect('image-selected', # self._image_selected_cb) - def _table_cb(self, abi, rows, cols): + def _table_btn_clicked_cb(self, button): + button.get_palette().popup(True, button.get_palette().SECONDARY) + + def _create_table_cb(self, abi, rows, cols): self._abiword_canvas.insert_table(rows, cols) def _table_rows_after_cb(self, button): |