proto-magpy.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. from PyQt6.QtCore import *
  3. from PyQt6.QtGui import *
  4. from PyQt6.QtWidgets import *
  5. import sys
  6. class MagpyPlainTextEdit(QPlainTextEdit):
  7. def focusOutEvent(self, event):
  8. super().focusOutEvent(event)
  9. if event.lostFocus():
  10. self.parentWidget().to_view(event)
  11. class MagpyTextToggle(QStackedWidget):
  12. def __init__(self, parent, doc=None):
  13. QStackedWidget.__init__(self, parent)
  14. self.editbox = MagpyPlainTextEdit(self)
  15. self.displaybox = QTextEdit(self)
  16. self.displaybox.setReadOnly(True)
  17. self.addWidget(self.editbox)
  18. self.addWidget(self.displaybox)
  19. if doc:
  20. self.doc = doc
  21. else:
  22. self.doc = QTextDocument(self)
  23. self.doc.setDocumentLayout(QPlainTextDocumentLayout(self.doc))
  24. self.editbox.setDocument(self.doc)
  25. self.displaybox.mouseReleaseEvent=self.to_edit
  26. self.to_view(None)
  27. def to_edit(self, event):
  28. print("to_edit!")
  29. self.setCurrentIndex(0)
  30. def to_view(self, event):
  31. print("to_view!")
  32. self.setCurrentIndex(1)
  33. self.displaybox.setMarkdown(self.doc.toPlainText())
  34. class MagpyContainer(QWidget):
  35. def __init__(self, parent, doc=None):
  36. QWidget.__init__(self, parent)
  37. vlayout = QVBoxLayout(self)
  38. for i in range(8):
  39. vlayout.addWidget(MagpyTextToggle(self, doc))
  40. self.setLayout(vlayout)
  41. # scroll = QScrollArea(self)
  42. # scroll.setWidget(self)
  43. # scroll.setWidgetResizable(True)
  44. class ProtoMagpy(QMainWindow):
  45. def __init__(self, parent=None):
  46. QMainWindow.__init__(self, parent)
  47. self.filename = ''
  48. self.actions = {}
  49. self.initUI()
  50. self.initActions()
  51. def initActions(self):
  52. self.actions['quit'] = QAction('Quit', self)
  53. self.actions['quit'].setShortcut(QKeySequence('Ctrl+Q'))
  54. self.actions['quit'].triggered.connect(self.close)
  55. s = QShortcut(QKeySequence('Ctrl+T'), self)
  56. s.activated.connect(self.pagedown)
  57. def pagedown(self):
  58. print(QApplication.focusWidget())
  59. def initUI(self):
  60. top_level = MagpyContainer(self)
  61. self.setCentralWidget(top_level)
  62. self.statusbar = self.statusBar()
  63. self.setGeometry(100, 100, 1030, 800)
  64. self.setWindowTitle("Proto-Magpy")
  65. def main():
  66. app = QApplication(sys.argv)
  67. main = ProtoMagpy()
  68. main.show()
  69. sys.exit(app.exec())
  70. if __name__ == '__main__':
  71. main()