#!/usr/bin/env python
#[DECLARATIVE UI LAYOUT ENGINE, URL: http://forums.4fips.com/viewtopic.php?f=3&t=6896]
#[CODE BY FIPS @ 4FIPS.COM, (c) 2016 FILIP STOKLAS, MIT-LICENSED]
from layout import Vec2, Rect, Margin, Elem, Border, VStack, HStack
import sys
from PySide.QtCore import *
from PySide.QtGui import *
def make_button(parent, title, stretch):
btn = QPushButton(title, parent)
return Elem(btn, min_size=Vec2(*btn.size().toTuple()), stretch=stretch)
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("LAYOUT DEMO BY FIPS @ 4FIPS.COM 2016")
self.resize(600, 480)
self.setup()
def setup(self):
self.meta_layout = \
Border(margin=Margin(6, 6, 6, 6), children=[
VStack([
Border(margin=Margin(0, 0, 0, 4), children=[
VStack([
make_button(self, "Question:", stretch=Vec2(1, 0)),
make_button(self, "<...multi-line-text...>", stretch=Vec2(1, 1)),
make_button(self, "Hint:", stretch=Vec2(1, 0)),
make_button(self, "<...multi-line-text...>", stretch=Vec2(1, 1)),
make_button(self, "Answer:", stretch=Vec2(1, 0)),
make_button(self, "<...text...>", stretch=Vec2(1, 0)),
]),
]),
VStack(stretch=Vec2(1, 0), children=[
HStack(stretch=Vec2(1, 0), children=[
make_button(self, "Browse >", stretch=Vec2(1, 0)),
make_button(self, "Test >", stretch=Vec2(1, 0)),
]),
HStack(stretch=Vec2(1, 0), children=[
make_button(self, "Graph >", stretch=Vec2(1, 0)),
make_button(self, "Setup >", stretch=Vec2(1, 0)),
make_button(self, "About >", stretch=Vec2(1, 0)),
]),
]),
]),
])
def resizeEvent(self, event):
self.meta_layout.reshape(Rect(0, 0, *event.size().toTuple()))
for ch in self.children():
ch.setGeometry(*ch.meta_elem.rect)
def main():
app = QApplication(sys.argv)
win = MainWindow()
win.show()
win.raise_()
return app.exec_()
if __name__ == "__main__":
main()