-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.lua
556 lines (421 loc) · 13.4 KB
/
view.lua
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
--[[
* view.lua
*
* Display a WMO dataset in tabular format.
]]
local wx = require("wx")
local trace = require("lib.trace")
local json = require("lib.json")
local palette = require("lib.wxX11Palette")
local _format = string.format
-- ----------------------------------------------------------------------------
--
local m_trace = trace.new("view")
-- ----------------------------------------------------------------------------
-- window's private members
--
local m_App =
{
sAppName = "WMO data view",
sAppVer = "0.0.1",
sRelDate = "24/04/2020",
sFileInput = "",
}
-- ----------------------------------------------------------------------------
--
local m_GridCities =
{
-- title active order [rows] [cols] [size row] [size col]
--
{ "Forecast", true, 0, 10, 8, -1, -1 },
{ "Climate", false, 1, 16, 10, -1, -1 },
{ "Details", false, 2, 15, 1, 240, 340 },
{ "Member", false, 3, 7, 1, 240, 400 },
}
local m_GridRegions =
{
{ "Africa", true, 0, 250, 4, 300, 400 },
{ "Asia", false, 1, 250, 4, 300, 400 },
{ "South America", false, 2, 250, 4, 300, 400 },
{ "North and Central America", false, 3, 250, 4, 300, 400 },
{ "South-West Pacific", false, 4, 250, 4, 300, 400 },
{ "Europe", false, 5, 250, 4, 300, 400 },
}
-- ----------------------------------------------------------------------------
--
local m_Frame =
{
hWindow = nil, -- main frame
hStatusBar = nil, -- the status bar
notebook = nil,
gridSet = nil, -- which grid set is in use
}
-- ----------------------------------------------------------------------------
--
local m_GridAttrs =
{
font = nil,
attrOdd = nil,
attrEven = nil,
}
-- ----------------------------------------------------------------------------
-- Simple interface to pop up a message
--
local function DlgMessage(message)
wx.wxMessageBox(message, m_App.sAppName,
wx.wxOK + wx.wxICON_INFORMATION, m_Frame.hWindow)
end
-- ----------------------------------------------------------------------------
--
local function OnAbout()
DlgMessage(_format( "%s [%s] Rel. date [%s]\n %s, %s, %s",
m_App.sAppName, m_App.sAppVer, m_App.sRelDate,
_VERSION, wxlua.wxLUA_VERSION_STRING, wx.wxVERSION_STRING))
end
-- ----------------------------------------------------------------------------
--
local function FillGridWithInfo(inGridIndex, inTable)
-- m_trace:line("FillGridWithInfo")
local gridRow = m_GridCities[inGridIndex]
local grid = gridRow[1]
local iRow = 0
for iRow=0, grid:GetNumberRows() - 1 do
for iCol=0, grid:GetNumberCols() - 1 do
grid:SetCellValue(iRow, iCol, "")
end
end
grid:SetRowLabelSize(gridRow[7])
grid:SetDefaultColSize(gridRow[8], false)
grid:SetColLabelValue(0, "")
if not inTable then
m_trace:line("Empty table: " .. gridRow[2])
return
end
iRow = 0
for field, value in pairs(inTable) do
grid:SetRowLabelValue(iRow, field)
if "table" ~= type(value) then
grid:SetCellValue(iRow, 0, tostring(value))
else
grid:SetCellValue(iRow, 0, "ref *")
end
iRow = iRow + 1
end
end
-- ----------------------------------------------------------------------------
--
local function FillGridRowByCol(inGridIndex, inTable)
-- m_trace:line("FillGridRowByCol")
local gridRow = m_GridCities[inGridIndex]
local grid = gridRow[1]
local iCol = 0
for iRow=0, grid:GetNumberRows() - 1 do
for iCol=0, grid:GetNumberCols() - 1 do
grid:SetCellValue(iRow, iCol, "")
end
end
if not inTable then
m_trace:line("Empty table: " .. gridRow[2])
return
end
-- set column's labels
--
local aMonth = inTable[1]
if aMonth then
iCol = 0
for field, _ in pairs(aMonth) do
grid:SetColLabelValue(iCol, tostring(field))
iCol = iCol + 1
end
-- fill with values
--
local tMonth = inTable
for iRow=1, #tMonth do
aMonth = tMonth[iRow]
iCol = 0
for _, value in pairs(aMonth) do
grid:SetCellValue(iRow - 1, iCol, tostring(value))
iCol = iCol + 1
end
end
end
end
-- ----------------------------------------------------------------------------
--
local function CreateGridAttributes()
-- m_trace:line("CreateGridAttributes")
if not m_GridAttrs.font then
m_GridAttrs.font = wx.wxFont( 11, wx.wxFONTFAMILY_MODERN, wx.wxFONTSTYLE_NORMAL,
wx.wxFONTWEIGHT_LIGHT, false, "Lucida Sans Unicode", wx.wxFONTENCODING_SYSTEM)
local fntCell = wx.wxFont( 10, wx.wxFONTFAMILY_MODERN, wx.wxFONTSTYLE_NORMAL,
wx.wxFONTWEIGHT_LIGHT, false, "Segoe UI", wx.wxFONTENCODING_SYSTEM)
m_GridAttrs.attrOdd = wx.wxGridCellAttr(palette.Gray0, palette.Ivory, fntCell, wx.wxALIGN_CENTRE, wx.wxALIGN_CENTRE)
m_GridAttrs.attrEven = wx.wxGridCellAttr(palette.Gray0, palette.BlanchedAlmond, fntCell, wx.wxALIGN_CENTRE, wx.wxALIGN_CENTRE)
end
end
-- ----------------------------------------------------------------------------
--
local function SetGridStyle(inGrid, inRows, inCols)
-- m_trace:line("SetGridStyle")
CreateGridAttributes()
inGrid:CreateGrid(inRows, inCols)
inGrid:SetMargins(5, 5)
inGrid:EnableEditing(false)
inGrid:SetLabelTextColour(palette.Gray0)
inGrid:SetGridLineColour(palette.SaddleBrown)
inGrid:SetDefaultColSize(160, false)
inGrid:SetLabelFont(m_GridAttrs.font)
-- make alternating colors for all columns
--
for i=0, inCols, 2 do
inGrid:SetColAttr(i, m_GridAttrs.attrOdd)
inGrid:SetColAttr(i + 1, m_GridAttrs.attrEven)
end
end
-- ----------------------------------------------------------------------------
-- destroy previously created pages
--
local function CleanUp()
-- m_trace:line("CleanUp")
local whichObj = m_Frame.gridSet
if whichObj then
-- remove all the GUI grids
--
for i=1, #whichObj do
table.remove(whichObj[i], 1)
end
m_Frame.gridSet = nil
m_Frame.notebook:DeleteAllPages()
end
m_Frame.hStatusBar:SetStatusText("No file selected", 0)
m_Frame.hStatusBar:SetStatusText("", 1)
end
-- ----------------------------------------------------------------------------
-- create all the grids
--
local function CreateNotebookPages()
-- m_trace:line("CreateNotebookPages")
local whichObj = m_Frame.gridSet
local notebook = m_Frame.notebook
local nbPage
local grNew
local row
for i=1, #whichObj do
nbPage = wx.wxPanel(notebook, wx.wxID_ANY)
grNew = wx.wxGrid(nbPage, wx.wxID_ANY, wx.wxDefaultPosition, notebook:GetSize())
-- insert the GUI grid in the current row of selected gridset
--
table.insert(whichObj[i], 1, grNew)
-- add page and apply styles
--
row = whichObj[i]
notebook:AddPage(nbPage, row[2], row[3], row[4])
SetGridStyle(grNew, row[5], row[6])
end
end
-- ----------------------------------------------------------------------------
--
local function ProcessFile()
-- m_trace:line("ProcessFile")
local hFile = io.open(m_App.sFileInput, "r")
local sBuffer = ""
local tJSonObjs
local tJSonCity = nil
local tJSonRegion = nil
-- issue a clean up of objects now
-- file load might fail and leave garbage
--
CleanUp()
-- read file
--
if hFile then
sBuffer = hFile:read("*a")
hFile:close()
end
if 0 == #sBuffer then return end
m_Frame.hStatusBar:SetStatusText(m_App.sFileInput, 0)
-- use the decoder to get a Lua table
--
tJSonObjs = json.decode(sBuffer)
-- sanity check
--
if not tJSonObjs then
m_trace:line("ProcessFile: json.decode failed!")
return
end
-- for debugging purpose
--
-- m_trace:table(tJSonObjs, false)
-- test known fields
--
tJSonCity = tJSonObjs.city
if not tJSonCity then
tJSonRegion = tJSonObjs.region
if not tJSonRegion then
m_trace:line("ProcessFile: format unknown!")
return
end
end
-- create necessary pages and fill with values
--
if tJSonCity then
m_Frame.gridSet = m_GridCities
CreateNotebookPages()
m_Frame.hStatusBar:SetStatusText(tJSonCity.cityName, 1) -- set city in statusbar
-- fill with value
--
FillGridWithInfo(3, tJSonCity) -- details
FillGridWithInfo(4, tJSonCity.member) -- member
FillGridRowByCol(1, tJSonCity.forecast.forecastDay) -- forecast
FillGridRowByCol(2, tJSonCity.climate.climateMonth) -- climate
elseif tJSonRegion then
m_Frame.gridSet = m_GridRegions
CreateNotebookPages()
m_Frame.hStatusBar:SetStatusText("All Regions", 1) -- set common label in statusbar
-- FillGridRowByCol
-- FillGridWithInfo(4, tJSonObjs.member) -- member
end
end
-- ----------------------------------------------------------------------------
-- Generate a unique new wxWindowID
--
local iRCEntry = wx.wxID_HIGHEST + 1
local function UniqueID()
iRCEntry = iRCEntry + 1
return iRCEntry
end
-- ----------------------------------------------------------------------------
-- open a file dialog to get a valid filename
--
local function OnOpenFile()
-- m_trace:line("OnOpenFile")
local dlgOpen = wx.wxFileDialog(m_Frame.hWindow,
"Select JSon file", "", "", "*.json",
wx.wxFD_OPEN | wx.wxFD_FILE_MUST_EXIST)
if wx.wxID_OK ~= dlgOpen:ShowModal() then return end
-- store full pathname
--
m_App.sFileInput = dlgOpen:GetPath()
ProcessFile()
end
-- ----------------------------------------------------------------------------
--
local function OnSize()
-- m_trace:line("OnSize")
local size = m_Frame.hWindow:GetClientSize()
m_Frame.notebook:SetSize(size)
-- need to resize all, doesn't cascade through
--
local gridSet = m_Frame.gridSet
if gridSet then
for i=1, #gridSet do gridSet[i][1]:SetSize(size) end
end
end
-- ----------------------------------------------------------------------------
--
local function OnClose()
-- m_trace:line("OnClose")
local m_Window = m_Frame.hWindow
if not m_Window then return end
-- finally destroy the window
--
m_Window.Destroy(m_Window)
m_Frame.hWindow = nil
end
-------------------------------------------------------------------------------
--
local function CreateFrame()
-- m_trace:line("CreateFrame")
-- create the frame
--
local frame = wx.wxFrame(wx.NULL, wx.wxID_ANY, m_App.sAppName,
wx.wxPoint(100, 100),
wx.wxSize(1900, 800))
-- create the menu entries
--
local rcMnuOpenFile = UniqueID()
local mnuFile = wx.wxMenu("", wx.wxMENU_TEAROFF)
mnuFile:Append(rcMnuOpenFile,"&Open file\tCtrl-O", "Select a json file")
mnuFile:AppendSeparator()
mnuFile:Append(wx.wxID_EXIT, "E&xit\tCtrl-X", "Quit the application")
local mnuHelp = wx.wxMenu("", wx.wxMENU_TEAROFF)
mnuHelp:Append(wx.wxID_ABOUT, "&About " .. m_App.sAppName)
-- attach the menu
--
local menuBar = wx.wxMenuBar()
menuBar:Append(mnuFile, "&File")
menuBar:Append(mnuHelp, "&Help")
-- assign the menubar to this frame
--
frame:SetMenuBar(menuBar)
-- create a statusbar
-- first pane will autosize
--
local stsBar = frame:CreateStatusBar(2, wx.wxST_SIZEGRIP)
stsBar:SetFont(wx.wxFont(10, wx.wxFONTFAMILY_DEFAULT, wx.wxFONTSTYLE_NORMAL, wx.wxFONTWEIGHT_NORMAL))
stsBar:SetStatusWidths({-1, 175})
-- assign event handlers for this frame
--
frame:Connect(rcMnuOpenFile, wx.wxEVT_COMMAND_MENU_SELECTED, OnOpenFile)
frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, OnClose)
frame:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, OnAbout)
frame:Connect(wx.wxEVT_SIZE, OnSize)
frame:Connect(wx.wxEVT_CLOSE_WINDOW, OnClose)
-- create a notebook style pane and apply styles
--
local notebook = wx.wxNotebook(frame, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxSize(100, 100))
local fntNote = wx.wxFont( 11, wx.wxFONTFAMILY_MODERN, wx.wxFONTSTYLE_NORMAL,
wx.wxFONTWEIGHT_BOLD, false, "Lucida Sans Unicode",
wx.wxFONTENCODING_SYSTEM)
notebook:SetBackgroundColour(palette.DarkSeaGreen)
notebook:SetFont(wx.wxFont(fntNote))
-- assign an icon to frame
--
local icon = wx.wxIcon("lib/icons/view.ico", wx.wxBITMAP_TYPE_ICO)
if icon then frame:SetIcon(icon) end
-- store
--
m_Frame.hWindow = frame
m_Frame.hStatusBar = stsBar
m_Frame.notebook = notebook
-- assign text to the statusbar
--
CleanUp()
return true
end
-- ----------------------------------------------------------------------------
--
local function RunApplication(...)
-- m_trace:line("RunApplication")
local sAppTitle = m_App.sAppName .. " [" .. m_App.sAppVer .. "]"
m_trace:time(sAppTitle .. " started")
assert(os.setlocale('us', 'all'))
m_trace:line("Current locale is [" .. os.setlocale() .. "]")
-- get the arguments on the command line
--
local tArgs = { }
for i, v in ipairs{...} do tArgs[i] = v end
if CreateFrame() then
m_Frame.hWindow:Show(true)
-- store full pathname
--
if tArgs[1] then
m_App.sFileInput = tArgs[1]
ProcessFile()
end
wx.wxGetApp():MainLoop()
end
m_trace:newline(sAppTitle .. " terminated ###")
end
-- ----------------------------------------------------------------------------
-- redirect logging
--
m_trace:open()
-- run
--
RunApplication(...)
-- end
--
m_trace:close()
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------