From ca5748e0448259475178d6244a6b233c07807de2 Mon Sep 17 00:00:00 2001
From: samuel <53528911+samuel-jimenez@users.noreply.github.com>
Date: Mon, 4 Nov 2024 08:59:44 -0600
Subject: [PATCH] Draw arcs for shadow

---
 package/contents/code/shadowcalcs.js  | 43 ---------------------------
 package/contents/config/main.xml      | 10 +++----
 package/contents/ui/LunaIcon.qml      | 38 +++++++++++------------
 package/contents/ui/configGeneral.qml | 42 ++++++++++++++++++++++++--
 4 files changed, 61 insertions(+), 72 deletions(-)
 delete mode 100644 package/contents/code/shadowcalcs.js

diff --git a/package/contents/code/shadowcalcs.js b/package/contents/code/shadowcalcs.js
deleted file mode 100644
index 1e06671..0000000
--- a/package/contents/code/shadowcalcs.js
+++ /dev/null
@@ -1,43 +0,0 @@
-/**
-
-		Copyright 2017 Bill Binder <dxtwjb@gmail.com>
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, see <http://www.gnu.org/licenses/>.
-
-*/
-
-.pragma library
-
-var rlookup;
-var radius;
-
-function sqr(x) {
-  return x * x;
-}
-
-function setup(r) {
-    radius = r;
-    rlookup = [];
-    for (var z=-r; z<=r; z++) {
-        rlookup[z] = r*Math.sqrt(1 - sqr(z*1.0/r));
-    }
-    // console.log("Radius: " + r.toString());
-    // console.log("Size:   " + rlookup.length.toString());
-
-    return radius;
-}
-
-function get(z) {
-    return rlookup[Math.abs(z)];
-}
diff --git a/package/contents/config/main.xml b/package/contents/config/main.xml
index 25a7f97..42c8a08 100644
--- a/package/contents/config/main.xml
+++ b/package/contents/config/main.xml
@@ -1,10 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
-      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-      xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
-      http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
+<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0       http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
   <kcfgfile name=""/>
-
   <group name="General">
     <entry name="latitude" type="int">
       <default>90</default>
@@ -15,6 +11,9 @@
     <entry name="showBackground" type="Bool">
       <default>false</default>
     </entry>
+    <entry name="showShadow" type="Bool">
+      <default>true</default>
+    </entry>
     <entry name="transparentShadow" type="Bool">
       <default>false</default>
     </entry>
@@ -30,5 +29,4 @@
       <default>#ffff80</default>
     </entry>
   </group>
-
 </kcfg>
diff --git a/package/contents/ui/LunaIcon.qml b/package/contents/ui/LunaIcon.qml
index 10c6867..22d2d15 100644
--- a/package/contents/ui/LunaIcon.qml
+++ b/package/contents/ui/LunaIcon.qml
@@ -21,7 +21,6 @@
 
 */
 
-import "../code/shadowcalcs.js" as ShadowCalcs
 import Qt5Compat.GraphicalEffects
 import QtQuick 2.1
 import org.kde.ksvg as KSvg
