[go: up one dir, main page]

Menu

[dfe9bc]: / ui / helper.py  Maximize  Restore  History

Download this file

260 lines (199 with data), 6.9 kB

  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
# ******************************************************
# * Copyright © 2017-2023 - Jordan Irwin (AntumDeluge) *
# ******************************************************
# * This software is licensed under the MIT license. *
# * See: LICENSE.txt for details. *
# ******************************************************
## Support functions for accessing wizard pages & attributes.
#
# @module ui.helper
import wx
import ui.app
from dbr.language import GT
from libdbr.logger import Logger
from libdebreate.ident import pgid
_logger = Logger(__name__)
## @todo Doxygen
class ErrorTuple:
def __init__(self, error_code=None, error_string=None):
# FIXME: Should throw exception for wrong instance types???
self.error_code = error_code
self.error_string = error_string
## Same as dbr.functions.ErrorTuple.GetTuple
def Get(self):
return self.GetTuple()
## @todo Doxygen
def GetCode(self):
return self.error_code
## @todo Doxygen
def GetMessage(self):
return self.GetString()
## @todo Doxygen
def GetString(self):
return self.error_string
## @todo Doxygen
def GetTuple(self):
return (self.error_code, self.error_string,)
## @todo Doxygen
def Set(self, error_code, error_string):
# FIXME: Should throw exception for wrong instance types???
self.error_code = error_code
self.error_string = error_string
## @todo Doxygen
def SetCode(self, error_code):
# FIXME: Should throw exception for wrong instance type???
if not isinstance(error_code, int):
return 1
self.error_code = error_code
return 0
## @todo Doxygen
def SetString(self, error_string):
# FIXME: Should throw exception for wrong instance type???
if not isinstance(error_string, str):
return 1
self.error_string = error_string
return 0
## Checks if a field (or widget) is enabled
#
# This is used for compatibility between wx. 2.8 & 3.0.
# 3.0 uses the method 'IsThisEnabled()' rather than
# 'IsEnabled()' to get the 'intrinsic' status of the
# widget.
# @param field
# The widget (wx.Window) to be checked
def FieldEnabled(field):
# wx. 3.0 must use 'IsThisEnabled' to get 'intrinsic' status in case parent is disabled
if wx.MAJOR_VERSION > 2:
return field.IsThisEnabled()
else:
return field.IsEnabled()
## Finds the ui.page.Page instance where an object is located
#
# @param field
# \b \e wx.Window instance to find parents of
def FindPageOf(field):
parent = field.GetParent()
if not parent:
return None
win_id = parent.GetId()
if win_id in pgid.IdList:
return ui.app.getPage(win_id)
return FindPageOf(parent)
## Retrieves all instances of a window type from a parent window
def GetAllTypeFields(page, fieldType):
# Objects that are not `ui.page.Page` instances must be passed as 'page' argument
if not isinstance(page, wx.Window):
page = ui.app.getPage(page)
field_list = []
# Recursively check children
children = page.GetChildren()
if children:
for C in children:
if isinstance(C, fieldType):
field_list.append(C)
field_list = field_list + GetAllTypeFields(C, fieldType)
return field_list
## Retrieves a field/control from a page
#
# @param page
# \b \e ui.page.Page : The page to search
# @param field_id
# \b \e int : ID of desired field/control
# @param field_type
# \b \b wx.Window : The class type that field/control should be
# @return
# \b \e wx.Window : Field control matching field_id or None
# @todo
# FIXME: field_type is currently unused
def GetField(page, field_id, field_type=wx.Window):
if not isinstance(page, wx.Window):
page = ui.app.getPage(page)
if isinstance(page, field_type) and page.GetId() == field_id:
return page
# Recursively check children
children = page.GetChildren()
if children:
for C in children:
field = GetField(C, field_id)
if field and isinstance(field, field_type) and \
field.GetId() == field_id:
return field
return ErrorTuple(1, GT("Field ID does not exist or match any fields: {}").format(field_id))
## Retrieves the input value of a field/control
#
# @param page
# \b \e Integer ID of desired page or page object
# @param field_id
# \b \e Integer ID of desired field/control
# @param field_type
# \b \b wx.Window : The class type that field/control should be
# @return
# The retrieved value of the field/control or an error tuple
# @todo
# FIXME: field_type is currently unused
def GetFieldValue(page, field_id, field_type=wx.Window):
if isinstance(page, int):
page = ui.app.getPage(page)
if not isinstance(page, wx.Window):
# FIXME: Should have error id
err_msg = GT("Page retrieved was not instance of a window/widget: Page name: {}").format(page.GetName())
return ErrorTuple(1, err_msg)
field = GetField(page, field_id)
if isinstance(field, ErrorTuple):
return field
if isinstance(field, wx.TextCtrl):
return field.GetValue()
if isinstance(field, wx.Choice):
return field.GetStringSelection()
# FIXME: Should have error id
err_msg = GT("Unrecognized field type: {} (ID: {})").format(type(field), field_id)
return ErrorTuple(1, err_msg)
## Finds the MainWindow instance.
#
# @deprecated
# Use `ui.app.getMainWindow`.
def GetMainWindow():
_logger.deprecated(GetMainWindow, alt=ui.app.getMainWindow)
return ui.app.getMainWindow()
## Retrieves a menu from the main window's menu bar by ID.
#
# @param menuId
# \b \e Integer ID of desired menu.
# @return
# The \b \e wx.Menu instance.
# @deprecated
# Use `ui.main.MainWindow.getMenu`.
def GetMenu(menuId):
_logger.deprecated(GetMenu, alt="ui.main.MainWindow.getMenu")
return ui.app.getMainWindow().getMenu(menuId)
## Retrieves the ui.menu.MenuBar instance in use by the main window.
#
# @deprecated
# Use `ui.main.MainWindow.getMenuBar`.
def GetMenuBar():
_logger.deprecated(GetMenuBar, alt="ui.main.MainWindow.getMenuBar")
return ui.app.getMainWindow().getMenuBar()
## Retrieves the `ui.page.Page` instance that matched pageId.
#
# @deprecated
# Use `ui.wizard.Wizard.GetPage`.
def GetPage(pageId):
_logger.deprecated(GetPage, alt="ui.wizard.Wizard.getPage")
page = ui.app.getWizard().getPage(pageId)
return page
## Retrieves the full list of page IDs from the wizard.
#
# @return
# \b e\ tuple : List of all active wizard page IDs.
# @deprecated
# Use `ui.wizard.Wizard.getPagesIdList`.
def GetPagesIdList():
_logger.deprecated(GetPagesIdList, alt="ui.wizard.Wizard.getPagesIdList")
return ui.app.getWizard().getPagesIdList()
## Retrieves the ui.wizard.Wizard instance.
#
# @deprecated
# Use `ui.app.getWizard`.
def GetWizard():
_logger.deprecated(GetWizard, alt=ui.app.getWizard)
return ui.app.getWizard()