1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-08 03:52:42 +01:00

Replace setContextProperty that will be removed in Qt6 with qmlRegisterSingletonInstance

This commit is contained in:
Elias Steurer 2020-07-18 20:21:47 +02:00
parent 3ba635a077
commit 3c1f5f72a4
7 changed files with 95 additions and 65 deletions

View File

@ -1,70 +1,70 @@
import QtQuick 2.12 import QtQuick 2.14
import ScreenPlay.Wallpaper 1.0 import QtQml 2.14
import ScreenPlayWallpaper 1.0
Rectangle { Rectangle {
id: root id: root
anchors.fill: parent anchors.fill: parent
color: { color: {
if (desktopProperties.color === null) { if (Qt.platform.os !== "windows") {
return "black" return "black"
} else { } else {
return desktopProperties.color return Wallpaper.windowsDesktopProperties.color
} }
} }
property bool canFadeByWallpaperFillMode: true property bool canFadeByWallpaperFillMode: true
Component.onCompleted: { Component.onCompleted: {
init(); init()
} }
Connections { Connections {
target: window target: Wallpaper
function onQmlExit() { function onQmlExit() {
if (canFadeByWallpaperFillMode && window.canFade) { if (canFadeByWallpaperFillMode && Wallpaper.canFade) {
imgCover.state = "outExit" imgCover.state = "outExit"
} else { } else {
window.terminate() Wallpaper.terminate()
} }
} }
function onQmlSceneValueReceived(key,value) { function onQmlSceneValueReceived(key, value) {
var obj2 = 'import QtQuick 2.0; Item {Component.onCompleted: loader.item.' var obj2 = 'import QtQuick 2.0; Item {Component.onCompleted: loader.item.'
+ key + ' = ' + value + '; }' + key + ' = ' + value + '; }'
print(key, value) print(key, value)
var newObject = Qt.createQmlObject(obj2.toString(), root, "err") var newObject = Qt.createQmlObject(obj2.toString(), root, "err")
newObject.destroy(10000) newObject.destroy(10000)
} }
function onReloadQML(){
function onReloadQML() {
loader.sourceComponent = undefined loader.sourceComponent = undefined
loader.source = "" loader.source = ""
window.clearComponentCache() Wallpaper.clearComponentCache()
root.init() root.init()
} }
} }
function init(){ function init() {
switch (window.type) { switch (Wallpaper.type) {
case Wallpaper.WallpaperType.Video: case Wallpaper.WallpaperType.Video:
loader.source = "qrc:/WebView.qml" loader.source = "qrc:/WebView.qml"
break break
case Wallpaper.WallpaperType.Html: case Wallpaper.WallpaperType.Html:
loader.webViewUrl = Qt.resolvedUrl(window.fullContentPath) loader.webViewUrl = Qt.resolvedUrl(Wallpaper.fullContentPath)
loader.source = "qrc:/WebView.qml" loader.source = "qrc:/WebView.qml"
break break
case Wallpaper.WallpaperType.Qml: case Wallpaper.WallpaperType.Qml:
loader.source = Qt.resolvedUrl(window.fullContentPath) loader.source = Qt.resolvedUrl(Wallpaper.fullContentPath)
imgCover.state = "out" imgCover.state = "out"
break break
} }
} }
function fadeIn() { function fadeIn() {
window.setVisible(true) Wallpaper.setVisible(true)
if (canFadeByWallpaperFillMode && window.canFade) { if (canFadeByWallpaperFillMode && Wallpaper.canFade) {
imgCover.state = "out" imgCover.state = "out"
} else { } else {
imgCover.opacity = 0 imgCover.opacity = 0
@ -77,10 +77,10 @@ Rectangle {
property string webViewUrl property string webViewUrl
onStatusChanged: { onStatusChanged: {
if (loader.status === Loader.Ready) { if (loader.status === Loader.Ready) {
if (window.type === Wallpaper.WallpaperType.Html if (Wallpaper.type === Wallpaper.WallpaperType.Html
|| window.type === Wallpaper.WallpaperType.ThreeJSScene) { || Wallpaper.type === Wallpaper.WallpaperType.ThreeJSScene) {
loader.item.url = loader.webViewUrl loader.item.url = loader.webViewUrl
print(loader.item.url," --- ",loader.webViewUrl) print(loader.item.url, " --- ", loader.webViewUrl)
} }
} }
} }
@ -103,9 +103,16 @@ Rectangle {
} }
state: "in" state: "in"
sourceSize.width: window.width sourceSize.width: Wallpaper.width
sourceSize.height: window.height sourceSize.height: Wallpaper.height
source: Qt.resolvedUrl("file:///" + desktopProperties.wallpaperPath) source: {
if (Qt.platform.os === "windows") {
return Qt.resolvedUrl(
"file:///" + Wallpaper.windowsDesktopProperties.wallpaperPath)
} else {
return ""
}
}
states: [ states: [
State { State {
@ -152,7 +159,7 @@ Rectangle {
property: "opacity" property: "opacity"
} }
ScriptAction { ScriptAction {
script: window.terminate() script: Wallpaper.terminate()
} }
} }
} }
@ -160,7 +167,10 @@ Rectangle {
Component.onCompleted: { Component.onCompleted: {
switch (desktopProperties.wallpaperStyle) { if (Qt.platform.os !== "windows")
return
switch (Wallpaper.windowsDesktopProperties.wallpaperStyle) {
case 10: case 10:
imgCover.fillMode = Image.PreserveAspectCrop imgCover.fillMode = Image.PreserveAspectCrop
break break

View File

@ -1,7 +1,7 @@
import QtQuick 2.0 import QtQuick 2.0
import QtWebEngine 1.8 import QtWebEngine 1.8
import ScreenPlay.Wallpaper 1.0 import ScreenPlayWallpaper 1.0
Item { Item {
id: root id: root
@ -20,24 +20,16 @@ Item {
WebEngine.settings.focusOnNavigationEnabled = true WebEngine.settings.focusOnNavigationEnabled = true
} }
Connections {
target: window
function onReloadVideo() {
webView.runJavaScript(root.getSetVideoCommand())
}
}
function getSetVideoCommand() { function getSetVideoCommand() {
// TODO 30: // TODO 30:
// Currently wont work. Commit anyways til QtCreator and Qt work with js template literals // Currently wont work. Commit anyways til QtCreator and Qt work with js template literals
var src = "" var src = ""
src += "var videoPlayer = document.getElementById('videoPlayer');" src += "var videoPlayer = document.getElementById('videoPlayer');"
src += "var videoSource = document.getElementById('videoSource');" src += "var videoSource = document.getElementById('videoSource');"
src += "videoSource.src = '" + window.fullContentPath + "';" src += "videoSource.src = '" + Wallpaper.fullContentPath + "';"
src += "videoPlayer.load();" src += "videoPlayer.load();"
src += "videoPlayer.volume = " + window.volume + ";" src += "videoPlayer.volume = " + Wallpaper.volume + ";"
src += "videoPlayer.setAttribute('style', 'object-fit :" + window.fillMode + ";');" src += "videoPlayer.setAttribute('style', 'object-fit :" + Wallpaper.fillMode + ";');"
src += "videoPlayer.play();" src += "videoPlayer.play();"
return src return src
@ -49,19 +41,19 @@ Item {
anchors.fill: parent anchors.fill: parent
url: { url: {
if (window.type === Wallpaper.WallpaperType.Video) { if (Wallpaper.type === Wallpaper.WallpaperType.Video) {
return Qt.resolvedUrl(window.getApplicationPath( return Qt.resolvedUrl(Wallpaper.getApplicationPath(
) + "/index.html") ) + "/index.html")
} }
if (window.type === Wallpaper.WallpaperType.Html) { if (Wallpaper.type === Wallpaper.WallpaperType.Html) {
return Qt.resolvedUrl(window.fullContentPath + "/index.html") return Qt.resolvedUrl(Wallpaper.fullContentPath + "/index.html")
} }
} }
onJavaScriptConsoleMessage: print(lineNumber, message) onJavaScriptConsoleMessage: print(lineNumber, message)
onLoadProgressChanged: { onLoadProgressChanged: {
if ((loadProgress === 100)) { if ((loadProgress === 100)) {
if (window.type === Wallpaper.WallpaperType.Video) { if (Wallpaper.type === Wallpaper.WallpaperType.Video) {
webView.runJavaScript(root.getSetVideoCommand(), webView.runJavaScript(root.getSetVideoCommand(),
function (result) { function (result) {
@ -98,13 +90,17 @@ Item {
id: timerCover id: timerCover
interval: 300 interval: 300
onTriggered: { onTriggered: {
webView.visible = !window.visualsPaused webView.visible = !Wallpaper.visualsPaused
txtVisualsPaused.visible = window.visualsPaused txtVisualsPaused.visible = Wallpaper.visualsPaused
} }
} }
Connections { Connections {
target: window target: Wallpaper
function onReloadVideo() {
webView.runJavaScript(root.getSetVideoCommand())
}
function onQmlExit() { function onQmlExit() {
webView.runJavaScript( webView.runJavaScript(
@ -117,7 +113,7 @@ Item {
"var videoPlayer = document.getElementById('videoPlayer'); videoPlayer.volume = 0;") "var videoPlayer = document.getElementById('videoPlayer'); videoPlayer.volume = 0;")
} else { } else {
webView.runJavaScript( webView.runJavaScript(
"var videoPlayer = document.getElementById('videoPlayer'); videoPlayer.volume = " + window.volume + ";") "var videoPlayer = document.getElementById('videoPlayer'); videoPlayer.volume = " + Wallpaper.volume + ";")
} }
} }
@ -159,7 +155,7 @@ Item {
function onVisualsPausedChanged(visualsPaused) { function onVisualsPausedChanged(visualsPaused) {
if (visualsPaused) { if (visualsPaused) {
// Wait until window animation is finsihed // Wait until Wallpaper animation is finsihed
timerCover.restart() timerCover.restart()
} else { } else {
webView.visible = true webView.visible = true

View File

@ -11,6 +11,7 @@ BaseWindow::BaseWindow(QString projectFilePath, const QVector<int> activeScreens
, m_activeScreensList(activeScreensList) , m_activeScreensList(activeScreensList)
{ {
QApplication::instance()->installEventFilter(this); QApplication::instance()->installEventFilter(this);
qRegisterMetaType<BaseWindow::WallpaperType>(); qRegisterMetaType<BaseWindow::WallpaperType>();
qmlRegisterType<BaseWindow>("ScreenPlay.Wallpaper", 1, 0, "Wallpaper"); qmlRegisterType<BaseWindow>("ScreenPlay.Wallpaper", 1, 0, "Wallpaper");
@ -147,14 +148,13 @@ void BaseWindow::replaceWallpaper(
setFillMode(fillMode); setFillMode(fillMode);
setType(parseWallpaperType(type)); setType(parseWallpaperType(type));
setFullContentPath("file:///" + absolutePath + "/" + file); setFullContentPath("file:///" + absolutePath + "/" + file);
qInfo( ) << file; qInfo() << file;
if (m_type == WallpaperType::Qml || m_type == WallpaperType::Html) if (m_type == WallpaperType::Qml || m_type == WallpaperType::Html)
emit reloadQML(); emit reloadQML();
if (m_type == WallpaperType::Video) if (m_type == WallpaperType::Video)
emit reloadVideo(); emit reloadVideo();
} }
// Used for loading shader // Used for loading shader

View File

@ -213,7 +213,6 @@ public slots:
const QString type, const QString type,
const bool checkWallpaperVisible) final; const bool checkWallpaperVisible) final;
QString loadFromFile(const QString& filename); QString loadFromFile(const QString& filename);
QString getApplicationPath() QString getApplicationPath()

View File

@ -3,7 +3,7 @@
WindowsDesktopProperties::WindowsDesktopProperties(QObject* parent) WindowsDesktopProperties::WindowsDesktopProperties(QObject* parent)
: QObject(parent) : QObject(parent)
{ {
qmlRegisterType<WindowsDesktopProperties>();
QSettings settings("HKEY_CURRENT_USER\\Control Panel\\Desktop", QSettings::NativeFormat); QSettings settings("HKEY_CURRENT_USER\\Control Panel\\Desktop", QSettings::NativeFormat);
setWallpaperPath(settings.value("WallPaper").toString()); setWallpaperPath(settings.value("WallPaper").toString());
@ -26,18 +26,19 @@ WindowsDesktopProperties::WindowsDesktopProperties(QObject* parent)
int colorG = colorStringRGBList.at(1).toInt(); int colorG = colorStringRGBList.at(1).toInt();
int colorB = colorStringRGBList.at(2).toInt(); int colorB = colorStringRGBList.at(2).toInt();
setColor(QColor::fromRgb(colorR, colorG, colorB)); setColor(QColor::fromRgb(colorR, colorG, colorB));
} else {
setColor(QColor::fromRgb(0, 0, 0));
} }
QSettings settingsWindowsVersion("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", QSettings::NativeFormat); QSettings settingsWindowsVersion("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", QSettings::NativeFormat);
// Only Windows > 10 has this variable // Only Windows > 10 has this variable
if(!settingsWindowsVersion.contains("ReleaseID")) if (!settingsWindowsVersion.contains("ReleaseID"))
return; return;
bool canParse {false}; bool canParse { false };
int value = settingsWindowsVersion.value("ReleaseId").toInt(&canParse); int value = settingsWindowsVersion.value("ReleaseId").toInt(&canParse);
if(canParse) if (canParse)
setWindowsVersion(value); setWindowsVersion(value);
} }

View File

@ -1,5 +1,7 @@
#include "winwindow.h" #include "winwindow.h"
#include "qqml.h"
BOOL WINAPI SearchForWorkerWindow(HWND hwnd, LPARAM lparam) BOOL WINAPI SearchForWorkerWindow(HWND hwnd, LPARAM lparam)
{ {
// 0xXXXXXXX "" WorkerW // 0xXXXXXXX "" WorkerW
@ -78,9 +80,12 @@ WinWindow::WinWindow(
const QString& fillmode, const QString& fillmode,
const bool checkWallpaperVisible) const bool checkWallpaperVisible)
: BaseWindow(projectPath, activeScreensList, checkWallpaperVisible) : BaseWindow(projectPath, activeScreensList, checkWallpaperVisible)
{ {
qRegisterMetaType<WindowsDesktopProperties*>();
qRegisterMetaType<WinWindow*>();
m_windowsDesktopProperties = std::make_unique<WindowsDesktopProperties>();
m_windowHandle = reinterpret_cast<HWND>(m_window.winId()); m_windowHandle = reinterpret_cast<HWND>(m_window.winId());
if (!IsWindow(m_windowHandle)) { if (!IsWindow(m_windowHandle)) {
@ -123,13 +128,12 @@ WinWindow::WinWindow(
setWidth(m_window.width()); setWidth(m_window.width());
setHeight(m_window.height()); setHeight(m_window.height());
m_window.setResizeMode(QQuickView::ResizeMode::SizeRootObjectToView); qmlRegisterSingletonInstance<WinWindow>("ScreenPlayWallpaper", 1, 0, "Wallpaper", this);
m_window.rootContext()->setContextProperty("window", this);
m_window.rootContext()->setContextProperty("desktopProperties", &m_windowsDesktopProperties);
// Instead of setting "renderType: Text.NativeRendering" every time // Instead of setting "renderType: Text.NativeRendering" every time
// we can set it here once :) // we can set it here once :)
m_window.setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering); m_window.setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering);
m_window.setResizeMode(QQuickView::ResizeMode::SizeRootObjectToView);
m_window.setSource(QUrl("qrc:/Wallpaper.qml")); m_window.setSource(QUrl("qrc:/Wallpaper.qml"));
m_window.hide(); m_window.hide();
@ -285,6 +289,9 @@ struct sEnumInfo {
BOOL CALLBACK GetMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) BOOL CALLBACK GetMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{ {
Q_UNUSED(hdcMonitor)
Q_UNUSED(lprcMonitor)
auto info = (sEnumInfo*)dwData; auto info = (sEnumInfo*)dwData;
if (info->hMonitor == hMonitor) if (info->hMonitor == hMonitor)
return FALSE; return FALSE;

View File

@ -52,20 +52,37 @@
#include "basewindow.h" #include "basewindow.h"
#include "windowsdesktopproperties.h" #include "windowsdesktopproperties.h"
class WinWindow : public BaseWindow { class WinWindow : public BaseWindow {
Q_OBJECT Q_OBJECT
Q_PROPERTY(WindowsDesktopProperties* windowsDesktopProperties READ windowsDesktopProperties WRITE setWindowsDesktopProperties NOTIFY windowsDesktopPropertiesChanged)
public: public:
explicit WinWindow(const QVector<int>& activeScreensList, const QString& projectPath, const QString& id, const QString& volume, const QString& fillmode, const bool checkWallpaperVisible); explicit WinWindow(const QVector<int>& activeScreensList, const QString& projectPath, const QString& id, const QString& volume, const QString& fillmode, const bool checkWallpaperVisible);
WindowsDesktopProperties* windowsDesktopProperties() const
{
return m_windowsDesktopProperties.get();
}
public slots: public slots:
void setVisible(bool show) override; void setVisible(bool show) override;
void destroyThis() override; void destroyThis() override;
void terminate(); void terminate();
void clearComponentCache(); void clearComponentCache();
void setWindowsDesktopProperties(WindowsDesktopProperties* windowsDesktopProperties)
{
if (m_windowsDesktopProperties.get() == windowsDesktopProperties)
return;
m_windowsDesktopProperties.reset(windowsDesktopProperties);
emit windowsDesktopPropertiesChanged(m_windowsDesktopProperties.get());
}
signals:
void windowsDesktopPropertiesChanged(WindowsDesktopProperties* windowsDesktopProperties);
private: private:
void calcOffsets(); void calcOffsets();
void setupWallpaperForOneScreen(int activeScreen); void setupWallpaperForOneScreen(int activeScreen);
@ -84,5 +101,5 @@ private:
HWND m_windowHandle; HWND m_windowHandle;
HWND m_windowHandleWorker; HWND m_windowHandleWorker;
QTimer m_checkForFullScreenWindowTimer; QTimer m_checkForFullScreenWindowTimer;
WindowsDesktopProperties m_windowsDesktopProperties; std::unique_ptr<WindowsDesktopProperties> m_windowsDesktopProperties;
}; };