Ejemplo n.º 1
0
 def initController(self):
     temp_dir = TemporaryDirectory.get()
     self.latex2image_converter = Converter(temp_dir)
     self.latex2image_converter.convertedSignal.connect(
         self.previewGenerated)
     self.latex2image_converter.conversionAbortedSignal.connect(
         self.conversionAborted)
Ejemplo n.º 2
0
	def initController(self):
		temp_dir = TemporaryDirectory.get()
		self.latex2image_converter = Converter(temp_dir)
		self.latex2image_converter.convertedSignal.connect(self.previewGenerated)
		self.latex2image_converter.conversionAbortedSignal.connect(self.conversionAborted)
Ejemplo n.º 3
0
class PreviewController(QObject):
	"""
	The preview controller converts when necessary or requested the document source to an
	image displayed in the preview view.
	"""
	willUpdatePreviewSignal = pyqtSignal()
	errorsInSourceSignal = pyqtSignal(list, str)
	logsSignal = pyqtSignal(str)

	def __init__(self):
		super(PreviewController, self).__init__()
		self.app_controller = None
		self.doc_controller = None
		self.preview_view = None
		self.latex2image_converter = None
		self.request_preview_update = False
		self.last_time_content_changed = datetime.now()

	def initController(self):
		temp_dir = TemporaryDirectory.get()
		self.latex2image_converter = Converter(temp_dir)
		self.latex2image_converter.convertedSignal.connect(self.previewGenerated)
		self.latex2image_converter.conversionAbortedSignal.connect(self.conversionAborted)

	@pyqtSlot(str)
	def documentContentChanged(self, content):
		self.last_time_content_changed = datetime.now()
		self.preview_view.showWaitingBackground()
		if not self.request_preview_update:
			self.requestPreviewUpdate()

	@pyqtSlot()
	def conversionAborted(self):
		self.preview_view.showErrorBackground()

	def requestPreviewUpdate(self):
		if self._isEndOfInstruction():
			self.updatePreview()
		elif Preferences.getAutoPreview():
			self.updatePreviewAfterTypingPause()
		else:
			self.request_preview_update = False

	def _isEndOfInstruction(self):
		"""
		Returns whether the last typed character is a semi-colon, meaning the
		end of current TikZ instruction
		"""
		return (self.doc_controller.view.content_view.current_editor_view.getCharacterAtCurrentCursorPosition() == ";")

	def updatePreviewAfterTypingPause(self):
		"""
		Updates the preview when the user is making a pause while typing.
		"""
		preview_threshold = Preferences.getPreviewThreshold()
		time_delta = datetime.now() - self.last_time_content_changed
		elapsed_ms = time_delta.seconds * 1000 + time_delta.microseconds / 1000
		if elapsed_ms >= preview_threshold:
			self.updatePreview()
		else:
			wait_for = max(100, Preferences.getPreviewThreshold() / 2)
			QTimer.singleShot(wait_for, self.updatePreviewAfterTypingPause)

	def updatePreview(self):
		self.request_preview_update = False
		doc = self.doc_controller.model
		template = Preferences.getPreviewTemplate()
		source = documentIO.buildFileContentFromDocument(template, doc)
		self.latex2image_converter.convertLatexToImage(source)

	def abortPreview(self):
		self.latex2image_converter.stopConversion()

	@pyqtSlot(LatexToImageConversion)
	def previewGenerated(self, conversion):
		self.willUpdatePreviewSignal.emit()
		if conversion.image_path:
			self.preview_view.figure = conversion.image_path
		self.preview_view.showNormalBackground()
		self.logsSignal.emit(conversion.logs)
		self.errorsInSourceSignal.emit(conversion.errors, conversion.source)
Ejemplo n.º 4
0
class PreviewController(QObject):
    """
	The preview controller converts when necessary or requested the document source to an
	image displayed in the preview view.
	"""
    willUpdatePreviewSignal = pyqtSignal()
    errorsInSourceSignal = pyqtSignal(list, str)
    logsSignal = pyqtSignal(str)

    def __init__(self):
        super(PreviewController, self).__init__()
        self.app_controller = None
        self.doc_controller = None
        self.preview_view = None
        self.latex2image_converter = None
        self.request_preview_update = False
        self.last_time_content_changed = datetime.now()

    def initController(self):
        temp_dir = TemporaryDirectory.get()
        self.latex2image_converter = Converter(temp_dir)
        self.latex2image_converter.convertedSignal.connect(
            self.previewGenerated)
        self.latex2image_converter.conversionAbortedSignal.connect(
            self.conversionAborted)

    @pyqtSlot(str)
    def documentContentChanged(self, content):
        self.last_time_content_changed = datetime.now()
        self.preview_view.showWaitingBackground()
        if not self.request_preview_update:
            self.requestPreviewUpdate()

    @pyqtSlot()
    def conversionAborted(self):
        self.preview_view.showErrorBackground()

    def requestPreviewUpdate(self):
        if self._isEndOfInstruction():
            self.updatePreview()
        elif Preferences.getAutoPreview():
            self.updatePreviewAfterTypingPause()
        else:
            self.request_preview_update = False

    def _isEndOfInstruction(self):
        """
		Returns whether the last typed character is a semi-colon, meaning the
		end of current TikZ instruction
		"""
        return (self.doc_controller.view.content_view.current_editor_view.
                getCharacterAtCurrentCursorPosition() == ";")

    def updatePreviewAfterTypingPause(self):
        """
		Updates the preview when the user is making a pause while typing.
		"""
        preview_threshold = Preferences.getPreviewThreshold()
        time_delta = datetime.now() - self.last_time_content_changed
        elapsed_ms = time_delta.seconds * 1000 + time_delta.microseconds / 1000
        if elapsed_ms >= preview_threshold:
            self.updatePreview()
        else:
            wait_for = max(100, Preferences.getPreviewThreshold() / 2)
            QTimer.singleShot(wait_for, self.updatePreviewAfterTypingPause)

    def updatePreview(self):
        self.request_preview_update = False
        doc = self.doc_controller.model
        template = Preferences.getPreviewTemplate()
        source = documentIO.buildFileContentFromDocument(template, doc)
        self.latex2image_converter.convertLatexToImage(source)

    def abortPreview(self):
        self.latex2image_converter.stopConversion()

    @pyqtSlot(LatexToImageConversion)
    def previewGenerated(self, conversion):
        self.willUpdatePreviewSignal.emit()
        if conversion.image_path:
            self.preview_view.figure = conversion.image_path
        self.preview_view.showNormalBackground()
        self.logsSignal.emit(conversion.logs)
        self.errorsInSourceSignal.emit(conversion.errors, conversion.source)