ADG Lua 

ADG Lua Git Source Tree

Root/adg-demo.lua.in

1--[[
2
3This file is part of adg-lua.
4Copyright (C) 2012-2013 Nicola Fontana <ntd at entidi.it>
5
6adg-lua is free software; you can redistribute it and/or modify
7it under the terms of the GNU Lesser General Public License as
8published by the Free Software Foundation; either version 2 of
9the License, or (at your option) any later version.
10
11adg-lua is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Lesser General Public License for more details.
15
16You should have received a copy of the GNU Lesser General
17Public License along with adg-lua; if not, write to the Free
18Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19Boston, MA 02110-1301, USA.
20
21]]
22
23
24local lgi = require 'lgi'
25local GLib = lgi.require 'GLib'
26local Gtk = lgi.require 'Gtk'
27local Adg = lgi.require 'Adg'
28local Piston = require 'piston'
29
30
31-- Command line parsing
32-----------------------------------------------------------------
33
34if arg[1] == '--version' or arg[1] == '-V' then
35 print('adg-demo @VERSION@')
36 return 0
37elseif arg[1] == '--help' or arg[1] == '-h' then
38 print([[
39Usage:
40 ]] .. arg[0] .. [[ [OPTION...] - ADG demonstration program
41
42 -h, --help Show help options
43 -V, --version Display version information
44 -E, --show-extents Show the boundary boxes of every entity
45]])
46 return 0
47elseif arg[1] == '--show-extents' or arg[1] == '-E' then
48 Adg.switch_extents(true)
49elseif arg[1] then
50 error('Invalid argument (' .. arg[1] .. ')')
51 return 1
52end
53
54
55-- Customize error handler
56-----------------------------------------------------------------
57
58error = function (message)
59 local dialog = Gtk.MessageDialog {
60message_type = Gtk.MessageType.ERROR,
61buttons = Gtk.ButtonsType.CLOSE,
62text = message,
63title = 'Error from adg-demo',
64 }
65 dialog:run()
66 dialog:destroy()
67end
68
69
70-- Part definition
71-----------------------------------------------------------------
72
73local piston = Piston.new {
74 A = 55,
75 B = 20.6,
76 C = 2,
77 DHOLE = 2,
78 LHOLE = 3,
79 D1 = 9.3,
80 D2 = 6.5,
81 D3 = 13.8,
82 D4 = 6.5,
83 D5 = 4.5,
84 D6 = 7.2,
85 D7 = 2,
86 RD34 = 1,
87 RD56 = 0.2,
88 LD2 = 7,
89 LD3 = 3.5,
90 LD5 = 5,
91 LD6 = 1,
92 LD7 = 0.5,
93 GROOVE = false,
94 ZGROOVE = 16,
95 DGROOVE = 8.3,
96 LGROOVE = 1,
97 CHAMFER = 0.3,
98
99 -- Metadata
100 TITLE = 'SAMPLE DRAWING',
101 DRAWING = 'PISTON',
102 AUTHOR = 'adg-demo',
103 DATE = os.date('%d/%m/%Y'),
104}
105
106
107-- GtkBuilder initialization
108-----------------------------------------------------------------
109
110local ui
111
112do
113 -- Path where the ADG library is installed: used for accessing
114 -- shared resources such as icons and GtkBuilder XML files.
115 local adg_data_dir = '@ADGDATADIR@'
116
117 -- This hack checks if ADGDATADIR has been expanded by
118 -- configure and provides a fallback otherwise. It allows to
119 -- directly run adg-demo.lua.in if ADG is installed in the
120 -- /usr prefix (as it happens to be on my system ;).
121 if adg_data_dir == '@ADGDATADIR' .. '@' then
122adg_data_dir = '/usr/share/adg'
123 end
124
125 Adg.gtk_use_default_icons(adg_data_dir)
126
127 local builder = Gtk.Builder()
128 builder:add_from_file(adg_data_dir .. '/adg-demo.ui')
129 ui = builder.objects
130end
131
132local function opener(button, dialog)
133 dialog:set_transient_for(ui.wndMain)
134
135 button.on_clicked = function ()
136dialog:run()
137Adg.gtk_window_hide_here(dialog)
138 end
139end
140
141
142-- Canvas settings
143-----------------------------------------------------------------
144
145local canvas = piston.view.detailed
146canvas:set_paper('iso_a4', Gtk.PageOrientation.LANDSCAPE)
147canvas:autoscale()
148
149local area = ui.mainCanvas
150area:set_canvas(canvas)
151area.on_button_press_event = function (area, event)
152 if event.button == 1 then
153 -- Restore the original zoom
154 area:reset()
155 elseif event.button == 3 then
156 canvas:autoscale()
157 area:queue_draw()
158 end
159
160 return false
161end
162
163
164-- Help dialog
165-----------------------------------------------------------------
166
167opener(ui.mainHelp, ui.wndHelp)
168
169
170-- About dialog
171-----------------------------------------------------------------
172
173do
174local dialog = ui.wndAbout
175opener(ui.mainAbout, dialog)
176
177-- The last icon is supposed to be the largest one:
178-- check adg_gtk_use_default_icons() implementation.
179local icon_list = Gtk.Window.get_default_icon_list()
180local last_icon = icon_list[#icon_list]
181dialog:set_logo(last_icon)
182
183end
184
185
186-- Edit dialog
187-----------------------------------------------------------------
188
189do
190local dialog = ui.wndEdit
191opener(ui.mainEdit, dialog)
192dialog:set_position(Gtk.WindowPosition.MOUSE)
193
194local function entry_info(widget)
195 if Gtk.ToggleButton:is_type_of(widget) then
196-- Boolean value
197return 'get_active', 'set_active', 'on_toggled'
198 elseif Gtk.Entry:is_type_of(widget) then
199-- String value
200return 'get_text', 'set_text', 'on_changed'
201 elseif Gtk.SpinButton:is_type_of(widget) then
202-- Number value
203return 'get_value', 'set_value', 'on_changed'
204 else
205-- Unhandled type
206return
207 end
208end
209
210local function lock_ui(status)
211 local sensitiveness = status == false
212 ui.btnApply:set_sensitive(sensitiveness)
213 ui.btnReset:set_sensitive(sensitiveness)
214end
215
216for field in pairs(piston.data) do
217 local widget = ui['edit' .. field]
218 if widget then
219-- Synchronize GtkSpinButton to GtkAdjustment, that is
220-- initialize the widget to its default value
221if Gtk.SpinButton:is_type_of(widget) then
222 local adj = widget:get_adjustment()
223 adj:value_changed()
224end
225
226-- Unlock the UI on every widget change
227local getter, _, notification = entry_info(widget)
228widget[notification] = function ()
229 lock_ui(false)
230end
231piston.data[field] = widget[getter](widget)
232 end
233end
234
235piston:refresh()
236
237ui.editGROOVE.on_toggled = function (self)
238 local toggled = self:get_active()
239 ui.editZGROOVE:set_sensitive(toggled)
240 ui.editZGROOVELabel:set_sensitive(toggled)
241 ui.editDGROOVE:set_sensitive(toggled)
242 ui.editDGROOVELabel:set_sensitive(toggled)
243 ui.editLGROOVE:set_sensitive(toggled)
244 ui.editLGROOVELabel:set_sensitive(toggled)
245end
246
247ui.btnApply.on_clicked = function ()
248 -- Refresh the piston data using the widgets
249 for field in pairs(piston.data) do
250local widget = ui['edit' .. field]
251local getter = entry_info(widget)
252if getter then
253 piston.data[field] = widget[getter](widget)
254end
255 end
256
257 piston:refresh()
258 area:queue_draw()
259 lock_ui()
260end
261
262ui.btnReset.on_clicked = function ()
263 -- Update the widgets using the piston data
264 for field, value in pairs(piston.data) do
265local widget = ui['edit' .. field]
266local _, setter = entry_info(widget)
267if setter then
268 widget[setter](widget, value)
269end
270 end
271
272 lock_ui()
273end
274
275end
276
277
278-- Save as dialog
279-----------------------------------------------------------------
280
281do
282local dialog = ui.wndSaveAs
283opener(ui.mainSaveAs, dialog)
284dialog:set_current_name('adg-demo')
285
286dialog.on_response = function (dialog, response)
287 if response ~= Gtk.ResponseType.OK then return end
288
289 -- Retrieve the file suffix (format type)
290 local suffix = ''
291 for _, radio in pairs(ui.saveAsPng:get_group()) do
292if radio:get_active() then
293 suffix = radio:get_tooltip_markup()
294 break
295end
296 end
297
298 -- Forcibly append the proper suffix, if not yet present
299 local file = dialog:get_filename()
300 if not GLib.str_has_suffix(file, suffix) then
301file = file .. suffix
302 end
303 _, err = canvas:export(file)
304 if err then error(err) end
305end
306
307end
308
309
310-- Print dialog
311-----------------------------------------------------------------
312
313do
314local settings
315
316ui.mainPrint.on_clicked = function ()
317 local operation = Gtk.PrintOperation.new()
318
319 operation:set_use_full_page(operation, false)
320 operation:set_unit(Gtk.Unit.POINTS)
321
322 if settings then
323operation:set_print_settings(settings)
324 end
325
326 if Gtk.PrintOperation.set_embed_page_setup then
327operation:set_embed_page_setup(true)
328 end
329
330 local page_setup = canvas:get_page_setup()
331 if page_setup then
332 operation:set_default_page_setup(page_setup)
333 end
334
335 operation.on_begin_print = function ()
336operation:set_n_pages(1)
337 end
338
339 operation.on_draw_page = function (operation, context)
340local cr = context:get_cairo_context()
341
342local old_map = canvas:get_global_map()
343canvas:set_global_map(Adg.matrix_identity())
344canvas:render(cr)
345canvas:set_global_map(old_map)
346 end
347
348 local result, err = operation:run(Gtk.PrintOperationAction.PRINT_DIALOG, ui.wndMain)
349 if result == Gtk.PrintOperationResult.APPLY then
350 settings = operation:get_print_settings()
351 elseif err then
352error(err)
353 end
354end
355
356end
357
358
359-- Quit command
360-----------------------------------------------------------------
361
362ui.mainQuit.on_clicked = Gtk.main_quit
363
364
365-- Main
366-----------------------------------------------------------------
367
368local window = ui.wndMain
369window.on_delete_event = Gtk.main_quit
370window:show_all()
371
372Gtk.main()

Archive Download this file

Branches

Tags