Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/reorderwindow.py
blob: c179447df529104e7d7efdb5af029188ed7dce9b (plain)
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
import cairo
from gettext import gettext as _

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import GdkPixbuf

from sugar3.graphics import style
from sugar3.graphics.toolbutton import ToolButton

HANDLE_SIZE = 18
MIN_IMAGE_SIZE = 50


class BaseWindow(Gtk.Window):

    def __init__(self, width=-1, height=-1):
        GObject.GObject.__init__(self)
        self.set_border_width(style.LINE_WIDTH)
        self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
        self.set_decorated(False)
        self.set_resizable(False)
        if width == -1:
            width = Gdk.Screen.width() - style.GRID_CELL_SIZE * 2
        if height == -1:
            height = Gdk.Screen.height() - style.GRID_CELL_SIZE * 2
        self.set_size_request(width, height)


class BasicToolbar(Gtk.Toolbar):

    def __init__(self, icon_name, title):
        GObject.GObject.__init__(self)

        icon = ToolButton(icon_name)
        self.insert(icon, -1)

        label = Gtk.Label()
        label.set_markup('<b>%s</b>' % title)
        label.set_alignment(0, 0.5)
        tool_item = Gtk.ToolItem()
        tool_item.set_expand(True)
        tool_item.add(label)
        tool_item.show_all()
        self.insert(tool_item, -1)

        self.separator = Gtk.SeparatorToolItem()
        self.separator.props.draw = False
        self.separator.set_expand(True)
        self.insert(self.separator, -1)

        self.stop = ToolButton(icon_name='dialog-cancel')
        self.stop.set_tooltip(_('Cancel'))
        self.insert(self.stop, -1)
        self.stop.show()

        self.confirm = ToolButton(icon_name='dialog-ok')
        self.confirm.set_tooltip(_('Done'))
        self.insert(self.confirm, -1)
        self.confirm.show()


class ReorderView(BaseWindow):

    def __init__(self, activity):
        BaseWindow.__init__(self)
        self.toolbar = BasicToolbar('thumbs-view',
                                    _('Drag the images to reorder'))

        self.toolbar.stop.connect('clicked', self.__stop_clicked_cb)
        self.toolbar.confirm.connect('clicked', self.__ok_clicked_cb)

        self.scrollwin = ReorderObjects(activity)
        self.vbox = Gtk.VBox()
        self.vbox.pack_start(self.toolbar, False, False, 0)
        self.vbox.pack_start(self.scrollwin, True, True, 0)
        self.add(self.vbox)
        self.scrollwin.show()

    def __stop_clicked_cb(self, button):
        self.destroy()

    def __ok_clicked_cb(self, button):
        self.scrollwin.reorder_comicboxs()
        self.scrollwin.display_comicboxs()
        self.destroy()


class ReorderObjects(Gtk.ScrolledWindow):
    def __init__(self, activity):
        GObject.GObject.__init__(self)
        self.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
        self.activity = activity
        self.comicboxes = self.activity.page.boxs

        self.liststore = Gtk.ListStore(GdkPixbuf.Pixbuf)
        self.iconview = Gtk.IconView.new()
        self.iconview.set_property('item-width', 200)
        self.iconview.set_model(self.liststore)
        self.iconview.set_pixbuf_column(0)
        self.iconview.set_reorderable(True)

        for comicbox in self.comicboxes[1:]:
            self.liststore.append([comicbox.get_thumbnail()])

        self.add(self.iconview)

    def on_item_activated(self):
        model = self.iconview.get_model()
        pixbuf = model[self.iconview.get_selected_items()[0]][0]
        for comicbox in self.comicboxes[1:]:
            if pixbuf is comicbox.thumbnail:
                self.editor = ImageEditorView(comicbox)
                self.editor.show_all()
                break

    def reorder_comicboxs(self):
        sorted_list = []
        for row in self.liststore:
            for comicbox in self.comicboxes[1:]:
                if row[0] is comicbox.thumbnail:
                    self.activity.page.table.remove(comicbox)
                    sorted_list.append(comicbox)
                    break
        sorted_list.insert(0, self.comicboxes[0])
        self.comicboxes = sorted_list
        self.activity.page.boxs = self.comicboxes

    def display_comicboxs(self):
        for i in range(0, len(self.comicboxes[1:])):
            reng = int(i / 2)
            column = i - (reng * 2)
            self.activity.page.table.attach(
                self.comicboxes[i+1], column, column + 1, reng, reng + 1)


