summaryrefslogtreecommitdiffstats
path: root/displayconfig/ktimerdialog.py
blob: 5cca210e3301cf29b5ab5a7d8b39ea363094dd6c (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python
###########################################################################
# ktimerdialog.py - description                                           #
# ------------------------------                                          #
# begin     : Mon Jul 26 2004                                             #
# copyright : (C) 2004 by Simon Edwards                                   #
# email     : simon@simonzone.com                                         #
#                                                                         #
###########################################################################
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
###########################################################################
# Based on Hamish Rodda's ktimerdialog.cpp.

from qt import *
from kdecore import *
from kdeui import *

class KTimerDialog(KDialogBase):
    CountDown = 0
    CountUp = 1
    Manual = 2

    def __init__(self, msec, style, parent, name, modal, caption="", \
            buttonmask=KDialogBase.Ok|KDialogBase.Cancel|KDialogBase.Apply, \
            defaultbutton=KDialogBase.Cancel, separator=False, \
            user1=KGuiItem(), user2=KGuiItem(), user3=KGuiItem()):
        """Parameters:

        msec - integer, timeout in milliseconds
        style - TimerStyle object.
        parent - parent QWidget
        name - String
        model - boolean.
        caption - String
        buttonmask - integer
        defaultbutton - ButtonCode
        separator - boolean
        user1 - KGuiItem
        user2 - KGuiItem
        user3 - KGuiItem
        """

        KDialogBase.__init__(self,parent, name, modal, caption, buttonmask, defaultbutton, \
                 separator, user1, user2, user3 )

        self.totaltimer = QTimer(self)
        self.updatetimer = QTimer(self)
        self.msectotal = self.msecremaining = msec
        self.updateinterval = 1000
        self.tstyle = style

        # default to cancelling the dialog on timeout
        if buttonmask & self.Cancel:
            self.buttonontimeout = self.Cancel

        self.connect(self.totaltimer, SIGNAL("timeout()"), self.slotInternalTimeout)
        self.connect(self.updatetimer, SIGNAL("timeout()"), self.slotUpdateTime)

        # create the widgets
        self.mainwidget = QVBox(self, "mainWidget")
        self.timerwidget = QHBox(self.mainwidget, "timerWidget")
        self.timerlabel = QLabel(self.timerwidget)
        self.timerprogress = QProgressBar(self.timerwidget)
        self.timerprogress.setTotalSteps(self.msectotal)
        self.timerprogress.setPercentageVisible(False)
        self.setMainWidget(self.mainwidget)
        self.slotUpdateTime(False)

    def show(self):
        self.msecremaining = self.msectotal
        self.slotUpdateTime(False)
        KDialogBase.show(self)
        self.totaltimer.start(self.msectotal, True)
        self.updatetimer.start(self.updateinterval, False)

    def exec_loop(self):
        self.totaltimer.start(self.msectotal, True)
        self.updatetimer.start(self.updateinterval, False)
        return KDialogBase.exec_loop(self)

    def setMainWidget(self, newmainwidget):
        # yuck, here goes.
        newwidget = QVBox(self)

        if newmainwidget.parentWidget()!=self.mainwidget:
            newmainwidget.reparent(newwidget, 0, QPoint(0,0))
        else:
            newwidget.insertChild(newmainwidget)

        self.timerwidget.reparent(newwidget, 0, QPoint(0, 0))

        self.mainwidget = newwidget
        KDialogBase.setMainWidget(self, self.mainwidget)

    def setRefreshInterval(self, msec):
        self.updateinterval = msec;
        if self.updatetimer.isActive():
            self.updatetimer.changeInterval(self.updateinterval)

    def timeoutButton(self):
        return self.buttonontimeout

    def setTimeoutButton(self, newbutton):
        self.buttonontimeout = newbutton

    def timerStyle(self):
        return self.tstyle

    def setTimerStyle(self, newstyle):
        self.tstyle = newstyle

    def slotUpdateTime(self, update=True):
        if update:
            if self.tstyle==self.CountDown:
                    self.msecremaining -= self.updateinterval
            elif self.tstyle==self.CountUp:
                    self.msecremaining += self.updateinterval

        self.timerprogress.setProgress(self.msecremaining)
        self.timerlabel.setText( i18n("%1 seconds remaining:").arg(self.msecremaining/1000.0) )

    def slotInternalTimeout(self):
        #self.emit(SIGNAL("timerTimeout()"), () )
        if self.buttonontimeout==self.Help:
            self.slotHelp()
        elif self.buttonontimeout==self.Default:
            self.slotDefault()
        elif self.buttonontimeout==self.Ok:
            self.slotOk()
        elif self.buttonontimeout==self.Apply:
            self.applyPressed()
        elif self.buttonontimeout==self.Try:
            self.slotTry()
        elif self.buttonontimeout==self.Cancel:
            self.slotCancel()
        elif self.buttonontimeout==self.Close:
            self.slotClose()
        #case User1:
        #    slotUser1();
        #case User2:
        #    slotUser2();
        #    break;
        elif self.buttonontimeout==self.User3:
            self.slotUser3()
        elif self.buttonontimeout==self.No:
            self.slotNo()
        elif self.buttonontimeout==self.Yes:
            self.slotCancel()
        elif self.buttonontimeout==self.Details:
            self.slotDetails()