ADG Lua 

ADG Lua Commit Details

Date:2013-03-16 18:52:39 (6 years 5 months ago)
Author:Nicola Fontana
Branch:master
Commit:e85424e9dc85718429e5991ed90ca354003a576b
Parents: 4e11e0fb7ecf1c12c4709ad1971398f520bda5c2
Message:Completed "Save as" dialog

Changes:
Madg-demo.lua.in (3 diffs)
Mpiston.lua (4 diffs)

File differences

adg-demo.lua.in
3333
3434
3535
36
3637
3738
3839
......
6263
6364
6465
65
66
67
6668
6769
6870
6971
7072
7173
74
75
76
77
78
79
80
81
7282
7383
7484
......
8292
8393
8494
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
85137
86138
87
88
89
90
91
92139
93140
94141
end
local lgi = require 'lgi'
local GLib = lgi.require 'GLib'
local Gtk = lgi.require 'Gtk'
local Adg = lgi.require 'Adg'
local Piston = require 'piston'
ZGROOVE = 16,
}
Adg.gtk_use_default_icons(adg_data_dir);
Adg.gtk_use_default_icons(adg_data_dir)
-- GtkBuilder initialization
local builder = Gtk.Builder()
builder:add_from_file(adg_data_dir .. '/adg-demo.ui')
local ui = builder.objects
-- Canvas settings
local canvas = piston.view.detailed
canvas:set_paper('iso_a4', Gtk.PageOrientation.LANDSCAPE)
ui.mainCanvas:set_canvas(canvas)
canvas:autoscale()
-- About dialog
ui.mainAbout.on_clicked = function ()
local dialog = ui.wndAbout
dialog.on_response = Gtk.Widget.hide
dialog:run()
end
-- Save as dialog
ui.mainSaveAs.on_clicked = function ()
local dialog = ui.wndSaveAs
-- Set the default destination file and directory
local dir = nil
if GLib.UserDirectory then
-- Issue: http://github.com/pavouk/lgi/issues/45
local dir_type = GLib.UserDirectory.DOCUMENTS or
GLib.UserDirectory.DIRECTORY_DOCUMENTS
dir = GLib.get_user_special_dir(dir_type)
end
dir = dir or GLib.get_home_dir()
dialog:set_current_folder(dir)
dialog:set_current_name('adg-demo')
-- Response handling
dialog.on_response = function (dialog, response)
if response == Gtk.ResponseType.OK then
-- Retrieve the file suffix (format type)
local suffix
for _, radio in pairs(ui.saveAsPng:get_group()) do
if radio:get_active() then
suffix = radio:get_tooltip_markup()
break
end
end
local file = dialog:get_filename()
if not GLib.str_has_suffix(file, suffix) then
file = file .. suffix
end
canvas:export(file)
end
dialog:hide()
end
dialog:run()
end
-- Quit command
ui.mainQuit.on_clicked = Gtk.main_quit
-- Canvas settings
local canvas = piston.view.detailed
canvas:set_paper('iso_a4', Gtk.PageOrientation.LANDSCAPE)
ui.mainCanvas:set_canvas(canvas)
canvas:autoscale()
-- Main window
local window = ui.wndMain
piston.lua
2525
2626
2727
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2844
2945
3046
......
339355
340356
341357
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
342403
343404
344405
......
350411
351412
352413
353
414
354415
355416
356417
......
402463
403464
404465
466
405467
local SQRT3 = math.sqrt(3)
-- Backward compatibility
if not cairo.Status.to_string then
-- Pull request: http://github.com/pavouk/lgi/pull/44
local core = require 'lgi.core'
local ffi = require 'lgi.ffi'
local ti = ffi.types
cairo._enum.Status.to_string = core.callable.new {
addr = cairo._module.cairo_status_to_string,
ret = ti.utf8,
ti.int
}
end
-- MODEL
-----------------------------------------------------------------
local view = {}
-- Inject the export method into Adg.Canvas
rawset(Adg.Canvas, 'export', function (canvas, file)
-- The export format is guessed from the file suffix
local suffix = file:match('%..*$')
local size = canvas:get_size()
size.x = size.x + canvas:left_margin() + canvas:right_margin()
size.y = size.y + canvas:top_margin() + canvas:bottom_margin()
-- Create the cairo surface
local surface
if suffix == '.png' and cairo.ImageSurface then
surface = cairo.ImageSurface.create(cairo.Format.RGB24, size.x, size.y)
elseif suffix == '.svg' and cairo.SvgSurface then
surface = cairo.SvgSurface.create(file, size.x, size.y)
elseif suffix == '.pdf' and cairo.PdfSurface then
surface = cairo.PdfSurface.create(file, size.x, size.y)
elseif suffix == '.ps' and cairo.PsSurface then
-- Pull request: http://github.com/pavouk/lgi/pull/46
surface = cairo.PsSurface.create(file, size.x, size.y)
surface:dsc_comment('%%Title: adg-lua demonstration program')
surface:dsc_comment('%%Copyright: Copyleft (C) 2013 Fontana Nicola')
surface:dsc_comment('%%Orientation: Landscape')
surface:dsc_begin_setup()
surface:dsc_begin_page_setup()
surface:dsc_comment('%%IncludeFeature: *PageSize A4')
end
if not surface then return nil, 'Requested format not supported' end
-- Render the canvas content
local cr = cairo.Context.create(surface);
canvas:render(cr)
local status
if cairo.Surface.get_type(surface) == 'IMAGE' then
status = cairo.Surface.write_to_png(surface, file)
else
cr:show_page()
status = cr.status
end
if status ~= 'SUCCESS' then
return nil, cairo.Status.to_string(cairo.Status[status])
end
end)
function view.detailed(model)
local canvas = Adg.Canvas {
title_block = Adg.TitleBlock {
projection = Adg.Projection { scheme = Adg.ProjectionScheme.FIRST_ANGLE },
scale = '---',
size = 'A4',
}
},
}
canvas:add(Adg.Stroke { trail = model.body })
return part
end
return controller

Archive Download the corresponding diff file

Branches

Tags