class ImageElement:
    def __init__(self, pixbuf, box, x, y, w, h):
        self.box = box
        self.pixbuf = pixbuf
        self.pixbuf_original = self.pixbuf.scale_simple(
            self.pixbuf.get_width(), self.pixbuf.get_height(),
            GdkPixbuf.InterpType.BILINEAR)
        self.x = x
        self.y = y
        self.width = w
        self.height = h
        self.calculate_boundaries()
        self.calculate_points()

    def calculate_boundaries(self):
        self.boundaries = {}
        self.boundaries['min_x'] = self.x
        self.boundaries['max_x'] = self.x + self.width
        self.boundaries['min_y'] = self.y
        self.boundaries['max_y'] = self.y + self.height

    def calculate_points(self):
        self.points = {}
        self.points["upper_left"] = [self.x, self.y]
        self.points["upper_right"] = [self.x + self.width - HANDLE_SIZE,
                                      self.y]
        self.points["lower_left"] = [self.x,
                                     self.y + self.height - HANDLE_SIZE]
        self.points["lower_right"] = [self.x + self.width - HANDLE_SIZE,
                                      self.y + self.height - HANDLE_SIZE]

    def is_selected(self, x, y):
        if (x >= self.boundaries['min_x'] and
            x <= self.boundaries['max_x']) and \
           (y >= self.boundaries['min_y'] and
                y <= self.boundaries['max_y']):
            return True
        else:
            return False

    def is_resize(self, x, y):
        if self.is_in_point(x, y):
            return True
        else:
            return False

    def is_in_point(self, x, y, point=None):
        if point is not None:
            if (x >= point[0] and x <= (point[0] + HANDLE_SIZE)) \
                    and (y >= point[1] and y <= (point[1] + HANDLE_SIZE)):
                return True
            else:
                return False
        else:
            if self.is_in_point(x, y, self.points["upper_left"]) or \
                self.is_in_point(x, y, self.points["upper_right"]) or \
                self.is_in_point(x, y, self.points["lower_left"]) or \
                    self.is_in_point(x, y, self.points["lower_right"]):
                return True
            else:
                return False

    def draw(self, ctx):
        self.image = ctx.get_target().create_similar(
            cairo.CONTENT_COLOR_ALPHA, self.box.width,
            self.box.height)
        pixb_scaled = self.pixbuf_original.scale_simple(
            self.width, self.height, GdkPixbuf.InterpType.BILINEAR)
        ct = cairo.Context(self.image)
        Gdk.cairo_set_source_pixbuf(ct, pixb_scaled, self.x, self.y)
        ct.paint()
        self.pixbuf = pixb_scaled
        ctx.save()
        ctx.rectangle(0, 0, self.box.width, self.box.height)
        ctx.clip()
        ctx.set_source_surface(self.image, 0, 0)
        ctx.paint()
        ctx.restore()

        # draw hadles
        self._draw_handle(ctx, self.x, self.y)
        self._draw_handle(ctx, self.x + self.width - HANDLE_SIZE,
                          self.y + self.height - HANDLE_SIZE)

    def _draw_handle(self, ctx, x, y):
        ctx.save()
        ctx.set_line_width(2)
        ctx.set_source_rgb(1, 1, 1)
        ctx.rectangle(x, y, HANDLE_SIZE, HANDLE_SIZE)
        ctx.stroke_preserve()
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_dash([2])
        ctx.stroke()
        ctx.restore()

    def move(self, x_movement, y_movement, allocation):
        self.x = self.x + x_movement
        self.y = self.y + y_movement

        if self.x < 0:
            self.x = 0
        if self.y < 0:
            self.y = 0

        if self.x + self.width > allocation.width:
            self.x -= (self.x + self.width) - (allocation.width)

        if self.y + self.height > allocation.height:
            self.y -= (self.y + self.height) - (allocation.height)

        self.calculate_boundaries()
        self.calculate_points()

    def resize(self, x_movement, y_movement, allocation, start_x, start_y):

        if self.is_in_point(start_x, start_y, self.points["lower_left"]):
            x_final_pos = self.x + x_movement
            if x_final_pos < 0:
                x_movement -= x_final_pos
            self.x += x_movement
            self.width -= x_movement
            self.height += y_movement
        elif self.is_in_point(start_x, start_y, self.points["upper_right"]):
            y_final_pos = self.y + y_movement
            if y_final_pos < 0:
                y_movement -= y_final_pos
            self.y += y_movement
            self.height -= y_movement
            self.width += x_movement
        elif self.is_in_point(start_x, start_y, self.points["upper_left"]):
            x_final_pos = self.x + x_movement
            y_final_pos = self.y + y_movement
            if x_final_pos < 0:
                x_movement -= x_final_pos
            if y_final_pos < 0:
                y_movement -= y_final_pos
            self.y += y_movement
            self.x += x_movement
            self.width -= x_movement
            self.height -= y_movement
        else:
            self.height += y_movement
            self.width += x_movement

        if self.width < MIN_IMAGE_SIZE:
            self.width = MIN_IMAGE_SIZE
        if self.height < MIN_IMAGE_SIZE:
            self.height = MIN_IMAGE_SIZE

        if self.x + self.width > allocation.width:
            self.width -= (self.x + self.width) - (allocation.width)

        if self.y + self.height > allocation.height:
            self.height -= (self.y + self.height) - (allocation.height)

        self.calculate_boundaries()
        self.calculate_points()


