Scratchpad:Layer Mechanism

From OpenLP

Jump to: navigation, search

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

alt text

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:


The other QStackedWidget Object manage different foreground widgets, these widgets should be transparent in the most cases.

Examples are:

Advantages

Open Issues

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

  1. from PyQt4 import QtCore, QtGui, QtWebKit
  2.  
  3. try:
  4.     _fromUtf8 = QtCore.QString.fromUtf8
  5. except AttributeError:
  6.     _fromUtf8 = lambda s: s
  7.  
  8. class Ui_MainWindow(object):
  9.     def setupUi(self, MainWindow):
  10.         MainWindow.setObjectName(_fromUtf8("MainWindow"))
  11.         MainWindow.resize(800, 600)
  12.         self.frameSize = 400
  13.         self.centralwidget = QtGui.QWidget(MainWindow)
  14.         self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
  15.         #self.display = QtGui.QGraphicsView(self.centralwidget)
  16.         self.display = QtGui.QFrame(self.centralwidget)
  17.         #self.display = QtGui.QFrame()
  18.         self.display.setGeometry(QtCore.QRect(360, 40, self.frameSize, self.frameSize))
  19.         self.display.setFrameShape(QtGui.QFrame.Box)
  20.         self.display.setFrameShadow(QtGui.QFrame.Raised)
  21.         self.display.setObjectName(_fromUtf8("display"))
  22.  
  23.         self.back = self.display#QtGui.QWidget(self.display)
  24.  
  25.         #self.back.setGeometry(QtCore.QRect(5, 5, self.frameSize, self.frameSize))
  26.         self.back.setObjectName(_fromUtf8("back"))
  27.         color = QtGui.QColor(0, 0, 0)
  28.         self.back.setAutoFillBackground(True)
  29.         self.back.setPalette(QtGui.QPalette(color))
  30.         self.name = QtGui.QLabel(self.back)
  31.         self.name.setGeometry(QtCore.QRect(10, 10, 46, 13))
  32.         self.name.setObjectName(_fromUtf8("name"))
  33.  
  34.         self.layer = Layer(self.back)
  35.         self.bg_image = self.layer.new_widget(LayerTyp.Background, 'bg_First', QtGui.QColor(0, 200, 0))
  36.         self.bg_video = self.layer.new_widget(LayerTyp.Background, 'bg_Second', QtGui.QColor(170, 0, 0))
  37.         self.bg_songs = self.layer.new_widget(LayerTyp.Background, 'bg_Third', QtGui.QColor(0, 0, 255))
  38.         self.bg_other = self.layer.new_widget(LayerTyp.Background, 'bg_Fourth', QtGui.QColor(100, 100, 100))
  39.  
  40.         self.add_web_view()
  41.         self.layer.add_widget(self.webView, LayerTyp.Foreground, 'fg_First(webView)')
  42.         self.fg_image = self.layer.new_widget(LayerTyp.Foreground, 'fg_Second')
  43.         self.fg_video = self.layer.new_widget(LayerTyp.Foreground, 'fg_Third')
  44.         self.fg_songs = self.layer.new_widget(LayerTyp.Foreground, 'fg_Fourth')
  45.  
  46.         self.Foreground = QtGui.QGroupBox(self.centralwidget)
  47.         self.Foreground.setGeometry(QtCore.QRect(20, 200, 120, 121))
  48.         self.Foreground.setObjectName(_fromUtf8("Foreground"))
  49.         self.rf_First = QtGui.QRadioButton(self.Foreground)
  50.         self.rf_First.setGeometry(QtCore.QRect(20, 20, 82, 17))
  51.         self.rf_First.setObjectName(_fromUtf8("rf_First"))
  52.         self.rf_Second = QtGui.QRadioButton(self.Foreground)
  53.         self.rf_Second.setGeometry(QtCore.QRect(20, 40, 82, 17))
  54.         self.rf_Second.setObjectName(_fromUtf8("rf_Second"))
  55.         self.rf_Third = QtGui.QRadioButton(self.Foreground)
  56.         self.rf_Third.setGeometry(QtCore.QRect(20, 60, 82, 17))
  57.         self.rf_Third.setObjectName(_fromUtf8("rf_Third"))
  58.         self.rf_Fourth = QtGui.QRadioButton(self.Foreground)
  59.         self.rf_Fourth.setGeometry(QtCore.QRect(20, 80, 82, 17))
  60.         self.rf_Fourth.setObjectName(_fromUtf8("rf_Fourth"))
  61.         self.bg_foreground = QtGui.QButtonGroup(self.Foreground)
  62.         self.bg_foreground.addButton(self.rf_First, 0)
  63.         self.bg_foreground.addButton(self.rf_Second, 1)
  64.         self.bg_foreground.addButton(self.rf_Third, 2)
  65.         self.bg_foreground.addButton(self.rf_Fourth, 3)
  66.         self.Background = QtGui.QGroupBox(self.centralwidget)
  67.         self.Background.setGeometry(QtCore.QRect(180, 200, 120, 121))
  68.         self.Background.setObjectName(_fromUtf8("Background"))
  69.         self.rb_First = QtGui.QRadioButton(self.Background)
  70.         self.rb_First.setGeometry(QtCore.QRect(20, 20, 82, 17))
  71.         self.rb_First.setObjectName(_fromUtf8("rb_First"))
  72.         self.rb_Second = QtGui.QRadioButton(self.Background)
  73.         self.rb_Second.setGeometry(QtCore.QRect(20, 40, 82, 17))
  74.         self.rb_Second.setObjectName(_fromUtf8("rb_Second"))
  75.         self.rb_Third = QtGui.QRadioButton(self.Background)
  76.         self.rb_Third.setGeometry(QtCore.QRect(20, 60, 82, 17))
  77.         self.rb_Third.setObjectName(_fromUtf8("rb_Third"))
  78.         self.rb_Fourth = QtGui.QRadioButton(self.Background)
  79.         self.rb_Fourth.setGeometry(QtCore.QRect(20, 80, 82, 17))
  80.         self.rb_Fourth.setObjectName(_fromUtf8("rb_Fourth"))
  81.         self.bg_background = QtGui.QButtonGroup(self.Background)
  82.         self.bg_background.addButton(self.rb_First, 0)
  83.         self.bg_background.addButton(self.rb_Second, 1)
  84.         self.bg_background.addButton(self.rb_Third, 2)
  85.         self.bg_background.addButton(self.rb_Fourth, 3)
  86.         self.bHide = QtGui.QPushButton(self.centralwidget)
  87.         self.bHide.setGeometry(QtCore.QRect(30, 140, 75, 23))
  88.         self.bHide.setObjectName(_fromUtf8("bHide"))
  89.         self.bShield = QtGui.QPushButton(self.centralwidget)
  90.         self.bShield.setGeometry(QtCore.QRect(120, 140, 75, 23))
  91.         self.bShield.setObjectName(_fromUtf8("bShield"))
  92.         self.bAlarm = QtGui.QPushButton(self.centralwidget)
  93.         self.bAlarm.setGeometry(QtCore.QRect(210, 140, 75, 23))
  94.         self.bAlarm.setObjectName(_fromUtf8("bAlarm"))
  95.         MainWindow.setCentralWidget(self.centralwidget)
  96.         self.menubar = QtGui.QMenuBar(MainWindow)
  97.         self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 20))
  98.         self.menubar.setObjectName(_fromUtf8("menubar"))
  99.         MainWindow.setMenuBar(self.menubar)
  100.         self.statusbar = QtGui.QStatusBar(MainWindow)
  101.         self.statusbar.setObjectName(_fromUtf8("statusbar"))
  102.         MainWindow.setStatusBar(self.statusbar)
  103.  
  104.         self.retranslateUi(MainWindow)
  105.         QtCore.QMetaObject.connectSlotsByName(MainWindow)
  106.         self.bHide.clicked.connect(self.switch_hide)
  107.         self.bAlarm.clicked.connect(self.resizing)
  108.         self.bShield.clicked.connect(self.switch_shield)
  109.         self.bg_foreground.buttonClicked.connect(self.switch_foregrounds)
  110.         self.bg_background.buttonClicked.connect(self.switch_backgrounds)
  111.  
  112.     def retranslateUi(self, MainWindow):
  113.         MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
  114.         self.name.setText(QtGui.QApplication.translate("MainWindow", "back", None, QtGui.QApplication.UnicodeUTF8))
  115.         self.Foreground.setTitle(QtGui.QApplication.translate("MainWindow", "Foreground", None, QtGui.QApplication.UnicodeUTF8))
  116.         self.rf_First.setText(QtGui.QApplication.translate("MainWindow", "First", None, QtGui.QApplication.UnicodeUTF8))
  117.         self.rf_Second.setText(QtGui.QApplication.translate("MainWindow", "Second", None, QtGui.QApplication.UnicodeUTF8))
  118.         self.rf_Third.setText(QtGui.QApplication.translate("MainWindow", "Third", None, QtGui.QApplication.UnicodeUTF8))
  119.         self.rf_Fourth.setText(QtGui.QApplication.translate("MainWindow", "Fourth", None, QtGui.QApplication.UnicodeUTF8))
  120.         self.Background.setTitle(QtGui.QApplication.translate("MainWindow", "Background", None, QtGui.QApplication.UnicodeUTF8))
  121.         self.rb_First.setText(QtGui.QApplication.translate("MainWindow", "First", None, QtGui.QApplication.UnicodeUTF8))
  122.         self.rb_Second.setText(QtGui.QApplication.translate("MainWindow", "Second", None, QtGui.QApplication.UnicodeUTF8))
  123.         self.rb_Third.setText(QtGui.QApplication.translate("MainWindow", "Third", None, QtGui.QApplication.UnicodeUTF8))
  124.         self.rb_Fourth.setText(QtGui.QApplication.translate("MainWindow", "Fourth", None, QtGui.QApplication.UnicodeUTF8))
  125.         self.bHide.setText(QtGui.QApplication.translate("MainWindow", "Hide", None, QtGui.QApplication.UnicodeUTF8))
  126.         self.bShield.setText(QtGui.QApplication.translate("MainWindow", "Shield", None, QtGui.QApplication.UnicodeUTF8))
  127.         self.bAlarm.setText(QtGui.QApplication.translate("MainWindow", "Grow", None, QtGui.QApplication.UnicodeUTF8))
  128.  
  129.     def add_web_view(self):
  130.         self.webView = QtWebKit.QWebView(self.back)
  131.         self.webView.setGeometry(QtCore.QRect(0, 0, self.back.size().width(), self.back.size().height()))
  132.         self.page = self.webView.page()
  133.         self.frame = self.page.mainFrame()
  134.  
  135.     def switch_shield(self):
  136.       self.layer.shield.setVisible(not self.layer.shield.isVisible())
  137.  
  138.     def switch_hide(self):
  139.       self.display.setVisible(not self.display.isVisible())
  140.  
  141.     def switch_backgrounds(self, button):
  142.       self.layer.activate_layer_index(self.layer.foregrounds.currentIndex(), self.bg_background.id(button))
  143.  
  144.     def switch_foregrounds(self, button):
  145.       self.layer.activate_layer_index(self.bg_foreground.id(button), self.layer.backgrounds.currentIndex())
  146.  
  147.     def resizing(self):
  148.       self.frameSize = self.frameSize + 50
  149.       self.display.setGeometry(QtCore.QRect(360, 40, self.frameSize, self.frameSize))
  150.       self.layer.resize_widget()
  151.  
  152. class LayerTyp(object):
  153.   """
  154.   This class defines the different types of Layers
  155.   """
  156.   Foreground = 1
  157.   Background = 2
  158.  
  159. class Layer(object):
  160.   """
  161.   This class provide a mechanism to store widget in a multi layer environment
  162.   So new foregrounds or backgrounds for other plugins can added and managed easely
  163.   """
  164.   def __init__(self, parent):
  165.     self.parent = parent
  166.     self.size = QtCore.QRect(0, 0, self.parent.size().width(), self.parent.size().height())
  167.  
  168.     self.alarm = QtGui.QWidget(self.parent)
  169.     self.alarm.setGeometry(self.size)
  170.     self.shield = QtGui.QWidget(self.parent)
  171.     self.shield.setGeometry(self.size)
  172.     self.shield.setAutoFillBackground(True)
  173.     self.shield.setPalette(QtGui.QPalette(QtGui.QColor(0, 255, 0)))
  174.     self.foregrounds = QtGui.QStackedWidget(self.parent)
  175.     self.foregrounds.setGeometry(self.size)
  176.     self.backgrounds = QtGui.QStackedWidget(self.parent)
  177.     self.backgrounds.setGeometry(self.size)
  178.  
  179.   def new_widget(self, type, title, color=QtGui.QColor(0, 0, 0)):
  180.     widget = QtGui.QWidget(self.parent)
  181.     self.add_widget(widget, type, title, color)
  182.     return widget
  183.  
  184.   def add_widget(self, widget, type, name, color=QtGui.QColor(0, 0, 0)):
  185.     if type == LayerTyp.Foreground:
  186.       widget.setAttribute(QtCore.Qt.WA_NoSystemBackground)
  187.       widget.setGeometry(self.size)
  188.       title = QtGui.QLabel(widget)
  189.       title.setGeometry(QtCore.QRect(widget.size().width()/2-50, widget.size().height()/2, 100, 13))
  190.       title.setObjectName(name)
  191.       title.setText(name)
  192.       self.foregrounds.addWidget(widget)
  193.     else:
  194.       widget.setGeometry(self.size)
  195.       widget.setAutoFillBackground(True)
  196.       widget.setPalette(QtGui.QPalette(color))
  197.       title = QtGui.QLabel(widget)
  198.       title.setGeometry(QtCore.QRect(widget.size().width()-100, 10, 100, 13))
  199.       title.setObjectName(name)
  200.       title.setText(name)
  201.       self.backgrounds.addWidget(widget)
  202.     self.arrange_widgets()
  203.  
  204.   def activate_layer_widget(self, fg_widget=None, bg_widget=None):
  205.     if fg_widget:
  206.       self.foregrounds.setVisible(True)
  207.       self.foregrounds.setCurrentWidget(fg_widget)
  208.     else:
  209.       self.foregrounds.setVisible(False)
  210.     if bg_widget:
  211.       self.backgrounds.setVisible(True)
  212.       self.backgrounds.setCurrentWidget(bg_widget)
  213.     else:
  214.       self.backgrounds.setVisible(False)
  215.  
  216.   def activate_layer_index(self, fg_index=-1, bg_index=-1):
  217.     if fg_index > -1:
  218.       self.foregrounds.setVisible(True)
  219.       self.foregrounds.setCurrentIndex(fg_index)
  220.     else:
  221.       self.foregrounds.setVisible(False)
  222.     if bg_index > -1:
  223.       self.backgrounds.setVisible(True)
  224.       self.backgrounds.setCurrentIndex(bg_index)
  225.     else:
  226.       self.backgrounds.setVisible(False)
  227.  
  228.   def arrange_widgets(self):
  229.     self.alarm.raise_()
  230.     self.shield.stackUnder(self.alarm)
  231.     self.foregrounds.stackUnder(self.shield)
  232.     self.backgrounds.stackUnder(self.foregrounds)
  233.  
  234.   def resize_widget(self):
  235.     self.size = QtCore.QRect(0, 0, self.parent.size().width(), self.parent.size().height())
  236.     #self.setGeometry(self.size)
  237.     self.shield.setGeometry(self.size)
  238.     self.shield.setAutoFillBackground(True)
  239.     self.shield.setPalette(QtGui.QPalette(QtGui.QColor(0, 255, 0)))
  240.     self.backgrounds.setGeometry(self.size)
  241.     self.foregrounds.setGeometry(self.size)
  242.  
  243.  
  244. if __name__ == "__main__":
  245.     import sys
  246.     app = QtGui.QApplication(sys.argv)
  247.     MainWindow = QtGui.QMainWindow()
  248.     ui = Ui_MainWindow()
  249.     ui.setupUi(MainWindow)
  250.     MainWindow.show()
  251.     sys.exit(app.exec_())
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox