CurveDemo3.py :  » GUI » PyQwt » PyQwt-5.2.0 » qt3examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » PyQwt 
PyQwt » PyQwt 5.2.0 » qt3examples » CurveDemo3.py
#!/usr/bin/env python

# The Python version of qwt-*/examples/curvdemo2
# with a ConfigDialog contributed by Hans-Peter Jansen.


import sys
from PyQt4 import Qt
import PyQt4.Qwt5 as Qwt
from PyQt4.Qwt5.anynumpy import *

Size=15
USize=13

class ConfigDiag(Qt.QDialog):

    def __init__(self, parent = None):
        Qt.QDialog.__init__(self, parent)
        formLayout = Qt.QGridLayout()
        self.setLayout(formLayout)
        
        speedLayout = Qt.QHBoxLayout()
        speedLabel = Qt.QLabel("Timer [s]")
        speedLayout.addWidget(speedLabel)
        speedCounter = Qwt.QwtCounter()
        speedCounter.setRange(0.002, 1.0, 0.001)
        speedLayout.addWidget(speedCounter)
        formLayout.addLayout(speedLayout, 0, 0)
        self.s = speedLayout

        dismissButton = Qt.QPushButton("Dismiss")
        formLayout.addWidget(dismissButton, 1, 0)
        
        self.speedCounter = speedCounter
        self.connect(dismissButton,
                     Qt.SIGNAL("clicked()"),
                     self.accept)
        self.connect(self.speedCounter,
                     Qt.SIGNAL("valueChanged(double)"),
                     self.changeTimerSpeed)

    # __init__()
    
    def setTimerSpeed(self, t):
        self.speedCounter.setValue(t)

    # setTimerSpeed()

    def changeTimerSpeed(self, t):
        self.emit(Qt.SIGNAL("updateSpeed(double)"), t)

    # changeTimerSpeed()