class CanvasEditor(Gtk.EventBox):
    def __init__(self, comicbox, width, height, window):
        Gtk.EventBox.__init__(self)

        self.width = width
        self.height = height
        self.is_resize = False
        self.is_move = False
        self.parentw = window

        self.modify_bg(Gtk.StateType.NORMAL,
                       style.COLOR_WHITE.get_gdk_color())
        self.fixed = Gtk.Fixed()
        self.add(self.fixed)
        self._drawingarea = Gtk.DrawingArea()
        self.fixed.put(self._drawingarea, 0, 0)

        self._drawingarea.add_events(
            Gdk.EventMask.POINTER_MOTION_MASK |
            Gdk.EventMask.BUTTON_PRESS_MASK |
            Gdk.EventMask.BUTTON_RELEASE_MASK |
            Gdk.EventMask.BUTTON_MOTION_MASK)

        self.image = ImageElement(
            comicbox.pixbuf, self,
            comicbox.img_x, comicbox.img_y,
            comicbox.img_w, comicbox.img_h)

        self._drawingarea.connect("draw", self.draw_cb)
        self.connect("button_press_event", self.pressing)
        self.connect("motion_notify_event", self.mouse_move)
        self.connect("motion_notify_event", self.moving)
        self.connect("button_release_event", self.releassing)
        self.redraw()

        def size_allocate(widget, allocation):
            self.fixed.set_size_request(self.width, self.height)
            self._drawingarea.set_size_request(self.width, self.height)

        self.connect('size_allocate', size_allocate)
        self.set_size_request(self.width, self.height)
        self.fixed.set_size_request(self.width, self.height)
        self._drawingarea.set_size_request(self.width, self.height)
        self.show_all()

    def redraw(self):
        self._drawingarea.queue_draw()

    def draw_cb(self, widget, context):
        self.image.draw(context)

    def pressing(self, widget, event):
        if self.image.is_selected(event.x, event.y):
            if self.image.is_in_point(event.x, event.y):
                self.is_resize = True
                self.is_move = False
            else:
                self.is_resize = False
                self.is_move = True
        self.start_x = event.x
        self.start_y = event.y

    def mouse_move(self, widget, event):
        cursor = None
        if self.image.is_in_point(event.x, event.y):
            cursor = Gdk.Cursor(Gdk.CursorType.SIZING)
        elif self.image.is_selected(event.x, event.y):
            cursor = Gdk.Cursor(Gdk.CursorType.HAND2)
        self.get_window().set_cursor(cursor)

    def moving(self, widget, event):
        if self.is_move:
            x_movement = event.x - self.start_x
            y_movement = event.y - self.start_y
            self.image.move(x_movement, y_movement, self.get_allocation())
            self.start_x = event.x
            self.start_y = event.y
            self.redraw()
        elif self.is_resize:
            x_movement = event.x - self.start_x
            y_movement = event.y - self.start_y
            self.image.resize(x_movement, y_movement, self.get_allocation(),
                              self.start_x, self.start_y)
            self.start_x = event.x
            self.start_y = event.y
            self.redraw()

    def releassing(self, widget, event):
        self.is_resize = False
        self.is_move = False
        self.start_x = -1
        self.start_y = -1


class ImageEditorView(BaseWindow):

    def __init__(self, comicbox):
        BaseWindow.__init__(self, comicbox.width,
                            comicbox.height + style.GRID_CELL_SIZE)

        self.toolbar = BasicToolbar(
            'contract-coordinates',
            _('Drag to move or resize using the marked corners'))
        self.toolbar.stop.connect('clicked', self.__stop_clicked_cb)
        self.toolbar.confirm.connect('clicked', self.__ok_clicked_cb)

        self.comicbox = comicbox
        self.canvas = CanvasEditor(
            self.comicbox, self.comicbox.width,
            self.comicbox.height, self)

        self.vbox = Gtk.VBox()
        self.vbox.pack_start(self.toolbar, False, False, 0)
        self.vbox.pack_start(self.canvas, True, True, 0)
        self.add(self.vbox)

    def __stop_clicked_cb(self, button):
        self.destroy()

    def __ok_clicked_cb(self, button):
        self.comicbox.img_x = self.canvas.image.x
        self.comicbox.img_y = self.canvas.image.y
        self.comicbox.img_w = self.canvas.image.width
        self.comicbox.img_h = self.canvas.image.height
        self.comicbox.redraw()
        self.destroy()