-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktimer.rb
More file actions
128 lines (105 loc) · 3.06 KB
/
Copy pathworktimer.rb
File metadata and controls
128 lines (105 loc) · 3.06 KB
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
require 'tk'
require 'tkextlib/tile'
# APP LOGIC
root = TkRoot.new {title "Work Time Allocator"}
content = Tk::Tile::Frame.new(root)
$content = content
$lastswitchtime = 0 #variable holds the last time projects were switched
$totaltime = 0 #total time worked for the day
class Project
@@projects = []
def initialize(name)
@name = name
@timeworked = 0
@@projects << self # adds new project to a hash of all projects
end
def button(col)
name = @name
thisproject = self # if you just put 'self' inside the button command it pulls the button, not the project the button is in
tkbutton = Tk::Tile::Button.new($content) {text name; command {thisproject.engage}}
tkbutton.grid :column => col, :row => 1
end
def timedisplay(col)
timeworked = @timeworked
tkfield = Tk::Tile::Label.new($content) {text timeworked}
tkfield.grid :column=> col, :row => 2
@fieldtimeworked = tkfield
end
def updatetime
@timeworked += (Time.now - $lastswitchtime)
@fieldtimeworked['text']=@timeworked.hourise
end
def engage
disengage #disengage from any other project
$engaged = self
$lastswitchtime = Time.now
clockin if !$clockintime
end
def self.all
@@projects
end
def cleartime
@timeworked = 0
@fieldtimeworked['text']=@timeworked
end
end
#extend the Float class to allow for making any time number a decimal hour to 2 places
class Float
def hourise
(self / 3600).truncate(2)
end
end
require "./project_list" # separate file with a list of projects
nonproj = Project.new('non-project time')
def putprojs
currentcolumn = 0
Project.all.each do |proj|
proj.button(currentcolumn)
proj.timedisplay(currentcolumn)
currentcolumn += 1
end
$totalcolumn = currentcolumn -1
end
putprojs
# --- NON-VARIABLE UI ELEMENTS ---
buttclockin = Tk::Tile::Button.new(content) {text "clock in"; command {clockin}}
buttclockout = Tk::Tile::Button.new(content) {text "clock out"; command {clockout}}
$viewclock = Tk::Tile::Label.new(content) {text 'clocked out'}
buttclear = Tk::Tile::Button.new(content) {text 'clear all'; command {clearall}}
buttdisengage = Tk::Tile::Button.new(content) {text 'disengage all'; command {disengage}}
content.grid :column => 0, :row => 0
buttclockin.grid :column => ($totalcolumn / 2)-1, :row => 0
buttclockout.grid :column => ($totalcolumn / 2)+1, :row => 0
$viewclock.grid :column => ($totalcolumn / 2), :row => 0
buttclear.grid :column => $totalcolumn, :row => 0
buttdisengage.grid :column => $totalcolumn, :row => 3
# --- END NON-VARIABLE ---
def clockin
$clockintime = Time.now
$viewclock['text']='clocked in'
end
def clockout
if $clockintime
disengage
clockouttime = Time.now
$totaltime = clockouttime - $clockintime + $totaltime
$viewclock['text'] = $totaltime.hourise
$clockintime = nil
else
return nil
end
end
def clearall
$clockintime = nil
$totaltime = 0
$viewclock['text'] = 'clocked out'
disengage
Project.all.each do |proj|
proj.cleartime
end
end
def disengage
$engaged.updatetime if $engaged
$engaged = nil
end
Tk.mainloop