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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -52,20 +52,37 @@
#include "basewindow.h"
#include "windowsdesktopproperties.h"
class WinWindow : public BaseWindow {
Q_OBJECT
Q_PROPERTY(WindowsDesktopProperties* windowsDesktopProperties READ windowsDesktopProperties WRITE setWindowsDesktopProperties NOTIFY windowsDesktopPropertiesChanged)
public:
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:
void setVisible(bool show) override;
void destroyThis() override;
void terminate();
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:
void calcOffsets();
void setupWallpaperForOneScreen(int activeScreen);
@ -84,5 +101,5 @@ private:
HWND m_windowHandle;
HWND m_windowHandleWorker;
QTimer m_checkForFullScreenWindowTimer;
WindowsDesktopProperties m_windowsDesktopProperties;
std::unique_ptr<WindowsDesktopProperties> m_windowsDesktopProperties;
};