-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMainWindow.py
195 lines (158 loc) · 7 KB
/
MainWindow.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
# -*- coding: utf-8 -*-
"""Python - GTK - PyGObject."""
import pathlib
import sys
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
BASE_DIR = pathlib.Path(__file__).resolve().parent
PDF_FILE = str(BASE_DIR.joinpath('file-name.pdf'))
TEXT = """<span size="xx-large">Lorem</span>
Lorem <b>ipsum</b> <span foreground="red">dolor</span> <big>sit</big> amet,
<i>consectetur</i> adipiscing <s>elit</s>, sed do
<span background="green">eiusmod</span> tempor incididunt
<small>ut</small> <tt>labore</tt> et dolore magna aliqua.\n"""
@Gtk.Template(filename=str(BASE_DIR.joinpath('MainWindow.ui')))
class ExampleWindow(Gtk.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
pango_layout = None
text_buffer = Gtk.Template.Child('text_buffer')
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Auxiliary variable with paper settings.
self.page_setup = self._page_setup()
# self.page_setup = self.custom_page_setup()
# Variable for the paper configuration dialog:
self.print_settings = Gtk.PrintSettings.new()
# Adding rendered text to Gtk.TextView.
text_buffer_iter = self.text_buffer.get_end_iter()
self.text_buffer.insert_markup(
iter=text_buffer_iter,
markup=TEXT,
len=-1,
)
@Gtk.Template.Callback()
def on_begin_print(self, print_operation, print_context):
self.pango_layout = print_context.create_pango_layout()
self.pango_layout.set_markup(text=TEXT, length=-1)
self.pango_layout.set_font_description(
desc=Pango.FontDescription('Arial 12'),
)
@Gtk.Template.Callback()
def on_draw_page(self, print_operation, print_context, page_nr):
cairo_context = print_context.get_cairo_context()
cairo_context.set_source_rgb(0, 0, 0)
PangoCairo.show_layout(cr=cairo_context, layout=self.pango_layout)
@Gtk.Template.Callback()
def on_button_open_print_dialog_clicked(self, widget):
print_operation = Gtk.PrintOperation.new()
print_operation.set_n_pages(n_pages=1)
print_operation.set_print_settings(print_settings=self.print_settings)
print_operation.set_default_page_setup(
default_page_setup=self.page_setup
)
print_operation.connect('begin-print', self.on_begin_print)
print_operation.connect('draw-page', self.on_draw_page)
response = print_operation.run(
parent=self,
action=Gtk.PrintOperationAction.PRINT_DIALOG,
)
self._check_print_dialog_response(response=response)
@Gtk.Template.Callback()
def on_button_open_preview_clicked(self, widget):
print_operation = Gtk.PrintOperation.new()
print_operation.set_n_pages(n_pages=1)
print_operation.set_print_settings(print_settings=self.print_settings)
print_operation.set_default_page_setup(
default_page_setup=self.page_setup
)
print_operation.connect('begin-print', self.on_begin_print)
print_operation.connect('draw-page', self.on_draw_page)
response = print_operation.run(
action=Gtk.PrintOperationAction.PREVIEW, parent=self
)
self._check_print_dialog_response(response=response)
@Gtk.Template.Callback()
def on_button_open_page_setup_dialog_clicked(self, widget):
print(self.page_setup.get_page_width(unit=Gtk.Unit.MM))
self.page_setup = Gtk.print_run_page_setup_dialog(
parent=self,
page_setup=self.page_setup,
settings=self.print_settings,
)
print(self.page_setup.get_page_width(unit=Gtk.Unit.MM))
@Gtk.Template.Callback()
def on_button_export_to_pdf_clicked(self, widget):
print_operation = Gtk.PrintOperation.new()
print_operation.set_n_pages(n_pages=1)
print_operation.set_print_settings(print_settings=self.print_settings)
print_operation.set_default_page_setup(
default_page_setup=self.page_setup
)
print_operation.connect('begin-print', self.on_begin_print)
print_operation.connect('draw-page', self.on_draw_page)
print_operation.set_export_filename(PDF_FILE)
response = print_operation.run(
action=Gtk.PrintOperationAction.EXPORT, parent=self
)
if response == Gtk.PrintOperationResult.APPLY:
print('Arquivo exportado com sucesso')
def _page_setup(self):
paper_size = Gtk.PaperSize.new(name=Gtk.PAPER_NAME_A4)
page_setup = Gtk.PageSetup.new()
page_setup.set_paper_size_and_default_margins(size=paper_size)
return page_setup
def _custom_page_setup(self):
paper_size = Gtk.PaperSize.new(name=Gtk.PAPER_NAME_A4)
custom_page_setup = Gtk.PageSetup.new()
custom_page_setup.set_top_margin(margin=20, unit=Gtk.Unit.MM)
custom_page_setup.set_left_margin(margin=20, unit=Gtk.Unit.MM)
custom_page_setup.set_orientation(
orientation=Gtk.PageOrientation.PORTRAIT,
)
custom_page_setup.set_paper_size(size=paper_size)
return custom_page_setup
def _check_print_dialog_response(self, response):
if response == Gtk.PrintOperationResult.ERROR:
print('ERROR')
elif response == Gtk.PrintOperationResult.APPLY:
print('APPLY')
elif response == Gtk.PrintOperationResult.CANCEL:
print('CANCEL')
elif response == Gtk.PrintOperationResult.IN_PROGRESS:
print('IN_PROGRESS')
class ExampleApplication(Gtk.Application):
def __init__(self):
super().__init__(
application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE,
)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)