[go: up one dir, main page]

Menu

[r422]: / sono / src / cmds / manu  Maximize  Restore  History

Download this file

321 lines (281 with data), 12.2 kB

#!/usr/bin/env python

import sys
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import sono.Processor
import sono.Kernel
from sono.utils import *
import Es
import manu

kernel = sono.Kernel.Kernel()

def makeBrushSetter(color, colorName):
    a = (lambda obj: obj.items["main"].setBrush(QBrush(color)))
    a.__doc__ = "Fill the shape with %s"%colorName
    return a

def makePenColorSetter(color, colorName):
    a = (lambda obj: 
         obj.items["main"].setPen(QPen(QBrush(color), 
                                       obj.items["main"].pen().width()))
         )
    a.__doc__ = "Stroke the shape with %s"%colorName
    return a

def makePenWidthSetter(step):
    def moreThanOne(value):
        if value < 1:
            return 1
        else:
            return value
    def currentPen(obj):
        return obj.items["main"].pen()
    def currentColor(obj):
        return obj.items["main"].pen().brush().color()

    a = (lambda obj:
             obj.items["main"].setPen(QPen(QBrush(currentColor(obj)),
                                           moreThanOne(currentPen(obj).width()
                                                       + step)))
         )
    doc = "Increase the pen width %d" % step

    a.__doc__ = doc

    return a

