controlpanel.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Copyright (C) 2012-2014 Diego Torres Milano
  4. Created on oct 30, 2014
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. @author: Diego Torres Milano
  15. @author: Ahmed Kasem
  16. '''
  17. __version__ = '10.3.0'
  18. import sys, os
  19. import Tkinter, tkFileDialog, ttk
  20. from com.dtmilano.android.culebron import Operation, Unit, Color
  21. class Key:
  22. GOOGLE_NOW='KEYCODE_ASSIST'
  23. PERIOD='KEYCODE_PERIOD'
  24. GO='KEYCODE_ENTER'
  25. class Layout:
  26. BUTTON_WIDTH=13
  27. BUTTONS_NUMBER=9
  28. class ControlPanel(Tkinter.Toplevel):
  29. def __init__(self, culebron, vc, printOperation, **kwargs):
  30. self.culebron = culebron
  31. self.vc = vc
  32. self.printOperation = printOperation
  33. self.parent = culebron.window
  34. self.childWindow = Tkinter.Toplevel(self.parent)
  35. self.notebook = ttk.Notebook(self.childWindow)
  36. self.notebook.pack(fill=Tkinter.BOTH, padx=2, pady=3)
  37. self.keycodeTab = ttk.Frame(self.notebook)
  38. self.keyboardTab = ttk.Frame(self.notebook)
  39. self.notebook.add(self.keycodeTab, text='KEYCODE')
  40. self.notebook.add(self.keyboardTab, text='KEYBOARD')
  41. self.childWindow.title('Control Panel')
  42. self.childWindow.resizable(width=Tkinter.FALSE, height=Tkinter.FALSE)
  43. self.childWindow.printOperation = printOperation
  44. self.childWindow.vc = vc
  45. self.childWindow.grid()
  46. self.childWindow.column = self.childWindow.row = 0
  47. self.createKeycodeTab()
  48. self.createKeyboardTab()
  49. def createKeycodeTab(self):
  50. ''' KEYCODE '''
  51. self.keycodeList = [
  52. 'KEYCODE_HOME', 'KEYCODE_DPAD_UP', 'KEYCODE_BACK', 'KEYCODE_SEARCH', 'KEYCODE_CHANNEL_UP', 'KEYCODE_TV',
  53. 'KEYCODE_MUSIC', 'KEYCODE_EXPLORER', 'KEYCODE_CAMERA', 'KEYCODE_POWER', 'KEYCODE_DPAD_LEFT','KEYCODE_DPAD_DOWN',
  54. 'KEYCODE_DPAD_RIGHT', 'KEYCODE_PAGE_UP', 'KEYCODE_CHANNEL_DOWN', 'KEYCODE_VOLUME_UP', 'KEYCODE_MEDIA_PLAY',
  55. 'KEYCODE_CONTACTS', 'KEYCODE_ZOOM_IN', 'SNAPSHOPT', 'KEYCODE_MENU', 'KEYCODE_DPAD_CENTER', 'KEYCODE_ENTER',
  56. 'KEYCODE_PAGE_DOWN', 'KEYCODE_BRIGHTNESS_DOWN', 'KEYCODE_VOLUME_DOWN', 'KEYCODE_MEDIA_PAUSE', 'KEYCODE_BOOKMARK',
  57. 'KEYCODE_ZOOM_OUT', 'REFRESH', 'KEYCODE_APP_SWITCH', 'KEYCODE_GOOGLE_NOW', 'KEYCODE_CALL', 'KEYCODE_ESCAPE',
  58. 'KEYCODE_BRIGHTNESS_UP', 'KEYCODE_VOLUME_MUTE', 'KEYCODE_MEDIA_STOP', 'KEYCODE_CALCULATOR', 'KEYCODE_SETTINGS', 'QUIT'
  59. ]
  60. for keycode in self.keycodeList:
  61. self.keycode = ControlPanelButton(self.keycodeTab, self.culebron, self.vc, self.printOperation, value=keycode, text=keycode[8:],
  62. width=Layout.BUTTON_WIDTH, bg=Color.DARK_GRAY, fg=Color.LIGHT_GRAY,
  63. highlightbackground=Color.DARK_GRAY)
  64. if keycode == 'REFRESH':
  65. self.keycode.configure(fg=Color.BLUE, bg=Color.DARK_GRAY, text=keycode, command=self.keycode.refreshScreen)
  66. self.keycode.grid(column=self.childWindow.column, row=self.childWindow.row)
  67. elif keycode == 'SNAPSHOPT':
  68. self.keycode.configure(fg=Color.BLUE, bg=Color.DARK_GRAY, text=keycode, command=self.keycode.takeSnapshot)
  69. self.keycode.grid(column=self.childWindow.column, row=self.childWindow.row)
  70. elif keycode == 'QUIT':
  71. self.keycode.configure(fg=Color.BLUE, bg=Color.DARK_GRAY, text=keycode, command=self.childWindow.destroy)
  72. self.keycode.grid(column=self.childWindow.column, row=self.childWindow.row)
  73. else:
  74. self.keycode.configure(command=self.keycode.command)
  75. self.keycode.grid(column=self.childWindow.column, row=self.childWindow.row)
  76. self.tabLayout()
  77. def createKeyboardTab(self):
  78. ''' KEYBOARD '''
  79. self.keyboardList = [
  80. 'KEYCODE_1', 'KEYCODE_2', 'KEYCODE_3', 'KEYCODE_4', 'KEYCODE_5', 'KEYCODE_6', 'KEYCODE_7', 'KEYCODE_8', 'KEYCODE_9', 'KEYCODE_0',
  81. 'KEYCODE_Q', 'KEYCODE_W', 'KEYCODE_E', 'KEYCODE_R', 'KEYCODE_T', 'KEYCODE_Y', 'KEYCODE_U', 'KEYCODE_I', 'KEYCODE_O', 'KEYCODE_P',
  82. 'KEYCODE_A', 'KEYCODE_S', 'KEYCODE_D', 'KEYCODE_F', 'KEYCODE_G', 'KEYCODE_H', 'KEYCODE_J', 'KEYCODE_K', 'KEYCODE_L',
  83. 'KEYCODE_DEL', 'KEYCODE_Z', 'KEYCODE_X', 'KEYCODE_C', 'KEYCODE_V', 'KEYCODE_B', 'KEYCODE_N', 'KEYCODE_M',
  84. 'KEYCODE_.', 'KEYCODE_SPACE', 'KEYCODE_GO'
  85. ]
  86. for keyboard in self.keyboardList:
  87. self.keyboard = ControlPanelButton(self.keyboardTab, self.culebron, self.vc, self.printOperation, value=keyboard, text=keyboard[8:],
  88. width=Layout.BUTTON_WIDTH, bg=Color.DARK_GRAY, fg=Color.LIGHT_GRAY,
  89. highlightbackground=Color.DARK_GRAY)
  90. self.keyboard.configure(command=self.keyboard.command)
  91. self.keyboard.grid(column=self.childWindow.column, row=self.childWindow.row)
  92. self.tabLayout()
  93. def tabLayout(self):
  94. ''' For all tabs, specify the number of buttons in a row '''
  95. self.childWindow.column += 1
  96. if self.childWindow.column > Layout.BUTTONS_NUMBER:
  97. self.childWindow.column = 0
  98. self.childWindow.row += 1
  99. class ControlPanelButton(Tkinter.Button):
  100. def __init__(self, parent, culebron, vc, printOperation, value=None, **kwargs):
  101. Tkinter.Button.__init__(self, parent, kwargs)
  102. self.culebron = culebron
  103. self.printOperation = printOperation
  104. self.value = value
  105. self.vc = vc
  106. self.device = vc.device
  107. def command(self):
  108. key = self.value
  109. if key == 'KEYCODE_GOOGLE_NOW':
  110. self.device.press(Key.GOOGLE_NOW)
  111. self.printOperation(None, Operation.PRESS, Key.GOOGLE_NOW)
  112. elif key == 'KEYCODE_.':
  113. self.device.press(Key.PERIOD)
  114. self.printOperation(None, Operation.PRESS, Key.PERIOD)
  115. elif key == 'KEYCODE_GO':
  116. self.device.press(Key.GO)
  117. self.printOperation(None, Operation.PRESS, Key.GO)
  118. else:
  119. self.device.press(key)
  120. self.printOperation(None, Operation.PRESS, key)
  121. def refreshScreen(self):
  122. self.culebron.refresh()
  123. def takeSnapshot(self):
  124. # No need to retake snapshot as it is already shown
  125. self.culebron.saveSnapshot()