File: test_parameter_interaction_timer.py

package info (click to toggle)
camitk 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 389,496 kB
  • sloc: cpp: 103,476; sh: 2,448; python: 1,618; xml: 984; makefile: 128; perl: 84; sed: 20
file content (45 lines) | stat: -rw-r--r-- 1,675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# This is a CamiTK python action
#
# Test the interaction between python and the CamiTK application using a parameter and a timer.
# This is a simple test where a counter is incremented in the tick() method called by a timer()
# that lives in Python.
# 
# It demonstrates how state can be preserved between two timer ticks calls using the action
# parameters. 
# Click 'Start'/'Stop' to start/stop the timer. 
# Note that the timer interval is slowed down/accelerated automatically within the tick() method.

import camitk
from PySide2.QtCore import QTimer
import sys

def tick(self:camitk.Action):
    counter = self.getParameterValue("Counter")
    print(f"test_parameter_interaction_timer tick {counter} (this is stderr)", file=sys.stderr) # first print is on stderr

    counter += 1
    self.setParameterValue("Counter", counter)

    self.timer.setInterval(int(1000.0 / float(counter%10 + 1)))
    self.updateWidget()
    print(f"test_parameter_interaction_timer tick {counter} (this is stdout)") # second print on stdout
    
def init(self:camitk.Action):
    print("test_parameter_interaction_timer init", file=sys.stderr)
    self.timer = None
    self.setApplyButtonText("Start")

def process(self:camitk.Action):
    self.counter = self.getParameterValue("Counter")
    print(f"test_parameter_interaction_timer process {self.counter}", file=sys.stderr)

    if not self.timer:
        self.timer = QTimer()
        connected = self.timer.timeout.connect(lambda: tick(self))     

    if not self.timer.isActive():
        self.setApplyButtonText("Stop")
        self.timer.start(1000)
    else:
        self.setApplyButtonText("Restart")
        self.timer.stop()