2010-08-05 11:23:17 -05:00
|
|
|
'''This module contains classes used for layouting graphical elements
|
|
|
|
(fields, widgets, groups, ...).'''
|
|
|
|
|
|
|
|
# A layout defines how a given field is rendered in a given context. Several
|
|
|
|
# contexts exist:
|
|
|
|
# "view" represents a given page for a given Appy class, in read-only mode.
|
|
|
|
# "edit" represents a given page for a given Appy class, in edit mode.
|
|
|
|
# "cell" represents a cell in a table, like when we need to render a field
|
|
|
|
# value in a query result or in a reference table.
|
2014-03-05 06:25:36 -06:00
|
|
|
# "search" represents an advanced search screen.
|
2010-08-05 11:23:17 -05:00
|
|
|
|
|
|
|
# Layout elements for a class or page ------------------------------------------
|
|
|
|
# s - The page summary, containing summarized information about the page or
|
|
|
|
# class, workflow information and object history.
|
|
|
|
# w - The widgets of the current page/class
|
|
|
|
# n - The navigation panel (inter-objects navigation)
|
|
|
|
# b - The range of buttons (intra-object navigation, save, edit, delete...)
|
|
|
|
|
|
|
|
# Layout elements for a field --------------------------------------------------
|
|
|
|
# l - "label" The field label
|
|
|
|
# d - "description" The field description (a description is always visible)
|
|
|
|
# h - "help" Help for the field (typically rendered as an icon,
|
|
|
|
# clicking on it shows a popup with online help
|
|
|
|
# v - "validation" The icon that is shown when a validation error occurs
|
|
|
|
# (typically only used on "edit" layouts)
|
|
|
|
# r - "required" The icon that specified that the field is required (if
|
|
|
|
# relevant; typically only used on "edit" layouts)
|
|
|
|
# f - "field" The field value, or input for entering a value.
|
2013-01-08 06:28:35 -06:00
|
|
|
# c - "changes" The button for displaying changes to a field
|
2010-08-05 11:23:17 -05:00
|
|
|
|
|
|
|
# For every field of a Appy class, you can define, for every layout context,
|
|
|
|
# what field-related information will appear, and how it will be rendered.
|
|
|
|
# Variables defaultPageLayouts and defaultFieldLayouts defined below give the
|
|
|
|
# default layouts for pages and fields respectively.
|
|
|
|
#
|
|
|
|
# How to express a layout? You simply define a string that is made of the
|
|
|
|
# letters corresponding to the field elements you want to render. The order of
|
|
|
|
# elements correspond to the order into which they will be rendered.
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
rowDelimiters = {'-':'middle', '=':'top', '_':'bottom'}
|
2015-10-27 15:10:24 -05:00
|
|
|
rowDelms = ''.join(list(rowDelimiters.keys()))
|
2010-08-05 11:23:17 -05:00
|
|
|
cellDelimiters = {'|': 'center', ';': 'left', '!': 'right'}
|
2015-10-27 15:10:24 -05:00
|
|
|
cellDelms = ''.join(list(cellDelimiters.keys()))
|
2010-11-10 08:15:00 -06:00
|
|
|
|
2013-08-21 05:35:30 -05:00
|
|
|
pxDict = {
|
2010-08-05 11:23:17 -05:00
|
|
|
# Page-related elements
|
2014-10-21 02:25:37 -05:00
|
|
|
's': 'pxHeader', 'w': 'pxFields', 'n': 'pxNavigationStrip', 'b': 'pxButtons',
|
2010-08-05 11:23:17 -05:00
|
|
|
# Field-related elements
|
2013-08-21 05:35:30 -05:00
|
|
|
'l': 'pxLabel', 'd': 'pxDescription', 'h': 'pxHelp', 'v': 'pxValidation',
|
|
|
|
'r': 'pxRequired', 'c': 'pxChanges'}
|
2010-08-05 11:23:17 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2013-08-21 15:25:27 -05:00
|
|
|
class Cell:
|
2010-08-05 11:23:17 -05:00
|
|
|
'''Represents a cell in a row in a table.'''
|
|
|
|
def __init__(self, content, align, isHeader=False):
|
|
|
|
self.align = align
|
|
|
|
self.width = None
|
|
|
|
self.content = None
|
|
|
|
self.colspan = 1
|
|
|
|
if isHeader:
|
|
|
|
self.width = content
|
|
|
|
else:
|
|
|
|
self.content = [] # The list of widgets to render in the cell
|
|
|
|
self.decodeContent(content)
|
|
|
|
|
|
|
|
def decodeContent(self, content):
|
|
|
|
digits = '' # We collect the digits that will give the colspan
|
|
|
|
for char in content:
|
|
|
|
if char.isdigit():
|
|
|
|
digits += char
|
|
|
|
else:
|
|
|
|
# It is a letter corresponding to a macro
|
2013-08-21 05:35:30 -05:00
|
|
|
if char in pxDict:
|
|
|
|
self.content.append(pxDict[char])
|
2010-08-05 11:23:17 -05:00
|
|
|
elif char == 'f':
|
|
|
|
# The exact macro to call will be known at render-time
|
|
|
|
self.content.append('?')
|
|
|
|
# Manage the colspan
|
|
|
|
if digits:
|
|
|
|
self.colspan = int(digits)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2013-08-21 15:25:27 -05:00
|
|
|
class Row:
|
2010-08-05 11:23:17 -05:00
|
|
|
'''Represents a row in a table.'''
|
|
|
|
def __init__(self, content, valign, isHeader=False):
|
|
|
|
self.valign = valign
|
|
|
|
self.cells = []
|
|
|
|
self.decodeCells(content, isHeader)
|
|
|
|
# Compute the row length
|
|
|
|
length = 0
|
|
|
|
for cell in self.cells:
|
2013-08-21 15:25:27 -05:00
|
|
|
length += cell.colspan
|
2010-08-05 11:23:17 -05:00
|
|
|
self.length = length
|
|
|
|
|
|
|
|
def decodeCells(self, content, isHeader):
|
|
|
|
'''Decodes the given chunk of layout string p_content containing
|
|
|
|
column-related information (if p_isHeader is True) or cell content
|
|
|
|
(if p_isHeader is False) and produces a list of Cell instances.'''
|
|
|
|
cellContent = ''
|
|
|
|
for char in content:
|
|
|
|
if char in cellDelimiters:
|
|
|
|
align = cellDelimiters[char]
|
2013-08-21 15:25:27 -05:00
|
|
|
self.cells.append(Cell(cellContent, align, isHeader))
|
2010-08-05 11:23:17 -05:00
|
|
|
cellContent = ''
|
|
|
|
else:
|
|
|
|
cellContent += char
|
|
|
|
# Manage the last cell if any
|
|
|
|
if cellContent:
|
2013-08-21 15:25:27 -05:00
|
|
|
self.cells.append(Cell(cellContent, 'left', isHeader))
|
2010-08-05 11:23:17 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2013-08-21 15:25:27 -05:00
|
|
|
class Table:
|
2010-08-05 11:23:17 -05:00
|
|
|
'''Represents a table where to dispose graphical elements.'''
|
2010-09-13 14:04:10 -05:00
|
|
|
simpleParams = ('style', 'css_class', 'cellpadding', 'cellspacing', 'width',
|
|
|
|
'align')
|
2014-03-05 06:25:36 -06:00
|
|
|
derivedRepls = {'view': 'hrvd', 'search': '', 'cell': 'ldc'}
|
2010-09-13 14:04:10 -05:00
|
|
|
def __init__(self, layoutString=None, style=None, css_class='',
|
|
|
|
cellpadding=0, cellspacing=0, width='100%', align='left',
|
|
|
|
other=None, derivedType=None):
|
|
|
|
if other:
|
|
|
|
# We need to create a Table instance from another Table instance,
|
|
|
|
# given in p_other. In this case, we ignore previous params.
|
|
|
|
if derivedType != None:
|
2012-11-06 04:32:39 -06:00
|
|
|
# We will not simply mimic p_other. If p_derivedType is:
|
2010-09-13 14:04:10 -05:00
|
|
|
# - "view", p_derivedFrom is an "edit" layout, and we must
|
|
|
|
# create the corresponding "view" layout;
|
|
|
|
# - "cell", p_derivedFrom is a "view" layout, and we must
|
|
|
|
# create the corresponding "cell" layout;
|
|
|
|
self.layoutString = Table.deriveLayout(other.layoutString,
|
|
|
|
derivedType)
|
|
|
|
else:
|
2010-10-29 07:36:36 -05:00
|
|
|
self.layoutString = other.layoutString
|
2010-09-13 14:04:10 -05:00
|
|
|
source = 'other.'
|
|
|
|
else:
|
|
|
|
source = ''
|
|
|
|
self.layoutString = layoutString
|
|
|
|
# Initialise simple params, either from the true params, either from
|
|
|
|
# the p_other Table instance.
|
|
|
|
for param in Table.simpleParams:
|
2015-10-27 15:10:24 -05:00
|
|
|
exec('self.%s = %s%s' % (param, source, param))
|
2010-08-05 11:23:17 -05:00
|
|
|
# The following attribute will store a special Row instance used for
|
|
|
|
# defining column properties.
|
|
|
|
self.headerRow = None
|
2010-09-13 14:04:10 -05:00
|
|
|
# The content rows will be stored hereafter.
|
2010-08-05 11:23:17 -05:00
|
|
|
self.rows = []
|
2010-09-13 14:04:10 -05:00
|
|
|
self.decodeRows(self.layoutString)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def deriveLayout(layout, derivedType):
|
|
|
|
'''Returns a layout derived from p_layout.'''
|
|
|
|
res = layout
|
|
|
|
for letter in Table.derivedRepls[derivedType]:
|
|
|
|
res = res.replace(letter, '')
|
2010-11-10 08:15:00 -06:00
|
|
|
# Strip the derived layout
|
|
|
|
res = res.lstrip(rowDelms); res = res.lstrip(cellDelms)
|
|
|
|
if derivedType == 'cell':
|
|
|
|
res = res.rstrip(rowDelms); res = res.rstrip(cellDelms)
|
2010-09-13 14:04:10 -05:00
|
|
|
return res
|
2010-08-05 11:23:17 -05:00
|
|
|
|
|
|
|
def addCssClasses(self, css_class):
|
|
|
|
'''Adds a single or a group of p_css_class.'''
|
2014-08-08 05:36:19 -05:00
|
|
|
if not self.css_class: self.css_class = css_class
|
2010-08-05 11:23:17 -05:00
|
|
|
else:
|
2014-08-08 05:36:19 -05:00
|
|
|
self.css_class += ' ' + css_class
|
2010-08-05 11:23:17 -05:00
|
|
|
# Ensures that every class appears once
|
|
|
|
self.css_class = ' '.join(set(self.css_class.split()))
|
|
|
|
|
|
|
|
def isHeaderRow(self, rowContent):
|
|
|
|
'''Determines if p_rowContent specified the table header row or a
|
|
|
|
content row.'''
|
|
|
|
# Find the first char that is a number or a letter
|
|
|
|
for char in rowContent:
|
|
|
|
if char not in cellDelimiters:
|
|
|
|
if char.isdigit(): return True
|
|
|
|
else: return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def decodeRows(self, layoutString):
|
|
|
|
'''Decodes the given p_layoutString and produces a list of Row
|
|
|
|
instances.'''
|
|
|
|
# Split the p_layoutString with the row delimiters
|
|
|
|
rowContent = ''
|
|
|
|
for char in layoutString:
|
|
|
|
if char in rowDelimiters:
|
|
|
|
valign = rowDelimiters[char]
|
|
|
|
if self.isHeaderRow(rowContent):
|
2010-09-20 04:33:54 -05:00
|
|
|
if not self.headerRow:
|
2013-08-21 15:25:27 -05:00
|
|
|
self.headerRow = Row(rowContent, valign, isHeader=True)
|
2010-08-05 11:23:17 -05:00
|
|
|
else:
|
2013-08-21 15:25:27 -05:00
|
|
|
self.rows.append(Row(rowContent, valign))
|
2010-08-05 11:23:17 -05:00
|
|
|
rowContent = ''
|
|
|
|
else:
|
|
|
|
rowContent += char
|
|
|
|
# Manage the last row if any
|
|
|
|
if rowContent:
|
2013-08-21 15:25:27 -05:00
|
|
|
self.rows.append(Row(rowContent, 'middle'))
|
2010-08-05 11:23:17 -05:00
|
|
|
|
|
|
|
def removeElement(self, elem):
|
|
|
|
'''Removes given p_elem from myself.'''
|
2013-08-21 05:35:30 -05:00
|
|
|
macroToRemove = pxDict[elem]
|
2010-08-05 11:23:17 -05:00
|
|
|
for row in self.rows:
|
2013-08-21 15:25:27 -05:00
|
|
|
for cell in row.cells:
|
|
|
|
if macroToRemove in cell.content:
|
|
|
|
cell.content.remove(macroToRemove)
|
2010-08-05 11:23:17 -05:00
|
|
|
|
2012-08-14 09:05:02 -05:00
|
|
|
# Some base layouts to use, for fields and pages -------------------------------
|
2013-08-21 15:25:27 -05:00
|
|
|
# The default layouts for pages.
|
2010-08-05 11:23:17 -05:00
|
|
|
defaultPageLayouts = {
|
2014-04-20 12:22:40 -05:00
|
|
|
'view': Table('w-b'), 'edit': Table('w-b', width=None)}
|
2012-08-14 09:05:02 -05:00
|
|
|
# A layout for pages, containing the page summary.
|
2014-04-20 12:22:40 -05:00
|
|
|
summaryPageLayouts = {'view': Table('s-w-b'), 'edit': Table('w-b', width=None)}
|
|
|
|
widePageLayouts = {'view': Table('w-b'), 'edit': Table('w-b')}
|
2013-04-26 19:15:44 -05:00
|
|
|
centeredPageLayouts = {
|
|
|
|
'view': Table('w|-b|', align="center"),
|
|
|
|
'edit': Table('w|-b|', width=None, align='center')
|
|
|
|
}
|
|
|
|
|
2012-08-14 09:05:02 -05:00
|
|
|
# The default layout for fields. Alternative layouts may exist and are declared
|
|
|
|
# as static attributes of the concerned Type subclass.
|
2014-05-17 09:44:56 -05:00
|
|
|
defaultFieldLayouts = {'edit': 'lrv-f', 'search': 'l-f'}
|
2012-11-02 16:27:54 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class ColumnLayout:
|
|
|
|
'''A "column layout" dictates the way a table column must be rendered. Such
|
|
|
|
a layout is of the form: <name>[*width][,|!|`|`]
|
|
|
|
* "name" is the name of the field whose content must be shown in
|
|
|
|
column's cells;
|
|
|
|
* "width" is the width of the column. Any valid value for the "width"
|
|
|
|
attribute of the "td" HTML tag is accepted;
|
|
|
|
* , | or ! indicates column alignment: respectively, left, centered or
|
|
|
|
right.
|
|
|
|
'''
|
|
|
|
def __init__(self, layoutString):
|
|
|
|
self.layoutString = layoutString
|
|
|
|
def get(self):
|
|
|
|
'''Returns a list containing the separate elements that are within
|
|
|
|
self.layoutString.'''
|
|
|
|
consumed = self.layoutString
|
|
|
|
# Determine column alignment
|
|
|
|
align = 'left'
|
|
|
|
lastChar = consumed[-1]
|
|
|
|
if lastChar in cellDelimiters:
|
|
|
|
align = cellDelimiters[lastChar]
|
|
|
|
consumed = consumed[:-1]
|
|
|
|
# Determine name and width
|
|
|
|
if '*' in consumed:
|
|
|
|
name, width = consumed.rsplit('*', 1)
|
|
|
|
else:
|
|
|
|
name = consumed
|
|
|
|
width = ''
|
|
|
|
return name, width, align
|
2010-08-05 11:23:17 -05:00
|
|
|
# ------------------------------------------------------------------------------
|