class Window(manu.Window):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.items = {}

        self.items["main"] = QGraphicsEllipseItem(0, 0, manu.SCENESIZE, manu.SCENESIZE)
        self.items["main"].setPen(QPen(QBrush(Qt.black), 5))
        self.items["main"].setBrush(QBrush(Qt.white))
        self.items["main"].scale = 1.0
        self.items["main"].angle = 0.0
        self.items["main"].hshift = 0.0
        self.items["main"].vshift = 0.0
        self.items["main"].rotate_index = 0
        self.scene.addItem(self.items["main"])

    def switchItem(self):
        oi = self.items["main"]
        p = oi.pen()
        b = oi.brush()
        t = oi.transform()
        if oi.rotate_index == 0:
            ni = QGraphicsRectItem(0, 0, manu.SCENESIZE, manu.SCENESIZE)
            ni.rotate_index = 1
        elif oi.rotate_index == 1:
	    ni = QGraphicsPathItem()
	    path = QPainterPath()
	    h = (1.732 * manu.SCENESIZE / 2) 
            offset = (manu.SCENESIZE - h) / 2.0 
            # offset = manu.SCENESIZE * (1.0 - (1.0 / 1.732)) / 2.0
	    path.moveTo(0, manu.SCENESIZE - offset )
	    path.lineTo(manu.SCENESIZE, manu.SCENESIZE - offset)
	    path.lineTo(manu.SCENESIZE/2, manu.SCENESIZE - h - offset)
	    path.closeSubpath()
	    ni.setPath(path) 
            ni.rotate_index = 2
        elif oi.rotate_index == 2:
            ni = QGraphicsPathItem()
	    path = QPainterPath()
            path.moveTo(0, 0)
            path.lineTo(manu.SCENESIZE, manu.SCENESIZE)
            path.moveTo(0, manu.SCENESIZE)
            path.lineTo(manu.SCENESIZE, 0)
            ni.setPath(path) 
            ni.rotate_index = 3
        elif oi.rotate_index == 3:
            ni = QGraphicsPathItem()
	    path = QPainterPath()
	    h = (1.732 * manu.SCENESIZE / 2) 
            offset = manu.SCENESIZE * (1.0 - (1.0 / 1.732)) / 2.0
            x0 = manu.SCENESIZE/2
            y0 = manu.SCENESIZE - offset - (h / 3.0)
            path.moveTo(x0, y0)
            path.lineTo (0, manu.SCENESIZE - offset )
            path.moveTo(x0, y0)
            path.lineTo(manu.SCENESIZE, manu.SCENESIZE - offset)
            path.moveTo(x0, y0)
            path.lineTo(manu.SCENESIZE/2, manu.SCENESIZE - h - offset)
            ni.setPath(path) 
            ni.rotate_index = 4
	else:
            ni = QGraphicsEllipseItem(0, 0, manu.SCENESIZE, manu.SCENESIZE)
            ni.rotate_index = 0
            
        self.scene.removeItem(oi)        
        ni.angle = oi.angle
        ni.scale = oi.scale
        ni.hshift = oi.hshift
        ni.vshift = oi.vshift
        ni.setPen(p)
        ni.setBrush(b)
        ni.setTransform(t)

        self.items["main"] = ni
        self.scene.addItem(self.items["main"])

    def zoomIn(self, obj):
        self.zoom(obj, 1.01)
    def zoomOut(self, obj):
        self.zoom(obj, 0.99)

    def zoom(self, obj, scale):
        obj = self.items[obj]
        otform = obj.transform()
        ntform = QTransform()
        ntform.translate(manu.SCENESIZE/2 - obj.hshift, manu.SCENESIZE/2 - obj.vshift)
        # TODO
        ntform.m11 = otform.m11()
        ntform.m12 = otform.m12()
        ntform.m13 = otform.m13()
        ntform.m21 = otform.m21()
        ntform.m22 = otform.m22()
        ntform.m23 = otform.m23()
        ntform.m31 = otform.m31()
        ntform.m32 = otform.m32()
        ntform.m33 = otform.m33()
        
        new_scale = obj.scale * scale;
        if new_scale < 0.01:
            new_scale = 0.01
        elif new_scale > 100.0:
            new_scale = 100.0
        obj.scale = new_scale
        ntform.scale(obj.scale, obj.scale)
        ntform.rotate(obj.angle)
        ntform.translate(-manu.SCENESIZE/2 + obj.hshift, -manu.SCENESIZE/2 + obj.vshift)
        obj.setTransform(ntform)

    def rotateRight(self, obj):
        self.rotate(obj, -5.0)
    def rotateLeft(self, obj):
        self.rotate(obj, 5.0)
    def rotate(self, obj, angle):
        obj = self.items[obj]
        otform = obj.transform()
        ntform = QTransform()
        ntform.translate(manu.SCENESIZE/2 - obj.hshift, manu.SCENESIZE/2 - obj.vshift)
        # TODO
        ntform.m11 = otform.m11()
        ntform.m12 = otform.m12()
        ntform.m13 = otform.m13()
        ntform.m21 = otform.m21()
        ntform.m22 = otform.m22()
        ntform.m23 = otform.m23()
        ntform.m31 = otform.m31()
        ntform.m32 = otform.m32()
        ntform.m33 = otform.m33()

        obj.angle += angle
        ntform.rotate(obj.angle)
        ntform.scale(obj.scale, obj.scale)

        ntform.translate(-manu.SCENESIZE/2 + obj.hshift, -manu.SCENESIZE/2 + obj.vshift)
        obj.setTransform(ntform)

    def translateRight(self, obj):
        self.translate(obj, 5.0, 0.0)
    def translateLeft(self, obj):
        self.translate(obj, -5.0, 0.0)
    def translateUp(self, obj):
        self.translate(obj, 5.0, 0.0)
    def translateDown(self, obj):
        self.translate(obj, -5.0, 0.0)
    def translate(self, obj, hshift, vshift):
        pass

    def setFillColor(self, color):
        self.items["main"].setBrush(QBrush(color))

        

class Manu ( sono.Processor.Processor ):
    def __init__(self, *args, **kargs):
        super(Manu, self).__init__(*args, **kargs)
        form = Window()
        def_key = form.define_key
        def_key("b", False, False, makeBrushSetter(Qt.blue,
                                                   "blue"))
        def_key("b", True,  False, makePenColorSetter(Qt.blue,
                                              "blue"))
        def_key("f", False, False,  makePenWidthSetter(1))
