2018-02-11 05:34:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2019-02-14 15:09:09 +00:00
|
|
|
# Copyright (c) 2008-2019 OpenLP Developers #
|
2018-02-11 05:34:14 +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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
The :mod:`~openlp.core.widgets.layouts` module contains customised layout classes
|
|
|
|
"""
|
|
|
|
from PyQt5 import QtCore, QtWidgets
|
|
|
|
|
|
|
|
|
|
|
|
class AspectRatioLayout(QtWidgets.QLayout):
|
|
|
|
"""
|
|
|
|
A layout that contains a single widget and maintains the aspect ratio of the widget
|
|
|
|
|
|
|
|
This is based on the C++ example here: https://gist.github.com/pavel-perina/1324ff064aedede0e01311aab315f83d
|
|
|
|
"""
|
2018-03-25 07:14:38 +00:00
|
|
|
resize = QtCore.pyqtSignal(QtCore.QSize)
|
|
|
|
|
2018-02-11 05:34:14 +00:00
|
|
|
def __init__(self, parent=None, aspect_ratio=None):
|
|
|
|
"""
|
|
|
|
Create a layout.
|
|
|
|
|
|
|
|
:param PyQt5.QtWidgets.QWidget parent: The parent widget, can be None.
|
|
|
|
:param float aspect_ratio: The aspect ratio as a float (e.g. 16.0/9.0)
|
|
|
|
"""
|
|
|
|
super().__init__(parent)
|
|
|
|
self._aspect_ratio = aspect_ratio
|
|
|
|
self._item = None
|
|
|
|
self._margin = 0
|
|
|
|
self.setContentsMargins(0, 0, 0, 0)
|
|
|
|
|
|
|
|
def get_aspect_ratio(self):
|
|
|
|
"""
|
|
|
|
Return the aspect ratio
|
|
|
|
|
|
|
|
:return: The aspect ration
|
|
|
|
:rtype: float
|
|
|
|
"""
|
|
|
|
return self._aspect_ratio
|
|
|
|
|
|
|
|
def set_aspect_ratio(self, aspect_ratio):
|
|
|
|
"""
|
|
|
|
Set the aspect ratio
|
|
|
|
|
|
|
|
:param float aspect_ratio: The aspect ratio to set
|
|
|
|
"""
|
|
|
|
# TODO: Update the layout/widget if this changes
|
|
|
|
self._aspect_ratio = aspect_ratio
|
|
|
|
|
|
|
|
aspect_ratio = property(get_aspect_ratio, set_aspect_ratio)
|
|
|
|
|
|
|
|
def get_margin(self):
|
|
|
|
"""
|
|
|
|
Return the margin
|
|
|
|
|
|
|
|
:return: The margin
|
|
|
|
:rtype: int
|
|
|
|
"""
|
|
|
|
return self._margin
|
|
|
|
|
|
|
|
def set_margin(self, value):
|
|
|
|
"""
|
|
|
|
Set the margin
|
|
|
|
|
|
|
|
:param int value: The margin
|
|
|
|
"""
|
|
|
|
self._margin = int(value)
|
|
|
|
|
|
|
|
margin = property(get_margin, set_margin)
|
|
|
|
|
|
|
|
def count(self):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
return 1 if self._item else 0
|
|
|
|
|
|
|
|
def addItem(self, item):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
if self._item is not None:
|
|
|
|
raise ValueError('AspectRatioLayout can contain only 1 widget')
|
|
|
|
self._item = item
|
|
|
|
# self._item.setAlignment(0)
|
|
|
|
|
|
|
|
def itemAt(self, index):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
if index != 0:
|
|
|
|
return None
|
|
|
|
return self._item
|
|
|
|
|
|
|
|
def takeAt(self, index):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
if index != 0:
|
|
|
|
return None
|
|
|
|
result = self._item
|
|
|
|
self._item = None
|
|
|
|
return result
|
|
|
|
|
|
|
|
def expandingDirections(self):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
return QtCore.Qt.Horizontal | QtCore.Qt.Vertical
|
|
|
|
|
|
|
|
def hasHeightForWidth(self):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
def heightForWidth(self, width):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
height = (width - 2 * self.margin) / (self._aspect_ratio + 2 * self.margin)
|
|
|
|
return height
|
|
|
|
|
|
|
|
def setGeometry(self, rect):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
super().setGeometry(rect)
|
|
|
|
if self._item:
|
|
|
|
widget = self._item.widget()
|
|
|
|
available_width = rect.width() - 2 * self.margin
|
|
|
|
available_height = rect.height() - 2 * self.margin
|
|
|
|
height = available_height
|
|
|
|
width = height * self._aspect_ratio
|
|
|
|
if width > available_width:
|
|
|
|
width = available_width
|
|
|
|
height = width / self._aspect_ratio
|
|
|
|
if self._item.alignment() & QtCore.Qt.AlignTop:
|
|
|
|
y = self.margin
|
|
|
|
elif self._item.alignment() & QtCore.Qt.AlignBottom:
|
|
|
|
y = rect.height() - self.margin - height
|
|
|
|
else:
|
|
|
|
y = self.margin + (available_height - height) / 2
|
|
|
|
widget.setGeometry(rect.x() + self.margin, rect.y() + y, width, height)
|
|
|
|
else:
|
|
|
|
if self._item.alignment() & QtCore.Qt.AlignLeft:
|
|
|
|
x = self.margin
|
|
|
|
elif self._item.alignment() & QtCore.Qt.AlignRight:
|
|
|
|
x = rect.width() - self.margin - width
|
|
|
|
else:
|
|
|
|
x = self.margin + (available_width - width) / 2
|
|
|
|
widget.setGeometry(rect.x() + x, rect.y() + self.margin, width, height)
|
2018-03-25 07:14:38 +00:00
|
|
|
self.resize.emit(QtCore.QSize(width, height))
|
2018-02-11 05:34:14 +00:00
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
margins = 2 * self.margin
|
|
|
|
return self._item.sizeHint() + QtCore.QSize(margins, margins) \
|
|
|
|
if self._item else QtCore.QSize(margins, margins)
|
|
|
|
|
|
|
|
def minimumSize(self):
|
|
|
|
"""
|
|
|
|
Overridden Qt method
|
|
|
|
"""
|
|
|
|
margins = 2 * self.margin
|
|
|
|
return self._item.minimumSize() + QtCore.QSize(margins, margins) \
|
|
|
|
if self._item else QtCore.QSize(margins, margins)
|