klavye_label.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from PyQt5.QtWidgets import QLabel
  2. from PyQt5.QtGui import QPixmap, QPainter, QPen, QColor
  3. from PyQt5.QtCore import QPoint, pyqtSignal
  4. import subprocess
  5. klavye_modeli = {
  6. "pc104" : [
  7. [0x29, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd],
  8. [0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x2b],
  9. [0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28],
  10. [0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35]
  11. ],
  12. "pc105" : [
  13. [0x29, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd],
  14. [0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b],
  15. [0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x2b],
  16. [0x56, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35]
  17. ],
  18. "pc106" : [
  19. [0x29, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe],
  20. [0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b],
  21. [0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29],
  22. [0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36]
  23. ]
  24. }
  25. class KlavyeLabel(QLabel):
  26. klavye_bilgi = pyqtSignal(str, str, str) # model, layout, variant = pc105, tr, f
  27. ciziliyor = False
  28. tus_yerlesimi = None
  29. klavye_modeli = None
  30. def __init__(self, ebeveyn=None):
  31. super().__init__()
  32. self.e = ebeveyn
  33. self.setFixedSize(900, 253)
  34. self.klavye_bilgi.connect(self.proc)
  35. def proc(self, model, layout, variant):
  36. if model in klavye_modeli:
  37. self.tus_yerlesimi = self.unicodeToString(model, layout, variant)
  38. if self.tus_yerlesimi:
  39. self.ciziliyor = True
  40. self.klavye_modeli = model
  41. self.update()
  42. else:
  43. self.ciziliyor = False
  44. def unicodeToString(self, model, layout, variant=""):
  45. try:
  46. keycodes = {}
  47. tus_yerlesimi_command = subprocess.Popen(["ckbcomp", "-model", model, "-layout", layout, "-variant", variant],
  48. stdout=subprocess.PIPE)
  49. ciktilar = tus_yerlesimi_command.stdout.read()
  50. for cikti in ciktilar.decode("utf-8").split("\n"):
  51. if cikti.startswith("keycode") and cikti.count("="):
  52. cikti = cikti.split()
  53. if cikti[3].startswith("U+") or cikti[3].startswith("+U"):
  54. first = bytes("\\u" + cikti[3][2:].replace("+", ""), "ascii").decode("unicode-escape")
  55. second = bytes("\\u" + cikti[4][2:].replace("+", ""), "ascii").decode("unicode-escape")
  56. keycodes[int(cikti[1])] = [first, second]
  57. return keycodes
  58. except:
  59. return False
  60. def paintEvent(self, event):
  61. boyayici = QPainter(self)
  62. boyayici.setRenderHints(QPainter.SmoothPixmapTransform|QPainter.Antialiasing|QPainter.HighQualityAntialiasing)
  63. boyayici.drawPixmap(QPoint(0, 0), QPixmap("./resimler/klavye.png"))
  64. koordinat_listesi = ((10, 30), (90, 90), (115, 150), (80, 215))
  65. sayac = 0
  66. if self.ciziliyor:
  67. for key_list in klavye_modeli[self.klavye_modeli]:
  68. coordinat = koordinat_listesi[sayac]
  69. sayac += 1
  70. for num, key in enumerate(key_list):
  71. try:
  72. font = boyayici.font()
  73. font.setPointSize(12)
  74. boyayici.setFont(font)
  75. big = QPen(QColor("#ccff00"))
  76. boyayici.setPen(big)
  77. boyayici.drawText(coordinat[0]+(60*num), coordinat[1], self.tus_yerlesimi[key][1])
  78. font = boyayici.font()
  79. font.setPointSize(14)
  80. boyayici.setFont(font)
  81. little = QPen(QColor(255, 255, 255))
  82. boyayici.setPen(little)
  83. boyayici.drawText(coordinat[0]+15+(60*num), coordinat[1]+20, self.tus_yerlesimi[key][0])
  84. except KeyError as err:
  85. print(self.tus_yerlesimi, key)