From 19cb16d65b88c038fde9ffd3a055770a0ad53e8d Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Tue, 30 Dec 2014 11:21:06 +0000 Subject: [PATCH] Moved 'eliding' code to a sublass of qlabel to enable the text to be rendrawn when resized --- openlp/core/ui/slidecontroller.py | 34 ++++++++++++++++--- .../functional/openlp_core_utils/test_init.py | 1 + .../openlp_plugins/bibles/test_mediaitem.py | 18 ++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/openlp/core/ui/slidecontroller.py b/openlp/core/ui/slidecontroller.py index 7f80bcfc7..7791604c1 100644 --- a/openlp/core/ui/slidecontroller.py +++ b/openlp/core/ui/slidecontroller.py @@ -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() diff --git a/tests/functional/openlp_core_utils/test_init.py b/tests/functional/openlp_core_utils/test_init.py index 563f0dc5b..6979be7b3 100644 --- a/tests/functional/openlp_core_utils/test_init.py +++ b/tests/functional/openlp_core_utils/test_init.py @@ -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;"') + diff --git a/tests/functional/openlp_plugins/bibles/test_mediaitem.py b/tests/functional/openlp_plugins/bibles/test_mediaitem.py index e9ca2c8d1..131a679f0 100644 --- a/tests/functional/openlp_plugins/bibles/test_mediaitem.py +++ b/tests/functional/openlp_plugins/bibles/test_mediaitem.py @@ -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) \ No newline at end of file