2013-04-14 21:38:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2015-12-31 22:46:06 +00:00
|
|
|
# Copyright (c) 2008-2016 OpenLP Developers #
|
2013-04-14 21:38:40 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
2013-04-23 17:12:08 +00:00
|
|
|
The :mod:`listpreviewwidget` is a widget that lists the slides in the slide controller.
|
|
|
|
It is based on a QTableWidget but represents its contents in list form.
|
2013-04-14 21:38:40 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
2013-04-14 21:38:40 +00:00
|
|
|
|
2016-03-05 16:41:32 +00:00
|
|
|
from openlp.core.common import RegistryProperties, Settings
|
2016-05-05 03:57:04 +00:00
|
|
|
from openlp.core.lib import ImageSource, ItemCapabilities, ServiceItem
|
2013-04-15 17:12:47 +00:00
|
|
|
|
2013-04-14 21:38:40 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
class ListPreviewWidget(QtWidgets.QTableWidget, RegistryProperties):
|
2014-10-22 20:43:05 +00:00
|
|
|
"""
|
|
|
|
A special type of QTableWidget which lists the slides in the slide controller
|
|
|
|
|
|
|
|
:param parent:
|
|
|
|
:param screen_ratio:
|
|
|
|
"""
|
|
|
|
|
2013-04-18 20:15:44 +00:00
|
|
|
def __init__(self, parent, screen_ratio):
|
2013-04-23 17:12:08 +00:00
|
|
|
"""
|
|
|
|
Initializes the widget to default state.
|
2014-10-22 20:43:05 +00:00
|
|
|
|
|
|
|
An empty ``ServiceItem`` is used by default. replace_service_manager_item() needs to be called to make this
|
|
|
|
widget display something.
|
2013-04-23 17:12:08 +00:00
|
|
|
"""
|
2015-11-07 00:49:40 +00:00
|
|
|
super(QtWidgets.QTableWidget, self).__init__(parent)
|
2014-10-22 20:43:05 +00:00
|
|
|
self._setup(screen_ratio)
|
|
|
|
|
|
|
|
def _setup(self, screen_ratio):
|
|
|
|
"""
|
|
|
|
Set up the widget
|
|
|
|
"""
|
2013-04-18 20:15:44 +00:00
|
|
|
self.setColumnCount(1)
|
|
|
|
self.horizontalHeader().setVisible(False)
|
2014-10-22 20:43:05 +00:00
|
|
|
self.setColumnWidth(0, self.parent().width())
|
2015-11-07 00:49:40 +00:00
|
|
|
self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
|
|
|
|
self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
|
|
|
|
self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
|
2013-04-18 20:15:44 +00:00
|
|
|
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
|
|
|
|
self.setAlternatingRowColors(True)
|
2013-04-27 20:59:56 +00:00
|
|
|
# Initialize variables.
|
|
|
|
self.service_item = ServiceItem()
|
|
|
|
self.screen_ratio = screen_ratio
|
2016-05-18 18:40:27 +00:00
|
|
|
self.auto_row_height = 100
|
2016-03-05 16:41:32 +00:00
|
|
|
# Connect signals
|
|
|
|
self.verticalHeader().sectionResized.connect(self.row_resized)
|
2013-04-18 20:15:44 +00:00
|
|
|
|
2014-10-22 20:43:05 +00:00
|
|
|
def resizeEvent(self, event):
|
2013-04-18 20:50:02 +00:00
|
|
|
"""
|
|
|
|
Overloaded method from QTableWidget. Will recalculate the layout.
|
|
|
|
"""
|
2013-04-18 20:15:44 +00:00
|
|
|
self.__recalculate_layout()
|
|
|
|
|
|
|
|
def __recalculate_layout(self):
|
2013-04-18 20:50:02 +00:00
|
|
|
"""
|
|
|
|
Recalculates the layout of the table widget. It will set height and width
|
2013-04-23 17:12:08 +00:00
|
|
|
of the table cells. QTableWidget does not adapt the cells to the widget size on its own.
|
2013-04-18 20:50:02 +00:00
|
|
|
"""
|
2013-04-18 20:15:44 +00:00
|
|
|
self.setColumnWidth(0, self.viewport().width())
|
2013-04-14 21:38:40 +00:00
|
|
|
if self.service_item:
|
|
|
|
# Sort out songs, bibles, etc.
|
|
|
|
if self.service_item.is_text():
|
2013-04-18 20:15:44 +00:00
|
|
|
self.resizeRowsToContents()
|
2016-03-05 16:41:32 +00:00
|
|
|
# Sort out image heights.
|
2013-04-14 21:38:40 +00:00
|
|
|
else:
|
2016-03-05 16:41:32 +00:00
|
|
|
height = self.viewport().width() // self.screen_ratio
|
|
|
|
max_img_row_height = Settings().value('advanced/slide max height')
|
|
|
|
# Adjust for row height cap if in use.
|
2016-05-18 18:40:27 +00:00
|
|
|
if isinstance(max_img_row_height, int):
|
|
|
|
if max_img_row_height > 0 and height > max_img_row_height:
|
|
|
|
height = max_img_row_height
|
|
|
|
elif max_img_row_height < 0:
|
|
|
|
# If auto setting, show that number of slides, or if the resulting slides too small, 100px.
|
|
|
|
# E.g. If setting is -4, 4 slides will be visible, unless those slides are < 100px high.
|
|
|
|
self.auto_row_height = max(self.viewport().height() / (-1 * max_img_row_height), 100)
|
|
|
|
height = min(height, self.auto_row_height)
|
2016-03-05 16:41:32 +00:00
|
|
|
# Apply new height to slides
|
2013-12-24 15:55:01 +00:00
|
|
|
for frame_number in range(len(self.service_item.get_frames())):
|
|
|
|
self.setRowHeight(frame_number, height)
|
2013-04-18 20:15:44 +00:00
|
|
|
|
2016-03-05 16:41:32 +00:00
|
|
|
def row_resized(self, row, old_height, new_height):
|
|
|
|
"""
|
|
|
|
Will scale non-image slides.
|
|
|
|
"""
|
|
|
|
# Only for non-text slides when row height cap in use
|
2016-04-16 16:35:08 +00:00
|
|
|
max_img_row_height = Settings().value('advanced/slide max height')
|
2016-05-18 18:40:27 +00:00
|
|
|
if self.service_item.is_text() or not isinstance(max_img_row_height, int) or max_img_row_height == 0:
|
2016-03-05 16:41:32 +00:00
|
|
|
return
|
|
|
|
# Get and validate label widget containing slide & adjust max width
|
|
|
|
try:
|
|
|
|
self.cellWidget(row, 0).children()[1].setMaximumWidth(new_height * self.screen_ratio)
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
|
2013-04-18 20:15:44 +00:00
|
|
|
def screen_size_changed(self, screen_ratio):
|
2013-04-18 20:50:02 +00:00
|
|
|
"""
|
2014-10-22 20:43:05 +00:00
|
|
|
This method is called whenever the live screen size changes, which then makes a layout recalculation necessary
|
|
|
|
|
|
|
|
:param screen_ratio: The new screen ratio
|
2013-04-18 20:50:02 +00:00
|
|
|
"""
|
2013-04-18 20:15:44 +00:00
|
|
|
self.screen_ratio = screen_ratio
|
|
|
|
self.__recalculate_layout()
|
|
|
|
|
2013-12-24 15:55:01 +00:00
|
|
|
def replace_service_item(self, service_item, width, slide_number):
|
2013-04-14 21:38:40 +00:00
|
|
|
"""
|
2014-10-22 20:43:05 +00:00
|
|
|
Replace the current preview items with the ones in service_item and display the given slide
|
|
|
|
|
|
|
|
:param service_item: The service item to insert
|
|
|
|
:param width: The width of the column
|
|
|
|
:param slide_number: The slide number to pre-select
|
2013-04-14 21:38:40 +00:00
|
|
|
"""
|
2013-04-15 16:33:50 +00:00
|
|
|
self.service_item = service_item
|
2013-04-18 20:15:44 +00:00
|
|
|
self.setRowCount(0)
|
2014-01-09 19:52:20 +00:00
|
|
|
self.clear()
|
2013-04-18 20:15:44 +00:00
|
|
|
self.setColumnWidth(0, width)
|
2013-04-14 21:38:40 +00:00
|
|
|
row = 0
|
|
|
|
text = []
|
2013-12-24 15:55:01 +00:00
|
|
|
for frame_number, frame in enumerate(self.service_item.get_frames()):
|
2013-04-23 17:12:08 +00:00
|
|
|
self.setRowCount(self.slide_count() + 1)
|
2015-11-07 00:49:40 +00:00
|
|
|
item = QtWidgets.QTableWidgetItem()
|
2013-06-18 09:12:49 +00:00
|
|
|
slide_height = 0
|
2013-04-14 21:38:40 +00:00
|
|
|
if self.service_item.is_text():
|
2013-08-31 18:17:38 +00:00
|
|
|
if frame['verseTag']:
|
2013-04-14 21:38:40 +00:00
|
|
|
# These tags are already translated.
|
2013-08-31 18:17:38 +00:00
|
|
|
verse_def = frame['verseTag']
|
|
|
|
verse_def = '%s%s' % (verse_def[0], verse_def[1:])
|
|
|
|
two_line_def = '%s\n%s' % (verse_def[0], verse_def[1:])
|
2013-04-14 21:38:40 +00:00
|
|
|
row = two_line_def
|
|
|
|
else:
|
|
|
|
row += 1
|
2013-08-31 18:17:38 +00:00
|
|
|
item.setText(frame['text'])
|
2013-04-14 21:38:40 +00:00
|
|
|
else:
|
2015-11-07 00:49:40 +00:00
|
|
|
label = QtWidgets.QLabel()
|
|
|
|
label.setContentsMargins(4, 4, 4, 4)
|
2013-04-14 21:38:40 +00:00
|
|
|
if self.service_item.is_media():
|
|
|
|
label.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
|
|
|
|
else:
|
|
|
|
label.setScaledContents(True)
|
2013-04-15 17:26:18 +00:00
|
|
|
if self.service_item.is_command():
|
2016-05-05 03:57:04 +00:00
|
|
|
if self.service_item.is_capable(ItemCapabilities.HasThumbnails):
|
|
|
|
image = self.image_manager.get_image(frame['image'], ImageSource.CommandPlugins)
|
|
|
|
pixmap = QtGui.QPixmap.fromImage(image)
|
|
|
|
else:
|
|
|
|
pixmap = QtGui.QPixmap(frame['image'])
|
2013-04-15 17:26:18 +00:00
|
|
|
else:
|
2013-08-31 18:17:38 +00:00
|
|
|
image = self.image_manager.get_image(frame['path'], ImageSource.ImagePlugin)
|
2016-05-05 03:57:04 +00:00
|
|
|
pixmap = QtGui.QPixmap.fromImage(image)
|
2016-05-04 12:10:42 +00:00
|
|
|
pixmap.setDevicePixelRatio(label.devicePixelRatio())
|
|
|
|
label.setPixmap(pixmap)
|
2013-06-18 09:12:49 +00:00
|
|
|
slide_height = width // self.screen_ratio
|
2016-04-16 16:35:08 +00:00
|
|
|
# Setup and validate row height cap if in use.
|
2016-03-05 16:41:32 +00:00
|
|
|
max_img_row_height = Settings().value('advanced/slide max height')
|
2016-05-18 18:40:27 +00:00
|
|
|
if isinstance(max_img_row_height, int) and max_img_row_height != 0:
|
|
|
|
if max_img_row_height > 0 and slide_height > max_img_row_height:
|
2016-03-05 16:41:32 +00:00
|
|
|
slide_height = max_img_row_height
|
2016-05-18 18:40:27 +00:00
|
|
|
elif max_img_row_height < 0 and slide_height > self.auto_row_height:
|
|
|
|
slide_height = self.auto_row_height
|
|
|
|
label.setMaximumWidth(slide_height * self.screen_ratio)
|
|
|
|
label.resize(slide_height * self.screen_ratio, slide_height)
|
2016-03-05 16:41:32 +00:00
|
|
|
# Build widget with stretch padding
|
|
|
|
container = QtWidgets.QWidget()
|
|
|
|
hbox = QtWidgets.QHBoxLayout()
|
|
|
|
hbox.setContentsMargins(0, 0, 0, 0)
|
|
|
|
hbox.addWidget(label, stretch=1)
|
|
|
|
hbox.addStretch(0)
|
|
|
|
container.setLayout(hbox)
|
|
|
|
# Add to table
|
|
|
|
self.setCellWidget(frame_number, 0, container)
|
|
|
|
else:
|
|
|
|
# Add to table
|
|
|
|
self.setCellWidget(frame_number, 0, label)
|
2013-04-14 21:38:40 +00:00
|
|
|
row += 1
|
2013-08-31 18:17:38 +00:00
|
|
|
text.append(str(row))
|
2013-12-24 15:55:01 +00:00
|
|
|
self.setItem(frame_number, 0, item)
|
2013-06-18 09:12:49 +00:00
|
|
|
if slide_height:
|
2013-12-24 15:55:01 +00:00
|
|
|
self.setRowHeight(frame_number, slide_height)
|
2013-04-18 20:15:44 +00:00
|
|
|
self.setVerticalHeaderLabels(text)
|
2013-04-14 21:38:40 +00:00
|
|
|
if self.service_item.is_text():
|
2013-04-18 20:15:44 +00:00
|
|
|
self.resizeRowsToContents()
|
|
|
|
self.setColumnWidth(0, self.viewport().width())
|
2013-12-24 15:55:01 +00:00
|
|
|
self.change_slide(slide_number)
|
2013-04-14 21:38:40 +00:00
|
|
|
|
2013-04-18 20:50:02 +00:00
|
|
|
def change_slide(self, slide):
|
2013-04-14 21:38:40 +00:00
|
|
|
"""
|
2013-04-18 20:50:02 +00:00
|
|
|
Switches to the given row.
|
2013-04-14 21:38:40 +00:00
|
|
|
"""
|
2016-04-16 06:53:01 +00:00
|
|
|
# Retrieve setting
|
|
|
|
autoscrolling = Settings().value('advanced/autoscrolling')
|
|
|
|
# Check if auto-scroll disabled (None) and validate value as dict containing 'dist' and 'pos'
|
2016-04-16 07:14:04 +00:00
|
|
|
# 'dist' represents the slide to scroll to relative to the new slide (-1 = previous, 0 = current, 1 = next)
|
|
|
|
# 'pos' represents the vert position of of the slide (0 = in view, 1 = top, 2 = middle, 3 = bottom)
|
2016-04-16 16:35:08 +00:00
|
|
|
if not (isinstance(autoscrolling, dict) and 'dist' in autoscrolling and 'pos' in autoscrolling and
|
|
|
|
isinstance(autoscrolling['dist'], int) and isinstance(autoscrolling['pos'], int)):
|
2016-04-16 06:53:01 +00:00
|
|
|
return
|
|
|
|
# prevent scrolling past list bounds
|
|
|
|
scroll_to_slide = slide + autoscrolling['dist']
|
|
|
|
if scroll_to_slide < 0:
|
|
|
|
scroll_to_slide = 0
|
|
|
|
if scroll_to_slide >= self.slide_count():
|
|
|
|
scroll_to_slide = self.slide_count() - 1
|
|
|
|
# Scroll to item if possible.
|
|
|
|
self.scrollToItem(self.item(scroll_to_slide, 0), autoscrolling['pos'])
|
2013-04-18 20:50:02 +00:00
|
|
|
self.selectRow(slide)
|
2013-04-14 21:38:40 +00:00
|
|
|
|
2013-04-23 17:12:08 +00:00
|
|
|
def current_slide_number(self):
|
|
|
|
"""
|
|
|
|
Returns the position of the currently active item. Will return -1 if the widget is empty.
|
|
|
|
"""
|
2013-04-18 20:15:44 +00:00
|
|
|
return super(ListPreviewWidget, self).currentRow()
|
2013-04-15 17:12:47 +00:00
|
|
|
|
2013-04-23 17:12:08 +00:00
|
|
|
def slide_count(self):
|
|
|
|
"""
|
|
|
|
Returns the number of slides this widget holds.
|
|
|
|
"""
|
2014-03-20 19:10:31 +00:00
|
|
|
return super(ListPreviewWidget, self).rowCount()
|