Display texts immediately if possible

This commit is contained in:
Florian Bruhin 2014-05-16 16:43:14 +02:00
parent 7eb19e2a1e
commit 39249bce9d

View File

@ -19,6 +19,7 @@
import logging import logging
from collections import deque from collections import deque
from datetime import datetime
from PyQt5.QtCore import pyqtSignal, pyqtSlot, pyqtProperty, Qt, QTimer from PyQt5.QtCore import pyqtSignal, pyqtSlot, pyqtProperty, Qt, QTimer
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QStackedLayout, QSizePolicy from PyQt5.QtWidgets import QWidget, QHBoxLayout, QStackedLayout, QSizePolicy
@ -53,6 +54,7 @@ class StatusBar(QWidget):
_text_queue: A deque of (error, text) tuples to be displayed. _text_queue: A deque of (error, text) tuples to be displayed.
error: True if message is an error, False otherwise error: True if message is an error, False otherwise
_text_pop_timer: A QTimer displaying the error messages. _text_pop_timer: A QTimer displaying the error messages.
_last_text_time: The timestamp where a message was last displayed.
Class attributes: Class attributes:
_error: If there currently is an error, accessed through the error _error: If there currently is an error, accessed through the error
@ -98,6 +100,7 @@ class StatusBar(QWidget):
self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed) self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed)
self._option = None self._option = None
self._last_text_time = None
self._hbox = QHBoxLayout(self) self._hbox = QHBoxLayout(self)
self._hbox.setContentsMargins(0, 0, 0, 0) self._hbox.setContentsMargins(0, 0, 0, 0)
@ -178,17 +181,39 @@ class StatusBar(QWidget):
self._text_pop_timer.start() self._text_pop_timer.start()
self._stack.setCurrentWidget(self.txt) self._stack.setCurrentWidget(self.txt)
@pyqtSlot(str)
def _disp_text(self, text, error):
"""Inner logic for disp_error and disp_temp_text."""
logging.debug("Displaying text: {} (error={})".format(text, error))
now = datetime.now()
mindelta = config.get('general', 'message-timeout')
delta = (None if self._last_text_time is None
else now - self._last_text_time)
self._last_text_time = now
logging.debug("queue: {} / delta: {}".format(self._text_queue, delta))
if not self._text_queue and (
delta is None or delta.total_seconds() * 1000.0 > mindelta):
# If the queue is empty and we didn't print messages for long
# enough, we can take the short route and display the message
# immediately. We then start the pop_timer only to restore the
# normal state in 2 seconds.
logging.debug("Displaying immediately")
self.error = error
self.txt.temptext = text
else:
logging.debug("queueing")
self._text_queue.append((error, text))
self._text_pop_timer.start()
@pyqtSlot(str) @pyqtSlot(str)
def disp_error(self, text): def disp_error(self, text):
"""Display an error in the statusbar.""" """Display an error in the statusbar."""
self._text_queue.append((True, text)) self._disp_text(text, True)
self._text_pop_timer.start()
@pyqtSlot(str) @pyqtSlot(str)
def disp_temp_text(self, text): def disp_temp_text(self, text):
"""Add a temporary text to the queue.""" """Add a temporary text to the queue."""
self._text_queue.append((False, text)) self._disp_text(text, False)
self._text_pop_timer.start()
@pyqtSlot(str) @pyqtSlot(str)
def set_text(self, val): def set_text(self, val):