@@ -41,8 +40,7 @@ Item {
     property bool showGrid: false
     property bool showTycho: false
     property bool showCopernicus: false
-    // Degrees. 0= new moon, 90= first quarter, 180= full moon, 270= third quarter
-    property int theta: 45
+    property int theta: 45 // Degrees: 0= new moon, 90= first quarter, 180= full moon, 270= third quarter
 
     KSvg.Svg {
         id: lunaSvg
@@ -59,7 +57,6 @@ Item {
         height: Math.min(parent.width, parent.height)
         svg: lunaSvg
         // Rotation to compensate the moon's image basic position to a north pole view
-        // FIXME: Somehow it does not work when applied to OpacityMask or Blend
         transformOrigin: Item.Center
         rotation: -lunarImageTweak
     }
@@ -127,17 +124,15 @@ Item {
         onShowGridChanged: requestPaint()
         onShowTychoChanged: requestPaint()
         onShowCopernicusChanged: requestPaint()
+        onShowShadowChanged: requestPaint()
         onPaint: {
             context.reset();
             context.globalAlpha = 0.9;
             context.fillStyle = '#000000';
-            var ct = Math.cos(theta / 180 * Math.PI);
-            var radius = ShadowCalcs.setup(Math.floor(shadow.height / 2));
+            var radius = Math.floor(height / 2);
+            var cosTheta = Math.cos(theta / 180 * Math.PI);
             context.translate(radius, radius);
-            // These two determine which side of the center meridian to draw
-            // the two arcs enclosing the shadow area.
-            var terminator = (theta <= 180) ? 1 : -1;
-            var edge = (theta <= 180) ? -1 : 1;
+            var counterclockwisep = (theta < 180);
             if (lunarImage === '') {
                 context.beginPath();
                 context.fillStyle = diskColor;
@@ -146,18 +141,19 @@ Item {
                 context.fill();
             }
             if (showShadow) {
-                context.beginPath();
-                context.fillStyle = '#000000';
-                context.moveTo(ShadowCalcs.get(-radius), -radius);
-                for (var z = -radius; z < radius; z++) {
-                    context.lineTo(terminator * ShadowCalcs.get(z) * ct, z);
-                }
-                for (var z = radius; z > -radius; z--) {
-                    context.lineTo(edge * ShadowCalcs.get(z), z);
+                if (theta != 180) {
+                    context.beginPath();
+                    context.fillStyle = '#000000';
+                    context.strokeStyle = '#000000';
+                    context.arc(0, 0, radius, -0.5 * Math.PI, 0.5 * Math.PI, counterclockwisep);
+                    if ((theta % 180) != 90) {
+                        context.scale(cosTheta, 1);
+                        context.arc(0, 0, radius, 0.5 * Math.PI, -0.5 * Math.PI, counterclockwisep);
+                    }
+                    context.closePath();
+                    context.fill();
+                    context.stroke();
                 }
-                context.closePath();
-                context.stroke();
-                context.fill();
             } else {
                 // Calibration markers
                 if (showGrid)
diff --git a/package/contents/ui/configGeneral.qml b/package/contents/ui/configGeneral.qml
index 870dd33..4c1f239 100644
--- a/package/contents/ui/configGeneral.qml
+++ b/package/contents/ui/configGeneral.qml
@@ -29,6 +29,7 @@ KCM.SimpleKCM {
     id: generalPage
 
     property alias cfg_latitude: latitude.value // 0=Equator, +90=North Pole, -90=South Pole
+    property alias cfg_phase: phase.value
     property alias cfg_transparentShadow: transparentShadow.checked // boolean
     property alias cfg_showBackground: showBackground.checked // boolean
     property alias cfg_dateFormat: dateFormat.currentIndex // code: 0= 1= 2=...
@@ -37,6 +38,7 @@ KCM.SimpleKCM {
     property int cfg_lunarIndex: 0 // index into imageChoices
     property string cfg_lunarImage: '' // filename (from imageChoices)
     property int cfg_lunarImageTweak: 0 // rotation angle adjustment for the image (from imageChoices)
+    property alias cfg_showShadow: showShadow.checked
     property alias cfg_showGrid: showGrid.checked
     property alias cfg_showTycho: showTycho.checked
     property alias cfg_showCopernicus: showCopernicus.checked
@@ -46,7 +48,7 @@ KCM.SimpleKCM {
         cfg_lunarImageTweak = imageChoices.get(cfg_lunarIndex).tweak;
         if (cfg_lunarImage == '')
             cfg_transparentShadow = false;
- //transparentShadow does not work with diskColor
+
     }
 
     ImageChoices {
@@ -89,7 +91,8 @@ KCM.SimpleKCM {
                 width: 200
                 height: 200
                 latitude: cfg_latitude
-                showShadow: false
+                theta: cfg_phase
+                showShadow: cfg_showShadow
                 transparentShadow: false
                 lunarImage: cfg_lunarImage
                 lunarImageTweak: cfg_lunarImageTweak
@@ -112,6 +115,12 @@ KCM.SimpleKCM {
             QtLayouts.ColumnLayout {
                 spacing: 20
 
+                QtControls.CheckBox {
+                    id: showShadow
+
+                    text: i18n("Show shadow")
+                }
+
                 QtControls.CheckBox {
                     id: showGrid
 
@@ -200,6 +209,35 @@ KCM.SimpleKCM {
 
         }
 
+        QtControls.Label {
+            text: i18n("Phase Preview")
+            QtLayouts.Layout.preferredWidth: 85
+            horizontalAlignment: Text.AlignRight
+        }
+
+        QtLayouts.RowLayout {
+            spacing: 20
+
+            QtControls.Label {
+                id: lbl_phase
+
+                text: Math.abs(phase.value) + "º "
+                QtLayouts.Layout.preferredWidth: 40
+                horizontalAlignment: Text.AlignRight
+            }
+
+            QtControls.Slider {
+                id: phase
+
+                value: lunaPreview.theta
+                QtLayouts.Layout.fillWidth: true
+                from: 0
+                to: 360
+                stepSize: 1
+            }
+
+        }
+
         QtControls.Label {
             text: i18n("Date Format")
         }
-- 
GitLab