ADG Lua 

ADG Lua Commit Details

Date:2013-03-28 16:42:51 (6 years 8 months ago)
Author:Nicola Fontana
Branch:master
Commit:ad93bbee05771cd2a2a5b2fc1f9ed33dcfc7f240
Parents: a99f492488da19bf7a18adf09abaaac1ec07e2fb
Message:adg-demo: improved UI initialization

There is no need (and in some case it is plain wrong) to initialize the
interface in the "clicked" callback: it will be called multiple times.
Changes:
Madg-demo.lua.in (9 diffs)

File differences

adg-demo.lua.in
4242
4343
4444
45
4546
4647
4748
......
6566
6667
6768
69
6870
6971
7072
73
74
75
76
77
78
79
7180
7281
82
7383
7484
7585
......
8494
8595
8696
97
8798
8899
89100
......
120131
121132
122133
134
123135
124136
125137
......
127139
128140
129141
142
130143
131144
132145
......
143156
144157
145158
146
159
147160
148161
149162
150163
164
151165
152
153
154
155
156
166
157167
158168
159169
170
160171
161
162
163
172
173
174
164175
165
166
167
168
176
177
178
179
180
169181
170
171
172182
173183
174184
175185
186
187
188
189
190
176191
177192
178193
......
198213
199214
200215
201
202
203
204
205
206
207
208
216
217
218
219
209220
210221
211222
......
221232
222233
223234
224
225
235
226236
227237
228238
229239
230240
231241
242
232243
233
234
235
236
237
238
239
240
244
245
246
247
248
249
250
251
252
241253
242
243
244
245
246
247
248
249
254
255
256
257
258
259
260
250261
262
251263
252
253
254
255
256
257
258
264
265
266
267
268
269
270
259271
260
261
272
273
262274
263
264
275
276
265277
266
267
268
269
270
271
272
273
278
279
280
281
282
283
284
274285
275
276
277286
278287
279
280
288
281289
282290
291
283292
284
285293
286
287
294
295
288296
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
297
298
299
300
301
302
303
304
305
306
307
308
309
310
318311
319
320312
321313
322
314
315
316
317
318
319
320
321
322
323323
324324
325325
326326
327
327328
328329
329330
......
372373
373374
374375
376
375377
376378
377379
378380
379
381
382
380383
381384
382385
383386
384387
385
386388
-- Command line parsing
-----------------------------------------------------------------
if arg[1] == '--version' or arg[1] == '-V' then
print('adg-demo @VERSION@')
-- User interface initialization
-----------------------------------------------------------------
Adg.gtk_use_default_icons(adg_data_dir)
local function opener(button, dialog)
button.on_clicked = function ()
dialog:run()
Adg.gtk_window_hide_here(dialog)
end
end
-- Customize error handler
-----------------------------------------------------------------
error = function (message)
local dialog = Gtk.MessageDialog {
-- Part definition
-----------------------------------------------------------------
local piston = Piston.new {
A = 55,
-- GtkBuilder initialization
-----------------------------------------------------------------
local builder = Gtk.Builder()
builder:add_from_file(adg_data_dir .. '/adg-demo.ui')
-- Canvas settings
-----------------------------------------------------------------
local canvas = piston.view.detailed
canvas:set_paper('iso_a4', Gtk.PageOrientation.LANDSCAPE)
area:queue_draw()
end
return FALSE;
return false
end
-- Help dialog
-----------------------------------------------------------------
ui.mainHelp.on_clicked = function ()
local dialog = ui.wndHelp
dialog.on_response = Gtk.Widget.hide
dialog:run()
end
opener(ui.mainHelp, ui.wndHelp)
-- About dialog
-----------------------------------------------------------------
ui.mainAbout.on_clicked = function ()
local dialog = ui.wndAbout
local icon_list = Gtk.Window.get_default_icon_list()
do
local dialog = ui.wndAbout
opener(ui.mainAbout, dialog)
-- The last icon is supposed to be the largest one:
-- check adg_gtk_use_default_icons() implementation.
local last_icon = icon_list[#icon_list]
dialog:set_logo(last_icon)
-- The last icon is supposed to be the largest one:
-- check adg_gtk_use_default_icons() implementation.
local icon_list = Gtk.Window.get_default_icon_list()
local last_icon = icon_list[#icon_list]
dialog:set_logo(last_icon)
dialog.on_response = Gtk.Widget.hide
dialog:run()
end
-- Edit dialog
-----------------------------------------------------------------
do
local dialog = ui.wndEdit
opener(ui.mainEdit, dialog)
local function entry_info(widget)
if Gtk.ToggleButton:is_type_of(widget) then
end
end
ui.editGROOVE.on_toggled = function (self)
local toggled = self:get_active()
ui.editZGROOVE:set_sensitive(toggled)
ui.editZGROOVELabel:set_sensitive(toggled)
ui.editDGROOVE:set_sensitive(toggled)
ui.editDGROOVELabel:set_sensitive(toggled)
ui.editLGROOVE:set_sensitive(toggled)
ui.editLGROOVELabel:set_sensitive(toggled)
local function lock_ui(status)
local sensitiveness = status == false
ui.btnApply:set_sensitive(sensitiveness)
ui.btnReset:set_sensitive(sensitiveness)
end
for field in pairs(piston.data) do
-- Unlock the UI on every widget change
local getter, _, notification = entry_info(widget)
widget[notification] = function ()
ui.btnApply:set_sensitive(true)
ui.btnReset:set_sensitive(true)
lock_ui(false)
end
piston.data[field] = widget[getter](widget)
end
end
update_title_block()
dialog:set_position(Gtk.WindowPosition.MOUSE)
ui.mainEdit.on_clicked = function ()
local dialog = ui.wndEdit
dialog:set_position(Gtk.WindowPosition.MOUSE);
local function lock_ui()
ui.btnApply:set_sensitive(false)
ui.btnReset:set_sensitive(false)
end
ui.editGROOVE.on_toggled = function (self)
local toggled = self:get_active()
ui.editZGROOVE:set_sensitive(toggled)
ui.editZGROOVELabel:set_sensitive(toggled)
ui.editDGROOVE:set_sensitive(toggled)
ui.editDGROOVELabel:set_sensitive(toggled)
ui.editLGROOVE:set_sensitive(toggled)
ui.editLGROOVELabel:set_sensitive(toggled)
end
ui.btnApply.on_clicked = function ()
-- Refresh the piston data using the widgets
for field in pairs(piston.data) do
local widget = ui['edit' .. field]
local getter = entry_info(widget)
if getter then
piston.data[field] = widget[getter](widget)
end
ui.btnApply.on_clicked = function ()
-- Refresh the piston data using the widgets
for field in pairs(piston.data) do
local widget = ui['edit' .. field]
local getter = entry_info(widget)
if getter then
piston.data[field] = widget[getter](widget)
end
end
-- Regenerate all the models
for _, model in pairs(piston.model) do
model:reset()
model:regenerate(piston)
model:changed()
end
update_title_block()
-- Regenerate all the models
for _, model in pairs(piston.model) do
model:reset()
model:regenerate(piston)
model:changed()
end
update_title_block()
-- Refresh the GTK+ interface
area:queue_draw()
-- Refresh the GTK+ interface
area:queue_draw()
lock_ui()
end
lock_ui()
end
ui.btnReset.on_clicked = function ()
-- Update the widgets using the piston data
for field, value in pairs(piston.data) do
local widget = ui['edit' .. field]
local _, setter = entry_info(widget)
if setter then
widget[setter](widget, value)
end
ui.btnReset.on_clicked = function ()
-- Update the widgets using the piston data
for field, value in pairs(piston.data) do
local widget = ui['edit' .. field]
local _, setter = entry_info(widget)
if setter then
widget[setter](widget, value)
end
lock_ui()
end
dialog.on_response = Adg.gtk_window_hide_here
dialog:run()
lock_ui()
end
end
-- Save as dialog
ui.mainSaveAs.on_clicked = function ()
local dialog = ui.wndSaveAs
-- Save as dialog
-----------------------------------------------------------------
-- 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
_, err = canvas:export(file)
if err then error(err) end
do
local dialog = ui.wndSaveAs
opener(ui.mainSaveAs, dialog)
dialog:set_current_name('adg-demo')
dialog.on_response = function (dialog, response)
if response ~= Gtk.ResponseType.OK then return end
-- 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
dialog:hide()
end
dialog:run()
-- Forcibly append the proper suffix, if not yet present
local file = dialog:get_filename()
if not GLib.str_has_suffix(file, suffix) then
file = file .. suffix
end
_, err = canvas:export(file)
if err then error(err) end
end
end
-- Print dialog
-----------------------------------------------------------------
do
local settings
-- Quit command
-----------------------------------------------------------------
ui.mainQuit.on_clicked = Gtk.main_quit
-- Main window
-- Main
-----------------------------------------------------------------
local window = ui.wndMain
window.on_delete_event = Gtk.main_quit
window:show_all()
Gtk.main()

Archive Download the corresponding diff file

Branches

Tags