Moved 'eliding' code to a sublass of qlabel to enable the text to be rendrawn when resized

This commit is contained in:
Phill Ridout 2014-12-30 11:21:06 +00:00
parent 3cf2a21ca2
commit 19cb16d65b
3 changed files with 49 additions and 4 deletions

View File

@ -106,6 +106,34 @@ class DisplayController(QtGui.QWidget):
Registry().execute('%s' % sender, [controller, args])
class InfoLabel(QtGui.QLabel):
"""
InfoLabel is a subclassed QLabel. Created to provide the ablilty add a ellipsis if the text is cut off. Original
source: https://stackoverflow.com/questions/11446478/pyside-pyqt-truncate-text-in-qlabel-based-on-minimumsize
"""
def paintEvent(self, event):
"""
Reimplemented to allow the drawing of elided text if the text is longer than the width of the label
"""
painter = QtGui.QPainter(self)
metrics = QtGui.QFontMetrics(self.font())
elided = metrics.elidedText(self.text(), QtCore.Qt.ElideRight, self.width())
if elided == self.text():
alignment = QtCore.Qt.AlignCenter
else:
alignment = QtCore.Qt.AlignLeft
painter.drawText(self.rect(), alignment, elided)
def setText(self, text):
"""
Reimplemented to set the tool tip text.
"""
self.setToolTip(text)
super().setText(text)
class SlideController(DisplayController, RegistryProperties):
"""
SlideController is the slide controller widget. This widget is what the
@ -160,8 +188,7 @@ class SlideController(DisplayController, RegistryProperties):
self.type_label.setText(UiStrings().Preview)
self.panel_layout.addWidget(self.type_label)
# Info label for the title of the current item, at the top of the slide controller
self.info_label = QtGui.QLabel(self.panel)
self.info_label.setAlignment(QtCore.Qt.AlignCenter)
self.info_label = InfoLabel(self.panel)
self.info_label.setSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Preferred)
self.panel_layout.addWidget(self.info_label)
# Splitter
@ -810,8 +837,7 @@ class SlideController(DisplayController, RegistryProperties):
if service_item.is_command():
Registry().execute(
'%s_start' % service_item.name.lower(), [self.service_item, self.is_live, self.hide_mode(), slide_no])
self.info_label.setText(elide_text(self.service_item.title, self.info_label.font(), self.info_label.width()))
self.info_label.setToolTip(self.service_item.title)
self.info_label.setText(self.service_item.title)
self.slide_list = {}
if self.is_live:
self.song_menu.menu().clear()

View File

@ -133,3 +133,4 @@ class TestInitFunctions(TestMixin, TestCase):
# THEN: The connection parameters should be set for socket
self.assertEqual(result, 'libreoffice --nologo --norestore --minimized --nodefault --nofirststartwizard'
' "--accept=socket,host=localhost,port=2002;urp;"')

View File

@ -29,6 +29,8 @@
"""
This module contains tests for the lib submodule of the Presentations plugin.
"""
from PyQt4 import QtGui
from unittest import TestCase
from openlp.plugins.bibles.lib.mediaitem import BibleMediaItem
from tests.functional import MagicMock, patch
@ -116,3 +118,19 @@ class TestMediaItem(TestCase, TestMixin):
mocked_list_view.selectAll.assert_called_once_with()
self.assertEqual(self.media_item.search_results, {})
self.assertEqual(self.media_item.second_search_results, {})
def elide_text_short_text_test(self):
result = elide_text('Test String', QtGui.QFont(),)
def elide_text(text, font, width):
"""
Add an ellipsis to text if it is wider than width.
:param text: The string to elide
:param font: The font that the text is being desplayed in
:param width: The width that the elided text string needs to fill
:return: The elided string or just text
"""
font_metrics = QtGui.QFontMetrics(font)
return font_metrics.elidedText(text, QtCore.Qt.ElideRight, width)