-
Notifications
You must be signed in to change notification settings - Fork 34
/
part2.py
269 lines (206 loc) · 8.53 KB
/
part2.py
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
import sys
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, Gio, Gdk, Graphene, GLib
class Custom(Gtk.Widget):
def __init__(self):
super().__init__()
self.set_size_request(30, 30)
def do_snapshot(self, s):
#s.save()
print("sn")
red = Gdk.RGBA()
# red.red = 1.
# red.green = 0.
# red.blue = 0.
# red.alpha = 1.
r = Graphene.Rect()
r.init(0, 0, 70, 70)
print(r)
print(r.get_height())
red.red = 1
red.alpha = 1
print(red.to_string())
s.append_color(red, r)
#s.restore()
def do_measure(self, orientation, for_size):
print("m")
return 50, 50, -1, -1
pass
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_default_size(600, 250)
self.set_title("MyApp")
# Main layout containers
self.box1 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.box2 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.box3 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.box2.set_spacing(10)
self.box2.set_margin_top(10)
self.box2.set_margin_bottom(10)
self.box2.set_margin_start(10)
self.box2.set_margin_end(10)
self.set_child(self.box1) # Horizontal box to window
self.box1.append(self.box2) # Put vert box in that box
self.box1.append(self.box3) # And another one, empty for now
# Add a button
self.button = Gtk.Button(label="Hello")
self.button.connect('clicked', self.hello)
self.box2.append(self.button) # But button in the first of the two vertical boxes
# Add a check button
self.check = Gtk.CheckButton(label="And goodbye?")
self.box2.append(self.check)
# Add a box containing a switch and label
self.switch_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.switch_box.set_spacing(5)
self.switch = Gtk.Switch()
self.switch.set_active(True) # Let's default it to on
self.switch.connect("state-set", self.switch_switched) # Lets trigger a function on state change
self.label = Gtk.Label(label="A switch")
self.switch_box.append(self.switch)
self.switch_box.append(self.label)
self.box2.append(self.switch_box)
self.slider = Gtk.Scale()
self.slider.set_digits(0) # Number of decimal places to use
self.slider.set_range(0, 10)
self.slider.set_draw_value(True) # Show a label with current value
self.slider.set_value(5) # Sets the current value/position
self.slider.connect('value-changed', self.slider_changed)
self.box2.append(self.slider)
self.header = Gtk.HeaderBar()
self.set_titlebar(self.header)
self.open_button = Gtk.Button(label="Open")
self.header.pack_start(self.open_button)
self.open_button.set_icon_name("document-open-symbolic")
self.open_dialog = Gtk.FileChooserNative.new(title="Choose a file",
parent=self, action=Gtk.FileChooserAction.OPEN)
self.open_dialog.connect("response", self.open_response)
self.open_button.connect("clicked", self.show_open_dialog)
f = Gtk.FileFilter()
f.set_name("Image files")
f.add_mime_type("image/jpeg")
f.add_mime_type("image/png")
self.open_dialog.add_filter(f)
# Create a new "Action"
action = Gio.SimpleAction.new("something", None)
action.connect("activate", self.print_something)
self.add_action(action) # Here the action is being added to the window, but you could add it to the
# application or an "ActionGroup"
# Create a new menu, containing that action
menu = Gio.Menu.new()
menu.append("Do Something", "win.something") # Or you would do app.grape if you had attached the
# action to the application
# Create a popover
self.popover = Gtk.PopoverMenu() # Create a new popover menu
self.popover.set_menu_model(menu)
# Create a menu button
self.hamburger = Gtk.MenuButton()
self.hamburger.set_popover(self.popover)
self.hamburger.set_icon_name("open-menu-symbolic") # Give it a nice icon
# Add menu button to the header bar
self.header.pack_start(self.hamburger)
# set app name
GLib.set_application_name("My App")
# Add an about dialog
action = Gio.SimpleAction.new("about", None)
action.connect("activate", self.show_about)
self.add_action(action) # Here the action is being added to the window, but you could add it to the
menu.append("About", "win.about")
self.dw = Gtk.DrawingArea()
# Make it fill the available space (It will stretch with the window)
self.dw.set_hexpand(True)
self.dw.set_vexpand(True)
# Instead, If we didn't want it to fill the available space but wanted a fixed size
#dw.set_content_width(100)
#dw.set_content_height(100)
self.dw.set_draw_func(self.draw, None)
self.box3.append(self.dw)
#evc = Gtk.EventController.key_new()
evk = Gtk.GestureClick.new()
evk.connect("pressed", self.dw_click) # could be "released"
self.dw.add_controller(evk)
evk = Gtk.EventControllerKey.new()
evk.connect("key-pressed", self.key_press)
self.add_controller(evk)
self.blobs = []
self.cursor_crosshair = Gdk.Cursor.new_from_name("crosshair")
self.dw.set_cursor(self.cursor_crosshair)
app = self.get_application()
sm = app.get_style_manager()
sm.set_color_scheme(Adw.ColorScheme.PREFER_DARK)
custom = Custom()
#self.box3.append(custom)
custom.set_hexpand(True)
custom.set_vexpand(True)
def show_about(self, action, param):
self.about = Gtk.AboutDialog()
self.about.set_transient_for(self)
self.about.set_modal(self)
self.about.set_authors(["Your Name"])
self.about.set_copyright("Copyright 2022 Your Full Name")
self.about.set_license_type(Gtk.License.GPL_3_0)
self.about.set_website("http://example.com")
self.about.set_website_label("My Website")
self.about.set_version("1.0")
self.about.set_logo_icon_name("org.example.example")
self.about.show()
def key_press(self, event, keyval, keycode, state):
if keyval == Gdk.KEY_q and state & Gdk.ModifierType.CONTROL_MASK:
self.close()
def show_open_dialog(self, button):
self.open_dialog.show()
def open_response(self, dialog, response):
if response == Gtk.ResponseType.ACCEPT:
file = dialog.get_file()
filename = file.get_path()
print(filename)
def dw_click(self, gesture, data, x, y):
self.blobs.append((x, y))
self.dw.queue_draw() # Force a redraw
def draw(self, area, c, w, h, data):
# c is a Cairo context
# Fill background
c.set_source_rgb(0, 0, 0)
c.paint()
c.set_source_rgb(1, 0, 1)
for x, y in self.blobs:
c.arc(x, y, 10, 0, 2 * 3.1215)
c.fill()
# Draw a line
c.set_source_rgb(0.5, 0.0, 0.5)
c.set_line_width(3)
c.move_to(10, 10)
c.line_to(w - 10, h - 10)
c.stroke()
# Draw a rectangle
c.set_source_rgb(0.8, 0.8, 0.0)
c.rectangle(20, 20, 50, 20)
c.fill()
# Draw some text
c.set_source_rgb(0.1, 0.1, 0.1)
c.select_font_face("Sans")
c.set_font_size(13)
c.move_to(25, 35)
c.show_text("Test")
def print_something(self, action, param):
print("Something!")
def slider_changed(self, slider):
print(int(slider.get_value()))
def switch_switched(self, switch, state):
print(f"The switch has been switched {'on' if state else 'off'}")
def hello(self, button):
print("Hello world")
if self.check.get_active():
print("Goodbye world!")
self.close()
class MyApp(Adw.Application):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.connect('activate', self.on_activate)
def on_activate(self, app):
self.win = MainWindow(application=app)
self.win.present()
app = MyApp(application_id="com.example.GtkApplication")
app.run(sys.argv)