Fixes bug #1405260 "Slide controller title forces window width to grow" and adds the functionality to elide the text in the label. The tool tip is set to the full text.

As bug #1390236 "OpenLP locks up / crashes when preforming a text search on bible" is caused by bug #1405260 "Slide controller title forces window width to grow" this has also been fixed.

Also fixes a third bug which affects making selections with the keyboard when "single click to preview" is selected.

Tests added for the new InfoLabel Class and for the bible media manager build_display_results method

bzr-revno: 2466
This commit is contained in:
2015-01-04 08:59:05 +00:00 committed by Tim Bentley
commit 6dfcbfd65a
4 changed files with 262 additions and 5 deletions

View File

@ -105,6 +105,35 @@ class DisplayController(QtGui.QWidget):
Registry().execute('%s' % sender, [controller, args])
class InfoLabel(QtGui.QLabel):
"""
InfoLabel is a subclassed QLabel. Created to provide the ablilty to 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 the text is elided align it left to stop it jittering as the label is resized
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
@ -159,8 +188,8 @@ 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
self.splitter = QtGui.QSplitter(self.panel)
@ -866,7 +895,8 @@ class SlideController(DisplayController, RegistryProperties):
if service_item.is_media():
self.on_media_start(service_item)
self.slide_selected(True)
self.preview_widget.setFocus()
if service_item.from_service:
self.preview_widget.setFocus()
if old_item:
# Close the old item after the new one is opened
# This avoids the service theme/desktop flashing on screen

View File

@ -29,12 +29,13 @@
"""
Package to test the openlp.core.ui.slidecontroller package.
"""
from PyQt4 import QtCore, QtGui
from unittest import TestCase
from openlp.core import Registry
from openlp.core.lib import ServiceItemAction
from openlp.core.ui import SlideController
from openlp.core.ui.slidecontroller import WIDE_MENU, NON_TEXT_MENU
from openlp.core.ui.slidecontroller import InfoLabel, WIDE_MENU, NON_TEXT_MENU
from tests.interfaces import MagicMock, patch
@ -558,3 +559,81 @@ class TestSlideController(TestCase):
self.assertEqual(0, mocked_update_preview.call_count, 'Update preview should not have been called')
mocked_preview_widget.change_slide.assert_called_once_with(7)
mocked_slide_selected.assert_called_once_with()
class TestInfoLabel(TestCase):
def paint_event_text_fits_test(self):
"""
Test the paintEvent method when text fits the label
"""
font = QtGui.QFont()
metrics = QtGui.QFontMetrics(font)
with patch('openlp.core.ui.slidecontroller.QtGui.QLabel'), \
patch('openlp.core.ui.slidecontroller.QtGui.QPainter') as mocked_qpainter:
# GIVEN: An instance of InfoLabel, with mocked text return, width and rect methods
info_label = InfoLabel()
test_string = 'Label Text'
mocked_rect = MagicMock()
mocked_text = MagicMock()
mocked_width = MagicMock()
mocked_text.return_value = test_string
info_label.rect = mocked_rect
info_label.text = mocked_text
info_label.width = mocked_width
# WHEN: The instance is wider than its text, and the paintEvent method is called
info_label.width.return_value = metrics.boundingRect(test_string).width() + 10
info_label.paintEvent(MagicMock())
# THEN: The text should be drawn centered with the complete test_string
mocked_qpainter().drawText.assert_called_once_with(mocked_rect(), QtCore.Qt.AlignCenter, test_string)
def paint_event_text_doesnt_fit_test(self):
"""
Test the paintEvent method when text fits the label
"""
font = QtGui.QFont()
metrics = QtGui.QFontMetrics(font)
with patch('openlp.core.ui.slidecontroller.QtGui.QLabel'), \
patch('openlp.core.ui.slidecontroller.QtGui.QPainter') as mocked_qpainter:
# GIVEN: An instance of InfoLabel, with mocked text return, width and rect methods
info_label = InfoLabel()
test_string = 'Label Text'
mocked_rect = MagicMock()
mocked_text = MagicMock()
mocked_width = MagicMock()
mocked_text.return_value = test_string
info_label.rect = mocked_rect
info_label.text = mocked_text
info_label.width = mocked_width
# WHEN: The instance is narrower than its text, and the paintEvent method is called
label_width = metrics.boundingRect(test_string).width() - 10
info_label.width.return_value = label_width
info_label.paintEvent(MagicMock())
# THEN: The text should be drawn aligned left with an elided test_string
elided_test_string = metrics.elidedText(test_string, QtCore.Qt.ElideRight, label_width)
mocked_qpainter().drawText.assert_called_once_with(mocked_rect(), QtCore.Qt.AlignLeft, elided_test_string)
def set_text_test(self):
"""
Test the reimplemented setText method
"""
with patch('builtins.super') as mocked_super:
# GIVEN: An instance of InfoLabel and mocked setToolTip method
info_label = InfoLabel()
set_tool_tip_mock = MagicMock()
info_label.setToolTip = set_tool_tip_mock
# WHEN: Calling the instance method setText
info_label.setText('Label Text')
# THEN: The setToolTip and super class setText methods should have been called with the same text
set_tool_tip_mock.assert_called_once_with('Label Text')
mocked_super().setText.assert_called_once_with('Label Text')

View File

@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2015 Raoul Snyman #
# Portions copyright (c) 2008-2015 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
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
from tests.helpers.testmixin import TestMixin
class TestMediaItem(TestCase, TestMixin):
"""
Test the bible mediaitem methods.
"""
def setUp(self):
"""
Set up the components need for all tests.
"""
with patch('openlp.plugins.bibles.lib.mediaitem.MediaManagerItem._setup'),\
patch('openlp.plugins.bibles.lib.mediaitem.BibleMediaItem.setup_item'):
self.media_item = BibleMediaItem(None, MagicMock())
self.setup_application()
def display_results_no_results_test(self):
"""
Test the display_results method when called with a single bible, returning no results
"""
# GIVEN: A mocked build_display_results which returns an empty list
with patch('openlp.plugins.bibles.lib.BibleMediaItem.build_display_results', **{'return_value': []}) \
as mocked_build_display_results:
mocked_list_view = MagicMock()
self.media_item.search_results = 'results'
self.media_item.list_view = mocked_list_view
# WHEN: Calling display_results with a single bible version
self.media_item.display_results('NIV')
# THEN: No items should be added to the list, and select all should have been called.
mocked_build_display_results.assert_called_once_with('NIV', '', 'results')
self.assertFalse(mocked_list_view.addItem.called)
mocked_list_view.selectAll.assert_called_once_with()
self.assertEqual(self.media_item.search_results, {})
self.assertEqual(self.media_item.second_search_results, {})
def display_results_two_bibles_no_results_test(self):
"""
Test the display_results method when called with two bibles, returning no results
"""
# GIVEN: A mocked build_display_results which returns an empty list
with patch('openlp.plugins.bibles.lib.BibleMediaItem.build_display_results', **{'return_value': []}) \
as mocked_build_display_results:
mocked_list_view = MagicMock()
self.media_item.search_results = 'results'
self.media_item.list_view = mocked_list_view
# WHEN: Calling display_results with two single bible versions
self.media_item.display_results('NIV', 'GNB')
# THEN: build_display_results should have been called with two bible versions.
# No items should be added to the list, and select all should have been called.
mocked_build_display_results.assert_called_once_with('NIV', 'GNB', 'results')
self.assertFalse(mocked_list_view.addItem.called)
mocked_list_view.selectAll.assert_called_once_with()
self.assertEqual(self.media_item.search_results, {})
self.assertEqual(self.media_item.second_search_results, {})
def display_results_returns_lots_of_results_test_test(self):
"""
Test the display_results method a large number of results (> 100) are returned
"""
# GIVEN: A mocked build_display_results which returns a large list of results
long_list = list(range(100))
with patch('openlp.plugins.bibles.lib.BibleMediaItem.build_display_results', **{'return_value': long_list})\
as mocked_build_display_results:
mocked_list_view = MagicMock()
self.media_item.search_results = 'results'
self.media_item.list_view = mocked_list_view
# WHEN: Calling display_results
self.media_item.display_results('NIV', 'GNB')
# THEN: addItem should have been called 100 times, and the lsit items should not be selected.
mocked_build_display_results.assert_called_once_with('NIV', 'GNB', 'results')
self.assertEqual(mocked_list_view.addItem.call_count, 100)
mocked_list_view.selectAll.assert_called_once_with()
self.assertEqual(self.media_item.search_results, {})
self.assertEqual(self.media_item.second_search_results, {})

View File

@ -1,3 +1,31 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2015 Raoul Snyman #
# Portions copyright (c) 2008-2015 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
This module contains tests for the lib submodule of the Songs plugin.
"""