[ocrfeeder/introspection: 3/14] Port customWidgets.py to GI
- From: Joaquim Manuel Pereira Rocha <jrocha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ocrfeeder/introspection: 3/14] Port customWidgets.py to GI
- Date: Mon, 21 Oct 2013 21:38:02 +0000 (UTC)
commit 478d9f18c2626a19b3574cf8bbb80becc95d9b6b
Author: Joaquim Rocha <me joaquimrocha com>
Date: Tue Sep 10 22:06:00 2013 +0200
Port customWidgets.py to GI
src/ocrfeeder/studio/customWidgets.py | 183 +++++++++++++++++----------------
1 files changed, 92 insertions(+), 91 deletions(-)
---
diff --git a/src/ocrfeeder/studio/customWidgets.py b/src/ocrfeeder/studio/customWidgets.py
index 9ec80bf..46f9eeb 100644
--- a/src/ocrfeeder/studio/customWidgets.py
+++ b/src/ocrfeeder/studio/customWidgets.py
@@ -18,52 +18,50 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###########################################################################
-import pygtk
-pygtk.require('2.0')
-import gtk, goocanvas
+from gi.repository import Gtk, Gdk, GooCanvas, GObject, GdkPixbuf
import os.path
-import gobject
import gettext
_ = gettext.gettext
-class SelectableBoxesArea(goocanvas.Canvas):
+class SelectableBoxesArea(GooCanvas.Canvas):
MINIMUM_ZOOM_HEIGHT = 50
IMAGE_FRAME_THICKNESS = 5
IMAGE_FRAME_COLOR = '#717171'
- __gtype_name__ = 'SelectableBoxesArea'
+# __gtype_name__ = 'SelectableBoxesArea'
__gsignals__ = {
- 'selected_box' : (gobject.SIGNAL_RUN_LAST,
- gobject.TYPE_NONE,
- (gobject.TYPE_PYOBJECT,)),
- 'removed_box' : (gobject.SIGNAL_RUN_LAST,
- gobject.TYPE_NONE,
- (gobject.TYPE_PYOBJECT,)),
- 'updated_box' : (gobject.SIGNAL_RUN_LAST,
- gobject.TYPE_NONE,
- (gobject.TYPE_PYOBJECT,)),
- 'deselected_box' : (gobject.SIGNAL_RUN_LAST,
- gobject.TYPE_NONE,
- (gobject.TYPE_PYOBJECT,)),
- 'dragged_box' : (gobject.SIGNAL_RUN_LAST,
- gobject.TYPE_NONE,
- (gobject.TYPE_PYOBJECT,)),
- 'changed_zoom' : (gobject.SIGNAL_RUN_LAST,
- gobject.TYPE_NONE,
- (gobject.TYPE_FLOAT,))
+ 'selected_box' : (GObject.SIGNAL_RUN_LAST,
+ GObject.TYPE_NONE,
+ (GObject.TYPE_PYOBJECT,)),
+ 'removed_box' : (GObject.SIGNAL_RUN_LAST,
+ GObject.TYPE_NONE,
+ (GObject.TYPE_PYOBJECT,)),
+ 'updated_box' : (GObject.SIGNAL_RUN_LAST,
+ GObject.TYPE_NONE,
+ (GObject.TYPE_PYOBJECT,)),
+ 'deselected_box' : (GObject.SIGNAL_RUN_LAST,
+ GObject.TYPE_NONE,
+ (GObject.TYPE_PYOBJECT,)),
+ 'dragged_box' : (GObject.SIGNAL_RUN_LAST,
+ GObject.TYPE_NONE,
+ (GObject.TYPE_PYOBJECT,)),
+ 'changed_zoom' : (GObject.SIGNAL_RUN_LAST,
+ GObject.TYPE_NONE,
+ (GObject.TYPE_FLOAT,))
}
def __init__(self, image_path):
- super(SelectableBoxesArea, self).__init__()
- self.image = goocanvas.Image()
+ GooCanvas.Canvas.__init__(self)
+ self.image = GooCanvas.CanvasImage()
self.frame = []
self.setBackgroundImage(image_path)
self.grab_focus(self.image)
- style = gtk.widget_get_default_style()
- self.set_property('background-color', style.bg[gtk.STATE_NORMAL].to_string())
- self.get_root_item().add_child(self.image, -1)
+ #style = Gtk.widget_get_default_style()
+ #self.set_property('background-color', style.bg[Gtk.STATE_NORMAL].to_string())
+ self.realize()
+ self.get_root_item().add_child(self.image, 0)
self.area_fill_rgba = (94, 156, 235, 150)
self.area_stroke_rgba = (94, 156, 235, 250)
self.area_selected_stroke_rgba = (255, 255, 0, 255)
@@ -94,25 +92,25 @@ class SelectableBoxesArea(goocanvas.Canvas):
return start_x, start_y, width, height
def setBackgroundImage(self, image_path):
- pixbuf = gtk.gdk.pixbuf_new_from_file(image_path)
+ pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_path)
self.image.set_property('pixbuf', pixbuf)
self.set_bounds(0, 0, self.image.props.width * 2, self.image.props.height * 1.5)
- for line in self.frame:
- line.remove()
- self.__createFrame()
- for line in self.frame:
- self.get_root_item().add_child(line, -1)
+ #for line in self.frame:
+ # line.remove()
+ #self.__createFrame()
+ #for line in self.frame:
+ # self.get_root_item().add_child(line, 0)
def __createFrame(self):
- line = goocanvas.Rect(fill_color = self.IMAGE_FRAME_COLOR, line_width = 0)
+ line = GooCanvas.CanvasRect(fill_color = self.IMAGE_FRAME_COLOR, line_width = 0)
line.props.x = self.image.props.x + self.image.props.width
line.props.y = self.image.props.y
line.props.width = self.IMAGE_FRAME_THICKNESS
line.props.height = self.image.props.height + self.IMAGE_FRAME_THICKNESS
self.frame.append(line)
- line = goocanvas.Rect(fill_color = self.IMAGE_FRAME_COLOR,
- line_width = 0)
+ line = GooCanvas.CanvasRect(fill_color = self.IMAGE_FRAME_COLOR,
+ line_width = 0)
line.props.x = self.image.props.x
line.props.y = self.image.props.y + self.image.props.height
line.props.width = self.image.props.width + 1
@@ -215,15 +213,16 @@ class SelectableBoxesArea(goocanvas.Canvas):
return self.image.props.height, self.image.props.width
def startSelectionArea(self, item, target, event):
- if event.type != gtk.gdk.BUTTON_PRESS:
+ if event.type != Gdk.EventType.BUTTON_PRESS:
return False
self.deselectAreas()
fill_color = self.__rgbaToInteger(self.area_fill_rgba)
stroke_color = self.__rgbaToInteger(self.area_stroke_rgba)
self.currently_created_area = Box(fill_color_rgba = fill_color, stroke_color_rgba = stroke_color)
- self.currently_created_area.props.x = event.x * self.get_scale()
- self.currently_created_area.props.y = event.y * self.get_scale()
- self.currently_created_area.set_data('start_point', (self.currently_created_area.props.x,
self.currently_created_area.props.y))
+ x, y = event.x * self.get_scale(), event.y * self.get_scale()
+ self.currently_created_area.props.x = x
+ self.currently_created_area.props.y = y
+ self.currently_created_area.start_point = x, y
self.currently_created_area.connect('button_release_event', self.releasedWithinArea)
self.currently_created_area.connect('motion_notify_event', self.dragArea)
self.get_root_item().add_child(self.currently_created_area, -1)
@@ -231,7 +230,6 @@ class SelectableBoxesArea(goocanvas.Canvas):
def updateSelectionArea(self, item, target, event):
if self.currently_created_area != None:
- start_point = self.currently_created_area.get_data('start_point')
end_x = event.x
end_y = event.y
if event.x > self.image.props.width:
@@ -243,6 +241,7 @@ class SelectableBoxesArea(goocanvas.Canvas):
elif event.y < 0:
end_y = 0
end_point = (end_x, end_y)
+ start_point = self.currently_created_area.start_point
x, y, width, height = self.__getRectangleFromPoints(start_point, end_point)
self.currently_created_area.props.x = x
self.currently_created_area.props.y = y
@@ -266,7 +265,8 @@ class SelectableBoxesArea(goocanvas.Canvas):
offset = 2
start_point = (area.props.x + 5, area.props.y + offset)
end_point = (area.props.x + area.props.width - offset, area.props.y + area.props.height - offset)
- bounds = goocanvas.Bounds(*(start_point + end_point))
+ bounds = GooCanvas.CanvasBounds()
+ bounds.x1, bounds.y1, bounds.x2, bounds.y2 = start_point + end_point
overlaped_items = self.get_items_in_area(bounds, True, True, True)
if area in overlaped_items:
overlaped_items.remove(area)
@@ -281,9 +281,9 @@ class SelectableBoxesArea(goocanvas.Canvas):
self.emit('removed_box', area)
def keyPressed(self, item, rect, event):
- key_name = gtk.gdk.keyval_name(event.keyval).lower()
+ key_name = Gdk.keyval_name(event.keyval).lower()
if key_name in ['left', 'up', 'right', 'down']:
- fast_mask = gtk.gdk.SHIFT_MASK
+ fast_mask = Gdk.ModifierType.SHIFT_MASK
step = 2
if event.state == fast_mask:
step = 10
@@ -300,8 +300,8 @@ class SelectableBoxesArea(goocanvas.Canvas):
return True
def pressedKeyOnImage(self, item, rect, event):
- key_name = gtk.gdk.keyval_name(event.keyval).lower()
- if key_name in ['up', 'down'] and event.state == gtk.gdk.CONTROL_MASK:
+ key_name = Gdk.keyval_name(event.keyval).lower()
+ if key_name in ['up', 'down'] and event.state == Gdk.ModifierType.CONTROL_MASK:
if key_name == 'up':
self.zoom(0.2)
return True
@@ -319,22 +319,21 @@ class SelectableBoxesArea(goocanvas.Canvas):
def scrollEventCb(self, widget, event):
# Note: This catches all modifier combinations that use Ctrl. Add
# further combinations _before_ for them to take precedence!
- if event.state & gtk.gdk.CONTROL_MASK:
- if event.direction == gtk.gdk.SCROLL_UP or \
- event.direction == gtk.gdk.SCROLL_RIGHT:
+ if event.state & Gdk.ModifierType.CONTROL_MASK:
+ if event.direction == Gdk.ScrollDirection.UP or \
+ event.direction == Gdk.ScrollDirection.RIGHT:
self.zoom(0.05)
return True # we have handled the event - don't propagate to parent
- elif event.direction == gtk.gdk.SCROLL_DOWN or \
- event.direction == gtk.gdk.SCROLL_LEFT:
+ elif event.direction == Gdk.ScrollDirection.DOWN or \
+ event.direction == Gdk.ScrollDirection.LEFT:
self.zoom(-0.05)
return True # we have handled the event - don't propagate to parent
- elif event.state & gtk.gdk.SHIFT_MASK:
- event.state &= ~gtk.gdk.SHIFT_MASK
- if event.direction == gtk.gdk.SCROLL_UP:
- event.direction = gtk.gdk.SCROLL_LEFT
+ elif event.state & Gdk.ModifierType.SHIFT_MASK:
+ if event.direction == Gdk.ScrollDirection.UP:
+ event.direction = Gdk.ScrollDirection.LEFT
return False # we have not handled the (new) event - propagate to parent
- elif event.direction == gtk.gdk.SCROLL_DOWN:
- event.direction = gtk.gdk.SCROLL_RIGHT
+ elif event.direction == Gdk.ScrollDirection.DOWN:
+ event.direction = Gdk.ScrollDirection.RIGHT
return False # we have not handled the (new) event - propagate to parent
def setAreaFillRgba(self, rgba):
@@ -364,11 +363,10 @@ class SelectableBoxesArea(goocanvas.Canvas):
area.remove()
def getAllAreas(self):
- bounds = goocanvas.Bounds(*self.get_bounds())
- areas = [area for area in self.get_items_in_area(bounds, True, True, True) if isinstance(area, Box)
and area not in self.frame]
+ areas = [area for area in self.get_items_in_area(self.bounds, True, True, True) if isinstance(area,
Box) and area not in self.frame]
return areas
-class Box(goocanvas.Rect):
+class Box(GooCanvas.CanvasRect):
MOUSE_STATE_NORMAL = 0
MOUSE_STATE_TOP_DRAG = 1 << 0
@@ -377,15 +375,15 @@ class Box(goocanvas.Rect):
MOUSE_STATE_RIGHT_DRAG = 1 << 3
CURSOR_CHANGE_MAX_DISTANCE = 5
- __bottom_side_cursor = gtk.gdk.Cursor(gtk.gdk.BOTTOM_SIDE)
- __top_side_cursor = gtk.gdk.Cursor(gtk.gdk.TOP_SIDE)
- __left_side_cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_SIDE)
- __right_side_cursor = gtk.gdk.Cursor(gtk.gdk.RIGHT_SIDE)
- __top_left_cursor = gtk.gdk.Cursor(gtk.gdk.TOP_LEFT_CORNER)
- __top_right_cursor = gtk.gdk.Cursor(gtk.gdk.TOP_RIGHT_CORNER)
- __bottom_left_cursor = gtk.gdk.Cursor(gtk.gdk.BOTTOM_LEFT_CORNER)
- __bottom_right_cursor = gtk.gdk.Cursor(gtk.gdk.BOTTOM_RIGHT_CORNER)
- __drag_cursor = gtk.gdk.Cursor(gtk.gdk.FLEUR)
+ __bottom_side_cursor = Gdk.Cursor(Gdk.CursorType.BOTTOM_SIDE)
+ __top_side_cursor = Gdk.Cursor(Gdk.CursorType.TOP_SIDE)
+ __left_side_cursor = Gdk.Cursor(Gdk.CursorType.LEFT_SIDE)
+ __right_side_cursor = Gdk.Cursor(Gdk.CursorType.RIGHT_SIDE)
+ __top_left_cursor = Gdk.Cursor(Gdk.CursorType.TOP_LEFT_CORNER)
+ __top_right_cursor = Gdk.Cursor(Gdk.CursorType.TOP_RIGHT_CORNER)
+ __bottom_left_cursor = Gdk.Cursor(Gdk.CursorType.BOTTOM_LEFT_CORNER)
+ __bottom_right_cursor = Gdk.Cursor(Gdk.CursorType.BOTTOM_RIGHT_CORNER)
+ __drag_cursor = Gdk.Cursor(Gdk.CursorType.FLEUR)
def __init__(self, *args, **kwargs):
super(Box, self).__init__(*args, **kwargs)
@@ -395,15 +393,18 @@ class Box(goocanvas.Rect):
self.connect('button-press-event', self.__buttonPressEventCb)
# Reset the cursor
self.connect('leave-notify-event', self.__leaveNotifyEventCb)
+ # To help on the creation of the box
+ self.start_point = None
+ self._distance = None
def __leaveNotifyEventCb(self, item, target, event):
- if event.state & gtk.gdk.BUTTON1_MASK:
+ if event.state & Gdk.ModifierType.BUTTON1_MASK:
return True
self.__setNormalState()
def __buttonPressEventCb(self, item, target, event):
deselected = False
- if event.state & gtk.gdk.SHIFT_MASK == 0:
+ if event.state & Gdk.ModifierType.SHIFT_MASK == 0:
self.get_canvas().deselectAreas()
else:
deselected = self.get_canvas().deselectArea(self)
@@ -412,20 +413,20 @@ class Box(goocanvas.Rect):
self.get_canvas().selectArea(self)
if self._state == self.MOUSE_STATE_NORMAL:
self.get_canvas().get_window().set_cursor(self.__drag_cursor)
- self.set_data('distance', (event.x - self.props.x, event.y - self.props.y))
+ self._distance = event.x - self.props.x, event.y - self.props.y
return True
def __buttonReleaseEventCb(self, item, target, event):
- self.set_data('distance', None)
+ self._distance = None
self.get_canvas().get_window().set_cursor(None)
def __motionNotifyEventCb(self, item, target, event):
- if self.get_data('distance'):
- distance_x, distance_y = self.get_data('distance')
+ if self._distance:
+ distance_x, distance_y = self._distance
new_x, new_y = event.x - distance_x, event.y - distance_y
else:
new_x, new_y = self.props.x, self.props.y
- if event.state & gtk.gdk.BUTTON1_MASK:
+ if event.state & Gdk.ModifierType.BUTTON1_MASK:
old_y = int(self.props.y)
old_x = int(self.props.x)
if self._state & self.MOUSE_STATE_TOP_DRAG:
@@ -561,32 +562,32 @@ class Box(goocanvas.Rect):
def __setNormalState(self):
self.__setMouseState(self.MOUSE_STATE_NORMAL, None)
-class PlainFrame(gtk.Frame):
+class PlainFrame(Gtk.Frame):
def __init__(self, label):
super(PlainFrame, self).__init__()
- label_widget = gtk.Label()
+ label_widget = Gtk.Label()
label_widget.set_markup('<b>' + label + '</b>')
self.set_label_widget(label_widget)
- self.container = gtk.Alignment(0, 0, 1, 1)
- self.container.set_padding(12, 0, 12, 12)
- super(PlainFrame, self).add(self.container)
- self.set_shadow_type(gtk.SHADOW_NONE)
+ self._container = Gtk.Alignment.new(0, 0, 1, 1)
+ self._container.set_padding(12, 0, 12, 12)
+ super(PlainFrame, self).add(self._container)
+ self.set_shadow_type(Gtk.ShadowType.NONE)
def add(self, widget):
- self.container.add(widget)
+ self._container.add(widget)
-class PlainExpander(gtk.Expander):
+class PlainExpander(Gtk.Expander):
def __init__(self, label):
super(PlainExpander, self).__init__()
- label_widget = gtk.Label()
+ label_widget = Gtk.Label()
label_widget.set_markup('<b>' + label + '</b>')
self.set_label_widget(label_widget)
- self.container = gtk.Alignment(0, 0, 1, 1)
- self.container.set_padding(12, 0, 12, 12)
- super(PlainExpander, self).add(self.container)
+ self._container = Gtk.Alignment.new(0, 0, 1, 1)
+ self._container.set_padding(12, 0, 12, 12)
+ super(PlainExpander, self).add(self._container)
self.set_expanded(False)
def add(self, widget):
- self.container.add(widget)
+ self._container.add(widget)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]