class CurveDemo(Qt.QFrame):

    def __init__(self, *args):
        Qt.QFrame.__init__(self, *args)

        self.setWindowTitle("Click me to configure me")
        self.setFrameStyle(Qt.QFrame.Box | Qt.QFrame.Raised)
        self.setLineWidth(2)
        self.setMidLineWidth(3)

        p = Qt.QPalette()
        p.setColor(self.backgroundRole(), Qt.QColor(30, 30, 50))
        self.setPalette(p)
        # make curves and maps
        self.tuples = []
        # curve 1
        curve = Qwt.QwtPlotCurve()
        curve.setPen(
            Qt.QPen(Qt.QColor(150, 150, 200), 2))
        curve.setStyle(Qwt.QwtPlotCurve.Spline)
        curve.setCurveAttribute(Qwt.QwtPlotCurve.Xfy)
        curve.setSymbol(Qwt.QwtSymbol(Qwt.QwtSymbol.XCross,
                                      Qt.QBrush(),
                                      Qt.QPen(Qt.Qt.yellow, 2),
                                      Qt.QSize(7, 7)))
        self.tuples.append((curve,
                            Qwt.QwtScaleMap(0, 100, -1.5, 1.5),
                            Qwt.QwtScaleMap(0, 100, 0.0, 2*pi)))
        # curve 2
        curve = Qwt.QwtPlotCurve()
        curve.setPen(Qt.QPen(Qt.QColor(200, 150, 50),
                                1,
                                Qt.Qt.DashDotDotLine))
        curve.setStyle(Qwt.QwtPlotCurve.Sticks)
        curve.setSymbol(Qwt.QwtSymbol(Qwt.QwtSymbol.Ellipse,
                                      Qt.QBrush(Qt.Qt.blue),
                                      Qt.QPen(Qt.Qt.yellow),
                                      Qt.QSize(5, 5)))
        self.tuples.append((curve,
                            Qwt.QwtScaleMap(0, 100, 0.0, 2*pi),
                            Qwt.QwtScaleMap(0, 100, -3.0, 1.1)))
        # curve 3
        curve = Qwt.QwtPlotCurve()
        curve.setPen(Qt.QPen(Qt.QColor(100, 200, 150)))
        curve.setStyle(Qwt.QwtPlotCurve.Spline)
        curve.setCurveAttribute(Qwt.QwtPlotCurve.Periodic)
        curve.setCurveAttribute(Qwt.QwtPlotCurve.Parametric)
        curve.setSplineSize(200)
        self.tuples.append((curve,
                            Qwt.QwtScaleMap(0, 100, -1.1, 3.0),
                            Qwt.QwtScaleMap(0, 100, -1.1, 3.0)))
        # curve 4
        curve = Qwt.QwtPlotCurve()
        curve.setPen(Qt.QPen(Qt.Qt.red))
        curve.setStyle(Qwt.QwtPlotCurve.Spline)
        curve.setSplineSize(200)
        self.tuples.append((curve,
                            Qwt.QwtScaleMap(0, 100, -5.0, 1.1),
                            Qwt.QwtScaleMap(0, 100, -1.1, 5.0)))
        # data
        self.phase = 0.0
        self.base = arange(0.0, 2.01*pi, 2*pi/(USize-1))
        self.uval = cos(self.base)
        self.vval = sin(self.base)
        self.uval[1::2] *= 0.5
        self.vval[1::2] *= 0.5
        self.newValues()
        # timer config
        self.config = ConfigDiag()
        self.connect(self.config,
                     Qt.SIGNAL('updateSpeed(double)'),
                     self.setTimerSpeed)
         # start timer
        self.tid = None
        self.setTimerSpeed(0.02)
        self.config.setTimerSpeed(0.02)

    # __init__()

    def mousePressEvent(self, e):
        if not self.config.isVisible():
            self.config.show()
        else:
            self.config.hide()

    # mousePressEvent()

    def setTimerSpeed(self, seconds):
        if self.tid:
            self.killTimer(self.tid)
        self.tid = self.startTimer(int(seconds * 1000.0))

    # setTSpeed()

    def paintEvent(self, event):
        Qt.QFrame.paintEvent(self,event)
        painter = Qt.QPainter(self)
        #painter.setRenderHint(Qt.QPainter.Antialiasing)
        painter.setClipRect(self.contentsRect())
        self.drawContents(painter)

    # paintEvent()

    def drawContents(self, painter):
        r = self.contentsRect()
        for curve, xMap, yMap in self.tuples:
            xMap.setPaintInterval(r.left(), r.right())
            yMap.setPaintInterval(r.top(), r.bottom())
            curve.draw(painter, xMap, yMap, r)

    # drawContents()

    def timerEvent(self, event):
        self.newValues()
        self.repaint()

    # timerEvent()
    
    def newValues(self):
        phase = self.phase
        
        self.xval = arange(0, 2.01*pi, 2*pi/(Size-1))
        self.yval = sin(self.xval - phase)
        self.zval = cos(3*(self.xval + phase))
    
        s = 0.25 * sin(phase)
        c = sqrt(1.0 - s*s)
        u = self.uval
        self.uval = c*self.uval-s*self.vval
        self.vval = c*self.vval+s*u

        self.tuples[0][0].setData(self.yval, self.xval)
        self.tuples[1][0].setData(self.xval, self.zval)
        self.tuples[2][0].setData(self.yval, self.zval)
        self.tuples[3][0].setData(self.uval, self.vval)
        
        self.phase += 2*pi/100
        if self.phase>2*pi:
            self.phase = 0.0

    # newValues()

# class CurveDemo


def make():
    demo = CurveDemo()
    demo.resize(300, 300)
    demo.show()
    return demo

# make()

 
def main(args):
    app = Qt.QApplication(args)
    demo = make()
    sys.exit(app.exec_())

# main()

 
# Admire!         
if __name__ == '__main__':
    main(sys.argv)

# Local Variables: ***
# mode: python ***
# End: ***
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.