import QtQuick 2.7 import QtQuick.Controls 2.4 import QtQuick.Layouts 1.3 import QtMultimedia 5.0 import Lomiri.Components 1.3 import Lomiri.Components.Popups 1.3 import io.thp.pyotherside 1.4 MainView { id: root applicationName: "fortunecookie.darklithium" width: units.gu(45) height: units.gu(75) theme.name: "Lomiri.Components.Themes.SuruDark" // ==================================================================== // PROPERTIES // ==================================================================== property bool fortuneOpened: false property string currentFortune: "" property bool musicPlaying: false property bool appInitialized: false property real musicVolume: 0.5 property real crackVolume: 1.0 Python { id: py Component.onCompleted: { addImportPath(Qt.resolvedUrl("../src")); importModule("fortunecookie", function() { console.log("Python-Modul fortunecookie geladen"); }); } } // Funktionen function reloadFortune() { py.call("fortunecookie.get_current_fortune", [], function(result) { currentFortune = result; currentFortuneLabel.text = currentFortune; if (fortuneOpened) { currentFortuneLabel.visible = true; } }); } MediaPlayer { id: mediaPlayer objectName: "mediaPlayer" source: Qt.resolvedUrl("../assets/chinese_music.mp3") loops: MediaPlayer.Infinite volume: root.musicVolume } MediaPlayer { id: crackMediaPlayer objectName: "crackMediaPlayer" source: Qt.resolvedUrl("../assets/cookie_crack.mp3") volume: root.crackVolume } // ==================================================================== // STACK LAYOUT FÜR NAVIGATION // ==================================================================== StackLayout { id: mainStack anchors.fill: parent currentIndex: 0 // ================================================================ // SEITE 0: HAUPTSPIELBILDSCHIRM // ================================================================ Page { id: mainPage objectName: "mainPage" header: PageHeader { title: "Fortune Cookie" trailingActionBar.actions: [ Action { iconName: "settings" text: "Einstellungen" onTriggered: { mainStack.currentIndex = 1; } } ] } Component.onCompleted: { // Verzögerte Initialisierung Qt.callLater(function() { try { // Initialisierung mit async Aufrufen py.call("fortunecookie.get_initial_fortune", [], function(result) { currentFortune = result; currentFortuneLabel.text = currentFortune; cookieImage.source = Qt.resolvedUrl("../assets/cookie_closed2.png"); }); // Musik-Status laden py.call("fortunecookie.get_music_enabled", [], function(result) { musicPlaying = result; console.log("DEBUG QML: musicPlaying geladen: " + musicPlaying); if (musicPlaying) { mediaPlayer.play(); } }); // Volumes laden py.call("fortunecookie.get_music_volume", [], function(result) { root.musicVolume = result; mediaPlayer.volume = result; }); py.call("fortunecookie.get_crack_volume", [], function(result) { root.crackVolume = result; crackMediaPlayer.volume = result; }); appInitialized = true; } catch (e) { console.log("ERROR QML: Initialisierung fehlgeschlagen: " + e); } }); } Image { id: cookieImage anchors.centerIn: parent width: fortuneOpened ? units.gu(36) : units.gu(32) height: fortuneOpened ? units.gu(28) : units.gu(24) source: fortuneOpened ? Qt.resolvedUrl("../assets/cookie_open2.png") : Qt.resolvedUrl("../assets/cookie_closed2.png") fillMode: Image.PreserveAspectFit MouseArea { anchors.fill: parent property real startY: 0 onPressed: startY = mouseY onReleased: { if (mouseY < startY - units.gu(2)) { py.call("fortunecookie.open_fortune", [], function() { crackMediaPlayer.play(); fortuneOpened = true; py.call("fortunecookie.get_current_fortune", [], function(result) { currentFortune = result; currentFortuneLabel.text = currentFortune; currentFortuneLabel.visible = true; cookieImage.source = Qt.resolvedUrl("../assets/cookie_open2.png"); }); }); } } } MouseArea { anchors.fill: parent hoverEnabled: true onClicked: { if (!fortuneOpened) { py.call("fortunecookie.open_fortune", [], function() { crackMediaPlayer.play(); fortuneOpened = true; py.call("fortunecookie.get_current_fortune", [], function(result) { currentFortune = result; currentFortuneLabel.text = currentFortune; currentFortuneLabel.visible = true; cookieImage.source = Qt.resolvedUrl("../assets/cookie_open2.png"); }); }); } else { fortuneOpened = false; currentFortuneLabel.text = ""; currentFortuneLabel.visible = false; cookieImage.source = Qt.resolvedUrl("../assets/cookie_closed2.png"); } } } } Label { id: currentFortuneLabel anchors { top: cookieImage.bottom topMargin: units.gu(2) left: parent.left right: parent.right leftMargin: units.gu(2) rightMargin: units.gu(2) } text: "" fontSize: "large" horizontalAlignment: Text.AlignHCenter visible: false wrapMode: Text.WordWrap MouseArea { anchors.fill: parent onClicked: { py.call("fortunecookie.get_new_fortune", [], function() { fortuneOpened = false; py.call("fortunecookie.get_current_fortune", [], function(result) { currentFortune = result; currentFortuneLabel.text = currentFortune; cookieImage.source = Qt.resolvedUrl("../assets/cookie_closed2.png"); }); }); } } } // MUSIK-BUTTON Label { id: musicButton anchors { right: parent.right bottom: parent.bottom margins: units.gu(2) } width: units.gu(10) height: units.gu(10) text: musicPlaying ? "\uD83D\uDD0A" : "\uD83D\uDD07" fontSize: "xxx-large" horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter color: theme.palette.normalText || "white" visible: true MouseArea { anchors.fill: parent hoverEnabled: true onClicked: { console.log("DEBUG QML: Music button clicked, current musicPlaying: " + musicPlaying); if (musicPlaying) { mediaPlayer.stop(); } else { mediaPlayer.play(); } musicPlaying = !musicPlaying; console.log("DEBUG QML: Setting music enabled to: " + musicPlaying); py.call("fortunecookie.set_music_enabled", [musicPlaying]); } } } } // ================================================================ // SEITE 1: EINSTELLUNGEN // ================================================================ Page { id: settingsPage objectName: 'settingsPage' header: PageHeader { title: "Einstellungen" } Component.onCompleted: { // Verzögerte Initialisierung Qt.callLater(function() { try { // Einstellungen laden py.call("fortunecookie.get_current_fortune_list", [], function(currentFortuneList) { py.call("fortunecookie.get_music_volume", [], function(musicVol) { py.call("fortunecookie.get_crack_volume", [], function(crackVol) { // Slider Werte setzen musicVolumeSlider.value = musicVol; crackVolumeSlider.value = crackVol; // Spruchlisten ComboBox füllen py.call("fortunecookie.get_fortune_lists_with_description", [], function(lists) { fortuneListCombo.model = lists; // Aktuelle Liste auswählen for (var i = 0; i < fortuneListCombo.count; i++) { var text = fortuneListCombo.textAt(i); var listName = text.split(" - ")[0]; if (listName === currentFortuneList) { fortuneListCombo.currentIndex = i; break; } } }); }); }); }); } catch (e) { console.log("ERROR: Einstellungen nicht geladen: " + e); } }); } ColumnLayout { anchors.fill: parent anchors.margins: units.gu(2) spacing: units.gu(2) // Spacer für Header Item { Layout.fillWidth: true Layout.preferredHeight: units.gu(2) } // SPRUCHLISTEN-AUSWAHL Label { text: "Spruchliste:" Layout.fillWidth: true fontSize: "large" } ComboBox { id: fortuneListCombo Layout.fillWidth: true Layout.preferredHeight: units.gu(8) onActivated: { var newListFull = fortuneListCombo.currentText; var newList = newListFull.split(" - ")[0]; py.call("fortunecookie.set_fortune_list", [newList], function() { console.log("Spruchliste gewaehlt: " + newList); reloadFortune(); }); } } // LAUTSTÄRKE - MUSIK Label { text: "Musik-Lautstärke:" Layout.fillWidth: true fontSize: "large" } RowLayout { Layout.fillWidth: true spacing: units.gu(2) Slider { id: musicVolumeSlider Layout.fillWidth: true minimumValue: 0.0 maximumValue: 1.0 stepSize: 0.1 value: 0.5 onValueChanged: { var volume = musicVolumeSlider.value; root.musicVolume = volume; mediaPlayer.volume = volume; py.call("fortunecookie.set_music_volume", [volume]); musicVolumeLabel.text = Math.round(volume * 100) + "%"; } } Label { id: musicVolumeLabel text: Math.round(musicVolumeSlider.value * 100) + "%" width: units.gu(10) horizontalAlignment: Text.AlignHCenter fontSize: "medium" } } // LAUTSTÄRKE - KNACK-GERÄUSCH Label { text: "Knack-Lautstärke:" Layout.fillWidth: true fontSize: "large" } RowLayout { Layout.fillWidth: true spacing: units.gu(2) Slider { id: crackVolumeSlider Layout.fillWidth: true minimumValue: 0.0 maximumValue: 1.0 stepSize: 0.1 value: 1.0 onValueChanged: { var volume = crackVolumeSlider.value; root.crackVolume = volume; crackMediaPlayer.volume = volume; py.call("fortunecookie.set_crack_volume", [volume]); crackVolumeLabel.text = Math.round(volume * 100) + "%"; } } Label { id: crackVolumeLabel text: Math.round(crackVolumeSlider.value * 100) + "%" width: units.gu(10) horizontalAlignment: Text.AlignHCenter fontSize: "medium" } } // ZURÜCK-BUTTON Item { Layout.fillWidth: true Layout.preferredHeight: units.gu(10) } Button { text: "Zurück" Layout.fillWidth: false Layout.preferredWidth: units.gu(20) Layout.preferredHeight: units.gu(8) Layout.alignment: Qt.AlignHCenter onClicked: { mainStack.currentIndex = 0; } } } } } }