File: graphics_interact.enaml

package info (click to toggle)
python-enamlx 0.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 388 kB
  • sloc: python: 3,338; makefile: 18
file content (99 lines) | stat: -rw-r--r-- 3,362 bytes parent folder | download
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
import math
import enamlx
enamlx.install()
from enaml.core.api import Looper
from enamlx.widgets.api import (
    GraphicsView, GraphicsItem, GraphicsTextItem, GraphicsRectItem,
    GraphicsPolygonItem, GraphicsEllipseItem, GraphicsLineItem,
    GraphicsPathItem, GraphicsItemGroup, Pen, Brush, Point
)
from enaml.widgets.api import (
    MainWindow, Container, PushButton, CheckBox, RadioButton, SpinBox,
    Menu, Action, MenuBar, Feature, ObjectCombo, Form, Label
)
from enaml.drag_drop import DragData, DropAction
from enaml.qt.QtGui import QPainterPath
from enaml.qt.QtCore import Qt


def create_drag_data(data):
    drag = DragData()
    drag.supported_actions = DropAction.Copy
    drag.mime_data.set_data('text/plain', data)
    return drag


enamldef Main(MainWindow): window:
    attr blue_pen = Pen(color='blue')
    attr red_pen = Pen(color='red')
    attr green_brush = Brush(color='green')
    MenuBar:
        Menu:
            title = "&File"
            Action:
                text = "Quit\tCtrl+Q"
                triggered :: raise SystemExit(0)
    Container:
        Form:
            Label:
                text = "Drag mode:"
            ObjectCombo:
                items = list(canvas.get_member('drag_mode').items)
                selected := canvas.drag_mode
        GraphicsView: canvas:
            attr size = (960, 960)
            background = "#fff"
            drag_mode = 'selection'
            minimum_size = size
            attr last_point = None

            Menu:
                context_menu = True
                Action:
                    text = 'Reset'
                    triggered :: canvas.reset_view()

            mouse_press_event => (evt):
                self.last_point = evt.pos()
            mouse_move_event => (evt):
                if self.last_point is None:
                    self.last_point = evt.pos()
                if self.selected_items or self.drag_mode == 'selection':
                    return
                dp = evt.pos()-self.last_point
                delta = Point(dp.x(), dp.y())
                self.last_point = evt.pos()

                if evt.buttons() in (Qt.MidButton, Qt.LeftButton):
                    px = self.pixel_density()
                    tr = -delta*px
                    self.translate_view(tr.x, tr.y)

            wheel_event => (evt):
                if hasattr(evt, 'delta'):
                    d = evt.delta()
                else:
                    d = evt.angleDelta().y()
                s = 1.001 ** d
                self.scale_view(s, s)
            selected_items ::
                old = change['oldvalue']
                new = change['value']
                for item in old:
                    item.pen = blue_pen
                for item in new:
                    item.pen = red_pen
            Looper:
                iterable = range(100)
                GraphicsTextItem:
                    position = (0, loop_index*20)
                    pen = blue_pen
                    movable = True
                    selectable = True
                    text << "{}".format(position)
                    features = Feature.DragEnabled
                    drag_start => ():
                        return create_drag_data(b"Hello")
                    drag_end => (drag_data, result):
                        print(drag_data, result)