Scratchpad:Layer Mechanism
From OpenLP
Contents |
Closed
I tried to adapt the code to play video in the background, but it doesn't work :-(
Why use Layers
Because of we have different plugins which partially use different widgets for displaying content
Schematic
Description
There will exist some special widgets: 1. "back" for actual theme image 2. "shield" is in front of other widget because it should hide any displayed items without changing the related content 3. "alarm" is in front of all widgets because the alarm should be possible to display in every situation
Now there are two special widgets for handling backgrounds and foreground items The QStackedWidget Object provide the possibility to switch easily between different backgrounds, whereat the other backgrounds will be hided automatically.
Examples are:
- Phonon widget
- Future VLC widget
- maybe a widget for optimized image display
The other QStackedWidget Object manage different foreground widgets, these widgets should be transparent in the most cases.
Examples are:
- webView widget (this is a special one, because of it can be transparent or not)
- maybe widgets for displaying flashs, of graphic objects, ...
Advantages
- Hide and Show mechanism is easier
- it is not necessary anymore to cache the actual content while hiding (shield) is active
- provide possiblity for more display widgets (optimized widgets for different content)
Open Issues
- add mechanism to select the right foreground/background for different plugins
Further Ideas
We should use the same display classes for the preview widgets as for the main Display, because of I have had the effect, that I opened a little movie in the preview -> this works, but after going to live the movie doesn't appeared :-(
Example Application
from PyQt4 import QtCore, QtGui, QtWebKit
try:_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: sclass Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.frameSize = 400
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
#self.display = QtGui.QGraphicsView(self.centralwidget)self.display = QtGui.QFrame(self.centralwidget)
#self.display = QtGui.QFrame()self.display.setGeometry(QtCore.QRect(360, 40, self.frameSize, self.frameSize))
self.display.setFrameShape(QtGui.QFrame.Box)
self.display.setFrameShadow(QtGui.QFrame.Raised)
self.display.setObjectName(_fromUtf8("display"))
self.back = self.display#QtGui.QWidget(self.display)
#self.back.setGeometry(QtCore.QRect(5, 5, self.frameSize, self.frameSize))self.back.setObjectName(_fromUtf8("back"))
color = QtGui.QColor(0, 0, 0)
self.back.setAutoFillBackground(True)
self.back.setPalette(QtGui.QPalette(color))
self.name = QtGui.QLabel(self.back)
self.name.setGeometry(QtCore.QRect(10, 10, 46, 13))
self.name.setObjectName(_fromUtf8("name"))
self.layer = Layer(self.back)
self.bg_image = self.layer.new_widget(LayerTyp.Background, 'bg_First', QtGui.QColor(0, 200, 0))
self.bg_video = self.layer.new_widget(LayerTyp.Background, 'bg_Second', QtGui.QColor(170, 0, 0))
self.bg_songs = self.layer.new_widget(LayerTyp.Background, 'bg_Third', QtGui.QColor(0, 0, 255))
self.bg_other = self.layer.new_widget(LayerTyp.Background, 'bg_Fourth', QtGui.QColor(100, 100, 100))
self.add_web_view()
self.layer.add_widget(self.webView, LayerTyp.Foreground, 'fg_First(webView)')
self.fg_image = self.layer.new_widget(LayerTyp.Foreground, 'fg_Second')
self.fg_video = self.layer.new_widget(LayerTyp.Foreground, 'fg_Third')
self.fg_songs = self.layer.new_widget(LayerTyp.Foreground, 'fg_Fourth')
self.Foreground = QtGui.QGroupBox(self.centralwidget)
self.Foreground.setGeometry(QtCore.QRect(20, 200, 120, 121))
self.Foreground.setObjectName(_fromUtf8("Foreground"))
self.rf_First = QtGui.QRadioButton(self.Foreground)
self.rf_First.setGeometry(QtCore.QRect(20, 20, 82, 17))
self.rf_First.setObjectName(_fromUtf8("rf_First"))
self.rf_Second = QtGui.QRadioButton(self.Foreground)
self.rf_Second.setGeometry(QtCore.QRect(20, 40, 82, 17))
self.rf_Second.setObjectName(_fromUtf8("rf_Second"))
self.rf_Third = QtGui.QRadioButton(self.Foreground)
self.rf_Third.setGeometry(QtCore.QRect(20, 60, 82, 17))
self.rf_Third.setObjectName(_fromUtf8("rf_Third"))
self.rf_Fourth = QtGui.QRadioButton(self.Foreground)
self.rf_Fourth.setGeometry(QtCore.QRect(20, 80, 82, 17))
self.rf_Fourth.setObjectName(_fromUtf8("rf_Fourth"))
self.bg_foreground = QtGui.QButtonGroup(self.Foreground)
self.bg_foreground.addButton(self.rf_First, 0)
self.bg_foreground.addButton(self.rf_Second, 1)
self.bg_foreground.addButton(self.rf_Third, 2)
self.bg_foreground.addButton(self.rf_Fourth, 3)
self.Background = QtGui.QGroupBox(self.centralwidget)
self.Background.setGeometry(QtCore.QRect(180, 200, 120, 121))
self.Background.setObjectName(_fromUtf8("Background"))
self.rb_First = QtGui.QRadioButton(self.Background)
self.rb_First.setGeometry(QtCore.QRect(20, 20, 82, 17))
self.rb_First.setObjectName(_fromUtf8("rb_First"))
self.rb_Second = QtGui.QRadioButton(self.Background)
self.rb_Second.setGeometry(QtCore.QRect(20, 40, 82, 17))
self.rb_Second.setObjectName(_fromUtf8("rb_Second"))
self.rb_Third = QtGui.QRadioButton(self.Background)
self.rb_Third.setGeometry(QtCore.QRect(20, 60, 82, 17))
self.rb_Third.setObjectName(_fromUtf8("rb_Third"))
self.rb_Fourth = QtGui.QRadioButton(self.Background)
self.rb_Fourth.setGeometry(QtCore.QRect(20, 80, 82, 17))
self.rb_Fourth.setObjectName(_fromUtf8("rb_Fourth"))
self.bg_background = QtGui.QButtonGroup(self.Background)
self.bg_background.addButton(self.rb_First, 0)
self.bg_background.addButton(self.rb_Second, 1)
self.bg_background.addButton(self.rb_Third, 2)
self.bg_background.addButton(self.rb_Fourth, 3)
self.bHide = QtGui.QPushButton(self.centralwidget)
self.bHide.setGeometry(QtCore.QRect(30, 140, 75, 23))
self.bHide.setObjectName(_fromUtf8("bHide"))
self.bShield = QtGui.QPushButton(self.centralwidget)
self.bShield.setGeometry(QtCore.QRect(120, 140, 75, 23))
self.bShield.setObjectName(_fromUtf8("bShield"))
self.bAlarm = QtGui.QPushButton(self.centralwidget)
self.bAlarm.setGeometry(QtCore.QRect(210, 140, 75, 23))
self.bAlarm.setObjectName(_fromUtf8("bAlarm"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 20))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.bHide.clicked.connect(self.switch_hide)
self.bAlarm.clicked.connect(self.resizing)
self.bShield.clicked.connect(self.switch_shield)
self.bg_foreground.buttonClicked.connect(self.switch_foregrounds)
self.bg_background.buttonClicked.connect(self.switch_backgrounds)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.name.setText(QtGui.QApplication.translate("MainWindow", "back", None, QtGui.QApplication.UnicodeUTF8))
self.Foreground.setTitle(QtGui.QApplication.translate("MainWindow", "Foreground", None, QtGui.QApplication.UnicodeUTF8))
self.rf_First.setText(QtGui.QApplication.translate("MainWindow", "First", None, QtGui.QApplication.UnicodeUTF8))
self.rf_Second.setText(QtGui.QApplication.translate("MainWindow", "Second", None, QtGui.QApplication.UnicodeUTF8))
self.rf_Third.setText(QtGui.QApplication.translate("MainWindow", "Third", None, QtGui.QApplication.UnicodeUTF8))
self.rf_Fourth.setText(QtGui.QApplication.translate("MainWindow", "Fourth", None, QtGui.QApplication.UnicodeUTF8))
self.Background.setTitle(QtGui.QApplication.translate("MainWindow", "Background", None, QtGui.QApplication.UnicodeUTF8))
self.rb_First.setText(QtGui.QApplication.translate("MainWindow", "First", None, QtGui.QApplication.UnicodeUTF8))
self.rb_Second.setText(QtGui.QApplication.translate("MainWindow", "Second", None, QtGui.QApplication.UnicodeUTF8))
self.rb_Third.setText(QtGui.QApplication.translate("MainWindow", "Third", None, QtGui.QApplication.UnicodeUTF8))
self.rb_Fourth.setText(QtGui.QApplication.translate("MainWindow", "Fourth", None, QtGui.QApplication.UnicodeUTF8))
self.bHide.setText(QtGui.QApplication.translate("MainWindow", "Hide", None, QtGui.QApplication.UnicodeUTF8))
self.bShield.setText(QtGui.QApplication.translate("MainWindow", "Shield", None, QtGui.QApplication.UnicodeUTF8))
self.bAlarm.setText(QtGui.QApplication.translate("MainWindow", "Grow", None, QtGui.QApplication.UnicodeUTF8))
def add_web_view(self):
self.webView = QtWebKit.QWebView(self.back)
self.webView.setGeometry(QtCore.QRect(0, 0, self.back.size().width(), self.back.size().height()))
self.page = self.webView.page()
self.frame = self.page.mainFrame()
def switch_shield(self):
self.layer.shield.setVisible(not self.layer.shield.isVisible())
def switch_hide(self):
self.display.setVisible(not self.display.isVisible())
def switch_backgrounds(self, button):
self.layer.activate_layer_index(self.layer.foregrounds.currentIndex(), self.bg_background.id(button))
def switch_foregrounds(self, button):
self.layer.activate_layer_index(self.bg_foreground.id(button), self.layer.backgrounds.currentIndex())
def resizing(self):
self.frameSize = self.frameSize + 50
self.display.setGeometry(QtCore.QRect(360, 40, self.frameSize, self.frameSize))
self.layer.resize_widget()
class LayerTyp(object):
"""This class defines the different types of Layers"""Foreground = 1Background = 2class Layer(object):
"""This class provide a mechanism to store widget in a multi layer environmentSo new foregrounds or backgrounds for other plugins can added and managed easely"""def __init__(self, parent):
self.parent = parent
self.size = QtCore.QRect(0, 0, self.parent.size().width(), self.parent.size().height())
self.alarm = QtGui.QWidget(self.parent)
self.alarm.setGeometry(self.size)
self.shield = QtGui.QWidget(self.parent)
self.shield.setGeometry(self.size)
self.shield.setAutoFillBackground(True)
self.shield.setPalette(QtGui.QPalette(QtGui.QColor(0, 255, 0)))
self.foregrounds = QtGui.QStackedWidget(self.parent)
self.foregrounds.setGeometry(self.size)
self.backgrounds = QtGui.QStackedWidget(self.parent)
self.backgrounds.setGeometry(self.size)
def new_widget(self, type, title, color=QtGui.QColor(0, 0, 0)):
widget = QtGui.QWidget(self.parent)
self.add_widget(widget, type, title, color)
return widgetdef add_widget(self, widget, type, name, color=QtGui.QColor(0, 0, 0)):
if type == LayerTyp.Foreground:
widget.setAttribute(QtCore.Qt.WA_NoSystemBackground)
widget.setGeometry(self.size)
title = QtGui.QLabel(widget)
title.setGeometry(QtCore.QRect(widget.size().width()/2-50, widget.size().height()/2, 100, 13))
title.setObjectName(name)
title.setText(name)
self.foregrounds.addWidget(widget)
else:widget.setGeometry(self.size)
widget.setAutoFillBackground(True)
widget.setPalette(QtGui.QPalette(color))
title = QtGui.QLabel(widget)
title.setGeometry(QtCore.QRect(widget.size().width()-100, 10, 100, 13))
title.setObjectName(name)
title.setText(name)
self.backgrounds.addWidget(widget)
self.arrange_widgets()
def activate_layer_widget(self, fg_widget=None, bg_widget=None):
if fg_widget:self.foregrounds.setVisible(True)
self.foregrounds.setCurrentWidget(fg_widget)
else:self.foregrounds.setVisible(False)
if bg_widget:self.backgrounds.setVisible(True)
self.backgrounds.setCurrentWidget(bg_widget)
else:self.backgrounds.setVisible(False)
def activate_layer_index(self, fg_index=-1, bg_index=-1):
if fg_index > -1:
self.foregrounds.setVisible(True)
self.foregrounds.setCurrentIndex(fg_index)
else:self.foregrounds.setVisible(False)
if bg_index > -1:
self.backgrounds.setVisible(True)
self.backgrounds.setCurrentIndex(bg_index)
else:self.backgrounds.setVisible(False)
def arrange_widgets(self):
self.alarm.raise_()
self.shield.stackUnder(self.alarm)
self.foregrounds.stackUnder(self.shield)
self.backgrounds.stackUnder(self.foregrounds)
def resize_widget(self):
self.size = QtCore.QRect(0, 0, self.parent.size().width(), self.parent.size().height())
#self.setGeometry(self.size)self.shield.setGeometry(self.size)
self.shield.setAutoFillBackground(True)
self.shield.setPalette(QtGui.QPalette(QtGui.QColor(0, 255, 0)))
self.backgrounds.setGeometry(self.size)
self.foregrounds.setGeometry(self.size)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())