design.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, os, re
  4. from PyQt5 import QtCore
  5. from PyQt5.QtCore import QUrl, QSettings
  6. from PyQt5.QtGui import QKeySequence, QFont, QSyntaxHighlighter
  7. from PyQt5.QtWidgets import QWidget, QLabel, QTabWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPlainTextEdit, QShortcut
  8. from PyQt5.QtWebKit import QWebSettings
  9. from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
  10. class WebkitView(QWebView):
  11. def __init__(self, parent, port):
  12. self.port = port
  13. self.view = QWebView.__init__(self, parent)
  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, parent, webkitview):
  19. super(WebkitInspector, self).__init__(parent)
  20. self.webkitview = webkitview
  21. self.setPage(self.webkitview.page())
  22. # TODO: webkitinspector is disappearing when chaging tabs
  23. class CodeEditor(QPlainTextEdit):
  24. def __init__(self, core, tabs, file=None):
  25. super(CodeEditor, self).__init__()
  26. self.core = core
  27. self.tabs = tabs
  28. # self.file = file
  29. self.file = os.path.join(self.core.cwd,file)
  30. self.insertPlainText(open(self.file, 'r').read())
  31. self.changed = False
  32. self.textChanged.connect(self.onTextChanged)
  33. self.shortcut = QShortcut(QKeySequence("Ctrl+s"), self)
  34. self.shortcut.activated.connect(self.save)
  35. def onTextChanged(self):
  36. # print('textChanged')
  37. # print(self.toPlainText())
  38. # open(self.file, 'w').write(self.toPlainText())
  39. if not self.changed:
  40. self.changed = True
  41. i = self.tabs.currentIndex()
  42. self.tabs.setTabText(i, "* "+self.tabs.tabText(i))
  43. def save(self):
  44. if self.changed:
  45. open(self.file, 'w').write(self.toPlainText())
  46. i = self.tabs.currentIndex()
  47. self.tabs.setTabText(i, re.sub(r'^\*\s', '', self.tabs.tabText(i)))
  48. self.changed = False
  49. # TODO: how to combine file save and project save
  50. class Editor(QWidget):
  51. def __init__(self, parent, core):
  52. super(Editor, self).__init__()
  53. self.core = core
  54. self.layout = QVBoxLayout(self)
  55. self.layout.setContentsMargins(0,0,0,0)
  56. # Initialize tab screen
  57. self.tabs = QTabWidget()
  58. self.scsstab = CodeEditor(core, self.tabs, 'assets/scss/styles.scss')
  59. self.jstab = CodeEditor(core, self.tabs, 'assets/js/script.js')
  60. # Add tabs
  61. self.tabs.addTab(self.scsstab,"scss")
  62. self.tabs.addTab(self.jstab,"js")
  63. # Add tabs to widget
  64. self.layout.addWidget(self.tabs)
  65. self.setLayout(self.layout)
  66. # font = QFont()
  67. # font.setFamily('Courier')
  68. # font.setFixedPitch(True)
  69. # font.setPointSize(10)
  70. # self.setFont(font)
  71. # self.highlighter = Highlighter(self.document())
  72. # https://pypi.python.org/pypi/QScintilla/2.9.2
  73. class DesignStack(QWidget):
  74. def __init__(self, core):
  75. super(DesignStack, self).__init__()
  76. # self.grid = QGridLayout()
  77. hbox = QHBoxLayout()
  78. hbox.setContentsMargins(0,0,0,0)
  79. self.setLayout(hbox)
  80. # webviewbox = QVBoxLayout()
  81. self.vsplitter = QSplitter(QtCore.Qt.Vertical)
  82. self.webkitview = WebkitView(self, core.server.port)
  83. self.vsplitter.addWidget(self.webkitview)
  84. self.webkitinspector = WebkitInspector(self, self.webkitview)
  85. self.vsplitter.addWidget(self.webkitinspector)
  86. shortcut = QShortcut(self)
  87. shortcut.setKey("F12")
  88. shortcut.activated.connect(self.toggleInspector)
  89. self.webkitinspector.setVisible(False)
  90. self.vsplitter.splitterMoved.connect(self.movedSplitter)
  91. self.hsplitter = QSplitter(QtCore.Qt.Horizontal)
  92. self.hsplitter.addWidget(self.vsplitter)
  93. self.editor = Editor(self, core)
  94. self.hsplitter.addWidget(self.editor)
  95. self.hsplitter.splitterMoved.connect(self.movedSplitter)
  96. hbox.addWidget(self.hsplitter)
  97. self.restorePrefs()
  98. def toggleInspector(self):
  99. self.webkitinspector.setVisible(not self.webkitinspector.isVisible())
  100. def restorePrefs(self):
  101. settings = QSettings('FiguresLibres', 'Cascade')
  102. print(settings.value('design/vsplitter/sizes', self.vsplitter.sizes()))
  103. vals = settings.value('design/vsplitter/sizes', None)
  104. if vals:
  105. sizes = []
  106. for size in vals: sizes.append(int(size))
  107. self.vsplitter.setSizes(sizes)
  108. vals = settings.value('design/hsplitter/sizes', None)
  109. if vals:
  110. sizes = []
  111. for size in vals: sizes.append(int(size))
  112. self.hsplitter.setSizes(sizes)
  113. def movedSplitter(self):
  114. settings = QSettings('FiguresLibres', 'Cascade')
  115. print(self.vsplitter.sizes())
  116. settings.setValue('design/vsplitter/sizes', self.vsplitter.sizes())
  117. settings.setValue('design/hsplitter/sizes', self.hsplitter.sizes())