From 1d61d5209893b70fdad8923e248788b12717274c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konsta=20Alaj=C3=A4rvi?= Date: Tue, 4 Jun 2024 10:32:26 +0300 Subject: [PATCH] Add another QML Component into qml_in_kotlin_based_android_project Add implementation for second qml component in qml_in_kotlin_based_android_project Task-number: QTBUG-125099 Pick-to: 6.8 Change-Id: Id0b6c693aac2d67cceda74e623fcd18c9568b84a Reviewed-by: Ville Voutilainen --- .../MainActivity.kt | 36 ++++++++++++++++++- .../app/src/main/res/layout/activity_main.xml | 36 ++++++++++++++++++- .../app/src/main/res/values/strings.xml | 3 ++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/java/com/example/qml_in_kotlin_based_android_project/MainActivity.kt b/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/java/com/example/qml_in_kotlin_based_android_project/MainActivity.kt index 6c631822f4..cf2a27035a 100644 --- a/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/java/com/example/qml_in_kotlin_based_android_project/MainActivity.kt +++ b/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/java/com/example/qml_in_kotlin_based_android_project/MainActivity.kt @@ -5,16 +5,19 @@ import android.graphics.Color import android.os.Bundle import android.util.DisplayMetrics import android.util.Log +import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import android.widget.LinearLayout import androidx.appcompat.app.AppCompatActivity import com.example.qml_in_kotlin_based_android_project.databinding.ActivityMainBinding import org.qtproject.example.qml_in_android_view.QmlModule.Main +import org.qtproject.example.qml_in_android_view.QmlModule.Second import org.qtproject.qt.android.QtQmlStatus import org.qtproject.qt.android.QtQmlStatusChangeListener import org.qtproject.qt.android.QtQuickView + class MainActivity : AppCompatActivity(), QtQmlStatusChangeListener { private val TAG = "myTag" @@ -25,6 +28,7 @@ class MainActivity : AppCompatActivity(), QtQmlStatusChangeListener { //! [qmlComponent] private var m_mainQmlComponent: Main = Main() //! [qmlComponent] + private val m_secondQmlComponent: Second = Second() private val m_statusNames = hashMapOf( QtQmlStatus.READY to "READY", QtQmlStatus.LOADING to "LOADING", @@ -51,6 +55,7 @@ class MainActivity : AppCompatActivity(), QtQmlStatusChangeListener { //! [setStatusChangeListener] m_mainQmlComponent.setStatusChangeListener(this) //! [setStatusChangeListener] + m_secondQmlComponent.setStatusChangeListener(this) //! [layoutParams] val params: ViewGroup.LayoutParams = FrameLayout.LayoutParams( @@ -63,6 +68,9 @@ class MainActivity : AppCompatActivity(), QtQmlStatusChangeListener { //! [loadComponent] m_binding.changeColorButton.setOnClickListener { onClickListener() } + m_binding.loadMainQml.setOnClickListener { loadMainQml() } + m_binding.loadSecondQml.setOnClickListener { loadSecondQml() } + m_binding.rotateQmlGridButton.setOnClickListener { rotateQmlGrid() } // Check target device orientation on launch handleOrientationChanges() @@ -110,7 +118,10 @@ class MainActivity : AppCompatActivity(), QtQmlStatusChangeListener { m_binding.getPropertyValueText.text = qmlBackgroundColor // Display the QML View background color in a view - m_binding.colorBox.setBackgroundColor(Color.parseColor(qmlBackgroundColor)) + // if qmlBackgroundColor is not null + if (qmlBackgroundColor != null) { + m_binding.colorBox.setBackgroundColor(Color.parseColor(qmlBackgroundColor)) + } } //! [onClickListener] @@ -165,4 +176,27 @@ class MainActivity : AppCompatActivity(), QtQmlStatusChangeListener { //! [qml signal listener] } //! [onStatusChanged] + + private fun loadSecondQml() { + m_qtQuickView!!.loadComponent(m_secondQmlComponent) + + // Reset box color and color text after component reload + m_binding.colorBox.setBackgroundColor(Color.parseColor("#00ffffff")) + m_binding.getPropertyValueText.text = "" + } + + private fun loadMainQml() { + m_qtQuickView!!.loadComponent(m_mainQmlComponent) + + // Reset box color and color text after component reload + m_binding.colorBox.setBackgroundColor(Color.parseColor("#00ffffff")) + m_binding.getPropertyValueText.text = "" + } + + private fun rotateQmlGrid() { + val previousGridRotation = m_secondQmlComponent.gridRotation + if (previousGridRotation != null) { + m_secondQmlComponent.gridRotation = previousGridRotation + 45 + } + } } diff --git a/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/res/layout/activity_main.xml b/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/res/layout/activity_main.xml index 03f01fdd25..b08cebf9c0 100644 --- a/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/res/layout/activity_main.xml +++ b/examples/platforms/android/qml_in_kotlin_based_android_project/app/src/main/res/layout/activity_main.xml @@ -121,7 +121,7 @@ @@ -159,6 +159,40 @@ android:layout_weight="0"/> + + +