-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.rb
153 lines (152 loc) · 4.74 KB
/
mainwindow.rb
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
require 'Qt4'
require 'ui_mainwindow'
require 'calibratewidget'
require 'homography'
require 'cwiid'
require 'x11test'
class MainWindow < Qt::MainWindow
DELAY = 5000
slots 'wiimote(bool)'
slots 'calibrate()'
slots 'calib_step(int)'
slots 'calib_finish()'
slots 'calib_abort()'
slots 'tray(QSystemTrayIcon::ActivationReason)'
signals 'ir_clicked(int,int)'
signals 'ir_released()'
def initialize(parent = nil)
super parent
@ui = Ui::MainWindow.new
@ui.setupUi self
tray_menu = Qt::Menu.new self do |t|
t.addAction @ui.action_connect
t.addAction @ui.action_calibrate
t.addSeparator
t.addAction @ui.action_quit
end
@tray = Qt::SystemTrayIcon.new self
@tray.contextMenu = tray_menu
@tray.icon = windowIcon
connect @ui.action_connect, SIGNAL('toggled(bool)'), self, SLOT('wiimote(bool)')
connect @ui.action_calibrate, SIGNAL('triggered()'), self, SLOT('calibrate()')
connect @ui.action_quit, SIGNAL('triggered()'), $qApp, SLOT('quit()')
connect @tray, SIGNAL('activated(QSystemTrayIcon::ActivationReason)'),
self, SLOT('tray(QSystemTrayIcon::ActivationReason)')
@tray.show
@wiimote = nil
@timer = 0
@state = false
@calibration = nil
@homography = nil
end
def wiimote(on)
begin
default = cursor
setCursor Qt::Cursor.new(Qt::WaitCursor)
if on
@wiimote = WiiMote.new
@wiimote.rpt_mode = WiiMote::RPT_BTN | WiiMote::RPT_IR
@ui.action_calibrate.enabled = true
@timer = startTimer 0
@tray.showMessage windowTitle, 'Established Bluetooth connection to Wii remote',
Qt::SystemTrayIcon::Information, DELAY
elsif @wiimote
@ui.action_calibrate.enabled = false
killTimer @timer
@homography = nil
@calibration = nil
@state = false
@timer = 0
@wiimote.close
@wiimote = nil
@tray.showMessage windowTitle, 'Dropped connection to Wii Remote',
Qt::SystemTrayIcon::Information, DELAY
end
setCursor default
rescue Exception => e
setCursor default
@tray.showMessage windowTitle, e.message, Qt::SystemTrayIcon::Critical, DELAY
@ui.action_connect.checked = false
end
end
def timerEvent(e)
led_next = Time.new.sec % 4
@wiimote.get_state
pos = @wiimote.ir.size > 0 ? @wiimote.ir.sort_by { |x,y,r| r }.last : nil
if pos and @homography
screen = @homography.transform pos[0], pos[1]
fake_motion_event screen[0].to_i, screen[1].to_i, 0
end
if pos.nil? == @state
@state = !@state
if @state
if @homography
fake_button_event 1, true, 0
else
emit ir_clicked(pos[0], pos[1])
end
else
if @homography
fake_button_event 1, false, 0
else
emit ir_released
end
end
end
end
def closeEvent(e)
if @tray.visible?
message = <<EOS
The application will continue to run in the system tray.
To terminate the program right-click on the tray icon and
choose Quit in the context menu.
EOS
@tray.showMessage windowTitle, message, Qt::SystemTrayIcon::Information, DELAY
hide
e.ignore
end
end
def calibrate
@homography = nil
@calibration = CalibrateWidget.new
flags = Qt::Enum.new 0, 'Qt::WindowFlags'
#flags |= Qt::DialogType
#flags |= Qt::FramelessWindowHint
#@calibration.windowFlags = flags
#@calibration.setWindowFlags(Qt::FramelessWindowHint || Qt::DialogType)
@calibration.setWindowFlags(Qt::FramelessWindowHint || Qt::DialogType)
@calibration.cursor = Qt::Cursor.new Qt::BlankCursor
connect @ui.action_quit, SIGNAL('triggered()'), self, SLOT('close()')
connect self, SIGNAL('ir_clicked(int,int)'), @calibration, SLOT('add_point(int,int)')
connect @calibration, SIGNAL('step_changed(int)'), self, SLOT('calib_step(int)')
connect @calibration, SIGNAL('finished()'), self, SLOT('calib_finish()')
connect @calibration, SIGNAL('aborted()'), self, SLOT('calib_abort()')
@calibration.showFullScreen
@wiimote.led = 1 << @calibration.id
end
def calib_step(step)
@wiimote.led = 1 << step
end
def calib_finish
@wiimote.led = 0
disconnect self, nil, @calibration, nil
@homography = Homography.new *@calibration.point_pairs
@tray.showMessage windowTitle, 'Wii remote whiteboard was calibrated',
Qt::SystemTrayIcon::Information, DELAY
end
def calib_abort
disconnect self, nil, @calibration, nil
@wiimote.led = 0
@tray.showMessage windowTitle, 'Calibration was aborted',
Qt::SystemTrayIcon::Information, DELAY
end
def tray(reason)
if reason == Qt::SystemTrayIcon::Trigger
if visible?
hide
else
show
end
end
end
end