view.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, os
  4. from PyQt5 import QtCore
  5. from PyQt5.QtCore import QUrl
  6. from PyQt5.QtGui import QFont, QSyntaxHighlighter
  7. from PyQt5.QtWidgets import QWidget, QLabel, QTabWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPlainTextEdit
  8. from PyQt5.QtWebKit import QWebSettings
  9. from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
  10. class WebkitView(QWebView):
  11. def __init__(self, port):
  12. self.port = port
  13. self.view = QWebView.__init__(self)
  14. self.load(QUrl('http://localhost:'+str(self.port)))
  15. self.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
  16. # self.settings().setAttribute(QWebSettings.PluginsEnabled, True)
  17. class WebkitInspector(QWebInspector):
  18. def __init__(self, webkitview):
  19. super(WebkitInspector, self).__init__()
  20. self.webkitview = webkitview
  21. self.setPage(self.webkitview.page())
  22. # TODO: webkitinspector is disappearing when chaging tabs
  23. class CodeEditor(QWidget):
  24. def __init__(self, core):
  25. super(CodeEditor, self).__init__()
  26. self.core = core
  27. self.layout = QVBoxLayout(self)
  28. self.layout.setContentsMargins(0,0,0,0)
  29. # Initialize tab screen
  30. self.tabs = QTabWidget()
  31. self.scsstab = QPlainTextEdit()
  32. self.scssfile = open(os.path.join(self.core.cwd,'assets/scss/styles.scss'))
  33. scssstr = self.scssfile.read()
  34. self.scsstab.insertPlainText(scssstr)
  35. self.jstab = QPlainTextEdit()
  36. self.jsfile = open(os.path.join(self.core.cwd,'assets/js/script.js'))
  37. jsstr = self.jsfile.read()
  38. self.jstab.insertPlainText(jsstr)
  39. # Add tabs
  40. self.tabs.addTab(self.scsstab,"scss")
  41. self.tabs.addTab(self.jstab,"js")
  42. # Add tabs to widget
  43. self.layout.addWidget(self.tabs)
  44. self.setLayout(self.layout)
  45. # font = QFont()
  46. # font.setFamily('Courier')
  47. # font.setFixedPitch(True)
  48. # font.setPointSize(10)
  49. # self.setFont(font)
  50. # self.highlighter = Highlighter(self.document())
  51. # https://pypi.python.org/pypi/QScintilla/2.9.2
  52. class ViewStack(QWidget):
  53. def __init__(self, core):
  54. super(ViewStack, self).__init__()
  55. # self.grid = QGridLayout()
  56. hbox = QHBoxLayout()
  57. hbox.setContentsMargins(0,0,0,0)
  58. self.setLayout(hbox)
  59. # webviewbox = QVBoxLayout()
  60. vsplitter = QSplitter(QtCore.Qt.Vertical)
  61. self.webkitview = WebkitView(core.server.port)
  62. vsplitter.addWidget(self.webkitview)
  63. self.webkitinspector = WebkitInspector(self.webkitview)
  64. vsplitter.addWidget(self.webkitinspector)
  65. hsplitter = QSplitter(QtCore.Qt.Horizontal)
  66. hsplitter.addWidget(vsplitter)
  67. self.codeeditor = CodeEditor(core)
  68. hsplitter.addWidget(self.codeeditor)
  69. hbox.addWidget(hsplitter)
  70. # def onChanged(self, text):
  71. # print("ViewTba Layout Changed")
  72. # self.lbl.setText(text)
  73. # self.lbl.adjustSize()