class TemplateControl(ElementControl): """ Defines an ElementControl that is rendered from a WUI Template NOTE: When subclassing set the template attribute - aka template = UITemplate.fromFile("myFile.wui") """ template = UITemplate.Template("empty") def __init__(self, id=None, name=None, parent=None, parentHandler=None, initScripts=None, **kwargs): ElementControl.__init__(self, id, name, parent, parentHandler, initScripts, **kwargs) templateDefinition = TemplateElement(template=self.template, factory=self.elementFactory) for control in templateDefinition.allChildren(): if isinstance(control, PageControl): self.registerControl(control.__class__) def buildUI(self, request): return TemplateElement(template=self.template, factory=self.elementFactory)
def __convertDictToNode(self, structure, node): if type(structure) in (str, unicode): structure = UITemplate.Template('textnode', properties=(('text', structure), )) create = structure.create if not create: return newNode = QTreeWidgetItem(node) newNode.setText(0, create) newNode.setIcon(0, self.elementIcon(create)) newNode.setText(1, structure.id) newNode.setText(2, structure.name) newNode.setText(3, structure.accessor) newNode.setText(4, self.newElementKey()) newNode.setExpanded( not self.collapsedMap.get(int(newNode.text(4)), False)) self.resizeTreeColumns() if self.selectedKey != None: if int(newNode.text(4)) == int(self.selectedKey): newNode.setSelected(True) self.ui.tree.setCurrentItem(newNode) self.ui.tree.scrollTo(self.ui.tree.currentIndex()) childElements = structure.childElements or () propertyDict = dict(structure.properties) propertyDict.update({ 'id': structure.id, 'name': structure.name, 'accessor': structure.accessor }) self.propertyMap[unicode(newNode.text(4))] = propertyDict for childElement in childElements: self.__convertDictToNode(childElement, newNode)
class TemplateControl(ElementControl): """ Defines an ElementControl that is rendered from a WUI Template NOTE: When subclassing set the template attribute - aka template = UITemplate.fromFile("myFile.wui") """ template = UITemplate.Template("empty") def __init__(self, id=None, name=None, parent=None, parentHandler=None, initScripts=None, **kwargs): ElementControl.__init__(self, id, name, parent, parentHandler, initScripts, **kwargs) self.autoRegister = [] def _postConnections(self): """ After all connections are made we automatically cache replacement actions where possible. """ templateDefinition = TemplateElement(template=self.template, factory=self.elementFactory) for control in templateDefinition.allChildren(): if isinstance(control, PageControl): self.registerControl(control.__class__) if isinstance(control, PageControlPlacement): self.autoRegister.append( (control.id, self._findRelativeControl(control.control))) def _findRelativeControl(self, name): control = self if name.startswith(".."): control = control.parentHandler name = name[2:] while name.startswith("."): startFrom = control.parentHandler name = name[1:] for controlName in name.split("."): control = getattr(control, controlName) return control def buildUI(self, request): """ Builds an instance of the defined template """ return TemplateElement(template=self.template, factory=self.elementFactory) def _modifyUI(self, ui, request): """ Automatically replaces any defined controls that exist on the template. """ for controlAccessor, control in self.autoRegister: placement = getattr(ui, controlAccessor) if not placement or not placement.parent: continue placement.replaceWith(control)
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ''' from WebElements import UITemplate EXPECTED_STRUCTURE = UITemplate.Template( 'container', properties=(("randomattribute", "Hello"), ), childElements=(UITemplate.Template( 'childelement', id="SomeRandomId", name="SomeRandomName", childElements=(UITemplate.Template("childishchildelement"), )), )) def test_fromFile(): """ Ensure UITemplate creates a dictionary structure from an XML File correctly """ assert UITemplate.fromFile( "testTemplate.xml", formatType=UITemplate.XML) == EXPECTED_STRUCTURE #xml file assert UITemplate.fromFile( "testTemplate.shpaml", formatType=UITemplate.SHPAML) == EXPECTED_STRUCTURE #shpaml file