ToolShell.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. ############################################################
  8. # from PyQt5.QtCore import pyqtSignal
  9. from PyQt5.QtCore import Qt
  10. from PyQt5.QtGui import QTextCursor
  11. from PyQt5.QtWidgets import QVBoxLayout, QWidget
  12. from GUIElements import _BrowserTextEdit, _ExpandableTextEdit
  13. import html
  14. import gettext
  15. import FlatCAMTranslation as fcTranslate
  16. fcTranslate.apply_language('ToolShell')
  17. def _tr(text):
  18. try:
  19. return _(text)
  20. except:
  21. return text
  22. class TermWidget(QWidget):
  23. """
  24. Widget wich represents terminal. It only displays text and allows to enter text.
  25. All highlevel logic should be implemented by client classes
  26. User pressed Enter. Client class should decide, if command must be executed or user may continue edit it
  27. """
  28. def __init__(self, version, *args):
  29. QWidget.__init__(self, *args)
  30. self._browser = _BrowserTextEdit(version=version)
  31. self._browser.setStyleSheet("font: 9pt \"Courier\";")
  32. self._browser.setReadOnly(True)
  33. self._browser.document().setDefaultStyleSheet(
  34. self._browser.document().defaultStyleSheet() +
  35. "span {white-space:pre;}")
  36. self._edit = _ExpandableTextEdit(self, self)
  37. self._edit.historyNext.connect(self._on_history_next)
  38. self._edit.historyPrev.connect(self._on_history_prev)
  39. self._edit.setFocus()
  40. self.setFocusProxy(self._edit)
  41. layout = QVBoxLayout(self)
  42. layout.setSpacing(0)
  43. layout.setContentsMargins(0, 0, 0, 0)
  44. layout.addWidget(self._browser)
  45. layout.addWidget(self._edit)
  46. self._history = [''] # current empty line
  47. self._historyIndex = 0
  48. def open_proccessing(self, detail=None):
  49. """
  50. Open processing and disable using shell commands again until all commands are finished
  51. :param detail: text detail about what is currently called from TCL to python
  52. :return: None
  53. """
  54. self._edit.setTextColor(Qt.white)
  55. self._edit.setTextBackgroundColor(Qt.darkGreen)
  56. if detail is None:
  57. self._edit.setPlainText(_tr("...proccessing..."))
  58. else:
  59. self._edit.setPlainText(_tr("...proccessing... [%s]") % detail)
  60. self._edit.setDisabled(True)
  61. self._edit.setFocus()
  62. def close_proccessing(self):
  63. """
  64. Close processing and enable using shell commands again
  65. :return:
  66. """
  67. self._edit.setTextColor(Qt.black)
  68. self._edit.setTextBackgroundColor(Qt.white)
  69. self._edit.setPlainText('')
  70. self._edit.setDisabled(False)
  71. self._edit.setFocus()
  72. def _append_to_browser(self, style, text):
  73. """
  74. Convert text to HTML for inserting it to browser
  75. """
  76. assert style in ('in', 'out', 'err', 'warning', 'success', 'selected')
  77. text = html.escape(text)
  78. text = text.replace('\n', '<br/>')
  79. if style == 'in':
  80. text = '<span style="font-weight: bold;">%s</span>' % text
  81. elif style == 'err':
  82. text = '<span style="font-weight: bold; color: red;">%s</span>' % text
  83. elif style == 'warning':
  84. text = '<span style="font-weight: bold; color: #f4b642;">%s</span>' % text
  85. elif style == 'success':
  86. text = '<span style="font-weight: bold; color: #084400;">%s</span>' % text
  87. elif style == 'selected':
  88. text = ''
  89. else:
  90. text = '<span>%s</span>' % text # without span <br/> is ignored!!!
  91. scrollbar = self._browser.verticalScrollBar()
  92. old_value = scrollbar.value()
  93. scrollattheend = old_value == scrollbar.maximum()
  94. self._browser.moveCursor(QTextCursor.End)
  95. self._browser.insertHtml(text)
  96. """TODO When user enters second line to the input, and input is resized, scrollbar changes its positon
  97. and stops moving. As quick fix of this problem, now we always scroll down when add new text.
  98. To fix it correctly, srcoll to the bottom, if before intput has been resized,
  99. scrollbar was in the bottom, and remove next lien
  100. """
  101. scrollattheend = True
  102. if scrollattheend:
  103. scrollbar.setValue(scrollbar.maximum())
  104. else:
  105. scrollbar.setValue(old_value)
  106. def exec_current_command(self):
  107. """
  108. Save current command in the history. Append it to the log. Clear edit line
  109. Reimplement in the child classes to actually execute command
  110. """
  111. text = str(self._edit.toPlainText())
  112. self._append_to_browser('in', '> ' + text + '\n')
  113. if len(self._history) < 2 or\
  114. self._history[-2] != text: # don't insert duplicating items
  115. if text[-1] == '\n':
  116. self._history.insert(-1, text[:-1])
  117. else:
  118. self._history.insert(-1, text)
  119. self._historyIndex = len(self._history) - 1
  120. self._history[-1] = ''
  121. self._edit.clear()
  122. if not text[-1] == '\n':
  123. text += '\n'
  124. self.child_exec_command(text)
  125. def child_exec_command(self, text):
  126. """
  127. Reimplement in the child classes
  128. """
  129. pass
  130. def add_line_break_to_input(self):
  131. self._edit.textCursor().insertText('\n')
  132. def append_output(self, text):
  133. """Appent text to output widget
  134. """
  135. self._append_to_browser('out', text)
  136. def append_success(self, text):
  137. """Appent text to output widget
  138. """
  139. self._append_to_browser('success', text)
  140. def append_selected(self, text):
  141. """Appent text to output widget
  142. """
  143. self._append_to_browser('selected', text)
  144. def append_warning(self, text):
  145. """Appent text to output widget
  146. """
  147. self._append_to_browser('warning', text)
  148. def append_error(self, text):
  149. """Appent error text to output widget. Text is drawn with red background
  150. """
  151. self._append_to_browser('err', text)
  152. def is_command_complete(self, text):
  153. """
  154. Executed by _ExpandableTextEdit. Reimplement this function in the child classes.
  155. """
  156. return True
  157. def browser(self):
  158. return self._browser
  159. def _on_history_next(self):
  160. """
  161. Down pressed, show next item from the history
  162. """
  163. if (self._historyIndex + 1) < len(self._history):
  164. self._historyIndex += 1
  165. self._edit.setPlainText(self._history[self._historyIndex])
  166. self._edit.moveCursor(QTextCursor.End)
  167. def _on_history_prev(self):
  168. """
  169. Up pressed, show previous item from the history
  170. """
  171. if self._historyIndex > 0:
  172. if self._historyIndex == (len(self._history) - 1):
  173. self._history[-1] = self._edit.toPlainText()
  174. self._historyIndex -= 1
  175. self._edit.setPlainText(self._history[self._historyIndex])
  176. self._edit.moveCursor(QTextCursor.End)
  177. class FCShell(TermWidget):
  178. def __init__(self, sysShell, version, *args):
  179. TermWidget.__init__(self, version, *args)
  180. self._sysShell = sysShell
  181. def is_command_complete(self, text):
  182. def skipQuotes(text):
  183. quote = text[0]
  184. text = text[1:]
  185. endIndex = str(text).index(quote)
  186. return text[endIndex:]
  187. while text:
  188. if text[0] in ('"', "'"):
  189. try:
  190. text = skipQuotes(text)
  191. except ValueError:
  192. return False
  193. text = text[1:]
  194. return True
  195. def child_exec_command(self, text):
  196. self._sysShell.exec_command(text)