#         def_key("g", False, False, makeBrushSetter(Qt.green,
#                                                    "green"))
#         def_key("g", True,  False, makePenColorSetter(Qt.green,
#                                                       "green"))
        def_key("g", False, False, makeBrushSetter(QColor(0x00, 0x80, 0x00),
                                                   "green"))
        def_key("g", True,  False, makePenColorSetter(QColor(0x00, 0x80, 0x00),
                                                      "green"))
        def_key("k", False, False, makeBrushSetter(Qt.black,
                                                   "black"))
        def_key("k", True,  False, makePenColorSetter(Qt.black,
                                                      "lime"))
        def_key("l", False, False, makeBrushSetter(QColor(0x00, 0xFF, 0x00),
                                                   "lime"))
        def_key("l", True,  False, makePenColorSetter(QColor(0x00, 0xFF, 0x00),
                                                      "green"))
        def_key("m", False, False, makeBrushSetter(QColor(0xa0, 0x20, 0xf0),
                                                   "purple"))
        def_key("m", True, False, makePenColorSetter(QColor(0xa0, 0x20, 0xf0),
                                                     "purple"))
        def_key("n", False, False,  makePenWidthSetter(-1))
        def_key("o", False, False, makeBrushSetter(QColor(0xff, 0x7f, 0x50),
                                                   "orange"))
        def_key("o", True, False, makePenColorSetter(QColor(0xff, 0x7f, 0x50),
                                                     "orange"))
        def_key("p", False, False, makeBrushSetter(QColor(0xff, 0xc0, 0xcb),
                                                   "pink"))
        def_key("p", True, False, makePenColorSetter(QColor(0xff, 0xc0, 0xcb),
                                                     "pink"))
        
        quit = (lambda obj: sys.exit(0))
        quit.__doc__ = "Quit"
        def_key("q", False, True,  quit)

        def_key("r", False, False, makeBrushSetter(Qt.red,
                                                   "red"))
        def_key("r", True,  False, makePenColorSetter(Qt.red,
                                                      "red"))
        
        switchItem = (lambda obj: obj.switchItem())
        switchItem.__doc__ = "Toggle the shape: Circle <-> Rectangle"
        def_key("t", False, False,  switchItem)
        def_key("w", False, False, makeBrushSetter(Qt.white,
                                                   "white"))
        def_key("w", True,  False, makePenColorSetter(Qt.white,
                                                      "white"))
        def_key("y", False, False, makeBrushSetter(Qt.yellow,
                                                   "yellow"))
        def_key("y", True,  False, makePenColorSetter(Qt.yellow,
                                                      "yellow"))
        
        
        zoomIn = (lambda obj: obj.zoomIn("main"))
        zoomIn.__doc__ = "Zoom in"
        def_key("u", False, False,  zoomIn)
        
        zoomOut = (lambda obj: obj.zoomOut("main"))
        zoomOut.__doc__ = "Zoom out"
        def_key("d", False, False,  zoomOut)
        
        rotateRight = (lambda obj: obj.rotateRight("main"))
        rotateRight.__doc__ = "Rotate right"
        def_key(",", False, False, rotateRight)

        rotateLeft = (lambda obj: obj.rotateLeft("main"))
        rotateLeft.__doc__ = "Rotate right"
        def_key(".", False, False, rotateLeft)

        translateRight = (lambda obj: obj.translateRight("main"))
        translateRight.__doc__ = "Translate right"
        def_key("x", False, False, translateRight)

        translateLeft = (lambda obj: obj.translateLeft("main"))
        rotateLeft.__doc__ = "Translate right"
        def_key("z", False, False, translateLeft)

        describe_bindings = (lambda obj: obj.describe_bindings())
        describe_bindings.__doc__ = "Describe key bindings"
        def_key("?", False, False, describe_bindings)

        self.form = form
        self.fill_abs_index = 0
    def loop(self):
        r = Es.es_read(self.stdin)
        if r[1] == Es.Symbol("show"):
            rect = QApplication.desktop().availableGeometry()
            self.form.resize(int(rect.width() * 0.9), int(rect.height() * 0.9))
            self.form.show()
        elif r[1] == Es.Symbol("fill"):
            i = r[2]
            candidate = [Qt.red, Qt.yellow, Qt.green, Qt.blue, Qt.black, Qt.white]
            i = i % len(candidate)
            self.form.setFillColor(candidate[i])
        elif r[1] == Es.Symbol("fill-abs"):
            self.fill_abs_index += r[2]
            candidate = [Qt.red, Qt.yellow, Qt.green, Qt.blue, Qt.black, Qt.white]
            i = self.fill_abs_index % len(candidate)
            print (i)
            self.form.setFillColor(candidate[i])
        elif r[1] == Es.Symbol("zoom"):
            d = r[2]
            if d > 0:
                self.form.zoomIn("main")
            else:
                self.form.zoomOut("main")
        elif r[1] == Es.Symbol("switchItem"):
            self.form.switchItem()

manu.loadInitFile(kernel, '.sono.es')